willothy / flatten.nvim

Open files and command output from wezterm, kitty, and neovim terminals in your current neovim instance
https://luarocks.org/modules/willothy/flatten.nvim
MIT License
473 stars 13 forks source link

`nvim --cmd 'let g:flatten_wait=1' file1` does not work, neither does `-b` #95

Open simonhughxyz opened 6 months ago

simonhughxyz commented 6 months ago

Describe the bug None of the blocking options work

To Reproduce Steps to reproduce the behavior:

  1. use either of the 2 blocking commands

Expected behavior To allow nested instances

Screenshots NA

Minimal Init

{
    "willothy/flatten.nvim",
    opts = function()
      ---@type Terminal?
      local saved_terminal

      return {
        window = {
          open = "alternate",
        },
        callbacks = {
          should_block = function(argv)
            -- Note that argv contains all the parts of the CLI command, including
            -- Neovim's path, commands, options and files.
            -- See: :help v:argv

            -- In this case, we would block if we find the `-b` flag
            -- This allows you to use `nvim -b file1` instead of
            -- `nvim --cmd 'let g:flatten_wait=1' file1`
            return vim.tbl_contains(argv, "-b")

            -- Alternatively, we can block if we find the diff-mode option
            -- return vim.tbl_contains(argv, "-d")
          end,
          pre_open = function()
            local term = require("toggleterm.terminal")
            local termid = term.get_focused_id()
            saved_terminal = term.get(termid)
          end,
          post_open = function(bufnr, winnr, ft, is_blocking)
            if is_blocking and saved_terminal then
              -- Hide the terminal while it's blocking
              saved_terminal:close()
            else
              -- If it's a normal file, just switch to its window
              vim.api.nvim_set_current_win(winnr)
            end

            -- If the file is a git commit, create one-shot autocmd to delete its buffer on write
            -- If you just want the toggleable terminal integration, ignore this bit
            if ft == "gitcommit" or ft == "gitrebase" then
              vim.api.nvim_create_autocmd("BufWritePost", {
                buffer = bufnr,
                once = true,
                callback = vim.schedule_wrap(function()
                  vim.api.nvim_buf_delete(bufnr, {})
                end),
              })
            end
          end,
          block_end = function()
            -- After blocking ends (for a git commit, etc), reopen the terminal
            vim.schedule(function()
              if saved_terminal then
                saved_terminal:open()
                saved_terminal = nil
              end
            end)
          end,
        },
      }
    end,
  },

Desktop (please complete the following information):

Additional context I'm using toggle term

willothy commented 6 months ago

Mind providing some more info / filling out the bug report form properly?

I can't really debug this if the only info I have is "it doesn't work."

simonhughxyz commented 6 months ago

Mind providing some more info / filling out the bug report form properly?

I can't really debug this if the only info I have is "it doesn't work."

Sorry, I accidentally pressed save before I was finished

willothy commented 6 months ago

No worries, thanks for the info!

I'll look into this ASAP :)

willothy commented 6 months ago

Ah, so you're expecting flatten to allow a nested instance when you use nvim -b? That's not the intended behavior - what should happen here is that the files will be opened in your current instance, and the nested instance will block until the file is closed.

simonhughxyz commented 6 months ago

Ah, so you're expecting flatten to allow a nested instance when you use nvim -b? That's not the intended behavior - what should happen here is that the files will be opened in your current instance, and the nested instance will block until the file is closed.

Ok I misunderstood that, but even that functionality does not work. There is no blocking at all.

To be clear, I am trying to use it with password-store to edit my pass files, I thought the -b option was supposed to solve that issue.

simonhughxyz commented 6 months ago

When running EDITOR="nvim --cmd 'let g:flatten_wait=1'" pass edit test neovim returns a bunch of errors:

Error detected while processing pre-vimrc command line:
E20: Mark not set: 'let
[null-ls] failed to load builtin eslint for method diagnostics; please check your config
Error detected while processing BufEnter Autocommands for "*":
Error executing lua callback: Error executing lua: ....local/share/nvim/lazy/flatten.nvim/lua/flatten/core.lua:125: nvim_exec2(): Vim:E
20: Mark not set: 'let
stack traceback:
        [C]: in function 'nvim_exec2'
        ....local/share/nvim/lazy/flatten.nvim/lua/flatten/core.lua:125: in function <....local/share/nvim/lazy/flatten.nvim/lua/flatte
n/core.lua:93>
stack traceback:
        [C]: in function 'exec_on_host'
        ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:59: in function 'send_files'
        ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:149: in function <...local/share/nvim/lazy/flatten.nvim/lua/flatten
/guest.lua:99>
willothy commented 6 months ago

Hmm, it looks like the command isn't being passed properly, but I should look into those errors too. Have you tried it with the environment variable set beforehand?

