com.intellij.openapi.command.undo.UndoUtil Java Examples

The following examples show how to use com.intellij.openapi.command.undo.UndoUtil. 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: PromptConsole.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
PromptConsole(@NotNull Project project, ConsoleViewImpl consoleView) {
    m_consoleView = consoleView;

    EditorFactory editorFactory = EditorFactory.getInstance();
    PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);

    Document outputDocument = ((EditorFactoryImpl) editorFactory).createDocument(true);
    UndoUtil.disableUndoFor(outputDocument);
    m_outputEditor = (EditorImpl) editorFactory.createViewer(outputDocument, project, CONSOLE);

    PsiFile file = fileFactory.createFileFromText("PromptConsoleDocument.ml", OclLanguage.INSTANCE, "");
    Document promptDocument = file.getViewProvider().getDocument();
    m_promptEditor = (EditorImpl) editorFactory.createEditor(promptDocument, project, CONSOLE);

    setupOutputEditor();
    setupPromptEditor();

    m_consoleView.print("(* ctrl+enter to send a command, ctrl+up/down to cycle through history *)\r\n", USER_INPUT);

    m_mainPanel.add(m_outputEditor.getComponent(), BorderLayout.CENTER);
    m_mainPanel.add(m_promptEditor.getComponent(), BorderLayout.SOUTH);
}
 
Example #2
Source File: ConsoleViewImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private EditorEx createConsoleEditor() {
  return ReadAction.compute(() -> {
    EditorEx editor = doCreateConsoleEditor();
    LOG.assertTrue(UndoUtil.isUndoDisabledFor(editor.getDocument()));
    editor.installPopupHandler(new ContextMenuPopupHandler() {
      @Override
      public ActionGroup getActionGroup(@Nonnull EditorMouseEvent event) {
        return getPopupGroup(event.getMouseEvent());
      }
    });

    int bufferSize = ConsoleBuffer.useCycleBuffer() ? ConsoleBuffer.getCycleBufferSize() : 0;
    editor.getDocument().setCyclicBufferSize(bufferSize);

    editor.putUserData(CONSOLE_VIEW_IN_EDITOR_VIEW, this);

    editor.getSettings().setAllowSingleLogicalLineFolding(true); // We want to fold long soft-wrapped command lines

    return editor;
  });
}
 
Example #3
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static EditorEx createPreviewComponent(Project project, FileType languageFileType) {
  Document document = EditorFactory.getInstance().createDocument("");
  UndoUtil.disableUndoFor(document);
  EditorEx previewEditor = (EditorEx)EditorFactory.getInstance().createEditor(document, project, languageFileType, true);
  previewEditor.setOneLineMode(true);
  final EditorSettings settings = previewEditor.getSettings();
  settings.setAdditionalLinesCount(0);
  settings.setAdditionalColumnsCount(1);
  settings.setRightMarginShown(false);
  settings.setFoldingOutlineShown(false);
  settings.setLineNumbersShown(false);
  settings.setLineMarkerAreaShown(false);
  settings.setIndentGuidesShown(false);
  settings.setVirtualSpace(false);
  previewEditor.setHorizontalScrollbarVisible(false);
  previewEditor.setVerticalScrollbarVisible(false);
  previewEditor.setCaretEnabled(false);
  settings.setLineCursorWidth(1);

  final Color bg = previewEditor.getColorsScheme().getColor(EditorColors.CARET_ROW_COLOR);
  previewEditor.setBackgroundColor(bg);
  previewEditor.setBorder(BorderFactory.createCompoundBorder(new DottedBorder(Color.gray), new LineBorder(bg, 2)));

  return previewEditor;
}
 
