use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyStatementMover method checkAvailable.
@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
if (!(file instanceof PyFile))
return false;
final int offset = editor.getCaretModel().getOffset();
final SelectionModel selectionModel = editor.getSelectionModel();
final Document document = editor.getDocument();
final int lineNumber = document.getLineNumber(offset);
int start = getLineStartSafeOffset(document, lineNumber);
final int lineEndOffset = document.getLineEndOffset(lineNumber);
int end = lineEndOffset == 0 ? 0 : lineEndOffset - 1;
if (selectionModel.hasSelection()) {
start = selectionModel.getSelectionStart();
final int selectionEnd = selectionModel.getSelectionEnd();
end = selectionEnd == 0 ? 0 : selectionEnd - 1;
}
PsiElement elementToMove1 = PyUtil.findNonWhitespaceAtOffset(file, start);
PsiElement elementToMove2 = PyUtil.findNonWhitespaceAtOffset(file, end);
if (elementToMove1 == null || elementToMove2 == null)
return false;
if (ifInsideString(document, lineNumber, elementToMove1, down))
return false;
elementToMove1 = getCommentOrStatement(document, elementToMove1);
elementToMove2 = getCommentOrStatement(document, elementToMove2);
if (PsiTreeUtil.isAncestor(elementToMove1, elementToMove2, false)) {
elementToMove2 = elementToMove1;
} else if (PsiTreeUtil.isAncestor(elementToMove2, elementToMove1, false)) {
elementToMove1 = elementToMove2;
}
info.toMove = new MyLineRange(elementToMove1, elementToMove2);
info.toMove2 = getDestinationScope(file, editor, down ? elementToMove2 : elementToMove1, down);
info.indentTarget = false;
info.indentSource = false;
return true;
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyStatementMover method adjustLineIndents.
private static void adjustLineIndents(@NotNull final Editor editor, @NotNull final PsiElement scope, @NotNull final Project project, @NotNull final PsiElement addedElement, int size) {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
final Document document = editor.getDocument();
if (!(scope instanceof PsiFile)) {
int line1 = editor.offsetToLogicalPosition(scope.getTextRange().getStartOffset()).line;
int line2 = editor.offsetToLogicalPosition(scope.getTextRange().getEndOffset()).line;
codeStyleManager.adjustLineIndent(scope.getContainingFile(), new TextRange(document.getLineStartOffset(line1), document.getLineEndOffset(line2)));
} else {
int line1 = editor.offsetToLogicalPosition(addedElement.getTextRange().getStartOffset()).line;
PsiElement end = addedElement;
while (size > 0) {
PsiElement tmp = end.getNextSibling();
if (tmp == null)
break;
size -= 1;
end = tmp;
}
int endOffset = end.getTextRange().getEndOffset();
int line2 = editor.offsetToLogicalPosition(endOffset).line;
codeStyleManager.adjustLineIndent(scope.getContainingFile(), new TextRange(document.getLineStartOffset(line1), document.getLineEndOffset(line2)));
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyStatementMover method getDestinationScope.
@Nullable
private static LineRange getDestinationScope(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final PsiElement elementToMove, boolean down) {
final Document document = file.getViewProvider().getDocument();
if (document == null)
return null;
final int offset = down ? elementToMove.getTextRange().getEndOffset() : elementToMove.getTextRange().getStartOffset();
int lineNumber = down ? document.getLineNumber(offset) + 1 : document.getLineNumber(offset) - 1;
if (moveOutsideFile(document, lineNumber))
return null;
int lineEndOffset = document.getLineEndOffset(lineNumber);
final int startOffset = document.getLineStartOffset(lineNumber);
final PyStatementList statementList = getStatementList(elementToMove);
final PsiElement destination = getDestinationElement(elementToMove, document, lineEndOffset, down);
final int start = destination != null ? destination.getTextRange().getStartOffset() : lineNumber;
final int end = destination != null ? destination.getTextRange().getEndOffset() : lineNumber;
final int startLine = document.getLineNumber(start);
final int endLine = document.getLineNumber(end);
if (elementToMove instanceof PyClass || elementToMove instanceof PyFunction) {
PyElement scope = statementList == null ? (PyElement) elementToMove.getContainingFile() : statementList;
if (destination != null)
return new ScopeRange(scope, destination, !down, true);
}
final String lineText = document.getText(TextRange.create(startOffset, lineEndOffset));
final boolean isEmptyLine = StringUtil.isEmptyOrSpaces(lineText);
if (isEmptyLine && moveToEmptyLine(elementToMove, down))
return new LineRange(lineNumber, lineNumber + 1);
LineRange scopeRange = moveOut(elementToMove, editor, down);
if (scopeRange != null)
return scopeRange;
scopeRange = moveInto(elementToMove, file, editor, down, lineEndOffset);
if (scopeRange != null)
return scopeRange;
if (elementToMove instanceof PsiComment && (PsiTreeUtil.isAncestor(destination, elementToMove, true)) || destination instanceof PsiComment) {
return new LineRange(lineNumber, lineNumber + 1);
}
final PyElement scope = statementList == null ? (PyElement) elementToMove.getContainingFile() : statementList;
if ((elementToMove instanceof PyClass) || (elementToMove instanceof PyFunction))
return new ScopeRange(scope, scope.getFirstChild(), !down, true);
return new LineRange(startLine, endLine + 1);
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class GoogleDocStringSectionFixer method doApply.
@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyStringLiteralExpression pyString) {
final int offset = editor.getCaretModel().getOffset();
final Document document = editor.getDocument();
final int lineNum = document.getLineNumber(offset);
final int lineStart = document.getLineStartOffset(lineNum);
final int lineEnd = document.getLineEndOffset(lineNum);
final String line = document.getText(TextRange.create(lineStart, lineEnd));
if (!StringUtil.isEmptyOrSpaces(line)) {
final String trimmedLine = line.trim();
final String header = trimmedLine.endsWith(":") ? trimmedLine.substring(0, trimmedLine.length() - 1) : trimmedLine;
if (SectionBasedDocString.isValidSectionTitle(header)) {
final String patch = (trimmedLine.endsWith(":") ? "\n" : ":\n") + PyIndentUtil.getLineIndent(line) + GoogleCodeStyleDocStringBuilder.getDefaultSectionIndent(pyString.getProject());
final int insertionOffset = lineStart + StringUtil.trimTrailing(line).length();
document.replaceString(insertionOffset, lineEnd, patch);
processor.registerUnresolvedError(insertionOffset + patch.length());
}
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PyArgumentListFixer method doApply.
@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyArgumentList arguments) throws IncorrectOperationException {
final PsiElement rBrace = PyPsiUtils.getChildByFilter(arguments, PyTokenTypes.CLOSE_BRACES, 0);
if (arguments.getParent() instanceof PyClass || arguments.getParent() instanceof PyDecorator) {
final PsiElement lBrace = PyPsiUtils.getChildByFilter(arguments, PyTokenTypes.OPEN_BRACES, 0);
if (lBrace != null && rBrace == null) {
final Document document = editor.getDocument();
document.insertString(arguments.getTextRange().getEndOffset(), ")");
}
} else {
if (rBrace == null) {
final Document document = editor.getDocument();
document.insertString(arguments.getTextRange().getEndOffset(), ")");
}
}
}
Aggregations