Skip to content

Commit ef97083

Browse files
committed
Test for the comment-handler if the offset is inside comment element
This fixes #94
1 parent 47ecca4 commit ef97083

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

src/de/halirutan/mathematica/codeinsight/editoractions/enter/CommentStarInsertEnterHandler.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ import com.intellij.psi.PsiComment
3434
import com.intellij.psi.PsiDocumentManager
3535
import com.intellij.psi.PsiFile
3636

37-
/**/
38-
3937
/**
40-
*
38+
* Inserts a * in each new line of a multi-line comment
4139
* @author patrick (04.12.17).
4240
*/
4341
class CommentStarInsertEnterHandler : MathematicaEnterHandler() {
@@ -47,14 +45,18 @@ class CommentStarInsertEnterHandler : MathematicaEnterHandler() {
4745
val offset = caretModel.offset
4846
val project = dataContext.getData(CommonDataKeys.PROJECT) ?: return EnterHandlerDelegate.Result.Continue
4947
val psiDocManager = PsiDocumentManager.getInstance(project)
50-
val element = file.findElementAt(offset) ?: return EnterHandlerDelegate.Result.Continue
51-
if (element is PsiComment) {
48+
49+
val comment = file.findElementAt(offset) ?: return EnterHandlerDelegate.Result.Continue
50+
val commentRange = comment.textRange ?: return EnterHandlerDelegate.Result.Continue
51+
52+
if (comment is PsiComment && offset >= commentRange.startOffset + 2 && offset <= commentRange.endOffset - 2) {
53+
5254
val document = editor.document
5355
val textLength = document.textLength
5456

5557
// The case that we opened a comment with (*|) and therefore the complete file is commented
5658
// We insert the missing *
57-
if (element.textRange.endOffset == textLength && offset < textLength && document.getText(TextRange.create(offset, offset + 1)) == ")") {
59+
if (comment.textRange.endOffset == textLength && offset < textLength && document.getText(TextRange.create(offset, offset + 1)) == ")") {
5860
document.insertString(offset, " * \n *")
5961
caretModel.moveToOffset(offset + 3)
6062
psiDocManager.commitDocument(document)
@@ -63,8 +65,8 @@ class CommentStarInsertEnterHandler : MathematicaEnterHandler() {
6365

6466

6567
val lineNumber = document.getLineNumber(offset)
66-
val elementStartLine = document.getLineNumber(element.textOffset)
67-
val elementEndLine = document.getLineNumber(element.textOffset + element.textLength)
68+
val elementStartLine = document.getLineNumber(comment.textOffset)
69+
val elementEndLine = document.getLineNumber(comment.textOffset + comment.textLength)
6870

6971
val insertString: String
7072
val move: Int
@@ -79,7 +81,7 @@ class CommentStarInsertEnterHandler : MathematicaEnterHandler() {
7981
caretModel.moveToOffset(offset + move)
8082

8183
if (lineNumber == elementEndLine) {
82-
document.insertString(offset + move, "\n ")
84+
document.insertString(commentRange.endOffset + move - 2, "\n ")
8385
}
8486
psiDocManager.commitDocument(document)
8587
return EnterHandlerDelegate.Result.DefaultSkipIndent

src/de/halirutan/mathematica/lang/psi/impl/SymbolImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ public ResolveResult[] multiResolve(boolean incompleteCode) {
179179
final PsiFile containingFile = getContainingFile();
180180
final ResolveResult[] localResult =
181181
myResolveCache.resolveWithCaching(this, LOCAL_SYMBOL_RESOLVER, true, incompleteCode, containingFile);
182-
if (!Arrays.equals(ResolveResult.EMPTY_ARRAY, localResult)) {
182+
if (!Arrays.equals(ResolveResult.EMPTY_ARRAY, localResult) && localResult[0] instanceof SymbolResolveResult) {
183183
cacheScope(localResult[0]);
184184
return localResult;
185185
}
186186

187187
final ResolveResult[] globalResult = GLOBAL_SYMBOL_RESOLVER.resolve(this, containingFile);
188-
if (!Arrays.equals(ResolveResult.EMPTY_ARRAY, globalResult)) {
188+
if (!Arrays.equals(ResolveResult.EMPTY_ARRAY, globalResult) && globalResult[0] instanceof SymbolResolveResult) {
189189
cacheScope(globalResult[0]);
190190
return globalResult;
191191
}

src/de/halirutan/mathematica/settings/MathematicaSettingsConfigurable.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
package de.halirutan.mathematica.settings;
2323

2424
import com.intellij.openapi.options.BaseConfigurable;
25-
import com.intellij.openapi.options.ConfigurationException;
2625
import org.jetbrains.annotations.Nls;
2726
import org.jetbrains.annotations.Nullable;
2827

@@ -56,7 +55,7 @@ public JComponent createComponent() {
5655
}
5756

5857
@Override
59-
public void apply() throws ConfigurationException {
58+
public void apply() {
6059
if (mySettingsUI != null) {
6160
final MathematicaSettings instance = MathematicaSettings.getInstance();
6261
instance.loadState(mySettingsUI.getSettings());

0 commit comments

Comments
 (0)