Skip to content

Commit

Permalink
extract: fix line selection
Browse files Browse the repository at this point in the history
Fix line selection for extraction.

Fixes #3615
  • Loading branch information
bhcleek committed Dec 24, 2023
1 parent 425784a commit dc6a084
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
10 changes: 2 additions & 8 deletions autoload/go/extract.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
let s:cpo_save = &cpo
set cpo&vim

function! go#extract#Extract(selected) abort
function! go#extract#Extract(line1, line2) abort
if !go#config#GoplsEnabled()
call go#util#EchoError('GoExtract requires gopls, but gopls is disabled')
return
endif

" Extract requires a selection
if a:selected == -1
call go#util#EchoError('GoExtract requires a selection (range) of code')
return
endif

call go#lsp#Extract(a:selected)
call go#lsp#Extract(a:line1, a:line2)
return
endfunction

Expand Down
2 changes: 1 addition & 1 deletion autoload/go/extract_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func! Test_Extract() abort

silent! execute "normal vj$\<Esc>"

call go#extract#Extract(line('.'))
call go#extract#Extract(line('.'), line('.'))

let start = reltime()
while &modified == 0 && reltimefloat(reltime(start)) < 10
Expand Down
17 changes: 10 additions & 7 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ endfunction
" Extract executes the refactor.extract code action for the current buffer
" and configures the handler to only apply the fillstruct command for the
" current location.
function! go#lsp#Extract(selected) abort
function! go#lsp#Extract(line1, line2) abort
let l:fname = expand('%:p')
" send the current file so that TextEdits will be relative to the current
" state of the buffer.
Expand All @@ -1692,13 +1692,16 @@ function! go#lsp#Extract(selected) abort
let l:state.error = l:handler.wrapper
let l:state.handleError = function('s:handleCodeActionError', [l:fname], l:state)

if a:selected == -1
call go#util#EchoError('no range selected')
return
if a:line1 == -1
let [l:startline, l:startcol] = go#lsp#lsp#Position(line('.'), 1)
else
let [l:startline, l:startcol] = go#lsp#lsp#Position(a:line1, 1)
endif
if a:line2 == -1
let [l:endline, l:endcol] = go#lsp#lsp#Position(line('.'), col('$'))
else
let [l:endline, l:endcol] = go#lsp#lsp#Position(a:line2, col([a:line2, '$']))
endif

let [l:startline, l:startcol] = go#lsp#lsp#Position(line("'<"), col("'<"))
let [l:endline, l:endcol] = go#lsp#lsp#Position(line("'>"), col("'>"))

let l:msg = go#lsp#message#CodeActionRefactorExtract(l:fname, l:startline, l:startcol, l:endline, l:endcol)
call l:lsp.sendMessage(l:msg, l:state)
Expand Down
3 changes: 1 addition & 2 deletions autoload/go/lsp/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ function! go#lsp#lsp#Position(...)
let l:line = a:1
let l:col = a:2
endif
let l:content = getline(l:line)

" LSP uses 0-based lines.
return [l:line - 1, s:character(l:line, l:col-1)]
return [l:line - 1, s:character(l:line, l:col)]
endfunction

function! s:strlen(str) abort
Expand Down
4 changes: 2 additions & 2 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -942,9 +942,9 @@ CTRL-t

Open a browser to see gopls debugging information.
*:GoExtract*
:GoExtract
:[range]GoExtract

Extract the code fragment in the selected range to a new function and
Extract the code fragment in the selected line range to a new function and
replace the fragment with call to the function.


Expand Down
2 changes: 1 addition & 1 deletion ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ command! -nargs=? GoModReload call go#lsp#ModReload()
command! GoToggleTermCloseOnExit call go#term#ToggleCloseOnExit()

" -- extract
command! -range=% GoExtract call go#extract#Extract(<count>)
command! -range GoExtract call go#extract#Extract(<line1>, <line2>)

" vim: sw=2 ts=2 et

0 comments on commit dc6a084

Please sign in to comment.