Skip to content

Commit

Permalink
GH-1280: do not show up mapping completions on the type declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlippert committed Jun 21, 2024
1 parent 2fa1f61 commit be10667
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public interface JavaSnippetContext {
JavaSnippetContext BOOT_MEMBERS = (node, offset, prefix) -> node instanceof TypeDeclaration || node instanceof SimpleName;

JavaSnippetContext AT_ROOT_LEVEL = (node, offset, prefix) -> {
if (node instanceof TypeDeclaration) return true;
if (node instanceof TypeDeclaration) {
TypeDeclaration typeNode = (TypeDeclaration) node;
SimpleName name = typeNode.getName();

return offset > (name.getStartPosition() + name.getLength());
}

ASTNode nodeBeforePrefix = NodeFinder.perform(node.getRoot(), offset - (prefix.length() + 1), 0);
return nodeBeforePrefix != null && nodeBeforePrefix instanceof TypeDeclaration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -227,13 +228,23 @@ void testSnippetNotShowUpWithinMethodParamDeclaration() throws Exception {
}
}

@Test
void testSnippetNotShowUpOnClassDeclaration() throws Exception {
prepareCase(CONTROLLER_CLASSNAME, "@Controller", "@Controller\n<*>");

List<CompletionItem> completions = editor.getCompletions();
for (CompletionItem completionItem : completions) {
assertNotEquals("@GetMapping(..) {..}", completionItem.getLabel());
}
}

private void prepareCase(String className, String prefix) throws Exception {
prepareCase(className, "class " + className + " {", "class " + className + " {\n\n" + prefix);
}

private void prepareCase(String className, String replace, String replaceWith) throws Exception {
InputStream resource = this.getClass().getResourceAsStream("/test-projects/test-request-mapping-completions/src/main/java/example/" + className + ".java");
String content = IOUtils.toString(resource);
String content = IOUtils.toString(resource, Charset.defaultCharset());

content = content.replace(replace, replaceWith);
editor = new Editor(harness, content, LanguageId.JAVA);
Expand Down

0 comments on commit be10667

Please sign in to comment.