/** * 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 protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { GoLiveTemplateContextType.File fileContextType = TemplateContextType.EP_NAME.findExtension(GoLiveTemplateContextType.File.class); return fileContextType != null && fileContextType.isInContext(file, editor.getCaretModel().getOffset()); }
public void addJump(@NotNull Editor editor, boolean reset) { addJump(editor, editor.getCaretModel().getOffset(), reset); }
public int moveCaretGotoNextTab(@NotNull Editor editor, @NotNull DataContext context, int rawCount) { final boolean absolute = rawCount >= 1; switchEditorTab(EditorWindow.DATA_KEY.getData(context), absolute ? rawCount - 1 : 1, absolute); return editor.getCaretModel().getOffset(); }
public int moveCaretToUnmatchedBlock(@NotNull Editor editor, @NotNull Caret caret, int count, char type) { if ((editor.getCaretModel().getOffset() == 0 && count < 0) || (editor.getCaretModel().getOffset() >= EditorHelper.getFileSize(editor) - 1 && count > 0)) { return -1; } else { int res = SearchHelper.findUnmatchedBlock(editor, caret, type, count); if (res != -1) { res = EditorHelper.normalizeOffset(editor, res, false); } return res; } }
public int moveCaretGotoPreviousTab(@NotNull Editor editor, @NotNull DataContext context, int rawCount) { switchEditorTab(EditorWindow.DATA_KEY.getData(context), rawCount >= 1 ? -rawCount : -1, false); return editor.getCaretModel().getOffset(); }
public void displayHexInfo(@NotNull Editor editor) { int offset = editor.getCaretModel().getOffset(); char ch = editor.getDocument().getCharsSequence().charAt(offset); VimPlugin.showMessage(Long.toHexString(ch)); }
public void editorReleased(@NotNull EditorFactoryEvent event) { // Save off the last caret position of the file before it is closed Editor editor = event.getEditor(); setMark(editor, '"', editor.getCaretModel().getOffset()); } }, ApplicationManager.getApplication());
/** * This counts all the words in the file. */ @NotNull public static CountPosition countWords(@NotNull Editor editor, int start, int end) { CharSequence chars = editor.getDocument().getCharsSequence(); int offset = editor.getCaretModel().getOffset(); return countWords(chars, start, end, offset); }
public void displayAsciiInfo(@NotNull Editor editor) { int offset = editor.getCaretModel().getOffset(); char ch = editor.getDocument().getCharsSequence().charAt(offset); VimPlugin.showMessage("<" + StringHelper.toKeyNotation(KeyStroke.getKeyStroke(ch)) + "> " + (int)ch + ", Hex " + Long.toHexString(ch) + ", Octal " + Long.toOctalString(ch)); }
private static boolean isCompletionCharAtSpace(Editor editor) { final int startOffset = editor.getCaretModel().getOffset(); final Document document = editor.getDocument(); return document.getTextLength() > startOffset && document.getCharsSequence().charAt(startOffset) == ':'; } }
@Nullable private static GoExpression findExpressionAtOffset(GoIntroduceOperation operation) { PsiFile file = operation.getFile(); int offset = operation.getEditor().getCaretModel().getOffset(); GoExpression expr = PsiTreeUtil.getNonStrictParentOfType(file.findElementAt(offset), GoExpression.class); GoExpression preExpr = PsiTreeUtil.getNonStrictParentOfType(file.findElementAt(offset - 1), GoExpression.class); if (expr == null || preExpr != null && PsiTreeUtil.isAncestor(expr, preExpr, false)) return preExpr; return expr; }
private static void pasteSurround(@NotNull List<KeyStroke> innerValue, @NotNull Editor editor) { // This logic is direct from vim-surround final int offset = editor.getCaretModel().getOffset(); final int lineEndOffset = EditorHelper.getLineEndForOffset(editor, offset); final Mark motionEndMark = VimPlugin.getMark().getMark(editor, ']'); final int motionEndOffset; if (motionEndMark != null) { motionEndOffset = EditorHelper.getOffset(editor, motionEndMark.getLogicalLine(), motionEndMark.getCol()); } else { motionEndOffset = -1; } final String pasteCommand = motionEndOffset == lineEndOffset && offset + 1 == lineEndOffset ? "p" : "P"; setRegister(REGISTER, innerValue); perform(pasteCommand, editor); }
@Override public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) { if (myOriginalHandler != null) myOriginalHandler.execute(editor, c, dataContext); if (c != 'e') return; Project project = editor.getProject(); if (project == null) return; int offset = editor.getCaretModel().getOffset(); if (offset < 4) return; TextRange from = TextRange.from(offset - 4, 4); String text = editor.getDocument().getText(from); if ("case".equals(text)) { PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); ApplicationManager.getApplication().runWriteAction(() -> { if (project.isDisposed()) return; PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project); if (file == null) return; CodeStyleManager.getInstance(project).adjustLineIndent(file, from); }); } } }
public void testWrongYankRegisterMotion() { final Editor editor = typeTextInFile(parseKeys("y\"", "0"), "one <caret>two\n" + "three\n" + "four\n"); assertEquals(0, editor.getCaretModel().getOffset()); }
private <T extends GoExpression> void doTest(@NotNull String text, @NotNull Class<T> aClass, int expected) { myFixture.configureByText("a.go", text); PsiElement element = myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset()); T callExpr = PsiTreeUtil.getParentOfType(element, aClass); assertEquals(expected, GoInspectionUtil.getExpressionResultCount(callExpr)); }
@Override protected void textChanged(DocumentEvent e) { final Editor editor = entry.getEditor(); final boolean forwards = !label.getText().equals("?"); if (incHighlighter != null) { editor.getMarkupModel().removeHighlighter(incHighlighter); } final String pattern = entry.getText(); final TextRange range = SearchGroup.findNext(editor, pattern, editor.getCaretModel().getOffset(), true, forwards); if (range != null) { final TextAttributes color = editor.getColorsScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES); incHighlighter = SearchGroup.highlightMatch(editor, range.getStartOffset(), range.getEndOffset()); incHighlighter.setErrorStripeMarkColor(color.getBackgroundColor()); incHighlighter.setErrorStripeTooltip(pattern); MotionGroup.scrollPositionIntoView(editor, editor.offsetToVisualPosition(range.getStartOffset()), 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()); }); } }
private void doTest(@NotNull String text, String expected) { myFixture.configureByText("a.go", "package a\n" + text); PsiElement element = myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset()); GoType type = PsiTreeUtil.getParentOfType(element, GoType.class); assertNotNull(type); assertEquals(expected, type.getUnderlyingType().getText()); }
@NotNull protected PsiElement findElementAtCaretOrInSelection() { SelectionModel selectionModel = myFixture.getEditor().getSelectionModel(); if (selectionModel.hasSelection()) { PsiElement left = myFixture.getFile().findElementAt(selectionModel.getSelectionStart()); PsiElement right = myFixture.getFile().findElementAt(selectionModel.getSelectionEnd() - 1); assertNotNull(left); assertNotNull(right); return ObjectUtils.assertNotNull(PsiTreeUtil.findCommonParent(left, right)); } else { return ObjectUtils.assertNotNull(myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset())); } }