use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class JavadocFixer method moveCaretToTheLineEndIfPossible.
private static void moveCaretToTheLineEndIfPossible(@NotNull Editor editor, int line) {
final Document document = editor.getDocument();
final CaretModel caretModel = editor.getCaretModel();
int offset;
if (line >= document.getLineCount()) {
offset = document.getTextLength();
} else {
offset = document.getLineEndOffset(line);
}
caretModel.moveToOffset(offset);
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class JavadocFixer method process.
/**
* Checks if caret of the given editor is located inside javadoc and tries to perform smart completion there in case of the positive
* answer.
*
* @param editor target editor
* @param psiFile PSI file for the document exposed via the given editor
* @return {@code true} if smart completion was performed; {@code false} otherwise
*/
public boolean process(@NotNull Editor editor, @NotNull PsiFile psiFile) {
// Check parameter description completion.
final CaretModel caretModel = editor.getCaretModel();
final Pair<JavadocHelper.JavadocParameterInfo, List<JavadocHelper.JavadocParameterInfo>> pair = myHelper.parse(psiFile, editor, caretModel.getOffset());
if (pair.first == null) {
return false;
}
final JavadocHelper.JavadocParameterInfo next = findNext(pair.second, pair.first);
if (next == null) {
final int line = pair.first.lastLine + 1;
final Document document = editor.getDocument();
if (line < document.getLineCount()) {
StringBuilder indent = new StringBuilder();
boolean insertIndent = true;
final CharSequence text = document.getCharsSequence();
for (int i = document.getLineStartOffset(line), max = document.getLineEndOffset(line); i < max; i++) {
final char c = text.charAt(i);
if (c == ' ' || c == '\t') {
indent.append(c);
continue;
} else if (c == '*') {
indent.append("* ");
if (i < max - 1 && text.charAt(i + 1) != '/') {
insertIndent = false;
}
}
indent.append("\n");
break;
}
if (insertIndent) {
document.insertString(document.getLineStartOffset(line), indent);
}
}
moveCaretToTheLineEndIfPossible(editor, line);
return true;
}
if (next.parameterDescriptionStartPosition != null) {
myHelper.navigate(next.parameterDescriptionStartPosition, editor, psiFile.getProject());
} else {
final LogicalPosition position = myHelper.calculateDescriptionStartPosition(psiFile, pair.second, next);
myHelper.navigate(position, editor, psiFile.getProject());
}
return true;
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class EnterInJavadocParamDescriptionHandler method postProcessEnter.
@Override
public Result postProcessEnter(@NotNull final PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) {
if (!(file instanceof PsiJavaFile) || !CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER || !CodeStyleSettingsManager.getSettings(file.getProject()).JD_ALIGN_PARAM_COMMENTS) {
return Result.Continue;
}
final CaretModel caretModel = editor.getCaretModel();
final int caretOffset = caretModel.getOffset();
if (!isInJavaDoc(editor, caretOffset)) {
return Result.Continue;
}
final Pair<JavadocHelper.JavadocParameterInfo, List<JavadocHelper.JavadocParameterInfo>> pair = myHelper.parse(file, editor, caretOffset);
if (pair.first == null || pair.first.parameterDescriptionStartPosition == null) {
return Result.Continue;
}
final LogicalPosition caretPosition = caretModel.getLogicalPosition();
final LogicalPosition nameEndPosition = pair.first.parameterNameEndPosition;
if (nameEndPosition.line == caretPosition.line && caretPosition.column <= nameEndPosition.column) {
return Result.Continue;
}
final int descriptionStartColumn = pair.first.parameterDescriptionStartPosition.column;
final LogicalPosition desiredPosition = new LogicalPosition(caretPosition.line, descriptionStartColumn);
final Document document = editor.getDocument();
final CharSequence text = document.getCharsSequence();
final int offsetAfterLastWs = CharArrayUtil.shiftForward(text, caretOffset, " \t");
if (editor.offsetToLogicalPosition(offsetAfterLastWs).column < desiredPosition.column) {
final int lineStartOffset = document.getLineStartOffset(desiredPosition.line);
final String toInsert = StringUtil.repeat(" ", desiredPosition.column - (offsetAfterLastWs - lineStartOffset));
ApplicationManager.getApplication().runWriteAction(() -> {
document.insertString(caretOffset, toInsert);
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
});
}
myHelper.navigate(desiredPosition, editor, file.getProject());
return Result.Stop;
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class PyStatementMover method restoreCaretAndSelection.
private static void restoreCaretAndSelection(@NotNull final PsiFile file, @NotNull final Editor editor, boolean selectionStartAtCaret, boolean hasSelection, @NotNull final SelectionContainer selectionContainer, int shift, int offset, @NotNull final MyLineRange toMove) {
final Document document = editor.getDocument();
final SelectionModel selectionModel = editor.getSelectionModel();
final CaretModel caretModel = editor.getCaretModel();
Integer selectionLen = selectionContainer.myLen;
final PsiElement at = file.findElementAt(offset);
if (at != null) {
final PsiElement added = getCommentOrStatement(document, at);
int size = toMove.size;
if (size > 1) {
PsiElement tmp = added.getNextSibling();
while (size > 1 && tmp != null) {
if (tmp instanceof PsiWhiteSpace) {
if (!selectionStartAtCaret)
shift += tmp.getTextLength();
selectionLen += tmp.getTextLength();
}
tmp = tmp.getNextSibling();
size -= 1;
}
}
if (shift < 0)
shift = 0;
final int column = editor.offsetToLogicalPosition(added.getTextRange().getStartOffset()).column;
if (selectionContainer.myAtTheBeginning || column < selectionContainer.myAdditional) {
selectionLen += column;
} else {
selectionLen += selectionContainer.myAdditional;
}
if (selectionContainer.myAtTheBeginning && selectionStartAtCaret)
shift = -column;
}
final int documentLength = document.getTextLength();
int newCaretOffset = offset + shift;
if (newCaretOffset >= documentLength)
newCaretOffset = documentLength;
caretModel.moveToOffset(newCaretOffset);
if (hasSelection) {
if (selectionStartAtCaret) {
int newSelectionEnd = newCaretOffset + selectionLen;
selectionModel.setSelection(newCaretOffset, newSelectionEnd);
} else {
int newSelectionStart = newCaretOffset - selectionLen;
selectionModel.setSelection(newSelectionStart, newCaretOffset);
}
}
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class PyPlainEnterProcessor method getStatementList.
@Nullable
private static PyStatementList getStatementList(PsiElement psiElement, Editor editor) {
if (psiElement instanceof PyStatementListContainer) {
return ((PyStatementListContainer) psiElement).getStatementList();
} else {
final CaretModel caretModel = editor.getCaretModel();
final PsiElement atCaret = psiElement.getContainingFile().findElementAt(caretModel.getOffset());
final PyStatementPart statementPart = PsiTreeUtil.getParentOfType(atCaret, PyStatementPart.class);
if (statementPart != null) {
return statementPart.getStatementList();
}
}
return null;
}
Aggregations