Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix off-by-one issues in TokenTree and TreeCursor #1891

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,18 @@ public IdlToken next() {
if (getToken().getIdlToken() == IdlToken.EOF) {
throw new NoSuchElementException();
}
trees.getFirst().appendChild(TokenTree.of(CapturedToken.from(this, this.stringTable)));
appendCurrentTokenToFirstTree();
return tokens.get(++cursor).getIdlToken();
}

void eof() {
appendCurrentTokenToFirstTree();
}

private void appendCurrentTokenToFirstTree() {
trees.getFirst().appendChild(TokenTree.of(CapturedToken.from(this, this.stringTable)));
}

CapturedToken peekPastSpaces() {
return peekWhile(0, token -> token == IdlToken.SPACE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public TreeCursor findAt(int line, int column) {
isMatch = column >= startColumn && column < endColumn;
} else if (line == startLine && column >= startColumn) {
isMatch = true;
} else if (line == endLine && column <= endColumn) {
} else if (line == endLine && column < endColumn) {
isMatch = true;
} else if (line > startLine && line < endLine) {
isMatch = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,8 @@ void parse(CapturingTokenizer tokenizer) {
optionalWs(tokenizer);
break;
case EOF:
tokenizer.eof();
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class TreeCursorTest {
@Test
public void hasChildren() {
TokenTree tree = createTree();
TokenTree tree = createTree("simple-model.smithy");
TreeCursor cursor = tree.zipper();
List<TreeCursor> children = cursor.getChildren();

Expand All @@ -36,7 +36,7 @@ public void hasChildren() {

@Test
public void hasParentAndSiblings() {
TokenTree tree = createTree();
TokenTree tree = createTree("simple-model.smithy");
TreeCursor cursor = tree.zipper();
List<TreeCursor> children = cursor.getChildren();

Expand All @@ -55,7 +55,7 @@ public void hasParentAndSiblings() {

@Test
public void findsNodeAtPosition() {
TokenTree tree = createTree();
TokenTree tree = createTree("simple-model.smithy");
TreeCursor cursor = tree.zipper();
TreeCursor click = cursor.findAt(3, 17);

Expand All @@ -65,8 +65,32 @@ public void findsNodeAtPosition() {
assertThat(click.getRoot(), equalTo(cursor));
}

private TokenTree createTree() {
String model = IoUtils.readUtf8Url(getClass().getResource("formatter/simple-model.smithy"));
@Test
public void findsNodeAtPositionBetweenTokens() {
TokenTree tree = createTree("incorrect-indentation.smithy");
TreeCursor cursor = tree.zipper();
TreeCursor click = cursor.findAt(10, 5);

assertThat(click, notNullValue());
assertThat(click.getTree().getType(), is(TreeType.TOKEN));
assertThat(click.getTree().tokens().iterator().next().getLexeme().toString(), equalTo("@"));
assertThat(click.getRoot(), equalTo(cursor));
}

@Test
public void findsNodeAtLastLineOfFile() {
TokenTree tree = createTree("missing-trailing-newline.smithy");
TreeCursor cursor = tree.zipper();
TreeCursor click = cursor.findAt(8, 4);

assertThat(click, notNullValue());
assertThat(click.getTree().getType(), is(TreeType.TOKEN));
assertThat(click.getTree().tokens().iterator().next().getLexeme().toString(), equalTo("foo"));
assertThat(click.getRoot(), equalTo(cursor));
}

private TokenTree createTree(String filename) {
String model = IoUtils.readUtf8Url(getClass().getResource("formatter/" + filename));
IdlTokenizer tokenizer = IdlTokenizer.create(model);
return TokenTree.of(tokenizer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$version: "2.0"

namespace smithy.example

structure Foo {}

structure Bar {}

// Comment
@input
structure Baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$version: "2.0"

namespace smithy.example

structure Foo {}

structure Bar {}

// Comment
@input
structure Baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$version: "2.0"

namespace smithy.example

@trait
structure foo {}

@foo
structure Bar {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$version: "2.0"

namespace smithy.example

@trait
structure foo {}

@foo
structure Bar {}