private Selection createSelection(EditorAdaptor editor, int startLine, int endLine) { TextContent modelContent = editor.getModelContent(); Position start = editor .getCursorService() .newPositionForModelOffset(modelContent.getLineInformation(startLine).getBeginOffset()); Position end = editor .getCursorService() .newPositionForModelOffset(modelContent.getLineInformation(endLine).getBeginOffset()); return new SimpleSelection(new StartEndTextRange(start, end)); }
@Override protected int destination(int offset, TextContent content, int count) { int result = Math.max(0, offset - count); LineInformation lineInformation = content.getLineInformationOfOffset(offset); if (result < lineInformation.getBeginOffset()) { LineInformation resultLine = content.getLineInformation(lineInformation.getNumber() - 1); result = resultLine.getEndOffset(); } return result; }
/** * 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; }
/** * 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; }
void execute(EditorAdaptor editorAdaptor, TextRange region, int count) throws CommandExecutionException { try { String action = left ? ITextEditorActionDefinitionIds.SHIFT_LEFT : ITextEditorActionDefinitionIds.SHIFT_RIGHT; editorAdaptor.getHistory().beginCompoundChange(); editorAdaptor.getHistory().lock(); TextContent modelContent = editorAdaptor.getModelContent(); int startOffset = region.getLeftBound().getModelOffset(); int startLine; // if the start is on a newline, put start on the next line // (otherwise, >i{ will indent the line ending with a '{') if (VimUtils.isNewLine(modelContent.getText(startOffset, 1))) { startLine = modelContent.getLineInformationOfOffset(startOffset).getNumber() + 1; } else { startLine = modelContent.getLineInformationOfOffset(startOffset).getNumber(); } int rightBoundOffset = region.getRightBound().getModelOffset(); LineInformation endLineInformation = modelContent.getLineInformationOfOffset(rightBoundOffset); String endText = modelContent.getText( endLineInformation.getBeginOffset(), rightBoundOffset - endLineInformation.getBeginOffset()); int endLine = endLineInformation.getNumber(); if (rightBoundOffset > endLineInformation.getBeginOffset() && !endText.matches("\\s*")) { // if the right bound is beyond the first character of a line, include that entire line // (unless everything within the bounds of this line is whitespace) endLine++; } for (int i = 0; i < count; i++) { editorAdaptor.setSelection(createSelection(editorAdaptor, startLine, endLine)); EclipseCommand.doIt(1, action, editorAdaptor); } editorAdaptor.setPosition( editorAdaptor .getCursorService() .newPositionForModelOffset( VimUtils.getFirstNonWhiteSpaceOffset( modelContent, modelContent.getLineInformation(startLine))), true); } finally { editorAdaptor.getHistory().unlock(); editorAdaptor.getHistory().endCompoundChange(); } }
public static SearchResult wrapAroundSearch( final EditorAdaptor vim, final Search search, final Position position) { final SearchAndReplaceService searcher = vim.getSearchAndReplaceService(); SearchResult result = searcher.find(search, position); if (!result.isFound() && vim.getConfiguration().get(Options.WRAP_SCAN)) { // redo search from beginning / end of document final TextContent p = vim.getModelContent(); final int index = search.isBackward() ? p.getLineInformation(p.getNumberOfLines() - 1).getEndOffset() - 1 : 0; result = searcher.find(search, position.setModelOffset(index)); } return result; }
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; }
/** @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())); }