Skip to content

Commit

Permalink
Some fixes to CompletionContextTests
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Dec 6, 2024
1 parent 42267bc commit f44d86b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,38 +113,65 @@ public int getTokenLocation() {
if (parent instanceof ImportDeclaration) {
return TL_IN_IMPORT;
}
parent = parent.getParent();
}
if (this.node.getParent() instanceof FieldAccess
|| this.node.getParent() instanceof QualifiedName) {
return 0;
}
if (this.node.getParent() instanceof VariableDeclaration) {
return 0;
}
if (this.node instanceof AbstractTypeDeclaration) {
return TL_MEMBER_START;
}
var locationInParent = node.getLocationInParent();
parent = node;
while (parent != null) {
if (parent instanceof ClassInstanceCreation) {
return TL_CONSTRUCTOR_START;
}
if (parent instanceof Block) {
return TL_STATEMENT_START;
}
if (parent instanceof AbstractTypeDeclaration) {
if (locationInParent == AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY ||
locationInParent == EnumDeclaration.BODY_DECLARATIONS_PROPERTY ||
locationInParent == ImplicitTypeDeclaration.BODY_DECLARATIONS_PROPERTY ||
locationInParent == RecordDeclaration.BODY_DECLARATIONS_PROPERTY ||
locationInParent == TypeDeclaration.BODY_DECLARATIONS_PROPERTY ||
locationInParent == AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY) {
return TL_MEMBER_START;
}
if (parent instanceof FieldAccess) {
return 0;
}
if (parent instanceof VariableDeclaration) {
return 0;
}
locationInParent = parent.getLocationInParent();
parent = parent.getParent();
}
return super.getTokenLocation();
return 0;
}

@Override
public int getTokenStart() {
return node.getStartPosition();
if (node instanceof SimpleName) {
return node.getStartPosition();
}
return this.offset;
}
@Override
public int getTokenEnd() {
return node.getStartPosition() + node.getLength() - 1;
if (node instanceof SimpleName) {
return node.getStartPosition() + node.getLength() - 1;
}
return getTokenStart() + token.length - 1;
}

@Override
public int getTokenKind() {
if (node instanceof Name) {
return TOKEN_KIND_NAME;
}
if (node instanceof StringLiteral) {
return TOKEN_KIND_STRING_LITERAL;
}
return super.getTokenKind();
return node instanceof StringLiteral ? TOKEN_KIND_STRING_LITERAL : TOKEN_KIND_NAME;
}

/// adapted from org.eclipse.jdt.internal.codeassist.InternalExtendedCompletionContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void run() {
completeAfter = simpleName.getIdentifier().substring(0, simpleName.getIdentifier().length() <= charCount ? simpleName.getIdentifier().length() : charCount);
}
if (this.cuBuffer != null) {
if (this.cuBuffer.getChar(this.offset - 1) == '.') {
if (this.cuBuffer.getChar(this.offset - 1) == '.' || this.cuBuffer.getChar(this.offset - 1) == '/') {
completeAfter = ""; //$NON-NLS-1$
}
}
Expand All @@ -260,9 +260,20 @@ public void run() {
context = simpleName.getParent().getParent();
}
} else if (this.toComplete instanceof TextElement textElement) {
int charCount = this.offset - textElement.getStartPosition();
completeAfter = textElement.getText().substring(0, textElement.getText().length() <= charCount ? textElement.getText().length() : charCount);
context = textElement.getParent();
if (offset >= textElement.getStartPosition() + textElement.getLength()) {
completeAfter = "";
ASTNode parent = textElement.getParent();
while (parent != null && !(parent instanceof Javadoc)) {
parent = parent.getParent();
}
if (parent instanceof Javadoc javadoc) {
context = javadoc.getParent();
}
} else {
int charCount = this.offset - textElement.getStartPosition();
completeAfter = textElement.getText().substring(0, textElement.getText().length() <= charCount ? textElement.getText().length() : charCount);
context = textElement.getParent();
}
} else if (this.toComplete instanceof TagElement tagElement) {
completeAfter = tagElement.getTagName();
int atIndex = completeAfter.indexOf('@');
Expand Down

0 comments on commit f44d86b

Please sign in to comment.