Search in sources :

Example 1 with Surrounder

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

the class PySurroundWithTest method doTestSurroundWithCustomFoldingRegion.

private void doTestSurroundWithCustomFoldingRegion() throws Exception {
    final Surrounder surrounder = ContainerUtil.find(CustomFoldingSurroundDescriptor.SURROUNDERS, new Condition<Surrounder>() {

        @Override
        public boolean value(Surrounder surrounder) {
            return surrounder.getTemplateDescription().contains("<editor-fold");
        }
    });
    assertNotNull(surrounder);
    doTest(surrounder);
}
Also used : PyWithWhileSurrounder(com.jetbrains.python.refactoring.surround.surrounders.statements.PyWithWhileSurrounder) Surrounder(com.intellij.lang.surroundWith.Surrounder) PyWithIfSurrounder(com.jetbrains.python.refactoring.surround.surrounders.statements.PyWithIfSurrounder) PyWithTryExceptSurrounder(com.jetbrains.python.refactoring.surround.surrounders.statements.PyWithTryExceptSurrounder)

Example 2 with Surrounder

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

the class SurroundWithHandler method doBuildSurroundActions.

@Nullable
private static List<AnAction> doBuildSurroundActions(Project project, Editor editor, PsiFile file, Map<Surrounder, PsiElement[]> surrounders) {
    final List<AnAction> applicable = new ArrayList<>();
    boolean hasEnabledSurrounders = false;
    Set<Character> usedMnemonicsSet = new HashSet<>();
    int index = 0;
    for (Map.Entry<Surrounder, PsiElement[]> entry : surrounders.entrySet()) {
        Surrounder surrounder = entry.getKey();
        PsiElement[] elements = entry.getValue();
        if (surrounder.isApplicable(elements)) {
            char mnemonic;
            if (index < 9) {
                mnemonic = (char) ('0' + index + 1);
            } else if (index == 9) {
                mnemonic = '0';
            } else {
                mnemonic = (char) ('A' + index - 10);
            }
            index++;
            usedMnemonicsSet.add(Character.toUpperCase(mnemonic));
            applicable.add(new InvokeSurrounderAction(surrounder, project, editor, elements, mnemonic));
            hasEnabledSurrounders = true;
        }
    }
    List<CustomLiveTemplate> customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true);
    List<TemplateImpl> templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true);
    if (!templates.isEmpty() || !customTemplates.isEmpty()) {
        applicable.add(new Separator("Live templates"));
    }
    for (TemplateImpl template : templates) {
        applicable.add(new InvokeTemplateAction(template, editor, project, usedMnemonicsSet));
        hasEnabledSurrounders = true;
    }
    for (CustomLiveTemplate customTemplate : customTemplates) {
        applicable.add(new WrapWithCustomTemplateAction(customTemplate, editor, file, usedMnemonicsSet));
        hasEnabledSurrounders = true;
    }
    if (!templates.isEmpty() || !customTemplates.isEmpty()) {
        applicable.add(Separator.getInstance());
        applicable.add(new ConfigureTemplatesAction());
    }
    return hasEnabledSurrounders ? applicable : null;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) CustomLiveTemplate(com.intellij.codeInsight.template.CustomLiveTemplate) InvokeTemplateAction(com.intellij.codeInsight.template.impl.InvokeTemplateAction) WrapWithCustomTemplateAction(com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction) Surrounder(com.intellij.lang.surroundWith.Surrounder) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Surrounder

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

use of com.intellij.lang.surroundWith.Surrounder 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

Surrounder (com.intellij.lang.surroundWith.Surrounder)4 PsiElement (com.intellij.psi.PsiElement)3 Nullable (org.jetbrains.annotations.Nullable)3 SurroundDescriptor (com.intellij.lang.surroundWith.SurroundDescriptor)2 CustomLiveTemplate (com.intellij.codeInsight.template.CustomLiveTemplate)1 InvokeTemplateAction (com.intellij.codeInsight.template.impl.InvokeTemplateAction)1 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)1 WrapWithCustomTemplateAction (com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction)1 Language (com.intellij.lang.Language)1 CustomFoldingSurroundDescriptor (com.intellij.lang.folding.CustomFoldingSurroundDescriptor)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 TextRange (com.intellij.openapi.util.TextRange)1 PyWithIfSurrounder (com.jetbrains.python.refactoring.surround.surrounders.statements.PyWithIfSurrounder)1 PyWithTryExceptSurrounder (com.jetbrains.python.refactoring.surround.surrounders.statements.PyWithTryExceptSurrounder)1 PyWithWhileSurrounder (com.jetbrains.python.refactoring.surround.surrounders.statements.PyWithWhileSurrounder)1