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