Skip to content

Commit 8165ceb

Browse files
committed
feat: 规范化文件位置
1. 去除message的依赖 2. 把css包名修改为completion 3. 修改插件的描述
1 parent 6dc96bd commit 8165ceb

15 files changed

Lines changed: 82 additions & 83 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
## [1.2.1] - 2025-04-11
6+
7+
- fix: fix some error completion not in JSIndexedPropertyAccessExpression
8+
59
## [1.2.0] - 2025-04-11
610

711
- feat: now support completion with dot and auto wrap by single quote

build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
id("java") // Java support
66
alias(libs.plugins.kotlin) // Kotlin support
77
alias(libs.plugins.changelog) // Gradle Changelog Plugin
8-
alias(libs.plugins.intelliJPlatform) // IntelliJ Platform Gradle Plugin
8+
alias(libs.plugins.intelliJPlatform) // IntelliJ Platform Gradle Plugin'
99
}
1010

1111

@@ -31,7 +31,6 @@ dependencies {
3131
intellijPlatform {
3232
webstorm("2024.2")
3333
bundledPlugin("JavaScript")
34-
instrumentationTools()
3534
testFramework(TestFrameworkType.Platform)
3635
}
3736
}

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pluginGroup = com.example.css
44
pluginName = React Css Modules All
55
pluginRepositoryUrl = https://github.com/Q-Peppa/react-css-modules-all
66
# SemVer format -> https://semver.org
7-
pluginVersion=1.2.0
8-
version=1.2.0
7+
pluginVersion=1.2.1
8+
version=1.2.1
99

1010

1111
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html

src/main/kotlin/com/example/ide/annotator/CssModulesClassAnnotator.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.example.ide.annotator
22

3-
import com.example.ide.message.QCssMessageBundle
43
import com.example.ide.psi.CssModulesUnknownClassPsiReference
54
import com.example.ide.psi.isStyleIndex
65
import com.intellij.lang.annotation.AnnotationHolder
@@ -11,12 +10,15 @@ import com.intellij.psi.PsiElement
1110
import com.intellij.psi.css.CssRuleset
1211
import org.jetbrains.annotations.NotNull
1312

