/** * Vim doesn't start a delimited range on a newline or end a range on an empty line (try 'vi{' * while within a function for proof). */ public static Position fixLeftDelimiter(TextContent model, CursorService cursor, Position delim) { // check if the character after delimiter is a newline if (isNewLine(model.getText(delim.getModelOffset() + 1, 1))) { // start after newline LineInformation line = model.getLineInformationOfOffset(delim.getModelOffset()); LineInformation nextLine = model.getLineInformation(line.getNumber() + 1); delim = cursor.newPositionForModelOffset(nextLine.getBeginOffset()); } else { delim = delim.addModelOffset(1); } return delim; }
/** * @param line a line in the text. * @return the offset where the first non-whitespace character occurs in the given line. */ public static int getFirstNonWhiteSpaceOffset( final TextContent content, final LineInformation line) { int index = line.getBeginOffset(); final int end = line.getEndOffset(); while (index < end) { final String s = content.getText(index, 1); if (!isWhiteSpace(s)) { break; } index += 1; } return index; }
/** * Grab the word currently under the cursor. If wholeWord, end at nearest whitespace. Otherwise, * use Options.KEYWORDS values. */ public static String getWordUnderCursor( final EditorAdaptor editorAdaptor, final boolean wholeWord) { String word = ""; TextContent p = editorAdaptor.getViewContent(); int index = editorAdaptor.getCursorService().getPosition().getViewOffset(); LineInformation line = p.getLineInformationOfOffset(index); int min = line.getBeginOffset(); int max = line.getEndOffset(); int first = -1; int last = -1; String s; boolean found = false; String keywords = wholeWord ? "\\S" : editorAdaptor.getConfiguration().get(Options.KEYWORDS); if (index < max) { s = p.getText(index, 1); if (Utils.characterType(s.charAt(0), keywords) == Utils.WORD) { found = true; first = index; last = index; } } while (index < max - 1) { index += 1; s = p.getText(index, 1); if (Utils.characterType(s.charAt(0), keywords) == Utils.WORD) { last = index; if (!found) { first = index; found = true; } } else if (found) { break; } } if (found) { index = first; while (index > min) { index -= 1; s = p.getText(index, 1); if (Utils.characterType(s.charAt(0), keywords) == Utils.WORD) { first = index; } else { break; } } word = p.getText(first, last - first + 1); } return word; }
/** * @param line a line in the text. * @return the offset of the last non-whitespace character. */ public static int getLastNonWhiteSpaceOffset( final TextContent content, final LineInformation line) { if (line.getLength() == 0) { return line.getBeginOffset(); } int index = line.getEndOffset() - 1; final int begin = line.getBeginOffset(); while (index > begin) { final String s = content.getText(index, 1); if (!isWhiteSpace(s)) { break; } index--; } return index; }
public static Position fixRightDelimiter( TextContent model, CursorService cursor, Position delim) { int delimIndex = delim.getModelOffset(); LineInformation line = model.getLineInformationOfOffset(delimIndex); int lineStart = line.getBeginOffset(); if (delimIndex > lineStart) { // is everything before the delimiter just whitespace? String text = model.getText(lineStart, delimIndex - lineStart); if (VimUtils.isBlank(text)) { // end on previous line LineInformation previousLine = model.getLineInformation(line.getNumber() - 1); delimIndex = previousLine.getEndOffset(); delim = cursor.newPositionForModelOffset(delimIndex); } } return delim; }
/** * Calculates an offset position. Line breaks are not counted. * * @param position TODO */ public static int calculatePositionForOffset( final TextContent p, int position, final int offset) { LineInformation line = p.getLineInformationOfOffset(position); if (offset < 0) { int i = -offset; while (i > 0) { if (position > line.getBeginOffset()) { position -= 1; } else { final int nextLine = line.getNumber() - 1; if (nextLine < 0) { break; } line = p.getLineInformation(nextLine); position = Math.max(line.getBeginOffset(), line.getEndOffset() - 1); } i -= 1; } } else if (offset > 0) { int i = offset; int end = Math.max(line.getBeginOffset(), line.getEndOffset() - 1); while (i > 0) { if (position < end) { position += 1; } else { final int nextLine = line.getNumber() + 1; if (nextLine > p.getNumberOfLines() - 1) { break; } line = p.getLineInformation(nextLine); end = Math.max(line.getBeginOffset(), line.getEndOffset() - 1); position = line.getBeginOffset(); } i -= 1; } } return position; }
public static int calculateLine(final TextContent text, final int offset) { final LineInformation line = text.getLineInformationOfOffset(offset); return line.getNumber(); }
public static int calculateColForOffset(final TextContent p, final int modelOffset) { final LineInformation line = p.getLineInformationOfOffset(modelOffset); return modelOffset - line.getBeginOffset(); }
/** @return true, if the last character in the text buffer is newline */ public static boolean endsWithEOL(final EditorAdaptor editor) { final TextContent content = editor.getModelContent(); final LineInformation line = content.getLineInformation(content.getNumberOfLines() - 1); return line.getNumber() > 0 && line.getLength() == 0; }
/** @return true, if line contains only whitespace characters */ public static boolean isLineBlank(final TextContent content, final int lineNo) { final LineInformation line = content.getLineInformation(lineNo); return VimUtils.isBlank(content.getText(line.getBeginOffset(), line.getLength())); }
/** * @param content textContent * @param line a line in the text. * @return the content of the given line, without preceeding whitespace. */ public static String getWithoutIndent(final TextContent content, final LineInformation info) { final int offset = getFirstNonWhiteSpaceOffset(content, info); return content.getText(offset, info.getEndOffset() - offset); }
/** * @param vim the vim emulator. * @param line a line in the text. * @return the whitespace at the begin of the given line. */ public static String getIndent(final TextContent content, final LineInformation line) { final int offset = getFirstNonWhiteSpaceOffset(content, line); return content.getText(line.getBeginOffset(), offset - line.getBeginOffset()); }