Fix #3322 - Apply rename changes correctly

This commit is contained in:
w0rp 2020-09-04 09:37:33 +01:00
parent d4a14746cd
commit 844febb9fb
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
2 changed files with 81 additions and 4 deletions

View File

@ -24,6 +24,42 @@ function! ale#code_action#HandleCodeAction(code_action, should_save) abort
endfor endfor
endfunction endfunction
function! s:ChangeCmp(left, right) abort
if a:left.start.line < a:right.start.line
return -1
endif
if a:left.start.line > a:right.start.line
return 1
endif
if a:left.start.offset < a:right.start.offset
return -1
endif
if a:left.start.offset > a:right.start.offset
return 1
endif
if a:left.end.line < a:right.end.line
return -1
endif
if a:left.end.line > a:right.end.line
return 1
endif
if a:left.end.offset < a:right.end.offset
return -1
endif
if a:left.end.offset > a:right.end.offset
return 1
endif
return 0
endfunction
function! ale#code_action#ApplyChanges(filename, changes, should_save) abort function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
let l:current_buffer = bufnr('') let l:current_buffer = bufnr('')
" The buffer is used to determine the fileformat, if available. " The buffer is used to determine the fileformat, if available.
@ -48,7 +84,8 @@ function! ale#code_action#ApplyChanges(filename, changes, should_save) abort
let l:column_offset = 0 let l:column_offset = 0
let l:last_end_line = 0 let l:last_end_line = 0
for l:code_edit in a:changes " Changes have to be sorted so we apply them from top-to-bottom.
for l:code_edit in sort(copy(a:changes), function('s:ChangeCmp'))
if l:code_edit.start.line isnot l:last_end_line if l:code_edit.start.line isnot l:last_end_line
let l:column_offset = 0 let l:column_offset = 0
endif endif

View File

@ -1,4 +1,8 @@
Before: Before:
Save g:ale_enabled
let g:ale_enabled = 0
runtime autoload/ale/code_action.vim runtime autoload/ale/code_action.vim
runtime autoload/ale/util.vim runtime autoload/ale/util.vim
@ -35,6 +39,8 @@ Before:
endfunction! endfunction!
After: After:
Restore
" Close the extra buffers if we opened it. " Close the extra buffers if we opened it.
if bufnr(g:file1) != -1 if bufnr(g:file1) != -1
execute ':bp! | :bd! ' . bufnr(g:file1) execute ':bp! | :bd! ' . bufnr(g:file1)
@ -50,9 +56,10 @@ After:
call delete(g:file2) call delete(g:file2)
endif endif
unlet g:file1 unlet! g:file1
unlet g:file2 unlet! g:file2
unlet g:test unlet! g:test
unlet! g:changes
delfunction WriteFileAndEdit delfunction WriteFileAndEdit
runtime autoload/ale/code_action.vim runtime autoload/ale/code_action.vim
@ -350,3 +357,36 @@ Execute(It should just modify file when should_save is set to v:false):
\ ' value: string', \ ' value: string',
\ '}', \ '}',
\], getline(1, '$') \], getline(1, '$')
Given typescript(An example TypeScript file):
type Foo = {}
export interface ISomething {
fooLongName: Foo | null
}
export class SomethingElse implements ISomething {
// Bindings
fooLongName!: ISomething['fooLongName']
}
Execute():
let g:changes = [
\ {'end': {'offset': 14, 'line': 4}, 'newText': 'foo', 'start': {'offset': 3, 'line': 4}},
\ {'end': {'offset': 40, 'line': 9}, 'newText': 'foo', 'start': {'offset': 29, 'line': 9}},
\ {'end': {'offset': 14, 'line': 9}, 'newText': 'foo', 'start': {'offset': 3, 'line': 9}},
\]
call ale#code_action#ApplyChanges(expand('%:p'), g:changes, 0)
Expect(The changes should be applied correctly):
type Foo = {}
export interface ISomething {
foo: Foo | null
}
export class SomethingElse implements ISomething {
// Bindings
foo!: ISomething['foo']
}