Skip to content

Commit abe19d4

Browse files
committed
Reworked smart enter
Built smart enter for comments which replaces the normal enter at the end of a comment
1 parent 3ccbfc5 commit abe19d4

7 files changed

Lines changed: 140 additions & 204 deletions

File tree

resources/META-INF/plugin.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<id>de.halirutan.mathematica</id>
2424
<name>Mathematica Support</name>
2525
<category>Custom Language</category>
26-
<version>2.0.31</version>
26+
<version>2.0.32</version>
2727
<idea-version since-build="163"/>
2828
<vendor email="patrick@halirutan.de" url="http://mathematicaplugin.halirutan.de">Patrick Scheibe</vendor>
2929
<depends>com.intellij.modules.lang</depends>
@@ -32,7 +32,7 @@
3232
<br/>
3333
<br/>
3434
<a href="https://github.com/halirutan/Mathematica-IntelliJ-Plugin">GitHub</a> |
35-
<a href="http://halirutan.myjetbrains.com/youtrack/issues/MMAP">Bug Tracker</a> |
35+
<a href="https://github.com/halirutan/Mathematica-IntelliJ-Plugin/issues">Bug Tracker</a> |
3636
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=876XDFGUQHKH6">Donate</a>
3737
<br/>
3838
<br/>
@@ -155,9 +155,6 @@
155155
<enterHandlerDelegate
156156
implementation="de.halirutan.mathematica.codeinsight.editoractions.enter.MathematicaEnterAfterOperatorHandler"/>
157157

158-
<enterHandlerDelegate
159-
implementation="de.halirutan.mathematica.codeinsight.editoractions.enter.MathematicaEnterInsideCommentHandler"/>
160-
161158
<!-- Code-style and colors-->
162159
<colorSettingsPage
163160
implementation="de.halirutan.mathematica.codeinsight.highlighting.MathematicaColorSettingsPage"/>

src/de/halirutan/mathematica/codeinsight/editoractions/enter/MathematicaEnterInsideCommentHandler.java

Lines changed: 0 additions & 147 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2017 Patrick Scheibe
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to deal
5+
* in the Software without restriction, including without limitation the rights
6+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
* THE SOFTWARE.
20+
*/
21+
22+
package de.halirutan.mathematica.codeinsight.editoractions.smartenter;
23+
24+
import com.intellij.lang.SmartEnterProcessorWithFixers.Fixer;
25+
import com.intellij.openapi.editor.CaretModel;
26+
import com.intellij.openapi.editor.Document;
27+
import com.intellij.openapi.editor.Editor;
28+
import com.intellij.psi.PsiComment;
29+
import com.intellij.psi.PsiElement;
30+
import com.intellij.psi.util.PsiTreeUtil;
31+
import com.intellij.util.IncorrectOperationException;
32+
import org.jetbrains.annotations.NotNull;
33+
34+
/**
35+
* Provides the ability to press SmartEnter inside comments and jump to the next line again being inside a new comment.
36+
* This is a very convenient functionality.
37+
*
38+
* @author patrick (11/12/13)
39+
*/
40+
class CommentFixer extends Fixer<MathematicaSmartEnter> {
41+
@Override
42+
public void apply(@NotNull Editor editor, @NotNull MathematicaSmartEnter processor, @NotNull PsiElement element) throws IncorrectOperationException {
43+
Document doc = editor.getDocument();
44+
final CaretModel caretModel = editor.getCaretModel();
45+
if (element instanceof PsiComment && !PsiTreeUtil.hasErrorElements(element)) {
46+
int endOffset = element.getTextOffset() + element.getTextLength();
47+
doc.insertString(endOffset, "\n(* *)");
48+
caretModel.moveToOffset(endOffset + 4);
49+
processor.commit(editor);
50+
}
51+
}
52+
}

src/de/halirutan/mathematica/codeinsight/editoractions/smartenter/CompoundExpressionFixer.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import com.intellij.lang.SmartEnterProcessorWithFixers.Fixer;
2525
import com.intellij.openapi.editor.Document;
2626
import com.intellij.openapi.editor.Editor;
27+
import com.intellij.openapi.util.TextRange;
2728
import com.intellij.psi.PsiElement;
2829
import com.intellij.util.IncorrectOperationException;
30+
import com.intellij.util.text.CharArrayUtil;
2931
import de.halirutan.mathematica.parsing.psi.api.CompoundExpression;
3032
import org.jetbrains.annotations.NotNull;
3133

@@ -38,12 +40,19 @@ class CompoundExpressionFixer extends Fixer<MathematicaSmartEnter> {
3840
@Override
3941
public void apply(@NotNull Editor editor, @NotNull MathematicaSmartEnter processor, @NotNull PsiElement element) throws IncorrectOperationException {
4042
Document doc = editor.getDocument();
41-
if (element instanceof CompoundExpression && element.getTextOffset()+element.getTextLength() == editor.getCaretModel().getOffset()) {
43+
final int caretOffset = editor.getCaretModel().getOffset();
44+
if (element instanceof CompoundExpression) {
4245
final PsiElement lastChild = element.getLastChild();
46+
final TextRange lastChildRange = lastChild.getTextRange();
47+
4348
if (!lastChild.getNode().getElementType().equals(SEMICOLON)) {
44-
final int offset = lastChild.getTextOffset() + lastChild.getTextLength();
45-
doc.insertString(offset, ";\n");
46-
editor.getCaretModel().moveToOffset(offset + 2);
49+
if (lastChildRange.containsOffset(caretOffset) || lastChildRange.getEndOffset() < caretOffset) {
50+
int offset = lastChild.getTextOffset() + lastChild.getTextLength();
51+
offset = CharArrayUtil.shiftBackward(editor.getDocument().getCharsSequence(), offset - 1, " \t\n") + 1;
52+
doc.insertString(offset, ";");
53+
editor.getCaretModel().moveToOffset(offset + 1);
54+
processor.commit(editor);
55+
}
4756
}
4857
}
4958
}

src/de/halirutan/mathematica/codeinsight/editoractions/smartenter/FunctionCallFixer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@ public void apply(@NotNull Editor editor, @NotNull MathematicaSmartEnter process
6969

7070
}
7171
}
72+
73+
7274
}

0 commit comments

Comments
 (0)