Skip to content

Commit

Permalink
VIM-2615 add support to sort u command, fix natural sort issue when b…
Browse files Browse the repository at this point in the history
…oth string not contain number
  • Loading branch information
samabcde committed May 14, 2023
1 parent 527b321 commit 1cb420f
Show file tree
Hide file tree
Showing 4 changed files with 486 additions and 105 deletions.
24 changes: 18 additions & 6 deletions src/main/java/com/maddyhome/idea/vim/group/ChangeGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.maddyhome.idea.vim.group;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.intellij.codeInsight.actions.AsyncActionExecutionService;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.DataContext;
Expand Down Expand Up @@ -62,6 +61,8 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static com.maddyhome.idea.vim.api.VimInjectorKt.injector;
import static com.maddyhome.idea.vim.api.VimInjectorKt.options;
Expand Down Expand Up @@ -500,9 +501,11 @@ public void indentRange(@NotNull VimEditor editor,
* @param editor The editor to replace text in
* @param range The range to sort
* @param lineComparator The comparator to use to sort
* @param unique The flag that only keep the first of a sequence of identical lines
* @return true if able to sort the text, false if not
*/
public boolean sortRange(@NotNull VimEditor editor, @NotNull VimCaret caret, @NotNull LineRange range, @NotNull Comparator<String> lineComparator) {
public boolean sortRange(@NotNull VimEditor editor, @NotNull VimCaret caret, @NotNull LineRange range, @NotNull Comparator<String> lineComparator,
boolean unique) {
final int startLine = range.startLine;
final int endLine = range.endLine;
final int count = endLine - startLine + 1;
Expand All @@ -513,7 +516,7 @@ public boolean sortRange(@NotNull VimEditor editor, @NotNull VimCaret caret, @No
final int startOffset = editor.getLineStartOffset(startLine);
final int endOffset = editor.getLineEndOffset(endLine);

return sortTextRange(editor, caret, startOffset, endOffset, lineComparator);
return sortTextRange(editor, caret, startOffset, endOffset, lineComparator, unique);
}

/**
Expand All @@ -523,19 +526,28 @@ public boolean sortRange(@NotNull VimEditor editor, @NotNull VimCaret caret, @No
* @param start The starting position for the sort
* @param end The ending position for the sort
* @param lineComparator The comparator to use to sort
* @param unique The flag that only keep the first of a sequence of identical lines
* @return true if able to sort the text, false if not
*/
private boolean sortTextRange(@NotNull VimEditor editor,
@NotNull VimCaret caret,
int start,
int end,
@NotNull Comparator<String> lineComparator) {
@NotNull Comparator<String> lineComparator,
boolean unique) {
final String selectedText = ((IjVimEditor) editor).getEditor().getDocument().getText(new TextRangeInterval(start, end));
final List<String> lines = Lists.newArrayList(Splitter.on("\n").split(selectedText));
final List<String> lines;
Stream<String> sorted =
StreamSupport.stream(Splitter.on("\n").split(selectedText).spliterator(), false).sorted(lineComparator);
if (unique) {
lines = sorted.distinct().toList();
}
else {
lines = sorted.toList();
}
if (lines.size() < 1) {
return false;
}
lines.sort(lineComparator);
replaceText(editor, caret, start, end, StringUtil.join(lines, "\n"));
return true;
}
Expand Down
Loading

0 comments on commit 1cb420f

Please sign in to comment.