Skip to content

Commit

Permalink
Handle file operations that are represented by new fixit chunk kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
bstaletic committed Oct 13, 2024
1 parent 35d1882 commit e47a87a
Showing 1 changed file with 75 additions and 8 deletions.
83 changes: 75 additions & 8 deletions python/ycm/vimsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,12 @@ def _SortChunksByFile( chunks ):
chunks_by_file = defaultdict( list )

for chunk in chunks:
filepath = chunk[ 'range' ][ 'start' ][ 'filepath' ]
if 'range' in chunk:
filepath = chunk[ 'range' ][ 'start' ][ 'filepath' ]
elif 'old_file' in chunk:
filepath = chunk[ 'old_file' ]
else:
filepath = chunk[ 'file' ]
chunks_by_file[ filepath ].append( chunk )

return chunks_by_file
Expand Down Expand Up @@ -1078,17 +1083,40 @@ def ReplaceChunksInBuffer( chunks, vim_buffer ):
# reverse order.
chunks.reverse()
chunks.sort( key = lambda chunk: (
chunk[ 'range' ][ 'start' ][ 'line_num' ],
chunk[ 'range' ][ 'start' ][ 'column_num' ]
chunk.get( 'range', {} ).get( 'start', {} ).get( 'line_num', 1 ),
chunk.get( 'range', {} ).get( 'start', {} ).get( 'column_num', 1 )
), reverse = True )

# However, we still want to display the locations from the top of the buffer
# to its bottom.
return reversed( [ ReplaceChunk( chunk[ 'range' ][ 'start' ],
chunk[ 'range' ][ 'end' ],
chunk[ 'replacement_text' ],
vim_buffer )
for chunk in chunks ] )
replace_chunks = []
for chunk in chunks:
if 'range' in chunk:
replace_chunks.append(
ReplaceChunk(
chunk[ 'range' ][ 'start' ],
chunk[ 'range' ][ 'end' ],
chunk[ 'replacement_text' ],
vim_buffer ) )
elif 'old_file' in chunk:
replace_chunks.append(
RenameChunk(
chunk[ 'old_file' ],
chunk[ 'new_file' ],
vim_buffer ) )
elif chunk[ 'kind' ] == 'create':
replace_chunks.append(
CreateChunk(
chunk[ 'file' ],
vim_buffer,
chunk[ 'kind' ] ) )
elif chunk[ 'kind' ] == 'delete':
replace_chunks.append(
DeleteChunk(
chunk[ 'file' ],
vim_buffer,
chunk[ 'kind' ] ) )
return reversed( replace_chunks )


def SplitLines( contents ):
Expand Down Expand Up @@ -1172,6 +1200,45 @@ def ReplaceChunk( start, end, replacement_text, vim_buffer ):
}


def RenameChunk( old_file, new_file, vim_buffer, kind = 'rename' ):
OpenFilename( old_file )
vim.command( f'silent! saveas { new_file }' )
vim.command( f'silent! bw! old_file' )
os.remove( old_file )
return {
'bufnr': vim_buffer.number,
'filename': new_file,
'lnum': 1,
'col': 1,
'text': '',
'type': 'F',
}


def CreateChunk( file, vim_buffer, kind = 'create' ):
return {
'bufnr': vim_buffer.number,
'filename': file,
'lnum': 1,
'col': 1,
'text': '',
'type': 'F',
}


def DeleteChunk( file, vim_buffer, kind = 'delete' ):
vim.command( f'silent! bw! { vim_buffer }' )
os.remove( file )
return {
'bufnr': vim_buffer.number,
'filename': file,
'lnum': 1,
'col': 1,
'text': '',
'type': 'F',
}


def InsertNamespace( namespace ):
if VariableExists( 'g:ycm_csharp_insert_namespace_expr' ):
expr = GetVariableValue( 'g:ycm_csharp_insert_namespace_expr' )
Expand Down

0 comments on commit e47a87a

Please sign in to comment.