@@ -43,20 +43,18 @@ public class Document {
4343
4444 public final String uri ;
4545 public final int version ;
46- public final String text ;
4746
4847 private final List <String > textLines ;
4948
50- private Document (String uri , int version , String text , List <String > textLines ) {
49+ private Document (String uri , int version , List <String > textLines ) {
5150 this .uri = uri ;
5251 this .version = version ;
53- this .text = text ;
5452 this .textLines = Collections .unmodifiableList (textLines );
5553 }
5654
5755
5856 public Document (String uri , int version , String text ) {
59- this (uri , version , text , initTextLines (text ));
57+ this (uri , version , splitLines (text ));
6058 }
6159
6260 /**
@@ -69,19 +67,19 @@ public Document(TextDocumentItem tdi) {
6967 }
7068
7169 /**
72- * Provides an updated version of this document.
70+ * Creates an updated version of this document.
7371 */
7472 public Document withChanges (int newVersion , List <TextDocumentContentChangeEvent > contentChanges ) {
7573 if (newVersion <= this .version ) {
76- throw new RuntimeException (
74+ throw new IllegalStateException (
7775 "Cannot update LSP document to version " + newVersion
7876 + " as current version is already " + this .version
7977 );
8078 }
8179
8280 // Shortcuts
8381 if (contentChanges .isEmpty ()) {
84- return new Document (this .uri , newVersion , this .text , this . textLines );
82+ return new Document (this .uri , newVersion , this .textLines );
8583 }
8684 if (contentChanges .size () == 1 ) {
8785 var change = contentChanges .getFirst ();
@@ -97,7 +95,7 @@ public Document withChanges(int newVersion, List<TextDocumentContentChangeEvent>
9795
9896 if (range == null ) {
9997 // Change replaces entire document
100- newTextLines = initTextLines ( change .getText ());
98+ newTextLines = new ArrayList <>( splitLines ( change .getText () ));
10199 continue ;
102100 }
103101
@@ -115,10 +113,10 @@ public Document withChanges(int newVersion, List<TextDocumentContentChangeEvent>
115113 continue ;
116114 }
117115
118- var insertTextLines = Arrays . asList ( splitLines (
116+ var insertTextLines = splitLines (
119117 startLineText .substring (0 , startCharacter ) + change .getText ()
120118 + endLineText .substring (endCharacter )
121- )) ;
119+ );
122120
123121 if (endLine == startLine && insertTextLines .size () == 1 ) {
124122 // Shortcut
@@ -130,7 +128,11 @@ public Document withChanges(int newVersion, List<TextDocumentContentChangeEvent>
130128 newTextLines .addAll (startLine , insertTextLines );
131129 }
132130
133- return new Document (uri , newVersion , String .join ("\n " , newTextLines ), newTextLines );
131+ return new Document (uri , newVersion , newTextLines );
132+ }
133+
134+ public String getText () {
135+ return String .join ("\n " , textLines );
134136 }
135137
136138 public Path getPath () {
@@ -213,15 +215,16 @@ public List<Integer> calculateUtf16Positions(
213215 }
214216
215217
216- private static List <String > initTextLines (String fullText ) {
218+ /**
219+ * Splits the given String into individual lines (as stored in {@code textLines}).
220+ *
221+ * @return text lines. This List has a fixed size! (see {@link Arrays#asList(Object[])})
222+ */
223+ private static List <String > splitLines (String text ) {
217224 // Note: We don't distinguish between the different line endings; and as long as this server
218225 // doesn't make editing suggestions to the client (which may use different eol sequences
219226 // than the user) this should be fine.
220- return new ArrayList <>(Arrays .asList (splitLines (fullText )));
221- }
222-
223- private static String [] splitLines (String text ) {
224- return EOL_REGEX .split (text , -1 );
227+ return Arrays .asList (EOL_REGEX .split (text , -1 ));
225228 }
226229
227230 private static int normalizeLineOffset (int lineOffset , List <String > textLines ) {
0 commit comments