Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(command): not being used if provided manually #21

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use {
```

## Usage
execute in command mode
execute in command mode or add your custom key map
```
:RunThis or :Runthis attach //will auto run the current buf if just 1 open

```

you'll be prompted the following
Expand Down Expand Up @@ -86,13 +87,7 @@ local defaults = {

## NOTE

1. I have to modify the plugin to allow change the compile process
1. I do not usually code with compiled languages so I don't think I'll tweak much the plugin for compiled languages,feel free to make pull requests
2. Obviously you cannot handle stdin using this plugin, at least not yet

## TODO

- [ ] rewrite prompt to allow autocompletion for path and commands
- [ ] track if the name of the file changes
- [ ] add a `attach/detach current` command
- [x] add configs to change the size of the plugin buffer, default runables, etc...
- [x] make possible use the plugin with compiled languages
3. If you want to run code selection you can use the built-in method
`vim.api.nvim_set_keymap('v', '<leader>r', ':w !python3<CR>', {noremap = true})`
24 changes: 17 additions & 7 deletions lua/runthis/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ local utils = require("runthis.utils")
local stateModule = require("runthis.state")
local state = stateModule.REF

-- BUG: if window closed and detached, when attaching again window not displayed
-- TODO: track if the name of the file changes
-- TODO: maybe detect deno' tasks

M.runAbles = {
["py"] = "python3",
["js"] = "node",
Expand Down Expand Up @@ -53,6 +49,13 @@ function M.attach_to_buf(command, client)

local auGroup = v.nvim_create_augroup("AutoRun " .. name, { clear = true })

v.nvim_create_autocmd({ "BufDelete" }, {
desc = "In case the buf is closed, the tracking will be closed too",
group = auGroup,
callback = function()
M.detach_buf(name)
end,
})
v.nvim_create_autocmd({ "BufWritePost" }, {
desc = "Creates a buffer to show what's executed on save for the given buf as client",
group = auGroup,
Expand Down Expand Up @@ -98,10 +101,17 @@ function M.attach_to_buf(command, client)
end

-- Write this while it is still loading or whatever is doing
v.nvim_buf_set_lines(pluginBufnr, 0, -1, false, { "🔎 Loading your File, please wait... " })
v.nvim_buf_set_lines(pluginBufnr, 0, -1, false, {
"🔎 Loading your File, please wait...",
"if it hangs out here, check if you program really returns anything to stdout",
})

fn.jobstart(utils.toTable(finalCommand), {
stdout_buffered = true,
on_stdin = function(job_id, _)
local user_input = vim.fn.input("Enter input: ")
vim.fn.jobsend(job_id, user_input .. "\n")
end,
on_stdout = handleStdout,
on_stderr = handleStdout,
-- INFO: solves deno color characters not being parsed
Expand All @@ -111,7 +121,8 @@ function M.attach_to_buf(command, client)
-- Save the state for this buf
state[pluginBufnr] = {
win = winnr,
client = name,
-- file = v.nvim_buf_get_name(clientBuf), -- Store the file name
client = vim.fn.fnamemodify(v.nvim_buf_get_name(clientBuf), ":t"),
}
end,
pattern = name,
Expand All @@ -134,7 +145,6 @@ function M.prompt(t)
local task = t.args
-- Split by whitespaces
local Ttask = utils.toTable(task, "%S+")
-- Case no arguments, then attach
local selectedBuf
local bufList = utils.getBufList()

Expand Down
58 changes: 32 additions & 26 deletions lua/runthis/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,45 @@ function M.toTable(str, pattern)
return t
end

local compilers = {
java = function(absPath)
vim._system(("javac %s"):format(absPath))
return "./" .. absPath:sub(1, absPath:find(".java") - 1)
end,
c = function(absPath)
vim._system(("clang %s -o %s"):format(absPath, "main"))
return "./main"
end,
cpp = function(absPath)
vim._system(("clang %s -o %s"):format(absPath, "main"))
return "./main"
end,
rs = function(absPath)
vim._system(("rustc -C linker=clang %s"):format(absPath))
return "./" .. absPath:sub(1, absPath:find(".rs") - 1)
end,
}

--- @return string command, string? origialCommand What will be ran on the file
function M.parseCommand(client, command, runAbles)
local auxCommand = command
local firstLine = v.nvim_buf_get_lines(client.buf, 0, 1, true)[1]
local absPath = "/" .. client.data.path
local extension = absPath:sub(-absPath:reverse():find("%.") + 1, -1)
-- case empty command check if shebang exists
if #auxCommand == 0 and firstLine:sub(1, 2) == "#!" then
auxCommand = firstLine:sub(3, -1)
-- Remove the shebang command
-- e.g /usr/bin/env python3 -> python3
auxCommand = auxCommand:sub((auxCommand:find(" ") or 0) + 1, -1)
-- otherwise try to find a possible executable
elseif runAbles[extension] then
local runable = runAbles[extension]
auxCommand = runable
else
return "echo 'No executable found for this file'"
end

-- Apply the compiling phase if needed
if extension == "java" then
vim._system(("javac %s"):format(absPath))
return auxCommand .. " " .. absPath:sub(1, absPath:find(".java") - 1)
elseif extension == "c" or extension == "cpp" then
vim._system(("clang %s -o %s"):format(absPath, "main"))
return auxCommand
elseif extension == "rs" then
vim._system(("rustc -C linker=clang %s"):format(absPath))
return "./" .. absPath:sub(1, absPath:find(".rs") - 1)
if #command == 0 then
if v.nvim_buf_get_lines(client.buf, 0, 1, true)[1]:sub(1, 2) == "#!" then
command = v.nvim_buf_get_lines(client.buf, 0, 1, true)[1]:sub(3, -1)
elseif not runAbles[extension] then
return "echo 'No executable found for this file'"
else
command = runAbles[extension]
end
end

return auxCommand .. " " .. absPath, auxCommand
if compilers[extension] then
return compilers[extension](absPath), command
else
return command .. " " .. absPath, command
end
end

function M.bufExists(name, ref)
Expand Down
6 changes: 6 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}
2 changes: 1 addition & 1 deletion test/test.js → test/testd.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
console.log(2+5)
console.log(2+11)
console.log([1,2,3])
console.log("Hi there!")