Search in sources :

Example 51 with Document

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

the class ConvertConcatenationToGstringIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiFile file = element.getContainingFile();
    final int offset = editor.getCaretModel().getOffset();
    final AccessToken accessToken = ReadAction.start();
    final List<GrExpression> expressions;
    try {
        expressions = collectExpressions(file, offset);
    } finally {
        accessToken.finish();
    }
    final Document document = editor.getDocument();
    if (expressions.size() == 1) {
        invokeImpl(expressions.get(0), document);
    } else if (!expressions.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            invokeImpl(expressions.get(expressions.size() - 1), document);
            return;
        }
        IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {

            @Override
            public void pass(final GrExpression selectedValue) {
                invokeImpl(selectedValue, document);
            }
        }, grExpression -> grExpression.getText());
    }
}
Also used : TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) WriteAction(com.intellij.openapi.application.WriteAction) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals) Document(com.intellij.openapi.editor.Document) ReadAction(com.intellij.openapi.application.ReadAction) ArrayList(java.util.ArrayList) AccessToken(com.intellij.openapi.application.AccessToken) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) GroovyCommonClassNames(org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) Project(com.intellij.openapi.project.Project) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) IntroduceTargetChooser(com.intellij.refactoring.IntroduceTargetChooser) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GroovyTokenTypes(org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrStringUtil(org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil) MethodSignatureUtil(com.intellij.psi.util.MethodSignatureUtil) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) TypesUtil(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Nullable(org.jetbrains.annotations.Nullable) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) List(java.util.List) Pass(com.intellij.openapi.util.Pass) ErrorUtil(org.jetbrains.plugins.groovy.lang.psi.util.ErrorUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) Pass(com.intellij.openapi.util.Pass) AccessToken(com.intellij.openapi.application.AccessToken) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document)

Example 52 with Document

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

the class RemoveUnnecessaryEscapeCharactersIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final Document document = editor.getDocument();
    final TextRange range = element.getTextRange();
    document.replaceString(range.getStartOffset(), range.getEndOffset(), removeUnnecessaryEscapeSymbols((GrLiteral) element));
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 53 with Document

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

the class TaskChangelistSupport method installSearch.

public void installSearch(EditorTextField name, final EditorTextField comment) {
    Document document = name.getDocument();
    final TaskAutoCompletionListProvider completionProvider = new TaskAutoCompletionListProvider(myProject);
    TextFieldWithAutoCompletion.installCompletion(document, myProject, completionProvider, false);
}
Also used : TaskAutoCompletionListProvider(com.intellij.tasks.actions.TaskAutoCompletionListProvider) Document(com.intellij.openapi.editor.Document)

Example 54 with Document

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

the class CustomRegexpFilter method createOpenFileHyperlink.

protected HyperlinkInfo createOpenFileHyperlink(String fileName, final int line, int column) {
    if ((fileName == null || fileName.length() == 0)) {
        if (myBase != null) {
            fileName = myBase.getPresentableUrl();
        } else {
            return null;
        }
    }
    fileName = fileName.replace(File.separatorChar, '/');
    VirtualFile file;
    // try to interpret the filename as URL
    if (URLUtil.containsScheme(fileName)) {
        try {
            file = VfsUtil.findFileByURL(new URL(fileName));
        } catch (MalformedURLException e) {
            file = VirtualFileManager.getInstance().findFileByUrl(VfsUtil.pathToUrl(fileName));
        }
    } else {
        file = VfsUtil.findRelativeFile(fileName, myBase);
    }
    if (file == null) {
        //noinspection ConstantConditions
        return null;
    }
    final FileType fileType = file.getFileType();
    if (fileType != null && column > 0) {
        final Document document = FileDocumentManager.getInstance().getDocument(file);
        final int start = document.getLineStartOffset(line);
        final int max = document.getLineEndOffset(line);
        final int tabSize = CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings().getTabSize(fileType);
        column = EditorUtil.calcColumnNumber(null, document.getCharsSequence(), start, Math.min(start + column, max), tabSize);
    }
    return new OpenFileHyperlinkInfo(myProject, file, line, column);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MalformedURLException(java.net.MalformedURLException) OpenFileHyperlinkInfo(com.intellij.execution.filters.OpenFileHyperlinkInfo) FileType(com.intellij.openapi.fileTypes.FileType) Document(com.intellij.openapi.editor.Document) URL(java.net.URL)

Example 55 with Document

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

the class PyStatementMover method moveInOut.

private static int moveInOut(@NotNull final MyLineRange toMove, @NotNull final Editor editor, @NotNull final MoveInfo info) {
    boolean removePass = false;
    final ScopeRange toMove2 = (ScopeRange) info.toMove2;
    final PsiElement scope = toMove2.getScope();
    final PsiElement anchor = toMove2.getAnchor();
    final Project project = scope.getProject();
    final PsiElement startElement = toMove.myStartElement;
    final PsiElement endElement = toMove.myEndElement;
    PsiElement parent = startElement.getParent();
    if (scope instanceof PyStatementList && !(startElement == endElement && startElement instanceof PsiComment)) {
        final PyStatement[] statements = ((PyStatementList) scope).getStatements();
        if (statements.length == 1 && statements[0] == anchor && statements[0] instanceof PyPassStatement) {
            removePass = true;
        }
    }
    final PsiElement addedElement;
    PsiElement nextSibling = startElement.getNextSibling();
    if (toMove2.isAddBefore()) {
        PsiElement tmp = endElement.getPrevSibling();
        if (startElement != endElement && tmp != null) {
            addedElement = scope.addRangeBefore(startElement, tmp, anchor);
            scope.addBefore(endElement, anchor);
        } else {
            addedElement = scope.addBefore(endElement, anchor);
        }
    } else {
        if (startElement != endElement && nextSibling != null) {
            scope.addRangeAfter(nextSibling, endElement, anchor);
        }
        addedElement = scope.addAfter(startElement, anchor);
    }
    addPassStatement(toMove, project);
    if (startElement != endElement && nextSibling != null) {
        parent.deleteChildRange(nextSibling, endElement);
    }
    startElement.delete();
    final int addedElementLine = editor.getDocument().getLineNumber(addedElement.getTextOffset());
    final PsiFile file = scope.getContainingFile();
    adjustLineIndents(editor, scope, project, addedElement, toMove.size);
    if (removePass) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            final Document document = editor.getDocument();
            final int lineNumber = document.getLineNumber(anchor.getTextOffset());
            final int endOffset = document.getLineCount() <= lineNumber + 1 ? document.getLineEndOffset(lineNumber) : document.getLineStartOffset(lineNumber + 1);
            document.deleteString(document.getLineStartOffset(lineNumber), endOffset);
            PsiDocumentManager.getInstance(startElement.getProject()).commitAllDocuments();
        });
    }
    int offset = addedElement.getTextRange().getStartOffset();
    int newLine = editor.getDocument().getLineNumber(offset);
    if (newLine != addedElementLine && !removePass) {
        // PsiComment gets broken after adjust indent
        PsiElement psiElement = PyUtil.findNonWhitespaceAtOffset(file, editor.getDocument().getLineEndOffset(addedElementLine) - 1);
        if (psiElement != null) {
            psiElement = getCommentOrStatement(editor.getDocument(), psiElement);
            offset = psiElement.getTextRange().getStartOffset();
        }
    }
    return offset;
}
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