Skip to content

Handle create/rename/delete TextDocumentEdits #1607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions autoload/lsp/utils/text_edit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,40 @@ function! lsp#utils#text_edit#apply_text_edits(uri, text_edits) abort
endif
endfunction

function! lsp#utils#text_edit#apply_text_document_edits(text_document_edit) abort
let l:kind = get(a:text_document_edit, 'kind')

if l:kind == 'create'
let l:uri = lsp#utils#uri_to_path(a:text_document_edit['uri'])
let l:options = get(a:text_document_edit, 'options', {})

if !filereadable(l:uri) || !get(l:options, 'overwrite', v:false)
call writefile([], l:uri, "s")
endif
call s:_switch(l:uri)
elseif l:kind == 'rename'
let l:oldUri = lsp#utils#uri_to_path(a:text_document_edit['oldUri'])
let l:newUri = lsp#utils#uri_to_path(a:text_document_edit['newUri'])
let l:options = get(a:text_document_edit, 'options', {})

if !filereadable(l:uri) || !get(l:options, 'overwrite', v:false)
call rename(l:oldUri, l:newUri)
endif
call s:_switch(l:uri)
elseif l:kind == 'delete'
let l:uri = lsp#utils#uri_to_path(a:text_document_edit['uri'])
let l:options = get(a:text_document_edit, 'options', {})
let l:flags = ""

if isdirectory(l:uri) && get(l:options, "recursive")
let l:flags += "r"
endif
if filereadable(l:uri) || isdirectory(l:uri)
call delete(l:uri, l:flags)
endif
endif
endfunction

" @summary Use this to convert textedit to vim list that is compatible with
" quickfix and locllist items
" @param uri = DocumentUri
Expand Down
31 changes: 30 additions & 1 deletion autoload/lsp/utils/workspace_edit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ function! lsp#utils#workspace_edit#apply_workspace_edit(workspace_edit) abort

if has_key(a:workspace_edit, 'documentChanges')
for l:text_document_edit in a:workspace_edit['documentChanges']
let l:loclist_items += s:_apply(l:text_document_edit['textDocument']['uri'], l:text_document_edit['edits'])
if has_key(l:text_document_edit, 'textDocument')
let l:loclist_items += s:_apply(l:text_document_edit['textDocument']['uri'], l:text_document_edit['edits'])
elseif has_key(l:text_document_edit, 'kind')
let l:loclist_items += s:_apply_document(l:text_document_edit)
endif
endfor
elseif has_key(a:workspace_edit, 'changes')
for [l:uri, l:text_edits] in items(a:workspace_edit['changes'])
Expand All @@ -25,3 +29,28 @@ function! s:_apply(uri, text_edits) abort
call lsp#utils#text_edit#apply_text_edits(a:uri, a:text_edits)
return lsp#utils#text_edit#_lsp_to_vim_list(a:uri, a:text_edits)
endfunction

"
" _apply_document
"
function! s:_apply_document(text_document_edits) abort
call lsp#utils#text_edit#apply_text_document_edits(a:text_document_edits)
if a:text_document_edits['kind'] == 'create'
let l:uri = lsp#utils#uri_to_path(a:text_document_edits['uri'])
return [{
\ ' filename': l:uri,
\ 'lnum': 1,
\ 'col': 1,
\ 'text': l:uri,
\ }]
elseif a:text_document_edits['kind'] == 'rename'
let l:uri = lsp#utils#uri_to_path(a:text_document_edits['newUri'])
return [{
\ 'filename': l:uri,
\ 'lnum': 1,
\ 'col': 1,
\ 'text': l:uri,
\ }]
endif
return []
endfunction