use of com.intellij.openapi.editor.CaretModel 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.CaretModel in project intellij-community by JetBrains.
the class ToggleColumnModeAction method setSelected.
@Override
public void setSelected(AnActionEvent e, boolean state) {
final EditorEx editor = getEditor(e);
final SelectionModel selectionModel = editor.getSelectionModel();
final CaretModel caretModel = editor.getCaretModel();
if (state) {
caretModel.removeSecondaryCarets();
boolean hasSelection = selectionModel.hasSelection();
int selStart = selectionModel.getSelectionStart();
int selEnd = selectionModel.getSelectionEnd();
LogicalPosition blockStart, blockEnd;
if (caretModel.supportsMultipleCarets()) {
LogicalPosition logicalSelStart = editor.offsetToLogicalPosition(selStart);
LogicalPosition logicalSelEnd = editor.offsetToLogicalPosition(selEnd);
int caretOffset = caretModel.getOffset();
blockStart = selStart == caretOffset ? logicalSelEnd : logicalSelStart;
blockEnd = selStart == caretOffset ? logicalSelStart : logicalSelEnd;
} else {
blockStart = selStart == caretModel.getOffset() ? caretModel.getLogicalPosition() : editor.offsetToLogicalPosition(selStart);
blockEnd = selEnd == caretModel.getOffset() ? caretModel.getLogicalPosition() : editor.offsetToLogicalPosition(selEnd);
}
editor.setColumnMode(true);
if (hasSelection) {
selectionModel.setBlockSelection(blockStart, blockEnd);
} else {
selectionModel.removeSelection();
}
} else {
boolean hasSelection = false;
int selStart = 0;
int selEnd = 0;
if (caretModel.supportsMultipleCarets()) {
hasSelection = true;
List<Caret> allCarets = caretModel.getAllCarets();
Caret fromCaret = allCarets.get(0);
Caret toCaret = allCarets.get(allCarets.size() - 1);
if (fromCaret == caretModel.getPrimaryCaret()) {
Caret tmp = fromCaret;
fromCaret = toCaret;
toCaret = tmp;
}
selStart = fromCaret.getLeadSelectionOffset();
selEnd = toCaret.getSelectionStart() == toCaret.getLeadSelectionOffset() ? toCaret.getSelectionEnd() : toCaret.getSelectionStart();
}
editor.setColumnMode(false);
caretModel.removeSecondaryCarets();
if (hasSelection) {
selectionModel.setSelection(selStart, selEnd);
} else {
selectionModel.removeSelection();
}
}
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class PsiElementBaseIntentionAction method getElement.
@Nullable
private static PsiElement getElement(@NotNull Editor editor, @NotNull PsiFile file) {
CaretModel caretModel = editor.getCaretModel();
int position = caretModel.getOffset();
return file.findElementAt(position);
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class FixDocCommentAction method generateComment.
/**
* Generates a comment if possible.
* <p/>
* It's assumed that this method {@link PsiDocumentManager#commitDocument(Document) syncs} all PSI-document
* changes during the processing.
*
* @param anchor target element for which a comment should be generated
* @param editor target editor
* @param commenter commenter to use
* @param project current project
*/
private static void generateComment(@NotNull PsiElement anchor, @NotNull Editor editor, @NotNull CodeDocumentationProvider documentationProvider, @NotNull CodeDocumentationAwareCommenter commenter, @NotNull Project project) {
Document document = editor.getDocument();
int commentStartOffset = anchor.getTextRange().getStartOffset();
int lineStartOffset = document.getLineStartOffset(document.getLineNumber(commentStartOffset));
if (lineStartOffset > 0 && lineStartOffset < commentStartOffset) {
// Example:
// void test1() {
// }
// void test2() {
// <offset>
// }
// We want to insert the comment at the start of the line where 'test2()' is declared.
int nonWhiteSpaceOffset = CharArrayUtil.shiftBackward(document.getCharsSequence(), commentStartOffset - 1, " \t");
commentStartOffset = Math.max(nonWhiteSpaceOffset, lineStartOffset);
}
int commentBodyRelativeOffset = 0;
int caretOffsetToSet = -1;
StringBuilder buffer = new StringBuilder();
String commentPrefix = commenter.getDocumentationCommentPrefix();
if (commentPrefix != null) {
buffer.append(commentPrefix).append("\n");
commentBodyRelativeOffset += commentPrefix.length() + 1;
}
String linePrefix = commenter.getDocumentationCommentLinePrefix();
if (linePrefix != null) {
buffer.append(linePrefix);
commentBodyRelativeOffset += linePrefix.length();
caretOffsetToSet = commentStartOffset + commentBodyRelativeOffset;
}
buffer.append("\n");
commentBodyRelativeOffset++;
String commentSuffix = commenter.getDocumentationCommentSuffix();
if (commentSuffix != null) {
buffer.append(commentSuffix).append("\n");
}
if (buffer.length() <= 0) {
return;
}
document.insertString(commentStartOffset, buffer);
PsiDocumentManager docManager = PsiDocumentManager.getInstance(project);
docManager.commitDocument(document);
Pair<PsiElement, PsiComment> pair = documentationProvider.parseContext(anchor);
if (pair == null || pair.second == null) {
return;
}
String stub = documentationProvider.generateDocumentationContentStub(pair.second);
CaretModel caretModel = editor.getCaretModel();
if (stub != null) {
int insertionOffset = commentStartOffset + commentBodyRelativeOffset;
//if (CodeStyleSettingsManager.getSettings(project).JD_ADD_BLANK_AFTER_DESCRIPTION) {
// buffer.setLength(0);
// if (linePrefix != null) {
// buffer.append(linePrefix);
// }
// buffer.append("\n");
// buffer.append(stub);
// stub = buffer.toString();
//}
document.insertString(insertionOffset, stub);
docManager.commitDocument(document);
pair = documentationProvider.parseContext(anchor);
}
if (caretOffsetToSet >= 0) {
caretModel.moveToOffset(caretOffsetToSet);
}
if (pair == null || pair.second == null) {
return;
}
int start = Math.min(calcStartReformatOffset(pair.first), calcStartReformatOffset(pair.second));
int end = pair.second.getTextRange().getEndOffset();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformatText(anchor.getContainingFile(), start, end);
int caretOffset = caretModel.getOffset();
if (caretOffset > 0 && caretOffset <= document.getTextLength()) {
char c = document.getCharsSequence().charAt(caretOffset - 1);
if (!StringUtil.isWhiteSpace(c)) {
document.insertString(caretOffset, " ");
caretModel.moveToOffset(caretOffset + 1);
}
}
}
use of com.intellij.openapi.editor.CaretModel in project intellij-community by JetBrains.
the class SmartIndentingBackspaceHandler method doCharDeleted.
@Override
protected boolean doCharDeleted(char c, PsiFile file, Editor editor) {
if (myReplacement == null) {
return false;
}
Document document = editor.getDocument();
CaretModel caretModel = editor.getCaretModel();
int endOffset = CharArrayUtil.shiftForward(document.getImmutableCharSequence(), caretModel.getOffset(), " \t");
document.replaceString(myStartOffset, endOffset, myReplacement);
caretModel.moveToOffset(myStartOffset + myReplacement.length());
return true;
}
Aggregations