Skip to content

Commit 86df022

Browse files
committed
Add support for file operations in CodeActions
1 parent 47371eb commit 86df022

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

ycmd/completers/language_server/language_server_completer.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,21 +3590,35 @@ def WorkspaceEditToFixIt( request_data,
35903590
if not workspace_edit:
35913591
return None
35923592

3593+
chunks = []
35933594
if 'changes' in workspace_edit:
3594-
chunks = []
35953595
# We sort the filenames to make the response stable. Edits are applied in
35963596
# strict sequence within a file, but apply to files in arbitrary order.
35973597
# However, it's important for the response to be stable for the tests.
35983598
for uri in sorted( workspace_edit[ 'changes' ].keys() ):
35993599
chunks.extend( TextEditToChunks( request_data,
36003600
uri,
36013601
workspace_edit[ 'changes' ][ uri ] ) )
3602-
else:
3603-
chunks = []
3602+
if 'documentChanges' in workspace_edit:
36043603
for text_document_edit in workspace_edit[ 'documentChanges' ]:
3605-
uri = text_document_edit[ 'textDocument' ][ 'uri' ]
3606-
edits = text_document_edit[ 'edits' ]
3607-
chunks.extend( TextEditToChunks( request_data, uri, edits ) )
3604+
kind = text_document_edit.get( 'kind', '' )
3605+
if 'edits' in text_document_edit:
3606+
uri = text_document_edit[ 'textDocument' ][ 'uri' ]
3607+
edits = text_document_edit[ 'edits' ]
3608+
chunks.extend( TextEditToChunks( request_data, uri, edits ) )
3609+
elif kind == 'rename':
3610+
chunks.append(
3611+
responses.RenameChunk(
3612+
old_filepath = lsp.UriToFilePath( text_document_edit[ 'oldUri' ] ),
3613+
new_filepath = lsp.UriToFilePath( text_document_edit[ 'newUri' ] ) ) )
3614+
elif kind == 'delete':
3615+
chunks.append(
3616+
responses.DeleteChunk(
3617+
lsp.UriToFilePath( text_document_edit[ 'uri' ] ) ) )
3618+
elif kind == 'create':
3619+
chunks.append(
3620+
responses.CreateChunk(
3621+
lsp.UriToFilePath( text_document_edit[ 'uri' ] ) ) )
36083622
return responses.FixIt(
36093623
responses.Location( request_data[ 'line_num' ],
36103624
request_data[ 'column_num' ],

ycmd/completers/language_server/language_server_protocol.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,10 @@ def Initialize( request_id,
307307
'didChangeWatchedFiles': {
308308
'dynamicRegistration': True
309309
},
310-
'workspaceEdit': { 'documentChanges': True, },
310+
'workspaceEdit': {
311+
'resourceOperations': [ 'create', 'rename', 'delete'],
312+
'documentChanges': True,
313+
},
311314
'symbol': {
312315
'symbolKind': {
313316
'valueSet': list( range( 1, len( SYMBOL_KIND ) ) ),

ycmd/responses.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,22 @@ def __init__( self, location: Location, chunks, text = '', kind = None ):
268268
self.kind = kind
269269

270270

271+
class DeleteChunk:
272+
def __init__( self, filepath ):
273+
self.filepath = filepath
274+
self.kind = 'delete'
275+
276+
class CreateChunk:
277+
def __init__( self, filepath ):
278+
self.filepath = filepath
279+
self.kind = 'create'
280+
281+
class RenameChunk:
282+
def __init__( self, old_filepath, new_filepath ):
283+
self.old_filepath = old_filepath
284+
self.new_filepath = new_filepath
285+
self.kind = 'rename'
286+
271287
class FixItChunk:
272288
"""An individual replacement within a FixIt (aka Refactor)"""
273289

@@ -315,10 +331,22 @@ def BuildFixItResponse( fixits ):
315331
both quick fix and refactor operations"""
316332

317333
def BuildFixitChunkData( chunk ):
318-
return {
319-
'replacement_text': chunk.replacement_text,
320-
'range': BuildRangeData( chunk.range ),
321-
}
334+
if hasattr( chunk, 'replacement_text' ):
335+
return {
336+
'replacement_text': chunk.replacement_text,
337+
'range': BuildRangeData( chunk.range ),
338+
}
339+
elif hasattr( chunk, 'new_filepath' ):
340+
return {
341+
'new_filepath': chunk.new_filepath,
342+
'old_filepath': chunk.old_filepath,
343+
'kind': chunk.kind
344+
}
345+
else:
346+
return {
347+
'filepath': chunk.filepath,
348+
'kind': chunk.kind
349+
}
322350

323351
def BuildFixItData( fixit ):
324352
if hasattr( fixit, 'resolve' ):

0 commit comments

Comments
 (0)