@@ -31,6 +31,11 @@ package body LSP.Ada_Handlers.Locations is
3131 Sloc : Langkit_Support.Slocs.Source_Location_Range)
3232 return LSP.Structures.A_Range;
3333
34+ function From_LSP_Range
35+ (Unit : Libadalang.Analysis.Analysis_Unit;
36+ Sloc : LSP.Structures.A_Range)
37+ return Langkit_Support.Slocs.Source_Location_Range;
38+
3439 -- -------------------
3540 -- Append_Location --
3641 -- -------------------
@@ -80,6 +85,99 @@ package body LSP.Ada_Handlers.Locations is
8085 end if ;
8186 end Append_Location ;
8287
88+ -- ------------------
89+ -- From_LSP_Range --
90+ -- ------------------
91+
92+ function From_LSP_Range
93+ (Self : in out Message_Handler'Class;
94+ Unit : Libadalang.Analysis.Analysis_Unit;
95+ Sloc : LSP.Structures.A_Range)
96+ return Langkit_Support.Slocs.Source_Location_Range
97+ is
98+ use type LSP.Ada_Documents.Document_Access;
99+
100+ URI : constant LSP.Structures.DocumentUri :=
101+ (VSS.Strings.Conversions.To_Virtual_String
102+ (URIs.Conversions.From_File (Unit.Get_Filename))
103+ with null record );
104+
105+ Doc : constant LSP.Ada_Documents.Document_Access :=
106+ Self.Get_Open_Document (URI);
107+
108+ begin
109+ if Doc /= null then
110+ return Doc.To_Source_Location_Range (Sloc);
111+
112+ else
113+ return From_LSP_Range (Unit, Sloc);
114+ end if ;
115+ end From_LSP_Range ;
116+
117+ -- ------------------
118+ -- From_LSP_Range --
119+ -- ------------------
120+
121+ function From_LSP_Range
122+ (Unit : Libadalang.Analysis.Analysis_Unit;
123+ Sloc : LSP.Structures.A_Range)
124+ return Langkit_Support.Slocs.Source_Location_Range
125+ is
126+ use type Langkit_Support.Slocs.Column_Number;
127+
128+ Result : Langkit_Support.Slocs.Source_Location_Range :=
129+ (Start_Line => Positive'Pos (Sloc.start.line + 1 ),
130+ End_Line => Positive'Pos (Sloc.an_end.line + 1 ),
131+ others => 0 );
132+ begin
133+ declare
134+ Line : constant VSS.Strings.Virtual_String :=
135+ VSS.Strings.To_Virtual_String
136+ (Unit.Get_Line (Sloc.start.line + 1 ));
137+
138+ Cursor : VSS.Strings.Character_Iterators.Character_Iterator :=
139+ Line.Before_First_Character;
140+
141+ begin
142+ while Cursor.Forward and then
143+ Natural (Cursor.First_UTF16_Offset) <= Sloc.start.character
144+ loop
145+ Result.Start_Column := @ + 1 ;
146+ end loop ;
147+
148+ if Sloc.start.line = Sloc.an_end.line then
149+
150+ Result.End_Column := Result.Start_Column;
151+
152+ while Cursor.Forward and then
153+ Natural (Cursor.First_UTF16_Offset) <= Sloc.an_end.character
154+ loop
155+ Result.End_Column := @ + 1 ;
156+ end loop ;
157+
158+ return Result;
159+ end if ;
160+ end ;
161+
162+ declare
163+ Line : constant VSS.Strings.Virtual_String :=
164+ VSS.Strings.To_Virtual_String
165+ (Unit.Get_Line (Sloc.an_end.line + 1 ));
166+
167+ Cursor : VSS.Strings.Character_Iterators.Character_Iterator :=
168+ Line.Before_First_Character;
169+
170+ begin
171+ while Cursor.Forward and then
172+ Natural (Cursor.First_UTF16_Offset) <= Sloc.an_end.character
173+ loop
174+ Result.End_Column := @ + 1 ;
175+ end loop ;
176+
177+ return Result;
178+ end ;
179+ end From_LSP_Range ;
180+
83181 -- ---------------
84182 -- Get_Node_At --
85183 -- ---------------
0 commit comments