Skip to content

Commit 0563f49

Browse files
BoulangerAdrienBoulanger
authored andcommitted
Add fallback indenter for onTypeFormatting on IndentOnly is False
In this case, the previous line should be formatted so indent it. Fix missing current line of the buffer for rangeFormatting allowing to reindent the current line with the fallback indenter. For eng/ide/ada_language_server#1729
1 parent cc3c613 commit 0563f49

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

source/ada/lsp-ada_formatter.adb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ package body LSP.Ada_Formatter is
226226
Buffer =>
227227
Document.Get_Text ((0, 0), (Value.position.line + 1, 0)),
228228
Span =>
229-
((Value.position.line, 0), (Value.position.line + 1, 0)),
229+
((0, 0), (Value.position.line + 1, 0)),
230230
Options => Full_Options);
231231
Indentation : constant VSS.Strings.Character_Count :=
232232
(declare
@@ -308,6 +308,26 @@ package body LSP.Ada_Formatter is
308308
(Filename => Context.URI_To_File (Document.URI),
309309
Options => Full_Options,
310310
S => Indentation * ' ')));
311+
312+
-- If not in indent-only mode, re-indent the previous line too
313+
if not Self.Parent.Context.Get_Configuration.Indent_Only then
314+
declare
315+
Prev_Line : constant Natural :=
316+
Natural'Max (Value.position.line - 1, 0);
317+
Indentation : constant Natural :=
318+
(if Indent_Array (Value.position.line) = -1
319+
then 0
320+
else Indent_Array (Value.position.line));
321+
begin
322+
Response.Append
323+
(LSP.Ada_Handlers.Formatting.Reindent_Line
324+
(Filename => Context.URI_To_File (Document.URI),
325+
Line => Document.Get_Line (Prev_Line),
326+
Pos => (Prev_Line, 0),
327+
Options => Full_Options,
328+
New_Indent => Indentation));
329+
end;
330+
end if;
311331
end Handle_Document_With_Diagnostics;
312332

313333
-----------------------------------------

source/ada/lsp-ada_handlers-formatting.adb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ package body LSP.Ada_Handlers.Formatting is
215215
Buffer : constant VSS.Strings.Virtual_String :=
216216
(if Span = Empty_Range
217217
then Document.Text
218-
else Document.Slice (((0, 0), Actual_Span.an_end)));
218+
else Document.Slice (((0, 0), (Actual_Span.an_end.line + 1, 0))));
219219
-- Get the relevant buffer to indent.
220220
-- If no span is provided, get the whole document buffer.
221221
-- Otherwise get the buffer from the start of the document

0 commit comments

Comments
 (0)