@@ -34,8 +34,8 @@ package body LSP.Ada_Handlers.Formatting is
3434 -- Error message sent when trying to format invalid code.
3535
3636 function Reindent_Line
37- (Context : LSP.Ada_Contexts.Context ;
38- Document : not null LSP.Ada_Documents.Document_Access ;
37+ (Filename : GNATCOLL.VFS.Virtual_File ;
38+ Line : VSS.Strings.Virtual_String ;
3939 Options : Gnatformat.Configuration.Format_Options_Type;
4040 Pos : LSP.Structures.Position;
4141 New_Indent : Natural) return LSP.Structures.TextEdit;
@@ -46,17 +46,15 @@ package body LSP.Ada_Handlers.Formatting is
4646 -- -----------------
4747
4848 function Reindent_Line
49- (Context : LSP.Ada_Contexts.Context ;
50- Document : not null LSP.Ada_Documents.Document_Access ;
49+ (Filename : GNATCOLL.VFS.Virtual_File ;
50+ Line : VSS.Strings.Virtual_String ;
5151 Options : Gnatformat.Configuration.Format_Options_Type;
5252 Pos : LSP.Structures.Position;
5353 New_Indent : Natural) return LSP.Structures.TextEdit
5454 is
5555 use type VSS.Characters.Virtual_Character;
5656 use VSS.Strings;
5757
58- Line : constant Virtual_String :=
59- Document.Get_Text (Pos, (Pos.line + 1 , 0 ));
6058 -- Get the full line because we want to remove its existing blank
6159 -- characters.
6260 New_Prefix : constant Virtual_String :=
@@ -81,7 +79,11 @@ package body LSP.Ada_Handlers.Formatting is
8179 return
8280 LSP.Structures.TextEdit'
8381 (a_range => (Pos, (Pos.line, First_Non_Blank)),
84- newText => Handle_Tabs (Context, Document, Options, New_Prefix));
82+ newText =>
83+ Handle_Tabs
84+ (Filename => Filename,
85+ Options => Options,
86+ S => New_Prefix));
8587 end Reindent_Line ;
8688
8789 -- ----------
@@ -170,14 +172,12 @@ package body LSP.Ada_Handlers.Formatting is
170172 -- -------------------
171173
172174 function Get_Indentation
173- (Context : LSP.Ada_Contexts.Context ;
174- Document : not null LSP.Ada_Documents.Document_Access ;
175+ (Filename : GNATCOLL.VFS.Virtual_File ;
176+ Buffer : VSS.Strings.Virtual_String ;
175177 Span : LSP.Structures.A_Range;
176178 Options : Gnatformat.Configuration.Format_Options_Type)
177179 return LSP.Formatters.Fallback_Indenter.Indentation_Array
178180 is
179- Filename : constant GNATCOLL.VFS.Virtual_File :=
180- Context.URI_To_File (Document.URI);
181181 Indentation : constant Positive :=
182182 Gnatformat.Configuration.Get_Indentation
183183 (Options, Filename.Display_Full_Name);
@@ -187,9 +187,7 @@ package body LSP.Ada_Handlers.Formatting is
187187 begin
188188 return
189189 LSP.Formatters.Fallback_Indenter.Get_Indentation
190- (Buffer =>
191- VSS.Strings.Conversions.To_UTF_8_String
192- (Document.Get_Text ((0 , 0 ), Span.an_end)),
190+ (Buffer => VSS.Strings.Conversions.To_UTF_8_String (Buffer),
193191 From => Span.start.line + 1 ,
194192 To => Span.an_end.line + 1 ,
195193 Indent_Level => Indentation,
@@ -201,38 +199,68 @@ package body LSP.Ada_Handlers.Formatting is
201199 -- ----------------
202200
203201 procedure Indent_Lines
204- (Context : LSP.Ada_Contexts.Context ;
205- Document : not null LSP.Ada_Documents.Document_Access ;
206- Span : LSP.Structures.A_Range ;
202+ (Tracer : not null LSP.Tracers.Tracer_Access ;
203+ Filename : GNATCOLL.VFS.Virtual_File ;
204+ Document : LSP.Text_Documents.Text_Document'Class ;
207205 Options : Gnatformat.Configuration.Format_Options_Type;
206+ Span : LSP.Structures.A_Range := LSP.Text_Documents.Empty_Range;
208207 Success : out Boolean;
209208 Response : out LSP.Structures.TextEdit_Vector;
210209 Messages : out VSS.String_Vectors.Virtual_String_Vector;
211210 Error : out LSP.Errors.ResponseError)
212211 is
213212 pragma Unreferenced (Messages);
213+ use LSP.Structures;
214+ use LSP.Text_Documents;
214215 use VSS.Strings;
216+
217+ Actual_Span : constant A_Range :=
218+ (if Span = Empty_Range
219+ then ((0 , 0 ), (Integer'Max (Document.Line_Count - 1 , 0 ), 0 ))
220+ else Span);
221+ -- If no span is provided, indent the whole document.
222+
223+ Buffer : constant VSS.Strings.Virtual_String :=
224+ (if Span = Empty_Range
225+ then Document.Text
226+ else Document.Slice (((0 , 0 ), Actual_Span.an_end)));
227+ -- Get the relevant buffer to indent.
228+ -- If no span is provided, get the whole document buffer.
229+ -- Otherwise get the buffer from the start of the document
230+ -- to the end of the given span.
231+
215232 Indent_Lines :
216233 constant LSP.Formatters.Fallback_Indenter.Indentation_Array :=
217- Get_Indentation (Context, Document, Span, Options => Options);
234+ Get_Indentation
235+ (Filename => Filename,
236+ Buffer => Buffer,
237+ Span => Actual_Span,
238+ Options => Options);
239+ -- Get the indentation levels for each line in the span.
240+
241+ Pos : LSP.Structures.Position;
218242 begin
219- Context.Tracer.Trace_Text
220- (Incorrect_Code_Msg & " , using the fallback indenter" );
243+ Tracer.Trace_Text (Incorrect_Code_Msg & " , using the fallback indenter" );
221244 for Line in Indent_Lines'Range loop
222245 if Indent_Lines (Line) /= -1 then
246+ -- LSP is 0-based, while the array returned by the fallback
247+ -- indenter is 1-based.
248+ Pos := (Line - 1 , 0 );
249+
250+ -- Generate a text edit to reindent the line.
223251 Response.Append
224252 (Reindent_Line
225- (Context => Context ,
226- Document => Document,
253+ (Filename => Filename ,
254+ Line => Document.Get_Line (Pos.line) ,
227255 Options => Options,
228- Pos => (Line - 1 , 0 ) ,
256+ Pos => Pos ,
229257 New_Indent => Indent_Lines (Line)));
230258 end if ;
231259 end loop ;
232260 Success := True;
233261 exception
234262 when E : others =>
235- Context. Tracer.Trace_Exception (E, Fallback_Exception_Found_Msg);
263+ Tracer.Trace_Exception (E, Fallback_Exception_Found_Msg);
236264 Success := False;
237265 Error :=
238266 (code =>
@@ -245,19 +273,17 @@ package body LSP.Ada_Handlers.Formatting is
245273 -- ---------------
246274
247275 function Handle_Tabs
248- (Context : LSP.Ada_Contexts.Context;
249- Document : not null LSP.Ada_Documents.Document_Access;
276+ (Filename : GNATCOLL.VFS.Virtual_File;
250277 Options : Gnatformat.Configuration.Format_Options_Type;
251278 S : VSS.Strings.Virtual_String) return VSS.Strings.Virtual_String
252279 is
253280 use Gnatformat.Configuration;
254281 use type VSS.Characters.Virtual_Character;
255282 use VSS.Strings;
256283
257- Filename : constant GNATCOLL.VFS.Virtual_File :=
258- Context.URI_To_File (Document.URI);
259284 Indentation : constant Character_Count :=
260- Character_Count (Get_Indentation (Options, Filename.Display_Full_Name));
285+ Character_Count
286+ (Get_Indentation (Options, Filename.Display_Full_Name));
261287 Use_Tabs : constant Boolean :=
262288 Get_Indentation_Kind (Options, Filename.Display_Full_Name) = Tabs;
263289 Res : VSS.Strings.Virtual_String;
0 commit comments