com.intellij.lang.surroundWith.Surrounder Java Examples

The following examples show how to use com.intellij.lang.surroundWith.Surrounder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HaxeSurroundTest.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
protected void doTest(final Surrounder surrounder) throws Exception {
  myFixture.configureByFile(getTestName(false) + ".hx");

  WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
    @Override
    public void run() {
      SurroundWithHandler.invoke(getProject(), myFixture.getEditor(), myFixture.getFile(), surrounder);
      PsiDocumentManager.getInstance(getProject()).doPostponedOperationsAndUnblockDocument(myFixture.getDocument(myFixture.getFile()));
      CodeStyleManager.getInstance(myFixture.getProject()).reformat(myFixture.getFile());
    }
  });
    /*CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
        @Override
        public void run() {
            SurroundWithHandler.invoke(getProject(), myFixture.getEditor(), myFixture.getFile(), surrounder);
            PsiDocumentManager.getInstance(getProject()).doPostponedOperationsAndUnblockDocument(myFixture.getDocument(myFixture.getFile()));
            CodeStyleManager.getInstance(myFixture.getProject()).reformat(myFixture.getFile());
        }
    }, null, null);*/

  myFixture.checkResultByFile(getTestName(false) + "_after.hx");
}
 
Example #2
Source File: SurroundWithHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void invokeSurrounderInTests(Project project,
                                            Editor editor,
                                            PsiFile file,
                                            Surrounder surrounder,
                                            int startOffset,
                                            int endOffset, List<SurroundDescriptor> surroundDescriptors) {
  assert ApplicationManager.getApplication().isUnitTestMode();
  for (SurroundDescriptor descriptor : surroundDescriptors) {
    final PsiElement[] elements = descriptor.getElementsToSurround(file, startOffset, endOffset);
    if (elements.length > 0) {
      for (Surrounder descriptorSurrounder : descriptor.getSurrounders()) {
        if (surrounder.getClass().equals(descriptorSurrounder.getClass())) {
          doSurround(project, editor, surrounder, elements);
          return;
        }
      }
    }
  }
}
 
Example #3
Source File: HaxeSurroundDescriptor.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public Surrounder[] getSurrounders() {
  return new Surrounder[]{
    new HaxeIfSurrounder(),
    new HaxeIfElseSurrounder(),
    new HaxeWhileSurrounder(),
    new HaxeDoWhileSurrounder(),
    new HaxeTryCatchSurrounder()
  };
}
 
Example #4
Source File: CodeInsightTestUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void doSurroundWithTest(@Nonnull final CodeInsightTestFixture fixture, @Nonnull final Surrounder surrounder,
                                      @Nonnull final String before, @Nonnull final String after) {
  fixture.configureByFile(before);
  new WriteCommandAction.Simple(fixture.getProject()) {
    @Override
    protected void run() throws Throwable {
      SurroundWithHandler.invoke(fixture.getProject(), fixture.getEditor(), fixture.getFile(), surrounder);
    }
  }.execute();
  fixture.checkResultByFile(after, false);
}
 
Example #5
Source File: SurroundWithHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void invoke(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file, Surrounder surrounder) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (file instanceof PsiCompiledElement) {
    HintManager.getInstance().showErrorHint(editor, "Can't modify decompiled code");
    return;
  }

  List<AnAction> applicable = buildSurroundActions(project, editor, file, surrounder);
  if (applicable != null) {
    showPopup(editor, applicable);
  }
  else if (!ApplicationManager.getApplication().isUnitTestMode()) {
    HintManager.getInstance().showErrorHint(editor, "Couldn't find Surround With variants applicable to the current context");
  }
}
 
