summaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
authorDalius Dobravolskas <dalius.dobravolskas@gmail.com>2020-11-14 12:15:17 +0200
committerGitHub <noreply@github.com>2020-11-14 10:15:17 +0000
commit01800a23addb52788265e0349f519556dab41301 (patch)
treed8f98728e4695718e9d7e142945cf7051a71044a /autoload
parent1ec573bf0df6cbc5eef8b593f93081e4c1837c1b (diff)
downloadale-01800a23addb52788265e0349f519556dab41301.zip
Support for LSP/tsserver Code Actions (#3437)
* Added tsserver and LSP code action support. * tsserver refactors support added. * Handling special case when new text is added after new line symbol. * ale#code_action#ApplyChanges simplified. * Initial attempt on LSP Code Actions. * workspace/executeCommand added. * Some null checks added. * Add last column to LSP Code Action message. * Pass diagnostics to LSP code action. Previously ApplyChanges code was applied from top-to-bottom that required extra parameters to track progress and there was bug. I have changed code to bottom-to-top approach as that does not require those extra parameters and solved the bug. Tested with typescript-language-server and it is working.
Diffstat (limited to 'autoload')
-rw-r--r--autoload/ale/code_action.vim106
-rw-r--r--autoload/ale/codefix.vim388
-rw-r--r--autoload/ale/completion.vim1
-rw-r--r--autoload/ale/lsp.vim10
-rw-r--r--autoload/ale/lsp/message.vim22
-rw-r--r--autoload/ale/lsp/response.vim1
-rw-r--r--autoload/ale/lsp/tsserver_message.vim36
-rw-r--r--autoload/ale/rename.vim56
8 files changed, 537 insertions, 83 deletions
diff --git a/autoload/ale/code_action.vim b/autoload/ale/code_action.vim
index 42f4f265..4dbb2d08 100644
--- a/autoload/ale/code_action.vim
+++ b/autoload/ale/code_action.vim
@@ -33,35 +33,35 @@ endfunction
function! s:ChangeCmp(left, right) abort
if a:left.start.line < a:right.start.line
- return -1
+ return 1
endif
if a:left.start.line > a:right.start.line
- return 1
+ return -1
endif
if a:left.start.offset < a:right.start.offset
- return -1
+ return 1
endif
if a:left.start.offset > a:right.start.offset
- return 1
+ return -1
endif
if a:left.end.line < a:right.end.line
- return -1
+ return 1
endif
if a:left.end.line > a:right.end.line
- return 1
+ return -1
endif
if a:left.end.offset < a:right.end.offset
- return -1
+ return 1
endif
if a:left.end.offset > a:right.end.offset
- return 1
+ return -1
endif
return 0
@@ -85,29 +85,14 @@ function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
let l:pos = [1, 1]
endif
- " We have to keep track of how many lines we have added, and offset
- " changes accordingly.
- let l:line_offset = 0
- let l:column_offset = 0
- let l:last_end_line = 0
-
- " Changes have to be sorted so we apply them from top-to-bottom.
+ " Changes have to be sorted so we apply them from bottom-to-top
for l:code_edit in sort(copy(a:changes), function('s:ChangeCmp'))
- if l:code_edit.start.line isnot l:last_end_line
- let l:column_offset = 0
- endif
-
- let l:line = l:code_edit.start.line + l:line_offset
- let l:column = l:code_edit.start.offset + l:column_offset
- let l:end_line = l:code_edit.end.line + l:line_offset
- let l:end_column = l:code_edit.end.offset + l:column_offset
+ let l:line = l:code_edit.start.line
+ let l:column = l:code_edit.start.offset
+ let l:end_line = l:code_edit.end.line
+ let l:end_column = l:code_edit.end.offset
let l:text = l:code_edit.newText
- let l:cur_line = l:pos[0]
- let l:cur_column = l:pos[1]
-
- let l:last_end_line = l:end_line
-
" Adjust the ends according to previous edits.
if l:end_line > len(l:lines)
let l:end_line_len = 0
@@ -125,6 +110,12 @@ function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
let l:start = l:lines[: l:line - 2]
endif
+ " Special case when text must be added after new line
+ if l:column > len(l:lines[l:line - 1])
+ call extend(l:start, [l:lines[l:line - 1]])
+ let l:column = 1
+ endif
+
if l:column is 1
" We need to handle column 1 specially, because we can't slice an
" empty string ending on index 0.
@@ -140,7 +131,6 @@ function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
let l:lines = l:start + l:middle + l:lines[l:end_line :]
let l:current_line_offset = len(l:lines) - l:lines_before_change
- let l:line_offset += l:current_line_offset
let l:column_offset = len(l:middle[-1]) - l:end_line_len
let l:pos = s:UpdateCursor(l:pos,
@@ -215,3 +205,61 @@ function! s:UpdateCursor(cursor, start, end, offset) abort
return [l:cur_line, l:cur_column]
endfunction
+
+function! ale#code_action#GetChanges(workspace_edit) abort
+ let l:changes = {}
+
+ if has_key(a:workspace_edit, 'changes') && !empty(a:workspace_edit.changes)
+ return a:workspace_edit.changes
+ elseif has_key(a:workspace_edit, 'documentChanges')
+ let l:document_changes = []
+
+ if type(a:workspace_edit.documentChanges) is v:t_dict
+ \ && has_key(a:workspace_edit.documentChanges, 'edits')
+ call add(l:document_changes, a:workspace_edit.documentChanges)
+ elseif type(a:workspace_edit.documentChanges) is v:t_list
+ let l:document_changes = a:workspace_edit.documentChanges
+ endif
+
+ for l:text_document_edit in l:document_changes
+ let l:filename = l:text_document_edit.textDocument.uri
+ let l:edits = l:text_document_edit.edits
+ let l:changes[l:filename] = l:edits
+ endfor
+ endif
+
+ return l:changes
+endfunction
+
+function! ale#code_action#BuildChangesList(changes_map) abort
+ let l:changes = []
+
+ for l:file_name in keys(a:changes_map)
+ let l:text_edits = a:changes_map[l:file_name]
+ let l:text_changes = []
+
+ for l:edit in l:text_edits
+ let l:range = l:edit.range
+ let l:new_text = l:edit.newText
+
+ call add(l:text_changes, {
+ \ 'start': {
+ \ 'line': l:range.start.line + 1,
+ \ 'offset': l:range.start.character + 1,
+ \ },
+ \ 'end': {
+ \ 'line': l:range.end.line + 1,
+ \ 'offset': l:range.end.character + 1,
+ \ },
+ \ 'newText': l:new_text,
+ \})
+ endfor
+
+ call add(l:changes, {
+ \ 'fileName': ale#path#FromURI(l:file_name),
+ \ 'textChanges': l:text_changes,
+ \})
+ endfor
+
+ return l:changes
+endfunction
diff --git a/autoload/ale/codefix.vim b/autoload/ale/codefix.vim
new file mode 100644
index 00000000..b58f5e4b
--- /dev/null
+++ b/autoload/ale/codefix.vim
@@ -0,0 +1,388 @@
+" Author: Dalius Dobravolskas <dalius.dobravolskas@gmail.com>
+" Description: Code Fix support for tsserver
+
+let s:codefix_map = {}
+
+" Used to get the codefix map in tests.
+function! ale#codefix#GetMap() abort
+ return deepcopy(s:codefix_map)
+endfunction
+
+" Used to set the codefix map in tests.
+function! ale#codefix#SetMap(map) abort
+ let s:codefix_map = a:map
+endfunction
+
+function! ale#codefix#ClearLSPData() abort
+ let s:codefix_map = {}
+endfunction
+
+function! s:message(message) abort
+ call ale#util#Execute('echom ' . string(a:message))
+endfunction
+
+function! ale#codefix#HandleTSServerResponse(conn_id, response) abort
+ if !has_key(a:response, 'request_seq')
+ \ || !has_key(s:codefix_map, a:response.request_seq)
+ return
+ endif
+
+ let l:location = remove(s:codefix_map, a:response.request_seq)
+
+ if get(a:response, 'command', '') is# 'getCodeFixes'
+ if get(a:response, 'success', v:false) is v:false
+ let l:message = get(a:response, 'message', 'unknown')
+ call s:message('Error while getting code fixes. Reason: ' . l:message)
+
+ return
+ endif
+
+ if len(a:response.body) == 0
+ call s:message('No code fixes available.')
+
+ return
+ endif
+
+ let l:code_fix_to_apply = 0
+
+ if len(a:response.body) == 1
+ let l:code_fix_to_apply = 1
+ else
+ let l:codefix_no = 1
+ let l:codefixstring = "Code Fixes:\n"
+
+ for l:codefix in a:response.body
+ let l:codefixstring .= l:codefix_no . ') ' . l:codefix.description . "\n"
+ let l:codefix_no += 1
+ endfor
+
+ let l:codefixstring .= 'Type number and <Enter> (empty cancels): '
+
+ let l:code_fix_to_apply = ale#util#Input(l:codefixstring, '')
+ let l:code_fix_to_apply = str2nr(l:code_fix_to_apply)
+
+ if l:code_fix_to_apply == 0
+ return
+ endif
+ endif
+
+ let l:changes = a:response.body[l:code_fix_to_apply - 1].changes
+
+ call ale#code_action#HandleCodeAction({
+ \ 'description': 'codefix',
+ \ 'changes': l:changes,
+ \}, {})
+ elseif get(a:response, 'command', '') is# 'getApplicableRefactors'
+ if get(a:response, 'success', v:false) is v:false
+ let l:message = get(a:response, 'message', 'unknown')
+ call s:message('Error while getting applicable refactors. Reason: ' . l:message)
+
+ return
+ endif
+
+ if len(a:response.body) == 0
+ call s:message('No applicable refactors available.')
+
+ return
+ endif
+
+ let l:refactors = []
+
+ for l:item in a:response.body
+ for l:action in l:item.actions
+ call add(l:refactors, {
+ \ 'name': l:action.description,
+ \ 'id': [l:item.name, l:action.name],
+ \})
+ endfor
+ endfor
+
+ let l:refactor_no = 1
+ let l:refactorstring = "Applicable refactors:\n"
+
+ for l:refactor in l:refactors
+ let l:refactorstring .= l:refactor_no . ') ' . l:refactor.name . "\n"
+ let l:refactor_no += 1
+ endfor
+
+ let l:refactorstring .= 'Type number and <Enter> (empty cancels): '
+
+ let l:refactor_to_apply = ale#util#Input(l:refactorstring, '')
+ let l:refactor_to_apply = str2nr(l:refactor_to_apply)
+
+ if l:refactor_to_apply == 0
+ return
+ endif
+
+ let l:id = l:refactors[l:refactor_to_apply - 1].id
+
+ let l:message = ale#lsp#tsserver_message#GetEditsForRefactor(
+ \ l:location.buffer,
+ \ l:location.line,
+ \ l:location.column,
+ \ l:location.end_line,
+ \ l:location.end_column,
+ \ l:id[0],
+ \ l:id[1],
+ \)
+
+ let l:request_id = ale#lsp#Send(l:location.connection_id, l:message)
+
+ let s:codefix_map[l:request_id] = l:location
+ elseif get(a:response, 'command', '') is# 'getEditsForRefactor'
+ if get(a:response, 'success', v:false) is v:false
+ let l:message = get(a:response, 'message', 'unknown')
+ call s:message('Error while getting edits for refactor. Reason: ' . l:message)
+
+ return
+ endif
+
+ call ale#code_action#HandleCodeAction({
+ \ 'description': 'editsForRefactor',
+ \ 'changes': a:response.body.edits,
+ \}, {})
+ endif
+endfunction
+
+function! ale#codefix#HandleLSPResponse(conn_id, response) abort
+ if has_key(a:response, 'method')
+ \ && a:response.method is# 'workspace/applyEdit'
+ \ && has_key(a:response, 'params')
+ let l:params = a:response.params
+
+ let l:changes_map = ale#code_action#GetChanges(l:params.edit)
+
+ if empty(l:changes_map)
+ return
+ endif
+
+ let l:changes = ale#code_action#BuildChangesList(l:changes_map)
+
+ call ale#code_action#HandleCodeAction({
+ \ 'description': 'applyEdit',
+ \ 'changes': l:changes,
+ \}, {})
+ elseif has_key(a:response, 'id')
+ \&& has_key(s:codefix_map, a:response.id)
+ let l:location = remove(s:codefix_map, a:response.id)
+
+ if !has_key(a:response, 'result')
+ \ || type(a:response.result) != v:t_list
+ \ || len(a:response.result) == 0
+ call s:message('No code actions received from server')
+
+ return
+ endif
+
+ let l:codeaction_no = 1
+ let l:codeactionstring = "Code Fixes:\n"
+
+ for l:codeaction in a:response.result
+ let l:codeactionstring .= l:codeaction_no . ') ' . l:codeaction.title . "\n"
+ let l:codeaction_no += 1
+ endfor
+
+ let l:codeactionstring .= 'Type number and <Enter> (empty cancels): '
+
+ let l:codeaction_to_apply = ale#util#Input(l:codeactionstring, '')
+ let l:codeaction_to_apply = str2nr(l:codeaction_to_apply)
+
+ if l:codeaction_to_apply == 0
+ return
+ endif
+
+ let l:item = a:response.result[l:codeaction_to_apply - 1]
+
+ if has_key(l:item, 'command')
+ \ && type(l:item.command) == v:t_dict
+ let l:command = l:item.command
+ let l:message = ale#lsp#message#ExecuteCommand(
+ \ l:command.command,
+ \ l:command.arguments,
+ \)
+
+ let l:request_id = ale#lsp#Send(l:location.connection_id, l:message)
+ elseif has_key(l:item, 'edit') || has_key(l:item, 'arguments')
+ if has_key(l:item, 'edit')
+ let l:topass = l:item.edit
+ else
+ let l:topass = l:item.arguments[0]
+ endif
+
+ let l:changes_map = ale#code_action#GetChanges(l:topass)
+
+ if empty(l:changes_map)
+ return
+ endif
+
+ let l:changes = ale#code_action#BuildChangesList(l:changes_map)
+
+ call ale#code_action#HandleCodeAction({
+ \ 'description': 'codeaction',
+ \ 'changes': l:changes,
+ \}, {})
+ endif
+ endif
+endfunction
+
+
+function! s:OnReady(line, column, end_line, end_column, linter, lsp_details) abort
+ let l:id = a:lsp_details.connection_id
+
+ if !ale#lsp#HasCapability(l:id, 'code_actions')
+ return
+ endif
+
+ let l:buffer = a:lsp_details.buffer
+
+ if a:linter.lsp is# 'tsserver'
+ if a:line == a:end_line && a:column == a:end_column
+ if !has_key(g:ale_buffer_info, l:buffer)
+ return
+ endif
+
+ let l:nearest_error = v:null
+ let l:nearest_error_diff = -1
+
+ for l:error in get(g:ale_buffer_info[l:buffer], 'loclist', [])
+ if has_key(l:error, 'code') && l:error.lnum == a:line
+ let l:diff = abs(l:error.col - a:column)
+
+ if l:nearest_error_diff == -1 || l:diff < l:nearest_error_diff
+ let l:nearest_error_diff = l:diff
+ let l:nearest_error = l:error.code
+ endif
+ endif
+ endfor
+
+ let l:message = ale#lsp#tsserver_message#GetCodeFixes(
+ \ l:buffer,
+ \ a:line,
+ \ a:column,
+ \ a:line,
+ \ a:column,
+ \ [l:nearest_error],
+ \)
+ else
+ let l:message = ale#lsp#tsserver_message#GetApplicableRefactors(
+ \ l:buffer,
+ \ a:line,
+ \ a:column,
+ \ a:end_line,
+ \ a:end_column,
+ \)
+ endif
+ else
+ " Send a message saying the buffer has changed first, otherwise
+ " completions won't know what text is nearby.
+ call ale#lsp#NotifyForChanges(l:id, l:buffer)
+
+ if a:line == a:end_line && a:column == a:end_column
+ if !has_key(g:ale_buffer_info, l:buffer)
+ return
+ endif
+
+ let l:nearest_error = v:null
+ let l:nearest_error_diff = -1
+
+ for l:error in get(g:ale_buffer_info[l:buffer], 'loclist', [])
+ if has_key(l:error, 'code') && l:error.lnum == a:line
+ let l:diff = abs(l:error.col - a:column)
+
+ if l:nearest_error_diff == -1 || l:diff < l:nearest_error_diff
+ let l:nearest_error_diff = l:diff
+ let l:nearest_error = l:error
+ endif
+ endif
+ endfor
+
+ let l:diagnostics = []
+
+ if l:nearest_error isnot v:null
+ let l:diagnostics = [{
+ \ 'code': l:nearest_error.code,
+ \ 'message': l:nearest_error.text,
+ \ 'range': {
+ \ 'start': { 'line': l:nearest_error.lnum - 1, 'character': l:nearest_error.col - 1 },
+ \ 'end': { 'line': l:nearest_error.end_lnum - 1, 'character': l:nearest_error.end_col - 1 }
+ \}
+ \}]
+ endif
+
+ let l:message = ale#lsp#message#CodeAction(
+ \ l:buffer,
+ \ a:line,
+ \ a:column,
+ \ a:end_line,
+ \ a:end_column,
+ \ l:diagnostics,
+ \)
+ else
+ let l:message = ale#lsp#message#CodeAction(
+ \ l:buffer,
+ \ a:line,
+ \ a:column,
+ \ a:end_line,
+ \ a:end_column,
+ \ [],
+ \)
+ endif
+ endif
+
+ let l:Callback = a:linter.lsp is# 'tsserver'
+ \ ? function('ale#codefix#HandleTSServerResponse')
+ \ : function('ale#codefix#HandleLSPResponse')
+
+ call ale#lsp#RegisterCallback(l:id, l:Callback)
+
+ let l:request_id = ale#lsp#Send(l:id, l:message)
+
+ let s:codefix_map[l:request_id] = {
+ \ 'connection_id': l:id,
+ \ 'buffer': l:buffer,
+ \ 'line': a:line,
+ \ 'column': a:column,
+ \ 'end_line': a:end_line,
+ \ 'end_column': a:end_column,
+ \}
+endfunction
+
+function! s:ExecuteGetCodeFix(linter, range) abort
+ let l:buffer = bufnr('')
+
+ if a:range == 0
+ let [l:line, l:column] = getpos('.')[1:2]
+ let l:end_line = l:line
+ let l:end_column = l:column
+ else
+ let [l:line, l:column] = getpos("'<")[1:2]
+ let [l:end_line, l:end_column] = getpos("'>")[1:2]
+ endif
+
+ let l:column = min([l:column, len(getline(l:line))])
+ let l:end_column = min([l:end_column, len(getline(l:end_line))])
+
+ let l:Callback = function(
+ \ 's:OnReady', [l:line, l:column, l:end_line, l:end_column])
+ call ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback)
+endfunction
+
+function! ale#codefix#Execute(range) abort
+ let l:lsp_linters = []
+
+ for l:linter in ale#linter#Get(&filetype)
+ if !empty(l:linter.lsp)
+ call add(l:lsp_linters, l:linter)
+ endif
+ endfor
+
+ if empty(l:lsp_linters)
+ call s:message('No active LSPs')
+
+ return
+ endif
+
+ for l:lsp_linter in l:lsp_linters
+ call s:ExecuteGetCodeFix(l:lsp_linter, a:range)
+ endfor
+endfunction
diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim
index efbf0fd5..48e9bf7c 100644
--- a/autoload/ale/completion.vim
+++ b/autoload/ale/completion.vim
@@ -617,6 +617,7 @@ function! ale#completion#ParseLSPCompletions(response) abort
let l:user_data = {'_ale_completion_item': 1}
if has_key(l:item, 'additionalTextEdits')
+ \ && l:item.additionalTextEdits isnot v:null
let l:text_changes = []
for l:edit in l:item.additionalTextEdits
diff --git a/autoload/ale/lsp.vim b/autoload/ale/lsp.vim
index 7d99e9d2..cb0573aa 100644
--- a/autoload/ale/lsp.vim
+++ b/autoload/ale/lsp.vim
@@ -44,6 +44,7 @@ function! ale#lsp#Register(executable_or_address, project, init_options) abort
\ 'definition': 0,
\ 'typeDefinition': 0,
\ 'symbol_search': 0,
+ \ 'code_actions': 0,
\ },
\}
endif
@@ -219,6 +220,14 @@ function! s:UpdateCapabilities(conn, capabilities) abort
let a:conn.capabilities.rename = 1
endif
+ if get(a:capabilities, 'codeActionProvider') is v:true
+ let a:conn.capabilities.code_actions = 1
+ endif
+
+ if type(get(a:capabilities, 'codeActionProvider')) is v:t_dict
+ let a:conn.capabilities.code_actions = 1
+ endif
+
if !empty(get(a:capabilities, 'completionProvider'))
let a:conn.capabilities.completion = 1
endif
@@ -350,6 +359,7 @@ function! ale#lsp#MarkConnectionAsTsserver(conn_id) abort
let l:conn.capabilities.definition = 1
let l:conn.capabilities.symbol_search = 1
let l:conn.capabilities.rename = 1
+ let l:conn.capabilities.code_actions = 1
endfunction
function! s:SendInitMessage(conn) abort
diff --git a/autoload/ale/lsp/message.vim b/autoload/ale/lsp/message.vim
index 5b0cb8b7..38be4da6 100644
--- a/autoload/ale/lsp/message.vim
+++ b/autoload/ale/lsp/message.vim
@@ -172,3 +172,25 @@ function! ale#lsp#message#Rename(buffer, line, column, new_name) abort
\ 'newName': a:new_name,
\}]
endfunction
+
+function! ale#lsp#message#CodeAction(buffer, line, column, end_line, end_column, diagnostics) abort
+ return [0, 'textDocument/codeAction', {
+ \ 'textDocument': {
+ \ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
+ \ },
+ \ 'range': {
+ \ 'start': {'line': a:line - 1, 'character': a:column - 1},
+ \ 'end': {'line': a:end_line - 1, 'character': a:end_column},
+ \ },
+ \ 'context': {
+ \ 'diagnostics': a:diagnostics
+ \ },
+ \}]
+endfunction
+
+function! ale#lsp#message#ExecuteCommand(command, arguments) abort
+ return [0, 'workspace/executeCommand', {
+ \ 'command': a:command,
+ \ 'arguments': a:arguments,
+ \}]
+endfunction
diff --git a/autoload/ale/lsp/response.vim b/autoload/ale/lsp/response.vim
index 30da77e1..a4f80980 100644
--- a/autoload/ale/lsp/response.vim
+++ b/autoload/ale/lsp/response.vim
@@ -56,6 +56,7 @@ function! ale#lsp#response#ReadDiagnostics(response) abort
endif
if has_key(l:diagnostic, 'relatedInformation')
+ \ && l:diagnostic.relatedInformation isnot v:null
let l:related = deepcopy(l:diagnostic.relatedInformation)
call map(l:related, {key, val ->
\ ale#path#FromURI(val.location.uri) .
diff --git a/autoload/ale/lsp/tsserver_message.vim b/autoload/ale/lsp/tsserver_message.vim
index b9fafaa0..3c1b47ed 100644
--- a/autoload/ale/lsp/tsserver_message.vim
+++ b/autoload/ale/lsp/tsserver_message.vim
@@ -103,3 +103,39 @@ function! ale#lsp#tsserver_message#OrganizeImports(buffer) abort
\ },
\}]
endfunction
+
+function! ale#lsp#tsserver_message#GetCodeFixes(buffer, line, column, end_line, end_column, error_codes) abort
+ " The lines and columns are 1-based.
+ " The errors codes must be a list of tsserver error codes to fix.
+ return [0, 'ts@getCodeFixes', {
+ \ 'startLine': a:line,
+ \ 'startOffset': a:column,
+ \ 'endLine': a:end_line,
+ \ 'endOffset': a:end_column + 1,
+ \ 'file': expand('#' . a:buffer . ':p'),
+ \ 'errorCodes': a:error_codes,
+ \}]
+endfunction
+
+function! ale#lsp#tsserver_message#GetApplicableRefactors(buffer, line, column, end_line, end_column) abort
+ " The arguments for this request can also be just 'line' and 'offset'
+ return [0, 'ts@getApplicableRefactors', {
+ \ 'startLine': a:line,
+ \ 'startOffset': a:column,
+ \ 'endLine': a:end_line,
+ \ 'endOffset': a:end_column + 1,
+ \ 'file': expand('#' . a:buffer . ':p'),
+ \}]
+endfunction
+
+function! ale#lsp#tsserver_message#GetEditsForRefactor(buffer, line, column, end_line, end_column, refactor, action) abort
+ return [0, 'ts@getEditsForRefactor', {
+ \ 'startLine': a:line,
+ \ 'startOffset': a:column,
+ \ 'endLine': a:end_line,
+ \ 'endOffset': a:end_column + 1,
+ \ 'file': expand('#' . a:buffer . ':p'),
+ \ 'refactor': a:refactor,
+ \ 'action': a:action,
+ \}]
+endfunction
diff --git a/autoload/ale/rename.vim b/autoload/ale/rename.vim
index 8190411d..0d074c24 100644
--- a/autoload/ale/rename.vim
+++ b/autoload/ale/rename.vim
@@ -90,31 +90,6 @@ function! ale#rename#HandleTSServerResponse(conn_id, response) abort
\)
endfunction
-function! s:getChanges(workspace_edit) abort
- let l:changes = {}
-
- if has_key(a:workspace_edit, 'changes') && !empty(a:workspace_edit.changes)
- return a:workspace_edit.changes
- elseif has_key(a:workspace_edit, 'documentChanges')
- let l:document_changes = []
-
- if type(a:workspace_edit.documentChanges) is v:t_dict
- \ && has_key(a:workspace_edit.documentChanges, 'edits')
- call add(l:document_changes, a:workspace_edit.documentChanges)
- elseif type(a:workspace_edit.documentChanges) is v:t_list
- let l:document_changes = a:workspace_edit.documentChanges
- endif
-
- for l:text_document_edit in l:document_changes
- let l:filename = l:text_document_edit.textDocument.uri
- let l:edits = l:text_document_edit.edits
- let l:changes[l:filename] = l:edits
- endfor
- endif
-
- return l:changes
-endfunction
-
function! ale#rename#HandleLSPResponse(conn_id, response) abort
if has_key(a:response, 'id')
\&& has_key(s:rename_map, a:response.id)
@@ -126,7 +101,7 @@ function! ale#rename#HandleLSPResponse(conn_id, response) abort
return
endif
- let l:changes_map = s:getChanges(a:response.result)
+ let l:changes_map = ale#code_action#GetChanges(a:response.result)
if empty(l:changes_map)
call s:message('No changes received from server')
@@ -134,34 +109,7 @@ function! ale#rename#HandleLSPResponse(conn_id, response) abort
return
endif
- let l:changes = []
-
- for l:file_name in keys(l:changes_map)
- let l:text_edits = l:changes_map[l:file_name]
- let l:text_changes = []
-
- for l:edit in l:text_edits
- let l:range = l:edit.range
- let l:new_text = l:edit.newText
-
- call add(l:text_changes, {
- \ 'start': {
- \ 'line': l:range.start.line + 1,
- \ 'offset': l:range.start.character + 1,
- \ },
- \ 'end': {
- \ 'line': l:range.end.line + 1,
- \ 'offset': l:range.end.character + 1,
- \ },
- \ 'newText': l:new_text,
- \})
- endfor
-
- call add(l:changes, {
- \ 'fileName': ale#path#FromURI(l:file_name),
- \ 'textChanges': l:text_changes,
- \})
- endfor
+ let l:changes = ale#code_action#BuildChangesList(l:changes_map)
call ale#code_action#HandleCodeAction(
\ {