Skip to content

Commit

Permalink
Get rid of extraneous Document methods
Browse files Browse the repository at this point in the history
  • Loading branch information
milesziemer committed Jan 17, 2025
1 parent 8d319d9 commit 3927e8a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 236 deletions.
156 changes: 26 additions & 130 deletions src/main/java/software/amazon/smithy/lsp/document/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public int indexOfPosition(int line, int character) {
return -1;
}


int idx = startLineIdx + character;
if (line == lastLine()) {
if (idx >= buffer.length()) {
Expand Down Expand Up @@ -196,9 +195,7 @@ public Range rangeBetween(int start, int end) {

Position endPos;
if (end == length()) {
int lastLine = lastLine();
int lastCol = length() - lineIndices[lastLine];
endPos = new Position(lastLine, lastCol);
endPos = end();
} else {
endPos = positionAtIndex(end);
}
Expand Down Expand Up @@ -234,9 +231,7 @@ public int lastLine() {
* @return The end position of this document
*/
public Position end() {
return new Position(
lineIndices.length - 1,
buffer.length() - lineIndices[lineIndices.length - 1]);
return new Position(lastLine(), lastColExclusive());
}

/**
Expand Down Expand Up @@ -266,111 +261,6 @@ public CharSequence borrowText() {
return buffer;
}

/**
* @param range The range to borrow the text of
* @return A reference to the text in this document within the given {@code range}
* or {@code null} if the range is out of bounds
*/
public CharBuffer borrowRange(Range range) {
int startLine = range.getStart().getLine();
int startChar = range.getStart().getCharacter();
int endLine = range.getEnd().getLine();
int endChar = range.getEnd().getCharacter();

// TODO: Maybe make this return the whole thing, thing up to an index, or thing after an
// index if one of the indicies is out of bounds.
int startLineIdx = indexOfLine(startLine);
int endLineIdx = indexOfLine(endLine);
if (startLineIdx < 0 || endLineIdx < 0) {
return null;
}

int startIdx = startLineIdx + startChar;
int endIdx = endLineIdx + endChar;
if (startIdx > buffer.length() || endIdx > buffer.length()) {
return null;
}

return CharBuffer.wrap(buffer, startIdx, endIdx);
}

/**
* @param position The position within the token to borrow
* @return A reference to the token that the given {@code position} is
* within, or {@code null} if the position is not within a token
*/
public CharBuffer borrowToken(Position position) {
int idx = indexOfPosition(position);
if (idx < 0) {
return null;
}

char atIdx = buffer.charAt(idx);
// Not a token
if (!Character.isLetterOrDigit(atIdx) && atIdx != '_') {
return null;
}

int startIdx = idx;
while (startIdx >= 0) {
char c = buffer.charAt(startIdx);
if (Character.isLetterOrDigit(c) || c == '_') {
startIdx--;
} else {
break;
}
}

int endIdx = idx;
while (endIdx < buffer.length()) {
char c = buffer.charAt(endIdx);
if (Character.isLetterOrDigit(c) || c == '_') {
endIdx++;
} else {
break;
}
}

return CharBuffer.wrap(buffer, startIdx + 1, endIdx);
}

/**
* @param line The line to borrow
* @return A reference to the text in the given line, or {@code null} if
* the line doesn't exist
*/
public CharBuffer borrowLine(int line) {
if (line >= lineIndices.length || line < 0) {
return null;
}

int lineStart = indexOfLine(line);
if (line + 1 >= lineIndices.length) {
return CharBuffer.wrap(buffer, lineStart, buffer.length());
}

return CharBuffer.wrap(buffer, lineStart, indexOfLine(line + 1));
}

/**
* @param start The index of the start of the span to borrow
* @param end The end of the index of the span to borrow (exclusive)
* @return A reference to the text within the indicies {@code start} and
* {@code end}, or {@code null} if the span is out of bounds or start > end
*/
public CharBuffer borrowSpan(int start, int end) {
if (start < 0 || end < 0) {
return null;
}

// end is exclusive
if (end > buffer.length() || start > end) {
return null;
}

return CharBuffer.wrap(buffer, start, end);
}

/**
* @return A copy of the text of this document
*/
Expand All @@ -384,12 +274,20 @@ public String copyText() {
* or {@code null} if the range is out of bounds
*/
public String copyRange(Range range) {
CharBuffer borrowed = borrowRange(range);
if (borrowed == null) {
return null;
int start = indexOfPosition(range.getStart());

int end;
Position endPosition = range.getEnd();
if (endPosition.getLine() == lastLine() && endPosition.getCharacter() == lastColExclusive()) {
end = length();
} else {
end = indexOfPosition(range.getEnd());
}
return copySpan(start, end);
}

return borrowed.toString();
private int lastColExclusive() {
return length() - lineIndices[lastLine()];
}

/**
Expand Down Expand Up @@ -479,18 +377,11 @@ public DocumentId copyDocumentId(Position position) {
}

// We go past the start and end in each loop, so startIdx is before the start character, and endIdx
// is after the end character.
// is after the end character. Since end is exclusive (both for creating the buffer and getting the
// range) we can leave it.
int startCharIdx = startIdx + 1;
int endCharIdx = endIdx - 1;

// For creating the buffer and the range, the start is inclusive, and the end is exclusive.
CharBuffer wrapped = CharBuffer.wrap(buffer, startCharIdx, endCharIdx + 1);
Position start = positionAtIndex(startCharIdx);
// However, we can't get the position for an index that may be out of bounds, so we need to make
// the end position exclusive manually.
Position end = positionAtIndex(endCharIdx);
end.setCharacter(end.getCharacter() + 1);
Range range = new Range(start, end);
CharBuffer wrapped = CharBuffer.wrap(buffer, startCharIdx, endIdx);
Range range = rangeBetween(startCharIdx, endIdx);
return new DocumentId(type, wrapped, range);
}

Expand All @@ -505,11 +396,16 @@ private static boolean isIdChar(char c) {
* {@code end}, or {@code null} if the span is out of bounds or start > end
*/
public String copySpan(int start, int end) {
CharBuffer borrowed = borrowSpan(start, end);
if (borrowed == null) {
if (start < 0 || end < 0) {
return null;
}
return borrowed.toString();

// end is exclusive
if (end > buffer.length() || start > end) {
return null;
}

return CharBuffer.wrap(buffer, start, end).toString();
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/test/java/software/amazon/smithy/lsp/LspMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,20 @@ public void describeMismatchSafely(Collection<TextEdit> item, Description descri
}

public static Matcher<Range> hasText(Document document, Matcher<String> expected) {
return new CustomTypeSafeMatcher<>("text in range") {
return new CustomTypeSafeMatcher<>("text in range " + expected.toString()) {
@Override
protected boolean matchesSafely(Range item) {
CharSequence borrowed = document.borrowRange(item);
if (borrowed == null) {
return false;
}
return expected.matches(borrowed.toString());
String actual = document.copyRange(item);
return expected.matches(actual);
}

@Override
public void describeMismatchSafely(Range range, Description description) {
if (document.borrowRange(range) == null) {
if (document.copyRange(range) == null) {
description.appendText("text was null");
} else {
description.appendDescriptionOf(expected)
.appendText("was " + document.borrowRange(range).toString());
.appendText("was " + document.copyRange(range));
}
}
};
Expand Down
102 changes: 4 additions & 98 deletions src/test/java/software/amazon/smithy/lsp/document/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,88 +255,11 @@ public void getsEnd() {
}

@Test
public void borrowsToken() {
String s = "abc\n" +
"def";
Document document = makeDocument(s);

CharSequence token = document.borrowToken(new Position(0, 2));

assertThat(token, string("abc"));
}

@Test
public void borrowsTokenWithNoWs() {
String s = "abc";
Document document = makeDocument(s);

CharSequence token = document.borrowToken(new Position(0, 1));

assertThat(token, string("abc"));
}

@Test
public void borrowsTokenAtStart() {
String s = "abc\n" +
"def";
Document document = makeDocument(s);

CharSequence token = document.borrowToken(new Position(0, 0));

assertThat(token, string("abc"));
}

@Test
public void borrowsTokenAtEnd() {
String s = "abc\n" +
"def";
Document document = makeDocument(s);
public void foo() {
Document a = makeDocument("abc");
Document b = makeDocument("def\n");

CharSequence token = document.borrowToken(new Position(1, 2));

assertThat(token, string("def"));
}

@Test
public void borrowsTokenAtBoundaryStart() {
String s = "a bc d";
Document document = makeDocument(s);

CharSequence token = document.borrowToken(new Position(0, 2));

assertThat(token, string("bc"));
}

@Test
public void borrowsTokenAtBoundaryEnd() {
String s = "a bc d";
Document document = makeDocument(s);

CharSequence token = document.borrowToken(new Position(0, 3));

assertThat(token, string("bc"));
}

@Test
public void doesntBorrowNonToken() {
String s = "abc def";
Document document = makeDocument(s);

CharSequence token = document.borrowToken(new Position(0, 3));

assertThat(token, nullValue());
}

@Test
public void borrowsLine() {
Document document = makeDocument("abc\n\ndef");

assertThat(makeDocument("").borrowLine(0), string(""));
assertThat(document.borrowLine(0), string(safeString("abc\n")));
assertThat(document.borrowLine(1), string(safeString("\n")));
assertThat(document.borrowLine(2), string("def"));
assertThat(document.borrowLine(-1), nullValue());
assertThat(document.borrowLine(3), nullValue());
System.out.println();
}

@Test
Expand Down Expand Up @@ -370,23 +293,6 @@ public void getsLastIndexOf() {
assertThat(document.lastIndexOf(" ", safeIndex(8, 1)), is(-1)); // not found
}

@Test
public void borrowsSpan() {
Document empty = makeDocument("");
Document line = makeDocument("abc");
Document multi = makeDocument("abc\ndef\n\n");

assertThat(empty.borrowSpan(0, 1), nullValue()); // empty
assertThat(line.borrowSpan(-1, 1), nullValue()); // negative
assertThat(line.borrowSpan(0, 0), string("")); // empty
assertThat(line.borrowSpan(0, 1), string("a")); // one
assertThat(line.borrowSpan(0, 3), string("abc")); // all
assertThat(line.borrowSpan(0, 4), nullValue()); // oob
assertThat(multi.borrowSpan(0, safeIndex(4, 1)), string(safeString("abc\n"))); // with newline
assertThat(multi.borrowSpan(3, safeIndex(5, 1)), string(safeString("\nd"))); // inner
assertThat(multi.borrowSpan(safeIndex(5, 1), safeIndex(9, 3)), string(safeString("ef\n\n"))); // up to end
}

@Test
public void getsLineOfIndex() {
Document empty = makeDocument("");
Expand Down

0 comments on commit 3927e8a

Please sign in to comment.