|
| 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.surround; |
| 23 | + |
| 24 | +import com.intellij.codeInsight.generation.surroundWith.SurroundWithRangeAdjuster; |
| 25 | +import com.intellij.openapi.editor.Editor; |
| 26 | +import com.intellij.openapi.fileEditor.FileEditorManager; |
| 27 | +import com.intellij.openapi.project.Project; |
| 28 | +import com.intellij.openapi.util.TextRange; |
| 29 | +import com.intellij.psi.FileViewProvider; |
| 30 | +import com.intellij.psi.PsiElement; |
| 31 | +import com.intellij.psi.PsiFile; |
| 32 | +import com.intellij.psi.PsiWhiteSpace; |
| 33 | +import com.intellij.psi.util.PsiUtilBase; |
| 34 | +import de.halirutan.mathematica.MathematicaLanguage; |
| 35 | +import de.halirutan.mathematica.parsing.psi.api.Expression; |
| 36 | +import org.jetbrains.annotations.Nullable; |
| 37 | + |
| 38 | +/** |
| 39 | + * The range adjuster has two purposes: If a user called surroundWith having an explicit selection, we just remove the |
| 40 | + * whitespace before and after the selection. If the user has not selected anything it is up to us to find a good |
| 41 | + * expression near the caret that is going to be surrounded. For this, we use {@link SurroundExpressionFinder} to inspect |
| 42 | + * the syntax tree and we return the range hopefully a useful expression. |
| 43 | + * |
| 44 | + * @author patrick (04.06.17). |
| 45 | + */ |
| 46 | +public class MathematicaSurroundWithRangeAdjuster implements SurroundWithRangeAdjuster { |
| 47 | + |
| 48 | + @Nullable |
| 49 | + @Override |
| 50 | + public TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange) { |
| 51 | + return adjustSurroundWithRange(file, selectedRange, true); |
| 52 | + } |
| 53 | + |
| 54 | + @Nullable |
| 55 | + @Override |
| 56 | + public TextRange adjustSurroundWithRange(PsiFile file, TextRange selectedRange, boolean hasSelection) { |
| 57 | + int startOffset = selectedRange.getStartOffset(); |
| 58 | + int endOffset = selectedRange.getEndOffset(); |
| 59 | + if (endOffset < startOffset) { |
| 60 | + int tmp = endOffset; |
| 61 | + endOffset = startOffset; |
| 62 | + startOffset = tmp; |
| 63 | + } |
| 64 | + |
| 65 | + if (hasSelection) { |
| 66 | + final FileViewProvider viewProvider = file.getViewProvider(); |
| 67 | + PsiElement element1 = viewProvider.findElementAt(startOffset, MathematicaLanguage.INSTANCE); |
| 68 | + PsiElement element2 = viewProvider.findElementAt(endOffset - 1, MathematicaLanguage.INSTANCE); |
| 69 | + if (element1 instanceof PsiWhiteSpace) { |
| 70 | + startOffset = element1.getTextRange().getEndOffset(); |
| 71 | + element1 = file.findElementAt(startOffset); |
| 72 | + } |
| 73 | + if (element2 instanceof PsiWhiteSpace) { |
| 74 | + endOffset = element2.getTextRange().getStartOffset(); |
| 75 | + element2 = file.findElementAt(endOffset - 1); |
| 76 | + } |
| 77 | + if (element1 != null && element2 != null) { |
| 78 | + final int startOffset1 = element1.getTextRange().getStartOffset(); |
| 79 | + final int endOffset1 = element2.getTextRange().getEndOffset(); |
| 80 | + if (startOffset1 < endOffset1) { |
| 81 | + return TextRange.create(startOffset1, endOffset1); |
| 82 | + } |
| 83 | + } |
| 84 | + return null; |
| 85 | + } else { |
| 86 | + final Project project = file.getProject(); |
| 87 | + final Editor selectedTextEditor = FileEditorManager.getInstance(project).getSelectedTextEditor(); |
| 88 | + if (selectedTextEditor == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + PsiElement element = PsiUtilBase.getElementAtCaret(selectedTextEditor); |
| 93 | + if (element == null) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + SurroundExpressionFinder expressionFinder = new SurroundExpressionFinder(); |
| 98 | + element.accept(expressionFinder); |
| 99 | + PsiElement bestExpression = expressionFinder.getBestExpression(); |
| 100 | + if (bestExpression instanceof Expression) { |
| 101 | + final TextRange textRange = bestExpression.getTextRange(); |
| 102 | + selectedTextEditor.getSelectionModel().setSelection(textRange.getStartOffset(), textRange.getEndOffset()); |
| 103 | + return bestExpression.getTextRange(); |
| 104 | + } |
| 105 | + } |
| 106 | + return null; |
| 107 | + } |
| 108 | +} |
0 commit comments