ConsoleFilterProvider in Run is not working.
Answered
<consoleFilterProvider implementation="MyConsoleFilterProvider" order="first" />
class DartInfo(
val file: VirtualFile,
val className: String = ""
)
fun VirtualFile.toPsiFile(project: Project): PsiFile? = PsiManager.getInstance(project).findFile(this)
class DartConsole(val project: Project) : Filter {
val dartList = getAllDartFiles(project)
companion object {
const val STATE_WITH = "GetX:"
fun getAllDartFiles(project: Project): List<DartInfo> {
var list = ArrayList<DartInfo>()
ProjectFileIndex.getInstance(project).iterateContent { e ->
if (e.name.endsWith(".dart")) {
val psiFile = e.toPsiFile(project)
if (psiFile != null) {
for (name in getDartClassName(psiFile)) {
list.add(DartInfo(e, name))
}
}
}
true
}
return list
}
fun getDartClassName(psiFile: PsiFile): List<String> {
val list = arrayListOf<String>()
val classList = PsiTreeUtil.findChildrenOfType(psiFile, DartClassDefinitionImpl::class.java)
for (cls in classList) {
val name = cls.name ?: continue
list.add(name)
}
return list
}
}
override fun applyFilter(line: String, entireLength: Int): Filter.Result? {
val index = line.indexOf(STATE_WITH)
if (index != -1) {
val clsName = line.substring(index + STATE_WITH.length)
println("clsName: $clsName")
val file = dartList.filter { it.className == clsName }.map { it.file }
if (file.isEmpty()) {
return null
}
println("size: ${file.size}")
return Filter.Result(
index, entireLength,
OpenFileHyperlinkInfo(project, file.first(), 0, 0)
)
}
return null
}
}
class MyConsoleFilterProvider : ConsoleFilterProvider {
override fun getDefaultFilters(project: Project): Array<Filter> {
return arrayOf(DartConsole(project))
}
}
It can run normally in the terminal
Does not work properly in Run
Please sign in to leave a comment.
Hi,
It is hard to understand the issue cause. Is your filter even created in console view? Did you try to debug
com.intellij.execution.impl.ConsoleViewUtil#computeConsoleFilters(Project, ConsoleView, GlobalSearchScope)
?Hi, I found the possible reason. The run configuration in the flutter plugin seems to be an extension of manually adding Filter.
https://github.com/flutter/flutter-intellij/blob/master/flutter-idea/src/io/flutter/run/SdkRunConfig.java#L225