]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/java/eclipselsp.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:

Merge commit '56df844d3c39ec494dacc69eae34272b27db185a' as '.vim/bundle/asyncomplete'
[etc/vim.git] / .vim / bundle / ale / ale_linters / java / eclipselsp.vim
1 " Author: Horacio Sanson <https://github.com/hsanson>
2 " Description: Support for the Eclipse language server https://github.com/eclipse/eclipse.jdt.ls
3
4 let s:version_cache = {}
5
6 call ale#Set('java_eclipselsp_path', ale#path#Simplify($HOME . '/eclipse.jdt.ls'))
7 call ale#Set('java_eclipselsp_config_path', '')
8 call ale#Set('java_eclipselsp_workspace_path', '')
9 call ale#Set('java_eclipselsp_executable', 'java')
10 call ale#Set('java_eclipselsp_javaagent', '')
11
12 function! ale_linters#java#eclipselsp#Executable(buffer) abort
13     return ale#Var(a:buffer, 'java_eclipselsp_executable')
14 endfunction
15
16 function! ale_linters#java#eclipselsp#TargetPath(buffer) abort
17     return ale#Var(a:buffer, 'java_eclipselsp_path')
18 endfunction
19
20 function! ale_linters#java#eclipselsp#JarPath(buffer) abort
21     let l:path = ale_linters#java#eclipselsp#TargetPath(a:buffer)
22
23     if has('win32')
24         let l:platform = 'win32'
25     elseif has('macunix')
26         let l:platform = 'macosx'
27     else
28         let l:platform = 'linux'
29     endif
30
31     " Search jar file within repository path when manually built using mvn
32     let l:files = globpath(l:path, '**/'.l:platform.'/**/plugins/org.eclipse.equinox.launcher_*\.jar', 1, 1)
33
34     if len(l:files) >= 1
35         return l:files[0]
36     endif
37
38     " Search jar file within VSCode extensions folder.
39     let l:files = globpath(l:path, '**/'.l:platform.'/plugins/org.eclipse.equinox.launcher_*\.jar', 1, 1)
40
41     if len(l:files) >= 1
42         return l:files[0]
43     endif
44
45     " Search jar file within unzipped tar.gz file
46     let l:files = globpath(l:path, 'plugins/org.eclipse.equinox.launcher_*\.jar', 1, 1)
47
48     if len(l:files) >= 1
49         return l:files[0]
50     endif
51
52     " Search jar file within system package path
53     let l:files = globpath('/usr/share/java/jdtls/plugins', 'org.eclipse.equinox.launcher_*\.jar', 1, 1)
54
55     if len(l:files) >= 1
56         return l:files[0]
57     endif
58
59     return ''
60 endfunction
61
62 function! ale_linters#java#eclipselsp#ConfigurationPath(buffer) abort
63     let l:path = fnamemodify(ale_linters#java#eclipselsp#JarPath(a:buffer), ':p:h:h')
64     let l:config_path = ale#Var(a:buffer, 'java_eclipselsp_config_path')
65
66     if !empty(l:config_path)
67         return ale#path#Simplify(l:config_path)
68     endif
69
70     if has('win32')
71         let l:path = l:path . '/config_win'
72     elseif has('macunix')
73         let l:path = l:path . '/config_mac'
74     else
75         let l:path = l:path . '/config_linux'
76     endif
77
78     return ale#path#Simplify(l:path)
79 endfunction
80
81 function! ale_linters#java#eclipselsp#VersionCheck(version_lines) abort
82     return s:GetVersion('', a:version_lines)
83 endfunction
84
85 function! s:GetVersion(executable, version_lines) abort
86     let l:version = []
87
88     for l:line in a:version_lines
89         let l:match = matchlist(l:line, '\(\d\+\)\.\(\d\+\)\.\(\d\+\)')
90
91         if !empty(l:match)
92             let l:version = [l:match[1] + 0, l:match[2] + 0, l:match[3] + 0]
93             let s:version_cache[a:executable] = l:version
94             break
95         endif
96     endfor
97
98     return l:version
99 endfunction
100
101 function! ale_linters#java#eclipselsp#CommandWithVersion(buffer, version_lines, meta) abort
102     let l:executable = ale_linters#java#eclipselsp#Executable(a:buffer)
103     let l:version = s:GetVersion(l:executable, a:version_lines)
104
105     return ale_linters#java#eclipselsp#Command(a:buffer, l:version)
106 endfunction
107
108 function! ale_linters#java#eclipselsp#WorkspacePath(buffer) abort
109     let l:wspath = ale#Var(a:buffer, 'java_eclipselsp_workspace_path')
110
111     if !empty(l:wspath)
112         return l:wspath
113     endif
114
115     return ale#path#Dirname(ale#java#FindProjectRoot(a:buffer))
116 endfunction
117
118 function! ale_linters#java#eclipselsp#Javaagent(buffer) abort
119     let l:rets = []
120     let l:raw = ale#Var(a:buffer, 'java_eclipselsp_javaagent')
121
122     if empty(l:raw)
123         return ''
124     endif
125
126     let l:jars = split(l:raw)
127
128     for l:jar in l:jars
129         call add(l:rets, ale#Escape('-javaagent:' . l:jar))
130     endfor
131
132     return join(l:rets, ' ')
133 endfunction
134
135 function! ale_linters#java#eclipselsp#Command(buffer, version) abort
136     let l:path = ale#Var(a:buffer, 'java_eclipselsp_path')
137
138     let l:executable = ale_linters#java#eclipselsp#Executable(a:buffer)
139
140     let l:cmd = [ ale#Escape(l:executable),
141     \ ale_linters#java#eclipselsp#Javaagent(a:buffer),
142     \ '-Declipse.application=org.eclipse.jdt.ls.core.id1',
143     \ '-Dosgi.bundles.defaultStartLevel=4',
144     \ '-Declipse.product=org.eclipse.jdt.ls.core.product',
145     \ '-Dlog.level=ALL',
146     \ '-noverify',
147     \ '-Xmx1G',
148     \ '-jar',
149     \ ale#Escape(ale_linters#java#eclipselsp#JarPath(a:buffer)),
150     \ '-configuration',
151     \ ale#Escape(ale_linters#java#eclipselsp#ConfigurationPath(a:buffer)),
152     \ '-data',
153     \ ale#Escape(ale_linters#java#eclipselsp#WorkspacePath(a:buffer))
154     \ ]
155
156     if ale#semver#GTE(a:version, [1, 9])
157         call add(l:cmd, '--add-modules=ALL-SYSTEM')
158         call add(l:cmd, '--add-opens java.base/java.util=ALL-UNNAMED')
159         call add(l:cmd, '--add-opens java.base/java.lang=ALL-UNNAMED')
160     endif
161
162     return join(l:cmd, ' ')
163 endfunction
164
165 function! ale_linters#java#eclipselsp#RunWithVersionCheck(buffer) abort
166     let l:executable = ale_linters#java#eclipselsp#Executable(a:buffer)
167
168     if empty(l:executable)
169         return ''
170     endif
171
172     let l:cache = s:version_cache
173
174     if has_key(s:version_cache, l:executable)
175         return ale_linters#java#eclipselsp#Command(a:buffer, s:version_cache[l:executable])
176     endif
177
178     let l:command = ale#Escape(l:executable) . ' -version'
179
180     return ale#command#Run(
181     \ a:buffer,
182     \ l:command,
183     \ function('ale_linters#java#eclipselsp#CommandWithVersion'),
184     \ { 'output_stream': 'both' }
185     \)
186 endfunction
187
188 call ale#linter#Define('java', {
189 \   'name': 'eclipselsp',
190 \   'lsp': 'stdio',
191 \   'executable': function('ale_linters#java#eclipselsp#Executable'),
192 \   'command': function('ale_linters#java#eclipselsp#RunWithVersionCheck'),
193 \   'language': 'java',
194 \   'project_root': function('ale#java#FindProjectRoot'),
195 \   'initialization_options': {
196 \     'extendedClientCapabilities': {
197 \       'classFileContentsSupport': v:true
198 \     }
199 \   }
200 \})