Skip to content

Commit 22f114d

Browse files
Merge branch 'topic/fallback_indenter' into 'master'
Fallback indenter extracted from GS Ada_Analyzer Closes gnatstudio#568 See merge request eng/ide/ada_language_server!2091
2 parents bb48477 + 7c7c5f3 commit 22f114d

File tree

6 files changed

+4695
-717
lines changed

6 files changed

+4695
-717
lines changed

source/ada/lsp-ada_documents.adb

Lines changed: 14 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -985,215 +985,6 @@ package body LSP.Ada_Documents is
985985
Input_Range =>
986986
Self.To_Source_Location_Range ((Position, Position))));
987987

988-
--------------------------
989-
-- Estimate_Indentation --
990-
--------------------------
991-
992-
function Estimate_Indentation
993-
(Self : Document; Context : LSP.Ada_Contexts.Context; Line : Positive)
994-
return VSS.Strings.Character_Count
995-
is
996-
use Langkit_Support.Slocs;
997-
use Libadalang.Analysis;
998-
use Libadalang.Common;
999-
1000-
function Get_Relevant_Parents
1001-
(Unit : Libadalang.Analysis.Analysis_Unit;
1002-
Token : Token_Reference) return Ada_Node_Array;
1003-
-- Analyze where in the tree this Token is and returns the appropriate
1004-
-- parent nodes that influence indentation.
1005-
1006-
function Parent_Based_Indentation
1007-
(Parents : Ada_Node_Array;
1008-
Indentation : Positive := 3;
1009-
Indentation_Continuation : Positive := 2) return Natural;
1010-
-- Estimate the indentation starting at zero and incrementing based on
1011-
-- that Parents kind. Returns earlier if it finds a parent that always
1012-
-- sets indentation, for instance, a parameter list.
1013-
1014-
------------------------------
1015-
-- Parent_Based_Indentation --
1016-
------------------------------
1017-
1018-
function Parent_Based_Indentation
1019-
(Parents : Ada_Node_Array;
1020-
Indentation : Positive := 3;
1021-
Indentation_Continuation : Positive := 2) return Natural
1022-
is
1023-
Current_Indentation : Natural := 0;
1024-
1025-
begin
1026-
for Parent of Parents loop
1027-
case Parent.Kind is
1028-
when Ada_Loop_Stmt_Range
1029-
| Ada_For_Loop_Stmt_Range
1030-
| Ada_While_Loop_Stmt_Range
1031-
| Ada_If_Stmt_Range
1032-
| Ada_Case_Stmt_Range
1033-
| Ada_Case_Stmt_Alternative_Range
1034-
| Ada_Record_Type_Def_Range
1035-
| Ada_Generic_Formal_Part_Range
1036-
| Ada_Begin_Block_Range
1037-
| Ada_Decl_Block_Range
1038-
=>
1039-
Current_Indentation := Current_Indentation + Indentation;
1040-
1041-
when Ada_Declarative_Part_Range =>
1042-
-- When we type declare, a DeclBlock is created but not a
1043-
-- DeclarativePart one. Only when you close the block with
1044-
-- an end the node is created.
1045-
-- DeclarativePart is a node that adds indentation.
1046-
-- We cannot simply make DeclBlock also add indentation
1047-
-- because it would double indent. So only add indentation
1048-
-- to DeclarativeParts if their parent is not DeclBlock.
1049-
if Parent.Parent.Kind not in Ada_Decl_Block_Range then
1050-
Current_Indentation := Current_Indentation + Indentation;
1051-
end if;
1052-
1053-
when Ada_Handled_Stmts_Range =>
1054-
-- HandledStmts can be children of DeclBlock and BeginBlock.
1055-
-- These two add indentation, so HandledStmts should not
1056-
-- double add if its their child.
1057-
if Parent.Parent.Kind not in
1058-
Ada_Begin_Block_Range
1059-
| Ada_Decl_Block_Range
1060-
then
1061-
Current_Indentation := Current_Indentation + Indentation;
1062-
end if;
1063-
1064-
when Ada_Subp_Spec_Range | Ada_Assign_Stmt_Range =>
1065-
Current_Indentation :=
1066-
Current_Indentation + Indentation_Continuation;
1067-
1068-
when Ada_Dotted_Name_Range =>
1069-
Current_Indentation :=
1070-
Natural (Parent.Sloc_Range.Start_Column) - 1
1071-
+ Indentation_Continuation;
1072-
exit;
1073-
1074-
when Ada_Params_Range =>
1075-
Current_Indentation :=
1076-
Natural (Parent.Sloc_Range.Start_Column) - 1 + 1;
1077-
exit;
1078-
1079-
when Ada_Assoc_List_Range | Ada_Component_List_Range =>
1080-
Current_Indentation :=
1081-
Natural (Parent.Sloc_Range.Start_Column) - 1;
1082-
exit;
1083-
1084-
when others =>
1085-
null;
1086-
end case;
1087-
end loop;
1088-
1089-
return Current_Indentation;
1090-
end Parent_Based_Indentation;
1091-
1092-
--------------------------
1093-
-- Get_Relevant_Parents --
1094-
--------------------------
1095-
1096-
function Get_Relevant_Parents
1097-
(Unit : Libadalang.Analysis.Analysis_Unit;
1098-
Token : Token_Reference) return Ada_Node_Array
1099-
is
1100-
Previous : Token_Reference :=
1101-
(if Token = No_Token then No_Token
1102-
else Libadalang.Common.Previous (Token, Exclude_Trivia => True));
1103-
1104-
begin
1105-
if Previous = No_Token then
1106-
return [];
1107-
end if;
1108-
1109-
if Kind (Data (Previous)) in Ada_Comma | Ada_Dot then
1110-
Previous :=
1111-
Libadalang.Common.Previous (Previous, Exclude_Trivia => True);
1112-
end if;
1113-
1114-
declare
1115-
Node : constant Ada_Node :=
1116-
Unit.Root.Lookup (Start_Sloc (Sloc_Range (Data (Previous))));
1117-
1118-
begin
1119-
if (Node.Kind in Ada_Begin_Block_Range
1120-
and Kind (Data (Previous)) in Ada_Begin)
1121-
or (Node.Kind in Ada_Decl_Block_Range
1122-
and Kind (Data (Previous)) in Ada_Declare)
1123-
or Kind (Data (Previous)) in Ada_Brack_Open
1124-
or Node.Kind in Ada_Params_Range
1125-
then
1126-
return Node.Parents;
1127-
1128-
elsif Node.Kind in Ada_Subp_Body_Range then
1129-
if Kind (Data (Previous)) in Ada_Is then
1130-
return Node.As_Subp_Body.F_Decls.Parents;
1131-
1132-
elsif Kind (Data (Previous)) in Ada_Begin then
1133-
return Node.As_Subp_Body.F_Stmts.Parents;
1134-
end if;
1135-
1136-
elsif Node.Kind in Ada_Package_Body_Range then
1137-
if Kind (Data (Previous)) in Ada_Is then
1138-
return Node.As_Package_Body.F_Decls.Parents;
1139-
1140-
elsif Kind (Data (Previous)) in Ada_Begin then
1141-
return Node.As_Package_Body.F_Stmts.Parents;
1142-
end if;
1143-
1144-
elsif Node.Kind in Ada_Package_Decl_Range then
1145-
if Kind (Data (Previous)) in Ada_Is then
1146-
return Node.As_Package_Decl.F_Public_Part.Parents;
1147-
1148-
elsif Kind (Data (Previous)) in Ada_Private then
1149-
return Node.As_Package_Decl.F_Private_Part.Parents;
1150-
end if;
1151-
1152-
elsif Node.Kind in Ada_Generic_Package_Internal_Range then
1153-
if Kind (Data (Previous)) in Ada_Is then
1154-
return
1155-
Node.As_Generic_Package_Internal.F_Public_Part.Parents;
1156-
1157-
elsif Kind (Data (Previous)) in Ada_Private then
1158-
return
1159-
Node.As_Generic_Package_Internal.F_Private_Part.Parents;
1160-
end if;
1161-
1162-
elsif Node.Kind in Ada_Generic_Formal_Part_Range then
1163-
if Kind (Data (Previous)) in Ada_Generic then
1164-
return Node.As_Generic_Formal_Part.F_Decls.Parents;
1165-
end if;
1166-
end if;
1167-
1168-
return Node.Parents (With_Self => False);
1169-
end;
1170-
end Get_Relevant_Parents;
1171-
1172-
Unit : constant Analysis_Unit := Self.Unit (Context);
1173-
Line_Number : constant Langkit_Support.Slocs.Line_Number :=
1174-
Self.To_Source_Location ((Line, 1)).Line;
1175-
Token : constant Token_Reference :=
1176-
Unit.Lookup_Token (Source_Location'(Line_Number, 1));
1177-
1178-
Format_Options : constant Gnatformat.Configuration.Format_Options_Type :=
1179-
Context.Get_Format_Options;
1180-
1181-
Indentation : constant Positive :=
1182-
Gnatformat.Configuration.Get_Indentation
1183-
(Format_Options, Unit.Get_Filename);
1184-
Indentation_Continuation : constant Positive :=
1185-
Gnatformat.Configuration.Get_Indentation_Continuation
1186-
(Format_Options, Unit.Get_Filename);
1187-
1188-
begin
1189-
return
1190-
VSS.Strings.Character_Count
1191-
(Parent_Based_Indentation
1192-
(Parents => Get_Relevant_Parents (Unit, Token),
1193-
Indentation => Indentation,
1194-
Indentation_Continuation => Indentation_Continuation));
1195-
end Estimate_Indentation;
1196-
1197988
-----------------
1198989
-- Get_Node_At --
1199990
-----------------
@@ -1362,6 +1153,20 @@ package body LSP.Ada_Documents is
13621153
return Result;
13631154
end Get_Word_At;
13641155

1156+
--------------
1157+
-- Get_Text --
1158+
--------------
1159+
1160+
function Get_Text
1161+
(Self : Document;
1162+
From : LSP.Structures.Position;
1163+
To : LSP.Structures.Position)
1164+
return VSS.Strings.Virtual_String
1165+
is
1166+
begin
1167+
return Self.Slice ((From, To));
1168+
end Get_Text;
1169+
13651170
---------------------
13661171
-- Has_Diagnostics --
13671172
---------------------

source/ada/lsp-ada_documents.ads

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,6 @@ package LSP.Ada_Documents is
107107
Result : out LSP.Structures.DocumentSymbol_Vector);
108108
-- Populate Result with a symbol hierarchy from the document.
109109

110-
function Estimate_Indentation
111-
(Self : Document; Context : LSP.Ada_Contexts.Context; Line : Positive)
112-
return VSS.Strings.Character_Count;
113-
-- Estimates the indention a line should have
114-
115110
function Get_Node_At
116111
(Self : Document;
117112
Context : LSP.Ada_Contexts.Context;
@@ -126,6 +121,13 @@ package LSP.Ada_Documents is
126121
return VSS.Strings.Virtual_String;
127122
-- Get an identifier at given position in the document or an empty string.
128123

124+
function Get_Text
125+
(Self : Document;
126+
From : LSP.Structures.Position;
127+
To : LSP.Structures.Position)
128+
return VSS.Strings.Virtual_String;
129+
-- Get the text in the document between From and To
130+
129131
procedure Get_Completion_Node
130132
(Self : Document;
131133
Context : LSP.Ada_Contexts.Context;

0 commit comments

Comments
 (0)