R-nvim / R.nvim

Neovim plugin to edit R files
GNU General Public License v3.0
155 stars 16 forks source link

Use utils.system() instead of vim.system() #41

Closed hongyuanjia closed 7 months ago

hongyuanjia commented 7 months ago

Pull request overview

jalvesaq commented 7 months ago

It's working for me after these changes:

diff --git a/lua/r/utils.lua b/lua/r/utils.lua
index f3267f3..6993046 100644
--- a/lua/r/utils.lua
+++ b/lua/r/utils.lua
@@ -193,8 +193,8 @@ function M.system(cmd, opts)
         {
             args = vim.list_slice(cmd, 2),
             stdio = { nil, stdout, stderr },
-            cwd = opts.cwd,
-            detach = opts.detach,
+            cwd = (opts and opts.cwd) and opts.cwd or ".",
+            detach = (opts and opts.detach) and opts.detach or false,
             hide = true
         },
         function(code, signal)
@@ -216,7 +216,7 @@ function M.system(cmd, opts)
             if err then error(err) end

             if data ~= nil then
-                if opts.text then
+                if opts and opts.text then
                     data = data:gsub("\r\n", "\n")
                     table.insert(store, data)
                 else
@@ -235,7 +235,7 @@ function M.system(cmd, opts)
     end
     if stderr then
         stderr_data = {}
-        stderr:read_start(stdio_handler(state.stderr, stdout_data))
+        stderr:read_start(stdio_handler(state.stderr, stderr_data))
     end

     local methods = {}
@@ -248,6 +248,7 @@ function M.system(cmd, opts)
             local err = string.format("Command timed out: %s", table.concat(cmd, " "))
             return { code = 0, signal = 2, stdout = "", stderr = err }
         end
+        return state.result
     end

     return setmetatable({ pid = state.pid, _state = state }, { __index = methods })
jalvesaq commented 7 months ago

I'm not sure if using "." as the default value for cwd is correct...

hongyuanjia commented 7 months ago

Thanks for the catch!

hongyuanjia commented 7 months ago

I still lack a proper setup for developing Vim/Lua plugins. I need to learn how to test submodules despite relaunching Neovim.

hongyuanjia commented 7 months ago

I'm not sure if using "." as the default value for cwd is correct...

Will nil give an error? If not, I think we can leave it as is to follow the same behavior as vim.system()?

jalvesaq commented 7 months ago

It's working on Linux. Can I merge it now?

hongyuanjia commented 7 months ago

I checked it on Windows and macOS. Both work ok. I think we can merge it.