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);
}
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);
}
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;
}
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());
}
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;
}
Aggregations