Skip to content

Commit

Permalink
Added support to Windows end of line in AcceleoLanguageServerPosition…
Browse files Browse the repository at this point in the history
…Utils.
  • Loading branch information
ylussaud committed Jan 15, 2024
1 parent dc8cdd4 commit e97d28c
Showing 1 changed file with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
*/
public final class AcceleoLanguageServerPositionUtils {

/**
* New line.
*/
public static final String NEW_LINE = "\n";

/**
* New line.
*/
public static final String WINDOWS_NEW_LINE = "\r\n";

private AcceleoLanguageServerPositionUtils() {
// Utility class.
}
Expand Down Expand Up @@ -412,13 +422,14 @@ public static int getCorrespondingCharacterIndex(Position position, String text)

int currentLine = 0;
int currentIndex = 0;
char currentCharacter;
while (currentLine != positionLine && currentIndex < text.length()) {
currentCharacter = text.charAt(currentIndex);
if (currentCharacter == '\n') {
final int newLineLength = startsWithNewLine(text.substring(currentIndex));
if (newLineLength != 0) {
currentLine++;
currentIndex += newLineLength;
} else {
currentIndex++;
}
currentIndex++;
}
if (currentLine == positionLine) {
return currentIndex + positionOffset;
Expand All @@ -429,6 +440,27 @@ public static int getCorrespondingCharacterIndex(Position position, String text)
}
}

/**
* Tells if the given text starts with a new line and return if length.
*
* @param text
* the text
* @return the length of the new line if the given text starts with a new line, <code>0</code> otherwise
*/
private static int startsWithNewLine(String text) {
final int res;

if (text.startsWith(WINDOWS_NEW_LINE)) {
res = WINDOWS_NEW_LINE.length();
} else if (text.startsWith(NEW_LINE)) {
res = NEW_LINE.length();
} else {
res = 0;
}

return res;
}

/**
* Creates the {@link Range} identified by the begin and end character indices in the given {@link String
* text}.
Expand Down Expand Up @@ -459,25 +491,21 @@ public static Range getCorrespondingRange(int beginCharacterIndex, int endCharac
public static Position getCorrespondingPosition(int characterIndex, String text) {
Objects.requireNonNull(text);

int lineNumber = 0;
int indexOfLastNewLine = -1;
for (int currentIndex = 0; currentIndex < characterIndex; currentIndex++) {
Character currentCharacter = text.charAt(currentIndex);
if (currentCharacter == '\n') {
lineNumber++;
indexOfLastNewLine = currentIndex;
int currentLine = 0;
int currentColumn = 0;
int i = 0;
while (i < characterIndex) {
final int newLineLength = startsWithNewLine(text.substring(i));
if (newLineLength != 0) {
currentLine++;
currentColumn = 0;
i += newLineLength;
} else {
currentColumn++;
i++;
}
}
int offset;
if (lineNumber == 0) {
// The text is only one line, so the offset is the same as the character index.
offset = characterIndex;
} else {
// The text is on a line that starts at character index "indexOfLastNewLine", but the newline is a
// character so we subtract 1.
offset = characterIndex - indexOfLastNewLine - 1;
}

return new Position(lineNumber, offset);
return new Position(currentLine, currentColumn);
}
}

0 comments on commit e97d28c

Please sign in to comment.