-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathimport.vim
310 lines (278 loc) · 8.75 KB
/
import.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
fun! s:FilterPrelude(respResults)
call filter(a:respResults, { idx, r -> index(g:psc_ide_prelude, r.module) == -1 })
endfun
fun! s:FilterTopFn(module, modules)
" module :: Array String
" modules :: Array (Array String)
let mods = map(copy(g:psc_ide_filter_submodules_do_not_hide), { idx, m -> split(m, '\.') })
return empty(filter(copy(a:modules), { idx, m -> s:IsSubmodule(a:module, m, mods) }))
endfun
fun! s:IsSubmodule(m1, m2, mods)
" is m1 a submodule of m2
" m1 :: Array String
" m2 :: Array String
if index(a:mods, a:m1) != -1
let res = v:false
else
if len(a:m1) > len(a:m2)
let res = a:m1[0:len(a:m2)-1] == a:m2 ? v:true : v:false
else
let res = v:false
endif
endif
return res
endfun
fun! s:FilterTop(respResults)
let modules = map(copy(a:respResults), { idx, r -> split(r.module, '\.') })
call filter(a:respResults, { idx, r -> s:FilterTopFn(split(r.module, '\.'), modules) })
endfun
function! purescript#ide#import#listImports(module, ...)
if a:0 >= 1
let qualifier = a:1
else
let qualifier = ""
endif
if a:0 >= 2
let ident = a:2
else
let ident = ""
endif
call purescript#ide#utils#update()
let filename = expand("%:p")
let resp = purescript#ide#callSync(
\ {'command': 'list', 'params': {'type': 'import', 'file': filename}},
\ 'Failed to get imports for: ' . a:module,
\ 0
\ )
call purescript#ide#utils#debug("PSCIDE purescript#ide#import#listImports result: " . string(resp), 3)
" Only need module names right now, so pluck just those.
if type(resp) == v:t_dict && resp['resultType'] ==# 'success'
" psc-ide >=0.11 returns imports on 'imports' property.
if type(resp.result) == v:t_list
let results = resp.result
else
let results = resp.result.imports
endif
if !empty(qualifier)
call filter(results, { idx, val -> get(val, "qualifier", "") == qualifier })
end
if !empty(ident)
call filter(results, {idx, val -> get(val, "importType", "") == "explicit" && has_key(val, "identifiers") ? index(val.identifiers, ident) != -1 : v:true})
endif
return results
else
call purescript#ide#handlePursError(resp)
return []
endif
endfunction
" Return line number of last import line (including blank lines).
"
" It will fail to find the proper line on
" ```
" import Prelude
" (Unit
" , bind
" , const)
" ```
"
" But it will run fine if all the lines are indented.
fun! purescript#ide#import#lastImportLine(lines)
let idx = len(a:lines) + 1
let import = v:false
for line in reverse(copy(a:lines))
let idx -= 1
if line =~# '^import\>'
break
endif
endfor
let nLine = get(a:lines, idx+1, "-")
while nLine =~# '^\s*$' || nLine =~# '^\s\+'
let idx += 1
let nLine = get(a:lines, idx, "-")
endwhile
return idx
endfun
" Import identifier callback
" resp - server response
" ident - identifier
" view - win view (as returned by `winsaveview()`)
" lines - number of lines in the buffer
" silent - do not output any messages
" rebuild - rebuild flag
" ignoreMultiple
" - ignore when received multiple results from the server
" fixCol - when invoked from `CompleteDone` autocommand, we need to add one
" to column. This works when one hits space to chose a completion
" result, while it moves the cursor when <C-E> is used (better than
" the other way around).
function! s:callback(resp, ident, view, lines, silent, rebuild, ignoreMultiple, fixCol)
if type(a:resp) != v:t_dict || get(a:resp, "resultType", "error") !=# "success"
if !a:silent && type(a:resp) == v:t_dict
return purescript#ide#utils#log(a:resp["result"])
else
return
endif
endif
if type(a:resp.result) == v:t_list && type(get(a:resp.result, 0, v:null)) == v:t_dict
" if v:false && type(a:resp.result) == v:t_list
" multiple possibilities
let respResults = a:resp.result
if g:psc_ide_filter_prelude_modules && len(filter(copy(respResults), { idx, r -> r.module ==# "Prelude" }))
" filter prelude modules (hopefully there are no identifires in prelude
" that clash
call s:FilterPrelude(respResults)
endif
if g:psc_ide_filter_submodules
call s:FilterTop(respResults)
endif
let results = []
for res in respResults
if empty(filter(copy(results), { idx, val -> val.module == res.module }))
call add(results, res)
endif
endfor
if (len(results) == 1)
let choice = { "option": results[0], "picked": v:true }
else
if !a:ignoreMultiple
let choice = purescript#ide#utils#pickOption("Multiple possibilities to import " . a:ident, results, "module")
else
return
endif
endif
if choice.picked == v:true
call purescript#ide#import#identifier(a:ident, choice.option.module)
endif
return
endif
let bLast = purescript#ide#import#lastImportLine(getline(1, '$'))
let nLast = purescript#ide#import#lastImportLine(a:resp.result)
exe "1," . bLast . "d_"
call append(0, a:resp.result[0:nLast - 1])
if mode() == 'i'
call feedkeys("\<C-g>u", "n")
endif
let a:view.topline = a:view.topline + line("$") - a:lines
let a:view.lnum = a:view.lnum + line("$") - a:lines
if a:fixCol
let a:view.col = a:view.col + 1
endif
call winrestview(a:view)
if mode() == 'i'
call feedkeys("\<C-g>u", "n")
endif
" trigger PSCIDErebuild
if a:rebuild
call purescript#ide#utils#update()
call PSCIDErebuild(v:true, "", function("PSCIDEerrors"))
endif
endfunction
" import identifier
" a:ident - the identifier (might be qualified)
" a:module - empty string or name of the module to search in
"
" Explicit a:module is used when there were multiple choices, to limit the
" choice, where it must be respected and also in
" `purescript#ide#imports#completeDone` where it might be modified.
function! purescript#ide#import#identifier(ident, module, ...)
call purescript#ide#utils#debug('PSCIDEimportIdentifier', 3)
call purescript#ide#utils#debug('ident: ' . a:ident, 3)
if a:0 >= 1
let silent = a:1
else
let silent = v:false
endif
if a:0 >= 2
let rebuild = a:2
else
let rebuild = v:true
endif
if a:0 >= 3
let ignoreMultiple = a:3
else
let ignoreMultiple = v:false
endif
if a:0 >= 4
let fixCol = a:4
else
let fixCol = v:false
endif
if (a:ident == "")
return
endif
if getline(".") =~ '^\s*import\>'
return
endif
let file = fnamemodify(bufname(""), ":p")
let [ident, qualifier] = purescript#ide#utils#splitQualifier(a:ident)
if !empty(a:module)
" When running through CompleteDone we need to preserve a:module. But
" also the module might not be imported yet with qualificaton or the
" qualified module was already imported in which case we'd limit the list
" of modules to `a:module` anyway.
let filters = [purescript#ide#utils#modulesFilter([a:module])]
let importCommand = {
\ "importCommand": "addImport",
\ "identifier": ident
\ }
elseif empty(qualifier)
let filters = []
let importCommand = {
\ "importCommand": "addImport",
\ "identifier": ident
\ }
else
" Otherwise filter imported modules by qualification
let currentModule = purescript#ide#utils#currentModule()
let imports = purescript#ide#import#listImports(currentModule, qualifier)
let modules = map(copy(imports), {key, val -> val["module"]})
if len(modules) > 0
let filters = [purescript#ide#utils#modulesFilter(modules)]
else
let filters = []
endif
let importCommand = {
\ "importCommand": "addImport",
\ "qualifier": qualifier,
\ "identifier": ident
\ }
endif
let input = {
\ 'command': 'import' ,
\ 'params': {
\ 'file': file,
\ 'filters': filters,
\ 'importCommand': importCommand
\ } }
if !empty(qualifier)
let input.params.importCommand.qualifier = qualifier
endif
let view = winsaveview()
let lines = line("$")
" updated the file
call purescript#ide#utils#update()
call purescript#ide#call(
\ input,
\ silent ? v:null : "Failed to import identifier " . a:ident,
\ 0,
\ {resp -> s:callback(resp, a:ident, view, lines, silent, rebuild, ignoreMultiple, fixCol)},
\ v:true
\ )
endfunction
" Import identifiers on completion. This differs from the import command in
" several ways:
" - run in silent mode (do not disturb when a user is typing)
" - do not rebuild when done
" - ignore when there is no unique result
" - add 1 to col after completion (see s:callback for explanation)
fun! purescript#ide#import#completeDone()
if !g:psc_ide_import_on_completion
return
endif
if !has_key(v:completed_item, "word")
return
endif
let ident = v:completed_item["word"]
let module = get(split(v:completed_item["info"]), 0, "")
call purescript#ide#import#identifier(ident, module, v:true, v:false, v:true, v:true)
endfun