diff --git a/autoload/nuwiki/commands.vim b/autoload/nuwiki/commands.vim index c32c281..daaedbf 100644 --- a/autoload/nuwiki/commands.vim +++ b/autoload/nuwiki/commands.vim @@ -144,16 +144,28 @@ function! s:wrap_cword_as_wikilink() abort return 1 endfunction +" Two-step behaviour matching vimwiki: +" 1. First press on a bare word → wrap it as `[[word]]` and stop. +" 2. Cursor is now inside `[[…]]`; next press follows the link. +" 3. Press inside an existing `[[…]]` follows immediately. function! nuwiki#commands#follow_link_or_create() abort - if !s:cursor_inside_wikilink() - call s:wrap_cword_as_wikilink() + if s:cursor_inside_wikilink() + if exists(':LspDefinition') == 2 + LspDefinition + else + echohl WarningMsg + echom 'nuwiki: :LspDefinition unavailable; install vim-lsp' + echohl None + endif + return endif + if s:wrap_cword_as_wikilink() + " Wrapped — wait for the user's second to follow. + return + endif + " Nothing to wrap — fall through. if exists(':LspDefinition') == 2 LspDefinition - else - echohl WarningMsg - echom 'nuwiki: :LspDefinition unavailable; install vim-lsp' - echohl None endif endfunction diff --git a/lua/nuwiki/commands.lua b/lua/nuwiki/commands.lua index 9db0d2f..49e760e 100644 --- a/lua/nuwiki/commands.lua +++ b/lua/nuwiki/commands.lua @@ -159,13 +159,24 @@ local function wrap_cword_as_wikilink() return true end +--- Two-step behaviour matching vimwiki: +--- 1. First press on a bare word → wrap it as `[[word]]` and stop. +--- 2. Cursor is now inside `[[…]]`; next press follows the link. +--- 3. Press inside an existing `[[…]]` follows immediately. +--- "Not on a word" falls through to plain definition so users can +--- still chord a definition request anywhere. function M.follow_link_or_create() - if not cursor_inside_wikilink() then - if not wrap_cword_as_wikilink() then - -- Not on a word either — fall through to plain definition; the - -- LSP will simply do nothing if there's no target. - end + if cursor_inside_wikilink() then + vim.lsp.buf.definition() + return end + if wrap_cword_as_wikilink() then + -- Wrapped — leave the user inside the new `[[…]]` and stop. A + -- second `` follows it, matching vimwiki's review-then-follow + -- flow. + return + end + -- No word under cursor either — fall through. vim.lsp.buf.definition() end