Formatter: Smart indent on Enter

Hi,

first of all, I read the last paragraph of the "Code Formatter" section in the "Developing Custom Language" document, but it seems I'm unable to get it right for my Mathematica IntelliJ Plugin. Let's assume a very simple situation:

f[
  a
]


This is how it should look (because a function call f[..] creates a new indentation level for its argument(s)) and it is correctly indented when I press Ctrl+Alt+L. The underlying block structure is the following (I'm showin it in the form: IElementType, beginText...endText,Range,Indent)

FILE(f[
...a
](0,8))<Indent: NONE>
--------------
FUNCTION_CALL_EXPRESSION(f[
...a
](0,8))<Indent: CONTINUATION_WITHOUT_FIRST>
--------------
SYMBOL_EXPRESSION(f...f(0,1))<Indent: NONE>
--------------
IDENTIFIER(f...f(0,1))<Indent: NONE>
--------------
LEFT_BRACKET([...[(1,2))<Indent: NONE>
--------------
SYMBOL_EXPRESSION(a...a(5,6))<Indent: NORMAL>
--------------
IDENTIFIER(a...a(5,6))<Indent: NONE>
--------------
RIGHT_BRACKET(]...](7,8))<Indent: NONE>
--------------


If I type this expression now bit after bit where | denotes the cursor it goes:

f|
f[|] (because paired braces are inserted)
f[
a|
]

The question is, how can I make that after pressing Enter an indent is automatically inserted. Please note that f[] is a *complete expression* by itself but even if I let isIncomplete() return true, it seems getChildAttributes(..) is not called. What I need is kind of the same as when you type the {} in a java function and press then Enter. The closing } is moved one line below and it is automatically indented. Until now, I thought that this is done with the formatters smart indent.

Cheers
Patrick

1 comment

OK, for now I have a solution. I registered my own enterHandlerDelegate

<enterHandlerDelegate implementation="de.halirutan.mathematica.codeInsight.formatter.MathematicaEnterBetweenBracesHandler"/>


The implementation class extends from com.intellij.codeInsight.editorActions.enter.EnterBetweenBracesHandler where the only thing I have overwritten is the isBracePair method

package de.halirutan.mathematica.codeInsight.formatter;

import com.intellij.codeInsight.editorActions.enter.EnterBetweenBracesHandler;

/**
* @author patrick (11/12/13)
*/
public class MathematicaEnterBetweenBracesHandler extends EnterBetweenBracesHandler {

  protected boolean isBracePair(char c1, char c2) {
    return (c1 == '(' && c2 == ')') || (c1 == '{' && c2 == '}') || (c1 == '[' && c2 == ']');
  }
}

0

Please sign in to leave a comment.