fix(follow-link): two-step <CR> — first wraps, second follows
User feedback: the smart `<CR>` should give the user a chance to
review/edit the freshly-created wikilink before committing to the
follow. Matches upstream vimwiki's flow.
`follow_link_or_create` (Lua + VimL) now:
1. Cursor already inside `[[…]]` → follow immediately.
2. Cursor on a bare word → wrap it as `[[word]]`, place cursor
inside, and STOP. A second `<CR>` then follows.
3. Neither — fall through to plain definition request so users
can still chord follow without a target word.
Verified: cursor on `MyPage rest` →
1st `<CR>` → `[[MyPage]] rest` (buffer unchanged)
2nd `<CR>` → opens `MyPage.wiki` (the synthesised future page).
381 tests still pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -144,16 +144,28 @@ function! s:wrap_cword_as_wikilink() abort
|
|||||||
return 1
|
return 1
|
||||||
endfunction
|
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
|
function! nuwiki#commands#follow_link_or_create() abort
|
||||||
if !s:cursor_inside_wikilink()
|
if s:cursor_inside_wikilink()
|
||||||
call s:wrap_cword_as_wikilink()
|
if exists(':LspDefinition') == 2
|
||||||
|
LspDefinition
|
||||||
|
else
|
||||||
|
echohl WarningMsg
|
||||||
|
echom 'nuwiki: :LspDefinition unavailable; install vim-lsp'
|
||||||
|
echohl None
|
||||||
|
endif
|
||||||
|
return
|
||||||
endif
|
endif
|
||||||
|
if s:wrap_cword_as_wikilink()
|
||||||
|
" Wrapped — wait for the user's second <CR> to follow.
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
" Nothing to wrap — fall through.
|
||||||
if exists(':LspDefinition') == 2
|
if exists(':LspDefinition') == 2
|
||||||
LspDefinition
|
LspDefinition
|
||||||
else
|
|
||||||
echohl WarningMsg
|
|
||||||
echom 'nuwiki: :LspDefinition unavailable; install vim-lsp'
|
|
||||||
echohl None
|
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|||||||
+16
-5
@@ -159,13 +159,24 @@ local function wrap_cword_as_wikilink()
|
|||||||
return true
|
return true
|
||||||
end
|
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()
|
function M.follow_link_or_create()
|
||||||
if not cursor_inside_wikilink() then
|
if cursor_inside_wikilink() then
|
||||||
if not wrap_cword_as_wikilink() then
|
vim.lsp.buf.definition()
|
||||||
-- Not on a word either — fall through to plain definition; the
|
return
|
||||||
-- LSP will simply do nothing if there's no target.
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
if wrap_cword_as_wikilink() then
|
||||||
|
-- Wrapped — leave the user inside the new `[[…]]` and stop. A
|
||||||
|
-- second `<CR>` follows it, matching vimwiki's review-then-follow
|
||||||
|
-- flow.
|
||||||
|
return
|
||||||
|
end
|
||||||
|
-- No word under cursor either — fall through.
|
||||||
vim.lsp.buf.definition()
|
vim.lsp.buf.definition()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user