Search in sources :

Example 1 with TextRange

use of com.intellij.openapi.util.TextRange in project buck by facebook.

the class BuckAnnotator method annotateErrors.

private void annotateErrors(PsiElement psiElement, AnnotationHolder annotationHolder) {
    BuckValue value = PsiTreeUtil.getParentOfType(psiElement, BuckValue.class);
    if (value == null) {
        return;
    }
    final Project project = psiElement.getProject();
    if (project == null) {
        return;
    }
    String target = psiElement.getText();
    if (target.matches("\".*\"") || target.matches("'.*'")) {
        target = target.substring(1, target.length() - 1);
    } else {
        return;
    }
    if (!BuckBuildUtil.isValidAbsoluteTarget(target)) {
        return;
    }
    VirtualFile buckDir = project.getBaseDir().findFileByRelativePath(BuckBuildUtil.extractAbsoluteTarget(target));
    VirtualFile targetBuckFile = buckDir != null ? buckDir.findChild("BUCK") : null;
    if (targetBuckFile == null) {
        TextRange range = new TextRange(psiElement.getTextRange().getStartOffset(), psiElement.getTextRange().getEndOffset());
        annotationHolder.createErrorAnnotation(range, ANNOTATOR_ERROR_CANNOT_LOCATE_TARGET);
        project.getMessageBus().syncPublisher(IntellijBuckAction.EVENT).consume(this.getClass().toString());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BuckValue(com.facebook.buck.intellij.ideabuck.lang.psi.BuckValue) Project(com.intellij.openapi.project.Project) TextRange(com.intellij.openapi.util.TextRange)

Example 2 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class GroovyEnterHandler method handleInString.

private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return false;
    final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
    assert vfile != null;
    PsiFile file = PsiManager.getInstance(project).findFile(vfile);
    Document document = editor.getDocument();
    String fileText = document.getText();
    if (fileText.length() == caretOffset)
        return false;
    if (!checkStringApplicable(editor, caretOffset))
        return false;
    if (file == null)
        return false;
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final PsiElement stringElement = inferStringPair(file, caretOffset);
    if (stringElement == null)
        return false;
    ASTNode node = stringElement.getNode();
    final IElementType nodeElementType = node.getElementType();
    boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);
    // For simple String literals like 'abc'
    CaretModel caretModel = editor.getCaretModel();
    if (nodeElementType == GroovyTokenTypes.mSTRING_LITERAL) {
        if (isSingleQuoteString(stringElement)) {
            //the case of print '\<caret>'
            if (isSlashBeforeCaret(caretOffset, fileText)) {
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else if (stringElement.getParent() instanceof GrReferenceExpression) {
                TextRange range = stringElement.getTextRange();
                convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
                document.insertString(range.getStartOffset(), "''");
                caretModel.moveToOffset(caretOffset + 2);
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, "'+");
                originalHandler.execute(editor, dataContext);
                EditorModificationUtil.insertStringAtCaret(editor, "'");
                PsiDocumentManager.getInstance(project).commitDocument(document);
                CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
            }
        } else {
            insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        }
        return true;
    }
    if (GSTRING_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
        PsiElement parent = stringElement.getParent();
        if (nodeElementType == GroovyTokenTypes.mGSTRING_LITERAL) {
            parent = stringElement;
        } else {
            while (parent != null && !(parent instanceof GrLiteral)) {
                parent = parent.getParent();
            }
        }
        if (parent == null)
            return false;
        if (isDoubleQuotedString(parent)) {
            PsiElement exprSibling = stringElement.getNextSibling();
            boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
            if (rightFromDollar)
                caretOffset--;
            TextRange parentRange = parent.getTextRange();
            if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
                convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
                document.insertString(parentRange.getStartOffset(), "\"\"");
                caretModel.moveToOffset(caretOffset + 2);
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
                if (rightFromDollar) {
                    caretModel.moveCaretRelatively(1, 0, false, false, true);
                }
            } else if (isSlashBeforeCaret(caretOffset, fileText)) {
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, "\"+");
                originalHandler.execute(editor, dataContext);
                EditorModificationUtil.insertStringAtCaret(editor, "\"");
                PsiDocumentManager.getInstance(project).commitDocument(document);
                CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
            }
        } else {
            insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        }
        return true;
    }
    if (REGEX_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
        PsiElement parent = stringElement.getParent();
        if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
            parent = stringElement;
        } else {
            while (parent != null && !(parent instanceof GrLiteral)) {
                parent = parent.getParent();
            }
        }
        if (parent == null || parent.getLastChild() instanceof PsiErrorElement)
            return false;
        PsiElement exprSibling = stringElement.getNextSibling();
        boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
        if (rightFromDollar) {
            caretModel.moveToOffset(caretOffset - 1);
        }
        insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        if (rightFromDollar) {
            caretModel.moveCaretRelatively(1, 0, false, false, true);
        }
        return true;
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CaretModel(com.intellij.openapi.editor.CaretModel) TextRange(com.intellij.openapi.util.TextRange) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) ASTNode(com.intellij.lang.ASTNode) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 3 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class GrExecuteCommandAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
    if (project == null || editor == null || virtualFile == null)
        return;
    FileDocumentManager.getInstance().saveAllDocuments();
    final Document document = editor.getDocument();
    final TextRange selectedRange = EditorUtil.getSelectionInAnyMode(editor);
    final String command = (selectedRange.isEmpty() ? document.getText() : document.getText(selectedRange));
    final GroovyConsole existingConsole = virtualFile.getUserData(GroovyConsole.GROOVY_CONSOLE);
    if (existingConsole == null) {
        GroovyConsole.getOrCreateConsole(project, virtualFile, console -> console.execute(command));
    } else {
        existingConsole.execute(command);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GroovyConsole(org.jetbrains.plugins.groovy.console.GroovyConsole) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Example 4 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class GroovyMembersWithDocSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    final GrDocCommentOwner owner;
    final GrDocComment doc;
    if (e instanceof GrDocComment) {
        doc = (GrDocComment) e;
        owner = GrDocCommentUtil.findDocOwner(doc);
    } else {
        owner = (GrDocCommentOwner) e;
        doc = GrDocCommentUtil.findDocComment(owner);
    }
    if (doc == null || owner == null)
        return Collections.emptyList();
    final TextRange range = new TextRange(doc.getTextRange().getStartOffset(), owner.getTextRange().getEndOffset());
    return ExtendWordSelectionHandlerBase.expandToWholeLine(editorText, range, true);
}
Also used : GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) TextRange(com.intellij.openapi.util.TextRange) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 5 with TextRange

