Skip to content

Commit 78d95fb

Browse files
committed
feat(completion): improve file reference completion by using editor history and project directory. #101
1 parent 1d2e422 commit 78d95fb

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed
Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package cc.unitmesh.devti.language.completion
22

3-
import com.intellij.codeInsight.completion.CompletionParameters
4-
import com.intellij.codeInsight.completion.CompletionProvider
5-
import com.intellij.codeInsight.completion.CompletionResultSet
3+
import com.intellij.codeInsight.completion.*
64
import com.intellij.codeInsight.lookup.LookupElementBuilder
5+
import com.intellij.ide.presentation.VirtualFilePresentation
6+
import com.intellij.openapi.fileEditor.impl.EditorHistoryManager
7+
import com.intellij.openapi.project.guessProjectDir
8+
import com.intellij.openapi.vfs.VirtualFile
79
import com.intellij.util.ProcessingContext
10+
import java.io.File
811

912
class FileReferenceLanguageProvider : CompletionProvider<CompletionParameters>() {
1013
companion object {
@@ -16,13 +19,25 @@ class FileReferenceLanguageProvider : CompletionProvider<CompletionParameters>()
1619
context: ProcessingContext,
1720
result: CompletionResultSet,
1821
) {
19-
// sample file: "file1", "file2"
20-
listOf("file1", "file2").forEach {
21-
result.addElement(
22-
LookupElementBuilder.create(it)
23-
.withTypeText("file", true)
24-
)
22+
val project = parameters.position.project
23+
val basePath = project.guessProjectDir()?.path ?: return
24+
25+
val editorHistoryManager = EditorHistoryManager.getInstance(project)
26+
val fileList: List<VirtualFile> = editorHistoryManager.fileList
27+
28+
fileList.forEach {
29+
val removePrefix = it.path.removePrefix(basePath)
30+
val relativePath: String = removePrefix.removePrefix(File.separator)
31+
32+
val element = LookupElementBuilder.create(relativePath)
33+
.withIcon(VirtualFilePresentation.getIcon(it))
34+
.withInsertHandler { context, _ ->
35+
context.editor.caretModel.moveCaretRelatively(
36+
1, 0, false, false, false
37+
)
38+
}
39+
40+
result.addElement(element)
2541
}
2642
}
27-
2843
}

0 commit comments

Comments
 (0)