Search in sources :

Example 26 with TextRange

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

the class KotlinParenthesesSurrounder method surroundExpression.

@Nullable
@Override
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull KtExpression expression) {
    KtParenthesizedExpression parenthesizedExpression = (KtParenthesizedExpression) KtPsiFactoryKt.KtPsiFactory(expression).createExpression("(a)");
    KtExpression expressionWithoutParentheses = parenthesizedExpression.getExpression();
    assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression";
    expressionWithoutParentheses.replace(expression);
    expression = (KtExpression) expression.replace(parenthesizedExpression);
    CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
    int offset = expression.getTextRange().getEndOffset();
    return new TextRange(offset, offset);
}
Also used : KtExpression(org.jetbrains.kotlin.psi.KtExpression) TextRange(com.intellij.openapi.util.TextRange) KtParenthesizedExpression(org.jetbrains.kotlin.psi.KtParenthesizedExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with TextRange

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

the class KotlinWhenSurrounder method surroundExpression.

@Nullable
@Override
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull KtExpression expression) {
    KtWhenExpression whenExpression = (KtWhenExpression) KtPsiFactoryKt.KtPsiFactory(expression).createExpression(getCodeTemplate(expression));
    KtExpression subjectExpression = whenExpression.getSubjectExpression();
    assert subjectExpression != null : "JetExpression should exists for " + whenExpression.getText() + " expression";
    subjectExpression.replace(expression);
    expression = (KtExpression) expression.replace(whenExpression);
    CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
    KtWhenEntry whenEntry = ((KtWhenExpression) expression).getEntries().get(0);
    KtWhenCondition whenEntryCondition = whenEntry.getConditions()[0];
    assert whenEntryCondition != null : "JetExpression for first entry should exists: " + expression.getText();
    TextRange whenRange = whenEntryCondition.getTextRange();
    editor.getDocument().deleteString(whenRange.getStartOffset(), whenRange.getEndOffset());
    int offset = whenRange.getStartOffset();
    return new TextRange(offset, offset);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with TextRange

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

the class KotlinIfSurrounderBase method surroundStatements.

@Nullable
@Override
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
    statements = MoveDeclarationsOutHelper.move(container, statements, isGenerateDefaultInitializers());
    if (statements.length == 0) {
        KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
        return null;
    }
    KtIfExpression ifExpression = (KtIfExpression) KtPsiFactoryKt.KtPsiFactory(project).createExpression(getCodeTemplate());
    ifExpression = (KtIfExpression) container.addAfter(ifExpression, statements[statements.length - 1]);
    // TODO move a comment for first statement
    KtBlockExpression thenBranch = (KtBlockExpression) ifExpression.getThen();
    assert thenBranch != null : "Then branch should exist for created if expression: " + ifExpression.getText();
    // Add statements in then branch of created if
    KotlinSurrounderUtils.addStatementsInBlock(thenBranch, statements);
    // Delete statements from original code
    container.deleteChildRange(statements[0], statements[statements.length - 1]);
    ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression);
    KtExpression condition = ifExpression.getCondition();
    assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText();
    // Delete condition from created if
    TextRange range = condition.getTextRange();
    TextRange textRange = new TextRange(range.getStartOffset(), range.getStartOffset());
    editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    return textRange;
}
Also used : KtIfExpression(org.jetbrains.kotlin.psi.KtIfExpression) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtBlockExpression(org.jetbrains.kotlin.psi.KtBlockExpression) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with TextRange

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

the class KotlinTryFinallySurrounder method surroundStatements.

@Nullable
@Override
protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) {
    TextRange textRange = super.surroundStatements(project, editor, container, statements);
    if (textRange == null) {
        return null;
    }
    // Delete dummy "b" element for caret
    editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
    return new TextRange(textRange.getStartOffset(), textRange.getStartOffset());
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with TextRange

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

the class KotlinDeclarationMover method getElementSourceLineRange.

@Override
protected LineRange getElementSourceLineRange(@NotNull PsiElement element, @NotNull Editor editor, @NotNull LineRange oldRange) {
    PsiElement first;
    PsiElement last;
    if (element instanceof KtDeclaration) {
        first = element.getFirstChild();
        last = element.getLastChild();
        if (first == null || last == null)
            return null;
    } else {
        first = last = element;
    }
    TextRange textRange1 = first.getTextRange();
    TextRange textRange2 = last.getTextRange();
    Document doc = editor.getDocument();
    if (doc.getTextLength() < textRange2.getEndOffset())
        return null;
    int startLine = editor.offsetToLogicalPosition(textRange1.getStartOffset()).line;
    int endLine = editor.offsetToLogicalPosition(textRange2.getEndOffset()).line + 1;
    if (element instanceof PsiComment || startLine == oldRange.startLine || startLine == oldRange.endLine || endLine == oldRange.startLine || endLine == oldRange.endLine) {
        return new LineRange(startLine, endLine);
    }
    TextRange lineTextRange = new TextRange(doc.getLineStartOffset(oldRange.startLine), doc.getLineEndOffset(oldRange.endLine));
    if (element instanceof KtDeclaration) {
        for (PsiElement anchor : getDeclarationAnchors((KtDeclaration) element)) {
            TextRange suspectTextRange = anchor.getTextRange();
            if (suspectTextRange != null && lineTextRange.intersects(suspectTextRange))
                return new LineRange(startLine, endLine);
        }
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) PsiElement(com.intellij.psi.PsiElement)

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