use of com.intellij.openapi.util.TextRange in project intellij-community by JetBrains.

the class GroovyStatementSelectioner method inferBlockRange.

private static TextRange inferBlockRange(PsiElement first, PsiElement last) {
    while (true) {
        PsiElement prev = first.getPrevSibling();
        prev = skipWhitespaceBack(prev);
        if (isOneLineFeed(prev))
            prev = prev.getPrevSibling();
        prev = skipWhitespaceBack(prev);
        if (prev != null && prev.getNode().getElementType() == GroovyTokenTypes.mSEMI || prev instanceof GrStatement) {
            first = prev;
        } else {
            break;
        }
    }
    while (true) {
        PsiElement next = last.getNextSibling();
        next = skipWhitespacesForward(next);
        if (isOneLineFeed(next))
            next = next.getNextSibling();
        next = skipWhitespacesForward(next);
        if (next != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI || next instanceof GrStatement) {
            last = next;
        } else {
            break;
        }
    }
    return new TextRange(first.getTextRange().getStartOffset(), last.getTextRange().getEndOffset());
}
Also used : TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)1314 PsiElement (com.intellij.psi.PsiElement)261 NotNull (org.jetbrains.annotations.NotNull)237 Nullable (org.jetbrains.annotations.Nullable)167 Document (com.intellij.openapi.editor.Document)132 ArrayList (java.util.ArrayList)117 Project (com.intellij.openapi.project.Project)96 PsiFile (com.intellij.psi.PsiFile)96 ASTNode (com.intellij.lang.ASTNode)95 Editor (com.intellij.openapi.editor.Editor)75 PsiReference (com.intellij.psi.PsiReference)54 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 IElementType (com.intellij.psi.tree.IElementType)48 IncorrectOperationException (com.intellij.util.IncorrectOperationException)48 Pair (com.intellij.openapi.util.Pair)47 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)33 ProperTextRange (com.intellij.openapi.util.ProperTextRange)32 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)31 XmlTag (com.intellij.psi.xml.XmlTag)31 List (java.util.List)31