/
lsp-ada_documents.adb
1168 lines (972 loc) · 39.5 KB
/
lsp-ada_documents.adb
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
------------------------------------------------------------------------------
-- Language Server Protocol --
-- --
-- Copyright (C) 2018-2023, AdaCore --
-- --
-- This is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. This software is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with this software; see file --
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license. --
------------------------------------------------------------------------------
with Ada.Tags;
with GNAT.Strings;
with GNATCOLL.Traces;
with GNATCOLL.VFS;
with Langkit_Support.Symbols;
with Langkit_Support.Text;
with Laltools.Common;
with Pp.Scanner;
with Utils.Char_Vectors;
with Libadalang.Iterators;
with Libadalang.Sources;
with VSS.Characters.Latin;
with VSS.Strings.Character_Iterators;
with VSS.Strings.Conversions;
with VSS.Strings.Formatters.Integers;
with VSS.Strings.Formatters.Strings;
with VSS.Strings.Templates;
with LSP.Ada_Completions.Filters;
with LSP.Ada_Contexts;
with LSP.Ada_Documentation;
with LSP.Ada_Documents.LAL_Diagnostics;
with LSP.Ada_Handlers.Locations;
with LSP.Ada_Id_Iterators;
with LSP.Enumerations;
with LSP.Formatters.File_Names;
with LSP.Formatters.Texts;
with LSP.Predicates;
with LSP.Structures.LSPAny_Vectors;
with LSP.Utils;
package body LSP.Ada_Documents is
pragma Warnings (Off);
package Utils renames Standard.Utils;
Lal_PP_Output : constant GNATCOLL.Traces.Trace_Handle :=
GNATCOLL.Traces.Create
("ALS.LAL_PP_OUTPUT_ON_FORMATTING", GNATCOLL.Traces.Off);
-- Logging lalpp output if On
function To_Completion_Kind (K : LSP.Enumerations.SymbolKind)
return LSP.Enumerations.CompletionItemKind
is
(case K is
when LSP.Enumerations.A_Function => LSP.Enumerations.A_Function,
when LSP.Enumerations.Field => LSP.Enumerations.Field,
when LSP.Enumerations.Variable => LSP.Enumerations.Variable,
when LSP.Enumerations.A_Package => LSP.Enumerations.Module,
when LSP.Enumerations.Module => LSP.Enumerations.Module,
when LSP.Enumerations.Class => LSP.Enumerations.Class,
when LSP.Enumerations.Struct => LSP.Enumerations.Class,
when LSP.Enumerations.Number => LSP.Enumerations.Value,
when LSP.Enumerations.Enum => LSP.Enumerations.Enum,
when LSP.Enumerations.String => LSP.Enumerations.Value,
when LSP.Enumerations.A_Constant => LSP.Enumerations.Value,
when others => LSP.Enumerations.Reference);
-- Convert a SymbolKind to a CompletionItemKind.
-- TODO: It might be better to have a unified kind, and then convert to
-- specific kind types, but for the moment this is good enough.
-------------
-- Cleanup --
-------------
procedure Cleanup (Self : in out Document) is
begin
for Source of Self.Diagnostic_Sources loop
LSP.Diagnostic_Sources.Unchecked_Free (Source);
end loop;
end Cleanup;
-----------------------------
-- Compute_Completion_Item --
-----------------------------
function Compute_Completion_Item
(Document : LSP.Ada_Documents.Document;
Handler : in out LSP.Ada_Handlers.Message_Handler;
Context : LSP.Ada_Contexts.Context;
Sloc : Langkit_Support.Slocs.Source_Location;
Node : Libadalang.Analysis.Ada_Node;
BD : Libadalang.Analysis.Basic_Decl;
Label : VSS.Strings.Virtual_String;
Use_Snippets : Boolean;
Compute_Doc_And_Details : Boolean;
Named_Notation_Threshold : Natural;
Is_Dot_Call : Boolean;
Is_Visible : Boolean;
Pos : Integer;
Weight : Ada_Completions.Completion_Item_Weight_Type;
Completions_Count : Natural)
return LSP.Structures.CompletionItem
is
package Weight_Formatters renames VSS.Strings.Formatters.Integers;
Item : LSP.Structures.CompletionItem;
Subp_Spec_Node : Libadalang.Analysis.Base_Subp_Spec;
Min_Width : constant Natural := Completions_Count'Image'Length - 1;
-- The -1 remove the whitespace added by 'Image
Last_Weight : constant Ada_Completions.Completion_Item_Weight_Type :=
Ada_Completions.Completion_Item_Weight_Type'Last;
function Get_Sort_Text
(Base_Label : VSS.Strings.Virtual_String)
return VSS.Strings.Virtual_String;
-- Return a suitable sortText according to the completion item's
-- visibility and position in the completion list.
-------------------
-- Get_Sort_Text --
-------------------
function Get_Sort_Text
(Base_Label : VSS.Strings.Virtual_String)
return VSS.Strings.Virtual_String
is
use VSS.Strings;
begin
return Sort_Text : VSS.Strings.Virtual_String do
Sort_Text := VSS.Strings.Templates.Format
("{:02}&{:05}{}",
Weight_Formatters.Image (Last_Weight - Weight),
Weight_Formatters.Image (Pos),
VSS.Strings.Formatters.Strings.Image (Base_Label));
if not Is_Visible then
Sort_Text.Prepend ('~');
end if;
end return;
end Get_Sort_Text;
begin
Item.label := Label;
Item.kind := (True, To_Completion_Kind (LSP.Utils.Get_Decl_Kind (BD)));
if not Is_Visible then
Item.insertText := Label;
Item.label.Append (" (invisible)");
Item.filterText := Label;
end if;
Item.sortText := Get_Sort_Text (Label);
Set_Completion_Item_Documentation
(Handler => Handler,
Context => Context,
BD => BD,
Item => Item,
Compute_Doc_And_Details => Compute_Doc_And_Details);
-- Return immediately if we should not use snippets (e.g: completion for
-- invisible symbols).
if not Use_Snippets then
return Item;
end if;
-- Check if we are dealing with a subprogram and return a completion
-- snippet that lists all the formal parameters if it's the case.
Subp_Spec_Node := BD.P_Subp_Spec_Or_Null;
if Subp_Spec_Node.Is_Null then
return Item;
end if;
declare
Insert_Text : VSS.Strings.Virtual_String := Label;
All_Params : constant Libadalang.Analysis.Param_Spec_Array :=
Subp_Spec_Node.P_Params;
Params : constant Libadalang.Analysis.Param_Spec_Array :=
(if Is_Dot_Call then
All_Params (All_Params'First + 1 .. All_Params'Last)
else
All_Params);
-- Remove the first formal parameter from the list when the dotted
-- notation is used.
Idx : Positive := 1;
Nb_Params : Natural := 0;
Use_Named_Notation : Boolean := False;
begin
-- Create a completion snippet if the subprogram expects some
-- parameters.
if Params'Length /= 0 then
Item.insertTextFormat :=
(Is_Set => True,
Value => LSP.Enumerations.Snippet);
Insert_Text.Append (" (");
-- Compute number of params to know if named notation should be
-- used.
for Param of Params loop
Nb_Params := Nb_Params + Param.F_Ids.Children_Count;
end loop;
Use_Named_Notation := Named_Notation_Threshold > 0
and then Nb_Params >= Named_Notation_Threshold;
for Param of Params loop
for Id of Param.F_Ids loop
declare
Mode : constant Langkit_Support.Text.Text_Type :=
Param.F_Mode.Text;
Mode_Text : constant Langkit_Support.Text.Text_Type :=
(if Mode /= "" then Mode & " " else "");
Named_Template : constant
VSS.Strings.Templates.Virtual_String_Template :=
"{} => ${{{}:{}{}}, ";
Positional_Template : constant
VSS.Strings.Templates.Virtual_String_Template :=
"${{{}:{} : {}{}}, ";
Text : VSS.Strings.Virtual_String;
begin
if Use_Named_Notation then
Text :=
Named_Template.Format
(LSP.Formatters.Texts.Image (Id.Text),
VSS.Strings.Formatters.Integers.Image (Idx),
LSP.Formatters.Texts.Image (Mode_Text),
LSP.Formatters.Texts.Image
(Param.F_Type_Expr.Text));
else
Text :=
Positional_Template.Format
(VSS.Strings.Formatters.Integers.Image (Idx),
LSP.Formatters.Texts.Image (Id.Text),
LSP.Formatters.Texts.Image (Mode_Text),
LSP.Formatters.Texts.Image
(Param.F_Type_Expr.Text));
end if;
Insert_Text.Append (Text);
Idx := Idx + 1;
end;
end loop;
end loop;
-- Remove the ", " substring that has been appended in the last
-- loop iteration.
declare
First : constant
VSS.Strings.Character_Iterators.Character_Iterator :=
Insert_Text.At_First_Character;
Last : VSS.Strings.Character_Iterators.Character_Iterator :=
Insert_Text.At_Last_Character;
Success : Boolean with Unreferenced;
begin
Success := Last.Backward;
Success := Last.Backward;
Insert_Text := Insert_Text.Slice (First, Last);
-- ??? May be replaced by "Head" like procedure when it will be
-- implemented.
end;
-- Insert '$0' (i.e: the final tab stop) at the end.
Insert_Text.Append (")$0");
Item.insertText := Insert_Text;
end if;
end;
return Item;
end Compute_Completion_Item;
-------------------------
-- Find_All_References --
-------------------------
procedure Find_All_References
(Self : Document; Context : LSP.Ada_Contexts.Context;
Definition : Libadalang.Analysis.Defining_Name;
Callback : not null access procedure
(Base_Id : Libadalang.Analysis.Base_Id;
Kind : Libadalang.Common.Ref_Result_Kind; Cancel : in out Boolean))
is
Units : constant Libadalang.Analysis.Analysis_Unit_Array :=
(1 => LSP.Ada_Documents.Unit (Self => Self,
Context => Context));
begin
LSP.Ada_Id_Iterators.Find_All_References (Definition, Units, Callback);
exception
when E : Libadalang.Common.Property_Error =>
Self.Tracer.Trace_Exception (E, "in Find_All_References");
end Find_All_References;
----------------
-- Formatting --
----------------
function Formatting
(Self : Document;
Context : LSP.Ada_Contexts.Context;
Span : LSP.Structures.A_Range;
Cmd : Pp.Command_Lines.Cmd_Line;
Edit : out LSP.Structures.TextEdit_Vector;
Messages : out VSS.String_Vectors.Virtual_String_Vector) return Boolean
is
use type Libadalang.Slocs.Source_Location_Range;
use type LSP.Structures.A_Range;
Sloc : constant Libadalang.Slocs.Source_Location_Range :=
(if Span = LSP.Constants.Empty
then Libadalang.Slocs.No_Source_Location_Range
else Self.To_Source_Location_Range (Span));
Input : Utils.Char_Vectors.Char_Vector;
Output : Utils.Char_Vectors.Char_Vector;
Out_Span : LSP.Structures.A_Range;
PP_Messages : Pp.Scanner.Source_Message_Vector;
Out_Sloc : Libadalang.Slocs.Source_Location_Range;
S : GNAT.Strings.String_Access;
begin
if Span /= LSP.Constants.Empty then
-- Align Span to line bounds
if Span.start.character /= 0 then
return Self.Formatting
(Context => Context,
Span => ((Span.start.line, 0), Span.an_end),
Cmd => Cmd,
Edit => Edit,
Messages => Messages);
elsif Span.an_end.character /= 0 then
return Self.Formatting
(Context => Context,
Span => (Span.start, (Span.an_end.line + 1, 0)),
Cmd => Cmd,
Edit => Edit,
Messages => Messages);
end if;
end if;
S := new String'(VSS.Strings.Conversions.To_UTF_8_String (Self.Text));
Input.Append (S.all);
GNAT.Strings.Free (S);
LSP.Utils.Format_Vector
(Cmd => Cmd,
Input => Input,
Node => Self.Unit (Context).Root,
In_Sloc => Sloc,
Output => Output,
Out_Sloc => Out_Sloc,
Messages => PP_Messages);
-- Properly format the messages received from gnatpp, using the
-- the GNAT standard way for messages (i.e: <filename>:<sloc>: <msg>)
if not PP_Messages.Is_Empty then
declare
File : constant GNATCOLL.VFS.Virtual_File :=
Context.URI_To_File (Self.URI);
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{}:{}:{}: {}";
begin
for Error of PP_Messages loop
Messages.Append
(Template.Format
(LSP.Formatters.File_Names.Image (File),
VSS.Strings.Formatters.Integers.Image (Error.Sloc.Line),
VSS.Strings.Formatters.Integers.Image (Error.Sloc.Col),
VSS.Strings.Formatters.Strings.Image
(VSS.Strings.Conversions.To_Virtual_String
(String
(Utils.Char_Vectors.Char_Vectors.To_Array
(Error.Text))))));
end loop;
return False;
end;
end if;
S := new String'(Output.To_Array);
if Lal_PP_Output.Is_Active then
Lal_PP_Output.Trace (S.all);
end if;
if Span = LSP.Constants.Empty then
-- diff for the whole document
Self.Diff
(VSS.Strings.Conversions.To_Virtual_String (S.all),
Edit => Edit);
elsif Out_Sloc = Libadalang.Slocs.No_Source_Location_Range then
-- Range formating fails. Do nothing, skip formating altogether
null;
else
-- diff for a part of the document
Out_Span := Self.To_A_Range (Out_Sloc);
-- Use line diff if the range is too wide
if Span.an_end.line - Span.start.line > 5 then
Self.Diff
(VSS.Strings.Conversions.To_Virtual_String (S.all),
Span,
Out_Span,
Edit);
else
declare
Formatted : constant VSS.Strings.Virtual_String :=
VSS.Strings.Conversions.To_Virtual_String (S.all);
Slice : VSS.Strings.Virtual_String;
begin
LSP.Utils.Span_To_Slice (Formatted, Out_Span, Slice);
Self.Diff_Symbols (Span, Slice, Edit);
end;
end if;
end if;
GNAT.Strings.Free (S);
return True;
exception
when E : others =>
Lal_PP_Output.Trace (E);
GNAT.Strings.Free (S);
return False;
end Formatting;
--------------------
-- Get_Any_Symbol --
--------------------
procedure Get_Any_Symbol
(Self : in out Document; Context : LSP.Ada_Contexts.Context;
Pattern : LSP.Search.Search_Pattern'Class;
Limit : Ada.Containers.Count_Type;
Only_Public : Boolean;
Canceled : access function return Boolean;
Result : in out LSP.Ada_Completions.Completion_Maps.Map)
is
procedure Refresh_Symbol_Cache;
-- Find intresting definings names in the document and put them
-- into Self.Symbol_Cache
procedure Insert
(Item : Name_Information;
Name : Libadalang.Analysis.Defining_Name);
-- Populate Result with the name information if Result doesn't have
-- the Name already
function Get_Defining_Name
(Loc : Langkit_Support.Slocs.Source_Location)
return Libadalang.Analysis.Defining_Name;
-----------------------
-- Get_Defining_Name --
-----------------------
function Get_Defining_Name
(Loc : Langkit_Support.Slocs.Source_Location)
return Libadalang.Analysis.Defining_Name
is
Unit : constant Libadalang.Analysis.Analysis_Unit :=
Self.Unit (Context);
Name : constant Libadalang.Analysis.Name :=
Laltools.Common.Get_Node_As_Name (Unit.Root.Lookup (Loc));
begin
return Laltools.Common.Get_Name_As_Defining (Name);
end Get_Defining_Name;
------------
-- Insert --
------------
procedure Insert
(Item : Name_Information;
Name : Libadalang.Analysis.Defining_Name) is
begin
if not Result.Contains (Name) and then
(not Only_Public or else Item.Is_Public)
then
Result.Insert
(Name,
(Is_Dot_Call => False,
Is_Visible => False,
Use_Snippets => False,
Pos => <>,
Weight => <>));
end if;
end Insert;
--------------------------
-- Refresh_Symbol_Cache --
--------------------------
procedure Refresh_Symbol_Cache is
use Langkit_Support.Symbols;
use Libadalang.Common;
use Libadalang.Iterators;
Node : Libadalang.Analysis.Ada_Node;
Global_Visible : constant Libadalang.Iterators.Ada_Node_Predicate :=
LSP.Predicates.Is_Global_Visible;
Restricted_Kind : constant Libadalang.Iterators.Ada_Node_Predicate :=
LSP.Predicates.Is_Restricted_Kind;
-- Find all definings names excluding private parts and bodies
It : Libadalang.Iterators.Traverse_Iterator'Class :=
Libadalang.Iterators.Find
(Self.Unit (Context).Root,
Libadalang.Iterators.Kind_Is (Ada_Defining_Name)
and not Restricted_Kind);
begin
while It.Next (Node) loop
declare
Token : constant Token_Reference := Node.Token_End;
Text : constant Langkit_Support.Text.Text_Type :=
Libadalang.Common.Text (Token);
Canonical : constant Symbolization_Result :=
Libadalang.Sources.Canonicalize (Text);
Cursor : Symbol_Maps.Cursor;
Inserted : Boolean;
begin
if Canonical.Success then
Self.Symbol_Cache.Insert
(VSS.Strings.To_Virtual_String (Canonical.Symbol),
Name_Vectors.Empty_Vector,
Cursor,
Inserted);
Self.Symbol_Cache (Cursor).Append
(Name_Information'
(Langkit_Support.Slocs.Start_Sloc (Node.Sloc_Range),
Global_Visible.Unchecked_Get.Evaluate (Node)));
end if;
end;
end loop;
end Refresh_Symbol_Cache;
Cursor : Symbol_Maps.Cursor;
use type LSP.Search.Search_Kind;
-- In "Celling" mode we scan only range of cache where a key prefix
-- matches lowercased pattern as is.
Use_Celling : constant Boolean :=
not Pattern.Get_Negate
and then ((Pattern.Get_Kind = LSP.Enumerations.Full_Text
and then Pattern.Get_Whole_Word)
or else Pattern.Get_Kind = LSP.Enumerations.Start_Word_Text);
begin
if Self.Refresh_Symbol_Cache then
Refresh_Symbol_Cache;
Self.Refresh_Symbol_Cache := False;
end if;
if Use_Celling then
Cursor := Self.Symbol_Cache.Ceiling (Pattern.Get_Canonical_Pattern);
else
Cursor := Self.Symbol_Cache.First;
end if;
while Symbol_Maps.Has_Element (Cursor) loop
if Use_Celling
and then not Pattern.Match (Symbol_Maps.Key (Cursor))
then
-- We use "Celling mode" and key stops matching,
-- Symbol_Cache is ordered so we will not find any
-- matches more
exit when Use_Celling or else Canceled.all;
else
for Item of Self.Symbol_Cache (Cursor) loop
declare
Defining_Name : constant Libadalang.Analysis.Defining_Name :=
Get_Defining_Name (Item.Loc);
begin
-- Match each element individually in case of sensitive
-- search or non-celling mode
if not Defining_Name.Is_Null
and then
((Use_Celling
and then not Pattern.Get_Case_Sensitive)
or else Pattern.Match
(VSS.Strings.To_Virtual_String
(Defining_Name.As_Ada_Node.Text)))
then
Insert (Item, Defining_Name);
end if;
exit when Canceled.all;
end;
end loop;
end if;
Symbol_Maps.Next (Cursor);
end loop;
end Get_Any_Symbol;
-------------------------
-- Get_Completion_Node --
-------------------------
procedure Get_Completion_Node
(Self : Document;
Context : LSP.Ada_Contexts.Context;
Position : LSP.Structures.Position;
Sloc : out Langkit_Support.Slocs.Source_Location;
Token : out Libadalang.Common.Token_Reference;
Node : out Libadalang.Analysis.Ada_Node)
is
use Libadalang.Common;
function Completion_Token
(Sloc : Langkit_Support.Slocs.Source_Location)
return Libadalang.Common.Token_Reference;
-- Get token under completion for given cursor position.
-- If cursor at the first symbol of a token return previous token:
-- XXX___
-- ^ cursor just after a token mean user is completion XXX token.
----------------------
-- Completion_Token --
----------------------
function Completion_Token
(Sloc : Langkit_Support.Slocs.Source_Location)
return Libadalang.Common.Token_Reference
is
use type Langkit_Support.Slocs.Source_Location;
Token : constant Libadalang.Common.Token_Reference :=
Self.Get_Token_At (Context, Position);
Prev : constant Libadalang.Common.Token_Reference :=
(if Token = Libadalang.Common.No_Token
then Token
else Libadalang.Common.Previous (Token));
begin
if Libadalang.Common.No_Token not in Token | Prev then
declare
Data : constant Libadalang.Common.Token_Data_Type :=
Libadalang.Common.Data (Token);
Start : constant Langkit_Support.Slocs.Source_Location :=
Langkit_Support.Slocs.Start_Sloc
(Libadalang.Common.Sloc_Range (Data));
begin
if Start = Sloc then
return Prev;
end if;
end;
end if;
return Token;
end Completion_Token;
begin
Sloc := Self.To_Source_Location (Position);
Token := Completion_Token (Sloc);
declare
From : constant Langkit_Support.Slocs.Source_Location :=
Langkit_Support.Slocs.Start_Sloc
(Libadalang.Common.Sloc_Range
(Libadalang.Common.Data (Token)));
Root : constant Libadalang.Analysis.Ada_Node :=
Self.Unit (Context).Root;
begin
Node := (if Root.Is_Null then Root else Root.Lookup (From));
end;
end Get_Completion_Node;
------------------------
-- Get_Completions_At --
------------------------
procedure Get_Completions_At
(Self : Document;
Providers : LSP.Ada_Completions.Completion_Provider_List;
Context : LSP.Ada_Contexts.Context;
Sloc : Langkit_Support.Slocs.Source_Location;
Token : Libadalang.Common.Token_Reference;
Node : Libadalang.Analysis.Ada_Node;
Names : out Ada_Completions.Completion_Maps.Map;
Result : out LSP.Structures.CompletionList)
is
Parent : constant Libadalang.Analysis.Ada_Node :=
(if Node.Is_Null then Node else Node.Parent);
Filter : LSP.Ada_Completions.Filters.Filter;
begin
if not Parent.Is_Null
and then (Parent.Kind not in
Libadalang.Common.Ada_Dotted_Name | Libadalang.Common.Ada_End_Name
and then Node.Kind in Libadalang.Common.Ada_String_Literal_Range)
then
-- Do nothing when inside a string
return;
end if;
Self.Tracer.Trace
("Getting completions, Pos = ("
& Sloc.Line'Image & ", " & Sloc.Column'Image & ") Node = "
& Libadalang.Analysis.Image (Node));
Filter.Initialize (Token, Node);
for Provider of Providers loop
begin
Provider.Propose_Completion
(Sloc => Sloc,
Token => Token,
Node => Node,
Filter => Filter,
Names => Names,
Result => Result);
exception
when E : Libadalang.Common.Property_Error =>
Self.Tracer.Trace_Exception
(E,
"LAL EXCEPTION occurred with following completion provider");
Self.Tracer.Trace (Ada.Tags.Expanded_Name (Provider'Tag));
end;
end loop;
Self.Tracer.Trace
("Number of filtered completions : " & Names.Length'Image);
end Get_Completions_At;
----------------
-- Get_Errors --
----------------
procedure Get_Errors
(Self : in out Document;
Context : LSP.Ada_Contexts.Context;
Changed : out Boolean;
Errors : out LSP.Structures.Diagnostic_Vector;
Force : Boolean := False)
is
begin
Errors.Clear;
Changed := (for some Source of Self.Diagnostic_Sources =>
Source.Has_New_Diagnostic (Context));
if Changed or else Force then
for Source of Self.Diagnostic_Sources loop
Source.Get_Diagnostic (Context, Errors);
end loop;
end if;
end Get_Errors;
---------------------------
-- Get_Formatting_Region --
---------------------------
function Get_Formatting_Region
(Self : Document;
Context : LSP.Ada_Contexts.Context;
Position : LSP.Structures.Position)
return Laltools.Partial_GNATPP.Formatting_Region_Type
is (Laltools.Partial_GNATPP.Get_Formatting_Region
(Unit => Self.Unit (Context),
Input_Range =>
Self.To_Source_Location_Range ((Position, Position))));
---------------------
-- Get_Indentation --
---------------------
function Get_Indentation
(Self : Document;
Context : LSP.Ada_Contexts.Context;
Line : Positive)
return VSS.Strings.Character_Count
is
(VSS.Strings.Character_Count
(Laltools.Partial_GNATPP.Estimate_Indentation
(Self.Unit (Context),
Self.To_Source_Location ((Line, 1)).Line)));
-----------------
-- Get_Node_At --
-----------------
function Get_Node_At
(Self : Document;
Context : LSP.Ada_Contexts.Context;
Position : LSP.Structures.Position) return Libadalang.Analysis.Ada_Node
is
Unit : constant Libadalang.Analysis.Analysis_Unit := Self.Unit (Context);
begin
return (if Unit.Root.Is_Null then Libadalang.Analysis.No_Ada_Node
else Unit.Root.Lookup (Self.To_Source_Location (Position)));
end Get_Node_At;
--------------------------
-- Get_Symbol_Hierarchy --
--------------------------
procedure Get_Symbol_Hierarchy
(Self : Document; Context : LSP.Ada_Contexts.Context;
Pattern : LSP.Search.Search_Pattern'Class;
Canceled : access function return Boolean;
Result : out LSP.Structures.DocumentSymbol_Vector)
is
begin
pragma Compile_Time_Warning
(Standard.True, "Get_Symbol_Hierarchy unimplemented");
raise Program_Error with "Unimplemented procedure Get_Symbol_Hierarchy";
end Get_Symbol_Hierarchy;
-----------------
-- Get_Symbols --
-----------------
procedure Get_Symbols
(Self : Document; Context : LSP.Ada_Contexts.Context;
Pattern : LSP.Search.Search_Pattern'Class;
Canceled : access function return Boolean;
Result : out LSP.Structures.DocumentSymbol_Vector)
is
begin
pragma Compile_Time_Warning (Standard.True, "Get_Symbols unimplemented");
raise Program_Error with "Unimplemented procedure Get_Symbols";
end Get_Symbols;
------------------
-- Get_Token_At --
------------------
function Get_Token_At
(Self : Document'Class; Context : LSP.Ada_Contexts.Context;
Position : LSP.Structures.Position)
return Libadalang.Common.Token_Reference
is
(Self.Unit (Context).Lookup_Token (Self.To_Source_Location (Position)));
-----------------
-- Get_Word_At --
-----------------
function Get_Word_At
(Self : Document;
Context : LSP.Ada_Contexts.Context;
Position : LSP.Structures.Position) return VSS.Strings.Virtual_String
is
use Langkit_Support.Slocs;
use all type Libadalang.Common.Token_Kind;
Result : VSS.Strings.Virtual_String;
Unit : constant Libadalang.Analysis.Analysis_Unit :=
Self.Unit (Context);
Origin : constant Source_Location := Self.To_Source_Location (Position);
Where : constant Source_Location := (Origin.Line, Origin.Column - 1);
-- Compute the position we want for completion, which is one character
-- before the cursor.
Token : constant Libadalang.Common.Token_Reference :=
Unit.Lookup_Token (Where);
Data : constant Libadalang.Common.Token_Data_Type :=
Libadalang.Common.Data (Token);
Kind : constant Libadalang.Common.Token_Kind :=
Libadalang.Common.Kind (Data);
Text : constant Langkit_Support.Text.Text_Type :=
Libadalang.Common.Text (Token);
Sloc : constant Source_Location_Range :=
Libadalang.Common.Sloc_Range (Data);
Span : constant Integer :=
Natural (Where.Column) - Natural (Sloc.Start_Column);
begin
if Kind in Ada_Identifier .. Ada_Xor
and then Compare (Sloc, Where) = Inside
then
Result := VSS.Strings.To_Virtual_String
(Text (Text'First .. Text'First + Span));
end if;
return Result;
end Get_Word_At;
---------------------
-- Has_Diagnostics --
---------------------
function Has_Diagnostics
(Self : Document; Context : LSP.Ada_Contexts.Context) return Boolean
is
(Self.Unit (Context).Has_Diagnostics);
----------------
-- Initialize --
----------------
procedure Initialize
(Self : in out Document;
URI : LSP.Structures.DocumentUri;
Text : VSS.Strings.Virtual_String;
Diagnostic : LSP.Diagnostic_Sources.Diagnostic_Source_Access) is
begin
LSP.Text_Documents.Constructors.Initialize (Self, URI, Text);
Self.Refresh_Symbol_Cache := True;
Self.Diagnostic_Sources (1) := new
LSP.Ada_Documents.LAL_Diagnostics.Diagnostic_Source
(Self'Unchecked_Access);
Self.Diagnostic_Sources (2) := Diagnostic;
end Initialize;
----------------------
-- Range_Formatting --
----------------------
function Range_Formatting
(Self : Document;
Context : LSP.Ada_Contexts.Context;
Span : LSP.Structures.A_Range;
PP_Options : Pp.Command_Lines.Cmd_Line;
Edit : out LSP.Structures.TextEdit_Vector;
Messages : out VSS.String_Vectors.Virtual_String_Vector) return Boolean
is
use Libadalang.Analysis;
use Langkit_Support.Slocs;
use Laltools.Partial_GNATPP;
use LSP.Structures;
use Utils.Char_Vectors;
use Utils.Char_Vectors.Char_Vectors;
procedure Append_PP_Messages
(PP_Messages : Pp.Scanner.Source_Message_Vector);
-- Append any message of PP_Messages to Messages properly formatting
-- them using the GNAT standard way for messages
-- (i.e: <filename>:<sloc>: <msg>)
------------------------
-- Append_PP_Messages --
------------------------