/** * Sets the specified mark to the caret position of the editor * * @param editor The editor to get the current position from * @param ch The mark set set * @return True if a valid, writable mark, false if not */ public boolean setMark(@NotNull Editor editor, char ch) { return VALID_SET_MARKS.indexOf(ch) >= 0 && setMark(editor, ch, editor.getCaretModel().getOffset()); }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (editor == null) { LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } PsiElement reference = PsiTreeUtil.getNonStrictParentOfType(startElement, GoReferenceExpressionBase.class); PsiElement anchor = reference != null ? findAnchor(reference) : null; if (anchor == null) { LOG.error("Cannot find anchor for " + myWhat + " (GoUnresolvedFixBase), offset: " + editor.getCaretModel().getOffset(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } Template template = TemplateSettings.getInstance().getTemplateById(myTemplateId); if (template == null) { LOG.error("Cannot find anchor for " + myWhat + " (GoUnresolvedFixBase), offset: " + editor.getCaretModel().getOffset(), AttachmentFactory.createAttachment(file.getVirtualFile())); return; } int start = anchor.getTextRange().getStartOffset(); editor.getCaretModel().moveToOffset(start); template.setToReformat(true); TemplateManager.getInstance(project).startTemplate(editor, template, true, ContainerUtil.stringMap("NAME", myName), null); }
int lline = editor.getCaretModel().getLogicalPosition().line; int total = EditorHelper.getLineCount(editor); int pct = (int)((float)lline / (float)total * 100f + 0.5); msg.append(" --").append(pct).append("%-- "); LogicalPosition lp = editor.getCaretModel().getLogicalPosition(); int col = editor.getCaretModel().getOffset() - doc.getLineStartOffset(lline);
? editor.getCaretModel().getVisualPosition().line : EditorHelper.logicalLineToVisualLine(editor, line - 1); break; if (visualLine != editor.getCaretModel().getVisualPosition().line || start) { int offset; if (start) { offset = moveCaretVertical(editor, editor.getCaretModel().getPrimaryCaret(), EditorHelper.visualLineToLogicalLine(editor, visualLine) - editor.getCaretModel().getLogicalPosition().line); moveCaret(editor, editor.getCaretModel().getPrimaryCaret(), offset);
startLine = defaultLine == -1 ? editor.getCaretModel().getLogicalPosition().line : defaultLine; endLine = startLine; boolean lastZero = false; endLine = range.getLine(editor, context, lastZero); if (range.isMove()) { MotionGroup.moveCaret(editor, editor.getCaretModel().getPrimaryCaret(), VimPlugin.getMotion().moveCaretToLine(editor, endLine));
caretModel.removeSecondaryCarets(); VisualPosition vp = caretModel.getVisualPosition(); int col = EditorHelper.normalizeVisualColumn(editor, vp.line, vp.column, CommandState.getInstance(editor).getMode() == CommandState.Mode.REPLACE); if (col != vp.column) { caretModel.moveToVisualPosition(new VisualPosition(vp.line, col)); caretModel.moveToOffset(CaretData.getVisualEnd(caretModel.getPrimaryCaret())); break; case VISUAL_LINE: caretModel.moveToLogicalPosition(editor.xyToLogicalPosition(event.getPoint())); break; CaretData.setVisualOffset(caretModel.getPrimaryCaret(), caretModel.getOffset()); CaretData.setLastColumn(editor, caretModel.getPrimaryCaret(), caretModel.getVisualPosition().column);
@Override public boolean doEnter(PsiElement psiElement, PsiFile file, @NotNull Editor editor, boolean modified) { GoBlock block = findBlock(psiElement); if (block != null) { editor.getCaretModel().moveToOffset(block.getLbrace().getTextRange().getEndOffset()); } plainEnter(editor); return true; } }
@Override public void handleInsert(@NotNull InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); CharSequence documentText = context.getDocument().getImmutableCharSequence(); int offset = skipWhiteSpaces(editor.getCaretModel().getOffset(), documentText); if (documentText.charAt(offset) != '{') { Project project = context.getProject(); Template template = TemplateManager.getInstance(project).createTemplate("braces", "go", myOneLine ? "{$END$}" : " {\n$END$\n}"); template.setToReformat(true); TemplateManager.getInstance(project).startTemplate(editor, template); } else { editor.getCaretModel().moveToOffset(offset); ApplicationManager.getApplication().runWriteAction(() -> { EditorActionHandler enterAction = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE); enterAction.execute(editor, editor.getCaretModel().getCurrentCaret(), ((EditorEx)editor).getDataContext()); }); } }
if (CommandState.getInstance(editor).getMode() != CommandState.Mode.VISUAL) { if (rawCount > 0) { if (editor.getCaretModel().getCaretCount() > 1) { return false; VisualChange range = CaretData.getLastVisualOperatorRange(editor.getCaretModel().getPrimaryCaret()); if (range == null) { return false; int start = editor.getCaretModel().getOffset(); int end = calculateVisualRange(editor, range, count); Caret primaryCaret = editor.getCaretModel().getPrimaryCaret(); CommandState.getInstance(editor).pushState(CommandState.Mode.VISUAL, mode, MappingMode.VISUAL); CaretData.setVisualStart(primaryCaret, start); updateBlockSelection(editor, editor.getSelectionModel().getSelectionEnd()); MotionGroup .moveCaret(editor, editor.getCaretModel().getPrimaryCaret(), EditorData.getVisualBlockEnd(editor), true); for (Caret caret : editor.getCaretModel().getAllCarets()) { CaretData.setVisualStart(caret, caret.getSelectionStart()); updateSelection(editor, caret, caret.getSelectionEnd()); for (Caret caret : editor.getCaretModel().getAllCarets()) { updateSelection(editor, caret, CaretData.getVisualEnd(caret));
PsiFile currentFile = event.getData(LangDataKeys.PSI_FILE); CaretModel caretModel = editor.getCaretModel(); LogicalPosition oldLogicPos = caretModel.getLogicalPosition(); String text = currentFile.getText(); List<String> lines = Splitter.on("\n").splitToList(text); LogicalPosition newStatementPos = new LogicalPosition(pojo.getLineNumber() , pojo.getLineStartPos() + 1); LogicalPosition insertPos = new LogicalPosition(pojo.getLineNumber() + 1 , 0 ); caretModel.moveToLogicalPosition(newStatementPos); PsiElement currentFileElement = event.getData(LangDataKeys.PSI_ELEMENT); if (currentFileElement instanceof PsiClassImpl || currentFileElement instanceof ClsClassImpl) { caretModel.moveToLogicalPosition(insertPos); Integer offset = caretModel.getOffset(); String insert = insertSetter(project, pojo, document, currentFileElement, offset); request.setInsert(insert); caretModel.moveToLogicalPosition(oldLogicPos); SendToServerService.post(project,request); } catch (Throwable ignored) {
private void doIndent(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext, EditorActionHandler originalHandler, Document document) { originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext); PsiDocumentManager.getInstance(file.getProject()).commitDocument(document); try { CodeStyleManager.getInstance(file.getProject()).adjustLineIndent(file, editor.getCaretModel().getOffset()); } catch (IncorrectOperationException e) { LOG.error(e); } } }
private boolean isCaretLine(int line, @NotNull Editor editor) { return line == editor.getCaretModel().getLogicalPosition().line; }
@Override public Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) { if (!file.getLanguage().is(BallerinaLanguage.INSTANCE)) { return Result.Continue; } // We need to save the file before checking. Otherwise issues can occur when we press enter in a string. Project project = file.getProject(); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); // Checks whether the previous line starts with "#". LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition(); int prevLine = caretPos.line - 1; String lineString = editor.getDocument().getText( new TextRange(editor.getDocument().getLineStartOffset(prevLine), editor.getDocument().getLineEndOffset(prevLine))); if (lineString.trim().startsWith("#")) { int newCol = lineString.indexOf("#"); String enteredText = editor.getDocument().getText( new TextRange(editor.getDocument().getLineStartOffset(caretPos.line), editor.getDocument().getLineEndOffset(caretPos.line))).trim(); editor.getDocument().deleteString(editor.getDocument().getLineStartOffset(caretPos.line), editor.getDocument().getLineEndOffset(caretPos.line)); editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretPos.line, 1)); enterNewLine(editor, enteredText, newCol); // Commit the document. PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); } return Result.Continue; }
protected PsiElement getCurrentElement(Editor editor, PsiFile file) { Caret currentCaret = editor.getCaretModel().getCurrentCaret(); // look for current element int currentOffset = currentCaret.getOffset(); PsiElement currentElement = file.findElementAt(currentOffset); if (currentElement == null) { currentOffset--; currentElement = file.findElementAt(currentOffset); } return currentElement; }
/** * Scroll to the error specified by the given tree path, or do nothing * if no error is specified. * * @param treePath the tree path to scroll to. */ private void scrollToError(final TreePath treePath) { final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); if (treeNode == null || !(treeNode.getUserObject() instanceof ResultTreeNode)) { return; } final ResultTreeNode nodeInfo = (ResultTreeNode) treeNode.getUserObject(); if (nodeInfo.getFile() == null || nodeInfo.getProblem() == null) { return; // no problem here :-) } final VirtualFile virtualFile = nodeInfo.getFile().getVirtualFile(); if (virtualFile == null || !virtualFile.exists()) { return; } final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); final FileEditor[] editor = fileEditorManager.openFile( virtualFile, true); if (editor.length > 0 && editor[0] instanceof TextEditor) { final LogicalPosition problemPos = new LogicalPosition( Math.max(lineFor(nodeInfo) - 1, 0), Math.max(columnFor(nodeInfo), 0)); final Editor textEditor = ((TextEditor) editor[0]).getEditor(); textEditor.getCaretModel().moveToLogicalPosition(problemPos); textEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER); } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (!(file instanceof GoFile) || editor == null || !(startElement instanceof GoBlock)) return; PsiElement brace = ((GoBlock)startElement).getRbrace(); if (brace == null) return; Template template = TemplateSettings.getInstance().getTemplateById("go_lang_add_return"); if (template == null) return; int start = brace.getTextRange().getStartOffset(); editor.getCaretModel().moveToOffset(start); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); template.setToReformat(true); TemplateManager.getInstance(project).startTemplate(editor, template, true, Collections.emptyMap(), null); } }
/** * Gets the number of characters on the current line. This will be different than the number of visual * characters if there are "real" tabs in the line. * * @param editor The editor * @return The number of characters in the current line */ public static int getLineLength(@NotNull final Editor editor) { return getLineLength(editor, editor.getCaretModel().getLogicalPosition().line); }
public boolean scrollScreen(@NotNull final Editor editor, int rawCount, boolean down) { final CaretModel caretModel = editor.getCaretModel(); final int currentLogicalLine = caretModel.getLogicalPosition().line; final int yInitialCaret = editor.visualLineToY(caretModel.getVisualPosition().line); final int yTargetVisualLine = editor.visualLineToY(targetCaretVisualLine); if (Math.abs(yTargetVisualLine - yInitialCaret) > visibleArea.height) { moveCaret(editor, caretModel.getPrimaryCaret(), caretOffset);
protected boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull Command cmd) { final Argument argument = cmd.getArgument(); if (argument == null) { return false; } TextRange range = MotionGroup .getMotionRange(editor, editor.getCaretModel().getPrimaryCaret(), context, cmd.getCount(), cmd.getRawCount(), argument, false); if (range == null) { return false; } LogicalPosition current = editor.getCaretModel().getLogicalPosition(); LogicalPosition start = editor.offsetToLogicalPosition(range.getStartOffset()); LogicalPosition end = editor.offsetToLogicalPosition(range.getEndOffset()); if (current.line != start.line) { MotionGroup.moveCaret(editor, editor.getCaretModel().getPrimaryCaret(), range.getStartOffset()); } int count; if (start.line < end.line) { count = end.line - start.line + 1; } else { count = 1; } Command command = new Command(count, null, null, Command.Type.UNDEFINED, 0); VimPlugin.getProcess().startFilterCommand(editor, context, command); return true; } }
Editor editor = getEditor(); Object psiElement = FileEditorManager.getInstance(getProject()) .getData(CommonDataKeys.PSI_ELEMENT.getName(), editor, editor.getCaretModel().getCurrentCaret()); MapDataContext dataContext = new MapDataContext(); dataContext.put(CommonDataKeys.PROJECT, getProject());