Search in sources :

Example 21 with Document

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

the class PyConsoleUtil method createTabCompletionAction.

public static AnAction createTabCompletionAction(PythonConsoleView consoleView) {
    final AnAction runCompletions = new AnAction() {

        @Override
        public void actionPerformed(AnActionEvent e) {
            Editor editor = consoleView.getConsoleEditor();
            if (LookupManager.getActiveLookup(editor) != null) {
                AnAction replace = ActionManager.getInstance().getAction(IdeActions.ACTION_CHOOSE_LOOKUP_ITEM_REPLACE);
                ActionUtil.performActionDumbAware(replace, e);
                return;
            }
            AnAction completionAction = ActionManager.getInstance().getAction(IdeActions.ACTION_CODE_COMPLETION);
            if (completionAction != null) {
                ActionUtil.performActionDumbAware(completionAction, e);
            }
        }

        @Override
        public void update(AnActionEvent e) {
            Editor editor = consoleView.getConsoleEditor();
            if (LookupManager.getActiveLookup(editor) != null) {
                e.getPresentation().setEnabled(false);
            }
            int offset = editor.getCaretModel().getOffset();
            Document document = editor.getDocument();
            int lineStart = document.getLineStartOffset(document.getLineNumber(offset));
            String textToCursor = document.getText(new TextRange(lineStart, offset));
            e.getPresentation().setEnabled(!CharMatcher.WHITESPACE.matchesAllOf(textToCursor));
        }
    };
    runCompletions.registerCustomShortcutSet(KeyEvent.VK_TAB, 0, consoleView.getConsoleEditor().getComponent());
    runCompletions.getTemplatePresentation().setVisible(false);
    return runCompletions;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) AnAction(com.intellij.openapi.actionSystem.AnAction)

Example 22 with Document

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

the class PySmartStepIntoHandler method computeSmartStepVariants.

@Override
@NotNull
public List<PySmartStepIntoVariant> computeSmartStepVariants(@NotNull XSourcePosition position) {
    final Document document = FileDocumentManager.getInstance().getDocument(position.getFile());
    final List<PySmartStepIntoVariant> variants = Lists.newArrayList();
    final Set<PyCallExpression> visitedCalls = Sets.newHashSet();
    final int line = position.getLine();
    XDebuggerUtil.getInstance().iterateLine(mySession.getProject(), document, line, psiElement -> {
        addVariants(document, line, psiElement, variants, visitedCalls);
        return true;
    });
    return variants;
}
Also used : PyCallExpression(com.jetbrains.python.psi.PyCallExpression) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with Document

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

the class PydevConsoleReference method getVariants.

@NotNull
public Object[] getVariants() {
    Map<String, LookupElement> variants = Maps.newHashMap();
    try {
        final List<PydevCompletionVariant> completions = myCommunication.getCompletions(getText(), myPrefix);
        for (PydevCompletionVariant completion : completions) {
            final PsiManager manager = myElement.getManager();
            final String name = completion.getName();
            final int type = completion.getType();
            LookupElementBuilder builder = LookupElementBuilder.create(new PydevConsoleElement(manager, name, completion.getDescription())).withIcon(PyCodeCompletionImages.getImageForType(type));
            String args = completion.getArgs();
            if (args.equals("(%)")) {
                builder.withPresentableText("%" + completion.getName());
                builder = builder.withInsertHandler(new InsertHandler<LookupElement>() {

                    @Override
                    public void handleInsert(InsertionContext context, LookupElement item) {
                        final Editor editor = context.getEditor();
                        final Document document = editor.getDocument();
                        int offset = context.getStartOffset();
                        if (offset == 0 || !"%".equals(document.getText(TextRange.from(offset - 1, 1)))) {
                            document.insertString(offset, "%");
                        }
                    }
                });
                args = "";
            } else if (!StringUtil.isEmptyOrSpaces(args)) {
                builder = builder.withTailText(args);
            }
            // Set function insert handler
            if (type == IToken.TYPE_FUNCTION || args.endsWith(")")) {
                builder = builder.withInsertHandler(ParenthesesInsertHandler.WITH_PARAMETERS);
            }
            variants.put(name, builder);
        }
    } catch (Exception e) {
    //LOG.error(e);
    }
    return variants.values().toArray();
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) Document(com.intellij.openapi.editor.Document) PydevCompletionVariant(com.jetbrains.python.console.pydev.PydevCompletionVariant) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) ParenthesesInsertHandler(com.intellij.codeInsight.completion.util.ParenthesesInsertHandler) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with Document

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

the class RemoveExtraClosingTagIntentionAction method doFix.

private static void doFix(@NotNull final PsiElement element) throws IncorrectOperationException {
    final XmlToken endNameToken = (XmlToken) element;
    final PsiElement tagElement = endNameToken.getParent();
    if (!(tagElement instanceof XmlTag) && !(tagElement instanceof PsiErrorElement))
        return;
    if (tagElement instanceof PsiErrorElement) {
        tagElement.delete();
    } else {
        final ASTNode astNode = tagElement.getNode();
        if (astNode != null) {
            final ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(astNode);
            if (endTagStart != null) {
                final Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(tagElement.getContainingFile());
                if (document != null) {
                    document.deleteString(endTagStart.getStartOffset(), tagElement.getLastChild().getTextRange().getEndOffset());
                }
            }
        }
    }
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) ASTNode(com.intellij.lang.ASTNode) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Example 25 with Document

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

the class RenameTagBeginOrEndIntentionAction method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null)
        return;
    if (psiElement instanceof PsiWhiteSpace)
        psiElement = PsiTreeUtil.prevLeaf(psiElement);
    if (psiElement instanceof XmlToken) {
        final IElementType tokenType = ((XmlToken) psiElement).getTokenType();
        if (tokenType != XmlTokenType.XML_NAME) {
            if (tokenType == XmlTokenType.XML_TAG_END) {
                psiElement = psiElement.getPrevSibling();
                if (psiElement == null)
                    return;
            }
        }
        PsiElement target = null;
        final String text = psiElement.getText();
        if (!myTargetName.equals(text)) {
            target = psiElement;
        } else {
            // we're in the other
            target = findOtherSide(psiElement, myStart);
        }
        if (target != null) {
            final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
            if (document != null) {
                final TextRange textRange = target.getTextRange();
                document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), myTargetName);
            }
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) XmlToken(com.intellij.psi.xml.XmlToken)

Aggregations

Document (com.intellij.openapi.editor.Document)1072 VirtualFile (com.intellij.openapi.vfs.VirtualFile)243 PsiFile (com.intellij.psi.PsiFile)191 Project (com.intellij.openapi.project.Project)163 NotNull (org.jetbrains.annotations.NotNull)138 Nullable (org.jetbrains.annotations.Nullable)129 TextRange (com.intellij.openapi.util.TextRange)122 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)117 PsiElement (com.intellij.psi.PsiElement)107 Editor (com.intellij.openapi.editor.Editor)104 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)56 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)45 IncorrectOperationException (com.intellij.util.IncorrectOperationException)43 IOException (java.io.IOException)42 RangeMarker (com.intellij.openapi.editor.RangeMarker)35 MockVirtualFile (com.intellij.mock.MockVirtualFile)33 ArrayList (java.util.ArrayList)32 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)30 File (java.io.File)30 List (java.util.List)29