From dc6a084a488665eed9744c3af18ee10ff4cdb59b Mon Sep 17 00:00:00 2001 From: Billie Cleek Date: Sat, 23 Dec 2023 16:07:10 -0800 Subject: [PATCH] extract: fix line selection Fix line selection for extraction. Fixes #3615 --- autoload/go/extract.vim | 10 ++-------- autoload/go/extract_test.vim | 2 +- autoload/go/lsp.vim | 17 ++++++++++------- autoload/go/lsp/lsp.vim | 3 +-- doc/vim-go.txt | 4 ++-- ftplugin/go/commands.vim | 2 +- 6 files changed, 17 insertions(+), 21 deletions(-) diff --git a/autoload/go/extract.vim b/autoload/go/extract.vim index e9e9971464..ab6fff65d0 100644 --- a/autoload/go/extract.vim +++ b/autoload/go/extract.vim @@ -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 diff --git a/autoload/go/extract_test.vim b/autoload/go/extract_test.vim index 26e1262b67..1a21b58a46 100644 --- a/autoload/go/extract_test.vim +++ b/autoload/go/extract_test.vim @@ -17,7 +17,7 @@ func! Test_Extract() abort silent! execute "normal vj$\" - call go#extract#Extract(line('.')) + call go#extract#Extract(line('.'), line('.')) let start = reltime() while &modified == 0 && reltimefloat(reltime(start)) < 10 diff --git a/autoload/go/lsp.vim b/autoload/go/lsp.vim index e7a29f5554..7f3be93e0e 100644 --- a/autoload/go/lsp.vim +++ b/autoload/go/lsp.vim @@ -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. @@ -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) diff --git a/autoload/go/lsp/lsp.vim b/autoload/go/lsp/lsp.vim index 7138155623..6bd22cec36 100644 --- a/autoload/go/lsp/lsp.vim +++ b/autoload/go/lsp/lsp.vim @@ -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 diff --git a/doc/vim-go.txt b/doc/vim-go.txt index f1d2540c86..5339abdddc 100644 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -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. diff --git a/ftplugin/go/commands.vim b/ftplugin/go/commands.vim index f5ad1c197e..b354fad2fc 100644 --- a/ftplugin/go/commands.vim +++ b/ftplugin/go/commands.vim @@ -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() +command! -range GoExtract call go#extract#Extract(, ) " vim: sw=2 ts=2 et