tamago324 / lir.nvim

Neovim file explorer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Path issues on OS:Windows

DrKGD opened this issue · comments

Hi there!
I just started using this plugin but some of the default actions are not working properly on OS:Windows, I suspect paths are not handled properly.
In particular require('lir.actions').up is not working at all. As my understandings of it, it should go up one level in the folder hierarchy (thus doing something like cd ..).

I gave a quick look to the function and i found out that the name variable is always empty, it HAS to do with Windows slash notation.

function actions.up()
  local cur_file, path, name, dir
  local ctx = get_context()
  cur_file = ctx:current_value()
  path = string.gsub(ctx.dir, "/$", "")
  name = vim.fn.fnamemodify(path, ":t") <--- This line
  if name == "" then
    return
  end
  ...

As my current vimscripting knowledge, name should be containing the current folder/file name underneath the cursor, but debugging with primordial tools (print) it is actually nil, so the function returns.

Turns out the ctx.dir is ill-formed, it always ends in \/ (e.g. ~\.config\nvim\lua\/), thus it detects no :t(ail) on the vim.fn.fnamemodify.

A dirty quickfix to this very function is literally repeat the string.gsub twice
path = string.gsub(string.gsub(ctx.dir, "/$", ""), "\\$", "")

Other actions are affected as well, not as severly (thus still working properly!), for example when using require('lir.actions').edit the echo'd pattern has an extra right slant in the name

immagine

Thanks!

Thanks for reporting this!
I haven't used Windows in a long time, so your report was a huge help!

I've created a #37 and would like to know what you think.

I am experiencing this issue with Windows and the lir from commit f10fd0d. Same root cause as described above. The ctx.dir ends with /\. I have the option shellslash set to true in my configs. If set to false, then the issue is not present. Adding the following lines to actions.up() fixes it for me:

  if (vim.fn.has('win32') or vim.fn.has('win64')) and vim.o.shellslash then
      path = string.gsub(path, "/$", "")
  end

The final function looks like the following:

actions.lua

function actions.up()
  local cur_file, path, name, dir
  local ctx = get_context()
  cur_file = ctx:current_value()
  path = string.gsub(ctx.dir, sep .. "$", "")

  if (vim.fn.has('win32') or vim.fn.has('win64')) and vim.o.shellslash then
      path = string.gsub(path, "/$", "")
  end

  name = vim.fn.fnamemodify(path, ":t")
  if name == "" then
    return
  end
  dir = vim.fn.fnamemodify(path, ":p:h:h")
  history.add(path, cur_file)
  history.add(dir, name)
  vim.cmd("keepalt edit " .. dir)
  if is_root(dir) then
    vim.cmd("doautocmd BufEnter")
  end
end

Seems like there is a better way to fix the issue.