From 1f6fde4bc25b22f4a3a66635aa3c4912a4e236c4 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Wed, 12 Dec 2018 00:02:25 +0300 Subject: [PATCH] KT-20706: Fix auto-popup completion after digit in KDoc #KT-20706 Fixed --- .../idea/completion/KDocCompletionContributor.kt | 10 ++++++---- .../testData/kdoc/AutoPopupAfterAtInKDoc.kt | 8 ++++++++ .../testData/kdoc/NoAutoPopupAfterDigitInKDoc.kt | 8 ++++++++ idea/idea-completion/testData/kdoc/TagName.kt | 1 + idea/idea-completion/testData/kdoc/TagNameInClass.kt | 1 + .../testData/kdoc/TagNameInExtensionFunction.kt | 1 + idea/idea-completion/testData/kdoc/TagNameStart.kt | 1 + .../completion/test/KDocCompletionTestGenerated.java | 10 ++++++++++ .../kotlin/idea/editor/KotlinTypedHandler.java | 12 ++++++++++++ 9 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 idea/idea-completion/testData/kdoc/AutoPopupAfterAtInKDoc.kt create mode 100644 idea/idea-completion/testData/kdoc/NoAutoPopupAfterDigitInKDoc.kt diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KDocCompletionContributor.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KDocCompletionContributor.kt index 83c6604c781..633c8fc6791 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KDocCompletionContributor.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KDocCompletionContributor.kt @@ -133,11 +133,13 @@ object KDocTagCompletionProvider : CompletionProvider() { override fun addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) { // findIdentifierPrefix() requires identifier part characters to be a superset of identifier start characters val prefix = CompletionUtil.findIdentifierPrefix( - parameters.position.containingFile, - parameters.offset, - StandardPatterns.character().javaIdentifierPart() or singleCharPattern('@'), - StandardPatterns.character().javaIdentifierStart() or singleCharPattern('@')) + parameters.position.containingFile, + parameters.offset, + StandardPatterns.character().javaIdentifierPart() or singleCharPattern('@'), + StandardPatterns.character().javaIdentifierStart() or singleCharPattern('@') + ) + if (parameters.isAutoPopup && prefix.isEmpty()) return if (prefix.isNotEmpty() && !prefix.startsWith('@')) { return } diff --git a/idea/idea-completion/testData/kdoc/AutoPopupAfterAtInKDoc.kt b/idea/idea-completion/testData/kdoc/AutoPopupAfterAtInKDoc.kt new file mode 100644 index 00000000000..9056856c8a3 --- /dev/null +++ b/idea/idea-completion/testData/kdoc/AutoPopupAfterAtInKDoc.kt @@ -0,0 +1,8 @@ + +/** + * @ + */ +fun some() {} + +// INVOCATION_COUNT: 0 +// EXIST: @return \ No newline at end of file diff --git a/idea/idea-completion/testData/kdoc/NoAutoPopupAfterDigitInKDoc.kt b/idea/idea-completion/testData/kdoc/NoAutoPopupAfterDigitInKDoc.kt new file mode 100644 index 00000000000..a365bcf846f --- /dev/null +++ b/idea/idea-completion/testData/kdoc/NoAutoPopupAfterDigitInKDoc.kt @@ -0,0 +1,8 @@ + +/** + * 0 + */ +fun some() {} + +// INVOCATION_COUNT: 0 +// NOTHING_ELSE \ No newline at end of file diff --git a/idea/idea-completion/testData/kdoc/TagName.kt b/idea/idea-completion/testData/kdoc/TagName.kt index 2445b4f9752..ec1b5ed7a0a 100644 --- a/idea/idea-completion/testData/kdoc/TagName.kt +++ b/idea/idea-completion/testData/kdoc/TagName.kt @@ -8,3 +8,4 @@ fun f(x: Int): Int { // EXIST: @return // EXIST: @suppress // ABSENT: @receiver +// INVOCATION_COUNT: 1 \ No newline at end of file diff --git a/idea/idea-completion/testData/kdoc/TagNameInClass.kt b/idea/idea-completion/testData/kdoc/TagNameInClass.kt index 7a9f9d92ec0..a02795cac1e 100644 --- a/idea/idea-completion/testData/kdoc/TagNameInClass.kt +++ b/idea/idea-completion/testData/kdoc/TagNameInClass.kt @@ -10,3 +10,4 @@ class C { // EXIST: @property // ABSENT: @return // ABSENT: @receiver +// INVOCATION_COUNT: 1 \ No newline at end of file diff --git a/idea/idea-completion/testData/kdoc/TagNameInExtensionFunction.kt b/idea/idea-completion/testData/kdoc/TagNameInExtensionFunction.kt index e31c5449882..eb02594999f 100644 --- a/idea/idea-completion/testData/kdoc/TagNameInExtensionFunction.kt +++ b/idea/idea-completion/testData/kdoc/TagNameInExtensionFunction.kt @@ -10,3 +10,4 @@ fun Int.f(): Int { // EXIST: @receiver // ABSENT: @constructor // ABSENT: @property +// INVOCATION_COUNT: 1 diff --git a/idea/idea-completion/testData/kdoc/TagNameStart.kt b/idea/idea-completion/testData/kdoc/TagNameStart.kt index 31ec329eb26..d59de95825d 100644 --- a/idea/idea-completion/testData/kdoc/TagNameStart.kt +++ b/idea/idea-completion/testData/kdoc/TagNameStart.kt @@ -4,3 +4,4 @@ fun f(x: Int): Int { // EXIST: @param // EXIST: @return +// INVOCATION_COUNT: 1 \ No newline at end of file diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KDocCompletionTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KDocCompletionTestGenerated.java index de4aac48beb..22d568a2a2a 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KDocCompletionTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KDocCompletionTestGenerated.java @@ -34,6 +34,11 @@ public class KDocCompletionTestGenerated extends AbstractJvmBasicCompletionTest KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/kdoc"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("AutoPopupAfterAtInKDoc.kt") + public void testAutoPopupAfterAtInKDoc() throws Exception { + runTest("idea/idea-completion/testData/kdoc/AutoPopupAfterAtInKDoc.kt"); + } + @TestMetadata("ExtensionsFQLink.kt") public void testExtensionsFQLink() throws Exception { runTest("idea/idea-completion/testData/kdoc/ExtensionsFQLink.kt"); @@ -79,6 +84,11 @@ public class KDocCompletionTestGenerated extends AbstractJvmBasicCompletionTest runTest("idea/idea-completion/testData/kdoc/MemberLink.kt"); } + @TestMetadata("NoAutoPopupAfterDigitInKDoc.kt") + public void testNoAutoPopupAfterDigitInKDoc() throws Exception { + runTest("idea/idea-completion/testData/kdoc/NoAutoPopupAfterDigitInKDoc.kt"); + } + @TestMetadata("NoCompletionAfterFunName.kt") public void testNoCompletionAfterFunName() throws Exception { runTest("idea/idea-completion/testData/kdoc/NoCompletionAfterFunName.kt"); diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java index 26a9b20e435..bcda18c1835 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java @@ -43,6 +43,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.text.CharArrayUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.KtNodeTypes; +import org.jetbrains.kotlin.kdoc.lexer.KDocTokens; import org.jetbrains.kotlin.lexer.KtTokens; import org.jetbrains.kotlin.psi.*; @@ -127,6 +128,7 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { case '@': autoPopupLabelLookup(project, editor); + autoPopupKDocTag(project, editor); return Result.CONTINUE; case ':': @@ -179,6 +181,16 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { }); } + private static void autoPopupKDocTag(Project project, final Editor editor) { + AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, (PsiFile file) -> { + int offset = editor.getCaretModel().getOffset(); + PsiElement lastElement = file.findElementAt(offset - 1); + if (lastElement == null) return false; + + return lastElement.getNode().getElementType() == KDocTokens.TEXT; + }); + } + private static void autoPopupLabelLookup(Project project, final Editor editor) { AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, new Condition() { @Override