Java Code Examples for com.intellij.util.DocumentUtil#writeInRunUndoTransparentAction()

The following examples show how to use com.intellij.util.DocumentUtil#writeInRunUndoTransparentAction() . 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: LanguageConsoleImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected String addToHistoryInner(@Nonnull final TextRange textRange, @Nonnull final EditorEx editor, boolean erase, final boolean preserveMarkup) {
  ApplicationManager.getApplication().assertIsDispatchThread();

  String result = addTextRangeToHistory(textRange, editor, preserveMarkup);
  if (erase) {
    DocumentUtil.writeInRunUndoTransparentAction(() -> editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset()));
  }
  // always scroll to end on user input
  scrollToEnd();
  return result;
}
 
Example 2
Source File: InflateLocalVariableAction.java    From idea-android-studio-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {
    DocumentUtil.writeInRunUndoTransparentAction(new Runnable() {
        @Override
        public void run() {
            List<AndroidView> androidViews = AndroidUtils.getIDsFromXML(xmlFile);

            PsiStatement psiStatement = PsiTreeUtil.getParentOfType(psiElement, PsiStatement.class);
            if (psiStatement == null) {
                return;
            }

            PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiStatement.getProject());

            PsiElement[] localVariables = PsiTreeUtil.collectElements(psiStatement.getParent(), new PsiElementFilter() {
                @Override
                public boolean isAccepted(PsiElement element) {
                    return element instanceof PsiLocalVariable;
                }
            });

            Set<String> variables = new HashSet<String>();
            for (PsiElement localVariable : localVariables) {
                variables.add(((PsiLocalVariable) localVariable).getName());
            }

            for (AndroidView v : androidViews) {
                if (!variables.contains(v.getFieldName())) {
                    String sb1;

                    if (variableName != null) {
                        sb1 = String.format("%s %s = (%s) %s.findViewById(%s);", v.getName(), v.getFieldName(), v.getName(), variableName, v.getId());
                    } else {
                        sb1 = String.format("%s %s = (%s) findViewById(%s);", v.getName(), v.getFieldName(), v.getName(), v.getId());
                    }

                    PsiStatement statementFromText = elementFactory.createStatementFromText(sb1, null);
                    psiStatement.getParent().addAfter(statementFromText, psiStatement);
                }
            }

            JavaCodeStyleManager.getInstance(psiStatement.getProject()).shortenClassReferences(psiStatement.getParent());
            new ReformatAndOptimizeImportsProcessor(psiStatement.getProject(), psiStatement.getContainingFile(), true).run();

        }
    });

}
 
Example 3
Source File: InflateThisExpressionAction.java    From idea-android-studio-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {

    DocumentUtil.writeInRunUndoTransparentAction(new Runnable() {
        @Override
        public void run() {
            List<AndroidView> androidViews = AndroidUtils.getIDsFromXML(xmlFile);

            PsiStatement psiStatement = PsiTreeUtil.getParentOfType(psiElement, PsiStatement.class);
            if(psiStatement == null) {
                return;
            }

            // collection class field
            // check if we need to set them
            PsiClass psiClass = PsiTreeUtil.getParentOfType(psiStatement, PsiClass.class);
            Set<String> fieldSet = new HashSet<String>();
            for(PsiField field: psiClass.getFields()) {
                fieldSet.add(field.getName());
            }

            // collect this.foo = "" and (this.)foo = ""
            // collection already init variables
            final Set<String> thisSet = new HashSet<String>();
            PsiTreeUtil.processElements(psiStatement.getParent(), new PsiElementProcessor() {

                @Override
                public boolean execute(@NotNull PsiElement element) {

                    if(element instanceof PsiThisExpression) {
                        attachFieldName(element.getParent());
                    } else if(element instanceof PsiAssignmentExpression) {
                       attachFieldName(((PsiAssignmentExpression) element).getLExpression());
                    }

                    return true;
                }

                private void attachFieldName(PsiElement psiExpression) {

                    if(!(psiExpression instanceof PsiReferenceExpression)) {
                        return;
                    }

                    PsiElement psiField = ((PsiReferenceExpression) psiExpression).resolve();
                    if(psiField instanceof PsiField) {
                        thisSet.add(((PsiField) psiField).getName());
                    }
                }
            });


            PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiStatement.getProject());
            for (AndroidView v: androidViews) {

                if(!fieldSet.contains(v.getFieldName())) {
                    String sb = "private " + v.getName() + " " + v.getFieldName() + ";";
                    psiClass.add(elementFactory.createFieldFromText(sb, psiClass));
                }

                if(!thisSet.contains(v.getFieldName())) {

                    String sb1;
                    if(variableName != null) {
                        sb1 = String.format("this.%s = (%s) %s.findViewById(%s);", v.getFieldName(), v.getName(), variableName, v.getId());
                    } else {
                        sb1 = String.format("this.%s = (%s) findViewById(%s);", v.getFieldName(), v.getName(), v.getId());
                    }


                    PsiStatement statementFromText = elementFactory.createStatementFromText(sb1, null);

                    psiStatement.getParent().addAfter(statementFromText, psiStatement);
                }

            }

            JavaCodeStyleManager.getInstance(psiStatement.getProject()).shortenClassReferences(psiStatement.getParent());
            new ReformatAndOptimizeImportsProcessor(psiStatement.getProject(), psiStatement.getContainingFile(), true).run();

        }
    });

}
 
Example 4
Source File: ConsoleExecutionEditor.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void setInputText(@Nonnull final String query) {
  DocumentUtil.writeInRunUndoTransparentAction(() -> myConsoleEditor.getDocument().setText(StringUtil.convertLineSeparators(query)));
}