Skip to content

Commit 6a06295

Browse files
Merge branch 'topic/gs.541.format_new_buffer' into 'master'
Fix Buffer corruption during textEdit Closes #541 See merge request eng/ide/gnatstudio!875
2 parents 6141295 + 2825da2 commit 6a06295

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

lsp_client/src/gps-lsp_client-edit_workspace.adb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,19 @@ package body GPS.LSP_Client.Edit_Workspace is
180180
To : constant GPS.Editors.Editor_Location'Class :=
181181
GPS.LSP_Client.Utilities.LSP_Position_To_Location
182182
(Editor, Span.last);
183-
Span_Text : constant VSS.Strings.Virtual_String :=
184-
Editor.Get_Text
185-
(From => From,
186-
To => To,
187-
Include_Hidden_Chars => False);
183+
-- We are using Get_Text below which includes the character after
184+
-- To. TextEdits should ignore this character so move To backward
185+
-- now to compensate.
186+
-- Also handle the special case where From = To, we should not
187+
-- move To backward or we will get the characters between From - 1
188+
-- and To + 1.
188189
Old_Text : constant VSS.Strings.Virtual_String :=
189-
Span_Text.Head_Before (Span_Text.At_Last_Character);
190-
-- XXX It is not clear why last character is ignored here. There
191-
-- are two incorrect things might be here:
192-
-- - remove LF character (CRLF must be handled too)
193-
-- - conversion of LSP range is not equivalent to conversion of
194-
-- two LSP positions
195-
-- Need to be investigated!
190+
(if From = To
191+
then VSS.Strings.Empty_Virtual_String
192+
else Editor.Get_Text
193+
(From => From,
194+
To => To.Forward_Char (-1),
195+
Include_Hidden_Chars => False));
196196
New_Text : constant VSS.Strings.Virtual_String :=
197197
Vectors.Element (C).Text;
198198

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
project Default is
2+
3+
end Default;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Create a new buffer and try to format it
3+
"""
4+
5+
import GPS
6+
from gs_utils.internal.utils import (
7+
run_test_driver,
8+
wait_idle,
9+
gps_assert,
10+
wait_language_server,
11+
timeout,
12+
)
13+
14+
INIT_TEXT = """procedure Main is
15+
begin
16+
null;
17+
end Main;"""
18+
19+
EXPECTED_TEXT = """procedure Main is
20+
begin
21+
null;
22+
end Main;"""
23+
24+
25+
@run_test_driver
26+
def driver():
27+
# Use GNATFormat LSP backend
28+
GPS.Preference("LSP-Ada-Use-GNATformat").set(True)
29+
GPS.Preference("Editor-Range-Formatter").set("LSP")
30+
b = GPS.EditorBuffer.get(GPS.File("main.adb"))
31+
yield wait_idle()
32+
b.insert(b.at(1, 1), INIT_TEXT)
33+
b.select(b.at(0, 0), b.at(4, 9))
34+
yield timeout(5000)
35+
36+
# Format the second line in the aggregate
37+
GPS.execute_action("format selection")
38+
yield wait_language_server("textDocument/rangeFormatting")
39+
yield wait_idle()
40+
41+
gps_assert(
42+
b.get_chars(), EXPECTED_TEXT, "Wrong formatting"
43+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: 'gs.541.format_new_buffer'

0 commit comments

Comments
 (0)