use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class CopyGroup method putText.
/**
* This performs the actual insert of the paste
*
* @param editor The editor to paste into
* @param context The data context
* @param offset The location within the file to paste the text
* @param text The text to paste
* @param type The type of paste
* @param count The number of times to paste the text
* @param indent True if pasted lines should be autoindented, false if not
* @param cursorAfter If true move cursor to just after pasted text
* @param mode The type of hightlight prior to the put.
*/
public void putText(@NotNull Editor editor, @NotNull DataContext context, int offset, @NotNull String text, @NotNull SelectionType type, int count, boolean indent, boolean cursorAfter, @NotNull CommandState.SubMode mode) {
if (logger.isDebugEnabled()) {
logger.debug("offset=" + offset);
logger.debug("type=" + type);
logger.debug("mode=" + mode);
}
if (mode == CommandState.SubMode.VISUAL_LINE && editor.isOneLineMode()) {
return;
}
// Don't indent if this there isn't anything about a linewise selection or register
if (indent && type != SelectionType.LINE_WISE && mode != CommandState.SubMode.VISUAL_LINE) {
indent = false;
}
if (type == SelectionType.LINE_WISE && text.length() > 0 && text.charAt(text.length() - 1) != '\n') {
text = text + '\n';
}
int insertCnt = 0;
int endOffset = offset;
if (type != SelectionType.BLOCK_WISE) {
for (int i = 0; i < count; i++) {
VimPlugin.getChange().insertText(editor, offset, text);
insertCnt += text.length();
endOffset += text.length();
}
} else {
LogicalPosition start = editor.offsetToLogicalPosition(offset);
int col = mode == CommandState.SubMode.VISUAL_LINE ? 0 : start.column;
int line = start.line;
if (logger.isDebugEnabled()) {
logger.debug("col=" + col + ", line=" + line);
}
int lines = 1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '\n') {
lines++;
}
}
if (line + lines >= EditorHelper.getLineCount(editor)) {
for (int i = 0; i < line + lines - EditorHelper.getLineCount(editor); i++) {
VimPlugin.getChange().insertText(editor, EditorHelper.getFileSize(editor, true), "\n");
insertCnt++;
}
}
StringTokenizer parser = new StringTokenizer(text, "\n");
int maxlen = 0;
while (parser.hasMoreTokens()) {
String segment = parser.nextToken();
maxlen = Math.max(maxlen, segment.length());
}
parser = new StringTokenizer(text, "\n");
while (parser.hasMoreTokens()) {
String segment = parser.nextToken();
String origSegment = segment;
if (segment.length() < maxlen) {
logger.debug("short line");
StringBuilder extra = new StringBuilder(maxlen - segment.length());
for (int i = segment.length(); i < maxlen; i++) {
extra.append(' ');
}
segment = segment + extra.toString();
if (col != 0 && col < EditorHelper.getLineLength(editor, line)) {
origSegment = segment;
}
}
String pad = EditorHelper.pad(editor, line, col);
int insoff = editor.logicalPositionToOffset(new LogicalPosition(line, col));
endOffset = insoff;
if (logger.isDebugEnabled()) {
logger.debug("segment='" + segment + "'");
logger.debug("origSegment='" + origSegment + "'");
logger.debug("insoff=" + insoff);
}
for (int i = 0; i < count; i++) {
String txt = i == 0 ? origSegment : segment;
VimPlugin.getChange().insertText(editor, insoff, txt);
insertCnt += txt.length();
endOffset += txt.length();
}
if (mode == CommandState.SubMode.VISUAL_LINE) {
VimPlugin.getChange().insertText(editor, endOffset, "\n");
insertCnt++;
endOffset++;
} else {
if (pad.length() > 0) {
VimPlugin.getChange().insertText(editor, insoff, pad);
insertCnt += pad.length();
endOffset += pad.length();
}
}
line++;
}
}
LogicalPosition slp = editor.offsetToLogicalPosition(offset);
/*
int adjust = 0;
if ((type & Command.FLAG_MOT_LINEWISE) != 0)
{
adjust = -1;
}
*/
LogicalPosition elp = editor.offsetToLogicalPosition(endOffset - 1);
if (logger.isDebugEnabled()) {
logger.debug("slp.line=" + slp.line);
logger.debug("elp.line=" + elp.line);
}
if (indent) {
int startOff = editor.getDocument().getLineStartOffset(slp.line);
int endOff = editor.getDocument().getLineEndOffset(elp.line);
VimPlugin.getChange().autoIndentRange(editor, context, new TextRange(startOff, endOff));
}
/*
boolean indented = false;
for (int i = slp.line; indent && i <= elp.line; i++)
{
MotionGroup.moveCaret(editor, context, editor.logicalPositionToOffset(new LogicalPosition(i, 0)));
KeyHandler.executeAction("OrigAutoIndentLines", context);
indented = true;
}
*/
if (logger.isDebugEnabled()) {
logger.debug("insertCnt=" + insertCnt);
}
if (indent) {
endOffset = EditorHelper.getLineEndOffset(editor, elp.line, true);
insertCnt = endOffset - offset;
if (logger.isDebugEnabled()) {
logger.debug("insertCnt=" + insertCnt);
}
}
int cursorMode;
if (type == SelectionType.BLOCK_WISE) {
if (mode == CommandState.SubMode.VISUAL_LINE) {
cursorMode = cursorAfter ? 4 : 1;
} else {
cursorMode = cursorAfter ? 5 : 1;
}
} else if (type == SelectionType.LINE_WISE) {
if (mode == CommandState.SubMode.VISUAL_LINE) {
cursorMode = cursorAfter ? 4 : 3;
} else {
cursorMode = cursorAfter ? 4 : 3;
}
} else /* Characterwise */
{
if (mode == CommandState.SubMode.VISUAL_LINE) {
cursorMode = cursorAfter ? 4 : 1;
} else {
cursorMode = cursorAfter ? 5 : 2;
}
}
switch(cursorMode) {
case 1:
MotionGroup.moveCaret(editor, offset);
break;
case 2:
MotionGroup.moveCaret(editor, endOffset - 1);
break;
case 3:
MotionGroup.moveCaret(editor, offset);
MotionGroup.moveCaret(editor, VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor));
break;
case 4:
MotionGroup.moveCaret(editor, endOffset + 1);
break;
case 5:
int pos = Math.min(endOffset, EditorHelper.getLineEndForOffset(editor, endOffset - 1) - 1);
MotionGroup.moveCaret(editor, pos);
break;
}
VimPlugin.getMark().setChangeMarks(editor, new TextRange(offset, endOffset));
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class FileGroup method displayLocationInfo.
public void displayLocationInfo(@NotNull Editor editor) {
StringBuilder msg = new StringBuilder();
Document doc = editor.getDocument();
if (CommandState.getInstance(editor).getMode() != CommandState.Mode.VISUAL) {
LogicalPosition lp = editor.getCaretModel().getLogicalPosition();
int col = editor.getCaretModel().getOffset() - doc.getLineStartOffset(lp.line);
int endoff = doc.getLineEndOffset(lp.line);
if (doc.getCharsSequence().charAt(endoff) == '\n') {
endoff--;
}
int ecol = endoff - doc.getLineStartOffset(lp.line);
LogicalPosition elp = editor.offsetToLogicalPosition(endoff);
msg.append("Col ").append(col + 1);
if (col != lp.column) {
msg.append("-").append(lp.column + 1);
}
msg.append(" of ").append(ecol + 1);
if (ecol != elp.column) {
msg.append("-").append(elp.column + 1);
}
int lline = editor.getCaretModel().getLogicalPosition().line;
int total = EditorHelper.getLineCount(editor);
msg.append("; Line ").append(lline + 1).append(" of ").append(total);
SearchHelper.CountPosition cp = SearchHelper.countWords(editor);
msg.append("; Word ").append(cp.getPosition()).append(" of ").append(cp.getCount());
int offset = editor.getCaretModel().getOffset();
int size = EditorHelper.getFileSize(editor);
msg.append("; Character ").append(offset + 1).append(" of ").append(size);
} else {
msg.append("Selected ");
TextRange vr = VimPlugin.getMotion().getVisualRange(editor);
vr.normalize();
int lines;
SearchHelper.CountPosition cp = SearchHelper.countWords(editor);
int words = cp.getCount();
int word = 0;
if (vr.isMultiple()) {
lines = vr.size();
int cols = vr.getMaxLength();
msg.append(cols).append(" Cols; ");
for (int i = 0; i < vr.size(); i++) {
cp = SearchHelper.countWords(editor, vr.getStartOffsets()[i], vr.getEndOffsets()[i] - 1);
word += cp.getCount();
}
} else {
LogicalPosition slp = editor.offsetToLogicalPosition(vr.getStartOffset());
LogicalPosition elp = editor.offsetToLogicalPosition(vr.getEndOffset());
lines = elp.line - slp.line + 1;
cp = SearchHelper.countWords(editor, vr.getStartOffset(), vr.getEndOffset() - 1);
word = cp.getCount();
}
int total = EditorHelper.getLineCount(editor);
msg.append(lines).append(" of ").append(total).append(" Lines");
msg.append("; ").append(word).append(" of ").append(words).append(" Words");
int chars = vr.getSelectionCount();
int size = EditorHelper.getFileSize(editor);
msg.append("; ").append(chars).append(" of ").append(size).append(" Characters");
}
VimPlugin.showMessage(msg.toString());
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class CmdFilterHandler method execute.
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) throws ExException {
logger.info("execute");
String command = cmd.getArgument();
if (command.length() == 0) {
return false;
}
if (command.indexOf('!') != -1) {
String last = VimPlugin.getProcess().getLastCommand();
if (last == null || last.length() == 0) {
VimPlugin.showMessage(MessageHelper.message(Msg.e_noprev));
return false;
}
command = command.replaceAll("!", last);
}
try {
Ranges ranges = cmd.getRanges();
if (ranges.size() == 0) {
// Show command output in a window
String commandOutput = VimPlugin.getProcess().executeCommand(command, null);
ExOutputModel.getInstance(editor).output(commandOutput);
return true;
} else {
// Filter
TextRange range = cmd.getTextRange(editor, context, false);
return VimPlugin.getProcess().executeFilter(editor, range, command);
}
} catch (IOException e) {
throw new ExException(e.getMessage());
}
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class YankLinesHandler method execute.
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) throws ExException {
StringBuilder arg = new StringBuilder(cmd.getArgument());
char register = VimPlugin.getRegister().getDefaultRegister();
if (arg.length() > 0 && (arg.charAt(0) < '0' || arg.charAt(0) > '9')) {
register = arg.charAt(0);
arg.deleteCharAt(0);
cmd.setArgument(arg.toString());
}
if (!VimPlugin.getRegister().selectRegister(register)) {
return false;
}
TextRange range = cmd.getTextRange(editor, context, true);
return VimPlugin.getCopy().yankRange(editor, range, SelectionType.LINE_WISE, false);
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method deleteCharacter.
/**
* Deletes count characters from the editor
*
* @param editor The editor to remove the characters from
* @param count The number of characters to delete
* @return true if able to delete, false if not
*/
public boolean deleteCharacter(@NotNull Editor editor, int count, boolean isChange) {
int offset = VimPlugin.getMotion().moveCaretHorizontal(editor, count, true);
if (offset != -1) {
boolean res = deleteText(editor, new TextRange(editor.getCaretModel().getOffset(), offset), SelectionType.CHARACTER_WISE);
int pos = editor.getCaretModel().getOffset();
int norm = EditorHelper.normalizeOffset(editor, editor.getCaretModel().getLogicalPosition().line, pos, isChange);
if (norm != pos) {
MotionGroup.moveCaret(editor, norm);
}
return res;
}
return false;
}
Aggregations