-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
57 deletions.
There are no files selected for viewing
52 changes: 1 addition & 51 deletions
52
src/main/java/com/example/ide/css/CssModulesClassNameCompletionContributor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,11 @@ | ||
package com.example.ide.css | ||
|
||
import com.example.ide.message.QCssMessageBundle | ||
import com.intellij.codeInsight.completion.* | ||
import com.intellij.codeInsight.lookup.LookupElement | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder | ||
import com.intellij.icons.AllIcons | ||
import com.intellij.lang.javascript.psi.JSLiteralExpression | ||
import com.intellij.patterns.PlatformPatterns | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.css.StylesheetFile | ||
import com.intellij.util.ProcessingContext | ||
|
||
class CssModulesClassNameCompletionContributor : CompletionContributor() { | ||
|
||
private val projectName = QCssMessageBundle.message("projectName") | ||
|
||
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) } } | ||
} | ||
|
||
|
||
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])) | ||
} | ||
} | ||
} | ||
} | ||
val provider = object : CompletionProvider<CompletionParameters>() { | ||
override fun addCompletions( | ||
parameters: CompletionParameters, | ||
context: ProcessingContext, | ||
resultSet: CompletionResultSet | ||
) { | ||
val completionElement = parameters.originalPosition ?: parameters.position | ||
if (completionElement.parent is JSLiteralExpression) { | ||
val literalExpression = completionElement.parent as JSLiteralExpression | ||
QCssModulesUtil.getCssClassNamesImportOrRequireDeclaration(literalExpression)?.let { declaration -> | ||
QCssModulesUtil.resolveStyleSheetFile(declaration)?.let { styleSheetFile -> | ||
completionHelper(resultSet, styleSheetFile) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
extend(CompletionType.BASIC, PlatformPatterns.psiElement(), provider) | ||
extend(CompletionType.BASIC, PlatformPatterns.psiElement(), CssModulesClassNameCompletionContributorImpl()) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/example/ide/css/CssModulesClassNameCompletionContributorImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.example.ide.css | ||
|
||
import com.example.ide.message.QCssMessageBundle | ||
import com.intellij.codeInsight.completion.CompletionParameters | ||
import com.intellij.codeInsight.completion.CompletionProvider | ||
import com.intellij.codeInsight.completion.CompletionResultSet | ||
import com.intellij.codeInsight.lookup.LookupElement | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder | ||
import com.intellij.icons.AllIcons | ||
import com.intellij.lang.javascript.psi.JSLiteralExpression | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.css.StylesheetFile | ||
import com.intellij.util.ProcessingContext | ||
|
||
class CssModulesClassNameCompletionContributorImpl: CompletionProvider<CompletionParameters>() { | ||
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) } } | ||
} | ||
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])) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun addCompletions( | ||
parameters: CompletionParameters, | ||
context: ProcessingContext, | ||
resultSet: CompletionResultSet | ||
) { | ||
val completionElement = parameters.originalPosition ?: parameters.position | ||
if (completionElement.parent is JSLiteralExpression) { | ||
val literalExpression = completionElement.parent as JSLiteralExpression | ||
QCssModulesUtil.getCssClassNamesImportOrRequireDeclaration(literalExpression)?.let { declaration -> | ||
QCssModulesUtil.resolveStyleSheetFile(declaration)?.let { styleSheetFile -> | ||
completionHelper(resultSet, styleSheetFile) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters