Search in sources :

Example 31 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class JavaSurroundWithTest method doTest.

private void doTest(@NotNull String fileName, Surrounder surrounder) {
    configureByFile(BASE_PATH + fileName + ".java");
    SurroundDescriptor item = ContainerUtil.getFirstItem(LanguageSurrounders.INSTANCE.allForLanguage(JavaLanguage.INSTANCE));
    assertNotNull(item);
    SelectionModel selectionModel = getEditor().getSelectionModel();
    PsiElement[] elements = item.getElementsToSurround(getFile(), selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
    assertTrue(surrounder.isApplicable(elements));
    SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);
    checkResultByFile(BASE_PATH + fileName + "_after.java");
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) SurroundDescriptor(com.intellij.lang.surroundWith.SurroundDescriptor) PsiElement(com.intellij.psi.PsiElement)

Example 32 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project intellij-community by JetBrains.

the class JavaSurroundWithTest method testNoParenthesisSurrounderForLambdaParameter.

public void testNoParenthesisSurrounderForLambdaParameter() {
    configureByFile(BASE_PATH + getTestName(false) + ".java");
    SurroundDescriptor item = ContainerUtil.getFirstItem(LanguageSurrounders.INSTANCE.allForLanguage(JavaLanguage.INSTANCE));
    assertNotNull(item);
    SelectionModel selectionModel = getEditor().getSelectionModel();
    PsiElement[] elements = item.getElementsToSurround(getFile(), selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
    assertEmpty(elements);
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) SurroundDescriptor(com.intellij.lang.surroundWith.SurroundDescriptor) PsiElement(com.intellij.psi.PsiElement)

Example 33 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project Intellij-Plugin by getgauge.

the class StepsBuilder method getPsiElements.

protected List<PsiElement> getPsiElements(Class stepClass) {
    SelectionModel selectionModel = editor.getSelectionModel();
    List<PsiElement> specSteps = new ArrayList<>();
    int currentOffset = selectionModel.getSelectionStart();
    while (selectionModel.getSelectionEnd() >= currentOffset) {
        try {
            if (psiFile.getText().charAt(currentOffset++) == '\n')
                continue;
            PsiElement step = getStep(psiFile.findElementAt(currentOffset), stepClass);
            if (step == null)
                return new ArrayList<>();
            specSteps.add(step);
            currentOffset += step.getText().length();
        } catch (Exception ignored) {
        }
    }
    return specSteps;
}
Also used : ArrayList(java.util.ArrayList) SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiElement(com.intellij.psi.PsiElement)

Example 34 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoCodeInsightFixtureTestCase method findElementAtCaretOrInSelection.

@NotNull
protected PsiElement findElementAtCaretOrInSelection() {
    SelectionModel selectionModel = myFixture.getEditor().getSelectionModel();
    if (selectionModel.hasSelection()) {
        PsiElement left = myFixture.getFile().findElementAt(selectionModel.getSelectionStart());
        PsiElement right = myFixture.getFile().findElementAt(selectionModel.getSelectionEnd() - 1);
        assertNotNull(left);
        assertNotNull(right);
        return ObjectUtils.assertNotNull(PsiTreeUtil.findCommonParent(left, right));
    } else {
        return ObjectUtils.assertNotNull(myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset()));
    }
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with SelectionModel

use of com.intellij.openapi.editor.SelectionModel in project buck by facebook.

the class BuckCopyPasteProcessor method preprocessOnPaste.

@Override
public String preprocessOnPaste(Project project, PsiFile psiFile, Editor editor, String text, RawText rawText) {
    if (!(psiFile instanceof BuckFile)) {
        return text;
    }
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final SelectionModel selectionModel = editor.getSelectionModel();
    // Pastes in block selection mode (column mode) are not handled by a CopyPasteProcessor.
    final int selectionStart = selectionModel.getSelectionStart();
    final PsiElement element = psiFile.findElementAt(selectionStart);
    if (element == null) {
        return text;
    }
    if (BuckPsiUtils.hasElementType(element.getNode(), TokenType.WHITE_SPACE, BuckTypes.SINGLE_QUOTED_STRING, BuckTypes.DOUBLE_QUOTED_STRING)) {
        PsiElement property = BuckPsiUtils.findAncestorWithType(element, BuckTypes.PROPERTY);
        if (checkPropertyName(property)) {
            return formatPasteText(text, element, project);
        }
    }
    return text;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) BuckFile(com.facebook.buck.intellij.ideabuck.lang.BuckFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Aggregations

SelectionModel (com.intellij.openapi.editor.SelectionModel)76 TextRange (com.intellij.openapi.util.TextRange)21 Document (com.intellij.openapi.editor.Document)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)16 Editor (com.intellij.openapi.editor.Editor)14 Nullable (org.jetbrains.annotations.Nullable)11 CaretModel (com.intellij.openapi.editor.CaretModel)10 PsiFile (com.intellij.psi.PsiFile)8 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)3 SurroundDescriptor (com.intellij.lang.surroundWith.SurroundDescriptor)3 Pass (com.intellij.openapi.util.Pass)3 List (java.util.List)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 EditorWindow (com.intellij.injected.editor.EditorWindow)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2