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+ }
0 commit comments