Skip to content

Commit

Permalink
Fix bug that prevents other surrounders to act
Browse files Browse the repository at this point in the history
Subtle issue: I returned null on my surround-with range adjuster which
would prevent the whole surround framework to test and apply other surrounders.

This fixes #85
  • Loading branch information
halirutan committed Dec 27, 2017
1 parent 4d77518 commit caebd77
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public Result preprocessEnter(@NotNull final PsiFile file,
if (project != null) {
CodeStyleManager.getInstance(project).adjustLineIndent(editor.getDocument(), caretOffset.get() + 1);
}
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
return Result.DefaultForceIndent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import de.halirutan.mathematica.lang.psi.api.MathematicaPsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -42,7 +43,7 @@ abstract class AbstractSurrounder implements Surrounder {

@Override
public boolean isApplicable(@NotNull PsiElement[] elements) {
return true;
return elements.length > 0 && elements[0].getContainingFile() instanceof MathematicaPsiFile;
}

protected abstract String getOpening();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import de.halirutan.mathematica.lang.psi.api.MathematicaPsiFile;
import org.jetbrains.annotations.NotNull;

/**
Expand Down Expand Up @@ -54,7 +55,10 @@ public boolean isExclusive() {
@NotNull
@Override
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
return findElementsInRange(file, startOffset, endOffset);
if (file instanceof MathematicaPsiFile) {
return findElementsInRange(file, startOffset, endOffset);
}
return PsiElement.EMPTY_ARRAY;
}

private PsiElement[] findElementsInRange(PsiFile file, int startOffset, int endOffset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.intellij.psi.util.PsiUtilBase;
import de.halirutan.mathematica.lang.MathematicaLanguage;
import de.halirutan.mathematica.lang.psi.api.Expression;
import de.halirutan.mathematica.lang.psi.api.MathematicaPsiFile;
import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -48,12 +49,18 @@ public class MathematicaSurroundWithRangeAdjuster implements SurroundWithRangeAd
@Nullable
@Override
public TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange) {
return adjustSurroundWithRange(file, selectedRange, true);
if (file instanceof MathematicaPsiFile) {
return adjustSurroundWithRange(file, selectedRange, true);
}
return selectedRange;
}

@Nullable
@Override
public TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange, boolean hasSelection) {
if (!(file instanceof MathematicaPsiFile)) {
return selectedRange;
}
int startOffset = selectedRange.getStartOffset();
int endOffset = selectedRange.getEndOffset();
if (endOffset < startOffset) {
Expand Down Expand Up @@ -103,6 +110,6 @@ public TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange,
return bestExpression.getTextRange();
}
}
return null;
return selectedRange;
}
}

0 comments on commit caebd77

Please sign in to comment.