Like

export EDITOR='nvim --cmd "let g:flatten_wait=1"'
pass edit test
willothy commented 6 months ago

Also, I think you should be using $VISUAL for this, since that is the editor that will be used for blocking commands as far as I know.

simonhughxyz commented 6 months ago

Does not work with export or $VISUAL. However the error only appears when using EDITOR="nvim --cmd 'let g:flatten_wait=1'" pass edit test the other options don't produce an error, the file opens but the command continues to run with to blocking.

simonhughxyz commented 6 months ago

Setting let g:flatten_wait=1 within the root terminal manually does not work either.

willothy commented 6 months ago

Ok, can repro using pass specifically. I will look into this more now, thanks for clarifying!

pass seems to user $EDITOR, not $VISUAL unlike git and some others.

simonhughxyz commented 6 months ago

Also, just a suggestion but it would be nice to also have a option to allow nesting when needed

willothy commented 6 months ago

Try EDITOR='nvim -b' pass edit test. That works for me. I will look into this... it's confusing because usually blocking edits use $VISUAL in my experience...

willothy commented 6 months ago

Also, just a suggestion but it would be nice to also have a option to allow nesting when needed

There is :) Check out the nest_if_no_args option and should_nest() hook.

simonhughxyz commented 6 months ago

Try EDITOR='nvim -b' pass edit test. That works for me. I will look into this... it's confusing because usually blocking edits use $VISUAL in my experience...

It works but before I close the buffer the terminal buffer has this error:

[null-ls] failed to load builtin eslint for method diagnostics; please check your config
Error detected while processing BufEnter Autocommands for "*":
Error executing lua callback: ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:25: Vim:Error executing vim.schedule lua call
back: vim/_editor.lua:0: Vim:Failed to run `config` for nvim-dap
stack traceback:
        [C]: in function 'nvim_exec2'
        vim/_editor.lua: in function 'cmd'
        ...vim/lazy/todo-comments.nvim/lua/todo-comments/config.lua:187: in function 'colors'
        ...vim/lazy/todo-comments.nvim/lua/todo-comments/config.lua:133: in function '_setup'
        ...vim/lazy/todo-comments.nvim/lua/todo-comments/config.lua:92: in function ''
        vim/_editor.lua: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
        [C]: in function 'sleep'
        ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:25: in function 'maybe_block'
        ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:60: in function 'send_files'
        ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:149: in function <...local/share/nvim/lazy/flatten.nvim/lua/flatten
/guest.lua:99>
stack traceback:
        [C]: in function 'sleep'
        ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:25: in function 'maybe_block'
        ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:60: in function 'send_files'
        ...local/share/nvim/lazy/flatten.nvim/lua/flatten/guest.lua:149: in function <...local/share/nvim/lazy/flatten.nvim/lua/flatten
/guest.lua:99>
Error executing lua callback: .../.local/share/nvim/lazy/neorg/lua/neorg/core/modules.lua:754: Invalid window id: -1
stack traceback:
        [C]: in function 'nvim_win_get_cursor'
        .../.local/share/nvim/lazy/neorg/lua/neorg/core/modules.lua:754: in function 'create_event'
        ...azy/neorg/lua/neorg/modules/core/autocommands/module.lua:49: in function '_neorg_module_autocommand_triggered'
        ...azy/neorg/lua/neorg/modules/core/autocommands/module.lua:77: in function <...azy/neorg/lua/neorg/modules/core/autocommands/m
odule.lua:76>
willothy commented 6 months ago

Hmm, alright. That might be tough for me to repro because it's largely errors from other plugins, but I will try. Seems like they don't like the editor actually blocking. Maybe I can just make it wait without blocking instead.

Btw I will probably make any future changes to the 1.0-dev branch, so I would suggest using that branch for now if you're comfortable updating your config. Configuration in that branch isn't likely to change much after the breaking changes that I've already made, but the features I want for 1.0 aren't fully complete yet. Config is fully typed and documented in that branch, so should make things easier if you're using lua_ls.

I can't promise there there won't be breaking changes on that branch until it's merged though, so up to you if you want to switch of course.

ragu-manjegowda commented 5 months ago

https://github.com/willothy/flatten.nvim/issues/95#issuecomment-1979816682

Not sure if related, just wanted to share this with you guys, perhaps it might help!

I faced this exact error whenever I tried to open a file from ranger.

Ranger has something like this to open text files -> ${VISUAL:-$EDITOR} -- "$@"

Issue is that, nvim --cmd 'let g:flatten_wait=1' this whole thing was getting treated as command instead of command + arg.

So I had to change my config to -> eval ${VISUAL:-$EDITOR} -- "$@" so that only nvim is treated as command and everything else as argument.