Skip to content

Commit

Permalink
Merge pull request #23 from dinbtechit/feature/#20-CodeInsights-Quick…
Browse files Browse the repository at this point in the history
…fix-Action

fix #20
  • Loading branch information
dinbtechit authored Sep 19, 2023
2 parents be8aa67 + 3c0417a commit b589819
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# ngxs Changelog

## [Unreleased]
### Added
- #20 - Code insights/Quickfix when an Action has no implementation (create with Payload) - Part 2


## [0.0.4] - 2023-09-18

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
<!-- Plugin description -->
NGXS is a state management library for Angular. This plugin provides NGXS CLI/Schematics for Jetbrains IDE.

> Please ensure you have [ngxs cli](https://www.ngxs.io/plugins/cli) installed either globally or at the project level.
# Features
- Simply right click -> New -> NGXS CLI/Schematics to generate a boiler plate store.
- Navigate to Action Implementation using Gutter Icons
- Many more coming soon. Checkout roadmap
> Please ensure you have [ngxs cli](https://www.ngxs.io/plugins/cli) installed either globally or at the project level.

# Roadmap

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.dinbtechit.ngxs
pluginName = ngxs
pluginRepositoryUrl = https://github.com/dinbtechit/ngxs
# SemVer format -> https://semver.org
pluginVersion = 0.0.4
pluginVersion = 0.0.5

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 223
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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.lang.typescript.psi.TypeScriptPsiUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.search.searches.ReferencesSearch
Expand All @@ -29,6 +30,11 @@ object NgxsActionUtil {
}
}

fun hasPayload(element: PsiElement): Boolean {
return TypeScriptPsiUtil.getParentClass(element)?.constructor
?.parameterList?.children?.isNotEmpty() == true
}

fun isActionImplExist(psiElement: PsiElement): Boolean {
return when {
isActionDispatched(psiElement) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,31 @@ class NgxsStatePsiFile(
if (stateClassPsi.node.lastChildNode.text == "}") {
val lastFunction = stateClassPsi.children.lastOrNull { it is TypeScriptFunctionImpl }
if (lastFunction != null) {
val elementText = """
val actionMethod = """
@Action(${actionPsiElement.text})
${actionPsiElement.text.toCamelCase()}(ctx: StateContext<${getTypeFromStateAnnotation()}>) {
// TODO implement action
}
""".trimIndent()

val actionMethodWithPayload = """
@Action(${actionPsiElement.text})
${actionPsiElement.text.toCamelCase()}(ctx: StateContext<${getTypeFromStateAnnotation()}>, payload: ${actionPsiElement.text}) {
// 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}")
if(NgxsActionUtil.hasPayload(actionPsiElement)) {
document.insertString(insertOffset, "\n${actionMethodWithPayload}")
} else {
document.insertString(insertOffset, "\n${actionMethod}")
}
PsiDocumentManager.getInstance(project).commitDocument(document)
PsiManager.getInstance(project).findFile(ngxsStatePsiFile)?.let { psiFile ->
val length = psiFile.textLength
Expand All @@ -76,7 +87,7 @@ class NgxsStatePsiFile(
return null
}

fun String.toCamelCase(): String = split(" ").joinToString("") { it.replaceFirstChar {
private fun String.toCamelCase(): String = split(" ").joinToString("") { it.replaceFirstChar {
if (it.isLowerCase()) it.titlecase(

Check notice on line 91 in src/main/kotlin/com/github/dinbtechit/ngxs/action/editor/NgxsStatePsiFile.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nested lambda has shadowed implicit parameter

Implicit parameter 'it' of enclosing lambda is shadowed

Check notice on line 91 in src/main/kotlin/com/github/dinbtechit/ngxs/action/editor/NgxsStatePsiFile.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nested lambda has shadowed implicit parameter

Implicit parameter 'it' of enclosing lambda is shadowed
Locale.getDefault()
) else it.toString()

Check notice on line 93 in src/main/kotlin/com/github/dinbtechit/ngxs/action/editor/NgxsStatePsiFile.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nested lambda has shadowed implicit parameter

Implicit parameter 'it' of enclosing lambda is shadowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.dinbtechit.ngxs.action.editor.codeIntellisense

import com.github.dinbtechit.ngxs.action.editor.NgxsStatePsiFile
import com.intellij.codeInsight.intention.impl.BaseIntentionAction
import com.intellij.lang.javascript.psi.ecma6.TypeScriptFunction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.fileEditor.FileEditorManager
Expand Down Expand Up @@ -37,9 +38,12 @@ class NgxsCreateActionQuickFix(private val key: String,
ngxsStateVirtualFile
), true
)

val start = actionFunction?.textRange?.startOffset ?: 0
if (actionFunction == null || actionFunction !is TypeScriptFunction) return
val functionBody = actionFunction.block?.firstChild?.nextSibling?.nextSibling
val start = functionBody?.textRange?.startOffset ?: 0
val end = functionBody?.textRange?.endOffset ?: 0
textEditor?.caretModel?.moveToOffset(start)
textEditor?.selectionModel?.setSelection(start, end)
textEditor?.scrollingModel?.scrollToCaret(ScrollType.CENTER)

}
Expand Down

0 comments on commit b589819

Please sign in to comment.