Questions about TreeStructureProvider

I want to implement plugin, which should be able to update Project View structure.

From:

  • my_project
    • project1_file1.xml
    • project1_file2.xml
    • project2_file1.xml
    • project2_file2.xml


To:

  • my_project
    • project1
      • file1.xml
      • file2.xml
    • project2
      • file1.xml
      • file2.xml


User should be able to switch back and forth. Note folders `project1` and `project2` should not be created on file system, also `xml` files should not be re-named on file system.

Questions:

  1. Is it possible?
  2. As I undesrtood I need to use `com.intellij.ide.projectView.TreeStructureProvider` and return my `Collection<AbstractTreeNode>` inside `modify` method, right?
  3. How to create `PsiDirectoryNode` for `project1` and `project2`? How to rename `project1_file1.xml` to `file1.xml`?

Code

// for creating folder
 
Project project = ProjectManager.getInstance().getOpenProjects()[0];

 
PsiDirectory psiDirectory = PsiDirectoryFactory.getInstance(project).createDirectory(file); <= I am stuck with creating VirtualFile
AbstractTreeNode
node = new PsiDirectoryNode(project, psiDirectory, viewSettings);


// for re-naming PsiFile
 
for (AbstractTreeNode child : children) {
    if(child.getValue() instanceof PsiFile) {
        PsiFile file = (PsiFile) child.getValue();
        
file.setName("new name"); // this will re-name my file physically, and I need only for Project View
    }
}


Thanks in advance.

3 comments
Comment actions Permalink

1. Yes, this is possible.
2. Yes, this is correct.
3. A PsiDirectoryNode represents an actual directory on disk. Since in your case there is no such directory, you need to create a different type of node representing your group of files. See FormNode for an example of such node class. To change the name shown for a file, you can create a node class extending PsiFileNode and override the updateImpl() method to customize the presentation.

0
Comment actions Permalink

Can you help me with one more thing. My plugin is working  with `com.intellij.ide.projectView.TreeStructureProvider` but in Android Studio user can switch project structure view to Android:

image.png

Method `modify` of my `TreeStructureProvider ` is never called.

I think this is because I declared my `TreeStructureProvider` as below:

<extensions defaultExtensionNs="com.intellij">
  <treeStructureProvider implementation="com.dd.FoldingStructureProvider"/>
</extensions>


How I need to declare `TreeStructureProvider` to make it work with Android project structure view?

0
Comment actions Permalink

You can't. The Android view defines its own structure and does not allow modifying the structure through any extensions.

0

Please sign in to leave a comment.