use of org.jetbrains.kotlin.psi.KtBlockExpression 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 org.jetbrains.kotlin.psi.KtBlockExpression in project kotlin by JetBrains.
the class AbstractKotlinUpDownMover method getSourceRange.
@Nullable
protected LineRange getSourceRange(@NotNull PsiElement firstElement, @NotNull PsiElement lastElement, @NotNull Editor editor, LineRange oldRange) {
PsiElement parent = PsiTreeUtil.findCommonParent(firstElement, lastElement);
int topExtension = 0;
int bottomExtension = 0;
if (parent instanceof KtFunctionLiteral) {
KtBlockExpression block = ((KtFunctionLiteral) parent).getBodyExpression();
if (block != null) {
PsiElement comment = null;
boolean extendDown = false;
if (checkCommentAtBlockBound(firstElement, lastElement, block)) {
comment = lastElement;
extendDown = true;
lastElement = block.getLastChild();
} else if (checkCommentAtBlockBound(lastElement, firstElement, block)) {
comment = firstElement;
firstElement = block.getFirstChild();
}
if (comment != null) {
int extension = KotlinRefactoringUtilKt.getLineCount(comment);
if (extendDown) {
bottomExtension = extension;
} else {
topExtension = extension;
}
}
parent = PsiTreeUtil.findCommonParent(firstElement, lastElement);
}
}
if (parent == null)
return null;
Pair<PsiElement, PsiElement> originalRange = getElementRange(parent, firstElement, lastElement);
if (!checkSourceElement(originalRange.first) || !checkSourceElement(originalRange.second))
return null;
LineRange lineRange1 = getElementSourceLineRange(originalRange.first, editor, oldRange);
if (lineRange1 == null)
return null;
LineRange lineRange2 = getElementSourceLineRange(originalRange.second, editor, oldRange);
if (lineRange2 == null)
return null;
LineRange parentLineRange = getElementSourceLineRange(parent, editor, oldRange);
LineRange sourceRange = new LineRange(lineRange1.startLine - topExtension, lineRange2.endLine + bottomExtension);
if (parentLineRange != null && sourceRange.startLine == parentLineRange.startLine && sourceRange.endLine == parentLineRange.endLine) {
sourceRange.firstElement = sourceRange.lastElement = parent;
} else {
sourceRange.firstElement = originalRange.first;
sourceRange.lastElement = originalRange.second;
}
return sourceRange;
}
Aggregations