Skip to content

Commit e6068d2

Browse files
Merge branch 'topic/xdiff_merge_conflict' into 'edge'
Use Ada-Xdiff library See merge request eng/ide/ada_language_server!2094
2 parents ca28997 + 138380a commit e6068d2

File tree

6 files changed

+90
-13
lines changed

6 files changed

+90
-13
lines changed

gnat/lsp_server.gpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ with "libgnatdoc.gpr";
1313
with "spawn.gpr";
1414
with "gnatformat.gpr";
1515
with "vss_os.gpr";
16+
with "ada_xdiff.gpr";
1617

1718
with "lsp_3_17";
1819
with "lsp_common";

source/ada/lsp-ada_documents.adb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ package body LSP.Ada_Documents is
563563
if Span = LSP.Constants.Empty then
564564
-- diff for the whole document
565565

566-
Self.Diff
566+
Self.Needleman_Diff
567567
(VSS.Strings.Conversions.To_Virtual_String (S.all),
568568
Edit => Edit);
569569

@@ -580,7 +580,7 @@ package body LSP.Ada_Documents is
580580
-- Use line diff if the range is too wide
581581

582582
if Span.an_end.line - Span.start.line > 5 then
583-
Self.Diff
583+
Self.Needleman_Diff
584584
(VSS.Strings.Conversions.To_Virtual_String (S.all),
585585
Span,
586586
Out_Span,
@@ -629,7 +629,8 @@ package body LSP.Ada_Documents is
629629
Context.Get_Format_Options));
630630

631631
begin
632-
Self.Diff (New_Text => Formatted_Document, Edit => Result);
632+
Self.Diff_C (New_Text => Formatted_Document, Edit => Result);
633+
-- Self.Needleman_Diff (New_Text => Formatted_Document, Edit => Result);
633634

634635
return Result;
635636
end Format;

source/server/lsp-text_documents.adb

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ with Ada.Unchecked_Deallocation;
1919

2020
with VSS.Characters.Latin;
2121
with VSS.Strings.Character_Iterators;
22+
with VSS.Strings.Conversions;
2223
with VSS.Strings.Line_Iterators;
2324
with VSS.String_Vectors;
2425
with VSS.Unicode;
26+
with Ada_XDiff;
2527

2628
package body LSP.Text_Documents is
2729

@@ -161,11 +163,78 @@ package body LSP.Text_Documents is
161163

162164
end Constructors;
163165

164-
----------
165-
-- Diff --
166-
----------
166+
------------
167+
-- Diff_C --
168+
------------
167169

168-
procedure Diff
170+
procedure Diff_C
171+
(Self : Text_Document'Class;
172+
New_Text : VSS.Strings.Virtual_String;
173+
Edit : out LSP.Structures.TextEdit_Vector)
174+
is
175+
C_Edit : constant Ada_XDiff.Edits := Ada_XDiff.XDiff
176+
(VSS.Strings.Conversions.To_UTF_8_String (Self.Text),
177+
VSS.Strings.Conversions.To_UTF_8_String (New_Text),
178+
Ada_XDiff.XDF_NEED_MINIMAL);
179+
Cur : Ada_XDiff.Edits := C_Edit;
180+
New_Lines : constant VSS.String_Vectors.Virtual_String_Vector :=
181+
New_Text.Split_Lines
182+
(Terminators => LSP_New_Line_Function_Set,
183+
Keep_Terminator => False);
184+
185+
function Get_Slice
186+
(Lines : VSS.String_Vectors.Virtual_String_Vector;
187+
Start_Line : Integer;
188+
End_Line : Integer)
189+
return VSS.Strings.Virtual_String;
190+
191+
---------------
192+
-- Get_Slice --
193+
---------------
194+
195+
function Get_Slice
196+
(Lines : VSS.String_Vectors.Virtual_String_Vector;
197+
Start_Line : Integer;
198+
End_Line : Integer)
199+
return VSS.Strings.Virtual_String
200+
is
201+
use VSS.Strings;
202+
Res : VSS.Strings.Virtual_String := VSS.Strings.Empty_Virtual_String;
203+
begin
204+
for I in Start_Line .. End_Line loop
205+
Res.Append (Lines (I) & VSS.Characters.Latin.Line_Feed);
206+
end loop;
207+
return Res;
208+
end Get_Slice;
209+
begin
210+
211+
loop
212+
-- Discard the first node which is fake and will have -1 for all its
213+
-- value. Only Delete_Line_Start is allowed to have -1 to indicate
214+
-- that nothing should be deleted.
215+
if Ada_XDiff.Delete_Line_End (Cur) /= -1 then
216+
Edit.Append
217+
(LSP.Structures.TextEdit'
218+
(a_range => (((if Ada_XDiff.Delete_Line_Start (Cur) = -1
219+
then Ada_XDiff.Delete_Line_End (Cur)
220+
else Ada_XDiff.Delete_Line_Start (Cur) - 1), 0),
221+
(Ada_XDiff.Delete_Line_End (Cur), 0)),
222+
newText => Get_Slice (New_Lines,
223+
Ada_XDiff.Insert_Line_Start (Cur),
224+
Ada_XDiff.Insert_Line_End (Cur))));
225+
end if;
226+
exit when not Ada_XDiff.Has_Next (Cur);
227+
Cur := Ada_XDiff.Next_Edit (Cur);
228+
end loop;
229+
230+
Ada_XDiff.Free_Edits (C_Edit);
231+
end Diff_C;
232+
233+
--------------------
234+
-- Needleman_Diff --
235+
--------------------
236+
237+
procedure Needleman_Diff
169238
(Self : Text_Document'Class;
170239
New_Text : VSS.Strings.Virtual_String;
171240
Old_Span : LSP.Structures.A_Range := Empty_Range;
@@ -475,7 +544,7 @@ package body LSP.Text_Documents is
475544

476545
raise;
477546
end;
478-
end Diff;
547+
end Needleman_Diff;
479548

480549
------------------
481550
-- Diff_Symbols --

source/server/lsp-text_documents.ads

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ package LSP.Text_Documents is
7171
Vector : LSP.Structures.TextDocumentContentChangeEvent_Vector);
7272
-- Modify document according to event vector provided by LSP client.
7373

74-
procedure Diff
74+
procedure Diff_C
75+
(Self : Text_Document'Class;
76+
New_Text : VSS.Strings.Virtual_String;
77+
Edit : out LSP.Structures.TextEdit_Vector);
78+
-- Generate TextEdit using the XDiff library
79+
80+
procedure Needleman_Diff
7581
(Self : Text_Document'Class;
7682
New_Text : VSS.Strings.Virtual_String;
7783
Old_Span : LSP.Structures.A_Range := Empty_Range;

testsuite/ada_lsp/T928-015.formatting.end_of_buffer/test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@
279279
"character": 0
280280
},
281281
"end": {
282-
"line": 2,
283-
"character": 42
282+
"line": 3,
283+
"character": 0
284284
}
285285
},
286286
"newText": " procedure Put (E : UI_Element);\nend UI;\n"

testsuite/ada_lsp/formatting.non_source_file_after_edit/test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@
123123
"character": 0
124124
},
125125
"end": {
126-
"line": 3,
127-
"character": 9
126+
"line": 4,
127+
"character": 0
128128
}
129129
},
130130
"newText": " null;\nend Main;\n"

0 commit comments

Comments
 (0)