Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7b95c45
Add base class for FS
MituuZ Jul 24, 2024
9a8b131
Ignore .kotlin
MituuZ Jul 24, 2024
26d59a8
Few options
MituuZ Jul 25, 2024
cdb63fa
Check treeModel from FileStructurePopup
MituuZ Jul 25, 2024
2f08388
Set up UAST
MituuZ Jul 25, 2024
d9949a0
Set up PsiLoop for PsiNamedElements
MituuZ Jul 27, 2024
4ee8a9b
Initial version with UAST handling
MituuZ Jul 27, 2024
837c691
Populate the lost model with simple containers
MituuZ Jul 27, 2024
fd95ca9
Minor refactor and clean up
MituuZ Jul 27, 2024
44882cb
Use score calculator and add a TODO for ETD issue.
MituuZ Jul 27, 2024
39c54bf
Always use file path with fs
MituuZ Jul 28, 2024
a75c1b0
Add license and clean up imports
MituuZ Jul 28, 2024
3b8e7a3
Add javadoc and displayString
MituuZ Jul 28, 2024
e8c640a
Remove displayString
MituuZ Jul 28, 2024
103f451
Make offset getting more concise
MituuZ Jul 28, 2024
8093603
Override initial view to show all structs in the current file
MituuZ Jul 28, 2024
fc724fd
Add return types to methods
MituuZ Jul 28, 2024
0916537
Improve processing
MituuZ Jul 28, 2024
9ea9d84
Parse parameter strings for methods
MituuZ Jul 28, 2024
4f306bf
Add variable types
MituuZ Jul 28, 2024
c06f258
Align string reps of the objects
MituuZ Jul 28, 2024
cfb430f
The text is not monospaced
MituuZ Jul 28, 2024
076915b
Skip empty methods
MituuZ Jul 28, 2024
84ce277
Clean imports
MituuZ Jul 28, 2024
90b96d8
Create a separate listener for fs and get text reps
MituuZ Jul 28, 2024
5d6ce8c
Refactor string reps
MituuZ Jul 28, 2024
94d12fe
Clean up
MituuZ Jul 28, 2024
0b64e2e
Add java dependency using platform 2.x
MituuZ Aug 25, 2024
aed493c
Get offset to fuzzy match container. Scrolling model testing
MituuZ Sep 27, 2024
5ebc627
Add vertical scrollbar back
MituuZ Sep 27, 2024
2dfa543
Scroll preview pane to the correct position
MituuZ Sep 27, 2024
600a730
Add second set of offsets
MituuZ Sep 27, 2024
c78d0ea
Move scrolling to functions
MituuZ Nov 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.gradle
.kotlin
build/
.kotlin/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ dependencies {
zipSigner()

testFramework(TestFrameworkType.Platform)

bundledPlugin("com.intellij.java")
}

testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/mituuz/fuzzier/Fuzzier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ open class Fuzzier : FuzzyAction() {
/**
* Populates the file list with recently opened files
*/
private fun createInitialView(project: Project) {
open fun createInitialView(project: Project) {
ApplicationManager.getApplication().executeOnPooledThread {
val editorHistoryManager = EditorHistoryManager.getInstance(project)

Expand Down Expand Up @@ -301,7 +301,7 @@ open class Fuzzier : FuzzyAction() {
popup?.cancel()
}

private fun createListeners(project: Project) {
open fun createListeners(project: Project) {
// Add a listener that updates the contents of the preview pane
component.fileList.addListSelectionListener { event ->
if (!event.valueIsAdjusting) {
Expand Down
243 changes: 243 additions & 0 deletions src/main/kotlin/com/mituuz/fuzzier/FuzzierFS.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*
MIT License

Copyright (c) 2024 Mitja Leino

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.mituuz.fuzzier

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.findPsiFile
import com.intellij.psi.*
import com.intellij.psi.impl.source.PsiClassReferenceType
import com.mituuz.fuzzier.components.FuzzyFinderComponent
import com.mituuz.fuzzier.entities.FuzzyMatchContainer
import com.mituuz.fuzzier.entities.FuzzyMatchContainer.FuzzyScore
import com.mituuz.fuzzier.entities.ScoreCalculator
import org.jetbrains.uast.*
import org.jetbrains.uast.visitor.AbstractUastVisitor
import javax.swing.DefaultListModel

class FuzzierFS : Fuzzier() {
override var title: String = "Fuzzy Search (File Structure)"
override fun updateListContents(project: Project, searchString: String) {
component.isFs = true
val fileEditorManager = FileEditorManager.getInstance(project)
val currentEditor = fileEditorManager.selectedEditor

val listModel = DefaultListModel<FuzzyMatchContainer>()

if (currentEditor != null) {
ApplicationManager.getApplication().runReadAction() {
val psiFile: PsiFile? = currentEditor.file.findPsiFile(project)

if (psiFile != null) {
val uFile = UastFacade.convertElementWithParent(psiFile, UFile::class.java)
uFile?.accept(getVisitor(listModel, searchString))

ProgressManager.getInstance().run {
component.fileList.model = listModel
component.fileList.cellRenderer = getCellRenderer()
component.fileList.setPaintBusy(false)
if (!component.fileList.isEmpty) {
component.fileList.setSelectedValue(listModel[0], true)
}
}
}
}
}
}

override fun createListeners(project: Project) {
// Add a listener that updates the contents of the preview pane
component.fileList.addListSelectionListener { event ->
if (!event.valueIsAdjusting) {
if (component.fileList.isEmpty) {
ApplicationManager.getApplication().invokeLater {
val previewPane = (component as FuzzyFinderComponent).previewPane
previewPane.updateFile(EditorFactory.getInstance().createDocument(""))
}
return@addListSelectionListener
}
val selectedValue = component.fileList.selectedValue
val currentFile = FileEditorManager.getInstance(project).selectedEditor?.file

ProgressManager.getInstance().run(object : Task.Backgroundable(null, "Loading file", false) {
override fun run(indicator: ProgressIndicator) {
ApplicationManager.getApplication().invokeLater {
val previewPane = (component as FuzzyFinderComponent).previewPane
previewPane.updateFile(currentFile, selectedValue?.fileOffset)
}
}
})
}
}
}

override fun createInitialView(project: Project) {
ApplicationManager.getApplication().executeOnPooledThread {
component.isFs = true
val fileEditorManager = FileEditorManager.getInstance(project)
val currentEditor = fileEditorManager.selectedEditor

val listModel = DefaultListModel<FuzzyMatchContainer>()

if (currentEditor != null) {
ApplicationManager.getApplication().runReadAction() {
val psiFile: PsiFile? = currentEditor.file.findPsiFile(project)

if (psiFile != null) {
val uFile = UastFacade.convertElementWithParent(psiFile, UFile::class.java)
uFile?.accept(getOpenVisitor(listModel))

ProgressManager.getInstance().run {
component.fileList.model = listModel
component.fileList.cellRenderer = getCellRenderer()
component.fileList.setPaintBusy(false)
if (!component.fileList.isEmpty) {
component.fileList.setSelectedValue(listModel[0], true)
}
}
}
}
}
}
}

private fun getOpenVisitor(listModel: DefaultListModel<FuzzyMatchContainer>): AbstractUastVisitor {
return object : AbstractUastVisitor() {
override fun visitClass(node: UClass): Boolean {
val name = node.name
val displayString = getTextRepresentation(node, name)
createStaticContainer(listModel, name, displayString, node.textRange)
return super.visitClass(node)
}

override fun visitMethod(node: UMethod): Boolean {
val name = node.name
val displayString = getTextRepresentation(node, name)
createStaticContainer(listModel, name, displayString, node.textRange)
return super.visitMethod(node)
}

override fun visitVariable(node: UVariable): Boolean {
val name = node.name
val displayString = getTextRepresentation(node, name)
createStaticContainer(listModel, name, displayString, node.textRange)
return super.visitVariable(node)
}
}
}

private fun createStaticContainer(listModel: DefaultListModel<FuzzyMatchContainer>, name: String?,
displayString: String?, textRange: com.intellij.openapi.util.TextRange?) {
if (name.isNullOrBlank() || displayString == null) {
return
}
val container = FuzzyMatchContainer(FuzzyScore(), displayString, name)
if (textRange != null) {
container.fileOffset = textRange.startOffset
}
listModel.addElement(container)
}

private fun getVisitor(listModel: DefaultListModel<FuzzyMatchContainer>,
searchString: String = ""): AbstractUastVisitor {
return object : AbstractUastVisitor() {
override fun visitClass(node: UClass): Boolean {
val name = node.name
val displayString = getTextRepresentation(node, name)
createContainer(listModel, searchString, displayString, name, node.textRange)
return super.visitClass(node)
}

override fun visitMethod(node: UMethod): Boolean {
val name = node.name
val displayString = getTextRepresentation(node, name)
createContainer(listModel, searchString, displayString, name, node.textRange)
return super.visitMethod(node)
}

override fun visitVariable(node: UVariable): Boolean {
val name = node.name
val displayString = getTextRepresentation(node, name)
createContainer(listModel, searchString, displayString, name, node.textRange)
return super.visitVariable(node)
}
}
}

private fun getTextRepresentation(uElement: UElement, name: String?): String? {
if (name.isNullOrBlank()) {
return null;
}
when (uElement) {
is UVariable -> {
val type = uElement.type.presentableText
return "Variable: $name: $type"
}
is UMethod -> {
val params: List<UParameter> = uElement.uastParameters
var paramString = ""
var returnString = ""
if (params.isNotEmpty()) {
paramString = "("
for (param: UParameter in params) {
paramString = "$paramString${param.name}: ${(param.type as PsiClassReferenceType).name}, "
}
paramString = paramString.removeSuffix(", ")
paramString = "$paramString)"
} else {
paramString = "()"
}
val returnType = uElement.returnType
if (returnType != null && returnType.presentableText != "void") {
returnString = ": ${returnType.presentableText}"
}
return "Method: $name$paramString$returnString"
}
is UClass -> return "Class: $name"
}
return null
}

private fun createContainer(listModel: DefaultListModel<FuzzyMatchContainer>, searchString: String,
displayString: String?, name: String?, textRange: com.intellij.openapi.util.TextRange?) {
if (name.isNullOrBlank() || displayString == null) {
return;
}
val scoreCalculator = ScoreCalculator(searchString)
val fs = scoreCalculator.calculateScore(name)
if (fs != null) {
val container = FuzzyMatchContainer(fs, displayString, name)
if (textRange != null) {
container.fileOffset = textRange.startOffset
}
listModel.addElement(container)
}
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/com/mituuz/fuzzier/FuzzyAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ abstract class FuzzyAction : AnAction() {
val renderer =
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus) as JLabel
val container = value as FuzzyMatchContainer
val filenameType: FilenameType = if (component.isDirSelector) {
FILE_PATH_ONLY // Directories are always shown as full paths
val filenameType: FilenameType = if (component.isDirSelector || component.isFs) {
// Directories and file structures are always shown as full paths
FILE_PATH_ONLY
} else {
fuzzierSettingsService.state.filenameType
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ open class FuzzyComponent : JPanel() {
var fileList = JBList<FuzzyMatchContainer?>()
var searchField = EditorTextField()
var isDirSelector = false
var isFs = false
}
32 changes: 27 additions & 5 deletions src/main/kotlin/com/mituuz/fuzzier/components/PreviewEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.fileEditor.FileDocumentManager
Expand Down Expand Up @@ -56,7 +57,12 @@ class PreviewEditor(project: Project?) : EditorTextField(
}

override fun createEditor(): EditorEx {
val editor = super.createEditor()
val editor = super.createEditor() // TODO: Exception in thread com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: Access is allowed from Event Dispatch Thread (EDT) only; see https://jb.gg/ij-platform-threading for details editor.setVerticalScrollbarVisible(true)
/*
at com.mituuz.fuzzier.components.PreviewEditor.updateFile(PreviewEditor.kt:78)
at com.mituuz.fuzzier.Fuzzier.createListeners$lambda$12(Fuzzier.kt:234)
at com.mituuz.fuzzier.FuzzierFS.updateListContents$lambda$1(FuzzierFS.kt:39)
*/
editor.setVerticalScrollbarVisible(true)
editor.setHorizontalScrollbarVisible(true)
editor.isOneLineMode = false
Expand All @@ -79,7 +85,7 @@ class PreviewEditor(project: Project?) : EditorTextField(
this.fileType = PlainTextFileType.INSTANCE
}

fun updateFile(virtualFile: VirtualFile?) {
fun updateFile(virtualFile: VirtualFile?, offset: Int? = null) {
ApplicationManager.getApplication().executeOnPooledThread {
val sourceDocument = ApplicationManager.getApplication().runReadAction<Document?> {
virtualFile?.let { FileDocumentManager.getInstance().getDocument(virtualFile) }
Expand All @@ -101,9 +107,9 @@ class PreviewEditor(project: Project?) : EditorTextField(
this.document = sourceDocument
}
ApplicationManager.getApplication().invokeLater {
editor?.scrollingModel?.run {
scrollHorizontally(0)
scrollVertically(0)
scrollTo0()
if (offset != null) {
moveCaretToOffset(offset)
}
}
}
Expand All @@ -114,4 +120,20 @@ class PreviewEditor(project: Project?) : EditorTextField(
}
}
}

fun scrollTo0() {
editor?.scrollingModel?.run {
scrollHorizontally(0)
scrollVertically(0)
}
}

fun moveCaretToOffset(offset: Int) {
val caret = editor?.caretModel?.primaryCaret
if (caret != null) {
caret.moveToOffset(offset)
val logicalPosition = caret.logicalPosition
editor?.scrollingModel?.scrollTo(logicalPosition, ScrollType.CENTER)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ class FuzzyMatchContainer(
) : Serializable {
@Transient
private var initialPath: String? = null
var fileOffset: Int? = null

companion object {
/**
* Used for showing recent files
*
* Creates a fuzzy match container with explicitly specified score.
*/
fun createOrderedContainer(
order: Int,
filePath: String,
Expand Down
Loading
Loading