Skip to content

Commit

Permalink
VIM-1476 Store last inserted text in ". register
Browse files Browse the repository at this point in the history
  • Loading branch information
michal authored and AlexPl292 committed Apr 28, 2021
1 parent 1479742 commit 96dbbcf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
65 changes: 47 additions & 18 deletions src/com/maddyhome/idea/vim/group/ChangeGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void insertNewLineAbove(final @NotNull Editor editor, @NotNull DataContex

for (Caret caret : editor.getCaretModel().getAllCarets()) {
if (firstLiners.contains(caret)) {
final int offset = VimPlugin.getMotion().moveCaretToLineEnd(editor, 0,true);
final int offset = VimPlugin.getMotion().moveCaretToLineEnd(editor, 0, true);
MotionGroup.moveCaret(editor, caret, offset);
}
}
Expand Down Expand Up @@ -323,7 +323,7 @@ public boolean insertRegister(@NotNull Editor editor, @NotNull DataContext conte
final Register register = VimPlugin.getRegister().getRegister(key);
if (register != null) {
final List<KeyStroke> keys = register.getKeys();
for (KeyStroke k:keys) {
for (KeyStroke k : keys) {
processKey(editor, context, k);
}
return true;
Expand Down Expand Up @@ -540,6 +540,10 @@ public void processEscape(@NotNull Editor editor, @Nullable DataContext context)
repeatInsert(editor, context, cnt == 0 ? 0 : cnt - 1, true);
}

if (CommandState.getInstance(editor).getMode() == CommandState.Mode.INSERT) {
updateLastInsertedTextRegister();
}

// The change pos '.' mark is the offset AFTER processing escape, and after switching to overtype
offset = editor.getCaretModel().getPrimaryCaret().getOffset();
markGroup.setMark(editor, MarkGroup.MARK_CHANGE_POS, offset);
Expand Down Expand Up @@ -738,13 +742,15 @@ public boolean deleteCharacter(@NotNull Editor editor, @NotNull Caret caret, int

final int pos = caret.getOffset();
final int norm = EditorHelper.normalizeOffset(editor, caret.getLogicalPosition().line, pos, isChange);
if (norm != pos || editor.offsetToVisualPosition(norm) != EditorUtil.inlayAwareOffsetToVisualPosition(editor, norm)) {
if (norm != pos ||
editor.offsetToVisualPosition(norm) != EditorUtil.inlayAwareOffsetToVisualPosition(editor, norm)) {
MotionGroup.moveCaret(editor, caret, norm);
}
// Always move the caret. Our position might or might not have changed, but an inlay might have been moved to our
// location, or deleting the character(s) might have caused us to scroll sideways in long files. Moving the caret
// will make sure it's in the right place, and visible
final int offset = EditorHelper.normalizeOffset(editor, caret.getLogicalPosition().line, caret.getOffset(), isChange);
final int offset =
EditorHelper.normalizeOffset(editor, caret.getLogicalPosition().line, caret.getOffset(), isChange);
MotionGroup.moveCaret(editor, caret, offset);

return res;
Expand Down Expand Up @@ -990,7 +996,8 @@ private boolean deleteJoinNLines(@NotNull Editor editor,
deleteText(editor, new TextRange(caret.getOffset(), offset), null);
if (spaces && !hasTrailingWhitespace) {
insertText(editor, caret, " ");
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().getOffsetOfHorizontalMotion(editor, caret, -1, true));
MotionGroup
.moveCaret(editor, caret, VimPlugin.getMotion().getOffsetOfHorizontalMotion(editor, caret, -1, true));
}
}

Expand Down Expand Up @@ -1177,7 +1184,8 @@ public boolean deleteRange(@NotNull Editor editor,
if (res) {
int pos = EditorHelper.normalizeOffset(editor, range.getStartOffset(), isChange);
if (type == SelectionType.LINE_WISE) {
pos = VimPlugin.getMotion().moveCaretToLineWithStartOfLineOption(editor, editor.offsetToLogicalPosition(pos).line, caret);
pos = VimPlugin.getMotion()
.moveCaretToLineWithStartOfLineOption(editor, editor.offsetToLogicalPosition(pos).line, caret);
}
MotionGroup.moveCaret(editor, caret, pos);
}
Expand All @@ -1187,7 +1195,7 @@ public boolean deleteRange(@NotNull Editor editor,
private boolean removeLastNewLine(@NotNull Editor editor, @NotNull TextRange range, @Nullable SelectionType type) {
int endOffset = range.getEndOffset();
int fileSize = EditorHelperRt.getFileSize(editor);
if (endOffset > fileSize){
if (endOffset > fileSize) {
StrictMode.INSTANCE.fail("Incorrect offset. File size: " + fileSize + ", offset: " + endOffset);
endOffset = fileSize;
}
Expand Down Expand Up @@ -1454,10 +1462,10 @@ private void changeCase(@NotNull Editor editor, int start, int end, char type) {
/**
* Deletes the range of text and enters insert mode
*
* @param editor The editor to change
* @param caret The caret to be moved after range deletion
* @param range The range to change
* @param type The type of the range
* @param editor The editor to change
* @param caret The caret to be moved after range deletion
* @param range The range to change
* @param type The type of the range
* @return true if able to delete the range, false if not
*/
public boolean changeRange(@NotNull Editor editor,
Expand Down Expand Up @@ -1633,7 +1641,10 @@ public void insertText(@NotNull Editor editor, @NotNull Caret caret, @NotNull St
insertText(editor, caret, caret.getOffset(), str);
}

public void insertText(@NotNull Editor editor, @NotNull Caret caret, @NotNull LogicalPosition start, @NotNull String str) {
public void insertText(@NotNull Editor editor,
@NotNull Caret caret,
@NotNull LogicalPosition start,
@NotNull String str) {
insertText(editor, caret, editor.logicalPositionToOffset(start), str);
}

Expand Down Expand Up @@ -1740,7 +1751,8 @@ public void indentRange(@NotNull Editor editor,

if (!CommandStateHelper.inInsertMode(editor)) {
if (!range.isMultiple()) {
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineWithStartOfLineOption(editor, sline, caret));
MotionGroup
.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineWithStartOfLineOption(editor, sline, caret));
}
else {
MotionGroup.moveCaret(editor, caret, range.getStartOffset());
Expand Down Expand Up @@ -1875,8 +1887,8 @@ public boolean changeNumberVisualMode(final @NotNull Editor editor,
boolean hex = nf.contains("hex");
boolean octal = nf.contains("octal");

@NotNull List<Pair<TextRange, SearchHelper.NumberType>>
numberRanges = SearchHelper.findNumbersInRange(editor, selectedRange, alpha, hex, octal);
@NotNull List<Pair<TextRange, SearchHelper.NumberType>> numberRanges =
SearchHelper.findNumbersInRange(editor, selectedRange, alpha, hex, octal);

List<String> newNumbers = new ArrayList<>();
for (int i = 0; i < numberRanges.size(); i++) {
Expand All @@ -1890,7 +1902,8 @@ public boolean changeNumberVisualMode(final @NotNull Editor editor,
// Replace text bottom up. In other direction ranges will be desynchronized after inc numbers like 99
Pair<TextRange, SearchHelper.NumberType> rangeToReplace = numberRanges.get(i);
String newNumber = newNumbers.get(i);
replaceText(editor, rangeToReplace.getFirst().getStartOffset(), rangeToReplace.getFirst().getEndOffset(), newNumber);
replaceText(editor, rangeToReplace.getFirst().getStartOffset(), rangeToReplace.getFirst().getEndOffset(),
newNumber);
}

InlayHelperKt.moveToInlayAwareOffset(caret, selectedRange.getStartOffset());
Expand All @@ -1916,7 +1929,8 @@ public boolean changeNumber(final @NotNull Editor editor, @NotNull Caret caret,
final boolean hex = nf.contains("hex");
final boolean octal = nf.contains("octal");

@Nullable Pair<TextRange, SearchHelper.NumberType> range = SearchHelper.findNumberUnderCursor(editor, caret, alpha, hex, octal);
@Nullable Pair<TextRange, SearchHelper.NumberType> range =
SearchHelper.findNumberUnderCursor(editor, caret, alpha, hex, octal);
if (range == null) {
logger.debug("no number on line");
return false;
Expand Down Expand Up @@ -1966,7 +1980,9 @@ public void reset() {

char ch = text.charAt(0);
if (hex && SearchHelper.NumberType.HEX.equals(numberType)) {
if (!text.toLowerCase().startsWith(HEX_START)) throw new RuntimeException("Hex number should start with 0x: " + text);
if (!text.toLowerCase().startsWith(HEX_START)) {
throw new RuntimeException("Hex number should start with 0x: " + text);
}
for (int i = text.length() - 1; i >= 2; i--) {
int index = "abcdefABCDEF".indexOf(text.charAt(i));
if (index >= 0) {
Expand Down Expand Up @@ -2047,6 +2063,19 @@ private void notifyListeners(Editor editor) {
insertListeners.forEach(listener -> listener.insertModeStarted(editor));
}

private void updateLastInsertedTextRegister() {
StringBuilder textToPutRegister = new StringBuilder();
if (lastStrokes != null) {
for (Object lastStroke : lastStrokes) {
if (lastStroke instanceof char[]) {
final char[] chars = (char[])lastStroke;
textToPutRegister.append(new String(chars));
}
}
}
VimPlugin.getRegister().storeTextSpecial(RegisterGroup.LAST_INSERTED_TEXT_REGISTER, textToPutRegister.toString());
}

private int oldOffset = -1;

private class InsertActionsDocumentListener implements DocumentListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,25 +176,36 @@ public void testNumberedRegistersShifting() {
public void testSearchRegisterAfterSearch() {
configureByText("<caret>one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n");
enterSearch("three", true);
assertEquals("three",getRegisterText(RegisterGroup.LAST_SEARCH_REGISTER));
assertEquals("three", getRegisterText(RegisterGroup.LAST_SEARCH_REGISTER));
}

public void testSearchRegisterAfterSubstitute() {
configureByText("<caret>one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n");
enterCommand("%s/three/3/g");
assertEquals("three",getRegisterText(RegisterGroup.LAST_SEARCH_REGISTER));
assertEquals("three", getRegisterText(RegisterGroup.LAST_SEARCH_REGISTER));
}

public void testSearchRegisterAfterSearchRange() {
configureByText("<caret>one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n");
enterCommand("/three/d");
assertEquals("three",getRegisterText(RegisterGroup.LAST_SEARCH_REGISTER));
assertEquals("three", getRegisterText(RegisterGroup.LAST_SEARCH_REGISTER));
}

public void testSearchRegisterAfterMultipleSearchRanges() {
configureByText("<caret>one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n");
enterCommand("/one/;/three/d");
assertEquals("three",getRegisterText(RegisterGroup.LAST_SEARCH_REGISTER));
assertEquals("three", getRegisterText(RegisterGroup.LAST_SEARCH_REGISTER));
}

public void testLastInsertedTextRegister() {
configureByText("<caret>");

typeText(parseKeys("i", "abc", "<Esc>"));

assertEquals("abc", getRegisterText('.'));

assertRegisterChanged(RegisterGroup.LAST_INSERTED_TEXT_REGISTER);

}

private void assertRegisterChanged(char registerName) {
Expand All @@ -214,4 +225,5 @@ private String getRegisterText(char registerName) {

return register.getText();
}

}

0 comments on commit 96dbbcf

Please sign in to comment.