generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(code-completion): refactor code completion task and add InsertUtil
#29 Refactored the CodeCompletionTask to improve code readability and maintainability. Added a new utility class, InsertUtil, to handle text insertion and replacement in the document. Also, made minor changes to SelectElementStrategy and LocationInteractionContext to support the refactored code completion task.
- Loading branch information
Showing
5 changed files
with
95 additions
and
21 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
58 changes: 58 additions & 0 deletions
58
core/src/main/kotlin/com/phodal/shirecore/provider/impl/InsertUtil.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,58 @@ | ||
package com.phodal.shirecore.provider.impl | ||
|
||
import com.intellij.openapi.application.runReadAction | ||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.editor.ScrollType | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.psi.PsiDocumentManager | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.codeStyle.CodeStyleManager | ||
import com.phodal.shirecore.ShireCoreBundle | ||
|
||
object InsertUtil { | ||
fun insertStringAndSaveChange( | ||
project: Project, | ||
content: String, | ||
document: Document, | ||
startOffset: Int, | ||
withReformat: Boolean, | ||
) { | ||
document.insertString(startOffset, content) | ||
PsiDocumentManager.getInstance(project).commitDocument(document) | ||
|
||
if (!withReformat) return | ||
|
||
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document) | ||
psiFile?.let { file -> | ||
val reformatRange = TextRange(startOffset, startOffset + content.length) | ||
CodeStyleManager.getInstance(project).reformatText(file, listOf(reformatRange)) | ||
} | ||
} | ||
|
||
fun insertStreamingToDoc(project: Project, char: String, editor: Editor, currentOffset: Int) { | ||
WriteCommandAction.runWriteCommandAction( | ||
project, | ||
ShireCoreBundle.message("intentions.chat.code.complete.name"), | ||
"intentions.write.action", | ||
{ | ||
insertStringAndSaveChange(project, char, editor.document, currentOffset, false) | ||
}) | ||
|
||
editor.caretModel.moveToOffset(currentOffset + char.length) | ||
editor.scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE) | ||
} | ||
|
||
fun replaceText(project: Project, editor: Editor, element: PsiElement?, output: String) { | ||
val primaryCaret = editor.caretModel.primaryCaret; | ||
val start = runReadAction { primaryCaret.selectionStart } | ||
val end = runReadAction { primaryCaret.selectionEnd } | ||
val document = runReadAction { editor.document } | ||
|
||
WriteCommandAction.runWriteCommandAction(project) { | ||
document.replaceString(start, end, output) | ||
} | ||
} | ||
} |
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