Skip to content

Commit

Permalink
intellij-plugin: privot approach (#2353)
Browse files Browse the repository at this point in the history
* feat: create own PsiFile

* feat: support renaming of modules

* feat: remove unnecessary RenameHandler

* feat: remove unused rename handler

* feat: movement

* fix: null exception on project switch

* feat: debug

* feat: radically simplify the plugin

* chore: remove old stuff

* Update apps/intellij-plugin/src/main/kotlin/ai/hash/rustextra/projectView/ModuleDirectoryNode.kt

Co-authored-by: Tim Diekmann <[email protected]>

* Update apps/intellij-plugin/src/main/resources/META-INF/plugin.xml

* Update apps/intellij-plugin/build.gradle.kts

* Update ModuleDirectoryNode.kt

* chore: run `ktlint`

---------

Co-authored-by: Tim Diekmann <[email protected]>
  • Loading branch information
indietyp and TimDiekmann authored Apr 10, 2023
1 parent 720c4dc commit 9420a82
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ai.hash.rustextra.projectView

import com.intellij.ide.projectView.NodeSortOrder
import com.intellij.ide.projectView.NodeSortSettings
import com.intellij.ide.projectView.ViewSettings
import com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDirectory

class ModuleDirectoryNode(project: Project?, value: PsiDirectory, viewSettings: ViewSettings?) : PsiDirectoryNode(project, value, viewSettings) {
// This is needed, as otherwise the `GroupByTypeComparator` won't fall back to the `AlphaComparator`
override fun getTypeSortWeight(sortByType: Boolean): Int = 0

// This value is taken from `PsiFileNode`, which assigns a static value of `20`
// we need to directly use the magic value of 20, as we're unable to invoke the method on `PsiFileNode`
// directly.
// This allows us to bypass the `AlphaComparator` and use the `FileNameComparator` for sorting.
override fun getWeight(): Int = 20

// this is the default `SortKey` of `RsFile`, using the same allows us to group those together
override fun getSortKey(): Int = 0

// taken from ProjectView, overwrites the directory sort order
@Suppress("UnstableApiUsage")
override fun getSortOrder(settings: NodeSortSettings): NodeSortOrder =
if (settings.isManualOrder) NodeSortOrder.MANUAL else NodeSortOrder.UNSPECIFIED

// a bit of a hack, if the `SortKey` is the same the `GroupByTypeComparator` redirects to the `AlphaComparator`,
// if the weights of both files are the same, the `AlphaComparator` uses the `FileNameComparator` with the `toString`
// representation of a node. For the node to be able to be displayed above the directory we "emulate" the name of
// the file (and add `.mod` to ensure that we're always last).
// AFAIK `toString()` is not used anywhere else.
override fun toString(): String {
val name = super.toString()

return "$name.rs.mod"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class ModuleGroupTreeStructureProvider : TreeStructureProvider, DumbAware {
// TODO: add a custom context menu
val (file, directory) = modules[value.virtualFile.nameWithoutExtension]!!

nodes.add(ModuleNode.fromPsiFileNode(file, settings, directory.children))

nodes.add(file)
nodes.add(ModuleDirectoryNode(directory.project, directory.value, settings))
continue
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 1 addition & 4 deletions apps/intellij-plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
<extensions defaultExtensionNs="com.intellij">
<treeStructureProvider implementation="ai.hash.rustextra.projectView.ModuleGroupTreeStructureProvider" order="last, after org.rust.lang"/>
<treeStructureProvider implementation="ai.hash.rustextra.projectView.ModuleGroupTreeStructureProvider" />
</extensions>
<projectListeners>
<listener class="ai.hash.rustextra.projectView.ModuleNodeToolWindowListener" topic="com.intellij.openapi.wm.ex.ToolWindowManagerListener" />
</projectListeners>
</idea-plugin>

0 comments on commit 9420a82

Please sign in to comment.