13+
14+
const val MESSAGE = "Selector declarations is Empty"
15+
const val UNKNOWN = "Unknown class name"
1416
class CssModulesClassAnnotator : Annotator {
1517
private fun resolveUnknownClass(holder: AnnotationHolder, psiElement: JSLiteralExpression) {
1618
val cssSelectorName = psiElement.stringValue?.trim().orEmpty()
1719
val reference = psiElement.reference
1820
if (reference is CssModulesUnknownClassPsiReference) {
19-
val message = "${QCssMessageBundle.message("UnknownClassName")} \"$cssSelectorName\""
21+
val message = "$UNKNOWN \"$cssSelectorName\""
2022
holder.newAnnotation(HighlightSeverity.WARNING, message)
2123
.range(psiElement)
2224
.withFix(SimpleCssSelectorFix(cssSelectorName, reference.stylesheetFile))
@@ -29,8 +31,7 @@ class CssModulesClassAnnotator : Annotator {
2931
if (ruleset is CssRuleset) {
3032
val declarations = ruleset.block?.declarations
3133
if (declarations.isNullOrEmpty()) {
32-
val message = QCssMessageBundle.message("EmptyClass")
33-
holder.newAnnotation(HighlightSeverity.WEAK_WARNING, message)
34+
holder.newAnnotation(HighlightSeverity.WEAK_WARNING, MESSAGE)
3435
.range(psiElement)
3536
.create()
3637
}

src/main/kotlin/com/example/ide/annotator/SimpleCssSelectorFix.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.example.ide.annotator
22

3-
import com.example.ide.message.QCssMessageBundle
43
import com.intellij.codeInsight.intention.impl.BaseIntentionAction
54
import com.intellij.openapi.editor.Editor
65
import com.intellij.openapi.fileEditor.FileEditorManager
@@ -11,14 +10,16 @@ import com.intellij.psi.css.CssElementFactory
1110
import com.intellij.psi.css.StylesheetFile
1211
import org.jetbrains.annotations.NotNull
1312

13+
const val FAMILY_NAME = "Unknown class name"
14+
1415
class SimpleCssSelectorFix(private val key: String, private val stylesheetFile: StylesheetFile) :
1516
BaseIntentionAction() {
1617

1718
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean = true
1819

1920
override fun getText(): String = "$familyName .$key"
2021

21-
override fun getFamilyName(): String = QCssMessageBundle.message("familyName")
22+
override fun getFamilyName(): String = FAMILY_NAME
2223

2324
override fun invoke(@NotNull project: Project, editor: Editor?, file: PsiFile?) {
2425
if (editor == null || file == null) return
@@ -34,6 +35,5 @@ class SimpleCssSelectorFix(private val key: String, private val stylesheetFile:
3435
it.editor.caretModel.moveToOffset(offset)
3536
}
3637
}
37-
// file.subtreeChanged()
3838
}
3939
}

src/main/kotlin/com/example/ide/css/CssModulesClassNameCompletionContributor.kt renamed to src/main/kotlin/com/example/ide/completion/CssModulesClassNameCompletionContributor.kt

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
package com.example.ide.css
1+
package com.example.ide.completion
22

33
import com.intellij.codeInsight.completion.*
4+
import com.intellij.codeInsight.lookup.LookupElementBuilder
45
import com.intellij.lang.ecmascript6.psi.ES6ImportedBinding
56
import com.intellij.lang.javascript.JavascriptLanguage
6-
import com.intellij.lang.javascript.psi.JSIndexedPropertyAccessExpression
77
import com.intellij.lang.javascript.psi.JSLiteralExpression
88
import com.intellij.lang.javascript.psi.JSReferenceExpression
99
import com.intellij.patterns.PlatformPatterns
1010
import com.intellij.psi.PsiElement
11+
import com.intellij.psi.css.CssSelectorSuffix
1112
import com.intellij.psi.css.StylesheetFile
13+
import com.intellij.psi.css.impl.stubs.index.CssClassIndex
14+
import com.intellij.psi.css.impl.util.CssUtil
15+
import com.intellij.psi.stubs.StubIndex
1216
import com.intellij.util.ProcessingContext
1317

1418
const val SplitChar = "-"
@@ -17,17 +21,17 @@ const val DotChar = "."
1721
class CssModulesClassNameCompletionContributor : CompletionContributor() {
1822

1923
init {
20-
extend(
21-
CompletionType.BASIC,
22-
PlatformPatterns
23-
.psiElement()
24-
.withLanguage(JavascriptLanguage.INSTANCE)
25-
// must in '' or ""
26-
.withParent(JSLiteralExpression::class.java)
27-
// styles["xxx"]
28-
.withSuperParent(2, JSIndexedPropertyAccessExpression::class.java),
29-
CssModulesClassNameCompletionContributorProvider()
30-
)
24+
// extend(
25+
// CompletionType.BASIC,
26+
// PlatformPatterns
27+
// .psiElement()
28+
// .withLanguage(JavascriptLanguage.INSTANCE)
29+
// // must in '' or ""
30+
// .withParent(JSLiteralExpression::class.java)
31+
// // styles["xxx"]
32+
// .withSuperParent(2, JSIndexedPropertyAccessExpression::class.java),
33+
// CssModulesClassNameCompletionContributorProvider()
34+
// )
3135
extend(
3236
CompletionType.BASIC,
3337
PlatformPatterns
@@ -59,26 +63,41 @@ class CssModulesClassNameCompletionContributor : CompletionContributor() {
5963
context: ProcessingContext,
6064
resultSet: CompletionResultSet
6165
) {
62-
66+
val time = System.nanoTime();
6367
val position = parameters.position
68+
val project = position.project
69+
val originalFile = parameters.originalFile
70+
val scope = CssUtil.getCompletionAndResolvingScopeForElement(position);
71+
val validSet = mutableMapOf<String, Boolean>()
6472
if (position.prevSibling is PsiElement
6573
&& position.prevSibling.text == DotChar
6674
&& position.prevSibling.prevSibling is JSReferenceExpression
6775
) {
6876
val style = position.prevSibling.prevSibling
69-
style.reference?.resolve()?.let {
70-
if (it !is ES6ImportedBinding || it.findReferencedElements().isEmpty()) return
71-
val first = it.findReferencedElements().first()
72-
first.let {
73-
resultSet.addAllElements(generateLookupElementList(it as StylesheetFile, true).map {
74-
// if choose completion with - , auto make to IndexedAccess
75-
it.withInsertHandler { context, item ->
76-
StylesInsertHandler(item.lookupString.contains(SplitChar)).handleInsert(context, item)
77-
}
78-
})
77+
val styleFile = style.reference?.resolve() as ES6ImportedBinding
78+
val file = styleFile.findReferencedElements().first() as StylesheetFile
79+
val importedFiles =
80+
CssUtil.getImportedFiles(file, file.lastChild, false)
81+
println(importedFiles.map { it.name })
82+
for (name in StubIndex.getInstance().getAllKeys(CssClassIndex.KEY, project)) {
83+
if (validSet[name] == false) continue
84+
StubIndex.getInstance().processElements(
85+
CssClassIndex.KEY,
86+
name,
87+
project,
88+
scope,
89+
CssSelectorSuffix::class.java
90+
) {
91+
val valid = importedFiles.contains(it.containingFile.virtualFile)
92+
if (valid) {
93+
resultSet.addElement(LookupElementBuilder.create(name))
94+
}
95+
true
7996
}
8097
}
8198
}
99+
val end = System.nanoTime();
100+
println("time is ${(end - time)} /ns ")
82101
}
83102
}
84103
}

src/main/kotlin/com/example/ide/css/QCssModulesUtil.kt renamed to src/main/kotlin/com/example/ide/completion/QCssModulesUtil.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.ide.css
1+
package com.example.ide.completion
22

33
import com.intellij.codeInsight.completion.PrioritizedLookupElement
44
import com.intellij.codeInsight.lookup.LookupElementBuilder
@@ -35,6 +35,7 @@ private fun recessivesClassInCssSelector(
3535
fun restoreAllSelector(stylesheetFile: StylesheetFile): MutableMap<String, PsiElement> {
3636
val of = mutableMapOf<String, PsiElement>()
3737
val scope = GlobalSearchScope.fileScope(stylesheetFile.project, stylesheetFile.virtualFile)
38+
3839
CssIndexUtil.processAmpersandSelectors(stylesheetFile.project, scope) {
3940
// realSelector in file , afterResolve is dummy file, can't find resolve and lineNumber
4041
val realSelector = it
@@ -79,6 +80,7 @@ fun buildLookupElementHelper(
7980
* foo["$1"] , $1 position is innerStringIndexPsiElement , the type should be JSLiteralExpression
8081
*/
8182
fun findReferenceStyleFile(innerStringIndexPsiElement: JSLiteralExpression?): StylesheetFile? {
83+
8284
if (innerStringIndexPsiElement == null) return null
8385
val callKey = innerStringIndexPsiElement.parent?.firstChild // by style["$1"] get styles
8486
if (callKey !is JSReferenceExpression) return null

src/main/kotlin/com/example/ide/css/StylesInsertHandler.kt renamed to src/main/kotlin/com/example/ide/completion/StylesInsertHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.ide.css
1+
package com.example.ide.completion
22

33
import com.intellij.codeInsight.completion.InsertHandler
44
import com.intellij.codeInsight.completion.InsertionContext

src/main/kotlin/com/example/ide/document/SimpleDocumentationProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SimpleDocumentationProvider : AbstractDocumentationProvider() {
2020
private fun renderDoc(cssRuleset: CssRuleset): String {
2121
val text = cssRuleset.text.trimIndent().replaceLast("}", "").trim() + "\n}"
2222
return StringBuilder()
23-
.appendStyledCodeBlock(cssRuleset.project, CSSLanguage.INSTANCE, text)
23+
.appendStyledCodeBlock(cssRuleset.project, CSSLanguage.INSTANCE, code = text)
2424
.toString()
2525
}
2626

src/main/kotlin/com/example/ide/message/QCssMessageBundle.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)