Skip to content

Commit c83fa41

Browse files
committed
Implement a simple comment enter handler that inserts * at the beginning
1 parent 6b20463 commit c83fa41

4 files changed

Lines changed: 167 additions & 1 deletion

File tree

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
<enterHandlerDelegate implementation="de.halirutan.mathematica.codeinsight.editoractions.enter.MathematicaEnterInsideFunctionHandler"/>
6868
<enterHandlerDelegate implementation="de.halirutan.mathematica.codeinsight.editoractions.enter.MathematicaEnterAfterOperatorHandler"/>
69+
<enterHandlerDelegate implementation="de.halirutan.mathematica.codeinsight.editoractions.enter.CommentStarInsertEnterHandler"/>
6970

7071
<liveTemplateContext implementation="de.halirutan.mathematica.codeinsight.livetemplates.MathematicaTemplateContextType"/>
7172
<defaultLiveTemplatesProvider implementation="de.halirutan.mathematica.codeinsight.livetemplates.MathematicaDefaultLiveTemplateProvider"/>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2017. Patrick Scheibe
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the “Software”), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package de.halirutan.mathematica.codeinsight.editoractions.enter
27+
28+
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate
29+
import com.intellij.openapi.actionSystem.CommonDataKeys
30+
import com.intellij.openapi.actionSystem.DataContext
31+
import com.intellij.openapi.editor.Editor
32+
import com.intellij.openapi.util.TextRange
33+
import com.intellij.psi.PsiComment
34+
import com.intellij.psi.PsiDocumentManager
35+
import com.intellij.psi.PsiFile
36+
37+
/**/
38+
39+
/**
40+
*
41+
* @author patrick (04.12.17).
42+
*/
43+
class CommentStarInsertEnterHandler : MathematicaEnterHandler() {
44+
override fun postProcessEnter(file: PsiFile, editor: Editor, dataContext: DataContext): EnterHandlerDelegate.Result {
45+
skipWithResultQ(file, editor, dataContext)?.let { return it }
46+
val caretModel = editor.caretModel
47+
val offset = caretModel.offset
48+
val project = dataContext.getData(CommonDataKeys.PROJECT) ?: return EnterHandlerDelegate.Result.Continue
49+
val psiDocManager = PsiDocumentManager.getInstance(project)
50+
val element = file.findElementAt(offset) ?: return EnterHandlerDelegate.Result.Continue
51+
if (element is PsiComment) {
52+
val document = editor.document
53+
val textLength = document.textLength
54+
55+
// The case that we opened a comment with (*|) and therefore the complete file is commented
56+
// We insert the missing *
57+
if (element.textRange.endOffset == textLength && offset < textLength && document.getText(TextRange.create(offset, offset + 1)) == ")") {
58+
document.insertString(offset, " * \n *")
59+
caretModel.moveToOffset(offset + 3)
60+
psiDocManager.commitDocument(document)
61+
return EnterHandlerDelegate.Result.Stop
62+
}
63+
64+
65+
val lineNumber = document.getLineNumber(offset)
66+
val elementStartLine = document.getLineNumber(element.textOffset)
67+
val elementEndLine = document.getLineNumber(element.textOffset + element.textLength)
68+
69+
val insertString: String
70+
val move: Int
71+
if (lineNumber == elementStartLine + 1) {
72+
insertString = " * "
73+
move = 3
74+
} else {
75+
insertString = "* "
76+
move = 2
77+
}
78+
document.insertString(offset, insertString)
79+
caretModel.moveToOffset(offset + move)
80+
81+
if (lineNumber == elementEndLine) {
82+
document.insertString(offset + move, "\n ")
83+
}
84+
psiDocManager.commitDocument(document)
85+
return EnterHandlerDelegate.Result.DefaultSkipIndent
86+
}
87+
return EnterHandlerDelegate.Result.Continue
88+
}
89+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2017. Patrick Scheibe
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the “Software”), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package de.halirutan.mathematica.codeinsight.editoractions.enter
27+
28+
import com.intellij.codeInsight.CodeInsightSettings
29+
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate
30+
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegateAdapter
31+
import com.intellij.openapi.actionSystem.CommonDataKeys
32+
import com.intellij.openapi.actionSystem.DataContext
33+
import com.intellij.openapi.editor.Editor
34+
import com.intellij.psi.PsiDocumentManager
35+
import com.intellij.psi.PsiFile
36+
import de.halirutan.mathematica.lang.MathematicaLanguage
37+
38+
/**
39+
*
40+
* @author patrick (04.12.17).
41+
*/
42+
open class MathematicaEnterHandler : EnterHandlerDelegateAdapter() {
43+
44+
fun skipWithResultQ(file: PsiFile, editor: Editor, dataContext: DataContext): EnterHandlerDelegate.Result? {
45+
val project = CommonDataKeys.PROJECT.getData(dataContext) ?: return EnterHandlerDelegate.Result.Continue
46+
47+
if (!file.viewProvider.languages.contains(MathematicaLanguage.INSTANCE)) {
48+
return EnterHandlerDelegate.Result.Continue
49+
}
50+
51+
if (editor.isViewer) {
52+
return EnterHandlerDelegate.Result.Continue
53+
}
54+
55+
val document = editor.document
56+
if (!document.isWritable) {
57+
return EnterHandlerDelegate.Result.Continue
58+
}
59+
60+
if (!CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER) {
61+
return EnterHandlerDelegate.Result.Continue
62+
}
63+
64+
PsiDocumentManager.getInstance(project).commitDocument(document)
65+
66+
val caret = editor.caretModel.offset
67+
if (caret == 0) {
68+
return EnterHandlerDelegate.Result.DefaultSkipIndent
69+
}
70+
return if (caret <= 0) {
71+
EnterHandlerDelegate.Result.Continue
72+
} else null
73+
}
74+
75+
}

src/de/halirutan/mathematica/codeinsight/livetemplates/MathematicaTemplateContextType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ protected MathematicaTemplateContextType() {
4949
public boolean isInContext(@NotNull PsiFile file, int offset) {
5050
if (PsiUtilBase.getLanguageAtOffset(file, offset).isKindOf(MathematicaFileType.INSTANCE.getLanguage())) {
5151
PsiElement element = PsiUtilBase.getElementAtOffset(file, offset);
52-
return PsiTreeUtil.getParentOfType(element, PsiComment.class, MString.class) == null;
52+
return !(element instanceof PsiComment) &&
53+
PsiTreeUtil.getParentOfType(element, PsiComment.class, MString.class) == null;
5354
}
5455
return false;
5556
}

0 commit comments

Comments
 (0)