Skip to content

Commit 6d86aac

Browse files
committed
extract: fix line selection
Fix line selection for extraction. Fixes fatih#3615
1 parent 7fb3826 commit 6d86aac

File tree

6 files changed

+17
-21
lines changed

6 files changed

+17
-21
lines changed

autoload/go/extract.vim

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22
let s:cpo_save = &cpo
33
set cpo&vim
44

5-
function! go#extract#Extract(selected) abort
5+
function! go#extract#Extract(line1, line2) abort
66
if !go#config#GoplsEnabled()
77
call go#util#EchoError('GoExtract requires gopls, but gopls is disabled')
88
return
99
endif
1010

11-
" Extract requires a selection
12-
if a:selected == -1
13-
call go#util#EchoError('GoExtract requires a selection (range) of code')
14-
return
15-
endif
16-
17-
call go#lsp#Extract(a:selected)
11+
call go#lsp#Extract(a:line1, a:line2)
1812
return
1913
endfunction
2014

autoload/go/extract_test.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func! Test_Extract() abort
1717

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

20-
call go#extract#Extract(line('.'))
20+
call go#extract#Extract(line('.'), line('.'))
2121

2222
let start = reltime()
2323
while &modified == 0 && reltimefloat(reltime(start)) < 10

autoload/go/lsp.vim

+10-7
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ endfunction
16641664
" Extract executes the refactor.extract code action for the current buffer
16651665
" and configures the handler to only apply the fillstruct command for the
16661666
" current location.
1667-
function! go#lsp#Extract(selected) abort
1667+
function! go#lsp#Extract(line1, line2) abort
16681668
let l:fname = expand('%:p')
16691669
" send the current file so that TextEdits will be relative to the current
16701670
" state of the buffer.
@@ -1678,13 +1678,16 @@ function! go#lsp#Extract(selected) abort
16781678
let l:state.error = l:handler.wrapper
16791679
let l:state.handleError = function('s:handleCodeActionError', [l:fname], l:state)
16801680

1681-
if a:selected == -1
1682-
call go#util#EchoError('no range selected')
1683-
return
1681+
if a:line1 == -1
1682+
let [l:startline, l:startcol] = go#lsp#lsp#Position(line('.'), 1)
1683+
else
1684+
let [l:startline, l:startcol] = go#lsp#lsp#Position(a:line1, 1)
1685+
endif
1686+
if a:line2 == -1
1687+
let [l:endline, l:endcol] = go#lsp#lsp#Position(line('.'), col('$'))
1688+
else
1689+
let [l:endline, l:endcol] = go#lsp#lsp#Position(a:line2, col([a:line2, '$']))
16841690
endif
1685-
1686-
let [l:startline, l:startcol] = go#lsp#lsp#Position(line("'<"), col("'<"))
1687-
let [l:endline, l:endcol] = go#lsp#lsp#Position(line("'>"), col("'>"))
16881691

16891692
let l:msg = go#lsp#message#CodeActionRefactorExtract(l:fname, l:startline, l:startcol, l:endline, l:endcol)
16901693
call l:lsp.sendMessage(l:msg, l:state)

autoload/go/lsp/lsp.vim

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ function! go#lsp#lsp#Position(...)
1212
let l:line = a:1
1313
let l:col = a:2
1414
endif
15-
let l:content = getline(l:line)
1615

1716
" LSP uses 0-based lines.
18-
return [l:line - 1, s:character(l:line, l:col-1)]
17+
return [l:line - 1, s:character(l:line, l:col)]
1918
endfunction
2019

2120
function! s:strlen(str) abort

doc/vim-go.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -942,9 +942,9 @@ CTRL-t
942942

943943
Open a browser to see gopls debugging information.
944944
*:GoExtract*
945-
:GoExtract
945+
:[range]GoExtract
946946

947-
Extract the code fragment in the selected range to a new function and
947+
Extract the code fragment in the selected line range to a new function and
948948
replace the fragment with call to the function.
949949

950950

ftplugin/go/commands.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,6 @@ command! -nargs=? GoModReload call go#lsp#ModReload()
138138
command! GoToggleTermCloseOnExit call go#term#ToggleCloseOnExit()
139139

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

143143
" vim: sw=2 ts=2 et

0 commit comments

Comments
 (0)