Search in sources :

Example 76 with Editor

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

the class ExtendedTagInsertHandler method handleInsert.

@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
    final XmlFile contextFile = (XmlFile) context.getFile();
    final XmlExtension extension = XmlExtension.getExtension(contextFile);
    final XmlFile file = extension.getContainingFile(contextFile);
    final Project project = context.getProject();
    assert file != null;
    final PsiElement psiElement = file.findElementAt(context.getStartOffset());
    assert psiElement != null;
    if (isNamespaceBound(psiElement)) {
        doDefault(context, item);
        return;
    }
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final int caretOffset = editor.getCaretModel().getOffset();
    final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
    caretMarker.setGreedyToRight(true);
    final XmlNamespaceHelper.Runner<String, IncorrectOperationException> runAfter = new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {

        @Override
        public void run(final String namespacePrefix) {
            PsiDocumentManager.getInstance(project).commitDocument(document);
            final PsiElement element = file.findElementAt(context.getStartOffset());
            if (element != null) {
                qualifyWithPrefix(namespacePrefix, element, document);
                PsiDocumentManager.getInstance(project).commitDocument(document);
            }
            editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
            PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
            doDefault(context, item);
        }
    };
    try {
        final String prefixByNamespace = getPrefixByNamespace(file, myNamespace);
        if (myNamespacePrefix != null || StringUtil.isEmpty(prefixByNamespace)) {
            final String nsPrefix = myNamespacePrefix == null ? suggestPrefix(file, myNamespace) : myNamespacePrefix;
            XmlNamespaceHelper.getHelper(file).insertNamespaceDeclaration(file, editor, Collections.singleton(myNamespace), nsPrefix, runAfter);
            FeatureUsageTracker.getInstance().triggerFeatureUsed(XmlCompletionContributor.TAG_NAME_COMPLETION_FEATURE);
        } else {
            // qualify && complete
            runAfter.run(prefixByNamespace);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) XmlExtension(com.intellij.xml.XmlExtension) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 77 with Editor

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

the class XmlAttributeInsertHandler method handleInsert.

@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    final int caretOffset = editor.getCaretModel().getOffset();
    final PsiFile file = context.getFile();
    final CharSequence chars = document.getCharsSequence();
    final String quote = getAttributeQuote(HtmlUtil.hasHtml(file) || HtmlUtil.supportsXmlTypedHandlers(file));
    final boolean insertQuotes = WebEditorOptions.getInstance().isInsertQuotesForAttributeValue() && StringUtil.isNotEmpty(quote);
    final boolean hasQuotes = CharArrayUtil.regionMatches(chars, caretOffset, "=\"") || CharArrayUtil.regionMatches(chars, caretOffset, "='");
    if (!hasQuotes) {
        PsiElement fileContext = file.getContext();
        String toInsert = null;
        if (fileContext != null) {
            if (fileContext.getText().startsWith("\""))
                toInsert = "=''";
            if (fileContext.getText().startsWith("\'"))
                toInsert = "=\"\"";
        }
        if (toInsert == null) {
            toInsert = "=" + quote + quote;
        }
        if (!insertQuotes)
            toInsert = "=";
        if (caretOffset >= document.getTextLength() || "/> \n\t\r".indexOf(document.getCharsSequence().charAt(caretOffset)) < 0) {
            document.insertString(caretOffset, toInsert + " ");
        } else {
            document.insertString(caretOffset, toInsert);
        }
        if ('=' == context.getCompletionChar()) {
            // IDEA-19449
            context.setAddCompletionChar(false);
        }
    }
    editor.getCaretModel().moveToOffset(caretOffset + (insertQuotes || hasQuotes ? 2 : 1));
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    editor.getSelectionModel().removeSelection();
    AutoPopupController.getInstance(editor.getProject()).scheduleAutoPopup(editor);
    if (myNamespaceToInsert != null && file instanceof XmlFile) {
        final PsiElement element = file.findElementAt(context.getStartOffset());
        final XmlTag tag = element != null ? PsiTreeUtil.getParentOfType(element, XmlTag.class) : null;
        if (tag != null) {
            String prefix = ExtendedTagInsertHandler.suggestPrefix((XmlFile) file, myNamespaceToInsert);
            if (prefix != null) {
                prefix = makePrefixUnique(prefix, tag);
                final XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(context.getFile());
                if (helper != null) {
                    final Project project = context.getProject();
                    PsiDocumentManager.getInstance(project).commitDocument(document);
                    qualifyWithPrefix(prefix, element);
                    helper.insertNamespaceDeclaration((XmlFile) file, editor, Collections.singleton(myNamespaceToInsert), prefix, null);
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 78 with Editor

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

the class XmlClosingTagInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    Editor editor = context.getEditor();
    Document document = editor.getDocument();
    Project project = context.getProject();
    if (item instanceof LookupElementDecorator) {
        ((LookupElementDecorator) item).getDelegate().handleInsert(context);
    }
    PsiDocumentManager.getInstance(project).commitDocument(document);
    int lineOffset = document.getLineStartOffset(document.getLineNumber(editor.getCaretModel().getOffset()));
    CodeStyleManager.getInstance(project).adjustLineIndent(document, lineOffset);
}
Also used : Project(com.intellij.openapi.project.Project) LookupElementDecorator(com.intellij.codeInsight.lookup.LookupElementDecorator) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Example 79 with Editor

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

the class CreateNSDeclarationIntentionFix method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiFile containingFile = descriptor.getPsiElement().getContainingFile();
    Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    final PsiFile file = editor != null ? PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()) : null;
    if (file == null || !Comparing.equal(file.getVirtualFile(), containingFile.getVirtualFile()))
        return;
    try {
        invoke(project, editor, containingFile);
    } catch (IncorrectOperationException ex) {
        LOG.error(ex);
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

Example 80 with Editor

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

the class IpnbPyReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    final List<Object> variants = Lists.newArrayList();
    Collections.addAll(variants, super.getVariants());
    PsiFile file = myElement.getContainingFile();
    if (file instanceof IpnbPyFragment) {
        final IpnbFilePanel panel = ((IpnbPyFragment) file).getFilePanel();
        final List<IpnbEditablePanel> panels = panel.getIpnbPanels();
        for (IpnbEditablePanel editablePanel : panels) {
            if (!(editablePanel instanceof IpnbCodePanel))
                continue;
            final Editor editor = ((IpnbCodePanel) editablePanel).getEditor();
            final IpnbPyFragment psiFile = (IpnbPyFragment) PsiDocumentManager.getInstance(myElement.getProject()).getPsiFile(editor.getDocument());
            if (psiFile == null)
                continue;
            final CompletionVariantsProcessor processor = new CompletionVariantsProcessor(myElement);
            PyResolveUtil.scopeCrawlUp(processor, psiFile, null, null);
            variants.addAll(getOriginalElements(processor));
        }
    }
    return variants.toArray();
}
Also used : IpnbFilePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel) PsiFile(com.intellij.psi.PsiFile) IpnbCodePanel(org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel) Editor(com.intellij.openapi.editor.Editor) IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel) NotNull(org.jetbrains.annotations.NotNull)

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