Skip to content

Commit da1ad3a

Browse files
Merge branch 'topic/als.1718.range_format_affecting_file' into 'master'
Fix RangeFormat affecting the whole file Closes #1718 See merge request eng/ide/ada_language_server!2131
2 parents c556a20 + 0382941 commit da1ad3a

File tree

10 files changed

+73
-76
lines changed

10 files changed

+73
-76
lines changed

source/ada/lsp-ada_documents.adb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,10 @@ package body LSP.Ada_Documents is
476476
Options));
477477

478478
begin
479-
Self.Diff_C (New_Text => Formatted_Document, Edit => Result);
479+
Self.Diff_C
480+
(New_Text => Formatted_Document,
481+
Span => LSP.Text_Documents.Empty_Range,
482+
Edit => Result);
480483
-- Self.Needleman_Diff (New_Text => Formatted_Document, Edit => Result);
481484

482485
return Result;
@@ -1055,28 +1058,34 @@ package body LSP.Ada_Documents is
10551058
Context : LSP.Ada_Contexts.Context;
10561059
Span : LSP.Structures.A_Range;
10571060
Options : Gnatformat.Configuration.Format_Options_Type)
1058-
return LSP.Structures.TextEdit
1061+
return LSP.Structures.TextEdit_Vector
10591062
is
10601063
use type LSP.Structures.A_Range;
10611064
use Gnatformat.Configuration;
10621065

1066+
Result : LSP.Structures.TextEdit_Vector;
10631067
begin
10641068
if Span = LSP.Text_Documents.Empty_Range then
1065-
return (LSP.Constants.Empty, VSS.Strings.Empty_Virtual_String);
1069+
return Result;
10661070
end if;
10671071

10681072
declare
1073+
10691074
Range_Formatted_Document :
10701075
constant Gnatformat.Edits.Formatting_Edit_Type :=
10711076
Gnatformat.Formatting.Range_Format
10721077
(Self.Unit (Context),
10731078
Self.To_Source_Location_Range (Span),
10741079
Options);
10751080
begin
1076-
return
1077-
(Self.To_A_Range (Range_Formatted_Document.Text_Edit.Location),
1078-
VSS.Strings.Conversions.To_Virtual_String
1079-
(Range_Formatted_Document.Text_Edit.Text));
1081+
Self.Diff_C
1082+
(New_Text =>
1083+
VSS.Strings.Conversions.To_Virtual_String
1084+
(Range_Formatted_Document.Text_Edit.Text),
1085+
Span =>
1086+
Self.To_A_Range (Range_Formatted_Document.Text_Edit.Location),
1087+
Edit => Result);
1088+
return Result;
10801089
end;
10811090
end Range_Format;
10821091

source/ada/lsp-ada_documents.ads

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ package LSP.Ada_Documents is
178178
Context : LSP.Ada_Contexts.Context;
179179
Span : LSP.Structures.A_Range;
180180
Options : Gnatformat.Configuration.Format_Options_Type)
181-
return LSP.Structures.TextEdit;
181+
return LSP.Structures.TextEdit_Vector;
182182
-- Format part of Self defined by Span with formatting options based on
183183
-- Context.
184184

source/ada/lsp-ada_handlers-formatting.adb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ package body LSP.Ada_Handlers.Formatting is
9898
Messages : out VSS.String_Vectors.Virtual_String_Vector;
9999
Error : out LSP.Errors.ResponseError)
100100
is
101-
pragma Unreferenced (Messages, Span);
101+
pragma Unreferenced (Messages);
102+
use type LSP.Structures.A_Range;
102103
begin
103104
if Document.Has_Diagnostics (Context) then
104105
Success := False;
@@ -110,7 +111,11 @@ package body LSP.Ada_Handlers.Formatting is
110111
return;
111112
end if;
112113

