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);
}
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;
}
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);
}
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;
}
Aggregations