Skip to content
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

[RFC] Handle file operations that are represented by new fixit chunk kinds #4271

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
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 @@
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' ]

Check warning on line 941 in python/ycm/vimsupport.py

View check run for this annotation

Codecov / codecov/patch

python/ycm/vimsupport.py#L940-L941

Added lines #L940 - L941 were not covered by tests
else:
filepath = chunk[ 'file' ]

Check warning on line 943 in python/ycm/vimsupport.py

View check run for this annotation

Codecov / codecov/patch

python/ycm/vimsupport.py#L943

Added line #L943 was not covered by tests
chunks_by_file[ filepath ].append( chunk )

return chunks_by_file
Expand Down Expand Up @@ -1078,17 +1083,40 @@
# 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(

Check warning on line 1102 in python/ycm/vimsupport.py

View check run for this annotation

Codecov / codecov/patch

python/ycm/vimsupport.py#L1101-L1102

Added lines #L1101 - L1102 were not covered by tests
RenameChunk(
chunk[ 'old_file' ],
chunk[ 'new_file' ],
vim_buffer ) )
elif chunk[ 'kind' ] == 'create':
replace_chunks.append(

Check warning on line 1108 in python/ycm/vimsupport.py

View check run for this annotation

Codecov / codecov/patch

python/ycm/vimsupport.py#L1107-L1108

Added lines #L1107 - L1108 were not covered by tests
CreateChunk(
chunk[ 'file' ],
vim_buffer,
chunk[ 'kind' ] ) )
elif chunk[ 'kind' ] == 'delete':
replace_chunks.append(

Check warning on line 1114 in python/ycm/vimsupport.py

View check run for this annotation

Codecov / codecov/patch

python/ycm/vimsupport.py#L1113-L1114

Added lines #L1113 - L1114 were not covered by tests
DeleteChunk(
chunk[ 'file' ],
vim_buffer,
chunk[ 'kind' ] ) )
return reversed( replace_chunks )


def SplitLines( contents ):
Expand Down Expand Up @@ -1172,6 +1200,45 @@
}


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 {

Check warning on line 1208 in python/ycm/vimsupport.py

View check run for this annotation

Codecov / codecov/patch

python/ycm/vimsupport.py#L1204-L1208

Added lines #L1204 - L1208 were not covered by tests
'bufnr': vim_buffer.number,
'filename': new_file,
'lnum': 1,
'col': 1,
'text': '',
'type': 'F',
}


def CreateChunk( file, vim_buffer, kind = 'create' ):
return {

Check warning on line 1219 in python/ycm/vimsupport.py

View check run for this annotation

Codecov / codecov/patch

python/ycm/vimsupport.py#L1219

Added line #L1219 was not covered by tests
'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 {

Check warning on line 1232 in python/ycm/vimsupport.py

View check run for this annotation

Codecov / codecov/patch

python/ycm/vimsupport.py#L1230-L1232

Added lines #L1230 - L1232 were not covered by tests
'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