Example #4
Source File: SupressAddShebangInspectionQuickfix.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiFile file = descriptor.getPsiElement().getContainingFile();
    if (file == null) {
        return;
    }

    if (!FileModificationService.getInstance().preparePsiElementForWrite(file)) {
        return;
    }

    PsiComment suppressionComment = SupressionUtil.createSuppressionComment(project, inspectionId);

    PsiElement firstChild = file.getFirstChild();
    PsiElement inserted;
    if (firstChild != null) {
        inserted = file.addBefore(suppressionComment, firstChild);
    } else {
        inserted = file.add(suppressionComment);
    }

    if (inserted != null) {
        file.addAfter(BashPsiElementFactory.createNewline(project), inserted);
    }

    UndoUtil.markPsiFileForUndo(file);
}
 
Example #5
Source File: BaseLombokHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
  if (file.isWritable()) {
    PsiClass psiClass = OverrideImplementUtil.getContextClass(project, editor, file, false);
    if (null != psiClass) {
      processClass(psiClass);

      UndoUtil.markPsiFileForUndo(file);
    }
  }
}
 
Example #6
Source File: ChangeAnnotationParameterQuickFix.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
  final PsiAnnotation myAnnotation = (PsiAnnotation) startElement;
  final Editor editor = CodeInsightUtil.positionCursor(project, psiFile, myAnnotation);
  if (editor != null) {
    WriteCommandAction.writeCommandAction(project, psiFile).run(() ->
      {
        final PsiNameValuePair valuePair = selectAnnotationAttribute(myAnnotation);

        if (null != valuePair) {
          // delete this parameter
          valuePair.delete();
        }

        if (null != myNewValue) {
          //add new parameter
          final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myAnnotation.getProject());
          PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText("@" + myAnnotation.getQualifiedName() + "(" + myName + "=" + myNewValue + ")", myAnnotation.getContext());
          final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();

          myAnnotation.setDeclaredAttributeValue(attributes[0].getName(), attributes[0].getValue());
        }

        UndoUtil.markPsiFileForUndo(psiFile);
      }
    );
  }
}
 
Example #7
Source File: CreateFieldQuickFix.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
  final PsiClass myClass = (PsiClass) startElement;
  final Editor editor = CodeInsightUtil.positionCursor(project, psiFile, myClass.getLBrace());
  if (editor != null) {
    WriteCommandAction.writeCommandAction(project, psiFile).run(() ->
      {
        final PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(project);
        final PsiField psiField = psiElementFactory.createField(myName, myType);

        final PsiModifierList modifierList = psiField.getModifierList();
        if (null != modifierList) {
          for (String modifier : myModifiers) {
            modifierList.setModifierProperty(modifier, true);
          }
        }
        if (null != myInitializerText) {
          PsiExpression psiInitializer = psiElementFactory.createExpressionFromText(myInitializerText, psiField);
          psiField.setInitializer(psiInitializer);
        }

        final List<PsiGenerationInfo<PsiField>> generationInfos = GenerateMembersUtil.insertMembersAtOffset(myClass.getContainingFile(), editor.getCaretModel().getOffset(),
          Collections.singletonList(new PsiGenerationInfo<>(psiField)));
        if (!generationInfos.isEmpty()) {
          PsiField psiMember = generationInfos.iterator().next().getPsiMember();
          editor.getCaretModel().moveToOffset(psiMember.getTextRange().getEndOffset());
        }

        UndoUtil.markPsiFileForUndo(psiFile);
      }
    );
  }
}
 
Example #8
Source File: QuickFixWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
  //if (!CodeInsightUtil.prepareFileForWrite(file)) return;
  // consider all local quick fixes do it themselves

  final PsiElement element = myDescriptor.getPsiElement();
  final PsiFile fileForUndo = element == null ? null : element.getContainingFile();
  LocalQuickFix fix = getFix();
  fix.applyFix(project, myDescriptor);
  DaemonCodeAnalyzer.getInstance(project).restart();
  if (fileForUndo != null && !fileForUndo.equals(file)) {
    UndoUtil.markPsiFileForUndo(fileForUndo);
  }
}
 
