Search in sources :

Example 36 with Editor

use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.

the class SaveContextAction method saveContext.

public static void saveContext(Project project) {
    String initial = null;
    Editor textEditor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    if (textEditor != null) {
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(textEditor.getDocument());
        if (file != null) {
            initial = file.getName();
        }
    }
    String comment = Messages.showInputDialog(project, "Enter comment (optional):", "Save Context", null, initial, null);
    if (comment != null) {
        WorkingContextManager.getInstance(project).saveContext(null, StringUtil.isEmpty(comment) ? null : comment);
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 37 with Editor

use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.

the class BaseIntroduceAction method extractFromExpression.

private void extractFromExpression(Editor e, final XPathExpression expression) {
    final Editor editor = (e instanceof EditorWindow) ? ((EditorWindow) e).getDelegate() : e;
    final HighlightManager highlightManager = HighlightManager.getInstance(expression.getProject());
    final Set<XPathExpression> matchingExpressions = RefactoringUtil.collectMatchingExpressions(expression);
    final List<XmlTag> otherMatches = new ArrayList<>(matchingExpressions.size());
    final ArrayList<RangeHighlighter> highlighters = new ArrayList<>(matchingExpressions.size() + 1);
    if (matchingExpressions.size() > 0) {
        final SelectionModel selectionModel = editor.getSelectionModel();
        highlightManager.addRangeHighlight(editor, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
        for (XPathExpression expr : matchingExpressions) {
            final TextRange range = XsltCodeInsightUtil.getRangeInsideHostingFile(expr);
            highlightManager.addRangeHighlight(editor, range.getStartOffset(), range.getEndOffset(), EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
            final XmlTag tag = PsiTreeUtil.getContextOfType(expr, XmlTag.class, true);
            assert tag != null;
            otherMatches.add(tag);
        }
    }
    final Settings dlg = getSettings(expression, matchingExpressions);
    if (dlg == null || dlg.isCanceled())
        return;
    if (getCommandName() != null) {
        new WriteCommandAction.Simple(e.getProject(), getCommandName()) {

            protected void run() throws Throwable {
                if (extractImpl(expression, matchingExpressions, otherMatches, dlg)) {
                    for (RangeHighlighter highlighter : highlighters) {
                        highlighter.dispose();
                    }
                }
            }
        }.execute();
    } else {
        extractImpl(expression, matchingExpressions, otherMatches, dlg);
    }
}
Also used : XPathExpression(org.intellij.lang.xpath.psi.XPathExpression) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) EditorWindow(com.intellij.injected.editor.EditorWindow) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 38 with Editor

use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.

the class VariableInlineHandler method invoke.

public static void invoke(@NotNull final XPathVariable variable, Editor editor) {
    final String type = LanguageFindUsages.INSTANCE.forLanguage(variable.getLanguage()).getType(variable);
    final Project project = variable.getProject();
    final XmlTag tag = ((XsltElement) variable).getTag();
    final String expression = tag.getAttributeValue("select");
    if (expression == null) {
        CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' has no value.", StringUtil.capitalize(type), variable.getName()), TITLE, null);
        return;
    }
    final Collection<PsiReference> references = ReferencesSearch.search(variable, new LocalSearchScope(tag.getParentTag()), false).findAll();
    if (references.size() == 0) {
        CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' is never used.", variable.getName()), TITLE, null);
        return;
    }
    boolean hasExternalRefs = false;
    if (XsltSupport.isTopLevelElement(tag)) {
        final Query<PsiReference> query = ReferencesSearch.search(variable, GlobalSearchScope.allScope(project), false);
        hasExternalRefs = !query.forEach(new Processor<PsiReference>() {

            int allRefs = 0;

            public boolean process(PsiReference psiReference) {
                if (++allRefs > references.size()) {
                    return false;
                } else if (!references.contains(psiReference)) {
                    return false;
                }
                return true;
            }
        });
    }
    final HighlightManager highlighter = HighlightManager.getInstance(project);
    final ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
    final PsiReference[] psiReferences = references.toArray(new PsiReference[references.size()]);
    TextRange[] ranges = ContainerUtil.map2Array(psiReferences, TextRange.class, s -> {
        final PsiElement psiElement = s.getElement();
        final XmlAttributeValue context = PsiTreeUtil.getContextOfType(psiElement, XmlAttributeValue.class, true);
        if (psiElement instanceof XPathElement && context != null) {
            return XsltCodeInsightUtil.getRangeInsideHostingFile((XPathElement) psiElement).cutOut(s.getRangeInElement());
        }
        return psiElement.getTextRange().cutOut(s.getRangeInElement());
    });
    final Editor e = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
    for (TextRange range : ranges) {
        final TextAttributes textAttributes = EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes();
        final Color color = getScrollmarkColor(textAttributes);
        highlighter.addOccurrenceHighlight(e, range.getStartOffset(), range.getEndOffset(), textAttributes, HighlightManagerImpl.HIDE_BY_ESCAPE, highlighters, color);
    }
    highlighter.addOccurrenceHighlights(e, new PsiElement[] { ((XsltVariable) variable).getNameIdentifier() }, EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
    if (!hasExternalRefs) {
        if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} occurrence{3})", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getQuestionIcon()) != Messages.YES) {
            return;
        }
    } else {
        if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} local occurrence{3})\n" + "\nWarning: It is being used in external files. Its declaration will not be removed.", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getWarningIcon()) != Messages.YES) {
            return;
        }
    }
    final boolean hasRefs = hasExternalRefs;
    new WriteCommandAction.Simple(project, "XSLT.Inline", tag.getContainingFile()) {

        @Override
        protected void run() throws Throwable {
            try {
                for (PsiReference psiReference : references) {
                    final PsiElement element = psiReference.getElement();
                    if (element instanceof XPathElement) {
                        final XPathElement newExpr = XPathChangeUtil.createExpression(element, expression);
                        element.replace(newExpr);
                    } else {
                        assert false;
                    }
                }
                if (!hasRefs) {
                    tag.delete();
                }
            } catch (IncorrectOperationException e) {
                Logger.getInstance(VariableInlineHandler.class.getName()).error(e);
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) ArrayList(java.util.ArrayList) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiElement(com.intellij.psi.PsiElement) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XPathElement(org.intellij.lang.xpath.psi.XPathElement) EditorWindow(com.intellij.injected.editor.EditorWindow) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 39 with Editor

use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.

the class AbstractFix method createQuickFix.

@Nullable
public LocalQuickFix createQuickFix(boolean isOnTheFly) {
    final boolean requiresEditor = requiresEditor();
    if (requiresEditor && !isOnTheFly)
        return null;
    return new LocalQuickFix() {

        @NotNull
        public String getName() {
            return AbstractFix.this.getText();
        }

        @NotNull
        public String getFamilyName() {
            return AbstractFix.this.getFamilyName();
        }

        public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
            Editor editor;
            if (requiresEditor) {
                final DataContext dataContext = DataManager.getInstance().getDataContext();
                editor = CommonDataKeys.EDITOR.getData(dataContext);
                if (editor == null) {
                    if ((editor = FileEditorManager.getInstance(project).getSelectedTextEditor()) == null) {
                        return;
                    }
                }
            } else {
                editor = null;
            }
            final PsiFile psiFile = descriptor.getPsiElement().getContainingFile();
            if (!isAvailable(project, editor, psiFile)) {
                return;
            }
            invoke(project, editor, psiFile);
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with Editor

use of com.intellij.openapi.editor.Editor in project intellij-community by JetBrains.

the class PyClassInsertHandler method handleInsert.

public void handleInsert(InsertionContext context, LookupElement item) {
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    if (context.getCompletionChar() == '(') {
        context.setAddCompletionChar(false);
        final int offset = context.getTailOffset();
        document.insertString(offset, "()");
        PyClass pyClass = PyUtil.as(item.getPsiElement(), PyClass.class);
        PyFunction init = pyClass != null ? pyClass.findInitOrNew(true, null) : null;
        if (init != null && PyFunctionInsertHandler.hasParams(context, init)) {
            editor.getCaretModel().moveToOffset(offset + 1);
            AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(context.getEditor(), init);
        } else {
            editor.getCaretModel().moveToOffset(offset + 2);
        }
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Aggregations

Editor (com.intellij.openapi.editor.Editor)748 Project (com.intellij.openapi.project.Project)281 PsiFile (com.intellij.psi.PsiFile)171 VirtualFile (com.intellij.openapi.vfs.VirtualFile)122 NotNull (org.jetbrains.annotations.NotNull)110 Document (com.intellij.openapi.editor.Document)108 PsiElement (com.intellij.psi.PsiElement)107 Nullable (org.jetbrains.annotations.Nullable)103 TextRange (com.intellij.openapi.util.TextRange)77 FileEditor (com.intellij.openapi.fileEditor.FileEditor)67 TextEditor (com.intellij.openapi.fileEditor.TextEditor)48 ArrayList (java.util.ArrayList)39 IncorrectOperationException (com.intellij.util.IncorrectOperationException)36 List (java.util.List)36 EditorEx (com.intellij.openapi.editor.ex.EditorEx)35 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)29 DataContext (com.intellij.openapi.actionSystem.DataContext)27 ApplicationManager (com.intellij.openapi.application.ApplicationManager)25 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)25 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)22