use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.
the class BackspaceHandler method getBackspaceUnindentPosition.
@Nullable
public static LogicalPosition getBackspaceUnindentPosition(final PsiFile file, final Editor editor) {
if (editor.getSelectionModel().hasSelection())
return null;
final LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition();
if (caretPos.column == 0) {
return null;
}
if (!isWhitespaceBeforeCaret(editor)) {
return null;
}
// Decrease column down to indentation * n
final int indent = CodeStyleSettingsManager.getSettings(file.getProject()).getIndentOptionsByFile(file).INDENT_SIZE;
int column = (caretPos.column - 1) / indent * indent;
if (column < 0) {
column = 0;
}
return new LogicalPosition(caretPos.line, column);
}
use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.
the class BackspaceHandler method isWhitespaceBeforeCaret.
public static boolean isWhitespaceBeforeCaret(Editor editor) {
final LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition();
final CharSequence charSeq = editor.getDocument().getCharsSequence();
// smart backspace is activated only if all characters in the check range are whitespace characters
for (int pos = 0; pos < caretPos.column; pos++) {
// use logicalPositionToOffset to make sure tabs are handled correctly
final LogicalPosition checkPos = new LogicalPosition(caretPos.line, pos);
final int offset = editor.logicalPositionToOffset(checkPos);
if (offset < charSeq.length()) {
final char c = charSeq.charAt(offset);
if (c != '\t' && c != ' ' && c != '\n') {
return false;
}
}
}
return true;
}
use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.
the class DiffDividerDrawUtil method getVisibleInterval.
@NotNull
private static LineRange getVisibleInterval(Editor editor) {
Rectangle area = editor.getScrollingModel().getVisibleArea();
if (area.height < 0)
return new LineRange(0, 0);
LogicalPosition position1 = editor.xyToLogicalPosition(new Point(0, area.y));
LogicalPosition position2 = editor.xyToLogicalPosition(new Point(0, area.y + area.height));
return new LineRange(position1.line, position2.line);
}
use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.
the class DeclarationMover method beforeMove.
@Override
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
super.beforeMove(editor, info, down);
if (myEnumToInsertSemicolonAfter != null) {
TreeElement semicolon = Factory.createSingleLeafElement(JavaTokenType.SEMICOLON, ";", 0, 1, null, myEnumToInsertSemicolonAfter.getManager());
try {
PsiElement inserted = myEnumToInsertSemicolonAfter.getParent().addAfter(semicolon.getPsi(), myEnumToInsertSemicolonAfter);
inserted = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(inserted);
final LogicalPosition position = editor.offsetToLogicalPosition(inserted.getTextRange().getEndOffset());
info.toMove2 = new LineRange(position.line + 1, position.line + 1);
} catch (IncorrectOperationException e) {
LOG.error(e);
} finally {
myEnumToInsertSemicolonAfter = null;
}
}
}
use of com.intellij.openapi.editor.LogicalPosition in project intellij-community by JetBrains.
the class JavadocHelper method navigate.
/**
* Tries to navigate caret at the given editor to the target position inserting missing white spaces if necessary.
*
* @param position target caret position
* @param editor target editor
* @param project target project
*/
@SuppressWarnings("MethodMayBeStatic")
public void navigate(@NotNull LogicalPosition position, @NotNull Editor editor, @NotNull final Project project) {
final Document document = editor.getDocument();
final CaretModel caretModel = editor.getCaretModel();
final int endLineOffset = document.getLineEndOffset(position.line);
final LogicalPosition endLinePosition = editor.offsetToLogicalPosition(endLineOffset);
if (endLinePosition.column < position.column && !editor.getSettings().isVirtualSpace() && !editor.isViewer()) {
final String toInsert = StringUtil.repeat(" ", position.column - endLinePosition.column);
ApplicationManager.getApplication().runWriteAction(() -> {
document.insertString(endLineOffset, toInsert);
PsiDocumentManager.getInstance(project).commitDocument(document);
});
}
caretModel.moveToLogicalPosition(position);
}
Aggregations