[Question] how can i replace identifer with other symbols

i want to write a plugin that can replace specific identifier with pretty symbols.

for example the origin code in java is

int a = 1;

and if i turn on the plugin i can see

int a → 1;

i can auto replace identifer = with the unicode symbol →, and this replacement did not affect the compile process, it's just replace the text showing in the editor.

but with Annotator and TextAttributes, i can just change the color of identifier, not the text of identifier.

and i can just get text form ASTNode and PsiElement, but i can not find any api to set the element's text.

so how can i achive the goal?

 

8 comments
Comment actions Permalink

I kind of had the same problem since Mathematica displays "characters" like \[Gamma] as real Greek characters. In some circumstances it is wanted to have theses things even in Mathematica-package code and what I did is, I implemented it as special code folding. 

Now these characters can be folded to their unicode character and they look like they should, but underneath the correct source code is kept. Here is how it looks

and you find the implementation of it in the MathematicaNamedCharacterFoldingBuilder.java in the GitHub repo.

4
Comment actions Permalink

thx sooooo much! for pointing out that i can use the magic of FoldingBuilder.

and i understand how you replace characters with their unicode characters throught the implementation, you just register

[MathematicaExpressionFoldingBuilder](https://github.com/halirutan/Mathematica-IntelliJ-Plugin/blob/develop/src/de/halirutan/mathematica/codeinsight/folding/MathematicaExpressionFoldingBuilder.java) in the plugin.xml.

but there is one thing that confused me, how can you give a shortcut to fold and unfold code showing in the gif. cause i find out that if i register a FoldingBuilder in plugin.xml, characters while be auto folded, and there is no way to bind a short cut on it.

thx again :)

0
Comment actions Permalink

Hello Jihui,

override this method to collapse symbols by default:

public abstract boolean isCollapsedByDefault(@NotNull ASTNode node);

the shortcut should work ok on both collapsed (by default or through user action) and expanded symbols.

1
Comment actions Permalink

thx Cekusins! i find some shortcuts here https://www.jetbrains.com/idea/help/folding-and-expanding-code-blocks.html

and i tought maybe Scheibe use expand all (Ctrl+Shift+NumPad Plus) and collapse all (Ctrl+Shift+NumPad -) shortcuts in the gif. am i right Scheibe?

0
Comment actions Permalink

Hi,

there are various shortcuts for folding and unfolding. Just press Ctrl+Shift+A within Idea and type "fold" and you will see what shortcuts exist. I accidentally gave you the wrong link, as this class is no longer used in my plugin. I merged everything to MathematicaExpressionFoldingBuilder.

Since folding of such symbols is a special thing, I implemented specific actions to fold/unfold them. Just look into the actions subdirectory and you find 3 folding actions for different purpose.

Additionally, please see the file

com.intellij.lang.folding.FoldingDescriptor

within the Idea code to see what is possible. You can easily make regions fold automatically or make them "unexpandable" so that the folding stays forever, etc.

Cheers
PATRICK

2
Comment actions Permalink

cool! best thx!

i learned a lot from your implementation of Mathematica-IntelliJ-Plugin.

but, one more question, i find out that you did not use or regist the MathematicaNamedFoldingDescriptor and is there any example of using FoldingDescriptor there? @Patrick Scheibe

0
Comment actions Permalink

The folding descriptor is basically the object that tells Idea which PsiNode, TextRange and substitute text is used at one specific place where you can fold your code. Please see my implementation MathematicaExpressionFoldingBuilder in line 93, 106, or 120. The code in this file basically does nothing than finding the places where a fold can happen. Then, you register the region that can be folded like in line 93:

descriptors.add(new MathematicaNamedFoldingDescriptor(node, range, null, ourNamedCharacters.get(key), false));

Cheers
Patrick

2
Comment actions Permalink

my bad, i just grep ''MathematicaNamedFoldingDescriptor" with the whole project and miss some usages there.

thx Patrick! you helped me a lot!

0

Please sign in to leave a comment.