Merge #9060 from janlazo/vim-8.1.0120

This commit is contained in:
Justin M. Keyes 2018-09-27 10:47:37 +02:00 committed by GitHub
commit 33887206b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 11 deletions

View File

@ -424,6 +424,7 @@ void ex_sort(exarg_T *eap)
sort_abort = sort_ic = sort_rx = sort_nr = sort_flt = 0; sort_abort = sort_ic = sort_rx = sort_nr = sort_flt = 0;
size_t format_found = 0; size_t format_found = 0;
bool change_occurred = false; // Buffer contents changed.
for (p = eap->arg; *p != NUL; ++p) { for (p = eap->arg; *p != NUL; ++p) {
if (ascii_iswhite(*p)) { if (ascii_iswhite(*p)) {
@ -584,8 +585,16 @@ void ex_sort(exarg_T *eap)
// Insert the lines in the sorted order below the last one. // Insert the lines in the sorted order below the last one.
lnum = eap->line2; lnum = eap->line2;
for (i = 0; i < count; ++i) { for (i = 0; i < count; i++) {
s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum); const linenr_T get_lnum = nrs[eap->forceit ? count - i - 1 : i].lnum;
// If the original line number of the line being placed is not the same
// as "lnum" (accounting for offset), we know that the buffer changed.
if (get_lnum + ((linenr_T)count - 1) != lnum) {
change_occurred = true;
}
s = ml_get(get_lnum);
if (!unique || i == 0 if (!unique || i == 0
|| (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0) { || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0) {
// Copy the line into a buffer, it may become invalid in // Copy the line into a buffer, it may become invalid in
@ -617,7 +626,9 @@ void ex_sort(exarg_T *eap)
} else if (deleted < 0) { } else if (deleted < 0) {
mark_adjust(eap->line2, MAXLNUM, -deleted, 0L, false); mark_adjust(eap->line2, MAXLNUM, -deleted, 0L, false);
} }
changed_lines(eap->line1, 0, eap->line2 + 1, -deleted, true); if (change_occurred || deleted != 0) {
changed_lines(eap->line1, 0, eap->line2 + 1, -deleted, true);
}
curwin->w_cursor.lnum = eap->line1; curwin->w_cursor.lnum = eap->line1;
beginline(BL_WHITE | BL_FIX); beginline(BL_WHITE | BL_FIX);

View File

@ -1,13 +1,13 @@
" Test sort() " Tests for the "sort()" function and for the ":sort" command.
:func Compare1(a, b) abort func Compare1(a, b) abort
call sort(range(3), 'Compare2') call sort(range(3), 'Compare2')
return a:a - a:b return a:a - a:b
:endfunc endfunc
:func Compare2(a, b) abort func Compare2(a, b) abort
return a:a - a:b return a:a - a:b
:endfunc endfunc
func Test_sort_strings() func Test_sort_strings()
" numbers compared as strings " numbers compared as strings
@ -45,7 +45,7 @@ func Test_sort_default()
call assert_fails('call sort([3.3, 1, "2"], 3)', "E474") call assert_fails('call sort([3.3, 1, "2"], 3)', "E474")
endfunc endfunc
" Tests for the :sort command " Tests for the ":sort" command.
func Test_sort_cmd() func Test_sort_cmd()
let tests = [ let tests = [
\ { \ {
@ -1167,15 +1167,54 @@ func Test_sort_cmd()
\ '1.234', \ '1.234',
\ '123.456' \ '123.456'
\ ] \ ]
\ } \ },
\ {
\ 'name' : 'alphabetical, sorted input',
\ 'cmd' : 'sort',
\ 'input' : [
\ 'a',
\ 'b',
\ 'c',
\ ],
\ 'expected' : [
\ 'a',
\ 'b',
\ 'c',
\ ]
\ },
\ {
\ 'name' : 'alphabetical, sorted input, unique at end',
\ 'cmd' : 'sort u',
\ 'input' : [
\ 'aa',
\ 'bb',
\ 'cc',
\ 'cc',
\ ],
\ 'expected' : [
\ 'aa',
\ 'bb',
\ 'cc',
\ ]
\ },
\ ] \ ]
for t in tests for t in tests
enew! enew!
call append(0, t.input) call append(0, t.input)
$delete _ $delete _
exe t.cmd setlocal nomodified
execute t.cmd
call assert_equal(t.expected, getline(1, '$'), t.name) call assert_equal(t.expected, getline(1, '$'), t.name)
" Previously, the ":sort" command would set 'modified' even if the buffer
" contents did not change. Here, we check that this problem is fixed.
if t.input == t.expected
call assert_false(&modified, t.name . ': &mod is not correct')
else
call assert_true(&modified, t.name . ': &mod is not correct')
endif
endfor endfor
call assert_fails('sort no', 'E474') call assert_fails('sort no', 'E474')