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

Better way to cancel the "session". #872

Open
allworldg opened this issue Apr 22, 2023 · 14 comments
Open

Better way to cancel the "session". #872

allworldg opened this issue Apr 22, 2023 · 14 comments

Comments

@allworldg
Copy link

allworldg commented Apr 22, 2023

I have read #258 and #770. Create a autocmd or set region_check_events to cancel the "session" is great. But if I create a big snippet, I think region_check_events cannot solve the "cancel" problem.

For example, I use html5~ snippet to create a basic html structure, so I will always code in the <body></body> (in the snippet area). I move the cursor in <body></body> then press Tab . the cursor still jump to <title></title>(the placeholder position). It's still annoyed.

Is there any way to make luasnip cancel the "session" by detecting if cursor move out of the placeholder region (not just snippet region) ?

Like vscode ("edit,change the mode,move the cursor" will not cancel the session , but move cursor out of the placeholder region will do.)
vscodeSnippet2

@L3MON4D3
Copy link
Owner

I think your desired behaviour should be history = false, without any further modifications. Does that work?

@allworldg
Copy link
Author

allworldg commented Apr 22, 2023

luasnip.setup({
  history = false
})

It doesn't seem to solve the problem. I guess because it does not exit the snippets.
neovimSnippet

@L3MON4D3
Copy link
Owner

Okay, history=false only takes care of the case you mentioned above, where all the placeholders were visited, for removing the snippet on leaving the snippet-region region_check_events has to be enabled still :/
Reducing the region to only the editable placeholders seems much more complicated..

@allworldg
Copy link
Author

allworldg commented Apr 22, 2023

It looks same with the problem, only useful when cursor skip the whole snippet region.
I'm just curious about this solution.
Thank you :)

@XXiaoA
Copy link

XXiaoA commented Aug 16, 2023

If the feature can be achieve, coding must be more fluent. :)

@bew
Copy link
Contributor

bew commented Sep 3, 2023

In Ultisnips we can use $0 placeholder to tell the engine what is the 'exit point', meaning we're not in a snippet anymore.

Could we have a kind of 'exit node' like this that cancels the current snippet when reached from a jump?

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Sep 3, 2023

Mhmm, yeah that could be implemented with a separate option.. should be worth it if ultisnips behaves like that (and they terminate snippets when the overall region is left, right?)

Regarding the original request, disabling the snippet if the cursor is outside all of the placeholders (that's it, right?), should also be possible, maybe with the same option.

@bew
Copy link
Contributor

bew commented Sep 3, 2023

Mhmm, yeah that could be implemented with a separate option.. should be worth it if ultisnips behaves like that (and they terminate snippets when the overall region is left, right?)

If I remember correctly no, the snippet is exited when reaching $0, by default it is placed at the very end of the snippet bu we can place it anywhere to exit the snippet early, even if the cursor is still in the snippet area.
For example:

snippet fn "desc" b
function $1(${2:args})
    $0  -- exits on first jump to this placeholder, we can't use ultisnip jump mappings anymore
end
endsnippet

Seems to be handled here:
https://github.com/SirVer/ultisnips/blob/24a3ebb36687b6d59a19d63173713575b486d739/pythonx/UltiSnips/snippet_manager.py#L600-L601
(placeholders are called tabstops in ultisnips)
When jumping, if the tabstop is 0, mark the snippet as done (exit it)

@MariaSolOs
Copy link

@L3MON4D3 I'm curious but is #941 also fixing this?

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Oct 4, 2023

Ah, mh... Maybe the underlying issue: one thing that is possible now is always activating the node the cursor is at right now (via ls.activate_node, and an autocommand with events like insertenter or CursorMovedI).
That would, I think, solve this issue for snippets where there is one big $0 where lots of text will be put, since that would be activated upon moving the cursor into it, and then the next jump will do nothing since the end of the snippet is already reached.

@MariaSolOs
Copy link

@L3MON4D3 so would you suggest using ls.activate_node instead of an autocommand like this?

Sorry for the silly questions, I'm going over the "Snippet Insertion" docs and I'm just trying to wrap my head around the new settings 😅

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Oct 4, 2023

Naaah, that autocommand should also work fine. With it, you'll deactivate the snippet as soon as InsertLeave happens, with actvate_node you'll always enter the node your cursor is on, which prevents this rather specific issue because the node the cursor is in is always the node that is "logically" active for luasnip, which has tge effect that jumps will generally not move back in the buffer, except if that is actually desired in the snippet.
Hope that makes sense😅
Definitely ask if not, I think I have to write more about how jumps work anyway :D

@MariaSolOs
Copy link

@L3MON4D3 that does help! My snippets aren't very complex, so I'm not looking for powerful snippet navigation. What I've just been struggling with is unlinking the snippet once my cursor has left the jumpable nodes, even if some of them remain unvisited.

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Oct 4, 2023

Nice :)
Seems like that would only need api for

  • getting the innermost snippet the cursor is inside,
  • finding the node of a snippet at the position of the cursor, and
  • unlinking some arbritary snippet (don't think snippet:remove_from_jumplist() is officially exposed yet),

all of which is already implemented (at least in some form, maybe not a very nice api yet :D ), and would only need some official documentation

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

No branches or pull requests

5 participants