-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix "Create procedure" placement of script
- Loading branch information
1 parent
370ac1a
commit e5d0c69
Showing
5 changed files
with
82 additions
and
95 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
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/io/runescript/plugin/ide/inspections/RsCreateScriptQuickFix.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,50 @@ | ||
package io.runescript.plugin.ide.inspections | ||
|
||
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo | ||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.openapi.editor.ScrollType | ||
import com.intellij.openapi.fileEditor.FileEditorManager | ||
import com.intellij.openapi.fileEditor.OpenFileDescriptor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiDocumentManager | ||
import com.intellij.psi.util.findParentOfType | ||
import io.runescript.plugin.lang.psi.RsElementGenerator | ||
import io.runescript.plugin.lang.psi.RsScript | ||
|
||
class RsCreateScriptQuickFix(private val trigger: String, private val functionName: String) : LocalQuickFix { | ||
|
||
override fun getName(): String { | ||
return "Create script ('${functionName}')" | ||
} | ||
|
||
override fun getFamilyName(): String { | ||
return "Create script" | ||
} | ||
|
||
override fun generatePreview(project: Project, previewDescriptor: ProblemDescriptor): IntentionPreviewInfo = | ||
IntentionPreviewInfo.EMPTY | ||
|
||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { | ||
val newScript = RsScriptBuilder(trigger, functionName) | ||
.statement("error(\"Not yet implemented\");") | ||
.build(project) | ||
val parentScript = descriptor.psiElement.findParentOfType<RsScript>()!! | ||
val parentFile = parentScript.parent | ||
parentFile.addAfter(newScript, parentScript) | ||
parentFile.addAfter(RsElementGenerator.createNewLine(project), parentScript) | ||
openAndSelect(project, newScript) | ||
} | ||
|
||
private fun openAndSelect(project: Project, script: RsScript) { | ||
val containingFile = script.containingFile.virtualFile ?: return | ||
val openDescriptor = OpenFileDescriptor(project, containingFile) | ||
val textEditor = FileEditorManager.getInstance(project).openTextEditor(openDescriptor, true) ?: return | ||
textEditor.selectionModel.removeSelection() | ||
val endOffset = script.textRange.endOffset | ||
textEditor.caretModel.moveToOffset(endOffset + 1) | ||
val manager = PsiDocumentManager.getInstance(project) | ||
manager.doPostponedOperationsAndUnblockDocument(textEditor.document) | ||
textEditor.scrollingModel.scrollToCaret(ScrollType.RELATIVE) | ||
} | ||
} |
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