Skip to content

Commit 3b06b62

Browse files
UA06-032: Add completion for attributes
All the tests have been adapted.
1 parent 2a66172 commit 3b06b62

File tree

105 files changed

+248
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+248
-8
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2018-2021, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
with Ada.Strings.UTF_Encoding;
19+
20+
with Langkit_Support.Text;
21+
22+
with LSP.Ada_Completions.Filters;
23+
with LSP.Predefined_Completion;
24+
25+
package body LSP.Ada_Completions.Attributes is
26+
27+
------------------------
28+
-- Propose_Completion --
29+
------------------------
30+
31+
overriding procedure Propose_Completion
32+
(Self : Attributes_Completion_Provider;
33+
Sloc : Langkit_Support.Slocs.Source_Location;
34+
Token : Libadalang.Common.Token_Reference;
35+
Node : Libadalang.Analysis.Ada_Node;
36+
Filter : in out LSP.Ada_Completions.Filters.Filter;
37+
Names : in out Ada_Completions.Completion_Maps.Map;
38+
Result : in out LSP.Messages.CompletionList)
39+
is
40+
pragma Unreferenced (Names);
41+
begin
42+
if Filter.Is_Attribute_Ref then
43+
declare
44+
Prefix : constant Ada.Strings.UTF_Encoding.UTF_8_String :=
45+
Langkit_Support.Text.To_UTF8 (Node.Text);
46+
begin
47+
LSP.Predefined_Completion.Get_Attributes
48+
(Prefix => (if Prefix /= "'" then Prefix else ""),
49+
Result => Result.items);
50+
end;
51+
end if;
52+
end Propose_Completion;
53+
54+
end LSP.Ada_Completions.Attributes;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2018-2021, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
-- A completion provider for attributes
18+
19+
package LSP.Ada_Completions.Attributes is
20+
21+
type Attributes_Completion_Provider is
22+
new Completion_Provider with null record;
23+
24+
overriding procedure Propose_Completion
25+
(Self : Attributes_Completion_Provider;
26+
Sloc : Langkit_Support.Slocs.Source_Location;
27+
Token : Libadalang.Common.Token_Reference;
28+
Node : Libadalang.Analysis.Ada_Node;
29+
Filter : in out LSP.Ada_Completions.Filters.Filter;
30+
Names : in out Ada_Completions.Completion_Maps.Map;
31+
Result : in out LSP.Messages.CompletionList);
32+
-- Get completion for attributes.
33+
34+
end LSP.Ada_Completions.Attributes;

source/ada/lsp-ada_completions-filters.adb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,49 @@ package body LSP.Ada_Completions.Filters is
8282
return Self.Is_End_Label.Value;
8383
end Is_End_Label;
8484