113-
Response := Document.Format (Context, Options);
114+
if Span /= LSP.Constants.Empty then
115+
Response := Document.Range_Format (Context, Span, Options);
116+
else
117+
Response := Document.Format (Context, Options);
118+
end if;
114119
Success := True;
115120

116121
exception
@@ -252,8 +257,7 @@ package body LSP.Ada_Handlers.Formatting is
252257
Filename : constant GNATCOLL.VFS.Virtual_File :=
253258
Context.URI_To_File (Document.URI);
254259
Indentation : constant Character_Count :=
255-
Character_Count
256-
(Get_Indentation (Options, Filename.Display_Full_Name));
260+
Character_Count (Get_Indentation (Options, Filename.Display_Full_Name));
257261
Use_Tabs : constant Boolean :=
258262
Get_Indentation_Kind (Options, Filename.Display_Full_Name) = Tabs;
259263
Res : VSS.Strings.Virtual_String;

source/server/lsp-text_documents.adb

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,62 @@ package body LSP.Text_Documents is
170170
procedure Diff_C
171171
(Self : Text_Document'Class;
172172
New_Text : VSS.Strings.Virtual_String;
173+
Span : LSP.Structures.A_Range;
173174
Edit : out LSP.Structures.TextEdit_Vector)
174175
is
176+
use type LSP.Structures.A_Range;
177+
175178
C_Edit : constant XDiff.Edits := XDiff.XDiff
176-
(VSS.Strings.Conversions.To_UTF_8_String (Self.Text),
179+
(VSS.Strings.Conversions.To_UTF_8_String
180+
(if Span /= LSP.Text_Documents.Empty_Range
181+
then Self.Slice (Span)
182+
else Self.Text),
177183
VSS.Strings.Conversions.To_UTF_8_String (New_Text),
178184
XDiff.XDF_NEED_MINIMAL);
179-
Cur : XDiff.Edits := C_Edit;
185+
180186
New_Lines : constant VSS.String_Vectors.Virtual_String_Vector :=
181187
New_Text.Split_Lines
182188
(Terminators => LSP_New_Line_Function_Set,
183189
Keep_Terminator => False);
184190

191+
Cur : XDiff.Edits := C_Edit;
192+
193+
function Get_Range
194+
(Cur_Edit : XDiff.Edits) return LSP.Structures.A_Range;
195+
185196
function Get_Slice
186197
(Lines : VSS.String_Vectors.Virtual_String_Vector;
187198
Start_Line : Integer;
188199
End_Line : Integer)
189200
return VSS.Strings.Virtual_String;
190201

202+
---------------
203+
-- Get_Range --
204+
---------------
205+
206+
function Get_Range (Cur_Edit : XDiff.Edits) return LSP.Structures.A_Range
207+
is
208+
Start_Line : Natural;
209+
End_Line : Natural;
210+
-- Start_Bloc indicates the offset between the current line and the
211+
-- first from the buffer
212+
Start_Bloc : constant Natural :=
213+
(if Span /= LSP.Text_Documents.Empty_Range
214+
then Span.start.line
215+
else 0);
216+
begin
217+
if XDiff.Delete_Line_Start (Cur_Edit) = -1 then
218+
Start_Line := XDiff.Delete_Line_End (Cur_Edit);
219+
else
220+
Start_Line := XDiff.Delete_Line_Start (Cur_Edit) - 1;
221+
end if;
222+
223+
End_Line := XDiff.Delete_Line_End (Cur_Edit);
224+
225+
return (start => (Start_Line + Start_Bloc, 0),
226+
an_end => (End_Line + Start_Bloc, 0));
227+
end Get_Range;
228+
191229
---------------
192230
-- Get_Slice --
193231
---------------
@@ -215,10 +253,7 @@ package body LSP.Text_Documents is
215253
if XDiff.Delete_Line_End (Cur) /= -1 then
216254
Edit.Append
217255
(LSP.Structures.TextEdit'
218-
(a_range => (((if XDiff.Delete_Line_Start (Cur) = -1
219-
then XDiff.Delete_Line_End (Cur)
220-
else XDiff.Delete_Line_Start (Cur) - 1), 0),
221-
(XDiff.Delete_Line_End (Cur), 0)),
256+
(a_range => Get_Range (Cur),
222257
newText => Get_Slice (New_Lines,
223258
XDiff.Insert_Line_Start (Cur),
224259
XDiff.Insert_Line_End (Cur))));

source/server/lsp-text_documents.ads

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ package LSP.Text_Documents is
7474
procedure Diff_C
7575
(Self : Text_Document'Class;
7676
New_Text : VSS.Strings.Virtual_String;
77+
Span : LSP.Structures.A_Range;
7778
Edit : out LSP.Structures.TextEdit_Vector);
7879
-- Generate TextEdit using the XDiff library
7980

testsuite/ada_lsp/gnatformat/range_format/test.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,6 @@
132132
}
133133
},
134134
"newText": " procedure You_Say_Hello is\n begin\n Ada.Text_Io.Put_Line (\"Hello\");\n"
135-
},
136-
{
137-
"range": {
138-
"start": {
139-
"line": 11,
140-
"character": 0
141-
},
142-
"end": {
143-
"line": 12,
144-
"character": 0
145-
}
146-
},
147-
"newText": "end Main;\n"
148135
}
149136
]
150137
}