Example #9
Source File: AbstractBatchSuppressByNoInspectionCommentFix.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void invoke(@Nonnull final Project project, @Nonnull final PsiElement element) throws IncorrectOperationException {
  if (!isAvailable(project, element)) return;
  PsiElement container = getContainer(element);
  if (container == null) return;

  if (!FileModificationService.getInstance().preparePsiElementForWrite(container)) return;

  if (replaceSuppressionComments(container)) return;

  createSuppression(project, element, container);
  UndoUtil.markPsiFileForUndo(element.getContainingFile());
}
 
Example #10
Source File: ConsoleViewUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static EditorEx setupConsoleEditor(Project project, final boolean foldingOutlineShown, final boolean lineMarkerAreaShown) {
  EditorFactory editorFactory = EditorFactory.getInstance();
  Document document = ((EditorFactoryImpl)editorFactory).createDocument(true);
  UndoUtil.disableUndoFor(document);
  EditorEx editor = (EditorEx)editorFactory.createViewer(document, project, EditorKind.CONSOLE);
  setupConsoleEditor(editor, foldingOutlineShown, lineMarkerAreaShown);
  return editor;
}
 
Example #11
Source File: AbstractSuppressByNoInspectionCommentFix.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@Nonnull final Project project, @Nullable Editor editor, @Nonnull final PsiElement element) throws IncorrectOperationException {
  PsiElement container = getContainer(element);
  if (container == null) return;

  if (!FileModificationService.getInstance().preparePsiElementForWrite(container)) return;

  final List<? extends PsiElement> comments = getCommentsFor(container);
  if (comments != null) {
    for (PsiElement comment : comments) {
      if (comment instanceof PsiComment && SuppressionUtil.isSuppressionComment(comment)) {
        replaceSuppressionComment(comment);
        return;
      }
    }
  }

  boolean caretWasBeforeStatement = editor != null && editor.getCaretModel().getOffset() == container.getTextRange().getStartOffset();
  try {
    createSuppression(project, element, container);
  }
  catch (IncorrectOperationException e) {
    if (!ApplicationManager.getApplication().isUnitTestMode() && editor != null) {
      Messages.showErrorDialog(editor.getComponent(),
                               InspectionsBundle.message("suppress.inspection.annotation.syntax.error", e.getMessage()));
    }
  }

  if (caretWasBeforeStatement) {
    editor.getCaretModel().moveToOffset(container.getTextRange().getStartOffset());
  }
  UndoUtil.markPsiFileForUndo(element.getContainingFile());
}
 
Example #12
Source File: LanguageConsoleImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public LanguageConsoleImpl(@Nonnull Helper helper) {
  super(helper.project, GlobalSearchScope.allScope(helper.project), true, true);
  myHelper = helper;
  EditorFactory editorFactory = EditorFactory.getInstance();
  myConsoleExecutionEditor = new ConsoleExecutionEditor(helper);
  Disposer.register(this, myConsoleExecutionEditor);
  Document historyDocument = ((EditorFactoryImpl)editorFactory).createDocument(true);
  UndoUtil.disableUndoFor(historyDocument);
  myHistoryViewer = (EditorEx)editorFactory.createViewer(historyDocument, getProject(), EditorKind.CONSOLE);
  myHistoryViewer.getDocument().addDocumentListener(myDocumentAdapter);
  myConsoleExecutionEditor.getDocument().addDocumentListener(myDocumentAdapter);
  myScrollBar.setModel(new MyModel(myScrollBar, myHistoryViewer, myConsoleExecutionEditor.getEditor()));
  myScrollBar.putClientProperty(Alignment.class, Alignment.BOTTOM);
}
 
Example #13
Source File: DelombokHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void finish(Project project, PsiFile psiFile) {
  JavaCodeStyleManager.getInstance(project).optimizeImports(psiFile);
  UndoUtil.markPsiFileForUndo(psiFile);
}