Skip to content

Commit 37c2bb6

Browse files
committed
fix ex_sort with pattern and r flag
1 parent 71919db commit 37c2bb6

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/vim.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6060,16 +6060,17 @@ export function initVim(CM) {
60606060
},
60616061
/** @arg {CodeMirrorV} cm @arg {ExParams} params*/
60626062
sort: function(cm, params) {
6063-
var reverse, ignoreCase, unique, number, pattern;
6063+
var reverse, ignoreCase, unique, number, pattern, includeMatch;
60646064
function parseArgs() {
60656065
if (params.argString) {
60666066
var args = new CM.StringStream(params.argString);
60676067
if (args.eat('!')) { reverse = true; }
60686068
if (args.eol()) { return; }
60696069
if (!args.eatSpace()) { return 'Invalid arguments'; }
6070-
var opts = args.match(/([dinuox]+)?\s*(\/.+\/)?\s*/);
6070+
var opts = args.match(/([dinuoxr]+)?\s*(\/.+\/)?\s*/);
60716071
if (!opts || !args.eol()) { return 'Invalid arguments'; }
60726072
if (opts[1]) {
6073+
includeMatch = opts[1].indexOf('r') != -1;
60736074
ignoreCase = opts[1].indexOf('i') != -1;
60746075
unique = opts[1].indexOf('u') != -1;
60756076
var decimal = opts[1].indexOf('d') != -1 || opts[1].indexOf('n') != -1;
@@ -6103,6 +6104,9 @@ export function initVim(CM) {
61036104
if (number || pattern) {
61046105
for (var i = 0; i < text.length; i++) {
61056106
var matchPart = pattern ? text[i].match(pattern) : null;
6107+
if (matchPart && matchPart.index != null && !includeMatch) {
6108+
matchPart[0] = text[i].slice(matchPart.index + matchPart[0].length);
6109+
}
61066110
if (matchPart && matchPart[0] != '') {
61076111
numPart.push(matchPart);
61086112
} else if (numberRegex && numberRegex.exec(text[i])) {

test/vim_test.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,33 +4326,45 @@ testVim('ex_sort_decimal_mixed_reverse', function(cm, vim, helpers) {
43264326
eq('a3\nb2\nc1\nz\ny', cm.getValue());
43274327
}, { value: 'a3\nz\nc1\ny\nb2'});
43284328
testVim('ex_sort_pattern_alpha', function(cm, vim, helpers) {
4329-
helpers.doEx('sort /[a-z]/');
4329+
helpers.doEx('sort r/[a-z]/');
43304330
eq('a3\nb2\nc1\ny\nz', cm.getValue());
43314331
}, { value: 'z\ny\nc1\nb2\na3'});
43324332
testVim('ex_sort_pattern_alpha_reverse', function(cm, vim, helpers) {
4333-
helpers.doEx('sort! /[a-z]/');
4333+
helpers.doEx('sort! r /[a-z]/');
43344334
eq('z\ny\nc1\nb2\na3', cm.getValue());
43354335
}, { value: 'z\ny\nc1\nb2\na3'});
43364336
testVim('ex_sort_pattern_alpha_ignoreCase', function(cm, vim, helpers) {
4337-
helpers.doEx('sort i/[a-z]/');
4337+
helpers.doEx('sort ri/[a-z]/');
43384338
eq('a3\nb2\nC1\nY\nz', cm.getValue());
43394339
}, { value: 'z\nY\nC1\nb2\na3'});
43404340
testVim('ex_sort_pattern_alpha_longer', function(cm, vim, helpers) {
4341-
helpers.doEx('sort /[a-z]+/');
4341+
helpers.doEx('sort r/[a-z]+/');
43424342
eq('a\naa\nab\nade\nadele\nadelle\nadriana\nalex\nalexandra\nb\nc\ny\nz', cm.getValue());
43434343
}, { value: 'z\nab\naa\nade\nadelle\nalexandra\nalex\nadriana\nadele\ny\nc\nb\na'});
43444344
testVim('ex_sort_pattern_alpha_only', function(cm, vim, helpers) {
4345-
helpers.doEx('sort /^[a-z]$/');
4345+
helpers.doEx('sort r/^[a-z]$/');
43464346
eq('z1\ny2\na3\nb\nc', cm.getValue());
43474347
}, { value: 'z1\ny2\na3\nc\nb'});
43484348
testVim('ex_sort_pattern_alpha_only_reverse', function(cm, vim, helpers) {
4349-
helpers.doEx('sort! /^[a-z]$/');
4349+
helpers.doEx('sort! r/^[a-z]$/');
43504350
eq('c\nb\nz1\ny2\na3', cm.getValue());
43514351
}, { value: 'z1\ny2\na3\nc\nb'});
43524352
testVim('ex_sort_pattern_alpha_num', function(cm, vim, helpers) {
4353-
helpers.doEx('sort /[a-z][0-9]/');
4353+
helpers.doEx('sort r/[a-z][0-9]/');
43544354
eq('c\nb\na3\ny2\nz1', cm.getValue());
43554355
}, { value: 'z1\ny2\na3\nc\nb'});
4356+
testVim('ex_sort_pattern_no_r', function(cm, vim, helpers) {
4357+
helpers.doEx('sort /in/');
4358+
eq(' z \n in\n\n3 in a \n1 in c \n2 in d ', cm.getValue());
4359+
helpers.doEx('sort r/in/');
4360+
eq(' z \n\n in\n3 in a \n1 in c \n2 in d ', cm.getValue());
4361+
helpers.doEx('sort r/. in/');
4362+
eq(' z \n\n in\n1 in c \n2 in d \n3 in a ', cm.getValue());
4363+
helpers.doEx('sort /./');
4364+
eq('\n3 in a \n1 in c \n2 in d \n in\n z ', cm.getValue());
4365+
helpers.doEx('sort /in d/');
4366+
eq('\n3 in a \n1 in c \n in\n z \n2 in d ', cm.getValue());
4367+
}, { value: '1 in c \n z \n2 in d \n in\n3 in a \n'});
43564368
// test for :global command
43574369
testVim('ex_global', function(cm, vim, helpers) {
43584370
cm.setCursor(0, 0);

0 commit comments

Comments
 (0)