diff --git a/src/main/java/com/example/ide/css/CssModulesClassNameCompletionContributor.kt b/src/main/java/com/example/ide/css/CssModulesClassNameCompletionContributor.kt index cd407dd..a4d863b 100644 --- a/src/main/java/com/example/ide/css/CssModulesClassNameCompletionContributor.kt +++ b/src/main/java/com/example/ide/css/CssModulesClassNameCompletionContributor.kt @@ -11,38 +11,34 @@ import com.intellij.psi.PsiElement import com.intellij.psi.css.StylesheetFile import com.intellij.util.ProcessingContext -internal class CssModulesClassNameCompletionContributor : CompletionContributor() { +class CssModulesClassNameCompletionContributor : CompletionContributor() { private val projectName = QCssMessageBundle.message("projectName") - private fun buildLookupElement(name: String, desc: String, psiElement: PsiElement?): LookupElement { - return LookupElementBuilder.create(name) - .withTailText(desc) - .withIcon(AllIcons.Xml.Css_class) - .bold() - .withCaseSensitivity(true) - .apply { psiElement?.let { withPsiElement(it) } } - } + init { + fun buildLookupElement(name: String, desc: String, psiElement: PsiElement?): LookupElement { + return LookupElementBuilder.create(name) + .withTailText(desc) + .withIcon(AllIcons.Xml.Css_class) + .bold() + .withCaseSensitivity(true) + .apply { psiElement?.let { withPsiElement(it) } } + } - private fun completionHelper(resultSet: CompletionResultSet, stylesheetFile: StylesheetFile) { - stylesheetFile.parent?.let { parent -> - val folderName = parent.name - val fileName = stylesheetFile.name - val psiElementRefHashMap = QCssModuleParseUtil.parseCssSelectorFormFile(stylesheetFile) - psiElementRefHashMap.forEach { (name, cssSelectors) -> - if (cssSelectors.isNotEmpty()) { - val desc = " ($folderName/$fileName:${cssSelectors[0].lineNumber})_by_$projectName" - resultSet.addElement(buildLookupElement(name, desc, cssSelectors[0])) + + fun completionHelper(resultSet: CompletionResultSet, stylesheetFile: StylesheetFile) { + stylesheetFile.parent?.let { parent -> + val folderName = parent.name + val fileName = stylesheetFile.name + val psiElementRefHashMap = QCssModuleParseUtil.parseCssSelectorFormFile(stylesheetFile) + psiElementRefHashMap.forEach { (name, cssSelectors) -> + if (cssSelectors.isNotEmpty()) { + val desc = " ($folderName/$fileName:${cssSelectors[0].lineNumber})_by_$projectName" + resultSet.addElement(buildLookupElement(name, desc, cssSelectors[0])) + } } } } - } - - private fun addCompletions(resultSet: CompletionResultSet, stylesheetFile: StylesheetFile) { - completionHelper(resultSet, stylesheetFile) - } - - init { val provider = object : CompletionProvider() { override fun addCompletions( parameters: CompletionParameters, @@ -54,7 +50,7 @@ internal class CssModulesClassNameCompletionContributor : CompletionContributor( val literalExpression = completionElement.parent as JSLiteralExpression QCssModulesUtil.getCssClassNamesImportOrRequireDeclaration(literalExpression)?.let { declaration -> QCssModulesUtil.resolveStyleSheetFile(declaration)?.let { styleSheetFile -> - addCompletions(resultSet, styleSheetFile) + completionHelper(resultSet, styleSheetFile) } } } diff --git a/src/main/java/com/example/ide/css/QCssModuleParseUtil.kt b/src/main/java/com/example/ide/css/QCssModuleParseUtil.kt index 1ea8396..2baf5cc 100644 --- a/src/main/java/com/example/ide/css/QCssModuleParseUtil.kt +++ b/src/main/java/com/example/ide/css/QCssModuleParseUtil.kt @@ -97,6 +97,7 @@ internal object QCssModuleParseUtil { * 解析样式表文件, 此方法对外暴露, 外部调用的时候不需要关心是css还是scss还是less * @param stylesheetFile 样式表文件 */ + @JvmStatic fun parseCssSelectorFormFile(stylesheetFile: StylesheetFile?): MutableMap> { if (stylesheetFile == null || PsiTreeUtil.hasErrorElements(stylesheetFile)) return mutableMapOf() if (contentMap.containsKey(stylesheetFile.text.length.toString())) return contentMap[stylesheetFile.text.length.toString()] ?: mutableMapOf() diff --git a/src/main/java/com/example/ide/css/QCssModulesUtil.kt b/src/main/java/com/example/ide/css/QCssModulesUtil.kt index 79b21bd..7c4394a 100644 --- a/src/main/java/com/example/ide/css/QCssModulesUtil.kt +++ b/src/main/java/com/example/ide/css/QCssModulesUtil.kt @@ -24,6 +24,7 @@ internal object QCssModulesUtil { * @param classNameLiteral CSS 类名的字面量表达式 * @return 对应的 PsiElement 对象,如果不存在则返回 null */ + @JvmStatic fun getCssClassNamesImportOrRequireDeclaration(classNameLiteral: JSLiteralExpression): PsiElement? { val expression = PsiTreeUtil.getParentOfType(classNameLiteral, JSIndexedPropertyAccessExpression::class.java) if (expression?.qualifier?.reference?.resolve() != null) { @@ -39,6 +40,7 @@ internal object QCssModulesUtil { * @param cssFileNameLiteralParent CSS 文件名的字面量表达式的父元素 * @return 对应的 StylesheetFile 对象,如果不存在则返回 null */ + @JvmStatic fun resolveStyleSheetFile(cssFileNameLiteralParent: PsiElement): StylesheetFile? { val stylesheetFileRef = Ref() cssFileNameLiteralParent.accept(object : PsiRecursiveElementVisitor() { @@ -97,6 +99,7 @@ internal object QCssModulesUtil { * @param rs 用于存储解析后的 StylesheetFile 的引用 * @return 对应的 CssSelector 对象,如果不存在则返回 null */ + @JvmStatic fun getCssClass(element: PsiElement, cssClass: String, rs: Ref): PsiElement? { val styleSheetFile = resolveStyleSheetFile(element) if (styleSheetFile == null) { @@ -125,6 +128,7 @@ internal object QCssModulesUtil { * @param sr 用于存储解析后的 StylesheetFile 的引用 * @return 如果解析成功则返回 true,否则返回 false */ + @JvmStatic fun resolveStyleSheetFile(element: PsiElement, sr: Ref): Boolean { element.references.find { it.resolve() is StylesheetFile }?.resolve()?.let { sr.set(it as StylesheetFile) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index ebd276e..daf6df6 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -16,13 +16,6 @@
  • quick fix css selector not exist
  • -

    -

    will support

    -
      -
    • quick documentation when mouse hover
    • -
    -

    - ]]>