-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding fomratting to mulitline autocomplete (#141)
It fixes [JetBrains: explore formatting multiline suggestions within IntelliJ before rendering inlay hint#72](#72) ## Test plan * Trigger multiline autocompletion * Change formatting style * Trigger multiline autocompletion again and check if formatting is in line with your settings https://github.com/sourcegraph/jetbrains/assets/9321940/35ba1eea-7666-4474-aeaf-e953eccce8fc <!-- All pull requests REQUIRE a test plan: https://docs.sourcegraph.com/dev/background-information/testing_principles Why does it matter? These test plans are there to demonstrate that are following industry standards which are important or critical for our customers. They might be read by customers or an auditor. There are meant be simple and easy to read. Simply explain what you did to ensure your changes are correct! Here are a non exhaustive list of test plan examples to help you: - Making changes on a given feature or component: - "Covered by existing tests" or "CI" for the shortest possible plan if there is zero ambiguity - "Added new tests" - "Manually tested" (if non trivial, share some output, logs, or screenshot) - Updating docs: - "previewed locally" - share a screenshot if you want to be thorough - Updating deps, that would typically fail immediately in CI if incorrect - "CI" - "locally tested" -->
- Loading branch information
Showing
6 changed files
with
76 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.sourcegraph.utils | ||
|
||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.fileEditor.FileDocumentManager | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiFileFactory | ||
import com.intellij.psi.codeStyle.CodeStyleManager | ||
import com.intellij.refactoring.suggested.endOffset | ||
|
||
class CodyFormatter { | ||
companion object { | ||
/** | ||
* Formatting used to format inlay text inserted by Cody, based on the surrounding code style in | ||
* the document. | ||
*/ | ||
fun formatStringBasedOnDocument( | ||
originalText: String, | ||
project: Project, | ||
document: Document, | ||
offset: Int | ||
): String { | ||
|
||
val appendedString = | ||
document.text.substring(0, offset) + originalText + document.text.substring(offset) | ||
|
||
val file = FileDocumentManager.getInstance().getFile(document)!! | ||
val psiFile = | ||
PsiFileFactory.getInstance(project) | ||
.createFileFromText("TEMP", file.fileType, appendedString) | ||
|
||
val codeStyleManager = CodeStyleManager.getInstance(project) | ||
|
||
var i = offset | ||
var startRefactoringPosition = offset | ||
while ((document.text.elementAt(i - 1) == ' ' || | ||
document.text.elementAt(i - 1) == '\n' || | ||
document.text.elementAt(i - 1) == '\t') && i > 0) { | ||
startRefactoringPosition = i | ||
i-- | ||
} | ||
var endOffset = offset + psiFile.endOffset - document.textLength | ||
codeStyleManager.reformatText(psiFile, startRefactoringPosition, endOffset) | ||
endOffset = offset + psiFile.endOffset - document.textLength | ||
return psiFile.text.substring(startRefactoringPosition, endOffset) | ||
} | ||
} | ||
} |