]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/organize_imports.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 / organize_imports.vim
1 " Author: Jerko Steiner <jerko.steiner@gmail.com>
2 " Description: Organize imports support for tsserver
3
4 function! ale#organize_imports#HandleTSServerResponse(conn_id, response) abort
5     if get(a:response, 'command', '') isnot# 'organizeImports'
6         return
7     endif
8
9     if get(a:response, 'success', v:false) isnot v:true
10         return
11     endif
12
13     let l:file_code_edits =  a:response.body
14
15     call ale#code_action#HandleCodeAction(
16     \   {
17     \       'description': 'Organize Imports',
18     \       'changes': l:file_code_edits,
19     \   },
20     \   {
21     \       'conn_id': a:conn_id,
22     \       'should_save': g:ale_save_hidden || !&hidden,
23     \   },
24     \)
25 endfunction
26
27 function! s:OnReady(linter, lsp_details) abort
28     let l:id = a:lsp_details.connection_id
29
30     if a:linter.lsp isnot# 'tsserver'
31         call ale#util#Execute('echom ''OrganizeImports currently only works with tsserver''')
32
33         return
34     endif
35
36     let l:buffer = a:lsp_details.buffer
37
38     let l:Callback = function('ale#organize_imports#HandleTSServerResponse')
39
40     call ale#lsp#RegisterCallback(l:id, l:Callback)
41
42     let l:message = ale#lsp#tsserver_message#OrganizeImports(l:buffer)
43
44     let l:request_id = ale#lsp#Send(l:id, l:message)
45 endfunction
46
47 function! s:OrganizeImports(linter) abort
48     let l:buffer = bufnr('')
49     let [l:line, l:column] = getpos('.')[1:2]
50
51     if a:linter.lsp isnot# 'tsserver'
52         let l:column = min([l:column, len(getline(l:line))])
53     endif
54
55     let l:Callback = function('s:OnReady')
56     call ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback)
57 endfunction
58
59 function! ale#organize_imports#Execute() abort
60     for l:linter in ale#lsp_linter#GetEnabled(bufnr(''))
61         call s:OrganizeImports(l:linter)
62     endfor
63 endfunction