Skip to content

Commit 90f978f

Browse files
committed
fix(completion): handle null paths in file completion
- Safely handle null paths in FileCompletionProvider by checking buildElement result - Make relativePath() more robust by catching exceptions and handling invalid files - Remove unused VirtualFilePresentation import
1 parent dac43aa commit 90f978f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

core/src/main/kotlin/cc/unitmesh/devti/util/ProjectFileUtil.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ fun VirtualFile.canBeAdded(project: Project): Boolean {
5252
}
5353

5454
fun VirtualFile.relativePath(project: Project): String {
55+
if (!this.isValid) return this.path
5556
val projectDir = project.guessProjectDir()!!.toNioPath().toFile()
56-
val relativePath = FileUtil.getRelativePath(projectDir, this.toNioPath().toFile())
57+
val relativePath = try {
58+
FileUtil.getRelativePath(projectDir, this.toNioPath().toFile())
59+
} catch (e: Exception) {
60+
null
61+
}
62+
5763
return relativePath ?: this.path
5864
}
5965

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/completion/provider/FileCompletionProvider.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import com.intellij.codeInsight.lookup.LookupElement
1010
import com.intellij.codeInsight.lookup.LookupElementBuilder
1111
import com.intellij.codeInsight.lookup.LookupElementPresentation
1212
import com.intellij.codeInsight.lookup.LookupElementRenderer
13-
import com.intellij.ide.presentation.VirtualFilePresentation
1413
import com.intellij.openapi.fileEditor.impl.EditorHistoryManager
1514
import com.intellij.openapi.project.Project
1615
import com.intellij.openapi.roots.ProjectFileIndex
@@ -29,7 +28,8 @@ class FileCompletionProvider : CompletionProvider<CompletionParameters>() {
2928
var recentlyFiles: MutableList<VirtualFile> = mutableListOf()
3029
EditorHistoryManager.getInstance(project).fileList.forEach {
3130
if (!it.canBeAdded()) return@forEach
32-
result.addElement(buildElement(it, project, 99.0))
31+
val element = buildElement(it, project, 99.0) ?: return@forEach
32+
result.addElement(element)
3333
recentlyFiles.add(it)
3434
}
3535

@@ -38,14 +38,14 @@ class FileCompletionProvider : CompletionProvider<CompletionParameters>() {
3838
if (recentlyFiles.contains(it)) return@iterateContent true
3939
if (!ProjectFileIndex.getInstance(project).isInContent(it)) return@iterateContent true
4040
if (ProjectFileIndex.getInstance(project).isUnderIgnored(it)) return@iterateContent true
41-
result.addElement(buildElement(it, project, 1.0))
41+
val element = buildElement(it, project, 1.0) ?: return@iterateContent true
42+
result.addElement(element)
4243
true
4344
}
4445
}
4546

46-
private fun buildElement(virtualFile: VirtualFile, project: Project, priority: Double): LookupElement {
47-
val filepath = virtualFile.relativePath(project)
48-
47+
private fun buildElement(virtualFile: VirtualFile, project: Project, priority: Double): LookupElement? {
48+
val filepath = virtualFile.relativePath(project) ?: return null
4949
val elementBuilder = LookupElementBuilder.create(filepath)
5050
.withCaseSensitivity(false)
5151
.withRenderer(object : LookupElementRenderer<LookupElement>() {

0 commit comments

Comments
 (0)