From 0941be5f44a5e9724608abe0757fb9a952788d07 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 5 Oct 2022 14:58:57 +0800 Subject: [PATCH] feat(usage): enable to search for usage --- .../highlight/FeakinSyntaxHighlighter.kt | 4 ++-- ...nBraceMatcher.java => FkBraceMatcher.java} | 2 +- .../ide/search/FkFindUsagesHandler.kt | 8 +++++++ .../ide/search/FkFindUsagesHandlerFactory.kt | 19 +++++++++++++++++ .../ide/search/FkFindUsagesProvider.kt | 21 +++++++++++++++++++ .../intellij/ide/search/FkWordScanner.kt | 13 ++++++++++++ .../intellij/lexer/FeakinLexerAdapter.kt | 5 ----- .../com/feakin/intellij/lexer/FkLexer.kt | 5 +++++ .../intellij/parser/FeakinParserDefinition.kt | 5 ++--- src/main/resources/META-INF/plugin.xml | 11 +++++++--- 10 files changed, 79 insertions(+), 14 deletions(-) rename src/main/kotlin/com/feakin/intellij/ide/{FeakinBraceMatcher.java => FkBraceMatcher.java} (93%) create mode 100644 src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesHandler.kt create mode 100644 src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesHandlerFactory.kt create mode 100644 src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesProvider.kt create mode 100644 src/main/kotlin/com/feakin/intellij/ide/search/FkWordScanner.kt delete mode 100644 src/main/kotlin/com/feakin/intellij/lexer/FeakinLexerAdapter.kt create mode 100644 src/main/kotlin/com/feakin/intellij/lexer/FkLexer.kt diff --git a/src/main/kotlin/com/feakin/intellij/highlight/FeakinSyntaxHighlighter.kt b/src/main/kotlin/com/feakin/intellij/highlight/FeakinSyntaxHighlighter.kt index 823a21f..f1180e6 100644 --- a/src/main/kotlin/com/feakin/intellij/highlight/FeakinSyntaxHighlighter.kt +++ b/src/main/kotlin/com/feakin/intellij/highlight/FeakinSyntaxHighlighter.kt @@ -1,6 +1,6 @@ package com.feakin.intellij.highlight -import com.feakin.intellij.lexer.FeakinLexerAdapter +import com.feakin.intellij.lexer.FkLexer import com.feakin.intellij.lexer.FeakinElementTypes import com.intellij.lexer.Lexer import com.intellij.openapi.editor.DefaultLanguageHighlighterColors @@ -10,7 +10,7 @@ import com.intellij.psi.tree.IElementType class FeakinSyntaxHighlighter : SyntaxHighlighterBase() { override fun getHighlightingLexer(): Lexer { - return FeakinLexerAdapter() + return FkLexer() } override fun getTokenHighlights(tokenType: IElementType): Array { diff --git a/src/main/kotlin/com/feakin/intellij/ide/FeakinBraceMatcher.java b/src/main/kotlin/com/feakin/intellij/ide/FkBraceMatcher.java similarity index 93% rename from src/main/kotlin/com/feakin/intellij/ide/FeakinBraceMatcher.java rename to src/main/kotlin/com/feakin/intellij/ide/FkBraceMatcher.java index 7886093..b8269fa 100644 --- a/src/main/kotlin/com/feakin/intellij/ide/FeakinBraceMatcher.java +++ b/src/main/kotlin/com/feakin/intellij/ide/FkBraceMatcher.java @@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class FeakinBraceMatcher implements PairedBraceMatcher { +public class FkBraceMatcher implements PairedBraceMatcher { private static final BracePair[] BRACE_PAIRS = { new BracePair(FeakinElementTypes.LBRACE, FeakinElementTypes.RBRACE, true), }; diff --git a/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesHandler.kt b/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesHandler.kt new file mode 100644 index 0000000..b499476 --- /dev/null +++ b/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesHandler.kt @@ -0,0 +1,8 @@ +package com.feakin.intellij.ide.search + +import com.intellij.find.findUsages.FindUsagesHandler +import com.intellij.psi.PsiElement + +class FkFindUsagesHandler(psiElement: PsiElement, private val secondaryElements: Array) : FindUsagesHandler(psiElement) { + override fun getSecondaryElements(): Array = secondaryElements +} diff --git a/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesHandlerFactory.kt b/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesHandlerFactory.kt new file mode 100644 index 0000000..41d82c3 --- /dev/null +++ b/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesHandlerFactory.kt @@ -0,0 +1,19 @@ +package com.feakin.intellij.ide.search + +import com.feakin.intellij.psi.FeakinNamedElement +import com.intellij.find.findUsages.FindUsagesHandler +import com.intellij.find.findUsages.FindUsagesHandlerFactory +import com.intellij.psi.PsiElement + +class FkFindUsagesHandlerFactory : FindUsagesHandlerFactory() { + override fun canFindUsages(element: PsiElement): Boolean = element is FeakinNamedElement + + override fun createFindUsagesHandler(element: PsiElement, forHighlightUsages: Boolean): FindUsagesHandler { + val secondaryElements = if (!forHighlightUsages) findSecondaryElements(element) else emptyList() + return FkFindUsagesHandler(element, secondaryElements.toTypedArray()) + } + + private fun findSecondaryElements(element: PsiElement): List { + return emptyList() + } +} diff --git a/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesProvider.kt b/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesProvider.kt new file mode 100644 index 0000000..bf6e82a --- /dev/null +++ b/src/main/kotlin/com/feakin/intellij/ide/search/FkFindUsagesProvider.kt @@ -0,0 +1,21 @@ +package com.feakin.intellij.ide.search + +import com.feakin.intellij.psi.FeakinNamedElement +import com.intellij.lang.HelpID +import com.intellij.lang.findUsages.FindUsagesProvider +import com.intellij.psi.PsiElement + +class FkFindUsagesProvider : FindUsagesProvider { + override fun getWordsScanner() = FkWordScanner() + + override fun canFindUsagesFor(psiElement: PsiElement): Boolean = psiElement is FeakinNamedElement + + override fun getHelpId(psiElement: PsiElement): String = HelpID.FIND_OTHER_USAGES + + override fun getType(element: PsiElement): String = "" + + override fun getDescriptiveName(element: PsiElement): String = (element as? FeakinNamedElement)?.name.orEmpty() + + override fun getNodeText(element: PsiElement, useFullName: Boolean): String = "" + +} \ No newline at end of file diff --git a/src/main/kotlin/com/feakin/intellij/ide/search/FkWordScanner.kt b/src/main/kotlin/com/feakin/intellij/ide/search/FkWordScanner.kt new file mode 100644 index 0000000..89d84cc --- /dev/null +++ b/src/main/kotlin/com/feakin/intellij/ide/search/FkWordScanner.kt @@ -0,0 +1,13 @@ +package com.feakin.intellij.ide.search + +import com.feakin.intellij.lexer.FeakinElementTypes.* +import com.feakin.intellij.lexer.FkLexer +import com.intellij.lang.cacheBuilder.DefaultWordsScanner +import com.intellij.psi.tree.TokenSet + +class FkWordScanner : DefaultWordsScanner( + FkLexer(), + TokenSet.create(IDENTIFIER), + TokenSet.create(BLOCK_COMMENT, COMMENT), + TokenSet.create(STRING_LITERAL) +) diff --git a/src/main/kotlin/com/feakin/intellij/lexer/FeakinLexerAdapter.kt b/src/main/kotlin/com/feakin/intellij/lexer/FeakinLexerAdapter.kt deleted file mode 100644 index 167263e..0000000 --- a/src/main/kotlin/com/feakin/intellij/lexer/FeakinLexerAdapter.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.feakin.intellij.lexer - -import com.intellij.lexer.FlexAdapter - -class FeakinLexerAdapter : FlexAdapter(com.feakin.intellij.lexer._FeakinLexer(null)) \ No newline at end of file diff --git a/src/main/kotlin/com/feakin/intellij/lexer/FkLexer.kt b/src/main/kotlin/com/feakin/intellij/lexer/FkLexer.kt new file mode 100644 index 0000000..29c7839 --- /dev/null +++ b/src/main/kotlin/com/feakin/intellij/lexer/FkLexer.kt @@ -0,0 +1,5 @@ +package com.feakin.intellij.lexer + +import com.intellij.lexer.FlexAdapter + +class FkLexer : FlexAdapter(com.feakin.intellij.lexer._FeakinLexer(null)) \ No newline at end of file diff --git a/src/main/kotlin/com/feakin/intellij/parser/FeakinParserDefinition.kt b/src/main/kotlin/com/feakin/intellij/parser/FeakinParserDefinition.kt index 4f90e5f..2079f99 100644 --- a/src/main/kotlin/com/feakin/intellij/parser/FeakinParserDefinition.kt +++ b/src/main/kotlin/com/feakin/intellij/parser/FeakinParserDefinition.kt @@ -3,7 +3,7 @@ package com.feakin.intellij.parser import com.feakin.intellij.FkFile import com.feakin.intellij.FkLanguage import com.feakin.intellij.lexer.FeakinElementTypes -import com.feakin.intellij.lexer.FeakinLexerAdapter +import com.feakin.intellij.lexer.FkLexer import com.intellij.lang.ASTNode import com.intellij.lang.ParserDefinition import com.intellij.lang.PsiParser @@ -18,13 +18,12 @@ import com.intellij.psi.tree.TokenSet class FeakinParserDefinition : ParserDefinition { companion object { - val WHITE_SPACES = TokenSet.create(TokenType.WHITE_SPACE) val COMMENTS = TokenSet.create(FeakinElementTypes.COMMENT) val FILE = IFileElementType(FkLanguage) } override fun createLexer(project: Project): Lexer { - return FeakinLexerAdapter() + return FkLexer() } override fun createParser(project: Project): PsiParser { diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 7b2f73c..f1bc4f1 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -27,14 +27,15 @@ + + - + + - - @@ -66,6 +67,10 @@ + + + +