generated from JetBrains/intellij-platform-plugin-template
-
-
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.
Browse files
Browse the repository at this point in the history
…fix-Action fix for #20
- Loading branch information
Showing
11 changed files
with
330 additions
and
22 deletions.
There are no files selected for viewing
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
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
70 changes: 70 additions & 0 deletions
70
src/main/kotlin/com/github/dinbtechit/ngxs/action/editor/NgxsActionUtil.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,70 @@ | ||
package com.github.dinbtechit.ngxs.action.editor | ||
|
||
import com.intellij.lang.ecmascript6.psi.impl.ES6FieldStatementImpl | ||
import com.intellij.lang.javascript.psi.JSReferenceExpression | ||
import com.intellij.lang.javascript.psi.ecma6.ES6Decorator | ||
import com.intellij.lang.javascript.types.TypeScriptClassElementType | ||
import com.intellij.lang.javascript.types.TypeScriptNewExpressionElementType | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiWhiteSpace | ||
import com.intellij.psi.search.searches.ReferencesSearch | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.intellij.psi.util.elementType | ||
import com.intellij.psi.util.nextLeafs | ||
|
||
object NgxsActionUtil { | ||
|
||
fun isActionDispatched(element: PsiElement): Boolean { | ||
return (element.parent is JSReferenceExpression | ||
&& element.parent.parent.elementType is TypeScriptNewExpressionElementType | ||
&& element.parent.reference?.resolve() !== null) && | ||
element.parent.reference?.resolve()!!.children.any { | ||
it is ES6FieldStatementImpl && it.text.contains("^static(.*)type".toRegex()) | ||
} | ||
} | ||
|
||
fun isActionClass(element: PsiElement): Boolean { | ||
return element.elementType is TypeScriptClassElementType && element.children.any { | ||
it is ES6FieldStatementImpl && it.text.contains("^static(.*)type".toRegex()) | ||
} | ||
} | ||
|
||
fun isActionImplExist(psiElement: PsiElement): Boolean { | ||
return when { | ||
isActionDispatched(psiElement) -> { | ||
val element2 = psiElement.parent.reference?.resolve()?.navigationElement | ||
this.findActionUsages(element2) | ||
} | ||
|
||
isActionClass(psiElement) -> { | ||
this.findActionUsages(psiElement) | ||
} | ||
|
||
else -> false | ||
} | ||
} | ||
|
||
fun getActionClassPsiElement(element: PsiElement): PsiElement? { | ||
val endIndex = element.firstChild.nextLeafs.indexOfFirst { it.text == "{" } | ||
return element.firstChild.nextLeafs.toList() | ||
.subList(0, if (endIndex < 0) 0 else endIndex) | ||
.firstOrNull { it !is PsiWhiteSpace && it.text != "class" } | ||
} | ||
|
||
fun findActionUsages(element: PsiElement?): Boolean { | ||
|
||
if (element == null) return false | ||
|
||
val refs = ReferencesSearch.search(element).findAll() | ||
for (ref in refs.toList()) { | ||
val actionDecoratorElement = PsiTreeUtil.findFirstParent(ref.element) { it is ES6Decorator } | ||
val hasActionDecorator = actionDecoratorElement != null | ||
if (hasActionDecorator | ||
&& ref.element.containingFile.name.contains(".state.ts") | ||
) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/kotlin/com/github/dinbtechit/ngxs/action/editor/NgxsStatePsiFile.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,85 @@ | ||
package com.github.dinbtechit.ngxs.action.editor | ||
|
||
import com.intellij.lang.javascript.psi.ecma6.impl.TypeScriptFunctionImpl | ||
import com.intellij.lang.javascript.psi.ecmal4.JSAttributeList | ||
import com.intellij.lang.javascript.types.TypeScriptClassElementType | ||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.fileEditor.FileDocumentManager | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.psi.PsiDocumentManager | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiManager | ||
import com.intellij.psi.codeStyle.CodeStyleManager | ||
import com.intellij.psi.util.elementType | ||
import com.intellij.refactoring.suggested.endOffset | ||
import java.util.* | ||
|
||
class NgxsStatePsiFile( | ||
private val ngxsStatePsiFile: VirtualFile, | ||
val project: Project | ||
) { | ||
|
||
fun getTypeFromStateAnnotation(): String? { | ||
val stateClassPsi = getStateClassElement()?.children?.firstOrNull() | ||
if (stateClassPsi !is JSAttributeList) return null | ||
val regex = Regex("<(.*?)>") | ||
val matchResult = regex.find(stateClassPsi.text) | ||
return matchResult?.groups?.get(1)?.value | ||
} | ||
|
||
fun getStateClassElement(): PsiElement? { | ||
return PsiManager.getInstance(project).findFile(ngxsStatePsiFile) | ||
?.children?.firstOrNull { | ||
it.elementType is TypeScriptClassElementType | ||
&& it.children[0] is JSAttributeList | ||
&& it.text.contains("@State") | ||
} | ||
} | ||
|
||
fun createActionMethod(actionPsiElement: PsiElement): PsiElement? { | ||
val stateClassPsi = getStateClassElement() | ||
if (stateClassPsi != null) { | ||
if (stateClassPsi.node.lastChildNode.text == "}") { | ||
val lastFunction = stateClassPsi.children.lastOrNull { it is TypeScriptFunctionImpl } | ||
if (lastFunction != null) { | ||
val elementText = """ | ||
@Action(${actionPsiElement.text}) | ||
${actionPsiElement.text.toCamelCase()}(ctx: StateContext<${getTypeFromStateAnnotation()}>) { | ||
// TODO implement action | ||
} | ||
""".trimIndent() | ||
|
||
val document: Document = FileDocumentManager.getInstance().getDocument(ngxsStatePsiFile) ?: return null | ||
|
||
WriteCommandAction.runWriteCommandAction(project) { | ||
// Check where to insert the new code | ||
val insertOffset: Int = lastFunction.endOffset | ||
// Insert the new code | ||
document.insertString(insertOffset, "\n${elementText}") | ||
PsiDocumentManager.getInstance(project).commitDocument(document) | ||
PsiManager.getInstance(project).findFile(ngxsStatePsiFile)?.let { psiFile -> | ||
val length = psiFile.textLength | ||
val range = TextRange.from(insertOffset, length - insertOffset) | ||
|
||
CodeStyleManager.getInstance(project) | ||
.reformatText(psiFile, range.startOffset, range.endOffset) | ||
} | ||
} | ||
FileDocumentManager.getInstance().saveDocument(document) | ||
return stateClassPsi.children.lastOrNull { it is TypeScriptFunctionImpl } | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
|
||
fun String.toCamelCase(): String = split(" ").joinToString("") { it.replaceFirstChar { | ||
if (it.isLowerCase()) it.titlecase( | ||
Locale.getDefault() | ||
) else it.toString() | ||
} }.replaceFirstChar { it.lowercase(Locale.getDefault()) } | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
src/main/kotlin/com/github/dinbtechit/ngxs/action/editor/codeIntellisense/NgxsAnnotator.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,92 @@ | ||
package com.github.dinbtechit.ngxs.action.editor.codeIntellisense | ||
|
||
import com.github.dinbtechit.ngxs.action.editor.NgxsActionUtil | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.lang.annotation.AnnotationHolder | ||
import com.intellij.lang.annotation.Annotator | ||
import com.intellij.lang.annotation.HighlightSeverity | ||
import com.intellij.lang.javascript.psi.ecmal4.JSAttributeList | ||
import com.intellij.lang.javascript.types.TypeScriptClassElementType | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.openapi.vfs.LocalFileSystem | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiManager | ||
import com.intellij.psi.util.elementType | ||
import com.intellij.refactoring.suggested.endOffset | ||
import com.intellij.refactoring.suggested.startOffset | ||
|
||
|
||
class NgxsAnnotator : Annotator { | ||
|
||
override fun annotate(element: PsiElement, holder: AnnotationHolder) { | ||
var isImplementationExist = true | ||
var problemType = ProblemHighlightType.GENERIC_ERROR_OR_WARNING | ||
var range = TextRange(element.textRange.startOffset, element.textRange.endOffset) | ||
var actionPsiClass: PsiElement? = null | ||
var actionName: String? = null | ||
var actionFileName: String? = null | ||
var actionVirtualFile: VirtualFile? = null | ||
|
||
if (NgxsActionUtil.isActionClass(element)) { | ||
isImplementationExist = NgxsActionUtil.isActionImplExist(element) | ||
problemType = ProblemHighlightType.LIKE_UNUSED_SYMBOL | ||
try { | ||
val classNamePsiElement = NgxsActionUtil.getActionClassPsiElement(element) | ||
if (classNamePsiElement != null) { | ||
range = TextRange(classNamePsiElement.startOffset, classNamePsiElement.endOffset) | ||
actionPsiClass = classNamePsiElement | ||
actionName = classNamePsiElement.text | ||
actionFileName = classNamePsiElement.containingFile.name | ||
actionVirtualFile = classNamePsiElement.containingFile.containingDirectory.virtualFile | ||
} | ||
} catch (e: Exception) { | ||
throw Exception("NgxsAnnotator - Unable to establish range for the ActionClass - ${element.text}") | ||
} | ||
|
||
} else if (NgxsActionUtil.isActionDispatched(element)) { | ||
isImplementationExist = NgxsActionUtil.isActionImplExist(element) | ||
val refElement = element.parent.reference?.resolve() | ||
if (refElement != null) { | ||
val classNamePsiElement = NgxsActionUtil.getActionClassPsiElement(refElement) | ||
if (classNamePsiElement != null) { | ||
actionName = classNamePsiElement.text | ||
actionPsiClass = classNamePsiElement | ||
actionFileName = refElement.containingFile.name | ||
actionVirtualFile = classNamePsiElement.containingFile.containingDirectory.virtualFile | ||
} | ||
} | ||
} | ||
|
||
if (!isImplementationExist ) { | ||
val stateFileName = if (actionFileName != null) | ||
"${actionFileName.split(".")[0]}.state.ts" | ||
else "*.state.ts" | ||
val stateFile = LocalFileSystem.getInstance().findFileByPath("${actionVirtualFile?.path}/$stateFileName") | ||
if (stateFile != null) { | ||
val stateClassPsi = PsiManager.getInstance(element.project).findFile(stateFile)?.children?.firstOrNull { | ||
it.elementType is TypeScriptClassElementType | ||
&& it.children[0] is JSAttributeList | ||
&& it.text.contains("@State") | ||
} | ||
if (stateClassPsi != null) { | ||
if (stateClassPsi.node.lastChildNode.text == "}") { | ||
stateClassPsi.node.lastChildNode | ||
} | ||
|
||
} | ||
} | ||
|
||
if (actionName == null) actionName = element.text | ||
holder.newAnnotation(HighlightSeverity.WARNING, "@Action(${actionName}) not found in $stateFileName") | ||
.range(range) | ||
.highlightType(problemType) | ||
.withFix( | ||
NgxsCreateActionQuickFix("Create @Action(${actionName}) in $stateFileName.", | ||
actionPsiClass!!, stateFile)) | ||
.create() | ||
} | ||
|
||
} | ||
} | ||
|
Oops, something went wrong.