use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method insertDeleteInsertedText.
/**
* If the cursor is currently after the start of the current insert this deletes all the newly inserted text.
* Otherwise it deletes all text from the cursor back to the first non-blank in the line.
*
* @param editor The editor to delete the text from
* @return true if able to delete the text, false if not
*/
public boolean insertDeleteInsertedText(@NotNull Editor editor) {
int deleteTo = insertStart;
int offset = editor.getCaretModel().getOffset();
if (offset == insertStart) {
deleteTo = VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor);
}
if (deleteTo != -1) {
deleteRange(editor, new TextRange(deleteTo, offset), SelectionType.CHARACTER_WISE, false);
return true;
}
return false;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method deleteText.
/**
* Delete text from the document. This will fail if being asked to store the deleted text into a read-only
* register.
*
* @param editor The editor to delete from
* @param range The range to delete
* @param type The type of deletion
* @return true if able to delete the text, false if not
*/
private boolean deleteText(@NotNull final Editor editor, @NotNull final TextRange range, @Nullable SelectionType type) {
// Fix for http://youtrack.jetbrains.net/issue/VIM-35
if (!range.normalize(EditorHelper.getFileSize(editor, true))) {
return false;
}
if (type == null || VimPlugin.getRegister().storeText(editor, range, type, true)) {
final Document document = editor.getDocument();
final int[] startOffsets = range.getStartOffsets();
final int[] endOffsets = range.getEndOffsets();
for (int i = range.size() - 1; i >= 0; i--) {
document.deleteString(startOffsets[i], endOffsets[i]);
}
if (type != null) {
int start = range.getStartOffset();
VimPlugin.getMark().setMark(editor, MarkGroup.MARK_CHANGE_POS, start);
VimPlugin.getMark().setChangeMarks(editor, new TextRange(start, start));
}
return true;
}
return false;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ShiftRightHandler method execute.
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) {
TextRange range = cmd.getTextRange(editor, context, true);
VimPlugin.getChange().indentRange(editor, context, range, cmd.getCommand().length(), 1);
return true;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method indentRange.
public void indentRange(@NotNull Editor editor, @NotNull DataContext context, @NotNull TextRange range, int count, int dir) {
if (logger.isDebugEnabled()) {
logger.debug("count=" + count);
}
// API change - don't merge
Project proj = PlatformDataKeys.PROJECT.getData(context);
int tabSize = 8;
int indentSize = 8;
boolean useTabs = true;
VirtualFile file = EditorData.getVirtualFile(editor);
if (file != null) {
FileType type = FileTypeManager.getInstance().getFileTypeByFile(file);
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(proj);
tabSize = settings.getTabSize(type);
indentSize = settings.getIndentSize(type);
useTabs = settings.useTabCharacter(type);
}
int sline = editor.offsetToLogicalPosition(range.getStartOffset()).line;
int eline = editor.offsetToLogicalPosition(range.getEndOffset()).line;
if (range.isMultiple()) {
int col = editor.offsetToLogicalPosition(range.getStartOffset()).column;
int size = indentSize * count;
if (dir == 1) {
// Right shift blockwise selection
StringBuilder space = new StringBuilder();
int tabCnt = 0;
int spcCnt;
if (useTabs) {
tabCnt = size / tabSize;
spcCnt = size % tabSize;
} else {
spcCnt = size;
}
for (int i = 0; i < tabCnt; i++) {
space.append('\t');
}
for (int i = 0; i < spcCnt; i++) {
space.append(' ');
}
for (int l = sline; l <= eline; l++) {
int len = EditorHelper.getLineLength(editor, l);
if (len > col) {
LogicalPosition spos = new LogicalPosition(l, col);
insertText(editor, editor.logicalPositionToOffset(spos), space.toString());
}
}
} else {
// Left shift blockwise selection
CharSequence chars = editor.getDocument().getCharsSequence();
for (int l = sline; l <= eline; l++) {
int len = EditorHelper.getLineLength(editor, l);
if (len > col) {
LogicalPosition spos = new LogicalPosition(l, col);
LogicalPosition epos = new LogicalPosition(l, col + size - 1);
int wsoff = editor.logicalPositionToOffset(spos);
int weoff = editor.logicalPositionToOffset(epos);
int pos;
for (pos = wsoff; pos <= weoff; pos++) {
if (CharacterHelper.charType(chars.charAt(pos), false) != CharacterHelper.CharacterType.WHITESPACE) {
break;
}
}
if (pos > wsoff) {
deleteText(editor, new TextRange(wsoff, pos), null);
}
}
}
}
} else {
// Shift non-blockwise selection
for (int l = sline; l <= eline; l++) {
int soff = EditorHelper.getLineStartOffset(editor, l);
int eoff = EditorHelper.getLineEndOffset(editor, l, true);
int woff = VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor, l);
int col = editor.offsetToVisualPosition(woff).column;
int newCol = Math.max(0, col + dir * indentSize * count);
if (col > 0 || soff != eoff) {
StringBuilder space = new StringBuilder();
int tabCnt = 0;
int spcCnt;
if (useTabs) {
tabCnt = newCol / tabSize;
spcCnt = newCol % tabSize;
} else {
spcCnt = newCol;
}
for (int i = 0; i < tabCnt; i++) {
space.append('\t');
}
for (int i = 0; i < spcCnt; i++) {
space.append(' ');
}
replaceText(editor, soff, woff, space.toString());
}
}
}
if (!CommandState.inInsertMode(editor)) {
if (!range.isMultiple()) {
MotionGroup.moveCaret(editor, VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor, sline));
} else {
MotionGroup.moveCaret(editor, range.getStartOffset());
}
}
EditorData.setLastColumn(editor, editor.getCaretModel().getVisualPosition().column);
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method deleteLine.
/**
* Deletes count lines including the current line
*
* @param editor The editor to remove the lines from
* @param count The number of lines to delete
* @return true if able to delete the lines, false if not
*/
public boolean deleteLine(@NotNull Editor editor, int count) {
int start = VimPlugin.getMotion().moveCaretToLineStart(editor);
int offset = Math.min(VimPlugin.getMotion().moveCaretToLineEndOffset(editor, count - 1, true) + 1, EditorHelper.getFileSize(editor, true));
if (logger.isDebugEnabled()) {
logger.debug("start=" + start);
logger.debug("offset=" + offset);
}
if (offset != -1) {
boolean res = deleteText(editor, new TextRange(start, offset), SelectionType.LINE_WISE);
if (res && editor.getCaretModel().getOffset() >= EditorHelper.getFileSize(editor) && editor.getCaretModel().getOffset() != 0) {
MotionGroup.moveCaret(editor, VimPlugin.getMotion().moveCaretToLineStartSkipLeadingOffset(editor, -1));
}
return res;
}
return false;
}
Aggregations