Search in sources :

Example 66 with Document

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

the class IntroduceHandler method performAction.

public void performAction(IntroduceOperation operation) {
    final PsiFile file = operation.getFile();
    if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) {
        return;
    }
    final Editor editor = operation.getEditor();
    if (editor.getSettings().isVariableInplaceRenameEnabled()) {
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(operation.getEditor());
        if (templateState != null && !templateState.isFinished()) {
            return;
        }
    }
    PsiElement element1 = null;
    PsiElement element2 = null;
    final SelectionModel selectionModel = editor.getSelectionModel();
    boolean singleElementSelection = false;
    if (selectionModel.hasSelection()) {
        element1 = file.findElementAt(selectionModel.getSelectionStart());
        element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
        if (element1 instanceof PsiWhiteSpace) {
            int startOffset = element1.getTextRange().getEndOffset();
            element1 = file.findElementAt(startOffset);
        }
        if (element2 instanceof PsiWhiteSpace) {
            int endOffset = element2.getTextRange().getStartOffset();
            element2 = file.findElementAt(endOffset - 1);
        }
        if (element1 == element2) {
            singleElementSelection = true;
        }
    } else {
        if (smartIntroduce(operation)) {
            return;
        }
        final CaretModel caretModel = editor.getCaretModel();
        final Document document = editor.getDocument();
        int lineNumber = document.getLineNumber(caretModel.getOffset());
        if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
            element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
            element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
        }
    }
    final Project project = operation.getProject();
    if (element1 == null || element2 == null) {
        showCannotPerformError(project, editor);
        return;
    }
    element1 = PyRefactoringUtil.getSelectedExpression(project, file, element1, element2);
    final PyComprehensionElement comprehension = PsiTreeUtil.getParentOfType(element1, PyComprehensionElement.class, true);
    if (element1 == null || comprehension != null) {
        showCannotPerformError(project, editor);
        return;
    }
    if (singleElementSelection && element1 instanceof PyStringLiteralExpression) {
        final PyStringLiteralExpression literal = (PyStringLiteralExpression) element1;
        // Currently introduce for substrings of a multi-part string literals is not supported
        if (literal.getStringNodes().size() > 1) {
            showCannotPerformError(project, editor);
            return;
        }
        final int offset = element1.getTextOffset();
        final TextRange selectionRange = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
        final TextRange elementRange = element1.getTextRange();
        if (!elementRange.equals(selectionRange) && elementRange.contains(selectionRange)) {
            final TextRange innerRange = literal.getStringValueTextRange();
            final TextRange intersection = selectionRange.shiftRight(-offset).intersection(innerRange);
            final TextRange finalRange = intersection != null ? intersection : selectionRange;
            final String text = literal.getText();
            if (getFormatValueExpression(literal) != null && breaksStringFormatting(text, finalRange) || getNewStyleFormatValueExpression(literal) != null && breaksNewStyleStringFormatting(text, finalRange) || breaksStringEscaping(text, finalRange)) {
                showCannotPerformError(project, editor);
                return;
            }
            element1.putUserData(PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE, Pair.create(element1, finalRange));
        }
    }
    if (!checkIntroduceContext(file, editor, element1)) {
        return;
    }
    operation.setElement(element1);
    performActionOnElement(operation);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) Project(com.intellij.openapi.project.Project) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor)

Example 67 with Document

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

the class PyWithTryExceptSurrounder method surroundStatement.

@Override
@Nullable
protected TextRange surroundStatement(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException {
    PyTryExceptStatement tryStatement = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyTryExceptStatement.class, getTemplate());
    final PsiElement parent = elements[0].getParent();
    final PyStatementList statementList = tryStatement.getTryPart().getStatementList();
    statementList.addRange(elements[0], elements[elements.length - 1]);
    statementList.getFirstChild().delete();
    tryStatement = (PyTryExceptStatement) parent.addBefore(tryStatement, elements[0]);
    parent.deleteChildRange(elements[0], elements[elements.length - 1]);
    final PsiFile psiFile = parent.getContainingFile();
    final Document document = psiFile.getViewProvider().getDocument();
    final TextRange range = tryStatement.getTextRange();
    assert document != null;
    final RangeMarker rangeMarker = document.createRangeMarker(range);
    final PsiElement element = psiFile.findElementAt(rangeMarker.getStartOffset());
    tryStatement = PsiTreeUtil.getParentOfType(element, PyTryExceptStatement.class);
    if (tryStatement != null) {
        return getResultRange(tryStatement);
    }
    return null;
}
Also used : PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 68 with Document

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

the class PyIntroduceFieldHandler method isTestClass.

private static boolean isTestClass(PsiFile file, Editor editor) {
    PsiElement element1 = null;
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        element1 = file.findElementAt(selectionModel.getSelectionStart());
    } else {
        final CaretModel caretModel = editor.getCaretModel();
        final Document document = editor.getDocument();
        int lineNumber = document.getLineNumber(caretModel.getOffset());
        if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
            element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
        }
    }
    if (element1 != null) {
        final PyClass clazz = PyUtil.getContainingClassOrSelf(element1);
        if (clazz != null && PythonUnitTestUtil.isTestCaseClass(clazz, null))
            return true;
    }
    return false;
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 69 with Document

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

the class XmlCopyPastePreProcessor method preprocessOnPaste.

@Override
@NotNull
public String preprocessOnPaste(Project project, PsiFile file, Editor editor, String text, RawText rawText) {
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    int caretOffset = editor.getCaretModel().getOffset();
    PsiElement element = PsiUtilCore.getElementAtOffset(file, caretOffset);
    ASTNode node = element.getNode();
    if (node != null) {
        boolean hasMarkup = text.indexOf('>') >= 0 || text.indexOf('<') >= 0;
        if (element.getTextOffset() == caretOffset && node.getElementType() == XmlTokenType.XML_END_TAG_START && node.getTreePrev().getElementType() == XmlTokenType.XML_TAG_END) {
            return hasMarkup ? text : encode(text, element);
        } else {
            XmlElement parent = PsiTreeUtil.getParentOfType(element, XmlText.class, XmlAttributeValue.class);
            if (parent != null) {
                if (parent instanceof XmlText && hasMarkup) {
                    return text;
                }
                if (TreeUtil.findParent(node, XmlElementType.XML_CDATA) == null && TreeUtil.findParent(node, XmlElementType.XML_COMMENT) == null) {
                    return encode(text, element);
                }
            }
        }
    }
    return text;
}
Also used : ASTNode(com.intellij.lang.ASTNode) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 70 with Document

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

the class XmlSlashTypedHandler method autoIndent.

private static void autoIndent(@NotNull Editor editor) {
    Project project = editor.getProject();
    if (project != null) {
        PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
        Document document = editor.getDocument();
        documentManager.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) Document(com.intellij.openapi.editor.Document)

Aggregations

Document (com.intellij.openapi.editor.Document)1075 VirtualFile (com.intellij.openapi.vfs.VirtualFile)246 PsiFile (com.intellij.psi.PsiFile)191 Project (com.intellij.openapi.project.Project)164 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 File (java.io.File)32 ArrayList (java.util.ArrayList)32 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)30 List (java.util.List)29