]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/filerename.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / filerename.vim
1 " Author: Dalius Dobravolskas <dalius.dobravolskas@gmail.com>
2 " Description: Rename file support for tsserver
3
4 let s:filerename_map = {}
5
6 " Used to get the rename map in tests.
7 function! ale#filerename#GetMap() abort
8     return deepcopy(s:filerename_map)
9 endfunction
10
11 " Used to set the rename map in tests.
12 function! ale#filerename#SetMap(map) abort
13     let s:filerename_map = a:map
14 endfunction
15
16 function! ale#filerename#ClearLSPData() abort
17     let s:filerename_map = {}
18 endfunction
19
20 function! s:message(message) abort
21     call ale#util#Execute('echom ' . string(a:message))
22 endfunction
23
24 function! ale#filerename#HandleTSServerResponse(conn_id, response) abort
25     if get(a:response, 'command', '') isnot# 'getEditsForFileRename'
26         return
27     endif
28
29     if !has_key(s:filerename_map, a:response.request_seq)
30         return
31     endif
32
33     let l:options = remove(s:filerename_map, a:response.request_seq)
34
35     let l:old_name = l:options.old_name
36     let l:new_name = l:options.new_name
37
38     if get(a:response, 'success', v:false) is v:false
39         let l:message = get(a:response, 'message', 'unknown')
40         call s:message('Error renaming file "' . l:old_name . '" to "' . l:new_name
41         \ . '". Reason: ' . l:message)
42
43         return
44     endif
45
46     let l:changes = a:response.body
47
48     if empty(l:changes)
49         call s:message('No changes while renaming "' . l:old_name . '" to "' . l:new_name . '"')
50     else
51         call ale#code_action#HandleCodeAction(
52         \   {
53         \       'description': 'filerename',
54         \       'changes': l:changes,
55         \   },
56         \   {
57         \       'should_save': 1,
58         \   },
59         \)
60     endif
61
62     silent! noautocmd execute 'saveas ' . l:new_name
63     call delete(l:old_name)
64 endfunction
65
66 function! s:OnReady(options, linter, lsp_details) abort
67     let l:id = a:lsp_details.connection_id
68
69     if !ale#lsp#HasCapability(l:id, 'filerename')
70         return
71     endif
72
73     let l:buffer = a:lsp_details.buffer
74
75     let l:Callback = function('ale#filerename#HandleTSServerResponse')
76
77     call ale#lsp#RegisterCallback(l:id, l:Callback)
78
79     let l:message = ale#lsp#tsserver_message#GetEditsForFileRename(
80     \   a:options.old_name,
81     \   a:options.new_name,
82     \)
83
84     let l:request_id = ale#lsp#Send(l:id, l:message)
85
86     let s:filerename_map[l:request_id] = a:options
87 endfunction
88
89 function! s:ExecuteFileRename(linter, options) abort
90     let l:buffer = bufnr('')
91
92     let l:Callback = function('s:OnReady', [a:options])
93     call ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback)
94 endfunction
95
96 function! ale#filerename#Execute() abort
97     let l:buffer = bufnr('')
98     let l:lsp_linters = []
99
100     for l:linter in ale#lsp_linter#GetEnabled(l:buffer)
101         if l:linter.lsp is# 'tsserver'
102             call add(l:lsp_linters, l:linter)
103         endif
104     endfor
105
106     if empty(l:lsp_linters)
107         call s:message('No active tsserver LSPs')
108
109         return
110     endif
111
112     let l:old_name = expand('#' . l:buffer . ':p')
113     let l:new_name = ale#util#Input('New file name: ', l:old_name, 'file')
114
115     if l:old_name is# l:new_name
116         call s:message('New file name matches old file name')
117
118         return
119     endif
120
121     if empty(l:new_name)
122         call s:message('New name cannot be empty!')
123
124         return
125     endif
126
127     for l:lsp_linter in l:lsp_linters
128         call s:ExecuteFileRename(l:lsp_linter, {
129         \   'old_name': l:old_name,
130         \   'new_name': l:new_name,
131         \})
132     endfor
133 endfunction