Skip to content

Commit

Permalink
feat(completion): improve file reference provider with project file i…
Browse files Browse the repository at this point in the history
…ndex #101

The completion provider for file references has been enhanced to use the project file index, which allows for more accurate and efficient completion suggestions. This change ensures that only relevant files within the project are considered, improving the user experience.
  • Loading branch information
phodal committed Mar 15, 2024
1 parent a2e96b6 commit 32bf931
Showing 1 changed file with 21 additions and 1 deletion.
@@ -1,10 +1,13 @@
package cc.unitmesh.devti.language.completion

import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.ide.presentation.VirtualFilePresentation
import com.intellij.openapi.fileEditor.impl.EditorHistoryManager
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.util.ProcessingContext
import java.io.File

Expand Down Expand Up @@ -33,5 +36,22 @@ class FileReferenceLanguageProvider : CompletionProvider<CompletionParameters>()

result.addElement(element)
}

val projectFileIndex = ProjectFileIndex.getInstance(project)
projectFileIndex.iterateContent {
val removePrefix = it.path.removePrefix(basePath)
val relativePath: String = removePrefix.removePrefix(File.separator)

val element = LookupElementBuilder.create(relativePath)
.withIcon(VirtualFilePresentation.getIcon(it))
.withInsertHandler { context, _ ->
context.editor.caretModel.moveCaretRelatively(
1, 0, false, false, false
)
}

result.addElement(element)
true
}
}
}

0 comments on commit 32bf931

Please sign in to comment.