Skip to content

Commit

Permalink
feat(core): fix invoke static ?
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-Peppa committed Sep 22, 2024
1 parent 22352c9 commit 6caf793
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompletionParameters>() {
override fun addCompletions(
parameters: CompletionParameters,
Expand All @@ -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)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/example/ide/css/QCssModuleParseUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ internal object QCssModuleParseUtil {
* 解析样式表文件, 此方法对外暴露, 外部调用的时候不需要关心是css还是scss还是less
* @param stylesheetFile 样式表文件
*/
@JvmStatic
fun parseCssSelectorFormFile(stylesheetFile: StylesheetFile?): MutableMap<String, Array<CssSelector>> {
if (stylesheetFile == null || PsiTreeUtil.hasErrorElements(stylesheetFile)) return mutableMapOf()
if (contentMap.containsKey(stylesheetFile.text.length.toString())) return contentMap[stylesheetFile.text.length.toString()] ?: mutableMapOf()
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/example/ide/css/QCssModulesUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -39,6 +40,7 @@ internal object QCssModulesUtil {
* @param cssFileNameLiteralParent CSS 文件名的字面量表达式的父元素
* @return 对应的 StylesheetFile 对象,如果不存在则返回 null
*/
@JvmStatic
fun resolveStyleSheetFile(cssFileNameLiteralParent: PsiElement): StylesheetFile? {
val stylesheetFileRef = Ref<StylesheetFile>()
cssFileNameLiteralParent.accept(object : PsiRecursiveElementVisitor() {
Expand Down Expand Up @@ -97,6 +99,7 @@ internal object QCssModulesUtil {
* @param rs 用于存储解析后的 StylesheetFile 的引用
* @return 对应的 CssSelector 对象,如果不存在则返回 null
*/
@JvmStatic
fun getCssClass(element: PsiElement, cssClass: String, rs: Ref<StylesheetFile>): PsiElement? {
val styleSheetFile = resolveStyleSheetFile(element)
if (styleSheetFile == null) {
Expand Down Expand Up @@ -125,6 +128,7 @@ internal object QCssModulesUtil {
* @param sr 用于存储解析后的 StylesheetFile 的引用
* @return 如果解析成功则返回 true,否则返回 false
*/
@JvmStatic
fun resolveStyleSheetFile(element: PsiElement, sr: Ref<StylesheetFile>): Boolean {
element.references.find { it.resolve() is StylesheetFile }?.resolve()?.let {
sr.set(it as StylesheetFile)
Expand Down
7 changes: 0 additions & 7 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@
<li>quick fix css selector not exist</li>
</ul>
</h3>
<h4>
<p>will support</p>
<ul>
<li>quick documentation when mouse hover</li>
</ul>
</h4>
]]></description>

<change-notes><![CDATA[
Expand Down

0 comments on commit 6caf793

Please sign in to comment.