85+
----------------------
86+
-- Is_Attribute_Ref --
87+
----------------------
88+
89+
function Is_Attribute_Ref (Self : in out Filter'Class) return Boolean is
90+
begin
91+
if not Self.Is_Attribute.Is_Set then
92+
declare
93+
use all type Libadalang.Common.Ada_Node_Kind_Type;
94+
use all type Libadalang.Common.Token_Kind;
95+
96+
Token_Kind : constant Libadalang.Common.Token_Kind :=
97+
Kind (Self.Token);
98+
Parent : Libadalang.Analysis.Ada_Node :=
99+
(if Self.Node.Is_Null then Self.Node else Self.Node.Parent);
100+
begin
101+
-- Get the outermost dotted name of which node is a prefix, so
102+
-- that when completing in a situation such as the following:
103+
--
104+
-- end Ada.Tex|
105+
-- ^ Cursor here
106+
--
107+
-- we get the DottedName node rather than just the "Tex" BaseId.
108+
-- We want the DottedName rather than the Id so as to get the
109+
-- proper completions (all elements in the "Ada" namespace).
110+
111+
while not Parent.Is_Null
112+
and then Parent.Kind = Ada_Dotted_Name
113+
loop
114+
Parent := Parent.Parent;
115+
end loop;
116+
117+
Self.Is_Attribute :=
118+
(True,
119+
Token_Kind = Ada_Tick or else
120+
(not Parent.Is_Null
121+
and then Parent.Kind = Ada_Attribute_Ref));
122+
end;
123+
end if;
124+
125+
return Self.Is_Attribute.Value;
126+
end Is_Attribute_Ref;
127+
85128
------------------------
86129
-- Is_Numeric_Literal --
87130
------------------------

source/ada/lsp-ada_completions-filters.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@ package LSP.Ada_Completions.Filters is
3636
function Is_Numeric_Literal (Self : in out Filter'Class) return Boolean;
3737
-- Check if we complete a numeric literal (even incomplete one, like 1E).
3838

39+
function Is_Attribute_Ref (Self : in out Filter'Class) return Boolean;
40+
-- Check if we complete "'<attribute>" text
41+
3942
private
4043

4144
type Filter is tagged limited record
4245
Token : Libadalang.Common.Token_Reference;
4346
Node : Libadalang.Analysis.Ada_Node;
4447
Is_End_Label : LSP.Types.Optional_Boolean;
4548
Is_Numeric_Literal : LSP.Types.Optional_Boolean;
49+
Is_Attribute : LSP.Types.Optional_Boolean;
4650
end record;
4751

4852
end LSP.Ada_Completions.Filters;

source/ada/lsp-ada_completions-names.adb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ package body LSP.Ada_Completions.Names is
9696
return;
9797
end if;
9898

99-
-- Don't complete numeric literals
100-
if Filter.Is_Numeric_Literal then
99+
-- Don't complete numeric literals or attributes
100+
if Filter.Is_Numeric_Literal or else Filter.Is_Attribute_Ref then
101101
return;
102102
end if;
103103

source/ada/lsp-ada_handlers.adb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ with LSP.Ada_Contexts; use LSP.Ada_Contexts;
3737
with LSP.Ada_Completions;
3838
with LSP.Ada_Completions.Aggregates;
3939
with LSP.Ada_Completions.Aspects;
40+
with LSP.Ada_Completions.Attributes;
4041
with LSP.Ada_Completions.Keywords;
4142
with LSP.Ada_Completions.Names;
4243
with LSP.Ada_Completions.Parameters;
@@ -722,7 +723,8 @@ package body LSP.Ada_Handlers is
722723
(True,
723724
(resolveProvider => LSP.Types.True,
724725
triggerCharacters => (True,
725-
Empty_Vector & (+".") & (+",") & (+"(")),
726+
Empty_Vector & (+".")
727+
& (+",") & (+"'") & (+"(")),
726728
allCommitCharacters => (Is_Set => False),
727729
workDoneProgress => LSP.Types.None));
728730
Response.result.capabilities.hoverProvider :=
@@ -4066,11 +4068,14 @@ package body LSP.Ada_Handlers is
40664068
P2 : aliased LSP.Ada_Completions.Aspects.Aspect_Completion_Provider;
40674069
P3 : aliased LSP.Ada_Completions.Pragmas.Pragma_Completion_Provider;
40684070
P4 : aliased LSP.Ada_Completions.Keywords.Keyword_Completion_Provider;
4069-
P5 : aliased LSP.Ada_Completions.Names.Name_Completion_Provider
4071+
P5 : aliased
4072+
LSP.Ada_Completions.Attributes.Attributes_Completion_Provider;
4073+
4074+
P6 : aliased LSP.Ada_Completions.Names.Name_Completion_Provider
40704075
(Self.Completion_Snippets_Enabled);
4071-
P6 : aliased LSP.Ada_Handlers.Invisibles.Invisible_Completion_Provider
4076+
P7 : aliased LSP.Ada_Handlers.Invisibles.Invisible_Completion_Provider
40724077
(Self, Context);
4073-
P7 : aliased
4078+
P8 : aliased
40744079
LSP.Ada_Completions.Parameters.Parameter_Completion_Provider
40754080
(Context => Context,
40764081
Compute_Doc_And_Details => Compute_Doc_And_Details);
@@ -4082,7 +4087,8 @@ package body LSP.Ada_Handlers is
40824087
P4'Unchecked_Access,
40834088
P5'Unchecked_Access,
40844089
P6'Unchecked_Access,
4085-
P7'Unchecked_Access);
4090+
P7'Unchecked_Access,
4091+
P8'Unchecked_Access);
40864092

40874093
Document : constant LSP.Ada_Documents.Document_Access :=
40884094
Get_Open_Document (Self, Value.textDocument.uri);

testsuite/ada_lsp/C825-005.xrefs.extending/test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"triggerCharacters": [
4343
".",
4444
",",
45+
"'",
4546
"("
4647
],
4748
"resolveProvider": true

testsuite/ada_lsp/D301-004.xrefs.generics/test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"triggerCharacters": [
4343
".",
4444
",",
45+
"'",
4546
"("
4647
],
4748
"resolveProvider": true

testsuite/ada_lsp/D803-003.xrefs.generics_go_to_body/test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"triggerCharacters": [
4343
".",
4444
",",
45+
"'",
4546
"("
4647
],
4748
"resolveProvider": true

testsuite/ada_lsp/F131-024.xrefs.separate_body/test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"triggerCharacters": [
5050
".",
5151
",",
52+
"'",
5253
"("
5354
],
5455
"resolveProvider": true

0 commit comments

Comments
 (0)