Search in sources :

Example 1 with SurroundDescriptor

use of com.intellij.lang.surroundWith.SurroundDescriptor in project intellij-plugins by JetBrains.

the class CfmlSurroundWithTest method doTestNoSurround.

private void doTestNoSurround() {
    myFixture.configureByFile(Util.getInputDataFileName(getTestName(true)));
    List<SurroundDescriptor> surroundDescriptors = new ArrayList<>();
    surroundDescriptors.addAll(LanguageSurrounders.INSTANCE.allForLanguage(CfmlLanguage.INSTANCE));
    for (SurroundDescriptor descriptor : surroundDescriptors) {
        assertEquals(descriptor.getElementsToSurround(myFixture.getFile(), myFixture.getEditor().getSelectionModel().getSelectionStart(), myFixture.getEditor().getSelectionModel().getSelectionEnd()), PsiElement.EMPTY_ARRAY);
    }
}
Also used : ArrayList(java.util.ArrayList) SurroundDescriptor(com.intellij.lang.surroundWith.SurroundDescriptor)

Example 2 with SurroundDescriptor

use of com.intellij.lang.surroundWith.SurroundDescriptor 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 3 with SurroundDescriptor

use of com.intellij.lang.surroundWith.SurroundDescriptor 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 4 with SurroundDescriptor

use of com.intellij.lang.surroundWith.SurroundDescriptor in project intellij-community by JetBrains.

the class SurroundWithHandler method buildSurroundActions.

@Nullable
public static List<AnAction> buildSurroundActions(final Project project, final Editor editor, PsiFile file, @Nullable Surrounder surrounder) {
    SelectionModel selectionModel = editor.getSelectionModel();
    boolean hasSelection = selectionModel.hasSelection();
    if (!hasSelection) {
        selectionModel.selectLineAtCaret();
    }
    int startOffset = selectionModel.getSelectionStart();
    int endOffset = selectionModel.getSelectionEnd();
    PsiElement element1 = file.findElementAt(startOffset);
    PsiElement element2 = file.findElementAt(endOffset - 1);
    if (element1 == null || element2 == null)
        return null;
    TextRange textRange = new TextRange(startOffset, endOffset);
    for (SurroundWithRangeAdjuster adjuster : Extensions.getExtensions(SurroundWithRangeAdjuster.EP_NAME)) {
        textRange = adjuster.adjustSurroundWithRange(file, textRange, hasSelection);
        if (textRange == null)
            return null;
    }
    startOffset = textRange.getStartOffset();
    endOffset = textRange.getEndOffset();
    element1 = file.findElementAt(startOffset);
    final Language baseLanguage = file.getViewProvider().getBaseLanguage();
    assert element1 != null;
    final Language l = element1.getParent().getLanguage();
    List<SurroundDescriptor> surroundDescriptors = new ArrayList<>();
    surroundDescriptors.addAll(LanguageSurrounders.INSTANCE.allForLanguage(l));
    if (l != baseLanguage)
        surroundDescriptors.addAll(LanguageSurrounders.INSTANCE.allForLanguage(baseLanguage));
    surroundDescriptors.add(CustomFoldingSurroundDescriptor.INSTANCE);
    int exclusiveCount = 0;
    List<SurroundDescriptor> exclusiveSurroundDescriptors = new ArrayList<>();
    for (SurroundDescriptor sd : surroundDescriptors) {
        if (sd.isExclusive()) {
            exclusiveCount++;
            exclusiveSurroundDescriptors.add(sd);
        }
    }
    if (exclusiveCount > 0) {
        surroundDescriptors = exclusiveSurroundDescriptors;
    }
    if (surrounder != null) {
        invokeSurrounderInTests(project, editor, file, surrounder, startOffset, endOffset, surroundDescriptors);
        return null;
    }
    Map<Surrounder, PsiElement[]> surrounders = ContainerUtil.newLinkedHashMap();
    for (SurroundDescriptor descriptor : surroundDescriptors) {
        final PsiElement[] elements = descriptor.getElementsToSurround(file, startOffset, endOffset);
        if (elements.length > 0) {
            for (PsiElement element : elements) {
                assert element != null : "descriptor " + descriptor + " returned null element";
                assert element.isValid() : descriptor;
            }
            for (Surrounder s : descriptor.getSurrounders()) {
                surrounders.put(s, elements);
            }
        }
    }
    return doBuildSurroundActions(project, editor, file, surrounders);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) SurroundDescriptor(com.intellij.lang.surroundWith.SurroundDescriptor) CustomFoldingSurroundDescriptor(com.intellij.lang.folding.CustomFoldingSurroundDescriptor) Language(com.intellij.lang.Language) Surrounder(com.intellij.lang.surroundWith.Surrounder) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with SurroundDescriptor

use of com.intellij.lang.surroundWith.SurroundDescriptor in project kotlin by JetBrains.

the class AbstractSurroundWithTest method getElementsToSurround.

@Nullable
private PsiElement[] getElementsToSurround(@NotNull Surrounder surrounder) {
    List<SurroundDescriptor> surroundDescriptors = LanguageSurrounders.INSTANCE.allForLanguage(getFile().getViewProvider().getBaseLanguage());
    String surrounderDescription = surrounder.getTemplateDescription();
    for (SurroundDescriptor descriptor : surroundDescriptors) {
        Surrounder[] surrounders = descriptor.getSurrounders();
        for (Surrounder surrounderInDescriptor : surrounders) {
            if (surrounderInDescriptor.getTemplateDescription().equals(surrounderDescription)) {
                SelectionModel selection = getEditor().getSelectionModel();
                PsiElement[] elements = descriptor.getElementsToSurround(getFile(), selection.getSelectionStart(), selection.getSelectionEnd());
                return elements;
            }
        }
    }
    return null;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) SurroundDescriptor(com.intellij.lang.surroundWith.SurroundDescriptor) Surrounder(com.intellij.lang.surroundWith.Surrounder) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

SurroundDescriptor (com.intellij.lang.surroundWith.SurroundDescriptor)5 PsiElement (com.intellij.psi.PsiElement)4 SelectionModel (com.intellij.openapi.editor.SelectionModel)3 Surrounder (com.intellij.lang.surroundWith.Surrounder)2 Nullable (org.jetbrains.annotations.Nullable)2 Language (com.intellij.lang.Language)1 CustomFoldingSurroundDescriptor (com.intellij.lang.folding.CustomFoldingSurroundDescriptor)1 TextRange (com.intellij.openapi.util.TextRange)1 ArrayList (java.util.ArrayList)1