use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.
the class PopupFactoryImpl method getVisibleBestPopupLocation.
@Nullable
private static Point getVisibleBestPopupLocation(@NotNull Editor editor) {
VisualPosition visualPosition = editor.getUserData(ANCHOR_POPUP_POSITION);
if (visualPosition == null) {
CaretModel caretModel = editor.getCaretModel();
if (caretModel.isUpToDate()) {
visualPosition = caretModel.getVisualPosition();
} else {
visualPosition = editor.offsetToVisualPosition(caretModel.getOffset());
}
}
Point p = editor.visualPositionToXY(new VisualPosition(visualPosition.line + 1, visualPosition.column));
final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
return !visibleArea.contains(p) && !visibleArea.contains(p.x, p.y - editor.getLineHeight()) ? null : p;
}
use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.
the class DeleteToLineStartAndEndActionsTest method testMoveCaretFromVirtualSpaceToRealOffset.
public void testMoveCaretFromVirtualSpaceToRealOffset() throws IOException {
boolean virtualSpacesBefore = EditorSettingsExternalizable.getInstance().isVirtualSpace();
EditorSettingsExternalizable.getInstance().setVirtualSpace(true);
try {
doTestDeleteToStart("\n a a \n b b \n", new VisualPosition(1, 10), "\n<caret>\n b b \n", new VisualPosition(1, 0));
doTestDeleteToStart("\n a a \n b b \n", new VisualPosition(2, 10), "\n a a \n<caret>\n", new VisualPosition(2, 0));
doTestDeleteToStart("\n a a \n b b \n", new VisualPosition(3, 10), "\n a a \n b b \n<caret>", new VisualPosition(3, 0));
doTestDeleteToStart("\n a a \n b b \n", new VisualPosition(20, 10), "\n a a \n b b \n<caret>", new VisualPosition(3, 0));
doTestDeleteToEnd("\n a a \n b b \n", new VisualPosition(1, 10), "\n a a <caret> b b \n", new VisualPosition(1, 5));
// keep old behavior - not sure if it was intentional or not
doTestDeleteToEnd("\n a a \n b b \n", new VisualPosition(3, 10), "\n a a \n b b \n<caret>", new VisualPosition(3, 10));
} finally {
EditorSettingsExternalizable.getInstance().setVirtualSpace(virtualSpacesBefore);
}
}
use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.
the class NextPrevWordHandler method doExecute.
@Override
protected void doExecute(Editor editor, @Nullable Caret caret, DataContext dataContext) {
assert caret != null;
if (EditorUtil.isPasswordEditor(editor)) {
int selectionStartOffset = caret.getLeadSelectionOffset();
caret.moveToOffset(myNext ? editor.getDocument().getTextLength() : 0);
if (myWithSelection)
caret.setSelection(selectionStartOffset, caret.getOffset());
} else {
VisualPosition currentPosition = caret.getVisualPosition();
if (caret.isAtBidiRunBoundary() && (myNext ^ currentPosition.leansRight)) {
int selectionStartOffset = caret.getLeadSelectionOffset();
VisualPosition selectionStartPosition = caret.getLeadSelectionPosition();
caret.moveToVisualPosition(currentPosition.leanRight(!currentPosition.leansRight));
if (myWithSelection) {
caret.setSelection(selectionStartPosition, selectionStartOffset, caret.getVisualPosition(), caret.getOffset());
}
} else {
if (myNext ^ caret.isAtRtlLocation()) {
EditorActionUtil.moveCaretToNextWord(editor, myWithSelection, myInDifferentHumpsMode ^ editor.getSettings().isCamelWords());
} else {
EditorActionUtil.moveCaretToPreviousWord(editor, myWithSelection, myInDifferentHumpsMode ^ editor.getSettings().isCamelWords());
}
}
}
}
use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.
the class IntentionHintComponent method getHintPosition.
@NotNull
private static Point getHintPosition(Editor editor) {
if (ApplicationManager.getApplication().isUnitTestMode())
return new Point();
final int offset = editor.getCaretModel().getOffset();
final VisualPosition pos = editor.offsetToVisualPosition(offset);
int line = pos.line;
final Point position = editor.visualPositionToXY(new VisualPosition(line, 0));
LOG.assertTrue(editor.getComponent().isDisplayable());
JComponent convertComponent = editor.getContentComponent();
Point realPoint;
final boolean oneLineEditor = editor.isOneLineMode();
if (oneLineEditor) {
// place bulb at the corner of the surrounding component
final JComponent contentComponent = editor.getContentComponent();
Container ancestorOfClass = SwingUtilities.getAncestorOfClass(JComboBox.class, contentComponent);
if (ancestorOfClass != null) {
convertComponent = (JComponent) ancestorOfClass;
} else {
ancestorOfClass = SwingUtilities.getAncestorOfClass(JTextField.class, contentComponent);
if (ancestorOfClass != null) {
convertComponent = (JComponent) ancestorOfClass;
}
}
realPoint = new Point(-(AllIcons.Actions.RealIntentionBulb.getIconWidth() / 2) - 4, -(AllIcons.Actions.RealIntentionBulb.getIconHeight() / 2));
} else {
// try to place bulb on the same line
int yShift = -(NORMAL_BORDER_SIZE + AllIcons.Actions.RealIntentionBulb.getIconHeight());
if (canPlaceBulbOnTheSameLine(editor)) {
final int borderHeight = NORMAL_BORDER_SIZE;
yShift = -(borderHeight + (AllIcons.Actions.RealIntentionBulb.getIconHeight() - editor.getLineHeight()) / 2 + 3);
}
final int xShift = AllIcons.Actions.RealIntentionBulb.getIconWidth();
Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
realPoint = new Point(Math.max(0, visibleArea.x - xShift), position.y + yShift);
}
Point location = SwingUtilities.convertPoint(convertComponent, realPoint, editor.getComponent().getRootPane().getLayeredPane());
return new Point(location.x, location.y);
}
use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.
the class NamedElementDuplicateHandler method executeWriteAction.
@Override
public void executeWriteAction(Editor editor, DataContext dataContext) {
Project project = editor.getProject();
if (project != null && !editor.getSelectionModel().hasSelection()) {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (file != null) {
VisualPosition caret = editor.getCaretModel().getVisualPosition();
Pair<LogicalPosition, LogicalPosition> lines = EditorUtil.calcSurroundingRange(editor, caret, caret);
TextRange toDuplicate = new TextRange(editor.logicalPositionToOffset(lines.first), editor.logicalPositionToOffset(lines.second));
PsiElement name = findNameIdentifier(editor, file, toDuplicate);
if (name != null && !name.getTextRange().containsOffset(editor.getCaretModel().getOffset())) {
editor.getCaretModel().moveToOffset(name.getTextOffset());
}
}
}
myOriginal.execute(editor, dataContext);
}
Aggregations