SmartEnterProcessor for custom language

I have started on a smart enter processor by extending my class from SmartEnterProcessorWithFixers. Before I go on, I want to know whether this is the correct way.
I saw that JavaSmartEnterProcessor uses Fixers to implement bahavior for several situations where the user can press Ctrl+Shift+Enter. Furthermore, there is an abstract SmartEnterProcessorWithFixers class which seems (unfortuntately) not to be used.

What is the correct to set up SmartEnterProcessor for your own language without reimplementing/copying most methods? To give a specific example:

globalVariable = 2;
myFunction[1,2+glo|


at the position of the | and pressing then smart enter should transform the code to

globalVariable = 2;
myFunction[1,2+globalVariable]


Meaning it should autocomplete (which it already does) and close the function call.

Thanks,
Patrick

3 comments

First of all, it's important to understand that completion is not part of SmartEnterProcessor's functionality. If you press Ctrl-Shift-Enter while a completion popup is displayed, the completion is triggered and then the smart enter processor is invoked. If you press Ctrl-Shift-Enter when no completion popup is visible, no completion happens.

SmartEnterProcessorWithFixers is used, for example, by the GroovySmartEnterProcessor class. You'll see that a lot of logic in this class is language-specific, so I'm not sure which "copying most methods" you want to avoid.

0

Just to add to what Dmitry said.

You even said yourself, you are doing 2 things.

1) Autocomplete
2) Close the function call


Completion can take care of everything if you allow '}' to end completion and accept the result. This may be the default behavior anyways. Just press the ')' key while the completions are being displayed and one is selected, it should insert your completion, then follow by inserting your close function token.

I use EnterHandlerDelegate to move the caret to the correct location during editing instead of just inserting a 'dumb' linefeed.

Before:

function a() <caret>end


After:

function a()     <caret> end


I mention this because in your example you hafve no closing ')'. You could have just inserted the pair ')' when they typed '('.

However - I should tell you my own belief that you should try to keep the code free from syntax errors if you can while you are editing. To that end, I do the following:

The user types the function keyword

a(function<caret>)


When he types '(' I insert a gramatically complete function expression

a(function(<caret>) end)


This uses the TypedHandlerDelegate EP which can be tricky to get right.

You could make the case that the function expression could be a completion I guess, except that the caret is not positioned at the end of the insterted code which is what you expect from a completion.


Sometimes it is a bit confusing which one to use. Though now after typing all this. I think these are both good use cases for Smart Enter also. Though smart enter'd behavor tends to change depending on when you press enter since it cant always figure out what you want to do when you press enter.

0

Hi Jon,

thanks for your answer. My example was chosen badly because I do always insert closing braces when I type an opening '('. The PairedBraceMatcher seems to take care of this automatically.

About the SmarEnterProcessor: I have had a closer look and I agree with Dmitry that it's probably best to implement it myself. Everything I have seen is language specific and since Mathematica is pretty different, because there are is no special do-loop, for-loop, switch, whatever syntax. Everything is a function-call like Do[...], For[...], While[...], Switch[...], Sqrt[...].

Though smart enter'd behavor tends to change depending on when you press enter since it cant always figure out what you want to do when you press enter.


Exactly this is the point. When someone presses SmartEnter, I have to go the PSI tree upwards and jump behind the expression where I "think" the user wants to be. I'll let you now what I came up with.

Thank you both for your answer.

Cheers
Patrick
0

Please sign in to leave a comment.