|
1 | | -package com.compiler.server.compiler |
2 | | - |
3 | | -import com.intellij.openapi.project.Project |
4 | | -import com.intellij.openapi.vfs.CharsetToolkit |
5 | | -import com.intellij.psi.PsiElement |
6 | | -import com.intellij.psi.PsiFileFactory |
7 | | -import com.intellij.psi.impl.PsiFileFactoryImpl |
8 | | -import com.intellij.testFramework.LightVirtualFile |
9 | | -import org.jetbrains.kotlin.idea.KotlinLanguage |
10 | | -import org.jetbrains.kotlin.psi.KtExpression |
11 | | -import org.jetbrains.kotlin.psi.KtFile |
12 | | - |
13 | | -class KotlinFile(val kotlinFile: KtFile) { |
14 | | - |
15 | | - fun elementAt( |
16 | | - line: Int, |
17 | | - character: Int |
18 | | - ): PsiElement? = kotlinFile.findElementAt(offsetFor(line, character))?.let { expressionFor(it) } |
19 | | - |
20 | | - fun insert(content: String, atLine: Int, atCharacter: Int): KotlinFile { |
21 | | - val caretPositionOffset = offsetFor(atLine, atCharacter) |
22 | | - return if (caretPositionOffset != 0) { |
23 | | - from( |
24 | | - project = kotlinFile.project, |
25 | | - name = kotlinFile.name, |
26 | | - content = StringBuilder(kotlinFile.text.substring(0, caretPositionOffset)) |
27 | | - .append(content) |
28 | | - .append(kotlinFile.text.substring(caretPositionOffset)).toString() |
29 | | - ) |
30 | | - } else this |
31 | | - } |
32 | | - |
33 | | - private fun offsetFor(line: Int, character: Int) = |
34 | | - (kotlinFile.viewProvider.document?.getLineStartOffset(line) ?: 0) + character |
35 | | - |
36 | | - private tailrec fun expressionFor(element: PsiElement): PsiElement = |
37 | | - if (element is KtExpression) element else expressionFor(element.parent) |
38 | | - |
39 | | - companion object { |
40 | | - fun from(project: Project, name: String, content: String) = |
41 | | - KotlinFile( |
42 | | - (PsiFileFactory.getInstance(project) as PsiFileFactoryImpl) |
43 | | - .trySetupPsiForFile( |
44 | | - LightVirtualFile( |
45 | | - if (name.endsWith(".kt")) name else "$name.kt", |
46 | | - KotlinLanguage.INSTANCE, |
47 | | - content |
48 | | - ).apply { charset = CharsetToolkit.UTF8_CHARSET }, |
49 | | - KotlinLanguage.INSTANCE, true, false |
50 | | - ) as KtFile |
51 | | - ) |
52 | | - } |
53 | | -} |
| 1 | +//package com.compiler.server.compiler |
| 2 | +// |
| 3 | +//import com.intellij.openapi.project.Project |
| 4 | +//import com.intellij.openapi.vfs.CharsetToolkit |
| 5 | +//import com.intellij.psi.PsiElement |
| 6 | +//import com.intellij.psi.PsiFileFactory |
| 7 | +//import com.intellij.psi.impl.PsiFileFactoryImpl |
| 8 | +//import com.intellij.testFramework.LightVirtualFile |
| 9 | +//import org.jetbrains.kotlin.idea.KotlinLanguage |
| 10 | +//import org.jetbrains.kotlin.psi.KtExpression |
| 11 | +//import org.jetbrains.kotlin.psi.KtFile |
| 12 | +// |
| 13 | +//class KotlinFile(val kotlinFile: KtFile) { |
| 14 | +// |
| 15 | +// fun elementAt( |
| 16 | +// line: Int, |
| 17 | +// character: Int |
| 18 | +// ): PsiElement? = kotlinFile.findElementAt(offsetFor(line, character))?.let { expressionFor(it) } |
| 19 | +// |
| 20 | +// fun insert(content: String, atLine: Int, atCharacter: Int): KotlinFile { |
| 21 | +// val caretPositionOffset = offsetFor(atLine, atCharacter) |
| 22 | +// return if (caretPositionOffset != 0) { |
| 23 | +// from( |
| 24 | +// project = kotlinFile.project, |
| 25 | +// name = kotlinFile.name, |
| 26 | +// content = StringBuilder(kotlinFile.text.substring(0, caretPositionOffset)) |
| 27 | +// .append(content) |
| 28 | +// .append(kotlinFile.text.substring(caretPositionOffset)).toString() |
| 29 | +// ) |
| 30 | +// } else this |
| 31 | +// } |
| 32 | +// |
| 33 | +// private fun offsetFor(line: Int, character: Int) = |
| 34 | +// (kotlinFile.viewProvider.document?.getLineStartOffset(line) ?: 0) + character |
| 35 | +// |
| 36 | +// private tailrec fun expressionFor(element: PsiElement): PsiElement = |
| 37 | +// if (element is KtExpression) element else expressionFor(element.parent) |
| 38 | +// |
| 39 | +// companion object { |
| 40 | +// fun from(project: Project, name: String, content: String) = |
| 41 | +// KotlinFile( |
| 42 | +// (PsiFileFactory.getInstance(project) as PsiFileFactoryImpl) |
| 43 | +// .trySetupPsiForFile( |
| 44 | +// LightVirtualFile( |
| 45 | +// if (name.endsWith(".kt")) name else "$name.kt", |
| 46 | +// KotlinLanguage.INSTANCE, |
| 47 | +// content |
| 48 | +// ).apply { charset = CharsetToolkit.UTF8_CHARSET }, |
| 49 | +// KotlinLanguage.INSTANCE, true, false |
| 50 | +// ) as KtFile |
| 51 | +// ) |
| 52 | +// } |
| 53 | +//} |
0 commit comments