Example #6
Source File: SurroundWithHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void doSurround(final Project project, final Editor editor, final Surrounder surrounder, final PsiElement[] elements) {
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
    return;
  }

  try {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    int col = editor.getCaretModel().getLogicalPosition().column;
    int line = editor.getCaretModel().getLogicalPosition().line;
    if (!editor.getCaretModel().supportsMultipleCarets()) {
      LogicalPosition pos = new LogicalPosition(0, 0);
      editor.getCaretModel().moveToLogicalPosition(pos);
    }
    TextRange range = surrounder.surroundElements(project, editor, elements);
    if (range != CARET_IS_OK) {
      if (TemplateManager.getInstance(project).getActiveTemplate(editor) == null &&
          InplaceRefactoring.getActiveInplaceRenamer(editor) == null) {
        LogicalPosition pos1 = new LogicalPosition(line, col);
        editor.getCaretModel().moveToLogicalPosition(pos1);
      }
      if (range != null) {
        int offset = range.getStartOffset();
        editor.getCaretModel().removeSecondaryCarets();
        editor.getCaretModel().moveToOffset(offset);
        editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
        editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
      }
    }
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
}
 
Example #7
Source File: SurroundWithHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public InvokeSurrounderAction(Surrounder surrounder, Project project, Editor editor, PsiElement[] elements, char mnemonic) {
  super(UIUtil.MNEMONIC + String.valueOf(mnemonic) + ". " + surrounder.getTemplateDescription());
  mySurrounder = surrounder;
  myProject = project;
  myEditor = editor;
  myElements = elements;
}
 
Example #8
Source File: PostfixTemplatesUtils.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static TextRange surround(@Nonnull Surrounder surrounder,
                                 @Nonnull Editor editor,
                                 @Nonnull PsiElement expr) {
  Project project = expr.getProject();
  PsiElement[] elements = {expr};
  if (surrounder.isApplicable(elements)) {
    return surrounder.surroundElements(project, editor, elements);
  }
  else {
    showErrorHint(project, editor);
  }
  return null;
}
 
Example #9
Source File: CppSurroundDescriptor.java    From CppTools with Apache License 2.0 5 votes vote down vote up
@NotNull
public Surrounder[] getSurrounders() {
  return new Surrounder[] {
    new IfSurrounder(),
    new ParensSurrounder(),
    new CStyleCastSurrounder(),
    new ConstCastSurrounder(),
    new DynamicCastSurrounder(),
    new ReinterpretCastSurrounder(),
    new StaticCastSurrounder()
  };
}
 
Example #10
Source File: IfElseStatementPostfixTemplate.java    From HakunaMatataIntelliJPlugin with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
protected Surrounder getSurrounder() {
    return new JavaWithIfElseExpressionSurrounder();
}
 
Example #11
Source File: IfElseIfStatementPostfixTemplate.java    From HakunaMatataIntelliJPlugin with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
protected Surrounder getSurrounder() {
    return new JavaWithIfElseIfExpressionSurrounder();
}
 
Example #12
Source File: CSharpExpressionSurroundDescriptor.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Surrounder[] getSurrounders()
{
	return mySurrounders;
}
 
Example #13
Source File: CustomFoldingSurroundDescriptor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Surrounder[] getSurrounders() {
  return SURROUNDERS;
}
 
Example #14
Source File: SurroundWithHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@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();

  PsiDocumentManager.getInstance(project).commitAllDocuments();

  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<SurroundDescriptor>();

  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<SurroundDescriptor>();
  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);
}
 
Example #15
Source File: SurroundWithHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private static List<AnAction> doBuildSurroundActions(Project project,
                                                     Editor editor,
                                                     PsiFile file,
                                                     Map<Surrounder, PsiElement[]> surrounders) {
  final List<AnAction> applicable = new ArrayList<AnAction>();
  boolean hasEnabledSurrounders = false;

  Set<Character> usedMnemonicsSet = new HashSet<Character>();

  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 AnSeparator("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(AnSeparator.getInstance());
    applicable.add(new ConfigureTemplatesAction());
  }
  return hasEnabledSurrounders ? applicable : null;
}
 
Example #16
Source File: SurroundPostfixTemplateBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
protected abstract Surrounder getSurrounder();