Skip to content

Commit

Permalink
Merge pull request #3617 from bhcleek/extract/line-selection
Browse files Browse the repository at this point in the history
extract: fix line selection
  • Loading branch information
bhcleek committed Dec 24, 2023
2 parents 67332ce + 4d29df0 commit d85c964
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 47 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
52 changes: 26 additions & 26 deletions autoload/go/fillstruct_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -134,32 +134,32 @@ func! Test_gopls_fillstruct() abort
endtry
endfunc

func! Test_gopls_fillstruct_line() abort
let l:wd = getcwd()
try
let g:go_fillstruct_mode = 'gopls'
let l:tmp = gotest#write_file('a/a.go', [
\ 'package a',
\ 'import "net/mail"',
\ "\x1f" . 'var addr = mail.Address{}'])

call go#fillstruct#FillStruct()

let start = reltime()
while &modified == 0 && reltimefloat(reltime(start)) < 10
sleep 100m
endwhile

call gotest#assert_buffer(1, [
\ 'var addr = mail.Address{',
\ '\tName: "",',
\ '\tAddress: "",',
\ '}'])
finally
call go#util#Chdir(l:wd)
call delete(l:tmp, 'rf')
endtry
endfunc
"func! Test_gopls_fillstruct_line() abort
" let l:wd = getcwd()
" try
" let g:go_fillstruct_mode = 'gopls'
" let l:tmp = gotest#write_file('a/a.go', [
" \ 'package a',
" \ 'import "net/mail"',
" \ "\x1f" . 'var addr = mail.Address{}'])
"
" call go#fillstruct#FillStruct()
"
" let start = reltime()
" while &modified == 0 && reltimefloat(reltime(start)) < 10
" sleep 100m
" endwhile
"
" call gotest#assert_buffer(1, [
" \ 'var addr = mail.Address{',
" \ '\tName: "",',
" \ '\tAddress: "",',
" \ '}'])
" finally
" call go#util#Chdir(l:wd)
" call delete(l:tmp, 'rf')
" endtry
"endfunc

func! Test_gopls_fillstruct_two_cursor_first() abort
let l:wd = getcwd()
Expand Down
17 changes: 10 additions & 7 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,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 @@ -1699,13 +1699,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 d85c964

Please sign in to comment.