testsuite/ada_lsp/onTypeFormatting/indentOnlyFalse/test.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@
163163
"character": 0
164164
},
165165
"end": {
166-
"line": 3,
167-
"character": 41
166+
"line": 4,
167+
"character": 0
168168
}
169169
},
170-
"newText": " Ada.Text_IO.Put_Line (\"Hello, World!\");"
170+
"newText": " Ada.Text_IO.Put_Line (\"Hello, World!\");\n"
171171
},
172172
{
173173
"range": {
@@ -202,4 +202,4 @@
202202
"exit_code": 0
203203
}
204204
}
205-
]
205+
]

testsuite/ada_lsp/range_formatting/UB30-006_range_format_whole_document/test.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,6 @@
168168
}
169169
},
170170
"newText": " end;\n"
171-
},
172-
{
173-
"range": {
174-
"start": {
175-
"line": 9,
176-
"character": 0
177-
},
178-
"end": {
179-
"line": 10,
180-
"character": 0
181-
}
182-
},
183-
"newText": "end Main;\n"
184171
}
185172
]
186173
}

testsuite/ada_lsp/range_formatting/W103-015_GitHub-1008/test.json

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,6 @@
119119
{
120120
"id": 2,
121121
"result": [
122-
{
123-
"range": {
124-
"start": {
125-
"line": 1,
126-
"character": 0
127-
},
128-
"end": {
129-
"line": 1,
130-
"character": 0
131-
}
132-
},
133-
"newText": "\n"
134-
},
135122
{
136123
"range": {
137124
"start": {
@@ -144,19 +131,6 @@
144131
}
145132
},
146133
"newText": " procedure You_Say_Hello is\n"
147-
},
148-
{
149-
"range": {
150-
"start": {
151-
"line": 8,
152-
"character": 0
153-
},
154-
"end": {
155-
"line": 11,
156-
"character": 0
157-
}
158-
},
159-
"newText": "\n procedure I_Say_Goodbye is\n"
160134
}
161135
]
162136
}

testsuite/ada_lsp/range_formatting/W103-015_GitHub-1073-1074/test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@
135135
"character": 0
136136
},
137137
"end": {
138-
"line": 6,
138+
"line": 7,
139139
"character": 0
140140
}
141141
},
142-
"newText": "procedure Debug is\nbegin\n null;\n"
142+
"newText": "procedure Debug is\nbegin\n null;\nend Debug;\n"
143143
}
144144
]
145145
}

0 commit comments

Comments
 (0)