public static int findUnmatchedBlock(@NotNull Editor editor, @NotNull Caret caret, char type, int count) { CharSequence chars = editor.getDocument().getCharsSequence(); int pos = caret.getOffset(); int loc = blockChars.indexOf(type); // What direction should we go now (-1 is backward, 1 is forward) int dir = loc % 2 == 0 ? -1 : 1; // Which character did we find and which should we now search for char match = blockChars.charAt(loc); char found = blockChars.charAt(loc - dir); return findBlockLocation(chars, found, match, dir, pos, count); }
@NotNull public static String getLeadingWhitespace(@NotNull final Editor editor, final int line) { int start = getLineStartOffset(editor, line); int end = getLeadingCharacterOffset(editor, line); return editor.getDocument().getCharsSequence().subSequence(start, end).toString(); }
public static boolean isLineEmpty(@NotNull final Editor editor, final int line, final boolean allowBlanks) { CharSequence chars = editor.getDocument().getCharsSequence(); int offset = getLineStartOffset(editor, line); if (offset >= chars.length() || chars.charAt(offset) == '\n') { return true; } else if (allowBlanks) { for (; offset < chars.length(); offset++) { if (chars.charAt(offset) == '\n') { return true; } else if (!Character.isWhitespace(chars.charAt(offset))) { return false; } } } return false; }
@NotNull public static CharBuffer getLineBuffer(@NotNull final Editor editor, final int line) { int start = getLineStartOffset(editor, line); return CharBuffer.wrap(editor.getDocument().getCharsSequence(), start, start + getLineCharCount(editor, line)); }
/** * Gets a string representation of the file for the supplied offset range * * @param editor The editor * @param start The starting offset (inclusive) * @param end The ending offset (exclusive) * @return The string, never null but empty if start == end */ @NotNull public static String getText(@NotNull final Editor editor, final int start, final int end) { if (start == end) return ""; final CharSequence documentChars = editor.getDocument().getCharsSequence(); return documentChars.subSequence(normalizeOffset(editor, start), normalizeOffset(editor, end)).toString(); }
/** * Gets the actual number of characters in the file * * @param editor The editor * @param includeEndNewLine True include newline * @return The file's character count */ public static int getFileSize(@NotNull final Editor editor, final boolean includeEndNewLine) { final int len = editor.getDocument().getTextLength(); return includeEndNewLine || len == 0 || editor.getDocument().getCharsSequence().charAt(len - 1) != '\n' ? len : len - 1; }
public static int getLeadingCharacterOffset(@NotNull final Editor editor, final int line, final int col) { int start = getLineStartOffset(editor, line) + col; int end = getLineEndOffset(editor, line, true); CharSequence chars = editor.getDocument().getCharsSequence(); int pos = end; for (int offset = start; offset < end; offset++) { if (offset >= chars.length()) { break; } if (!Character.isWhitespace(chars.charAt(offset))) { pos = offset; break; } } return pos; }
public void displayHexInfo(@NotNull Editor editor) { int offset = editor.getCaretModel().getOffset(); char ch = editor.getDocument().getCharsSequence().charAt(offset); VimPlugin.showMessage(Long.toHexString(ch)); }
private static int findFirstQuoteInLine(@NotNull Editor editor, int pos, char quote) { final int start = EditorHelper.getLineStartForOffset(editor, pos); return findNextQuoteInLine(editor.getDocument().getCharsSequence(), start, quote); }
/** * 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 static int findNextWord(@NotNull Editor editor, @NotNull Caret caret, int count, boolean bigWord) { CharSequence chars = editor.getDocument().getCharsSequence(); final int pos = caret.getOffset(); final int size = EditorHelper.getFileSize(editor); return findNextWord(chars, pos, size, count, bigWord, false); }
public boolean executeFilter(@NotNull Editor editor, @NotNull TextRange range, @NotNull String command) throws IOException { final CharSequence charsSequence = editor.getDocument().getCharsSequence(); final int startOffset = range.getStartOffset(); final int endOffset = range.getEndOffset(); final String output = executeCommand(command, charsSequence.subSequence(startOffset, endOffset)); editor.getDocument().replaceString(startOffset, endOffset, output); return true; }
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)); }
public static int findSection(@NotNull Editor editor, @NotNull Caret caret, char type, int dir, int count) { CharSequence chars = editor.getDocument().getCharsSequence(); int line = caret.getLogicalPosition().line + dir; int maxline = EditorHelper.getLineCount(editor); int res = -1; while (line > 0 && line < maxline && count > 0) { int offset = EditorHelper.getLineStartOffset(editor, line); char ch = chars.charAt(offset); if (ch == type || ch == '\u000C') { res = offset; count--; } line += dir; } if (res == -1) { res = dir < 0 ? 0 : chars.length() - 1; } return res; }
/** * This finds the offset to the end of the next/previous word/WORD. * * @param editor The editor to search in * @param caret The caret to start search from * @param count The number of words to skip. Negative for backward searches * @param bigWord If true then find WORD, if false then find word * @return The offset of match */ public static int findNextWordEnd(@NotNull Editor editor, @NotNull Caret caret, int count, boolean bigWord) { CharSequence chars = editor.getDocument().getCharsSequence(); int pos = caret.getOffset(); int size = EditorHelper.getFileSize(editor); return findNextWordEnd(chars, pos, size, count, bigWord, false); }
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) == ':'; } }
/** * Gets the number of actual lines in the file * * @param editor The editor * @return The file line count */ public static int getLineCount(@NotNull final Editor editor) { int len = editor.getDocument().getLineCount(); if (editor.getDocument().getTextLength() > 0 && editor.getDocument().getCharsSequence().charAt(editor.getDocument().getTextLength() - 1) == '\n') { len--; } return len; }
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) throws ExException { LineRange range = cmd.getLineRange(editor, context); CharSequence chars = editor.getDocument().getCharsSequence(); for (int l = range.getStartLine(); l <= range.getEndLine(); l++) { int start = editor.getDocument().getLineStartOffset(l); int end = editor.getDocument().getLineEndOffset(l); if (logger.isDebugEnabled()) { logger.debug("Line " + l + ", start offset=" + start + ", end offset=" + end); for (int i = start; i <= end; i++) { logger.debug("Offset " + i + ", char=" + chars.charAt(i) + ", lp=" + editor.offsetToLogicalPosition(i) + ", vp=" + editor.offsetToVisualPosition(i)); } } } return true; }
@Override public void handleInsert(@NotNull InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); int tailOffset = context.getTailOffset(); Document document = editor.getDocument(); context.commitDocument(); boolean staysAtChar = document.getTextLength() > tailOffset && document.getCharsSequence().charAt(tailOffset) == myChar; context.setAddCompletionChar(false); if (!staysAtChar) { document.insertString(tailOffset, String.valueOf(myChar)); } editor.getCaretModel().moveToOffset(tailOffset + 1); AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(editor); } }
private boolean isLBraceTokenBrace() { int offset = myFixture.getCaretOffset(); Editor editor = myFixture.getEditor(); CharSequence text = editor.getDocument().getCharsSequence(); FileType fileType = ElixirFileType.INSTANCE; HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset); return BraceMatchingUtil.isLBraceToken(iterator, text, fileType); } }