-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathpurescript_pscide.vim
1344 lines (1196 loc) · 40.2 KB
/
purescript_pscide.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" Inits ----------------------------------------------------------------------
if !exists("b:loaded_psc_ide_vim")
let b:loaded_psc_ide_vim = v:true
else
finish
endif
if !exists("g:loaded_psc_ide_vim")
let g:loaded_psc_ide_vim = v:false
endif
if !exists('g:psc_ide_suggestions')
let g:psc_ide_suggestions = {}
endif
" Options -------------------------------------------------------------------
if !exists('g:psc_ide_log_level')
let g:psc_ide_log_level = 0
endif
if !exists('g:psc_ide_auto_imports')
let g:psc_ide_auto_imports = 0
endif
if !exists('g:psc_ide_server_port')
let g:psc_ide_server_port = 4242
endif
if !exists('g:psc_ide_check_output_dir')
let g:psc_ide_check_output_dir = 1
endif
if !exists('g:psc_ide_notify')
let g:psc_ide_notify = v:true
endif
if !exists('g:psc_ide_filter_prelude_modules')
let g:psc_ide_filter_prelude_modules = v:true
endif
if !exists('g:psc_ide_omnicompletion_filter_modules')
let g:psc_ide_omnicompletion_filter_modules = v:false
endif
if !exists('g:psc_ide_omnicompletion_sort_by')
" flex / identifier / module
let g:psc_ide_omnicompletion_sort_by = "flex"
endif
if !exists('g:psc_ide_codegen')
" js / corefn / sourcemaps
let g:psc_ide_codegen = ['js']
endif
if !exists('g:psc_ide_import_on_completion')
let g:psc_ide_import_on_completion = v:true
endif
if !exists("g:psc_ide_omnicompletion_prefix_filter")
" with this option will let purs ide filter by prefix (this disables flex
" matching) (tip: use i^xu when searching for a command)
let g:psc_ide_omnicompletion_prefix_filter = v:true
endif
if !exists('g:psc_ide_log_level')
let g:psc_ide_log_level = 0
endif
if !exists("g:psc_ide_prelude")
let g:psc_ide_prelude = [
\ "Control.Applicative",
\ "Control.Apply",
\ "Control.Bind",
\ "Control.Category",
\ "Control.Monad",
\ "Control.Semigroupoid",
\ "Data.Boolean",
\ "Data.BooleanAlgebra",
\ "Data.Bounded",
\ "Data.CommutativeRing",
\ "Data.Eq",
\ "Data.EuclideanRing",
\ "Data.Field",
\ "Data.Function",
\ "Data.Functor",
\ "Data.HeytingAlgebra",
\ "Data.NaturalTransformation",
\ "Data.Ord",
\ "Data.Ordering",
\ "Data.Ring",
\ "Data.Semigroup",
\ "Data.Semiring",
\ "Data.Show",
\ "Data.Unit",
\ "Data.Void",
\ ]
endif
if !exists('g:psc_ide_filter_submodules')
" this might hide some modules, e.g. React.DOM.Dynamic will be hidden by
" React.DOM module, you can adjust g:psc_ide_filter_submodules_do_not_hide
" variable.
let g:psc_ide_filter_submodules = v:false
endif
if !exists("g:psc_ide_filter_submodules_do_not_hide")
let g:psc_ide_filter_submodules_do_not_hide = [ "React.DOM.Dynamic" ]
endif
" Adding iskeyword symbols to improve <cword> expansion- ---------------------
" 124 = |
setl omnifunc=PSCIDEomni
setl completefunc=PSCIDEcomplete
" Syntastic initialization ---------------------------------------------------
if exists('g:syntastic_extra_filetypes')
call add(g:syntastic_extra_filetypes, 'purescript')
else
let g:syntastic_extra_filetypes = ['purescript']
endif
let g:syntastic_purescript_checkers = ['pscide']
" Check if vim has support for module names (non standard)
let loclist = getloclist(0)
call setloclist(0, [{"module": "X"}])
let s:vim_module_names = has_key(get(getloclist(0), 0, {}), "module")
call setloclist(0, loclist)
" COMMANDS -------------------------------------------------------------------
com! -buffer -bang
\ PaddClause
\ call PSCIDEaddClause(<q-bang>)
com! -buffer
\ PaddImportQualifications
\ call PSCIDEaddImportQualifications()
com! -buffer
\ PaddType
\ call PSCIDEaddTypeAnnotation(matchstr(getline(line(".")), '^\s*\zs\k\+\ze'))
com! -buffer -bang
\ Papply
\ call PSCIDEapplySuggestion(<q-bang>)
com! -buffer -bang -nargs=1
\ Pcase
\ call PSCIDEcaseSplit(<q-bang>, <q-args>)
com! -buffer
\ Pcwd
\ call PSCIDEcwd()
com! -buffer
\ Pend
\ call PSCIDEend()
com! -buffer -nargs=* -bang -complete=custom,PSCIDEcompleteIdentifier
\ Pgoto
\ call PSCIDEgoToDefinition(<q-bang>, len(<q-args>) ? <q-args> : PSCIDEgetKeyword())
com! -buffer -nargs=* -complete=custom,PSCIDEcompleteIdentifier
\ Pimport
\ call PSCIDEimportIdentifier(len(<q-args>) ? <q-args> : PSCIDEgetKeyword())
com! -buffer
\ Plist
\ call PSCIDElist()
com! -buffer
\ Pimports
\ call PSCIDElistImports()
com! -buffer -bang
\ Pload
\ call PSCIDEload(0, <q-bang>)
com! -buffer Pvalidate
\ call PSCIDEprojectValidate(v:false)
com! -buffer -nargs=*
\ Pursuit
\ call PSCIDEpursuit(len(<q-args>) ? <q-args> : PSCIDEgetKeyword())
com! -buffer -bang
\ Prebuild
\ call PSCIDErebuild(v:true, <q-bang>, function("PSCIDEerrors"))
com! -buffer
\ Pstart
\ call PSCIDEstart(0)
com! -buffer -nargs=* -complete=custom,PSCIDEcompleteIdentifier
\ Ptype
\ call PSCIDEtype(len(<q-args>) ? <q-args> : PSCIDEgetKeyword(), v:true)
com! -buffer -nargs=1 -complete=custom,PSCIDEcompleteIdentifier
\ Psearch
\ call PSCIDEsearch(len(<q-args>) ? <q-args> : PSCIDEgetKeyword())
com! -buffer -nargs=* -complete=custom,PSCIDEimportModuleCompletion
\ PimportModule
\ call PSCIDEimportModule(len(<q-args>) ? <q-args> : PSCIDEgetKeyword())
" AUTOSTART ------------------------------------------------------------------
fun! s:autoStart()
if exists("g:psc_ide_syntastic_mode") && g:psc_ide_syntastic_mode == 0
augroup purescript
au! BufWritePost *.purs call PSCIDErebuild(v:true, "", function("PSCIDEerrors"))
au! BufAdd *.purs call PSCIDErebuild(v:true, "", function("PSCIDEerrors"))
augroup END
endif
silent! call PSCIDEstart(0)
silent! call PSCIDEload(0, "")
endfun
" INTERNALS -------------------------------------------------------------------
" execute only once so we do not redefine functions when they are running
if g:loaded_psc_ide_vim
call s:autoStart()
finish
endif
let g:loaded_psc_ide_vim = v:true
augroup purescript_CompleteDone
au!
au CompleteDone * :call purescript#ide#import#completeDone()
augroup END
" START ----------------------------------------------------------------------
let s:psc_ide_server = v:null
"Looks for bower.json, assumes that's the root directory, starts
"`purs ide server` in the background
"Returns Nothing
function! PSCIDEstart(silent)
if purescript#ide#started()
return
endif
let loglevel = a:silent == 1 ? 1 : 0
let dir = purescript#ide#utils#findRoot()
if empty(dir)
echom "No psc-package.json, bower.json or spago.dhall found, couldn't start `purs ide server`"
return
endif
let command = [
\ "purs", "ide", "server",
\ "-p", g:psc_ide_server_port,
\ "-d", dir,
\ "src/**/*.purs",
\ "bower_components/**/*.purs",
\ ]
if executable("spago")
let fullCommand = command + systemlist("spago sources")
else
let fullCommand = command
endif
exe "lcd" dir
call purescript#ide#utils#debug("PSCIDEstart: " . json_encode(fullCommand), 3)
let jobid = purescript#job#start(
\ fullCommand,
\ { "on_stderr": { ch, msg -> purescript#ide#utils#warn(purescript#ide#utils#toString(msg), v:true) }
\ , "on_stdout": { ch, msg -> type(msg) == v:t_string ? purescript#ide#utils#log(msg) : v:null }
\ , "on_exit": function("s:onServerExit")
\ }
\ )
lcd -
sleep 100m
call purescript#ide#setStarted(v:true)
endfunction
let s:onServerExit = v:true
function! s:onServerExit(ch, msg, ev)
if s:onServerExit
call purescript#ide#utils#log(purescript#ide#utils#toString(a:ev), v:true)
else
call purescript#ide#setStarted(v:false)
endif
endfunction
if v:version > 704 || (v:version == 704 && has('patch279'))
function! s:globpath(dir, pattern) abort
return globpath(a:dir, a:pattern, 1, 1)
endfunction
else
function! s:globpath(dir, pattern) abort
return split(globpath(a:dir, a:pattern, 1), '\n')
endfunction
endif
" END ------------------------------------------------------------------------
" Tell the `purs ide server` to quit
function! PSCIDEend()
if purescript#ide#external()
return
endif
let jobid = purescript#job#start(
\ ["purs", "ide", "client", "-p", g:psc_ide_server_port],
\ { "on_exit": {job, status, ev -> s:PSCIDEendCallback() }
\ , "on_stderr": {err -> purescript#ide#utils#log(string(err), v:true)}
\ })
call purescript#job#send(jobid, json_encode({'command': 'quit'}) . "\n")
endfunction
function! s:PSCIDEendCallback()
call purescript#ide#setStarted(v:false)
call purescript#ide#setValid(v:false)
endfunction
function! s:projectProblems()
let rootdir = purescript#ide#utils#findRoot()
let problems = []
if empty(rootdir)
call add(problems, "Your project is missing a bower.json, psc-package.json or spago.dhall file")
elseif g:psc_ide_check_output_dir == 1
let outputcontent = s:globpath(rootdir, "output/*")
if len(outputcontent) == 0
call add(problems, "Your project's /output directory is empty. You should run `pulp build` to compile your project.")
endif
endif
return problems
endfunction
" LOAD -----------------------------------------------------------------------
" Load module of current buffer + its dependencies into `purs ide server`
function! PSCIDEload(logLevel, bang, ...)
let hasCb = a:0 >= 1 && type(a:1) == v:t_func
if hasCb
let Fn = { resp -> a:1(s:PSCIDEloadCallback(a:logLevel, resp)) }
else
let Fn = { resp -> s:PSCIDEloadCallback(a:logLevel, resp) }
endif
if a:bang == "!"
return purescript#ide#call(
\ {"command": "reset"},
\ "failed to reset",
\ 0,
\ { resp -> resp["resultType"] == "success" ? PSCIDEload(a:logLevel, "", hasCb ? a:1 : v:null) : "" }
\ )
endif
let input = {'command': 'load'}
call purescript#ide#call(
\ input,
\ "Failed to load",
\ 0,
\ Fn
\ )
endfunction
function! s:PSCIDEloadCallback(logLevel, resp)
if type(a:resp) != v:t_dict || get(a:resp, "resultType", "error") !=# "success"
return purescript#ide#handlePursError(a:resp)
endif
call purescript#ide#utils#log(tolower(a:resp["result"]))
return a:resp
endfunction
" Import given identifier
function! PSCIDEimportIdentifier(ident)
call purescript#ide#import#identifier(a:ident, "")
endfunction
fun! s:completeCommand(ident, qualifier, ...)
let currentModule = purescript#ide#utils#currentModule()
let modules = a:0 >= 1 ? a:1 : v:null
if type(modules) == v:t_list
let filters = [purescript#ide#utils#modulesFilter(modules)]
elseif !empty(a:qualifier)
let modules = map(purescript#ide#import#listImports(currentModule, a:qualifier), { idx, val -> val["module"] })
let filters = [purescript#ide#utils#modulesFilter(modules)]
else
let filters = []
endif
return
\ {'command': 'complete'
\ , 'params':
\ { 'matcher': empty(a:ident) ? {} : s:flexMatcher(a:ident)
\ , 'options': { 'groupReexports': v:true }
\ , 'filters': filters
\ }
\ }
endfun
fun! PSCIDEcompleteIdentifier(argLead, cmdLead, cursorPos)
let res = s:completeFn(v:false, a:argLead, function("s:completeCommand"))
return join(uniq(sort(map(res, {idx, r -> r.word}))), "\n")
endfun
function! PSCIDEgoToDefinition(bang, ident)
let currentModule = purescript#ide#utils#currentModule()
let [ident, qualifier] = purescript#ide#utils#splitQualifier(a:ident)
let imports = purescript#ide#import#listImports(currentModule, qualifier, a:bang != "!" ? ident : "")
if a:bang == "!"
if empty(qualifier)
let filters = []
else
let modules = map(copy(imports), {key, val -> val["module"]})
let filters = [purescript#ide#utils#modulesFilter(modules)]
endif
else
let modules = map(copy(imports), {key, val -> val["module"]})
call add(modules, currentModule)
call add(modules, "Prim")
let filters = [purescript#ide#utils#modulesFilter(modules)]
endif
call purescript#ide#call(
\ {'command': 'type', 'params': {'search': ident, 'filters': filters}, 'currentModule': currentModule},
\ 'Failed to get location info for: ' . a:ident,
\ 0,
\ { resp -> s:PSCIDEgoToDefinitionCallback(a:bang, a:ident, resp) }
\ )
endfunction
function! s:PSCIDEgoToDefinitionCallback(bang, ident, resp)
if type(a:resp) != v:t_dict || get(a:resp, "resultType", "error") !=# "success"
return purescript#ide#handlePursError(a:resp)
endif
let results = []
for res in a:resp.result
if empty(filter(copy(results), { idx, val ->
\ type(val.definedAt) == v:t_dict
\ && type(res.definedAt) == v:t_dict
\ && val.definedAt.name == res.definedAt.name
\ && val.definedAt.start[0] == res.definedAt.start[0]}))
call add(results, res)
endif
endfor
if a:bang != "!" && empty(results)
" try again with a bang
return PSCIDEgoToDefinition("!", a:ident)
endif
if len(results) > 1
let choice = purescript#ide#utils#pickOption("Multiple possibilities for " . a:ident, results, "module")
elseif len(results) == 1
let choice = {"picked": v:true, "option": results[0]}
else
let choice = {"picked": v:false, "option": v:null}
endif
if choice.picked && type(choice.option.definedAt) == type({})
call s:goToDefinition(choice.option.definedAt)
elseif type(choice.option) == v:t_dict
call purescript#ide#utils#warn("no location information found for: " . a:ident . " in module " . choice.option.module)
else
call purescript#ide#utils#warn("no location information found for: " . a:ident)
endif
endfunction
function! s:goToDefinition(definedAt)
let currentfile = expand("%:p")
let fname = a:definedAt.name
let cwd = purescript#ide#utils#findRoot()
let fname = fnameescape(findfile(fname, cwd))
if (currentfile == fname)
" set ' mark at the current position
m'
call cursor(a:definedAt.start[0], a:definedAt.start[1])
else
call purescript#ide#utils#debug("PSCIDE s:goToDefinition: fname: " . fname, 3)
let command = "e +" . a:definedAt.start[0] . " " . fname
call purescript#ide#utils#debug("PSCIDE s:goToDefinition: command: " . command, 3)
exe command
exe "normal " . a:definedAt.start[1] . "|"
endif
endfunction
function! PSCIDErebuild(async, bang, ...)
let filename = expand("%:p")
let input = {'command': 'rebuild', 'params': {'file': filename, 'codegen': g:psc_ide_codegen}}
if a:0 >= 1 && type(a:1) == v:t_func
let CallBack = a:1
else
let CallBack = {resp -> resp}
endif
if a:0 >= 2
let silent = a:2
else
let silent = v:false
endif
if a:async
if a:bang == "!"
call PSCIDEload(0, "!", { resp -> PSCIDErebuild(a:async, "", CallBack )})
else
call purescript#ide#call(
\ input,
\ "failed to rebuild",
\ 0,
\ { msg -> CallBack(s:PSCIDErebuildCallback(filename, msg, silent)) }
\ )
endif
else
let resp = s:PSCIDErebuildCallback(
\ filename,
\ purescript#ide#callSync(input, 0, 0),
\ silent
\ )
return CallBack(resp)
endif
endfunction
function! s:PSCIDErebuildCallback(filename, resp, silent)
let g:psc_ide_suggestions = {}
if type(a:resp) == v:t_dict && has_key(a:resp, "resultType")
\ && has_key (a:resp, "result") && type(a:resp.result) == v:t_list
let out = s:qfList(a:filename, a:resp.result, a:resp.resultType)
let g:psc_ide_suggestions = out.suggestions
return out.qfList
else
if !a:silent
call purescript#ide#utils#error("failed to rebuild")
endif
return []
endif
endfunction
" Add type annotation
function! PSCIDEaddTypeAnnotation(ident)
call s:getType(
\ a:ident,
\ v:true,
\ { resp -> s:PSCIDEaddTypeAnnotationCallback(a:ident, resp) }
\ )
endfunction
function! s:PSCIDEaddTypeAnnotationCallback(ident, resp)
if type(a:resp) != v:t_dict || get(a:resp, "resultType", "error") !=# "success"
return purescript#ide#handlePursError(a:resp)
endif
if !empty(a:resp["result"])
let result = a:resp["result"]
let lnr = line(".")
let indent = matchstr(getline(lnr), '^\s*\ze')
call append(lnr - 1, indent . s:StripNewlines(result[0]['identifier']) . ' :: ' . s:StripNewlines(result[0]["type"]))
else
call purescript#ide#utils#warn("no type information found for " .a:ident)
endif
endfunction
" CWD ------------------------------------------------------------------------
" Get current working directory of `pure ide server`
function! PSCIDEcwd()
call purescript#ide#call(
\ {'command': 'cwd'},
\ "Failed to get current working directory",
\ 0,
\ function("s:PSCIDEcwdCallback")
\ )
endfunction
function! s:PSCIDEcwdCallback(resp)
if type(a:resp) != v:t_dict || get(a:resp, "resultType", "error") !=# "success"
return purescript#ide#handlePursError(a:resp)
endif
call purescript#ide#utils#log("current working directory: " . a:resp.result)
endfunction
" ADDCLAUSE
" Makes template function implementation from signature
function! PSCIDEaddClause(bang)
let lnr = line(".")
let line = getline(lnr)
let command = {
\ 'command': 'addClause',
\ 'params':
\ { 'line': line
\ , 'annotations': a:bang == "!" ? v:true : v:false
\ }
\ }
call purescript#ide#call(
\ command,
\ "Failed to add clause",
\ 0,
\ { resp -> s:PSCIDEaddClauseCallback(lnr, resp) }
\ )
endfunction
function! s:PSCIDEaddClauseCallback(lnr, resp)
if type(a:resp) != v:t_dict || get(a:resp, "resultType", "error") !=# "success"
return purescript#ide#handlePursError(a:resp)
endif
call purescript#ide#utils#debug('PSCIDEaddClause results: ' . string(a:resp.result), 3)
call append(a:lnr, a:resp.result)
normal dd
endfunction
" CASESPLIT
" Hover cursor over variable in function declaration -> pattern match on all
" different cases of the variable
function! PSCIDEcaseSplit(bang, ...)
if (a:0 >= 1)
let type = a:1
else
let type = input("Please provide a type: ")
endif
let winview = winsaveview()
let lnr = line(".")
let begin = s:findStart()
let line = getline(lnr)
let len = len(matchstr(line[begin:], '^\k*'))
let word = line[:len]
call winrestview(winview)
let command = {
\ 'command': 'caseSplit',
\ 'params':
\ { 'line': line
\ , 'begin': begin
\ , 'end': begin + len
\ , 'annotations': a:bang == "!" ? v:true : v:false
\ , 'type': type
\ }
\ }
call purescript#ide#call(
\ command,
\ 'Failed to split case for: ' . word,
\ 0,
\ { resp -> s:PSCIDEcaseSplitCallback(lnr, type, resp) }
\ )
endfunction
function! s:PSCIDEcaseSplitCallback(lnr, type, resp)
if type(a:resp) != v:t_dict || get(a:resp, "resultType", "error") !=# "success"
if get(a:resp, "result", "") == "Not Found"
call purescript#ide#utils#error("type `" . a:type . "` not found", v:true)
else
call purescript#ide#handlePursError(a:resp)
endif
return
endif
call append(a:lnr, a:resp.result)
normal dd
normal $
endfunction
" TYPE -----------------------------------------------------------------------
" Get type of word under cursor
function! PSCIDEtype(ident, filterModules)
call s:getType(
\ a:ident,
\ a:filterModules,
\ { resp -> s:PSCIDEtypeCallback(a:ident, resp.result, a:filterModules) }
\ )
endfunction
function! s:PSCIDEtypeCallback(ident, result, filterModules)
if type(a:result) == v:t_list && !empty(a:result)
let filePadding = min([max(map(copy(a:result), { i, r -> type(r.definedAt) == v:t_dict && has_key(r.definedAt, "name") ? len(r.definedAt.name) : 0})) + 1, 30])
let modulePadding = min([max(map(copy(a:result), { i, r -> type(r.module) == v:t_string ? len(r.module) : 0})) + 1, 30])
call setloclist(0, map(a:result, { idx, r -> s:formattype(r, filePadding, modulePadding)}))
call setloclist(0, [], 'a', {'title': 'PureScript Types'})
lopen
wincmd p
elseif a:filterModules
call PSCIDEtype(a:ident, v:false)
else
call purescript#ide#utils#log("no type information found for " . a:ident)
endif
endfunction
" LISTIMPORTS -----------------------------------------------------------------------
" List the modules imported by the current module
function! PSCIDElistImports()
let currentModule = purescript#ide#utils#currentModule()
call purescript#ide#utils#debug('PSCIDElistImports ' . currentModule, 3)
let imports = purescript#ide#import#listImports(currentModule)
for import in imports
call s:echoImport(import)
endfor
if (len(imports) == 0)
echom "PSC-IDE: No import information found for " . currentModule
endif
endfunction
function! s:echoImport(import)
echohl Identifier
echon a:import["module"]
echohl Normal
if has_key(a:import, "identifiers")
echon " ("
let len = len(a:import["identifiers"])
let idx = 0
for ident in a:import["identifiers"]
echohl Identifier
echon ident
echohl Normal
if (idx < len - 1)
echon ", "
else
echon ")"
endif
let idx += 1
endfor
endif
if has_key(a:import, "qualifier")
echohl Keyword
echon " as "
echohl Identifier
echon a:import["qualifier"]
echohl Normal
endif
echon "\n"
endfunction
function! s:getType(ident, filterModules, cb)
let currentModule = purescript#ide#utils#currentModule()
let [ident, qualifier] = purescript#ide#utils#splitQualifier(a:ident)
let imports = purescript#ide#import#listImports(currentModule, qualifier, a:filterModules ? ident : "")
let modules = map(copy(imports), {key, val -> val["module"]})
call add(modules, currentModule)
let filters = [purescript#ide#utils#modulesFilter(modules)]
call purescript#ide#utils#debug('PSCIDE s:getType currentModule: ' . currentModule, 3)
call purescript#ide#call(
\ { 'command': 'type'
\ , 'params':
\ { 'search': ident
\ , 'filters': filters
\ , 'currentModule': currentModule
\ }
\ },
\ 'Failed to get type info for: ' . a:ident,
\ 0,
\ {resp -> a:cb(resp)}
\ )
endfunction
function! s:formattype(record, filePadding, modulePadding)
let definedAt = a:record.definedAt
if type(definedAt) != v:t_dict
" v:null's are ignored by vim's setqflist()
let definedAt = {"name": "", "start": [v:null, v:null]}
endif
let entry =
\ { "filename": s:vim_module_names ? printf("%-" . a:filePadding . "s", definedAt["name"]) : ""
\ , "module": empty(a:record["module"]) ? "" : printf("%-" . a:modulePadding . "s", a:record["module"])
\ , "lnum": definedAt["start"][0]
\ , "col": definedAt["start"][1]
\ , "text": s:CleanEnd(s:StripNewlines(a:record['identifier']) . ' ∷ ' . s:StripNewlines(a:record['type']))
\ }
return entry
endfunction
" APPLYSUGGESTION ------------------------------------------------------
" Apply suggestion in loclist to buffer --------------------------------
function! PSCIDEapplySuggestion(bang)
if empty(a:bang)
call PSCIDEapplySuggestionPrime(expand("%:p") . "|" . line("."), v:true, 0)
else
let l = 0
let len = len(keys(g:psc_ide_suggestions))
while l < len
" PSCIDEapplySuggestionPrime will change g:psc_ide_suggestions keys on
" the fly
let keys = keys(g:psc_ide_suggestions)
if len(keys) > 0
let key = keys[0]
call PSCIDEapplySuggestionPrime(key, v:true, 0)
else
break
endif
endwhile
endif
endfunction
function! PSCIDEapplySuggestionPrime(key, cursor, silent)
call purescript#ide#utils#debug('PSCIDEapplySuggestion: a:key: ' . a:key, 3)
if (has_key(g:psc_ide_suggestions, a:key))
let sugg = g:psc_ide_suggestions[a:key]
else
if !a:silent
call purescript#ide#utils#debug('PSCIDEapplySuggestion: No suggestion found', 0)
endif
return
endif
call purescript#ide#utils#debug('PSCIDEapplySuggestion: Suggestion found: ' . string(sugg), 3)
let replacement = sugg.replacement
let range = sugg.replaceRange
let startLine = range.startLine
let startColumn = range.startColumn
let endLine = range.endLine
let endColumn = range.endColumn
if startLine == endLine
let line = getline(startLine)
" remove trailing new lines
let replacement = substitute(replacement, '\_s*$', "\n", '')
" add identation to each line (except first one)
" and remove trailing white space from each line
let RSpace = { line -> substitute(line, '\s*$', '', '') }
let replacement = join(
\ map(
\ split(replacement, "\n"),
\ { idx, line -> idx == 0 ? RSpace(line) : repeat(" ", startColumn) . RSpace(line)}
\ ),
\ "\n")
let cursor = getcurpos()
if startColumn == 1
let newLines = split(replacement . "\n" . line[endColumn - 1:], "\n")
else
let newLines = split(line[0:startColumn - 2] . replacement . "\n" . line[endColumn - 1:], "\n")
endif
exe startLine . "d _"
call append(startLine - 1, newLines)
if a:cursor
call cursor(cursor[1], startColumn - 1)
endif
call remove(g:psc_ide_suggestions, a:key)
let g:psc_ide_suggestions = s:updateSuggestions(startLine, len(newLines) - 1)
" trigger PSCIDErebuild
call purescript#ide#utils#update()
call PSCIDErebuild(v:true, "", function("PSCIDEerrors"))
else
call purescript#ide#utils#debug("multiline suggestions are not supported in vim - please grab g:psc_ide_suggestions and open an issue")
endif
endfunction
fun! s:updateSuggestions(startLine, newLines)
let suggestions = {}
for key in keys(g:psc_ide_suggestions)
let sug = g:psc_ide_suggestions[key]
if sug.replaceRange.startLine < a:startLine
let suggestions[key] = sug
else
let keyParts = split(key, "|")
let keyParts[len(keyParts) - 1] = sug.replaceRange.startLine + a:newLines
let newKey = join(keyParts, "|")
let sug.replaceRange.startLine = sug.replaceRange.startLine + a:newLines
let sug.replaceRange.endLine = sug.replaceRange.endLine + a:newLines
let suggestions[newKey] = sug
endif
endfor
return suggestions
endfun
" Add all import qualifications
function! PSCIDEaddImportQualifications()
let foundLines = []
let filename = expand("%:p")
let oldCursorPos = getcurpos()
call cursor(1, 0)
let found = searchpos("import", "W")
while found != [0,0]
let foundLines = insert(foundLines, found[0]) " Insert = unshift -> list is in reverse = what we want because of deleting
call cursor(found[0], 0)
let found = searchpos("import", "W")
endwhile
call purescript#ide#utils#debug('Adding import qualifications to : ' . string(foundLines), 3)
for lnr in foundLines
call PSCIDEapplySuggestionPrime(lnr, filename, 1)
endfor
call cursor(oldCursorPos[1], oldCursorPos[2])
endfunction
" PURSUIT --------------------------------------------------------------------
function! PSCIDEpursuit(ident)
call purescript#ide#call(
\ {'command': 'pursuit', 'params': {'query': a:ident, 'type': "completion"}},
\ 'Failed to get pursuit info for: ' . a:ident,
\ 0,
\ { resp -> s:PSCIDEpursuitCallback(resp) }
\ )
endfunction
function! s:PSCIDEpursuitCallback(resp)
if type(a:resp) != v:t_dict || get(a:resp, "resultType", "error") !=# "success"
return purescript#ide#handlePursError(a:resp)
endif
call setloclist(0, map(a:resp.result, { idx, r -> { "text": s:formatpursuit(r) }}))
call setloclist(0, [], 'a', {'title': 'Pursuit'})
lopen
wincmd p
endfunction
function! s:formatpursuit(record)
let package = s:CleanEnd(s:StripNewlines(get(a:record, "package", "")))
let module = s:CleanEnd(s:StripNewlines(get(a:record, "module", "")))
let ident = s:CleanEnd(s:StripNewlines(get(a:record, "ident", "")))
let type = get(a:record, "type", "")
if empty(type)
let type = ""
else
let type = "∷ " . s:CleanEnd(s:StripNewlines(type))
endif
return printf("%-20s %s.%s %s", package, module, ident, type)
endfunction
" VALIDATE -------------------------------------------------------------------
function! PSCIDEprojectValidate(silent)
let problems = s:projectProblems()
if len(problems) == 0
call purescript#ide#setValid(v:true)
if !a:silent
call purescript#ide#utils#log("your project is setup correctly")
endif
else
call purescript#ide#setValid(v:true)
call purescript#ide#utils#warn("your project is not setup correctly. " . join(problems))
endif
endfunction
" LIST -----------------------------------------------------------------------
function! PSCIDElist()
let resp = purescript#ide#callSync(
\ {'command': 'list', 'params': {'type': 'loadedModules'}},
\ 'Failed to get loaded modules',
\ 0
\ )
call s:PSCIDElistCallback(resp)
endfunction
function! s:PSCIDElistCallback(resp)
if type(a:resp) == v:t_dict && a:resp['resultType'] ==# 'success'
if len(a:resp["result"]) > 0
for m in a:resp["result"]
echom m
endfor
endif
elseif type(a:resp) == v:t_dict
call purescript#ide#utils#error(get(a:resp, "result", "error"))
endif
endfunction
fun! s:findStart()
let col = col(".")
let line = getline(".")
" search backwards for start of identifier (iskeyword pattern)
let start = col
while start > 0 && (line[start - 2] =~ '\(\k\|[<>$#+*/%''&=!:~?^-]\)' || line[start - 2] =~ '\.')
let start -= 1
endwhile
"Looking for the start of the identifier that we want to complete
return start - 1
endfun
" COMPLETION FUNCTION --------------------------------------------------------
fun! s:completeFn(findstart, base, commandFn,...)
let completeImportLine = a:0 >= 1 ? a:1 : v:false
if a:findstart
return s:findStart()
else
let [ident, qualifier] = purescript#ide#utils#splitQualifier(a:base)
let command = v:null
if completeImportLine
let line = getline(".")
let sline = line[0:col(".")-1]
if sline =~ '^\s*import\s\+[a-zA-Z.]*$'
let resp = purescript#ide#callSync(
\ {"command": "list", "params": {"type": "loadedModules"}},
\ "Failed to get loaded modules",
\ 0
\ )
let res = get(resp, "result", [])
if (type(res) != v:t_list)
let res = []
endif
let len = len(a:base)
let mlen = len(split(a:base, '\.', v:true))
return filter(res, { idx, val -> val[0:len-1] == a:base })
elseif line =~ '^\s*import\s*[a-zA-Z.]\+\s*('
let moduleName = matchstr(line, '^\s*import\>\s*\<\zs[a-zA-Z.]\+\>\ze')
let command = a:commandFn(ident, "", [moduleName])
endif
endif
if type(command) != v:t_dict
let command = a:commandFn(ident, qualifier)
endif
if empty(command)
return
endif
let resp = purescript#ide#callSync(
\ command,
\ 'Failed to get completions for: '. a:base,
\ 0)
let entries = get(resp, "result", [])
"Popuplating the omnicompletion list
let result = []