Skip to content

Commit

Permalink
fix(devti-lang): improve file reference completion provider #101
Browse files Browse the repository at this point in the history
The FileReferenceLanguageProvider has been refactored to correctly handle the addition of lookup elements for files within the project. The `canBeAdded` function now ensures that only valid files are considered for completion, and the `buildElement` function has been updated to correctly construct the lookup element with the appropriate icon and insert handler. Additionally, the use of `FileUtilRt` has been introduced to handle file paths more robustly.
  • Loading branch information
phodal committed Mar 15, 2024
1 parent 32bf931 commit ded9c3c
Showing 1 changed file with 35 additions and 25 deletions.
Expand Up @@ -8,7 +8,10 @@ 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.openapi.util.io.FileUtilRt
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.ProcessingContext
import org.jetbrains.annotations.NonNls
import java.io.File

class FileReferenceLanguageProvider : CompletionProvider<CompletionParameters>() {
Expand All @@ -21,37 +24,44 @@ class FileReferenceLanguageProvider : CompletionProvider<CompletionParameters>()
val basePath = project.guessProjectDir()?.path ?: return
val recentlyFiles = EditorHistoryManager.getInstance(project).fileList

// TODO: file should be in project
recentlyFiles.forEach {
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)
if (!canBeAdded(it)) return@forEach
result.addElement(buildElement(it, basePath))
}

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)
if (!canBeAdded(it)) return@iterateContent true
result.addElement(buildElement(it, basePath))
true
}
}

private fun buildElement(
virtualFile: VirtualFile,
basePath: @NonNls String
): LookupElementBuilder {
val removePrefix = virtualFile.path.removePrefix(basePath)
val relativePath: String = removePrefix.removePrefix(File.separator)

return LookupElementBuilder.create(relativePath)
.withIcon(VirtualFilePresentation.getIcon(virtualFile))
.withInsertHandler { context, _ ->
context.editor.caretModel.moveCaretRelatively(
1, 0, false, false, false
)
}
}

private fun canBeAdded(file: VirtualFile): Boolean {
if (!file.isValid || file.isDirectory) {
return false
}

if (file.fileType.isBinary || FileUtilRt.isTooLarge(file.length)) {
return false
}

return true
}
}

0 comments on commit ded9c3c

Please sign in to comment.