From 9915b5497bcd6a055842011c23fe0a8086f91f03 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 15 Sep 2019 16:19:49 -0700 Subject: [PATCH 0001/1293] version bump --- CMakeLists.txt | 4 ++-- runtime/nvim.appdata.xml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c3c20d23f..f9bd87c085 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,9 +113,9 @@ set_property(CACHE CMAKE_BUILD_TYPE PROPERTY # If not in a git repo (e.g., a tarball) these tokens define the complete # version string, else they are combined with the result of `git describe`. set(NVIM_VERSION_MAJOR 0) -set(NVIM_VERSION_MINOR 4) +set(NVIM_VERSION_MINOR 5) set(NVIM_VERSION_PATCH 0) -set(NVIM_VERSION_PRERELEASE "") # for package maintainers +set(NVIM_VERSION_PRERELEASE "-dev") # for package maintainers # API level set(NVIM_API_LEVEL 6) # Bump this after any API change. diff --git a/runtime/nvim.appdata.xml b/runtime/nvim.appdata.xml index 32d3c523c6..10946c356e 100644 --- a/runtime/nvim.appdata.xml +++ b/runtime/nvim.appdata.xml @@ -26,6 +26,7 @@ + From 319563725243485d11bc980efd3821ae9cef4ddd Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 15 Sep 2019 16:34:31 -0700 Subject: [PATCH 0002/1293] release.sh [ci skip] --- scripts/release.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/release.sh b/scripts/release.sh index 29d61370ce..7ac80b7e01 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -80,7 +80,7 @@ _do_bump_commit() { ,' runtime/nvim.appdata.xml rm CMakeLists.txt.bk rm runtime/nvim.appdata.xml.bk - nvim +'/NVIM_VERSION' +1new +'exe "norm! iUpdate version numbers!!!\"' \ + nvim +'/NVIM_VERSION' +1new +'exe "norm! iUpdate version numbers!!!"' \ -O CMakeLists.txt runtime/nvim.appdata.xml git add CMakeLists.txt runtime/nvim.appdata.xml From 1f76c4af13c71e4737c3e311da93ab491b7384f7 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 15 Sep 2019 17:24:41 -0700 Subject: [PATCH 0003/1293] nvim.appdata.xml [ci skip] --- runtime/nvim.appdata.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/nvim.appdata.xml b/runtime/nvim.appdata.xml index 10946c356e..ea69d5a872 100644 --- a/runtime/nvim.appdata.xml +++ b/runtime/nvim.appdata.xml @@ -26,6 +26,7 @@ + From c410440edcb438d1c7d090da24722fcbcb4e3b74 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 15 Sep 2019 17:28:42 -0700 Subject: [PATCH 0004/1293] fix api_level_6.mpack This was generated incorrectly in the 0.4.1 release, fixed in the 0.4.1 release. --- test/functional/fixtures/api_level_6.mpack | Bin 25194 -> 25287 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/test/functional/fixtures/api_level_6.mpack b/test/functional/fixtures/api_level_6.mpack index b348f86beb8ba96441d3621af24ec03e663b34c5..1c939d59318546b42ea79ee29960bf1e01e0948c 100644 GIT binary patch delta 102 zcmaELgz@-M#tEMMhgPPQ<|UV8=I0gPVK~z|xxrF-@>w3<$qm9hljrHPZJhN+bTY3l z$7B;RRq<_kWtqA0rJ3;srMdCNsU`6lshQ~+C7TPy%mpX^Q|Fl+$H6_BQ<`^jfx5uv I8|ss+0E_x3_W%F@ delta 62 zcmX?pl=0OO#tEMMhgYVR<|UV8=I0gPVL077d7_8X Date: Sun, 15 Sep 2019 17:36:17 -0700 Subject: [PATCH 0005/1293] CI/AppVeyor: revert whitelist revert 6b028ec5f29c #10746 This seems to skip tags also, which breaks our release automation. From the AppVeyor logs: 2019-09-15 17:25:00.232 Warning Commit "1f76c4af" of branch "master" skipped as commit message contains either "[skip ci]" or "[ci skip]" or "[skip appveyor]" 2019-09-15 17:20:24.152 Warning Commit "baa5263b" skipped as branch "stable" is not in white-list 2019-09-15 17:20:03.163 Warning Commit "6cb5ffc0" skipped as branch "release-0.4" is not in white-list 2019-09-15 17:20:02.977 Warning Commit "baa5263b" skipped as branch "v0.4.1" is not in white-list 2019-09-15 16:35:38.810 Warning Commit "31956372" of branch "master" skipped as commit message contains either "[skip ci]" or "[ci skip]" or "[skip appveyor]" 2019-09-15 16:28:35.237 Warning Commit "e2cc5fe0" skipped as branch "stable" is not in white-list 2019-09-15 16:28:17.814 Information Build version 31489 created 2019-09-15 16:28:16.364 Warning Commit "e2cc5fe0" skipped as branch "v0.4.0" is not in white-list --- appveyor.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 756c7fea2e..bb7bb1c4e9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -40,6 +40,3 @@ cache: artifacts: - path: build/Neovim.zip - path: build/bin/nvim.exe -branches: - only: - - master From 60e0000c5dd4465fb2982ba83d40e642fe10060b Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 15 Sep 2019 19:44:11 -0700 Subject: [PATCH 0006/1293] nvim.appdata.xml [ci skip] --- runtime/nvim.appdata.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/nvim.appdata.xml b/runtime/nvim.appdata.xml index ea69d5a872..ceffc7ba98 100644 --- a/runtime/nvim.appdata.xml +++ b/runtime/nvim.appdata.xml @@ -26,6 +26,7 @@ + From 8f3d0276ee8ff0d35e07d3767e7dc5ace9d2b2d0 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 15 Sep 2019 23:08:40 -0700 Subject: [PATCH 0007/1293] release.sh [ci skip] --- scripts/release.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/release.sh b/scripts/release.sh index 7ac80b7e01..67268ba9bf 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -93,8 +93,7 @@ fi _do_bump_commit echo " Next steps: - - Double-check NVIM_VERSION_* in CMakeLists.txt - - Double-check runtime/nvim.appdata.xml + - Run tests/CI (version_spec.lua)! - Push the tag: git push --follow-tags - Update the 'stable' tag: From 4df38ec9df4764d4b2ae0b12c1b62c34889f1aa5 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 16 Sep 2019 19:16:39 +0200 Subject: [PATCH 0008/1293] server_requests_spec: fix assertion, pass Lua paths via args (#10875) This makes it pick up the nvim Luarocks module properly when not installed via third-party. --- test/functional/api/rpc_fixture.lua | 14 +++++--------- test/functional/api/server_requests_spec.lua | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/test/functional/api/rpc_fixture.lua b/test/functional/api/rpc_fixture.lua index 87f5a91115..94df751363 100644 --- a/test/functional/api/rpc_fixture.lua +++ b/test/functional/api/rpc_fixture.lua @@ -1,12 +1,8 @@ -local deps_prefix = (os.getenv('DEPS_PREFIX') and os.getenv('DEPS_PREFIX') - or './.deps/usr') - -package.path = deps_prefix .. '/share/lua/5.1/?.lua;' .. - deps_prefix .. '/share/lua/5.1/?/init.lua;' .. - package.path - -package.cpath = deps_prefix .. '/lib/lua/5.1/?.so;' .. - package.cpath +--- RPC server fixture. +-- +-- Lua's paths are passed as arguments to reflect the path in the test itself. +package.path = arg[1] +package.cpath = arg[2] local mpack = require('mpack') local StdioStream = require('nvim.stdio_stream') diff --git a/test/functional/api/server_requests_spec.lua b/test/functional/api/server_requests_spec.lua index e275d8cd35..5a9ef7dd40 100644 --- a/test/functional/api/server_requests_spec.lua +++ b/test/functional/api/server_requests_spec.lua @@ -241,10 +241,14 @@ describe('server -> client', function() \ 'rpc': v:true \ } ]]) - meths.set_var("args", {helpers.test_lua_prg, - 'test/functional/api/rpc_fixture.lua'}) + meths.set_var("args", { + helpers.test_lua_prg, + 'test/functional/api/rpc_fixture.lua', + package.path, + package.cpath, + }) jobid = eval("jobstart(g:args, g:job_opts)") - neq(0, 'jobid') + neq(0, jobid) end) after_each(function() @@ -254,7 +258,11 @@ describe('server -> client', function() if helpers.pending_win32(pending) then return end it('rpc and text stderr can be combined', function() - eq("ok",funcs.rpcrequest(jobid, "poll")) + local status, rv = pcall(funcs.rpcrequest, jobid, 'poll') + if not status then + error(string.format('missing nvim Lua module? (%s)', rv)) + end + eq('ok', rv) funcs.rpcnotify(jobid, "ping") eq({'notification', 'pong', {}}, next_msg()) eq("done!",funcs.rpcrequest(jobid, "write_stderr", "fluff\n")) From 4b2d7bb5eafde6edbbd55b563e8f73dba4165853 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 16 Sep 2019 19:17:07 +0200 Subject: [PATCH 0009/1293] tests: fix flaky 'scrollback' option deletes lines (only) if necessary (#11003) Using `screen:expect` with the complete grid appears to fix its flakiness. Fixes https://github.com/neovim/neovim/issues/10723. --- test/functional/terminal/scrollback_spec.lua | 24 ++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/test/functional/terminal/scrollback_spec.lua b/test/functional/terminal/scrollback_spec.lua index 7413081510..ff6a74fe89 100644 --- a/test/functional/terminal/scrollback_spec.lua +++ b/test/functional/terminal/scrollback_spec.lua @@ -438,9 +438,29 @@ describe("'scrollback' option", function() command('sleep 100m') feed_data(nvim_dir.."/shell-test REP 41 line"..(iswin() and '\r' or '\n')) - screen:expect{any='40: line '} + if iswin() then + screen:expect{grid=[[ + 37: line | + 38: line | + 39: line | + 40: line | + | + ${1: } | + {3:-- TERMINAL --} | + ]]} + else + screen:expect{grid=[[ + 36: line | + 37: line | + 38: line | + 39: line | + 40: line | + {IGNORE}| + {3:-- TERMINAL --} | + ]]} + end + expect_lines(58) - retry(nil, nil, function() expect_lines(58) end) -- Verify off-screen state eq((iswin() and '36: line' or '35: line'), eval("getline(line('w0') - 1)")) eq((iswin() and '27: line' or '26: line'), eval("getline(line('w0') - 10)")) From 0b71bb73e8431400c870dcd458f322dc50da96d1 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 17 Sep 2019 00:39:33 +0200 Subject: [PATCH 0010/1293] tests: improve error message with literat "~" directory (#11032) --- test/unit/os/fs_spec.lua | 2 +- test/unit/path_spec.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua index 526a09e845..7fd71cb1ae 100644 --- a/test/unit/os/fs_spec.lua +++ b/test/unit/os/fs_spec.lua @@ -110,7 +110,7 @@ describe('fs.c', function() describe('os_chdir', function() itp('fails with path="~"', function() - eq(false, os_isdir('~')) -- sanity check: no literal "~" directory. + eq(false, os_isdir('~'), 'sanity check: no literal "~" directory') local length = 4096 local expected_cwd = cstr(length, '') local cwd = cstr(length, '') diff --git a/test/unit/path_spec.lua b/test/unit/path_spec.lua index e8ce660ce1..da52af1bf9 100644 --- a/test/unit/path_spec.lua +++ b/test/unit/path_spec.lua @@ -456,7 +456,7 @@ describe('path.c', function() end) itp('fails and uses filename when the path is relative to HOME', function() - eq(false, cimp.os_isdir('~')) -- sanity check: no literal "~" directory. + eq(false, cimp.os_isdir('~'), 'sanity check: no literal "~" directory') local absolute_path = '~/home.file' local buflen = string.len(absolute_path) + 1 local do_expand = 1 From 792c2903435ceda05e68007d7bee344f65ee3a4f Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Mon, 16 Sep 2019 21:02:49 -0400 Subject: [PATCH 0011/1293] vim-patch:8.0.1523: cannot write and read terminal screendumps Problem: Cannot write and read terminal screendumps. Solution: Add term_dumpwrite(), term_dumpread() and term_dumpdiff(). Also add assert_equalfile(). https://github.com/vim/vim/commit/d96ff165113ce5fe62107add590997660e3d4802 --- runtime/doc/eval.txt | 12 +++++-- src/nvim/eval.c | 56 ++++++++++++++++++++++++++++++++ src/nvim/eval.lua | 1 + src/nvim/testdir/test_assert.vim | 35 ++++++++++++++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 512cfc4e58..fb39617c17 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -2007,6 +2007,8 @@ argv([-1, {winid}]) List the argument list assert_beeps({cmd}) none assert {cmd} causes a beep assert_equal({exp}, {act} [, {msg}]) none assert {exp} is equal to {act} +assert_equalfile({fname-one}, {fname-two}) + none assert file contents is equal assert_exception({error} [, {msg}]) none assert {error} is in v:exception assert_fails({cmd} [, {error}]) none assert {cmd} fails @@ -2597,6 +2599,13 @@ assert_equal({expected}, {actual}, [, {msg}]) < Will result in a string to be added to |v:errors|: test.vim line 12: Expected 'foo' but got 'bar' ~ + *assert_equalfile()* +assert_equalfile({fname-one}, {fname-two}) + When the files {fname-one} and {fname-two} do not contain + exactly the same text an error message is added to |v:errors|. + When {fname-one} or {fname-two} does not exist the error will + mention that. + assert_exception({error} [, {msg}]) *assert_exception()* When v:exception does not contain the string {error} an error message is added to |v:errors|. @@ -4479,8 +4488,7 @@ getftype({fname}) *getftype()* systems that support it. On some systems only "dir" and "file" are returned. - *getjumplist()* -getjumplist([{winnr} [, {tabnr}]]) +getjumplist([{winnr} [, {tabnr}]]) *getjumplist()* Returns the |jumplist| for the specified window. Without arguments use the current window. diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 1f753608d2..1a2bdcf88f 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6961,6 +6961,56 @@ static void assert_equal_common(typval_T *argvars, assert_type_T atype) } } +static void assert_equalfile(typval_T *argvars) + FUNC_ATTR_NONNULL_ALL +{ + char buf1[NUMBUFLEN]; + char buf2[NUMBUFLEN]; + const char *const fname1 = tv_get_string_buf_chk(&argvars[0], buf1); + const char *const fname2 = tv_get_string_buf_chk(&argvars[1], buf2); + garray_T ga; + + if (fname1 == NULL || fname2 == NULL) { + return; + } + + IObuff[0] = NUL; + FILE *const fd1 = os_fopen(fname1, READBIN); + if (fd1 == NULL) { + snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1); + } else { + FILE *const fd2 = os_fopen(fname2, READBIN); + if (fd2 == NULL) { + fclose(fd1); + snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2); + } else { + for (int64_t count = 0; ; count++) { + const int c1 = fgetc(fd1); + const int c2 = fgetc(fd2); + if (c1 == EOF) { + if (c2 != EOF) { + STRCPY(IObuff, "first file is shorter"); + } + break; + } else if (c2 == EOF) { + STRCPY(IObuff, "second file is shorter"); + break; + } else if (c1 != c2) { + snprintf((char *)IObuff, IOSIZE, + "difference at byte %" PRId64, count); + break; + } + } + } + } + if (IObuff[0] != NUL) { + prepare_assert_error(&ga); + ga_concat(&ga, IObuff); + assert_error(&ga); + ga_clear(&ga); + } +} + static void f_assert_beeps(typval_T *argvars, typval_T *rettv, FunPtr fptr) { const char *const cmd = tv_get_string_chk(&argvars[0]); @@ -6988,6 +7038,12 @@ static void f_assert_equal(typval_T *argvars, typval_T *rettv, FunPtr fptr) assert_equal_common(argvars, ASSERT_EQUAL); } +// "assert_equalfile(fname-one, fname-two)" function +static void f_assert_equalfile(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + assert_equalfile(argvars); +} + // "assert_notequal(expected, actual[, msg])" function static void f_assert_notequal(typval_T *argvars, typval_T *rettv, FunPtr fptr) { diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index ab5ff57c2f..8efbcc71f1 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -28,6 +28,7 @@ return { asin={args=1, func="float_op_wrapper", data="&asin"}, -- WJMc assert_beeps={args={1, 2}}, assert_equal={args={2, 3}}, + assert_equalfile={args=2}, assert_exception={args={1, 2}}, assert_fails={args={1, 2}}, assert_false={args={1, 2}}, diff --git a/src/nvim/testdir/test_assert.vim b/src/nvim/testdir/test_assert.vim index a4c8ce7e43..cbb65ffc01 100644 --- a/src/nvim/testdir/test_assert.vim +++ b/src/nvim/testdir/test_assert.vim @@ -1,5 +1,40 @@ " Test that the methods used for testing work. +func Test_assert_equalfile() + call assert_equalfile('abcabc', 'xyzxyz') + call assert_match("E485: Can't read file abcabc", v:errors[0]) + call remove(v:errors, 0) + + let goodtext = ["one", "two", "three"] + call writefile(goodtext, 'Xone') + call assert_equalfile('Xone', 'xyzxyz') + call assert_match("E485: Can't read file xyzxyz", v:errors[0]) + call remove(v:errors, 0) + + call writefile(goodtext, 'Xtwo') + call assert_equalfile('Xone', 'Xtwo') + + call writefile([goodtext[0]], 'Xone') + call assert_equalfile('Xone', 'Xtwo') + call assert_match("first file is shorter", v:errors[0]) + call remove(v:errors, 0) + + call writefile(goodtext, 'Xone') + call writefile([goodtext[0]], 'Xtwo') + call assert_equalfile('Xone', 'Xtwo') + call assert_match("second file is shorter", v:errors[0]) + call remove(v:errors, 0) + + call writefile(['1234X89'], 'Xone') + call writefile(['1234Y89'], 'Xtwo') + call assert_equalfile('Xone', 'Xtwo') + call assert_match("difference at byte 4", v:errors[0]) + call remove(v:errors, 0) + + call delete('Xone') + call delete('Xtwo') +endfunc + func Test_assert_fails_in_try_block() try call assert_equal(0, assert_fails('throw "error"')) From 8db9e82e3e1aa094ca9224b01384da1b07fda410 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Mon, 16 Sep 2019 22:26:41 -0400 Subject: [PATCH 0012/1293] vim-patch:8.0.1770: assert functions don't return anything Problem: Assert functions don't return anything. Solution: Return non-zero when the assertion fails. https://github.com/vim/vim/commit/65a5464985f980d2bbbf4e14d39d416dce065ec7 --- runtime/doc/eval.txt | 45 ++++++++----- src/nvim/eval.c | 93 +++++++++++++++++--------- src/nvim/testdir/test_assert.vim | 12 ++-- test/functional/legacy/assert_spec.lua | 36 +++++----- 4 files changed, 113 insertions(+), 73 deletions(-) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index fb39617c17..bac7709ef5 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -1547,10 +1547,12 @@ v:errmsg Last given error message. :if v:errmsg != "" : ... handle error < - *v:errors* *errors-variable* + *v:errors* *errors-variable* *assert-return* v:errors Errors found by assert functions, such as |assert_true()|. This is a list of strings. The assert functions append an item when an assert fails. + The return value indicates this: a one is returned if an item + was added to v:errors, otherwise zero is returned. To remove old results make it empty: > :let v:errors = [] < If v:errors is set to anything but a list it is made an empty @@ -2004,26 +2006,26 @@ argidx() Number current index in the argument list arglistid([{winnr} [, {tabnr}]]) Number argument list id argv({nr} [, {winid}]) String {nr} entry of the argument list argv([-1, {winid}]) List the argument list -assert_beeps({cmd}) none assert {cmd} causes a beep +assert_beeps({cmd}) Number assert {cmd} causes a beep assert_equal({exp}, {act} [, {msg}]) - none assert {exp} is equal to {act} + Number assert {exp} is equal to {act} assert_equalfile({fname-one}, {fname-two}) - none assert file contents is equal + Number assert file contents is equal assert_exception({error} [, {msg}]) - none assert {error} is in v:exception -assert_fails({cmd} [, {error}]) none assert {cmd} fails + Number assert {error} is in v:exception +assert_fails({cmd} [, {error}]) Number assert {cmd} fails assert_false({actual} [, {msg}]) - none assert {actual} is false + Number assert {actual} is false assert_inrange({lower}, {upper}, {actual} [, {msg}]) - none assert {actual} is inside the range + Number assert {actual} is inside the range assert_match({pat}, {text} [, {msg}]) - none assert {pat} matches {text} + Number assert {pat} matches {text} assert_notequal({exp}, {act} [, {msg}]) - none assert {exp} is not equal {act} + Number assert {exp} is not equal {act} assert_notmatch({pat}, {text} [, {msg}]) - none assert {pat} not matches {text} -assert_report({msg}) none report a test failure -assert_true({actual} [, {msg}]) none assert {actual} is true + Number assert {pat} not matches {text} +assert_report({msg}) Number report a test failure +assert_true({actual} [, {msg}]) Number assert {actual} is true asin({expr}) Float arc sine of {expr} atan({expr}) Float arc tangent of {expr} atan2({expr}, {expr}) Float arc tangent of {expr1} / {expr2} @@ -2582,12 +2584,13 @@ argv([{nr} [, {winid}]) assert_beeps({cmd}) *assert_beeps()* Run {cmd} and add an error message to |v:errors| if it does NOT produce a beep or visual bell. - Also see |assert_fails()|. + Also see |assert_fails()| and |assert-return|. *assert_equal()* assert_equal({expected}, {actual}, [, {msg}]) When {expected} and {actual} are not equal an error message is - added to |v:errors|. + added to |v:errors| and 1 is returned. Otherwise zero is + returned |assert-return|. There is no automatic conversion, the String "4" is different from the Number 4. And the number 4 is different from the Float 4.0. The value of 'ignorecase' is not used here, case @@ -2603,12 +2606,13 @@ assert_equal({expected}, {actual}, [, {msg}]) assert_equalfile({fname-one}, {fname-two}) When the files {fname-one} and {fname-two} do not contain exactly the same text an error message is added to |v:errors|. + Also see |assert-return|. When {fname-one} or {fname-two} does not exist the error will mention that. assert_exception({error} [, {msg}]) *assert_exception()* When v:exception does not contain the string {error} an error - message is added to |v:errors|. + message is added to |v:errors|. Also see |assert-return|. This can be used to assert that a command throws an exception. Using the error number, followed by a colon, avoids problems with translations: > @@ -2621,7 +2625,7 @@ assert_exception({error} [, {msg}]) *assert_exception()* assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()* Run {cmd} and add an error message to |v:errors| if it does - NOT produce an error. + NOT produce an error. Also see |assert-return|. When {error} is given it must match in |v:errmsg|. Note that beeping is not considered an error, and some failing commands only beep. Use |assert_beeps()| for those. @@ -2629,6 +2633,7 @@ assert_fails({cmd} [, {error} [, {msg}]]) *assert_fails()* assert_false({actual} [, {msg}]) *assert_false()* When {actual} is not false an error message is added to |v:errors|, like with |assert_equal()|. + Also see |assert-return|. A value is false when it is zero or |v:false|. When "{actual}" is not a number or |v:false| the assert fails. When {msg} is omitted an error in the form @@ -2645,7 +2650,7 @@ assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()* *assert_match()* assert_match({pattern}, {actual} [, {msg}]) When {pattern} does not match {actual} an error message is - added to |v:errors|. + added to |v:errors|. Also see |assert-return|. {pattern} is used as with |=~|: The matching is always done like 'magic' was set and 'cpoptions' is empty, no matter what @@ -2666,18 +2671,22 @@ assert_match({pattern}, {actual} [, {msg}]) assert_notequal({expected}, {actual} [, {msg}]) The opposite of `assert_equal()`: add an error message to |v:errors| when {expected} and {actual} are equal. + Also see |assert-return|. *assert_notmatch()* assert_notmatch({pattern}, {actual} [, {msg}]) The opposite of `assert_match()`: add an error message to |v:errors| when {pattern} matches {actual}. + Also see |assert-return|. assert_report({msg}) *assert_report()* Report a test failure directly, using {msg}. + Always returns one. assert_true({actual} [, {msg}]) *assert_true()* When {actual} is not true an error message is added to |v:errors|, like with |assert_equal()|. + Also see |assert-return|. A value is |TRUE| when it is a non-zero number or |v:true|. When {actual} is not a number or |v:true| the assert fails. When {msg} is omitted an error in the form "Expected True but diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 1a2bdcf88f..248edfc9cc 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6947,7 +6947,8 @@ static void assert_error(garray_T *gap) (const char *)gap->ga_data, (ptrdiff_t)gap->ga_len); } -static void assert_equal_common(typval_T *argvars, assert_type_T atype) +static int assert_equal_common(typval_T *argvars, assert_type_T atype) + FUNC_ATTR_NONNULL_ALL { garray_T ga; @@ -6958,10 +6959,12 @@ static void assert_equal_common(typval_T *argvars, assert_type_T atype) &argvars[0], &argvars[1], atype); assert_error(&ga); ga_clear(&ga); + return 1; } + return 0; } -static void assert_equalfile(typval_T *argvars) +static int assert_equalfile(typval_T *argvars) FUNC_ATTR_NONNULL_ALL { char buf1[NUMBUFLEN]; @@ -6971,7 +6974,7 @@ static void assert_equalfile(typval_T *argvars) garray_T ga; if (fname1 == NULL || fname2 == NULL) { - return; + return 0; } IObuff[0] = NUL; @@ -7008,13 +7011,16 @@ static void assert_equalfile(typval_T *argvars) ga_concat(&ga, IObuff); assert_error(&ga); ga_clear(&ga); + return 1; } + return 0; } static void f_assert_beeps(typval_T *argvars, typval_T *rettv, FunPtr fptr) { const char *const cmd = tv_get_string_chk(&argvars[0]); garray_T ga; + int ret = 0; called_vim_beep = false; suppress_errthrow = true; @@ -7026,28 +7032,30 @@ static void f_assert_beeps(typval_T *argvars, typval_T *rettv, FunPtr fptr) ga_concat(&ga, (const char_u *)cmd); assert_error(&ga); ga_clear(&ga); + ret = 1; } suppress_errthrow = false; emsg_on_display = false; + rettv->vval.v_number = ret; } // "assert_equal(expected, actual[, msg])" function static void f_assert_equal(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - assert_equal_common(argvars, ASSERT_EQUAL); + rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL); } // "assert_equalfile(fname-one, fname-two)" function static void f_assert_equalfile(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - assert_equalfile(argvars); + rettv->vval.v_number = assert_equalfile(argvars); } // "assert_notequal(expected, actual[, msg])" function static void f_assert_notequal(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - assert_equal_common(argvars, ASSERT_NOTEQUAL); + rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL); } /// "assert_report(msg) @@ -7059,27 +7067,13 @@ static void f_assert_report(typval_T *argvars, typval_T *rettv, FunPtr fptr) ga_concat(&ga, (const char_u *)tv_get_string(&argvars[0])); assert_error(&ga); ga_clear(&ga); + rettv->vval.v_number = 1; } /// "assert_exception(string[, msg])" function static void f_assert_exception(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - garray_T ga; - - const char *const error = tv_get_string_chk(&argvars[0]); - if (vimvars[VV_EXCEPTION].vv_str == NULL) { - prepare_assert_error(&ga); - ga_concat(&ga, (char_u *)"v:exception is not set"); - assert_error(&ga); - ga_clear(&ga); - } else if (error != NULL - && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL) { - prepare_assert_error(&ga); - fill_assert_error(&ga, &argvars[1], NULL, &argvars[0], - &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER); - assert_error(&ga); - ga_clear(&ga); - } + rettv->vval.v_number = assert_exception(argvars); } /// "assert_fails(cmd [, error])" function @@ -7087,6 +7081,7 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr) { const char *const cmd = tv_get_string_chk(&argvars[0]); garray_T ga; + int ret = 0; int save_trylevel = trylevel; // trylevel must be zero for a ":throw" command to be considered failed @@ -7102,6 +7097,7 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr) ga_concat(&ga, (const char_u *)cmd); assert_error(&ga); ga_clear(&ga); + ret = 1; } else if (argvars[1].v_type != VAR_UNKNOWN) { char buf[NUMBUFLEN]; const char *const error = tv_get_string_buf_chk(&argvars[1], buf); @@ -7113,6 +7109,7 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr) &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER); assert_error(&ga); ga_clear(&ga); + ret = 1; } } @@ -7122,9 +7119,11 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr) emsg_silent = false; emsg_on_display = false; set_vim_var_string(VV_ERRMSG, NULL, 0); + rettv->vval.v_number = ret; } -void assert_inrange(typval_T *argvars) +static int assert_inrange(typval_T *argvars) + FUNC_ATTR_NONNULL_ALL { bool error = false; const varnumber_T lower = tv_get_number_chk(&argvars[0], &error); @@ -7132,7 +7131,7 @@ void assert_inrange(typval_T *argvars) const varnumber_T actual = tv_get_number_chk(&argvars[2], &error); if (error) { - return; + return 0; } if (actual < lower || actual > upper) { garray_T ga; @@ -7146,11 +7145,14 @@ void assert_inrange(typval_T *argvars) ASSERT_INRANGE); assert_error(&ga); ga_clear(&ga); + return 1; } + return 0; } // Common for assert_true() and assert_false(). -static void assert_bool(typval_T *argvars, bool is_true) +static int assert_bool(typval_T *argvars, bool is_true) + FUNC_ATTR_NONNULL_ALL { bool error = false; garray_T ga; @@ -7169,16 +7171,43 @@ static void assert_bool(typval_T *argvars, bool is_true) NULL, &argvars[0], ASSERT_OTHER); assert_error(&ga); ga_clear(&ga); + return 1; } + return 0; +} + +static int assert_exception(typval_T *argvars) + FUNC_ATTR_NONNULL_ALL +{ + garray_T ga; + + const char *const error = tv_get_string_chk(&argvars[0]); + if (vimvars[VV_EXCEPTION].vv_str == NULL) { + prepare_assert_error(&ga); + ga_concat(&ga, (char_u *)"v:exception is not set"); + assert_error(&ga); + ga_clear(&ga); + return 1; + } else if (error != NULL + && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL) { + prepare_assert_error(&ga); + fill_assert_error(&ga, &argvars[1], NULL, &argvars[0], + &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER); + assert_error(&ga); + ga_clear(&ga); + return 1; + } + return 0; } // "assert_false(actual[, msg])" function static void f_assert_false(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - assert_bool(argvars, false); + rettv->vval.v_number = assert_bool(argvars, false); } -static void assert_match_common(typval_T *argvars, assert_type_T atype) +static int assert_match_common(typval_T *argvars, assert_type_T atype) + FUNC_ATTR_NONNULL_ALL { char buf1[NUMBUFLEN]; char buf2[NUMBUFLEN]; @@ -7194,31 +7223,33 @@ static void assert_match_common(typval_T *argvars, assert_type_T atype) fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], atype); assert_error(&ga); ga_clear(&ga); + return 1; } + return 0; } /// "assert_inrange(lower, upper[, msg])" function static void f_assert_inrange(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - assert_inrange(argvars); + rettv->vval.v_number = assert_inrange(argvars); } /// "assert_match(pattern, actual[, msg])" function static void f_assert_match(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - assert_match_common(argvars, ASSERT_MATCH); + rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH); } /// "assert_notmatch(pattern, actual[, msg])" function static void f_assert_notmatch(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - assert_match_common(argvars, ASSERT_NOTMATCH); + rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH); } // "assert_true(actual[, msg])" function static void f_assert_true(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - assert_bool(argvars, true); + rettv->vval.v_number = assert_bool(argvars, true); } /* diff --git a/src/nvim/testdir/test_assert.vim b/src/nvim/testdir/test_assert.vim index cbb65ffc01..4cc90eca7a 100644 --- a/src/nvim/testdir/test_assert.vim +++ b/src/nvim/testdir/test_assert.vim @@ -1,33 +1,33 @@ " Test that the methods used for testing work. func Test_assert_equalfile() - call assert_equalfile('abcabc', 'xyzxyz') + call assert_equal(1, assert_equalfile('abcabc', 'xyzxyz')) call assert_match("E485: Can't read file abcabc", v:errors[0]) call remove(v:errors, 0) let goodtext = ["one", "two", "three"] call writefile(goodtext, 'Xone') - call assert_equalfile('Xone', 'xyzxyz') + call assert_equal(1, assert_equalfile('Xone', 'xyzxyz')) call assert_match("E485: Can't read file xyzxyz", v:errors[0]) call remove(v:errors, 0) call writefile(goodtext, 'Xtwo') - call assert_equalfile('Xone', 'Xtwo') + call assert_equal(0, assert_equalfile('Xone', 'Xtwo')) call writefile([goodtext[0]], 'Xone') - call assert_equalfile('Xone', 'Xtwo') + call assert_equal(1, assert_equalfile('Xone', 'Xtwo')) call assert_match("first file is shorter", v:errors[0]) call remove(v:errors, 0) call writefile(goodtext, 'Xone') call writefile([goodtext[0]], 'Xtwo') - call assert_equalfile('Xone', 'Xtwo') + call assert_equal(1, assert_equalfile('Xone', 'Xtwo')) call assert_match("second file is shorter", v:errors[0]) call remove(v:errors, 0) call writefile(['1234X89'], 'Xone') call writefile(['1234Y89'], 'Xtwo') - call assert_equalfile('Xone', 'Xtwo') + call assert_equal(1, assert_equalfile('Xone', 'Xtwo')) call assert_match("difference at byte 4", v:errors[0]) call remove(v:errors, 0) diff --git a/test/functional/legacy/assert_spec.lua b/test/functional/legacy/assert_spec.lua index 8df2d89b70..97c1f1405c 100644 --- a/test/functional/legacy/assert_spec.lua +++ b/test/functional/legacy/assert_spec.lua @@ -47,18 +47,18 @@ describe('assert function:', function() end) it('should not change v:errors when expected is equal to actual', function() - call('assert_equal', '', '') - call('assert_equal', 'string', 'string') + eq(0, call('assert_equal', '', '')) + eq(0, call('assert_equal', 'string', 'string')) expected_empty() end) it('should change v:errors when expected is not equal to actual', function() - call('assert_equal', 0, {0}) + eq(1, call('assert_equal', 0, {0})) expected_errors({'Expected 0 but got [0]'}) end) it('should change v:errors when expected is not equal to actual', function() - call('assert_equal', 0, "0") + eq(1, call('assert_equal', 0, "0")) expected_errors({"Expected 0 but got '0'"}) end) @@ -96,13 +96,13 @@ describe('assert function:', function() -- assert_notequal({expected}, {actual}[, {msg}]) describe('assert_notequal', function() it('should not change v:errors when expected differs from actual', function() - call('assert_notequal', 'foo', 4) - call('assert_notequal', {1, 2, 3}, 'foo') + eq(0, call('assert_notequal', 'foo', 4)) + eq(0, call('assert_notequal', {1, 2, 3}, 'foo')) expected_empty() end) it('should change v:errors when expected is equal to actual', function() - call('assert_notequal', 'foo', 'foo') + eq(1, call('assert_notequal', 'foo', 'foo')) expected_errors({"Expected not equal to 'foo'"}) end) end) @@ -110,13 +110,13 @@ describe('assert function:', function() -- assert_false({actual}, [, {msg}]) describe('assert_false', function() it('should not change v:errors when actual is false', function() - call('assert_false', 0) - call('assert_false', false) + eq(0, call('assert_false', 0)) + eq(0, call('assert_false', false)) expected_empty() end) it('should change v:errors when actual is not false', function() - call('assert_false', 1) + eq(1, call('assert_false', 1)) expected_errors({'Expected False but got 1'}) end) @@ -129,14 +129,14 @@ describe('assert function:', function() -- assert_true({actual}, [, {msg}]) describe('assert_true', function() it('should not change v:errors when actual is true', function() - call('assert_true', 1) - call('assert_true', -1) -- In Vim script, non-zero Numbers are TRUE. - call('assert_true', true) + eq(0, call('assert_true', 1)) + eq(0, call('assert_true', -1)) -- In Vim script, non-zero Numbers are TRUE. + eq(0, call('assert_true', true)) expected_empty() end) it('should change v:errors when actual is not true', function() - call('assert_true', 1.5) + eq(1, call('assert_true', 1.5)) expected_errors({'Expected True but got 1.5'}) end) end) @@ -277,7 +277,7 @@ describe('assert function:', function() -- assert_report({msg}) describe('assert_report()', function() it('should add a message to v:errors', function() - command("call assert_report('something is wrong')") + eq(1, call('assert_report', 'something is wrong')) command("call assert_match('something is wrong', v:errors[0])") command('call remove(v:errors, 0)') expected_empty() @@ -291,7 +291,7 @@ describe('assert function:', function() try nocommand catch - call assert_exception('E492') + call assert_equal(0, assert_exception('E492')) endtry ]]) expected_empty() @@ -304,9 +304,9 @@ describe('assert function:', function() catch try " illegal argument, get NULL for error - call assert_exception([]) + call assert_equal(1, assert_exception([])) catch - call assert_exception('E730') + call assert_equal(0, assert_exception('E730')) endtry endtry ]]) From d4785421106c9ac81adc9ddd5778446d80dbc4ba Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 18 Sep 2019 18:21:44 +0200 Subject: [PATCH 0013/1293] health#provider: fix duplicated output/stderr (#11048) Ref: https://github.com/neovim/neovim/pull/11047#issuecomment-532268826 --- runtime/autoload/health/provider.vim | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/runtime/autoload/health/provider.vim b/runtime/autoload/health/provider.vim index 87d82150b6..f1238edbde 100644 --- a/runtime/autoload/health/provider.vim +++ b/runtime/autoload/health/provider.vim @@ -38,9 +38,10 @@ endfunction " Handler for s:system() function. function! s:system_handler(jobid, data, event) dict abort if a:event ==# 'stderr' - let self.stderr .= join(a:data, '') - if !self.ignore_stderr + if self.add_stderr_to_output let self.output .= join(a:data, '') + else + let self.stderr .= join(a:data, '') endif elseif a:event ==# 'stdout' let self.output .= join(a:data, '') @@ -64,7 +65,7 @@ function! s:system(cmd, ...) abort let stdin = a:0 ? a:1 : '' let ignore_error = a:0 > 2 ? a:3 : 0 let opts = { - \ 'ignore_stderr': a:0 > 1 ? a:2 : 0, + \ 'add_stderr_to_output': a:0 > 1 ? a:2 : 0, \ 'output': '', \ 'stderr': '', \ 'on_stdout': function('s:system_handler'), @@ -89,8 +90,15 @@ function! s:system(cmd, ...) abort call health#report_error(printf('Command timed out: %s', s:shellify(a:cmd))) call jobstop(jobid) elseif s:shell_error != 0 && !ignore_error - call health#report_error(printf("Command error (job=%d, exit code %d): `%s` (in %s)\nOutput: %s\nStderr: %s", - \ jobid, s:shell_error, s:shellify(a:cmd), string(getcwd()), opts.output, opts.stderr)) + let emsg = printf("Command error (job=%d, exit code %d): `%s` (in %s)", + \ jobid, s:shell_error, s:shellify(a:cmd), string(getcwd())) + if !empty(opts.output) + let emsg .= "\noutput: " . opts.output + end + if !empty(opts.stderr) + let emsg .= "\nstderr: " . opts.stderr + end + call health#report_error(emsg) endif return opts.output From 1070c092c7bf989f261047b861165e61e94c1941 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 18 Sep 2019 18:22:38 +0200 Subject: [PATCH 0014/1293] win_update: fix redraw regression (#11027) Before 6e9ea5adc `win_ins_lines` would return `FAIL` for `i/line_count == 0`. Handle this by checking it in the outer `if`. Ref: https://github.com/neovim/neovim/commit/6e9ea5ad#commitcomment-35084669 --- src/nvim/screen.c | 2 +- test/functional/ui/diff_spec.lua | 74 ++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 5bcd2c808d..25dd3aad7e 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -871,7 +871,7 @@ static void win_update(win_T *wp) if (wp->w_lines[0].wl_lnum != wp->w_topline) i += diff_check_fill(wp, wp->w_lines[0].wl_lnum) - wp->w_old_topfill; - if (i < wp->w_grid.Rows - 2) { // less than a screen off + if (i != 0 && i < wp->w_grid.Rows - 2) { // less than a screen off // Try to insert the correct number of lines. // If not the last window, delete the lines at the bottom. // win_ins_lines may fail when the terminal can't do it. diff --git a/test/functional/ui/diff_spec.lua b/test/functional/ui/diff_spec.lua index 8eb2bbf779..572ed5c695 100644 --- a/test/functional/ui/diff_spec.lua +++ b/test/functional/ui/diff_spec.lua @@ -3,6 +3,8 @@ local Screen = require('test.functional.ui.screen') local feed = helpers.feed local clear = helpers.clear +local command = helpers.command +local insert = helpers.insert local write_file = helpers.write_file describe('Diff mode screen', function() @@ -957,3 +959,75 @@ int main(int argc, char **argv) end) end) end) + +it('win_update redraws lines properly', function() + local screen + clear() + screen = Screen.new(30, 10) + screen:attach() + screen:set_default_attr_ids({ + [1] = {bold = true, foreground = Screen.colors.Blue1}, + [2] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red}, + [3] = {background = Screen.colors.Red, foreground = Screen.colors.Grey100, special = Screen.colors.Yellow}, + [4] = {bold = true, foreground = Screen.colors.SeaGreen4}, + [5] = {special = Screen.colors.Yellow}, + [6] = {special = Screen.colors.Yellow, bold = true, foreground = Screen.colors.SeaGreen4}, + [7] = {foreground = Screen.colors.Grey0, background = Screen.colors.Grey100}, + [8] = {foreground = Screen.colors.Gray90, background = Screen.colors.Grey100}, + [9] = {foreground = tonumber('0x00000c'), background = Screen.colors.Grey100}, + [10] = {background = Screen.colors.Grey100, bold = true, foreground = tonumber('0xe5e5ff')}, + [11] = {background = Screen.colors.Grey100, bold = true, foreground = tonumber('0x2b8452')}, + [12] = {bold = true, reverse = true}, + [13] = {foreground = Screen.colors.DarkBlue, background = Screen.colors.WebGray}, + [14] = {reverse = true}, + [15] = {background = Screen.colors.LightBlue}, + [16] = {background = Screen.colors.LightCyan1, bold = true, foreground = Screen.colors.Blue1}, + [17] = {bold = true, background = Screen.colors.Red}, + [18] = {background = Screen.colors.LightMagenta}, + }) + + insert([[ + 1 + + + 2 + 1a + ]]) + command("vnew") + insert([[ + 2 + 2a + 2b + ]]) + command("windo diffthis") + command("windo 1") + screen:expect{grid=[[ + {13: }{16:-------}{14:│}{13: }{15:^1 }| + {13: }{16:-------}{14:│}{13: }{15: }| + {13: }{16:-------}{14:│}{13: }{15: }| + {13: }2 {14:│}{13: }2 | + {13: }{17:2}{18:a }{14:│}{13: }{17:1}{18:a }| + {13: }{15:2b }{14:│}{13: }{16:------------------}| + {13: } {14:│}{13: } | + {1:~ }{14:│}{1:~ }| + {14:') + feed('') + feed('') + feed('') + feed('') + screen:expect{grid=[[ + {13: }{16:-------}{14:│}{13: }{15:1 }| + {13: }{16:-------}{14:│}{13: }{15: }| + {13: }{16:-------}{14:│}{13: }{15:^ }| + {13: }2 {14:│}{13: }2 | + {13: }{17:2}{18:a }{14:│}{13: }{17:1}{18:a }| + {13: }{15:2b }{14:│}{13: }{16:------------------}| + {13: } {14:│}{13: } | + {1:~ }{14:│}{1:~ }| + {14: Date: Wed, 18 Sep 2019 22:36:38 +0200 Subject: [PATCH 0015/1293] vim-patch:8.1.0496: no tests for indent files Problem: No tests for indent files. Solution: Add a mechanism for running indent file tests. Add a first test for Vim indenting. https://github.com/vim/vim/commit/c0fe4978f2311be9a0221d4c2369251c719b399a --- runtime/indent/Makefile | 13 +++ runtime/indent/README.txt | 2 + runtime/indent/testdir/README.txt | 92 +++++++++++++++++++++ runtime/indent/testdir/cleantest.vim | 6 ++ runtime/indent/testdir/runtest.vim | 117 +++++++++++++++++++++++++++ runtime/indent/testdir/vim.in | 46 +++++++++++ runtime/indent/testdir/vim.ok | 46 +++++++++++ 7 files changed, 322 insertions(+) create mode 100644 runtime/indent/Makefile create mode 100644 runtime/indent/testdir/README.txt create mode 100644 runtime/indent/testdir/cleantest.vim create mode 100644 runtime/indent/testdir/runtest.vim create mode 100644 runtime/indent/testdir/vim.in create mode 100644 runtime/indent/testdir/vim.ok diff --git a/runtime/indent/Makefile b/runtime/indent/Makefile new file mode 100644 index 0000000000..fa81db0470 --- /dev/null +++ b/runtime/indent/Makefile @@ -0,0 +1,13 @@ +# Portable Makefile for running indent tests. + +VIM = vim + +# Run the tests that didn't run yet or failed previously. +# If a test succeeds a testdir/*.out file will be written. +# If a test fails a testdir/*.fail file will be written. +test: + $(VIM) --not-a-term -u testdir/runtest.vim + + +clean: + $(VIM) --not-a-term -u testdir/cleantest.vim diff --git a/runtime/indent/README.txt b/runtime/indent/README.txt index a424b4f8c0..8b114365c6 100644 --- a/runtime/indent/README.txt +++ b/runtime/indent/README.txt @@ -43,3 +43,5 @@ running. Add a test if the function exists and use ":finish", like this: The user may have several options set unlike you, try to write the file such that it works with any option settings. Also be aware of certain features not being compiled in. + +To test the indent file, see testdir/README.txt. diff --git a/runtime/indent/testdir/README.txt b/runtime/indent/testdir/README.txt new file mode 100644 index 0000000000..8efd34ed4c --- /dev/null +++ b/runtime/indent/testdir/README.txt @@ -0,0 +1,92 @@ +TESTING INDENT SCRIPTS + +We'll use FILETYPE for the filetype name here. + + +FORMAT OF THE FILETYPE.IN FILE + +First of all, create a FILETYPE.in file. It should contain: + +- A modeline setting the 'filetype' and any other option values. + This must work like a comment for FILETYPE. E.g. for vim: + " vim: set ft=vim sw=4 : + +- At least one block of lines to indent, prefixed with START_INDENT and + followed by END_INDENT. These lines must also look like a comment for your + FILETYPE. You would normally leave out all indent, so that the effect of + the indent command results in adding indent. Example: + + " START_INDENT + func Some() + let x = 1 + endfunc + " END_INDENT + +- Optionally, a line with INDENT_EXE, followed by a Vim command. This will be + executed before indenting the lines. Example: + + " START_INDENT + " INDENT_EXE let g:vim_indent_cont = 6 + let cmd = + \ 'some ' + \ 'string' + " END_INDENT + + Note that the command is not undone, you may need to reverse the effect for + the next block of lines. + +- Alternatively to indenting all the lines between START_INDENT and + END_INDENT, use a INDENT_AT line, which specifies a pattern to find the line + to indent. Example: + + " START_INDENT + " INDENT_AT this-line + func Some() + let f = x " this-line + endfunc + " END_INDENT + + Alternatively you can use INDENT_NEXT to indent the line below the matching + pattern: + + " START_INDENT + " INDENT_NEXT next-line + func Some() + " next-line + let f = x + endfunc + " END_INDENT + + Or use INDENT_PREV to indent the line above the matching pattern: + + " START_INDENT + " INDENT_PREV prev-line + func Some() + let f = x + " prev-line + endfunc + " END_INDENT + +It's best to keep the whole file valid for FILETYPE, so that syntax +highlighting works normally, and any indenting that depends on the syntax +highlighting also works. + + +RUNNING THE TEST + +Before running the test, create a FILETYPE.ok file. You can leave it empty at +first. + +Now run "make test". After Vim has done the indenting you will see a +FILETYPE.fail file. This contains the actual result of indenting, and it's +different from the FILETYPE.ok file. + +Check the contents of the FILETYPE.fail file. If it is perfectly OK, then +rename it to overwrite the FILETYPE.ok file. If you now run "make test" again, +the test will pass and create a FILETYPE.out file, which is identical to the +FILETYPE.ok file. + +If you try to run "make test" again you will notice that nothing happens, +because the FILETYPE.out file already exists. Delete it, or do "make clean", +so that the text runs again. If you edit the FILETYPE.in file, so that it's +newer than the FILETYPE.out file, the test will also run. diff --git a/runtime/indent/testdir/cleantest.vim b/runtime/indent/testdir/cleantest.vim new file mode 100644 index 0000000000..909cf812c4 --- /dev/null +++ b/runtime/indent/testdir/cleantest.vim @@ -0,0 +1,6 @@ +" Deletes all the test output files: *.fail and *.out +for fname in glob('testdir/*.out', 1, 1) + glob('testdir/*.fail', 1, 1) + call delete(fname) +endfor + +quit diff --git a/runtime/indent/testdir/runtest.vim b/runtime/indent/testdir/runtest.vim new file mode 100644 index 0000000000..5d36512f94 --- /dev/null +++ b/runtime/indent/testdir/runtest.vim @@ -0,0 +1,117 @@ +" Runs all the indent tests for which there is no .out file + +set nocp +filetype indent on +set nowrapscan + +au! SwapExists * call HandleSwapExists() +func HandleSwapExists() + " Ignore finding a swap file for the test input and output, the user might be + " editing them and that's OK. + if expand('') =~ '.*\.\(in\|out\|fail\|ok\)' + let v:swapchoice = 'e' + endif +endfunc + +for fname in glob('testdir/*.in', 1, 1) + let root = substitute(fname, '\.in', '', '') + + " Execute the test if the .out file does not exist of when the .in file is + " newer. + let in_time = getftime(fname) + let out_time = getftime(root . '.out') + if out_time < 0 || in_time > out_time + call delete(root . '.fail') + call delete(root . '.out') + + set sw& ts& filetype= + exe 'split ' . fname + + let did_some = 0 + let failed = 0 + let end = 1 + while 1 + " Indent all the lines between "START_INDENT" and "END_INDENT" + exe end + let start = search('\') + let end = search('\') + if start <= 0 || end <= 0 || end <= start + if did_some == 0 + call append(0, 'ERROR: START_INDENT and/or END_INDENT not found') + let failed = 1 + endif + break + else + let did_some = 1 + + " Execute all commands marked with INDENT_EXE and find any pattern. + let lnum = start + let pattern = '' + let at = '' + while 1 + exe lnum + 1 + let lnum_exe = search('\') + exe lnum + 1 + let indent_at = search('\') + if lnum_exe > 0 && lnum_exe < end && (indent_at <= 0 || lnum_exe < indent_at) + exe substitute(getline(lnum_exe), '.*INDENT_EXE', '', '') + let lnum = lnum_exe + let start = lnum + elseif indent_at > 0 && indent_at < end + if pattern != '' + call append(indent_at, 'ERROR: duplicate pattern') + let failed = 1 + break + endif + let text = getline(indent_at) + let pattern = substitute(text, '.*INDENT_\S*\s*', '', '') + let at = substitute(text, '.*INDENT_\(\S*\).*', '\1', '') + let lnum = indent_at + let start = lnum + else + break + endif + endwhile + + exe start + 1 + if pattern == '' + exe 'normal =' . (end - 1) . 'G' + else + let lnum = search(pattern) + if lnum <= 0 + call append(indent_at, 'ERROR: pattern not found: ' . pattern) + let failed = 1 + break + endif + if at == 'AT' + exe lnum + elseif at == 'NEXT' + exe lnum + 1 + else + exe lnum - 1 + endif + normal == + endif + endif + endwhile + + if !failed + " Check the resulting text equals the .ok file. + if getline(1, '$') != readfile(root . '.ok') + let failed = 1 + endif + endif + + if failed + exe 'write ' . root . '.fail' + echoerr 'Test ' . fname . ' FAILED!' + sleep 2 + else + exe 'write ' . root . '.out' + endif + + quit! " close the indented file + endif +endfor + +qall! diff --git a/runtime/indent/testdir/vim.in b/runtime/indent/testdir/vim.in new file mode 100644 index 0000000000..ca105f6eda --- /dev/null +++ b/runtime/indent/testdir/vim.in @@ -0,0 +1,46 @@ +" vim: set ft=vim sw=4 : + +" START_INDENT + +func Some() +let x = 1 +endfunc + +let cmd = +\ 'some ' +\ 'string' + +" END_INDENT + +" START_INDENT +" INDENT_EXE let g:vim_indent_cont = 6 + +let cmd = +\ 'some ' +\ 'string' + +" END_INDENT + +" START_INDENT +" INDENT_EXE unlet g:vim_indent_cont +" INDENT_AT this-line +func Some() +let f = x " this-line +endfunc +" END_INDENT + +" START_INDENT +" INDENT_NEXT next-line +func Some() + " next-line +let f = x +endfunc +" END_INDENT + +" START_INDENT +" INDENT_PREV prev-line +func Some() +let f = x +" prev-line +endfunc +" END_INDENT diff --git a/runtime/indent/testdir/vim.ok b/runtime/indent/testdir/vim.ok new file mode 100644 index 0000000000..542861ec9c --- /dev/null +++ b/runtime/indent/testdir/vim.ok @@ -0,0 +1,46 @@ +" vim: set ft=vim sw=4 : + +" START_INDENT + +func Some() + let x = 1 +endfunc + +let cmd = + \ 'some ' + \ 'string' + +" END_INDENT + +" START_INDENT +" INDENT_EXE let g:vim_indent_cont = 6 + +let cmd = + \ 'some ' + \ 'string' + +" END_INDENT + +" START_INDENT +" INDENT_EXE unlet g:vim_indent_cont +" INDENT_AT this-line +func Some() + let f = x " this-line +endfunc +" END_INDENT + +" START_INDENT +" INDENT_NEXT next-line +func Some() + " next-line + let f = x +endfunc +" END_INDENT + +" START_INDENT +" INDENT_PREV prev-line +func Some() + let f = x +" prev-line +endfunc +" END_INDENT From b466f0e1141fd09847c9763aaa2d02de770e299d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 18 Sep 2019 22:37:41 +0200 Subject: [PATCH 0016/1293] vim-patch:8.1.0545: when executing indent tests user preferences interfere Problem: When executing indent tests user preferences interfere. Solution: Add "--clean". https://github.com/vim/vim/commit/dc2f73a6980be13c97a83047d0de50824bc0f20f --- runtime/indent/Makefile | 4 ++-- runtime/indent/testdir/runtest.vim | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/runtime/indent/Makefile b/runtime/indent/Makefile index fa81db0470..3791f3348d 100644 --- a/runtime/indent/Makefile +++ b/runtime/indent/Makefile @@ -6,8 +6,8 @@ VIM = vim # If a test succeeds a testdir/*.out file will be written. # If a test fails a testdir/*.fail file will be written. test: - $(VIM) --not-a-term -u testdir/runtest.vim + $(VIM) --clean --not-a-term -u testdir/runtest.vim clean: - $(VIM) --not-a-term -u testdir/cleantest.vim + $(VIM) --clean --not-a-term -u testdir/cleantest.vim diff --git a/runtime/indent/testdir/runtest.vim b/runtime/indent/testdir/runtest.vim index 5d36512f94..1cc0d3f716 100644 --- a/runtime/indent/testdir/runtest.vim +++ b/runtime/indent/testdir/runtest.vim @@ -1,4 +1,6 @@ -" Runs all the indent tests for which there is no .out file +" Runs all the indent tests for which there is no .out file. +" +" Current directory must be runtime/indent. set nocp filetype indent on From 865aaa031a06fca61d717621a26906cef42732d1 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 18 Sep 2019 22:38:07 +0200 Subject: [PATCH 0017/1293] vim-patch:8.1.0576: indent script tests pick up installed scripts Problem: Indent script tests pick up installed scripts. Solution: Use current runtime indent scripts. https://github.com/vim/vim/commit/30700cd5ffa258f1d684ab6b34bd03e970450dba --- runtime/indent/Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime/indent/Makefile b/runtime/indent/Makefile index 3791f3348d..1917e46c6b 100644 --- a/runtime/indent/Makefile +++ b/runtime/indent/Makefile @@ -1,13 +1,14 @@ # Portable Makefile for running indent tests. VIM = vim +VIMRUNTIME = .. # Run the tests that didn't run yet or failed previously. # If a test succeeds a testdir/*.out file will be written. # If a test fails a testdir/*.fail file will be written. test: - $(VIM) --clean --not-a-term -u testdir/runtest.vim + VIMRUNTIME=$(VIMRUNTIME) $(VIM) --clean --not-a-term -u testdir/runtest.vim clean: - $(VIM) --clean --not-a-term -u testdir/cleantest.vim + VIMRUNTIME=$(VIMRUNTIME) $(VIM) --clean --not-a-term -u testdir/cleantest.vim From 10c050caf99df7324f84b4595e8fd1db7f7a2c10 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 18 Sep 2019 22:38:27 +0200 Subject: [PATCH 0018/1293] vim-patch:8.1.0599: without the +eval feature the indent tests don't work Problem: Without the +eval feature the indent tests don't work. Solution: Skip the body of the tests. https://github.com/vim/vim/commit/eeed665b0ecd917e88e3475c9615d52546aa124d --- runtime/indent/testdir/cleantest.vim | 13 +++++++++---- runtime/indent/testdir/runtest.vim | 6 ++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/runtime/indent/testdir/cleantest.vim b/runtime/indent/testdir/cleantest.vim index 909cf812c4..69c0a1d4ba 100644 --- a/runtime/indent/testdir/cleantest.vim +++ b/runtime/indent/testdir/cleantest.vim @@ -1,6 +1,11 @@ -" Deletes all the test output files: *.fail and *.out -for fname in glob('testdir/*.out', 1, 1) + glob('testdir/*.fail', 1, 1) - call delete(fname) -endfor +" Only do this with the +eval feature +if 1 + + " Deletes all the test output files: *.fail and *.out + for fname in glob('testdir/*.out', 1, 1) + glob('testdir/*.fail', 1, 1) + call delete(fname) + endfor + +endif quit diff --git a/runtime/indent/testdir/runtest.vim b/runtime/indent/testdir/runtest.vim index 1cc0d3f716..96c31453af 100644 --- a/runtime/indent/testdir/runtest.vim +++ b/runtime/indent/testdir/runtest.vim @@ -2,6 +2,9 @@ " " Current directory must be runtime/indent. +" Only do this with the +eval feature +if 1 + set nocp filetype indent on set nowrapscan @@ -116,4 +119,7 @@ for fname in glob('testdir/*.in', 1, 1) endif endfor +" Matching "if 1" at the start. +endif + qall! From 48b2d21d5ea328089a2e8fb3798aa93d1a15a388 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 18 Sep 2019 22:40:06 +0200 Subject: [PATCH 0019/1293] vim-patch:8.1.1213: "make clean" in top dir does not cleanup indent test output Problem: "make clean" in top dir does not cleanup indent test output. Solution: Clean the indent test output. Do not rely on the vim executable for that. (closes vim/vim#4307) https://github.com/vim/vim/commit/e13a3901cae0afb4d2af30d497696af08029fd81 --- Makefile | 1 + runtime/indent/Makefile | 2 +- runtime/indent/testdir/cleantest.vim | 11 ----------- 3 files changed, 2 insertions(+), 12 deletions(-) delete mode 100644 runtime/indent/testdir/cleantest.vim diff --git a/Makefile b/Makefile index 264ae8a470..f5b9459b0c 100644 --- a/Makefile +++ b/Makefile @@ -158,6 +158,7 @@ clean: +test -d build && $(BUILD_CMD) -C build clean || true $(MAKE) -C src/nvim/testdir clean $(MAKE) -C runtime/doc clean + $(MAKE) -C runtime/indent clean distclean: rm -rf $(DEPS_BUILD_DIR) build diff --git a/runtime/indent/Makefile b/runtime/indent/Makefile index 1917e46c6b..d192605527 100644 --- a/runtime/indent/Makefile +++ b/runtime/indent/Makefile @@ -11,4 +11,4 @@ test: clean: - VIMRUNTIME=$(VIMRUNTIME) $(VIM) --clean --not-a-term -u testdir/cleantest.vim + rm -f testdir/*.fail testdir/*.out diff --git a/runtime/indent/testdir/cleantest.vim b/runtime/indent/testdir/cleantest.vim deleted file mode 100644 index 69c0a1d4ba..0000000000 --- a/runtime/indent/testdir/cleantest.vim +++ /dev/null @@ -1,11 +0,0 @@ -" Only do this with the +eval feature -if 1 - - " Deletes all the test output files: *.fail and *.out - for fname in glob('testdir/*.out', 1, 1) + glob('testdir/*.fail', 1, 1) - call delete(fname) - endfor - -endif - -quit From 0e75a9eead33b99d8bbf30ec2b67f09257bf71ee Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 18 Sep 2019 22:51:50 +0200 Subject: [PATCH 0020/1293] Update runtime/indent/testdir to latest Vim runtime Several runtime updates ignored the non-existing files. --- runtime/indent/testdir/README.txt | 23 ++++++++++++++--------- runtime/indent/testdir/runtest.vim | 3 ++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/runtime/indent/testdir/README.txt b/runtime/indent/testdir/README.txt index 8efd34ed4c..65975605c2 100644 --- a/runtime/indent/testdir/README.txt +++ b/runtime/indent/testdir/README.txt @@ -22,8 +22,12 @@ First of all, create a FILETYPE.in file. It should contain: endfunc " END_INDENT -- Optionally, a line with INDENT_EXE, followed by a Vim command. This will be - executed before indenting the lines. Example: + If you just want to test normal indenting with default options, you can make + this a large number of lines. Just add all kinds of language constructs, + nested statements, etc. with valid syntax. + +- Optionally, add lines with INDENT_EXE after START_INDENT, followed by a Vim + command. This will be executed before indenting the lines. Example: " START_INDENT " INDENT_EXE let g:vim_indent_cont = 6 @@ -36,8 +40,8 @@ First of all, create a FILETYPE.in file. It should contain: the next block of lines. - Alternatively to indenting all the lines between START_INDENT and - END_INDENT, use a INDENT_AT line, which specifies a pattern to find the line - to indent. Example: + END_INDENT, use an INDENT_AT line, which specifies a pattern to find the + line to indent. Example: " START_INDENT " INDENT_AT this-line @@ -47,7 +51,8 @@ First of all, create a FILETYPE.in file. It should contain: " END_INDENT Alternatively you can use INDENT_NEXT to indent the line below the matching - pattern: + pattern. Keep in mind that quite often it will indent relative to the + matching line: " START_INDENT " INDENT_NEXT next-line @@ -77,14 +82,14 @@ RUNNING THE TEST Before running the test, create a FILETYPE.ok file. You can leave it empty at first. -Now run "make test". After Vim has done the indenting you will see a -FILETYPE.fail file. This contains the actual result of indenting, and it's -different from the FILETYPE.ok file. +Now run "make test" from the parent directory. After Vim has done the +indenting you will see a FILETYPE.fail file. This contains the actual result +of indenting, and it's different from the FILETYPE.ok file. Check the contents of the FILETYPE.fail file. If it is perfectly OK, then rename it to overwrite the FILETYPE.ok file. If you now run "make test" again, the test will pass and create a FILETYPE.out file, which is identical to the -FILETYPE.ok file. +FILETYPE.ok file. The FILETYPE.fail file will be deleted. If you try to run "make test" again you will notice that nothing happens, because the FILETYPE.out file already exists. Delete it, or do "make clean", diff --git a/runtime/indent/testdir/runtest.vim b/runtime/indent/testdir/runtest.vim index 96c31453af..0f0051415d 100644 --- a/runtime/indent/testdir/runtest.vim +++ b/runtime/indent/testdir/runtest.vim @@ -7,7 +7,9 @@ if 1 set nocp filetype indent on +syn on set nowrapscan +set report=9999 au! SwapExists * call HandleSwapExists() func HandleSwapExists() @@ -110,7 +112,6 @@ for fname in glob('testdir/*.in', 1, 1) if failed exe 'write ' . root . '.fail' echoerr 'Test ' . fname . ' FAILED!' - sleep 2 else exe 'write ' . root . '.out' endif From 660b452440d60725f0cd68f264ba10b6add068c8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 18 Sep 2019 22:52:22 +0200 Subject: [PATCH 0021/1293] vim-patch:8.1.2056: "make test" for indent files doesn't cause make to fail Problem: "make test" for indent files doesn't cause make to fail. Solution: Exit the script with ":cquit". (Daniel Hahler, closes vim/vim#4949) https://github.com/vim/vim/commit/cd67059c0c3abf1e28aa66458abdf6f338252eb2 --- .gitignore | 1 + runtime/indent/testdir/runtest.vim | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/.gitignore b/.gitignore index 7db3d96e2b..b7f710d1d7 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,7 @@ tags /src/nvim/testdir/valgrind.* /src/nvim/testdir/.gdbinit /runtime/indent/testdir/*.out ++runtime/indent/testdir/*.fail # Generated by src/nvim/testdir/runnvim.sh. /src/nvim/testdir/*.tlog diff --git a/runtime/indent/testdir/runtest.vim b/runtime/indent/testdir/runtest.vim index 0f0051415d..9502c42f3e 100644 --- a/runtime/indent/testdir/runtest.vim +++ b/runtime/indent/testdir/runtest.vim @@ -20,6 +20,7 @@ func HandleSwapExists() endif endfunc +let failed_count = 0 for fname in glob('testdir/*.in', 1, 1) let root = substitute(fname, '\.in', '', '') @@ -110,6 +111,7 @@ for fname in glob('testdir/*.in', 1, 1) endif if failed + let failed_count += 1 exe 'write ' . root . '.fail' echoerr 'Test ' . fname . ' FAILED!' else @@ -123,4 +125,8 @@ endfor " Matching "if 1" at the start. endif +if failed_count > 0 + " have make report an error + cquit +endif qall! From 828a6e75681143a04d60be8d4f8a35dc40b6cd60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Fri, 20 Sep 2019 09:37:43 +0200 Subject: [PATCH 0022/1293] screen: fix vcol counting with virtual text. Fixes #9941 --- src/nvim/screen.c | 2 +- test/functional/ui/bufhl_spec.lua | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 25dd3aad7e..f4aa10ecf5 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -4007,7 +4007,7 @@ win_line ( break; } - ++vcol; + vcol += cells; } } diff --git a/test/functional/ui/bufhl_spec.lua b/test/functional/ui/bufhl_spec.lua index bcccef84b6..d8ca947645 100644 --- a/test/functional/ui/bufhl_spec.lua +++ b/test/functional/ui/bufhl_spec.lua @@ -31,6 +31,9 @@ describe('Buffer highlighting', function() [14] = {background = Screen.colors.Gray90}, [15] = {background = Screen.colors.Gray90, bold = true, foreground = Screen.colors.Brown}, [16] = {foreground = Screen.colors.Magenta, background = Screen.colors.Gray90}, + [17] = {foreground = Screen.colors.Magenta, background = Screen.colors.LightRed}, + [18] = {background = Screen.colors.LightRed}, + [19] = {foreground = Screen.colors.Blue1, background = Screen.colors.LightRed}, }) end) @@ -516,6 +519,32 @@ describe('Buffer highlighting', function() | ]]) end) + + it('works with color column', function() + eq(-1, set_virtual_text(-1, 3, {{"暗x事", "Comment"}}, {})) + screen:expect{grid=[[ + ^1 + 2 {3:=}{2: 3} | + 3 + {11:ERROR:} invalid syntax | + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5| + , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s| + x = 4 {12:暗x事} | + {1:~ }| + {1:~ }| + | + ]]} + + command("set colorcolumn=9") + screen:expect{grid=[[ + ^1 + 2 {3:=}{2: }{17:3} | + 3 + {11:ERROR:} invalid syntax | + 5, 5, 5,{18: }5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5| + , 5, 5, 5, 5, 5, 5, Lorem ipsum dolor s| + x = 4 {12:暗}{19:x}{12:事} | + {1:~ }| + {1:~ }| + | + ]]} + end) end) it('and virtual text use the same namespace counter', function() From 690cd4f012cef0ed1817ad55db84dc3d0a51e2d0 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 18 Sep 2019 21:52:19 -0400 Subject: [PATCH 0023/1293] vim-patch:8.1.1783: MS-Windows: compiler test may fail when using %:S Problem: MS-Windows: compiler test may fail when using %:S. Solution: Reset 'shellslash'. https://github.com/vim/vim/commit/dff2adc8ddcb6c8f3390a82c321362f8d6756fb8 --- src/nvim/testdir/test_compiler.vim | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/nvim/testdir/test_compiler.vim b/src/nvim/testdir/test_compiler.vim index 46c14d8bc3..f561e84a38 100644 --- a/src/nvim/testdir/test_compiler.vim +++ b/src/nvim/testdir/test_compiler.vim @@ -10,6 +10,10 @@ func Test_compiler() unlet $LANG endif + " %:S does not work properly with 'shellslash' set + let save_shellslash = &shellslash + set noshellslash + e Xfoo.pl compiler perl call assert_equal('perl', b:current_compiler) @@ -27,6 +31,7 @@ func Test_compiler() call assert_match("\n 1 Xfoo.pl:3: Global symbol \"\$foo\" " \ . "requires explicit package name", a) + let &shellslash = save_shellslash call delete('Xfoo.pl') bw! endfunc From 1c71a3c657ed7668de0d0fc3fae928d8857a62cb Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 18 Sep 2019 21:52:37 -0400 Subject: [PATCH 0024/1293] vim-patch:8.1.2054: compiler test for Perl may fail Problem: Compiler test for Perl may fail. Solution: Accept any error line number. (James McCoy, closes vim/vim#4944) https://github.com/vim/vim/commit/cebfcffa40c058119bc2f92f0db02dffd3f6affe --- src/nvim/testdir/test_compiler.vim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/nvim/testdir/test_compiler.vim b/src/nvim/testdir/test_compiler.vim index f561e84a38..40d3cdbdae 100644 --- a/src/nvim/testdir/test_compiler.vim +++ b/src/nvim/testdir/test_compiler.vim @@ -28,8 +28,9 @@ func Test_compiler() w! call feedkeys(":make\\", 'tx') let a=execute('clist') - call assert_match("\n 1 Xfoo.pl:3: Global symbol \"\$foo\" " - \ . "requires explicit package name", a) + call assert_match('\n \d\+ Xfoo.pl:3: Global symbol "$foo" ' + \ . 'requires explicit package name', a) + let &shellslash = save_shellslash call delete('Xfoo.pl') From 42a05130955829847e68c1af5add386596b697fd Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Thu, 19 Sep 2019 20:41:08 -0400 Subject: [PATCH 0025/1293] vim-patch:8.1.2058: function for ex command is named inconsistently Problem: Function for ex command is named inconsistently. Solution: Rename do_marks() to ex_marks(). https://github.com/vim/vim/commit/4bd782339e370bde82c2a8976df9f335cc12eba9 --- src/nvim/ex_cmds.lua | 2 +- src/nvim/mark.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nvim/ex_cmds.lua b/src/nvim/ex_cmds.lua index 8c0d22809f..a709acd4ef 100644 --- a/src/nvim/ex_cmds.lua +++ b/src/nvim/ex_cmds.lua @@ -1632,7 +1632,7 @@ return { command='marks', flags=bit.bor(EXTRA, TRLBAR, CMDWIN), addr_type=ADDR_LINES, - func='do_marks', + func='ex_marks', }, { command='match', diff --git a/src/nvim/mark.c b/src/nvim/mark.c index e103d3cb55..e8f1651a6e 100644 --- a/src/nvim/mark.c +++ b/src/nvim/mark.c @@ -616,7 +616,7 @@ static char_u *mark_line(pos_T *mp, int lead_len) /* * print the marks */ -void do_marks(exarg_T *eap) +void ex_marks(exarg_T *eap) { char_u *arg = eap->arg; int i; From b853b6e4ea1269ab7ae766bd71d9bafd54dc2b98 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 20 Sep 2019 01:31:01 -0400 Subject: [PATCH 0026/1293] vim-patch:8.0.1109: timer causes error on exit from Ex mode Problem: Timer causes error on exit from Ex mode. (xtal8) Solution: save and restore the ex_pressedreturn flag. (Christian Brabandt, closes vim/vim#2079) https://github.com/vim/vim/commit/f5291f301e9322545f0621b2157e93050d1d4fb3 --- src/nvim/eval.c | 2 ++ src/nvim/ex_docmd.c | 17 ++++++++++++++--- src/nvim/testdir/test_timers.vim | 11 +++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 248edfc9cc..04204e45d0 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -18437,6 +18437,7 @@ static void timer_due_cb(TimeWatcher *tw, void *data) timer_T *timer = (timer_T *)data; int save_did_emsg = did_emsg; int save_called_emsg = called_emsg; + const bool save_ex_pressedreturn = get_pressedreturn(); if (timer->stopped || timer->paused) { return; @@ -18465,6 +18466,7 @@ static void timer_due_cb(TimeWatcher *tw, void *data) } did_emsg = save_did_emsg; called_emsg = save_called_emsg; + set_pressedreturn(save_ex_pressedreturn); if (timer->emsg_count >= 3) { timer_stop(timer); diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 72d39adb3e..a6931f3acd 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -77,7 +77,7 @@ #include "nvim/api/private/helpers.h" static int quitmore = 0; -static int ex_pressedreturn = FALSE; +static bool ex_pressedreturn = false; /// Whether ":lcd" or ":tcd" was produced for a session. static int did_lcd; @@ -1278,14 +1278,14 @@ static char_u * do_one_cmd(char_u **cmdlinep, || getline_equal(fgetline, cookie, getexline)) && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) { ea.cmd = (char_u *)"+"; - ex_pressedreturn = TRUE; + ex_pressedreturn = true; } /* ignore comment and empty lines */ if (*ea.cmd == '"') goto doend; if (*ea.cmd == NUL) { - ex_pressedreturn = TRUE; + ex_pressedreturn = true; goto doend; } @@ -10131,6 +10131,17 @@ static void ex_folddo(exarg_T *eap) ml_clearmarked(); // clear rest of the marks } +bool get_pressedreturn(void) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + return ex_pressedreturn; +} + +void set_pressedreturn(bool val) +{ + ex_pressedreturn = val; +} + static void ex_terminal(exarg_T *eap) { char ex_cmd[1024]; diff --git a/src/nvim/testdir/test_timers.vim b/src/nvim/testdir/test_timers.vim index 24c735865c..ab5d89d675 100644 --- a/src/nvim/testdir/test_timers.vim +++ b/src/nvim/testdir/test_timers.vim @@ -252,4 +252,15 @@ func Test_peek_and_get_char() call timer_stop(intr) endfunc +func Test_ex_mode() + " Function with an empty line. + func Foo(...) + + endfunc + let timer = timer_start(40, function('g:Foo'), {'repeat':-1}) + " This used to throw error E749. + exe "normal Qsleep 100m\rvi\r" + call timer_stop(timer) +endfunc + " vim: shiftwidth=2 sts=2 expandtab From ca116625153d806bd192f0f533346cc9536904a9 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 20 Sep 2019 02:18:38 -0400 Subject: [PATCH 0027/1293] vim-patch:8.0.1539: no test for the popup menu positioning Problem: No test for the popup menu positioning. Solution: Add a screendump test for the popup menu. https://github.com/vim/vim/commit/6bb2cdfe604e51eec216cbe23bb6e8fb47810347 --- runtime/doc/eval.txt | 2 +- src/nvim/testdir/test_popup.vim | 33 ++++++++++++++++++++++++++++++++ src/nvim/testdir/test_syntax.vim | 3 ++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index bac7709ef5..5e6bfd0dbc 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -6657,7 +6657,7 @@ remote_expr({server}, {string} [, {idvar} [, {timeout}]]) between (not at the end), like with join(expr, "\n"). If {idvar} is present and not empty, it is taken as the name of a variable and a {serverid} for later use with - remote_read() is stored there. + |remote_read()| is stored there. If {timeout} is given the read times out after this many seconds. Otherwise a timeout of 600 seconds is used. See also |clientserver| |RemoteReply|. diff --git a/src/nvim/testdir/test_popup.vim b/src/nvim/testdir/test_popup.vim index 53df30bb19..efef91789d 100644 --- a/src/nvim/testdir/test_popup.vim +++ b/src/nvim/testdir/test_popup.vim @@ -1,6 +1,7 @@ " Test for completion menu source shared.vim +source screendump.vim let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] let g:setting = '' @@ -735,6 +736,38 @@ func Test_popup_and_preview_autocommand() bw! endfunc +func Test_popup_position() + if !CanRunVimInTerminal() + return + endif + call writefile([ + \ '123456789_123456789_123456789_a', + \ '123456789_123456789_123456789_b', + \ ' 123', + \ ], 'Xtest') + let buf = RunVimInTerminal('Xtest', {}) + call term_sendkeys(buf, ":vsplit\") + + " default pumwidth in left window: overlap in right window + call term_sendkeys(buf, "GA\") + call VerifyScreenDump(buf, 'Test_popup_position_01', {'rows': 8}) + call term_sendkeys(buf, "\u") + + " default pumwidth: fill until right of window + call term_sendkeys(buf, "\l") + call term_sendkeys(buf, "GA\") + call VerifyScreenDump(buf, 'Test_popup_position_02', {'rows': 8}) + + " larger pumwidth: used as minimum width + call term_sendkeys(buf, "\u") + call term_sendkeys(buf, ":set pumwidth=30\") + call term_sendkeys(buf, "GA\") + call VerifyScreenDump(buf, 'Test_popup_position_03', {'rows': 8}) + + call term_sendkeys(buf, "\u") + call StopVimInTerminal(buf) + call delete('Xtest') +endfunc func Test_popup_complete_backwards() new diff --git a/src/nvim/testdir/test_syntax.vim b/src/nvim/testdir/test_syntax.vim index 598a00476c..b9310e2168 100644 --- a/src/nvim/testdir/test_syntax.vim +++ b/src/nvim/testdir/test_syntax.vim @@ -1,6 +1,7 @@ " Test for syntax and syntax iskeyword option source view_util.vim +source screendump.vim func GetSyntaxItem(pat) let c = '' @@ -526,7 +527,7 @@ func Test_syntax_c() let $COLORFGBG = '15;0' let buf = RunVimInTerminal('Xtest.c', {}) - call VerifyScreenDump(buf, 'Test_syntax_c_01') + call VerifyScreenDump(buf, 'Test_syntax_c_01', {}) call StopVimInTerminal(buf) let $COLORFGBG = '' From 7cffc87868db846d9306bbc8f055630967c72c21 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 20 Sep 2019 18:41:15 -0400 Subject: [PATCH 0028/1293] vim-patch:8.0.1733: incomplete testing for completion fix Problem: Incomplete testing for completion fix. (Lifepillar) Solution: Add a test with CTRL-P. https://github.com/vim/vim/commit/bad0ce7b26be5eed8524347018f4c835b212f8d1 --- src/nvim/testdir/test_popup.vim | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/nvim/testdir/test_popup.vim b/src/nvim/testdir/test_popup.vim index efef91789d..4806bb1855 100644 --- a/src/nvim/testdir/test_popup.vim +++ b/src/nvim/testdir/test_popup.vim @@ -779,6 +779,16 @@ func Test_popup_complete_backwards() bwipe! endfunc +func Test_popup_complete_backwards_ctrl_p() + new + call setline(1, ['Post', 'Port', 'Po']) + let expected=['Post', 'Port', 'Port'] + call cursor(3,2) + call feedkeys("A\\rt\", 'tx') + call assert_equal(expected, getline(1,'$')) + bwipe! +endfunc + fun! Test_complete_o_tab() throw 'skipped: Nvim does not support test_override()' let s:o_char_pressed = 0 From 3878b0822e4a7e76fcc741d739bd399915920a4e Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 20 Sep 2019 19:08:01 -0400 Subject: [PATCH 0029/1293] vim-patch:8.0.1771: in tests, when WaitFor() fails it doesn't say why Problem: In tests, when WaitFor() fails it doesn't say why. (James McCoy) Solution: Add WaitForAssert(), which produces an assert error when it fails. https://github.com/vim/vim/commit/50182fa84e20a0547f3e2bd6683ef799fcd27855 --- src/nvim/testdir/shared.vim | 77 ++++++++++++++++++++------ src/nvim/testdir/test_autocmd.vim | 4 +- src/nvim/testdir/test_clientserver.vim | 13 ++--- 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/src/nvim/testdir/shared.vim b/src/nvim/testdir/shared.vim index df512e2e3f..84f636077d 100644 --- a/src/nvim/testdir/shared.vim +++ b/src/nvim/testdir/shared.vim @@ -135,39 +135,80 @@ endfunc " Wait for up to five seconds for "expr" to become true. "expr" can be a " stringified expression to evaluate, or a funcref without arguments. +" Using a lambda works best. Example: +" call WaitFor({-> status == "ok"}) " " A second argument can be used to specify a different timeout in msec. " -" Return time slept in milliseconds. With the +reltime feature this can be -" more than the actual waiting time. Without +reltime it can also be less. +" When successful the time slept is returned. +" When running into the timeout an exception is thrown, thus the function does +" not return. func WaitFor(expr, ...) let timeout = get(a:000, 0, 5000) + let slept = s:WaitForCommon(a:expr, v:null, timeout) + if slept < 0 + throw 'WaitFor() timed out after ' . timeout . ' msec' + endif + return slept +endfunc + +" Wait for up to five seconds for "assert" to return zero. "assert" must be a +" (lambda) function containing one assert function. Example: +" call WaitForAssert({-> assert_equal("dead", job_status(job)}) +" +" A second argument can be used to specify a different timeout in msec. +" +" Return zero for success, one for failure (like the assert function). +func WaitForAssert(assert, ...) + let timeout = get(a:000, 0, 5000) + if s:WaitForCommon(v:null, a:assert, timeout) < 0 + return 1 + endif + return 0 +endfunc + +" Common implementation of WaitFor() and WaitForAssert(). +" Either "expr" or "assert" is not v:null +" Return the waiting time for success, -1 for failure. +func s:WaitForCommon(expr, assert, timeout) " using reltime() is more accurate, but not always available + let slept = 0 if has('reltime') let start = reltime() - else - let slept = 0 endif - if type(a:expr) == v:t_func - let Test = a:expr - else - let Test = {-> eval(a:expr) } - endif - for i in range(timeout / 10) - if Test() - if has('reltime') - return float2nr(reltimefloat(reltime(start)) * 1000) - endif + + while 1 + if type(a:expr) == v:t_func + let success = a:expr() + elseif type(a:assert) == v:t_func + let success = a:assert() == 0 + else + let success = eval(a:expr) + endif + if success return slept endif - if !has('reltime') + + if slept >= a:timeout + break + endif + if type(a:assert) == v:t_func + " Remove the error added by the assert function. + call remove(v:errors, -1) + endif + + sleep 10m + if has('reltime') + let slept = float2nr(reltimefloat(reltime(start)) * 1000) + else let slept += 10 endif - sleep 10m - endfor - throw 'WaitFor() timed out after ' . timeout . ' msec' + endwhile + + return -1 " timed out endfunc + " Wait for up to a given milliseconds. " With the +timers feature this waits for key-input by getchar(), Resume() " feeds key-input and resumes process. Return time waited in milliseconds. diff --git a/src/nvim/testdir/test_autocmd.vim b/src/nvim/testdir/test_autocmd.vim index 5848940a2b..4dd48beef4 100644 --- a/src/nvim/testdir/test_autocmd.vim +++ b/src/nvim/testdir/test_autocmd.vim @@ -1344,11 +1344,11 @@ func Test_Changed_FirstTime() let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3}) call assert_equal('running', term_getstatus(buf)) " Wait for the ruler (in the status line) to be shown. - call WaitFor({-> term_getline(buf, 3) =~# '\ assert_match('\ call writefile(['No'], 'Xchanged.txt')\") call term_sendkeys(buf, "\\:qa!\") - call WaitFor({-> term_getstatus(buf) == 'finished'}) + call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) call assert_equal([''], readfile('Xchanged.txt')) " clean up diff --git a/src/nvim/testdir/test_clientserver.vim b/src/nvim/testdir/test_clientserver.vim index 813cb338a5..3377f86126 100644 --- a/src/nvim/testdir/test_clientserver.vim +++ b/src/nvim/testdir/test_clientserver.vim @@ -28,12 +28,11 @@ func Test_client_server() let name = 'XVIMTEST' let cmd .= ' --servername ' . name let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'}) - call WaitFor({-> job_status(job) == "run"}) + call WaitForAssert({-> assert_equal("run", job_status(job))}) " Takes a short while for the server to be active. " When using valgrind it takes much longer. - call WaitFor('serverlist() =~ "' . name . '"') - call assert_match(name, serverlist()) + call WaitForAssert({-> assert_match(name, serverlist())}) call remote_foreground(name) @@ -54,12 +53,10 @@ func Test_client_server() endif " Wait for the server to be up and answering requests. sleep 100m - call WaitFor('remote_expr("' . name . '", "v:version", "", 1) != ""') - call assert_true(remote_expr(name, "v:version", "", 1) != "") + call WaitForAssert({-> assert_true(remote_expr(name, "v:version", "", 1) != "")}) call remote_send(name, ":let testvar = 'maybe'\") - call WaitFor('remote_expr("' . name . '", "testvar", "", 1) == "maybe"') - call assert_equal('maybe', remote_expr(name, "testvar", "", 2)) + call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))}) endif call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\")', 'E241') @@ -94,7 +91,7 @@ func Test_client_server() call remote_send(name, ":qa!\") try - call WaitFor({-> job_status(job) == "dead"}) + call WaitForAssert({-> assert_equal("dead", job_status(job))}) finally if job_status(job) != 'dead' call assert_report('Server did not exit') From 2a7ffc6567097232f5e12b6d3dc6483748eaad0a Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 20 Sep 2019 19:19:46 -0400 Subject: [PATCH 0030/1293] vim-patch:8.0.1776: in tests, when WaitFor() fails it doesn't say why Problem: In tests, when WaitFor() fails it doesn't say why. Solution: Turn a few more WaitFor() into WaitForAssert(). https://github.com/vim/vim/commit/0e9d1ae3216a5940b36bb56d155fb300b2e55b00 --- src/nvim/testdir/test_popup.vim | 9 +++------ src/nvim/testdir/test_quotestar.vim | 22 ++++++++++------------ src/nvim/testdir/test_timers.vim | 6 +++--- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/nvim/testdir/test_popup.vim b/src/nvim/testdir/test_popup.vim index 4806bb1855..c63269e5d2 100644 --- a/src/nvim/testdir/test_popup.vim +++ b/src/nvim/testdir/test_popup.vim @@ -681,18 +681,15 @@ func Test_popup_and_window_resize() call term_sendkeys(buf, "\") call term_wait(buf, 100) " popup first entry "!" must be at the top - call WaitFor({-> term_getline(buf, 1) =~ "^!"}) - call assert_match('^!\s*$', term_getline(buf, 1)) + call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, 1))}) exe 'resize +' . (h - 1) call term_wait(buf, 100) redraw! " popup shifted down, first line is now empty - call WaitFor({-> term_getline(buf, 1) == ""}) - call assert_equal('', term_getline(buf, 1)) + call WaitForAssert({-> assert_equal('', term_getline(buf, 1))}) sleep 100m " popup is below cursor line and shows first match "!" - call WaitFor({-> term_getline(buf, term_getcursor(buf)[0] + 1) =~ "^!"}) - call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1)) + call WaitForAssert({-> assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0] + 1))}) " cursor line also shows ! call assert_match('^!\s*$', term_getline(buf, term_getcursor(buf)[0])) bwipe! diff --git a/src/nvim/testdir/test_quotestar.vim b/src/nvim/testdir/test_quotestar.vim index b83fbe40e8..ce5a9ee827 100644 --- a/src/nvim/testdir/test_quotestar.vim +++ b/src/nvim/testdir/test_quotestar.vim @@ -54,34 +54,33 @@ func Do_test_quotestar_for_x11() " Make sure a previous server has exited try call remote_send(name, ":qa!\") - call WaitFor('serverlist() !~ "' . name . '"') catch /E241:/ endtry - call assert_notmatch(name, serverlist()) + call WaitForAssert({-> assert_notmatch(name, serverlist())}) let cmd .= ' --servername ' . name let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'}) - call WaitFor({-> job_status(job) == "run"}) + call WaitForAssert({-> assert_equal("run", job_status(job))}) " Takes a short while for the server to be active. - call WaitFor('serverlist() =~ "' . name . '"') + call WaitForAssert({-> assert_match(name, serverlist())}) " Wait for the server to be up and answering requests. One second is not " always sufficient. - call WaitFor('remote_expr("' . name . '", "v:version", "", 2) != ""') + call WaitForAssert({-> assert_notequal('', remote_expr(name, "v:version", "", 2))}) " Clear the *-register of this vim instance and wait for it to be picked up " by the server. let @* = 'no' call remote_foreground(name) - call WaitFor('remote_expr("' . name . '", "@*", "", 1) == "no"') + call WaitForAssert({-> assert_equal("no", remote_expr(name, "@*", "", 1))}) " Set the * register on the server. call remote_send(name, ":let @* = 'yes'\") - call WaitFor('remote_expr("' . name . '", "@*", "", 1) == "yes"') + call WaitForAssert({-> assert_equal("yes", remote_expr(name, "@*", "", 1))}) " Check that the *-register of this vim instance is changed as expected. - call WaitFor('@* == "yes"') + call WaitForAssert({-> assert_equal("yes", @*)}) " Handle the large selection over 262040 byte. let length = 262044 @@ -109,18 +108,17 @@ func Do_test_quotestar_for_x11() call remote_send(name, ":gui -f\") endif " Wait for the server in the GUI to be up and answering requests. - call WaitFor('remote_expr("' . name . '", "has(\"gui_running\")", "", 1) =~ "1"') + call WaitForAssert({-> assert_match("1", remote_expr(name, "has('gui_running')", "", 1))}) call remote_send(name, ":let @* = 'maybe'\") - call WaitFor('remote_expr("' . name . '", "@*", "", 1) == "maybe"') - call assert_equal('maybe', remote_expr(name, "@*", "", 2)) + call WaitForAssert({-> assert_equal("maybe", remote_expr(name, "@*", "", 2))}) call assert_equal('maybe', @*) endif call remote_send(name, ":qa!\") try - call WaitFor({-> job_status(job) == "dead"}) + call WaitForAssert({-> assert_equal("dead", job_status(job))}) finally if job_status(job) != 'dead' call assert_report('Server did not exit') diff --git a/src/nvim/testdir/test_timers.vim b/src/nvim/testdir/test_timers.vim index ab5d89d675..bd63d94729 100644 --- a/src/nvim/testdir/test_timers.vim +++ b/src/nvim/testdir/test_timers.vim @@ -141,7 +141,7 @@ endfunc func Test_delete_myself() let g:called = 0 let t = timer_start(10, 'StopMyself', {'repeat': -1}) - call WaitFor('g:called == 2') + call WaitForAssert({-> assert_equal(2, g:called)}) call assert_equal(2, g:called) call assert_equal([], timer_info(t)) endfunc @@ -208,7 +208,7 @@ func Test_timer_errors() let g:call_count = 0 let timer = timer_start(10, 'FuncWithError', {'repeat': -1}) " Timer will be stopped after failing 3 out of 3 times. - call WaitFor('g:call_count == 3') + call WaitForAssert({-> assert_equal(3, g:call_count)}) sleep 50m call assert_equal(3, g:call_count) endfunc @@ -226,7 +226,7 @@ func Test_timer_catch_error() let g:call_count = 0 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4}) " Timer will not be stopped. - call WaitFor('g:call_count == 4') + call WaitForAssert({-> assert_equal(4, g:call_count)}) sleep 50m call assert_equal(4, g:call_count) endfunc From eb3888a322304434848ca2f6b1b12b821c9788f4 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 20 Sep 2019 21:58:56 -0400 Subject: [PATCH 0031/1293] vim-patch:8.0.1529: assert_equalfile() does not close file descriptors Problem: Assert_equalfile() does not close file descriptors. (Coverity) Solution: Close the file descriptors. https://github.com/vim/vim/commit/3049418f3dbc571463a04d068069f6c5b7a8ccf1 --- src/nvim/eval.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 04204e45d0..e409d57bfd 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -7004,6 +7004,8 @@ static int assert_equalfile(typval_T *argvars) break; } } + fclose(fd1); + fclose(fd2); } } if (IObuff[0] != NUL) { From 111d34849a0670842b56c17c3922dbf0576bb39b Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 21 Sep 2019 11:07:07 -0400 Subject: [PATCH 0032/1293] vim-patch:8.0.1621: using invalid default value for highlight attribute Problem: Using invalid default value for highlight attribute. Solution: Use zero instead of -1. https://github.com/vim/vim/commit/6185903e3d07eb53326fc1403fc2de97ca31b775 --- src/nvim/syntax.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 5a61238d8c..675d484d67 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -7569,8 +7569,8 @@ void highlight_changed(void) { int id; char_u userhl[30]; // use 30 to avoid compiler warning - int id_SNC = -1; int id_S = -1; + int id_SNC = 0; int hlcnt; need_highlight_changed = FALSE; From b3e56957f8e9468497e5db508d97d7b560ccfe85 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 21 Sep 2019 17:03:46 -0400 Subject: [PATCH 0033/1293] vim-patch:8.1.0460: assert_fails() message argument #11051 Problem: assert_fails() does not take a message argument Solution: Add the argument. https://github.com/vim/vim/commit/1307d1c003b01b4f67524c95feb07c3d91c7c428 --- src/nvim/eval.c | 11 +++++++++-- src/nvim/eval.lua | 2 +- test/functional/legacy/assert_spec.lua | 18 +++++++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 248edfc9cc..7d45787fae 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -7076,7 +7076,7 @@ static void f_assert_exception(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = assert_exception(argvars); } -/// "assert_fails(cmd [, error])" function +/// "assert_fails(cmd [, error [, msg]])" function static void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr) { const char *const cmd = tv_get_string_chk(&argvars[0]); @@ -7094,7 +7094,14 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr) if (!called_emsg) { prepare_assert_error(&ga); ga_concat(&ga, (const char_u *)"command did not fail: "); - ga_concat(&ga, (const char_u *)cmd); + if (argvars[1].v_type != VAR_UNKNOWN + && argvars[2].v_type != VAR_UNKNOWN) { + char *const tofree = encode_tv2echo(&argvars[2], NULL); + ga_concat(&ga, (char_u *)tofree); + xfree(tofree); + } else { + ga_concat(&ga, (const char_u *)cmd); + } assert_error(&ga); ga_clear(&ga); ret = 1; diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 8efbcc71f1..0ae250e626 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -30,7 +30,7 @@ return { assert_equal={args={2, 3}}, assert_equalfile={args=2}, assert_exception={args={1, 2}}, - assert_fails={args={1, 2}}, + assert_fails={args={1, 3}}, assert_false={args={1, 2}}, assert_inrange={args={3, 4}}, assert_match={args={2, 3}}, diff --git a/test/functional/legacy/assert_spec.lua b/test/functional/legacy/assert_spec.lua index 97c1f1405c..3cb5d97869 100644 --- a/test/functional/legacy/assert_spec.lua +++ b/test/functional/legacy/assert_spec.lua @@ -3,6 +3,7 @@ local nvim, call = helpers.meths, helpers.call local clear, eq = helpers.clear, helpers.eq local source, command = helpers.source, helpers.command local exc_exec = helpers.exc_exec +local eval = helpers.eval local function expected_errors(errors) eq(errors, nvim.get_vvar('errors')) @@ -233,20 +234,31 @@ describe('assert function:', function() -- assert_fails({cmd}, [, {error}]) describe('assert_fails', function() it('should change v:errors when error does not match v:errmsg', function() - command([[call assert_fails('xxx', {})]]) + eq(1, eval([[assert_fails('xxx', {})]])) command([[call assert_match("Expected {} but got 'E731:", v:errors[0])]]) expected_errors({"Expected {} but got 'E731: using Dictionary as a String'"}) end) it('should not change v:errors when cmd errors', function() - call('assert_fails', 'NonexistentCmd') + eq(0, eval([[assert_fails('NonexistentCmd')]])) expected_empty() end) it('should change v:errors when cmd succeeds', function() - call('assert_fails', 'call empty("")') + eq(1, eval([[assert_fails('call empty("")', '')]])) expected_errors({'command did not fail: call empty("")'}) end) + + it('can specify and get a message about what failed', function() + eq(1, eval([[assert_fails('xxx', {}, 'stupid')]])) + command([[call assert_match("stupid: Expected {} but got 'E731:", v:errors[0])]]) + expected_errors({"stupid: Expected {} but got 'E731: using Dictionary as a String'"}) + end) + + it('can specify and get a message even when cmd succeeds', function() + eq(1, eval([[assert_fails('echo', '', 'echo command')]])) + expected_errors({'command did not fail: echo command'}) + end) end) -- assert_inrange({lower}, {upper}, {actual}[, {msg}]) From ad0f97f4123b3b84e0f0883afce305d20aec954a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 21 Sep 2019 23:18:56 +0200 Subject: [PATCH 0034/1293] vim-patch:8.1.2055: profile: adjust line format #11058 Problem: Not easy to jump to function line from profile. Solution: Use "file:99" instead of "file line 99" so that "gf" works. (Daniel Hahler, closes vim/vim#4951) https://github.com/vim/vim/commit/181d4f58cc421f2e6d3b16333d4cb70d35ad1342 --- src/nvim/eval.c | 2 +- src/nvim/testdir/test_profile.vim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index be761afa1f..2ddcd389fe 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -22270,7 +22270,7 @@ void func_dump_profile(FILE *fd) .channel_id = 0, }; char_u *p = get_scriptname(last_set, &should_free); - fprintf(fd, " Defined: %s line %" PRIdLINENR "\n", + fprintf(fd, " Defined: %s:%" PRIdLINENR "\n", p, fp->uf_script_ctx.sc_lnum); if (should_free) { xfree(p); diff --git a/src/nvim/testdir/test_profile.vim b/src/nvim/testdir/test_profile.vim index 7e853eeac3..b677ac3704 100644 --- a/src/nvim/testdir/test_profile.vim +++ b/src/nvim/testdir/test_profile.vim @@ -54,7 +54,7 @@ func Test_profile_func() call assert_equal(30, len(lines)) call assert_equal('FUNCTION Foo1()', lines[0]) - call assert_match('Defined:.*Xprofile_func.vim', lines[1]) + call assert_match('Defined:.*Xprofile_func.vim:3', lines[1]) call assert_equal('Called 2 times', lines[2]) call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[3]) call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[4]) From 45447e3b647259d78434798ddd9c2ae245dcdbcc Mon Sep 17 00:00:00 2001 From: Yoshio S Date: Sun, 22 Sep 2019 08:17:22 +0900 Subject: [PATCH 0035/1293] checkhealth: skip python checks if intentionally disabled #11044 close #11040 --- runtime/autoload/health/provider.vim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/runtime/autoload/health/provider.vim b/runtime/autoload/health/provider.vim index f1238edbde..f52c2c2cbf 100644 --- a/runtime/autoload/health/provider.vim +++ b/runtime/autoload/health/provider.vim @@ -276,7 +276,11 @@ function! s:check_python(version) abort let python_multiple = [] if exists(loaded_var) && !exists('*provider#'.pyname.'#Call') - call health#report_info('Disabled ('.loaded_var.'='.eval(loaded_var).'). This might be due to some previous error.') + let v = eval(loaded_var) + call health#report_info('Disabled ('.loaded_var.'='.v.').'.(0 is v ? '' : ' This might be due to some previous error.')) + if 0 is v + return + endif endif let [pyenv, pyenv_root] = s:check_for_pyenv() From 18e5869f56aab8a52d84185e5bd043799c36ae2d Mon Sep 17 00:00:00 2001 From: Zach Wegner Date: Sun, 15 Sep 2019 14:16:44 -0500 Subject: [PATCH 0036/1293] Fix "precedes" listchar behavior in wrap mode Previously, the "precedes" character would be rendered on every row when w_skipcol > 0 (i.e., when viewing a single line longer than the entire screen), instead of just on the first row. Make sure to only render it on the first row in this case. Add a test for this behavior. Fix documentation for the "precedes" character, which erroneously stated that it was only active when wrap mode was off. --- runtime/doc/options.txt | 5 ++--- src/nvim/screen.c | 2 +- test/functional/ui/highlight_spec.lua | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 188f7fc2e2..ec0f119033 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -3698,9 +3698,8 @@ A jump table for the options with a short description can be found at |Q_op|. off and the line continues beyond the right of the screen. *lcs-precedes* - precedes:c Character to show in the first column, when 'wrap' - is off and there is text preceding the character - visible in the first column. + precedes:c Character to show in the first column, when there is + text preceding the character visible in the first column. *lcs-conceal* conceal:c Character to show in place of concealed text, when 'conceallevel' is set to 1. A space when omitted. diff --git a/src/nvim/screen.c b/src/nvim/screen.c index f4aa10ecf5..f107985df7 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -3763,7 +3763,7 @@ win_line ( */ if (lcs_prec_todo != NUL && wp->w_p_list - && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0) + && (wp->w_p_wrap ? (wp->w_skipcol > 0 && row == 0) : wp->w_leftcol > 0) && filler_todo <= 0 && draw_state > WL_NR && c != NUL) { diff --git a/test/functional/ui/highlight_spec.lua b/test/functional/ui/highlight_spec.lua index f40f658275..95a19aec81 100644 --- a/test/functional/ui/highlight_spec.lua +++ b/test/functional/ui/highlight_spec.lua @@ -657,6 +657,30 @@ describe("'listchars' highlight", function() ]]) end) + it("'listchar' with wrap", function() + screen:set_default_attr_ids({ + [0] = {bold=true, foreground=Screen.colors.Blue}, + }) + feed_command('set wrap') + feed_command('set listchars=eol:¬,precedes:< list') + feed('90ia') + screen:expect([[ + {0:<}aaaaaaaaaaaaaaaaaaa| + aaaaaaaaaaaaaaaaaaaa| + aaaaaaaaaaaaaaaaaaaa| + aaaaaaaaa^a{0:¬} | + | + ]]) + feed('0') + screen:expect([[ + ^aaaaaaaaaaaaaaaaaaaa| + aaaaaaaaaaaaaaaaaaaa| + aaaaaaaaaaaaaaaaaaaa| + aaaaaaaaaaaaaaaaaaaa| + | + ]]) + end) + it("'listchar' in visual mode", function() screen:set_default_attr_ids({ [1] = {background=Screen.colors.Grey90}, From 16549324988be0717b59f7e5fec818ee9ad70f52 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 21 Sep 2019 20:29:15 -0700 Subject: [PATCH 0037/1293] vim-patch:8.1.2060: "precedes" in 'listchars' not used properly (Credit: Zach Wegner, https://github.com/neovim/neovim/pull/11034) Problem: "precedes" in 'listchars' not used properly. Solution: Correctly handle the "precedes" char in list mode for long lines. https://github.com/vim/vim/commit/bffba7f7042f6082e75b42484b15f66087b01941 --- runtime/doc/options.txt | 5 +-- src/nvim/screen.c | 8 ++--- src/nvim/testdir/test_display.vim | 55 +++++++++++++++++++++++++++++++ src/nvim/testdir/view_util.vim | 1 + 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index ec0f119033..d87898bb89 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -3698,8 +3698,9 @@ A jump table for the options with a short description can be found at |Q_op|. off and the line continues beyond the right of the screen. *lcs-precedes* - precedes:c Character to show in the first column, when there is - text preceding the character visible in the first column. + precedes:c Character to show in the first visible column of the + physical line, when there is text preceding the + character visible in the first column. *lcs-conceal* conceal:c Character to show in place of concealed text, when 'conceallevel' is set to 1. A space when omitted. diff --git a/src/nvim/screen.c b/src/nvim/screen.c index f107985df7..a866901b78 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -3756,11 +3756,9 @@ win_line ( char_attr = hl_combine_attr(char_attr, extra_attr); } - /* - * Handle the case where we are in column 0 but not on the first - * character of the line and the user wants us to show us a - * special character (via 'listchars' option "precedes:". - */ + // Handle the case where we are in column 0 but not on the first + // character of the line and the user wants us to show us a + // special character (via 'listchars' option "precedes:". if (lcs_prec_todo != NUL && wp->w_p_list && (wp->w_p_wrap ? (wp->w_skipcol > 0 && row == 0) : wp->w_leftcol > 0) diff --git a/src/nvim/testdir/test_display.vim b/src/nvim/testdir/test_display.vim index 5feb59eef1..66c13ded82 100644 --- a/src/nvim/testdir/test_display.vim +++ b/src/nvim/testdir/test_display.vim @@ -69,3 +69,58 @@ func! Test_display_foldtext_mbyte() set foldtext& fillchars& foldmethod& fdc& bw! endfunc + +func Test_display_listchars_precedes() + call NewWindow(10, 10) + " Need a physical line that wraps over the complete + " window size + call append(0, repeat('aaa aaa aa ', 10)) + call append(1, repeat(['bbb bbb bbb bbb'], 2)) + " remove blank trailing line + $d + set list nowrap + call cursor(1, 1) + " move to end of line and scroll 2 characters back + norm! $2zh + let lines=ScreenLines([1,4], winwidth(0)+1) + let expect = [ + \ " aaa aa $ |", + \ "$ |", + \ "$ |", + \ "~ |", + \ ] + call assert_equal(expect, lines) + set list listchars+=precedes:< nowrap + call cursor(1, 1) + " move to end of line and scroll 2 characters back + norm! $2zh + let lines = ScreenLines([1,4], winwidth(0)+1) + let expect = [ + \ " Date: Sun, 22 Sep 2019 16:02:28 +0900 Subject: [PATCH 0038/1293] env: use putenv_s for LC_ALL, LANG, etc. #11050 Problem: ":lang messages en_US.UTF-8" no longer overrides the language detected from the environment (at startup). Solution: In os_setenv, special-case "LC_ALL", "LANG", et al. to use putenv_s instead of uv_os_setenv. fixes #11045 --- src/nvim/os/env.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index f5dbf0694e..54fdd7961c 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -135,7 +135,16 @@ int os_setenv(const char *name, const char *value, int overwrite) } #endif uv_mutex_lock(&mutex); - int r = uv_os_setenv(name, value); + int r; +#ifdef WIN32 + // libintl uses getenv() for LC_ALL/LANG/etc., so we must use _putenv_s(). + if (striequal(name, "LC_ALL") || striequal(name, "LANGUAGE") + || striequal(name, "LANG") || striequal(name, "LC_MESSAGES")) { + r = _putenv_s(name, value); // NOLINT + assert(r == 0); + } +#endif + r = uv_os_setenv(name, value); assert(r != UV_EINVAL); // Destroy the old map item. Do this AFTER uv_os_setenv(), because `value` // could be a previous os_getenv() result. From f316916758c487054138762d66a966430e38e612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Tue, 17 Sep 2019 20:26:43 +0200 Subject: [PATCH 0039/1293] screen: missing redraw/highlight for ruler in message area --- src/nvim/screen.c | 13 ++++++---- test/functional/ui/messages_spec.lua | 36 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/nvim/screen.c b/src/nvim/screen.c index a866901b78..17a91f69d5 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -5101,6 +5101,8 @@ win_redr_custom ( win_T *ewp; int p_crb_save; + ScreenGrid *grid = &default_grid; + /* There is a tiny chance that this gets called recursively: When * redrawing a status line triggers redrawing the ruler or tabline. * Avoid trouble by not allowing recursion. */ @@ -5140,10 +5142,11 @@ win_redr_custom ( } maxwidth = wp->w_width - col; if (!wp->w_status_height) { + grid = &msg_grid_adj; row = Rows - 1; maxwidth--; // writing in last column may cause scrolling fillchar = ' '; - attr = 0; + attr = HL_ATTR(HLF_MSG); } use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0); @@ -5193,13 +5196,13 @@ win_redr_custom ( /* * Draw each snippet with the specified highlighting. */ - grid_puts_line_start(&default_grid, row); + grid_puts_line_start(grid, row); curattr = attr; p = buf; for (n = 0; hltab[n].start != NULL; n++) { int textlen = (int)(hltab[n].start - p); - grid_puts_len(&default_grid, p, textlen, row, col, curattr); + grid_puts_len(grid, p, textlen, row, col, curattr); col += vim_strnsize(p, textlen); p = hltab[n].start; @@ -5213,7 +5216,7 @@ win_redr_custom ( curattr = highlight_user[hltab[n].userhl - 1]; } // Make sure to use an empty string instead of p, if p is beyond buf + len. - grid_puts(&default_grid, p >= buf + len ? (char_u *)"" : p, row, col, + grid_puts(grid, p >= buf + len ? (char_u *)"" : p, row, col, curattr); grid_puts_line_flush(false); @@ -7058,7 +7061,7 @@ static void win_redr_ruler(win_T *wp, int always) } else { row = Rows - 1; fillchar = ' '; - attr = 0; + attr = HL_ATTR(HLF_MSG); width = Columns; off = 0; } diff --git a/test/functional/ui/messages_spec.lua b/test/functional/ui/messages_spec.lua index 875e4092a6..377d49c036 100644 --- a/test/functional/ui/messages_spec.lua +++ b/test/functional/ui/messages_spec.lua @@ -810,6 +810,7 @@ describe('ui/builtin messages', function() [4] = {bold = true, foreground = Screen.colors.SeaGreen4}, [5] = {foreground = Screen.colors.Blue1}, [6] = {bold = true, foreground = Screen.colors.Magenta}, + [7] = {background = Screen.colors.Grey20}, }) end) @@ -902,6 +903,41 @@ vimComment xxx match /\s"[^\-:.%#=*].*$/ms=s+1,lc=1 excludenl contains=@vim meths.command_output('syntax list vimComment')) -- luacheck: pop end) + + it('supports ruler with laststatus=0', function() + command("set ruler laststatus=0") + screen:expect{grid=[[ + ^ | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + 0,0-1 All | + ]]} + + command("hi MsgArea guibg=#333333") + screen:expect{grid=[[ + ^ | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {7: 0,0-1 All }| + ]]} + + command("set rulerformat=%15(%c%V\\ %p%%%)") + screen:expect{grid=[[ + ^ | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {7: 0,0-1 100% }| + ]]} + end) end) describe('ui/ext_messages', function() From ed11721b6bb36042ab065b5045c8eb01115b8902 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 22 Sep 2019 14:57:44 +0200 Subject: [PATCH 0040/1293] tests: unit: fix preprocess: pass -m32 for 32bit ABI (#11073) --- test/unit/preprocess.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/unit/preprocess.lua b/test/unit/preprocess.lua index 1073855a7d..3786bc2122 100644 --- a/test/unit/preprocess.lua +++ b/test/unit/preprocess.lua @@ -89,6 +89,10 @@ local Gcc = { get_defines_extra_flags = {'-std=c99', '-dM', '-E'}, get_declarations_extra_flags = {'-std=c99', '-P', '-E'}, } +if ffi.abi("32bit") then + table.insert(Gcc.get_defines_extra_flags, '-m32') + table.insert(Gcc.get_declarations_extra_flags, '-m32') +end function Gcc:define(name, args, val) local define = '-D' .. name From 6807779c68220e738b99486118c97c0ebc108b0e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 22 Sep 2019 14:58:38 +0200 Subject: [PATCH 0041/1293] tests: make 'win_update redraws lines properly' more readable (#11068) --- test/functional/ui/diff_spec.lua | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/test/functional/ui/diff_spec.lua b/test/functional/ui/diff_spec.lua index 572ed5c695..252991aca7 100644 --- a/test/functional/ui/diff_spec.lua +++ b/test/functional/ui/diff_spec.lua @@ -963,7 +963,7 @@ end) it('win_update redraws lines properly', function() local screen clear() - screen = Screen.new(30, 10) + screen = Screen.new(50, 10) screen:attach() screen:set_default_attr_ids({ [1] = {bold = true, foreground = Screen.colors.Blue1}, @@ -993,7 +993,7 @@ it('win_update redraws lines properly', function() 2 1a ]]) - command("vnew") + command("vnew left") insert([[ 2 2a @@ -1002,16 +1002,16 @@ it('win_update redraws lines properly', function() command("windo diffthis") command("windo 1") screen:expect{grid=[[ - {13: }{16:-------}{14:│}{13: }{15:^1 }| - {13: }{16:-------}{14:│}{13: }{15: }| - {13: }{16:-------}{14:│}{13: }{15: }| - {13: }2 {14:│}{13: }2 | - {13: }{17:2}{18:a }{14:│}{13: }{17:1}{18:a }| - {13: }{15:2b }{14:│}{13: }{16:------------------}| - {13: } {14:│}{13: } | - {1:~ }{14:│}{1:~ }| - {14:') feed('') @@ -1019,15 +1019,15 @@ it('win_update redraws lines properly', function() feed('') feed('') screen:expect{grid=[[ - {13: }{16:-------}{14:│}{13: }{15:1 }| - {13: }{16:-------}{14:│}{13: }{15: }| - {13: }{16:-------}{14:│}{13: }{15:^ }| - {13: }2 {14:│}{13: }2 | - {13: }{17:2}{18:a }{14:│}{13: }{17:1}{18:a }| - {13: }{15:2b }{14:│}{13: }{16:------------------}| - {13: } {14:│}{13: } | - {1:~ }{14:│}{1:~ }| - {14: Date: Sun, 22 Sep 2019 19:49:03 +0200 Subject: [PATCH 0042/1293] vim-patch:8.1.2052: using "x" before a closed fold may delete that fold Problem: Using "x" before a closed fold may delete that fold. Solution: Do not translate 'x' do "dl". (Christian Brabandt, closes vim/vim#4927) https://github.com/vim/vim/commit/7a9bd7c1e0ce1baf5a02daf36eeae3638aa315c7 --- src/nvim/normal.c | 9 ++++++++- src/nvim/testdir/test_fold.vim | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/nvim/normal.c b/src/nvim/normal.c index e7e6d2b365..ffdc8e23d7 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -6248,7 +6248,14 @@ static void nv_optrans(cmdarg_T *cap) if (cap->count0) { stuffnumReadbuff(cap->count0); } - stuffReadbuff(ar[strchr(str, (char)cap->cmdchar) - str]); + // If on an empty line and using 'x' and "l" is included in the + // whichwrap option, do not delete the next line. + if (cap->cmdchar == 'x' && vim_strchr(p_ww, 'l') != NULL + && gchar_cursor() == NUL) { + stuffReadbuff((char *)"dd"); + } else { + stuffReadbuff(ar[strchr(str, (char)cap->cmdchar) - str]); + } } cap->opcount = 0; } diff --git a/src/nvim/testdir/test_fold.vim b/src/nvim/testdir/test_fold.vim index 3cb42579be..03723b3cb5 100644 --- a/src/nvim/testdir/test_fold.vim +++ b/src/nvim/testdir/test_fold.vim @@ -756,3 +756,15 @@ func Test_fold_delete_with_marker() bwipe! bwipe! endfunc + +func Test_fold_delete_with_marker_and_whichwrap() + new + let content1 = [''] + let content2 = ['folded line 1 "{{{1', ' test', ' test2', ' test3', '', 'folded line 2 "{{{1', ' test', ' test2', ' test3'] + call setline(1, content1 + content2) + set fdm=marker ww+=l + normal! x + call assert_equal(content2, getline(1, '$')) + set fdm& ww& + bwipe! +endfunc From 6c3d34e4dfc7fd3f810dd78f9dec016540c13bda Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 22 Sep 2019 20:04:34 +0200 Subject: [PATCH 0043/1293] vim-patch:8.1.2059: fix for "x" deleting a fold has side effects Problem: Fix for "x" deleting a fold has side effects. Solution: Fix it where the fold is included. https://github.com/vim/vim/commit/56ebbabea1d8409ba67127b9674f6c714739c8e0 --- src/nvim/normal.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/nvim/normal.c b/src/nvim/normal.c index ffdc8e23d7..4dfde96e94 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -1553,9 +1553,11 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) if (!VIsual_active) { if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL)) oap->start.col = 0; - if (hasFolding(curwin->w_cursor.lnum, NULL, - &curwin->w_cursor.lnum)) + if ((curwin->w_cursor.col > 0 || oap->inclusive) + && hasFolding(curwin->w_cursor.lnum, NULL, + &curwin->w_cursor.lnum)) { curwin->w_cursor.col = (colnr_T)STRLEN(get_cursor_line_ptr()); + } } oap->end = curwin->w_cursor; curwin->w_cursor = oap->start; @@ -5125,14 +5127,10 @@ static void nv_right(cmdarg_T *cap) break; } else if (PAST_LINE) { curwin->w_set_curswant = true; - if (virtual_active()) + if (virtual_active()) { oneright(); - else { - if (has_mbyte) - curwin->w_cursor.col += - (*mb_ptr2len)(get_cursor_pos_ptr()); - else - ++curwin->w_cursor.col; + } else { + curwin->w_cursor.col += (*mb_ptr2len)(get_cursor_pos_ptr()); } } } @@ -6248,14 +6246,7 @@ static void nv_optrans(cmdarg_T *cap) if (cap->count0) { stuffnumReadbuff(cap->count0); } - // If on an empty line and using 'x' and "l" is included in the - // whichwrap option, do not delete the next line. - if (cap->cmdchar == 'x' && vim_strchr(p_ww, 'l') != NULL - && gchar_cursor() == NUL) { - stuffReadbuff((char *)"dd"); - } else { - stuffReadbuff(ar[strchr(str, (char)cap->cmdchar) - str]); - } + stuffReadbuff(ar[strchr(str, (char)cap->cmdchar) - str]); } cap->opcount = 0; } From 3626d2107eae23433b88612b01bba7015951d37d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 22 Sep 2019 21:51:55 +0200 Subject: [PATCH 0044/1293] terminfo_start: flush buffer #11074 This aligns with `terminfo_stop`, which also flushes the buffer after disabling things. This ensures Neovim gets the response to the terminal background query before exiting (`nvim -u NONE -cq` with e.g. urxvt or kitty). Caveats: * With kitty this causes some "flickering", likely since the alternate screen is being setup with `nvim -u NONE -cq`, whereas it would not be processed otherwise before quitting (as with the background query). * tmux after this patch may print ^[[I (CSI I / FocusGained) after `nvim -u NONE -cq`. Fixes https://github.com/neovim/neovim/issues/11062 --- src/nvim/tui/tui.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index 791756e5c5..0b3bed1c76 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -312,6 +312,7 @@ static void terminfo_start(UI *ui) uv_pipe_init(&data->write_loop, &data->output_handle.pipe, 0); uv_pipe_open(&data->output_handle.pipe, data->out_fd); } + flush_buf(ui); } static void terminfo_stop(UI *ui) From d9032308fbfd90eb71266bfe6d9a11930045f745 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 22 Sep 2019 21:56:31 -0400 Subject: [PATCH 0045/1293] vim-patch:8.0.1812: refactor qf_jump_to_usable_window() #11078 Problem: The qf_jump_to_usable_window() function is too long. Solution: Split it in parts. (Yegappan Lakshmanan, closes vim/vim#2891) https://github.com/vim/vim/commit/7a2b0e55e9460493c4a949bda8be70950dbb8f85 --- src/nvim/quickfix.c | 358 +++++++++++++++++++++++++------------------- 1 file changed, 203 insertions(+), 155 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 301f72fa79..b476a665c1 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -1795,26 +1795,24 @@ static void qf_clean_dir_stack(struct dir_stack_T **stackptr) } } -/* - * Check in which directory of the directory stack the given file can be - * found. - * Returns a pointer to the directory name or NULL if not found. - * Cleans up intermediate directory entries. - * - * TODO: How to solve the following problem? - * If we have the this directory tree: - * ./ - * ./aa - * ./aa/bb - * ./bb - * ./bb/x.c - * and make says: - * making all in aa - * making all in bb - * x.c:9: Error - * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb. - * qf_guess_filepath will return NULL. - */ +// Check in which directory of the directory stack the given file can be +// found. +// Returns a pointer to the directory name or NULL if not found. +// Cleans up intermediate directory entries. +// +// TODO(vim): How to solve the following problem? +// If we have this directory tree: +// ./ +// ./aa +// ./aa/bb +// ./bb +// ./bb/x.c +// and make says: +// making all in aa +// making all in bb +// x.c:9: Error +// Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb. +// qf_guess_filepath will return NULL. static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename) { struct dir_stack_T *ds_ptr; @@ -1876,7 +1874,7 @@ static bool qflist_valid(win_T *wp, unsigned int qf_id) /// When loading a file from the quickfix, the autocommands may modify it. /// This may invalidate the current quickfix entry. This function checks -/// whether a entry is still present in the quickfix. +/// whether an entry is still present in the quickfix list. /// Similar to location list. static bool is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr) { @@ -2005,6 +2003,18 @@ static qfline_T *get_nth_entry(qf_info_T *qi, int errornr, qfline_T *qf_ptr, return qf_ptr; } +// Find a window displaying a Vim help file. +static win_T *qf_find_help_win(void) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + if (bt_help(wp->w_buffer)) { + return wp; + } + } + return NULL; +} + /// Find a help window or open one. static int jump_to_help_window(qf_info_T *qi, int *opened_window) { @@ -2013,12 +2023,7 @@ static int jump_to_help_window(qf_info_T *qi, int *opened_window) if (cmdmod.tab != 0) { wp = NULL; } else { - FOR_ALL_WINDOWS_IN_TAB(wp2, curtab) { - if (bt_help(wp2->w_buffer)) { - wp = wp2; - break; - } - } + wp = qf_find_help_win(); } if (wp != NULL && wp->w_buffer->b_nwindows > 0) { @@ -2061,119 +2066,89 @@ static int jump_to_help_window(qf_info_T *qi, int *opened_window) return OK; } -/// Find a suitable window for opening a file (qf_fnum) and jump to it. -/// If the file is already opened in a window, jump to it. -static int qf_jump_to_usable_window(int qf_fnum, int *opened_window) +// Find a non-quickfix window using the given location list. +// Returns NULL if a matching window is not found. +static win_T *qf_find_win_with_loclist(const qf_info_T *ll) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { - win_T *usable_win_ptr = NULL; - int usable_win; - int flags; - win_T *win; - win_T *altwin; + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer)) { + return wp; + } + } + return NULL; +} - usable_win = 0; +// Find a window containing a normal buffer +static win_T *qf_find_win_with_normal_buf(void) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + if (wp->w_buffer->b_p_bt[0] == NUL) { + return wp; + } + } + return NULL; +} - qf_info_T *ll_ref = curwin->w_llist_ref; +// Go to a window in any tabpage containing the specified file. Returns TRUE +// if successfully jumped to the window. Otherwise returns FALSE. +static bool qf_goto_tabwin_with_file(int fnum) +{ + FOR_ALL_TAB_WINDOWS(tp, wp) { + if (wp->w_buffer->b_fnum == fnum) { + goto_tabpage_win(tp, wp); + return true; + } + } + return false; +} + +// Create a new window to show a file above the quickfix window. Called when +// only the quickfix window is present. +static int qf_open_new_file_win(qf_info_T *ll_ref) +{ + int flags = WSP_ABOVE; if (ll_ref != NULL) { - // Find a window using the same location list that is not a - // quickfix window. - FOR_ALL_WINDOWS_IN_TAB(usable_win_ptr2, curtab) { - if (usable_win_ptr2->w_llist == ll_ref - && !bt_quickfix(usable_win_ptr2->w_buffer)) { - usable_win_ptr = usable_win_ptr2; - usable_win = 1; - break; - } - } + flags |= WSP_NEWLOC; } + if (win_split(0, flags) == FAIL) { + return FAIL; // not enough room for window + } + p_swb = empty_option; // don't split again + swb_flags = 0; + RESET_BINDING(curwin); + if (ll_ref != NULL) { + // The new window should use the location list from the + // location list window + curwin->w_llist = ll_ref; + ll_ref->qf_refcount++; + } + return OK; +} - if (!usable_win) { - // Locate a window showing a normal buffer +// Go to a window that shows the right buffer. If the window is not found, go +// to the window just above the location list window. This is used for opening +// a file from a location window and not from a quickfix window. If some usable +// window is previously found, then it is supplied in 'use_win'. +static void qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, + qf_info_T *ll_ref) +{ + win_T *win = use_win; + + if (win == NULL) { + // Find the window showing the selected file FOR_ALL_WINDOWS_IN_TAB(win2, curtab) { - if (win2->w_buffer->b_p_bt[0] == NUL) { - usable_win = 1; + if (win2->w_buffer->b_fnum == qf_fnum) { + win = win2; break; } } - } - - // If no usable window is found and 'switchbuf' contains "usetab" - // then search in other tabs. - if (!usable_win && (swb_flags & SWB_USETAB)) { - FOR_ALL_TAB_WINDOWS(tp, wp) { - if (wp->w_buffer->b_fnum == qf_fnum) { - goto_tabpage_win(tp, wp); - usable_win = 1; - goto win_found; - } - } - } -win_found: - - // If there is only one window and it is the quickfix window, create a - // new one above the quickfix window. - if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win) { - flags = WSP_ABOVE; - if (ll_ref != NULL) { - flags |= WSP_NEWLOC; - } - if (win_split(0, flags) == FAIL) { - return FAIL; // not enough room for window - } - *opened_window = true; // close it when fail - p_swb = empty_option; // don't split again - swb_flags = 0; - RESET_BINDING(curwin); - if (ll_ref != NULL) { - // The new window should use the location list from the - // location list window - curwin->w_llist = ll_ref; - ll_ref->qf_refcount++; - } - } else { - if (curwin->w_llist_ref != NULL) { - // In a location window - win = usable_win_ptr; - if (win == NULL) { - // Find the window showing the selected file - FOR_ALL_WINDOWS_IN_TAB(win2, curtab) { - if (win2->w_buffer->b_fnum == qf_fnum) { - win = win2; - break; - } - } - if (win == NULL) { - // Find a previous usable window - win = curwin; - do { - if (win->w_buffer->b_p_bt[0] == NUL) { - break; - } - if (win->w_prev == NULL) { - win = lastwin; // wrap around the top - } else { - win = win->w_prev; // go to previous window - } - } while (win != curwin); - } - } - win_goto(win); - - // If the location list for the window is not set, then set it - // to the location list from the location window - if (win->w_llist == NULL) { - win->w_llist = ll_ref; - if (ll_ref != NULL) { - ll_ref->qf_refcount++; - } - } - } else { - // Try to find a window that shows the right buffer. - // Default to the window just above the quickfix buffer. + if (win == NULL) { + // Find a previous usable window win = curwin; - altwin = NULL; - for (;;) { - if (win->w_buffer->b_fnum == qf_fnum) { + do { + if (win->w_buffer->b_p_bt[0] == NUL) { break; } if (win->w_prev == NULL) { @@ -2181,26 +2156,104 @@ win_found: } else { win = win->w_prev; // go to previous window } - if (IS_QF_WINDOW(win)) { - // Didn't find it, go to the window before the quickfix window. - if (altwin != NULL) { - win = altwin; - } else if (curwin->w_prev != NULL) { - win = curwin->w_prev; - } else { - win = curwin->w_next; - } - break; - } + } while (win != curwin); + } + } + win_goto(win); - // Remember a usable window. - if (altwin == NULL && !win->w_p_pvw - && win->w_buffer->b_p_bt[0] == NUL) { - altwin = win; - } + // If the location list for the window is not set, then set it + // to the location list from the location window + if (win->w_llist == NULL) { + win->w_llist = ll_ref; + ll_ref->qf_refcount++; + } +} + +// Go to a window that shows the specified file. If a window is not found, go +// to the window just above the quickfix window. This is used for opening a +// file from a quickfix window and not from a location window. +static void qf_goto_win_with_qfl_file(int qf_fnum) +{ + win_T *win = curwin; + win_T *altwin = NULL; + for (;;) { + if (win->w_buffer->b_fnum == qf_fnum) { + break; + } + if (win->w_prev == NULL) { + win = lastwin; // wrap around the top + } else { + win = win->w_prev; // go to previous window + } + + if (IS_QF_WINDOW(win)) { + // Didn't find it, go to the window before the quickfix + // window. + if (altwin != NULL) { + win = altwin; + } else if (curwin->w_prev != NULL) { + win = curwin->w_prev; + } else { + win = curwin->w_next; } + break; + } - win_goto(win); + // Remember a usable window. + if (altwin == NULL + && !win->w_p_pvw + && win->w_buffer->b_p_bt[0] == NUL) { + altwin = win; + } + } + + win_goto(win); +} + +// Find a suitable window for opening a file (qf_fnum) from the +// quickfix/location list and jump to it. If the file is already opened in a +// window, jump to it. Otherwise open a new window to display the file. This is +// called from either a quickfix or a location list window. +static int qf_jump_to_usable_window(int qf_fnum, int *opened_window) +{ + win_T *usable_win_ptr = NULL; + bool usable_win = false; + + qf_info_T *ll_ref = curwin->w_llist_ref; + if (ll_ref != NULL) { + // Find a non-quickfix window with this location list + usable_win_ptr = qf_find_win_with_loclist(ll_ref); + if (usable_win_ptr != NULL) { + usable_win = true; + } + } + + if (!usable_win) { + // Locate a window showing a normal buffer + win_T *win = qf_find_win_with_normal_buf(); + if (win != NULL) { + usable_win = true; + } + } + + // If no usable window is found and 'switchbuf' contains "usetab" + // then search in other tabs. + if (!usable_win && (swb_flags & SWB_USETAB)) { + usable_win = qf_goto_tabwin_with_file(qf_fnum); + } + + // If there is only one window and it is the quickfix window, create a + // new one above the quickfix window. + if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win) { + if (qf_open_new_file_win(ll_ref) != OK) { + return FAIL; + } + *opened_window = true; // close it when fail + } else { + if (curwin->w_llist_ref != NULL) { // In a location window + qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref); + } else { // In a quickfix window + qf_goto_win_with_qfl_file(qf_fnum); } } @@ -2258,8 +2311,8 @@ static int qf_jump_edit_buffer(qf_info_T *qi, qfline_T *qf_ptr, int forceit, return retval; } -/// Goto the error line in the current file using either line/column number or a -/// search pattern. +/// Go to the error line in the current file using either line/column number or +/// a search pattern. static void qf_jump_goto_line(linenr_T qf_lnum, int qf_col, char_u qf_viscol, char_u *qf_pattern) { @@ -5177,7 +5230,7 @@ static int qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di) // Set quickfix/location list properties (title, items, context). // Also used to add items from parsing a list of lines. -// Used by the setqflist() and setloclist() VimL functions. +// Used by the setqflist() and setloclist() Vim script functions. static int qf_set_properties(qf_info_T *qi, const dict_T *what, int action, char_u *title) FUNC_ATTR_NONNULL_ALL @@ -5585,12 +5638,7 @@ void ex_helpgrep(exarg_T *eap) wp = curwin; } else { // Find an existing help window - FOR_ALL_WINDOWS_IN_TAB(wp2, curtab) { - if (bt_help(wp2->w_buffer)) { - wp = wp2; - break; - } - } + wp = qf_find_help_win(); } if (wp == NULL) { // Help window not found From 6d213593ed0af948d23e9d0e8c6d993577344b48 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 24 Sep 2019 01:28:04 -0400 Subject: [PATCH 0046/1293] vim-patch:8.0.1754: ex_helpgrep() is too long #11084 Problem: ex_helpgrep() is too long. Solution: Refactor the function. (Yegappan Lakshmanan, closes vim/vim#2766) https://github.com/vim/vim/commit/2225ebb48644f3924311b8df02a1319ab7675d42 --- src/nvim/quickfix.c | 260 +++++++++++++++++------------ src/nvim/testdir/test_quickfix.vim | 6 + 2 files changed, 156 insertions(+), 110 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index b476a665c1..528829e63d 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -5593,26 +5593,149 @@ void ex_cexpr(exarg_T *eap) } } -/* - * ":helpgrep {pattern}" - */ +// Get the location list for ":lhelpgrep" +static qf_info_T *hgr_get_ll(bool *new_ll) + FUNC_ATTR_NONNULL_ALL +{ + win_T *wp; + qf_info_T *qi; + + // If the current window is a help window, then use it + if (bt_help(curwin->w_buffer)) { + wp = curwin; + } else { + // Find an existing help window + wp = qf_find_help_win(); + } + + if (wp == NULL) { // Help window not found + qi = NULL; + } else { + qi = wp->w_llist; + } + if (qi == NULL) { + // Allocate a new location list for help text matches + if ((qi = ll_new_list()) == NULL) { + return NULL; + } + *new_ll = true; + } + + return qi; +} + +// Search for a pattern in a help file. +static void hgr_search_file( + qf_info_T *qi, + char_u *fname, + regmatch_T *p_regmatch) + FUNC_ATTR_NONNULL_ARG(1, 3) +{ + FILE *const fd = os_fopen((char *)fname, "r"); + if (fd == NULL) { + return; + } + + long lnum = 1; + while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) { + char_u *line = IObuff; + + if (vim_regexec(p_regmatch, line, (colnr_T)0)) { + int l = (int)STRLEN(line); + + // remove trailing CR, LF, spaces, etc. + while (l > 0 && line[l - 1] <= ' ') { + line[--l] = NUL; + } + + if (qf_add_entry(qi, + qi->qf_curlist, + NULL, // dir + fname, + NULL, + 0, + line, + lnum, + (int)(p_regmatch->startp[0] - line) + 1, // col + false, // vis_col + NULL, // search pattern + 0, // nr + 1, // type + true // valid + ) == FAIL) { + got_int = true; + if (line != IObuff) { + xfree(line); + } + break; + } + } + if (line != IObuff) { + xfree(line); + } + lnum++; + line_breakcheck(); + } + fclose(fd); +} + +// Search for a pattern in all the help files in the doc directory under +// the given directory. +static void hgr_search_files_in_dir( + qf_info_T *qi, + char_u *dirname, + regmatch_T *p_regmatch, + const char_u *lang) + FUNC_ATTR_NONNULL_ARG(1, 2, 3) +{ + int fcount; + char_u **fnames; + + // Find all "*.txt" and "*.??x" files in the "doc" directory. + add_pathsep((char *)dirname); + STRCAT(dirname, "doc/*.\\(txt\\|??x\\)"); // NOLINT + if (gen_expand_wildcards(1, &dirname, &fcount, + &fnames, EW_FILE|EW_SILENT) == OK + && fcount > 0) { + for (int fi = 0; fi < fcount && !got_int; fi++) { + // Skip files for a different language. + if (lang != NULL + && STRNICMP(lang, fnames[fi] + STRLEN(fnames[fi]) - 3, 2) != 0 + && !(STRNICMP(lang, "en", 2) == 0 + && STRNICMP("txt", fnames[fi] + STRLEN(fnames[fi]) - 3, 3) + == 0)) { + continue; + } + + hgr_search_file(qi, fnames[fi], p_regmatch); + } + FreeWild(fcount, fnames); + } +} + +// Search for a pattern in all the help files in the 'runtimepath'. +static void hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, + char_u *arg) + FUNC_ATTR_NONNULL_ALL +{ + // Check for a specified language + char_u *const lang = check_help_lang(arg); + + // Go through all directories in 'runtimepath' + char_u *p = p_rtp; + while (*p != NUL && !got_int) { + copy_option_part(&p, NameBuff, MAXPATHL, ","); + + hgr_search_files_in_dir(qi, NameBuff, p_regmatch, lang); + } +} + +// ":helpgrep {pattern}" void ex_helpgrep(exarg_T *eap) { - regmatch_T regmatch; - char_u *save_cpo; - char_u *p; - int fcount; - char_u **fnames; - FILE *fd; - int fi; - long lnum; - char_u *lang; - qf_info_T *qi = &ql_info; - int new_qi = FALSE; - char_u *au_name = NULL; - - /* Check for a specified language */ - lang = check_help_lang(eap->arg); + qf_info_T *qi = &ql_info; + bool new_qi = false; + char_u *au_name = NULL; switch (eap->cmdidx) { case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break; @@ -5626,109 +5749,26 @@ void ex_helpgrep(exarg_T *eap) } } - /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ - save_cpo = p_cpo; + // Make 'cpoptions' empty, the 'l' flag should not be used here. + char_u *const save_cpo = p_cpo; p_cpo = empty_option; if (eap->cmdidx == CMD_lhelpgrep) { - win_T *wp = NULL; - - // If the current window is a help window, then use it - if (bt_help(curwin->w_buffer)) { - wp = curwin; - } else { - // Find an existing help window - wp = qf_find_help_win(); - } - - if (wp == NULL) { // Help window not found - qi = NULL; - } else { - qi = wp->w_llist; - } + qi = hgr_get_ll(&new_qi); if (qi == NULL) { - /* Allocate a new location list for help text matches */ - qi = ll_new_list(); - new_qi = TRUE; + return; } } - regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING); - regmatch.rm_ic = FALSE; + regmatch_T regmatch = { + .regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING), + .rm_ic = false, + }; if (regmatch.regprog != NULL) { // Create a new quickfix list. qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep)); - // Go through all the directories in 'runtimepath' - p = p_rtp; - while (*p != NUL && !got_int) { - copy_option_part(&p, NameBuff, MAXPATHL, ","); - - /* Find all "*.txt" and "*.??x" files in the "doc" directory. */ - add_pathsep((char *)NameBuff); - STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)"); - - // Note: We cannot just do `&NameBuff` because it is a statically sized array - // so `NameBuff == &NameBuff` according to C semantics. - char_u *buff_list[1] = {NameBuff}; - if (gen_expand_wildcards(1, buff_list, &fcount, - &fnames, EW_FILE|EW_SILENT) == OK - && fcount > 0) { - for (fi = 0; fi < fcount && !got_int; fi++) { - // Skip files for a different language. - if (lang != NULL - && STRNICMP(lang, fnames[fi] + STRLEN(fnames[fi]) - 3, 2) != 0 - && !(STRNICMP(lang, "en", 2) == 0 - && STRNICMP("txt", fnames[fi] - + STRLEN(fnames[fi]) - 3, 3) == 0)) { - continue; - } - fd = os_fopen((char *)fnames[fi], "r"); - if (fd != NULL) { - lnum = 1; - while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) { - char_u *line = IObuff; - if (vim_regexec(®match, line, (colnr_T)0)) { - int l = (int)STRLEN(line); - - /* remove trailing CR, LF, spaces, etc. */ - while (l > 0 && line[l - 1] <= ' ') - line[--l] = NUL; - - if (qf_add_entry(qi, - qi->qf_curlist, - NULL, // dir - fnames[fi], - NULL, - 0, - line, - lnum, - (int)(regmatch.startp[0] - line) - + 1, // col - false, // vis_col - NULL, // search pattern - 0, // nr - 1, // type - true) // valid - == FAIL) { - got_int = true; - if (line != IObuff) { - xfree(line); - } - break; - } - } - if (line != IObuff) - xfree(line); - ++lnum; - line_breakcheck(); - } - fclose(fd); - } - } - FreeWild(fcount, fnames); - } - } + hgr_search_in_rtp(qi, ®match, eap->arg); vim_regfree(regmatch.regprog); diff --git a/src/nvim/testdir/test_quickfix.vim b/src/nvim/testdir/test_quickfix.vim index b7f45aeeb1..83ef3c2fce 100644 --- a/src/nvim/testdir/test_quickfix.vim +++ b/src/nvim/testdir/test_quickfix.vim @@ -2317,6 +2317,12 @@ func XvimgrepTests(cchar) call assert_equal('Xtestfile2', bufname('')) call assert_equal('Editor:Emacs EmAcS', l[0].text) + " Test for unloading a buffer after vimgrep searched the buffer + %bwipe + Xvimgrep /Editor/j Xtestfile* + call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded) + call assert_equal([], getbufinfo('Xtestfile2')) + call delete('Xtestfile1') call delete('Xtestfile2') endfunc From 445f2f409676f6e788861af439ce0b823aab3f37 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 24 Sep 2019 08:34:00 +0200 Subject: [PATCH 0047/1293] tui: flush ui buffer in tui_terminal_after_startup (#11083) This avoids having a dummy event to tickle the main loop. Confirmed using `nvim -u NONE -c 'au FocusGained * q'` in tmux (with `:set -g focus-events on`): without the flushing it would only exit after pressing a key. Moves the flushing done recently in 3626d2107. `nvim -u NONE -cq` is still working (i.e. consuming the response for the terminal background query itself), and the flickering mentioned in 3626d2107 is reduced again. Reverts part of bfb21f3e0 (#7729). --- src/nvim/event/loop.c | 4 ---- src/nvim/tui/tui.c | 5 +---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/nvim/event/loop.c b/src/nvim/event/loop.c index a01bbc9888..c2be472acd 100644 --- a/src/nvim/event/loop.c +++ b/src/nvim/event/loop.c @@ -162,10 +162,6 @@ size_t loop_size(Loop *loop) return rv; } -void loop_dummy_event(void **argv) -{ -} - static void async_cb(uv_async_t *handle) { Loop *l = handle->loop->data; diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index 0b3bed1c76..55c4a955c2 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -312,7 +312,6 @@ static void terminfo_start(UI *ui) uv_pipe_init(&data->write_loop, &data->output_handle.pipe, 0); uv_pipe_open(&data->output_handle.pipe, data->out_fd); } - flush_buf(ui); } static void terminfo_stop(UI *ui) @@ -364,6 +363,7 @@ static void tui_terminal_after_startup(UI *ui) // Emit this after Nvim startup, not during. This works around a tmux // 2.3 bug(?) which caused slow drawing during startup. #7649 unibi_out_ext(ui, data->unibi_ext.enable_focus_reporting); + flush_buf(ui); } static void tui_terminal_stop(UI *ui) @@ -431,9 +431,6 @@ static void tui_main(UIBridgeData *bridge, UI *ui) } if (!tui_is_stopped(ui)) { tui_terminal_after_startup(ui); - // Tickle `main_loop` with a dummy event, else the initial "focus-gained" - // terminal response may not get processed until user hits a key. - loop_schedule_deferred(&main_loop, event_create(loop_dummy_event, 0)); } // "Passive" (I/O-driven) loop: TUI thread "main loop". while (!tui_is_stopped(ui)) { From 2476a97ced2a868b0dd2146618c9c5d77c1c84b1 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 24 Sep 2019 08:35:29 +0200 Subject: [PATCH 0048/1293] tui_spec: improve "TUI paste: exactly 64 bytes" (#11086) Doing the screen test first might give insights about a possible (flaky?) failure, where it looks like "feed_data" is processed out of order: [ ERROR ] test/functional/terminal/tui_spec.lua @ 561: TUI paste: exactly 64 bytes #10311 test/functional/helpers.lua:388: retry() attempts: 490 test/functional/terminal/tui_spec.lua:66: Expected objects to be the same. Passed in: (table: 0x44042de8) { *[1] = ' endzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' } Expected: (table: 0x41d6e568) { *[1] = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz end' } stack traceback: test/functional/helpers.lua:388: in function 'retry' test/functional/terminal/tui_spec.lua:63: in function 'expect_child_buf_lines' test/functional/terminal/tui_spec.lua:569: in function Ref: https://github.com/neovim/neovim/pull/11083#issuecomment-534375201 Build log: https://travis-ci.org/neovim/neovim/jobs/588749739#L5597 --- test/functional/terminal/tui_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index c55093cb0f..f70b630442 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -566,7 +566,6 @@ describe('TUI', function() feed_data('\027[200~'..expected..'\027[201~') feed_data(' end') expected = expected..' end' - expect_child_buf_lines({expected}) screen:expect([[ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz| zzzzzzzzzzzzzz end{1: } | @@ -576,6 +575,7 @@ describe('TUI', function() {3:-- INSERT --} | {3:-- TERMINAL --} | ]]) + expect_child_buf_lines({expected}) end) it('paste: big burst of input', function() From 0ab7da8561bfcc4c644afb9cf48083a5696c1792 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 24 Sep 2019 08:55:27 +0200 Subject: [PATCH 0049/1293] timer_spec: fix/harden flaky tests (#11080) Those are flaky when using luacov (which causes reproducible slowness). E.g.: [ ERROR ] test/functional\eval\timer_spec.lua @ 105: timers can invoke redraw in blocking getchar() call test\functional\ui\screen.lua:587: Row 3 did not match. Expected: |ITEM 1 | |ITEM 2 | |*{1:~ }| |{1:~ }| |{1:~ }| |^ | Actual: |ITEM 1 | |ITEM 2 | |*ITEM 3 | |{1:~ }| |{1:~ }| |^ | --- test/functional/eval/timer_spec.lua | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/test/functional/eval/timer_spec.lua b/test/functional/eval/timer_spec.lua index 2ccb9cfbac..ef7df69fdb 100644 --- a/test/functional/eval/timer_spec.lua +++ b/test/functional/eval/timer_spec.lua @@ -111,7 +111,13 @@ describe('timers', function() curbufmeths.set_lines(0, -1, true, {"ITEM 1", "ITEM 2"}) source([[ + let g:cont = 0 func! AddItem(timer) + if !g:cont + return + endif + call timer_stop(a:timer) + call nvim_buf_set_lines(0, 2, 2, v:true, ['ITEM 3']) " Meant to test for what Vim tests in Test_peek_and_get_char. @@ -121,7 +127,7 @@ describe('timers', function() endfunc ]]) nvim_async("command", "let g:c2 = getchar()") - nvim_async("command", "call timer_start("..load_adjust(100)..", 'AddItem')") + nvim_async("command", "call timer_start("..load_adjust(100)..", 'AddItem', {'repeat': -1})") screen:expect([[ ITEM 1 | @@ -131,6 +137,7 @@ describe('timers', function() {1:~ }| ^ | ]]) + nvim_async("command", "let g:cont = 1") screen:expect([[ ITEM 1 | @@ -222,12 +229,17 @@ describe('timers', function() source([[ let g:val = 0 func! MyHandler(timer) + while !g:val + return + endwhile + call timer_stop(a:timer) + echo "evil" redraw - let g:val = 1 + let g:val = 2 endfunc ]]) - command("call timer_start(100, 'MyHandler', {'repeat': 1})") + command("call timer_start(100, 'MyHandler', {'repeat': -1})") feed(":good") screen:expect([[ | @@ -237,6 +249,7 @@ describe('timers', function() {0:~ }| :good^ | ]]) + command('let g:val = 1') screen:expect{grid=[[ | @@ -247,6 +260,6 @@ describe('timers', function() :good^ | ]], intermediate=true, timeout=load_adjust(200)} - eq(1, eval('g:val')) + eq(2, eval('g:val')) end) end) From 81b19df5f2f319a36c4230a5f4bcea30d6306f4a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 25 Sep 2019 00:09:23 +0200 Subject: [PATCH 0050/1293] cmake/GetCompileFlags: include CMAKE_C_COMPILER_ARG1 (#11091) This is used internally (e.g. on Travis) for 32-bit builds (`-m32`). --- cmake/GetCompileFlags.cmake | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmake/GetCompileFlags.cmake b/cmake/GetCompileFlags.cmake index 2238744a66..49b57f6f75 100644 --- a/cmake/GetCompileFlags.cmake +++ b/cmake/GetCompileFlags.cmake @@ -3,6 +3,13 @@ function(get_compile_flags _compile_flags) set(compile_flags " ") # Get C compiler. + if(CMAKE_C_COMPILER_ARG1) + string(REPLACE + "" + " ${CMAKE_C_COMPILER_ARG1}" + compile_flags + "${compile_flags}") + endif() string(REPLACE "" "${CMAKE_C_COMPILER}" From db6b4b677d611a2891a5b6d686e485aea08a8f81 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 25 Sep 2019 02:20:32 +0200 Subject: [PATCH 0051/1293] tests: busted: nvim handler: display durations always (#11075) This shows them also with test failures/errors, where it is useful to see how long the test took (for flaky failures running into timeout). --- test/busted/outputHandlers/nvim.lua | 44 ++++++++--------------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/test/busted/outputHandlers/nvim.lua b/test/busted/outputHandlers/nvim.lua index 1b500fc999..8f3aad776e 100644 --- a/test/busted/outputHandlers/nvim.lua +++ b/test/busted/outputHandlers/nvim.lua @@ -227,8 +227,12 @@ return function(options) return nil, true end + local function write_status(element, string) + io.write(timeString:format(getElapsedTime(element)) .. ' ' .. string) + io.flush() + end + handler.testEnd = function(element, _parent, status, _debug) - local elapsedTime_ms = getElapsedTime(element) local string fileTestCount = fileTestCount + 1 @@ -241,45 +245,21 @@ return function(options) string = skippedString elseif status == 'failure' then failureCount = failureCount + 1 - string = nil + string = failureString .. failureDescription(handler.failures[#handler.failures]) elseif status == 'error' then errorCount = errorCount + 1 - string = nil + string = errorString .. failureDescription(handler.errors[#handler.errors]) + else + string = "unexpected test status! ("..status..")" end + write_status(element, string) - if string ~= nil then - if elapsedTime_ms == elapsedTime_ms then - string = timeString:format(elapsedTime_ms) .. ' ' .. string - end - io.write(string) - io.flush() - end - - return nil, true - end - - handler.testFailure = function(_element, _parent, _message, _debug) - io.write(failureString) - io.flush() - - io.write(failureDescription(handler.failures[#handler.failures])) - io.flush() - return nil, true - end - - handler.testError = function(_element, _parent, _message, _debug) - io.write(errorString) - io.flush() - - io.write(failureDescription(handler.errors[#handler.errors])) - io.flush() return nil, true end handler.error = function(element, _parent, _message, _debug) if element.descriptor ~= 'it' then - io.write(failureDescription(handler.errors[#handler.errors])) - io.flush() + write_status(element, failureDescription(handler.errors[#handler.errors])) errorCount = errorCount + 1 end @@ -293,8 +273,6 @@ return function(options) busted.subscribe({ 'file', 'end' }, handler.fileEnd) busted.subscribe({ 'test', 'start' }, handler.testStart, { predicate = handler.cancelOnPending }) busted.subscribe({ 'test', 'end' }, handler.testEnd, { predicate = handler.cancelOnPending }) - busted.subscribe({ 'failure', 'it' }, handler.testFailure) - busted.subscribe({ 'error', 'it' }, handler.testError) busted.subscribe({ 'failure' }, handler.error) busted.subscribe({ 'error' }, handler.error) From 227ef7162138a55657b013ae633978c54a1411a8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 9 Sep 2019 09:09:22 +0200 Subject: [PATCH 0052/1293] third-party: update libuv to v1.31.0 --- third-party/CMakeLists.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index 43cad34aae..c555151c35 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -134,11 +134,12 @@ include(ExternalProject) if(WIN32) # "nvim" branch of https://github.com/neovim/libuv - set(LIBUV_URL https://github.com/neovim/libuv/archive/eeae18d085de25f138c23966f98a179f0fb609e7.tar.gz) - set(LIBUV_SHA256 c37d0b7cb1defe69ae8dbb4d712c0d7cf838d6539178e8bcf71c72579ab5b666) + set(LIBUV_URL https://github.com/neovim/libuv/archive/d5ff3004d26b9bb863b76247399a9c72a0ff184c.tar.gz) + set(LIBUV_SHA256 0f5dfd92269713ed275273966ed73578fc68db669c509b01210cd58c1cf6361d) else() - set(LIBUV_URL https://github.com/libuv/libuv/archive/v1.30.0.tar.gz) - set(LIBUV_SHA256 44c8fdadf3b3f393006a4ac4ba144020673a3f9cd72bed1fbb2c366ebcf0d199) + # blueyed/nvim-fixes (for *BSD build fixes). + set(LIBUV_URL https://github.com/blueyed/libuv/archive/2af4cf2.tar.gz) + set(LIBUV_SHA256 SKIP) endif() set(MSGPACK_URL https://github.com/msgpack/msgpack-c/releases/download/cpp-3.0.0/msgpack-3.0.0.tar.gz) From bb6b1267e7532e0c2e065c4e9b5552623062c70f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 11 Sep 2019 13:48:09 +0200 Subject: [PATCH 0053/1293] Revert "win/os_env_exists(): workaround libuv bug #10734" This reverts commit 278c5d452c2cbc436a9cc317407ae6021a226c3a. --- src/nvim/os/env.c | 3 --- test/functional/eval/environ_spec.lua | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index 54fdd7961c..13853016d1 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -102,9 +102,6 @@ bool os_env_exists(const char *name) assert(r != UV_EINVAL); if (r != 0 && r != UV_ENOENT && r != UV_ENOBUFS) { ELOG("uv_os_getenv(%s) failed: %d %s", name, r, uv_err_name(r)); -#ifdef WIN32 - return (r == UV_UNKNOWN); -#endif } return (r == 0 || r == UV_ENOBUFS); } diff --git a/test/functional/eval/environ_spec.lua b/test/functional/eval/environ_spec.lua index 4c2adcf1bf..54d2dc960b 100644 --- a/test/functional/eval/environ_spec.lua +++ b/test/functional/eval/environ_spec.lua @@ -10,6 +10,7 @@ describe('environment variables', function() eq("", environ()['EMPTY_VAR']) eq(nil, environ()['DOES_NOT_EXIST']) end) + it('exists() handles empty env variable', function() clear({env={EMPTY_VAR=""}}) eq(1, exists('$EMPTY_VAR')) From 70827ea1fa3db6cb221b8c50677e1a6524937a5c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 11 Sep 2019 13:47:23 +0200 Subject: [PATCH 0054/1293] test/functional/preload.lua: _set_fmode for Windows --- test/functional/preload.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/functional/preload.lua b/test/functional/preload.lua index 1107b45d54..24a3977e6b 100644 --- a/test/functional/preload.lua +++ b/test/functional/preload.lua @@ -2,3 +2,13 @@ -- Busted started doing this to help provide more isolation. See issue #62 -- for more information about this. local helpers = require('test.functional.helpers')(nil) +local iswin = helpers.iswin + +if iswin() then + local ffi = require('ffi') + ffi.cdef[[ + typedef int errno_t; + errno_t _set_fmode(int mode); + ]] + ffi.C._set_fmode(0x8000) +end From 0571145c40b0a2ae106acc43891c2680c76b8383 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 25 Sep 2019 09:15:33 +0200 Subject: [PATCH 0055/1293] paste: fix handling of "<" in cmdline (#11094) Fixes https://github.com/neovim/neovim/issues/11088. --- src/nvim/lua/vim.lua | 2 +- test/functional/terminal/tui_spec.lua | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index a03e97490d..b1a684b977 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -189,7 +189,7 @@ paste = (function() if mode == 'c' and not got_line1 then -- cmdline-mode: paste only 1 line. got_line1 = (#lines > 1) vim.api.nvim_set_option('paste', true) -- For nvim_input(). - local line1, _ = string.gsub(lines[1], '[\r\n\012\027]', ' ') -- Scrub. + local line1 = lines[1]:gsub('<', ''):gsub('[\r\n\012\027]', ' ') -- Scrub. vim.api.nvim_input(line1) vim.api.nvim_set_option('paste', false) elseif mode ~= 'c' then -- Else: discard remaining cmdline-mode chunks. diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index f70b630442..89468359ef 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -578,6 +578,23 @@ describe('TUI', function() expect_child_buf_lines({expected}) end) + it('paste: less-than sign in cmdline #11088', function() + local expected = '<' + feed_data(':') + wait_for_mode('c') + -- "bracketed paste" + feed_data('\027[200~'..expected..'\027[201~') + screen:expect{grid=[[ + | + {4:~ }| + {4:~ }| + {4:~ }| + {5:[No Name] }| + :<{1: } | + {3:-- TERMINAL --} | + ]]} + end) + it('paste: big burst of input', function() feed_data(':set ruler\n') local t = {} From cb252071718a58c2d9747177ebeb81ff1210ddd1 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 17 Jun 2019 22:35:07 +0200 Subject: [PATCH 0056/1293] vim-patch:8.0.0914: highlight attributes are always combined (#10256) Problem: Highlight attributes are always combined. Solution: Add the 'nocombine' value to replace attributes instead of combining them. (scauligi, closes vim/vim#1963) https://github.com/vim/vim/commit/0cd2a94a4030f6bd12eaec44db92db108e33c913 Closes https://github.com/neovim/neovim/pull/10256. --- runtime/doc/syntax.txt | 5 +-- src/nvim/highlight.c | 12 +++++-- src/nvim/highlight_defs.h | 1 + src/nvim/syntax.c | 4 +-- test/functional/ui/highlight_spec.lua | 48 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index 0a4257e2b2..5424ad00ec 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -4720,18 +4720,19 @@ the same syntax file on all UIs. *bold* *underline* *undercurl* *inverse* *italic* *standout* - *strikethrough* + *nocombine* *strikethrough* cterm={attr-list} *attr-list* *highlight-cterm* *E418* attr-list is a comma separated list (without spaces) of the following items (in any order): bold underline undercurl curly underline + strikethrough reverse inverse same as reverse italic standout - strikethrough + nocombine override attributes instead of combining them NONE no attributes used (used to reset it) Note that "bold" can be used here and by using a bold font. They diff --git a/src/nvim/highlight.c b/src/nvim/highlight.c index 093cc4923b..83ee89b2a1 100644 --- a/src/nvim/highlight.c +++ b/src/nvim/highlight.c @@ -308,8 +308,16 @@ int hl_combine_attr(int char_attr, int prim_attr) // start with low-priority attribute, and override colors if present below. HlAttrs new_en = char_aep; - new_en.cterm_ae_attr |= spell_aep.cterm_ae_attr; - new_en.rgb_ae_attr |= spell_aep.rgb_ae_attr; + if (spell_aep.cterm_ae_attr & HL_NOCOMBINE) { + new_en.cterm_ae_attr = spell_aep.cterm_ae_attr; + } else { + new_en.cterm_ae_attr |= spell_aep.cterm_ae_attr; + } + if (spell_aep.rgb_ae_attr & HL_NOCOMBINE) { + new_en.rgb_ae_attr = spell_aep.rgb_ae_attr; + } else { + new_en.rgb_ae_attr |= spell_aep.rgb_ae_attr; + } if (spell_aep.cterm_fg_color > 0) { new_en.cterm_fg_color = spell_aep.cterm_fg_color; diff --git a/src/nvim/highlight_defs.h b/src/nvim/highlight_defs.h index f9c2c03fc6..255699c8e0 100644 --- a/src/nvim/highlight_defs.h +++ b/src/nvim/highlight_defs.h @@ -18,6 +18,7 @@ typedef enum { HL_UNDERCURL = 0x10, HL_STANDOUT = 0x20, HL_STRIKETHROUGH = 0x40, + HL_NOCOMBINE = 0x80, } HlAttrFlags; /// Stores a complete highlighting entry, including colors and attributes diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 675d484d67..ac8ace9fff 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -116,10 +116,10 @@ static int include_link = 0; /* when 2 include "nvim/link" and "clear" */ /// following names, separated by commas (but no spaces!). static char *(hl_name_table[]) = { "bold", "standout", "underline", "undercurl", - "italic", "reverse", "inverse", "strikethrough", "NONE" }; + "italic", "reverse", "inverse", "strikethrough", "nocombine", "NONE" }; static int hl_attr_table[] = { HL_BOLD, HL_STANDOUT, HL_UNDERLINE, HL_UNDERCURL, HL_ITALIC, HL_INVERSE, - HL_INVERSE, HL_STRIKETHROUGH, 0 }; + HL_INVERSE, HL_STRIKETHROUGH, HL_NOCOMBINE, 0 }; // The patterns that are being searched for are stored in a syn_pattern. // A match item consists of one pattern. diff --git a/test/functional/ui/highlight_spec.lua b/test/functional/ui/highlight_spec.lua index 95a19aec81..1b25570997 100644 --- a/test/functional/ui/highlight_spec.lua +++ b/test/functional/ui/highlight_spec.lua @@ -438,6 +438,54 @@ describe('highlight', function() }) end) + it('nocombine', function() + screen:detach() + screen = Screen.new(25,6) + screen:set_default_attr_ids{ + [1] = {foreground = Screen.colors.SlateBlue, underline = true}, + [2] = {bold = true, foreground = Screen.colors.Blue1}, + [3] = {underline = true, reverse = true, foreground = Screen.colors.SlateBlue}, + [4] = {background = Screen.colors.Yellow, reverse = true, foreground = Screen.colors.SlateBlue}, + [5] = {foreground = Screen.colors.Red}, + } + screen:attach() + feed_command('syntax on') + feed_command('hi! Underlined cterm=underline gui=underline') + feed_command('syn keyword Underlined foobar') + feed_command('hi Search cterm=inverse,nocombine gui=inverse,nocombine') + insert([[ + foobar + foobar + ]]) + screen:expect{grid=[[ + {1:foobar} | + {1:foobar} | + ^ | + {2:~ }| + {2:~ }| + | + ]]} + + feed('/foo') + screen:expect{grid=[[ + {3:foo}{1:bar} | + {4:foo}{1:bar} | + | + {2:~ }| + {2:~ }| + /foo^ | + ]]} + feed('') + screen:expect{grid=[[ + {4:^foo}{1:bar} | + {4:foo}{1:bar} | + | + {2:~ }| + {2:~ }| + {5:search hit...uing at TOP} | + ]]} + end) + it('guisp (special/undercurl)', function() feed_command('syntax on') feed_command('syn keyword TmpKeyword neovim') From b52ae0e8ba04498b0dd6de2e2947a45057111b3c Mon Sep 17 00:00:00 2001 From: John Szakmeister Date: Sun, 1 Sep 2019 12:13:34 -0400 Subject: [PATCH 0057/1293] build: add support for building for FreeBSD under Sourcehut [skip ci] --- .builds/freebsd.yml | 40 ++++++++++++++++++++++++++++++++++++++++ test/helpers.lua | 7 +++++-- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .builds/freebsd.yml diff --git a/.builds/freebsd.yml b/.builds/freebsd.yml new file mode 100644 index 0000000000..9ffd6d9c37 --- /dev/null +++ b/.builds/freebsd.yml @@ -0,0 +1,40 @@ +image: freebsd/12.x + +packages: +- cmake +- gmake +- ninja +- libtool +- sha +- automake +- pkgconf +- unzip +- wget +- gettext +- python +- libffi + +sources: +- https://github.com/neovim/neovim + +environment: + SOURCEHUT: 1 + LANG: en_US.UTF-8 + CMAKE_EXTRA_FLAGS: -DTRAVIS_CI_BUILD=ON -DMIN_LOG_LEVEL=3 + +tasks: +- build-deps: | + cd neovim + gmake deps +- build: | + cd neovim + gmake CMAKE_BUILD_TYPE=Release CMAKE_EXTRA_FLAGS="${CMAKE_EXTRA_FLAGS}" nvim +- test: | + cd neovim + gmake unittest functionaltest + +# Unfortunately, oldtest is tanking hard on sourcehut's FreeBSD instance +# and not producing any logs as a result. So don't do this task for now. +# - test-oldtest: | +# cd neovim +# gmake oldtest diff --git a/test/helpers.lua b/test/helpers.lua index ebc0a7d811..30e43a9ea4 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -715,11 +715,14 @@ end function module.isCI(name) local any = (name == nil) - assert(any or name == 'appveyor' or name == 'quickbuild' or name == 'travis') + assert(any or name == 'appveyor' or name == 'quickbuild' or name == 'travis' + or name == 'sourcehut') local av = ((any or name == 'appveyor') and nil ~= os.getenv('APPVEYOR')) local tr = ((any or name == 'travis') and nil ~= os.getenv('TRAVIS')) local qb = ((any or name == 'quickbuild') and nil ~= lfs.attributes('/usr/home/quickbuild')) - return tr or av or qb + local sh = ((any or name == 'sourcehut') and nil ~= os.getenv('SOURCEHUT')) + return tr or av or qb or sh + end -- Gets the contents of $NVIM_LOG_FILE for printing to the build log. From 54c66e636aa7c9f9379c0028e62c7d6d617f691c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 26 Sep 2019 07:37:51 +0200 Subject: [PATCH 0058/1293] Fix two more flaky tests (#11095) * mode_spec: retry with increasing matchtime `matchtime=2` might still be too low (with luacov on AppVeyor). [ ERROR ] test/functional\ui\mode_spec.lua @ 47: ui mode_change event works in insert mode test\functional\ui\screen.lua:587: mode Expected objects to be the same. Passed in: (string) 'insert' Expected: (string) 'showmatch' Hint: full state of "mode": "insert" Followup to fe60013fb. ref #10941 Initially regressed in 7ed212262242 `` * ui/screen_basic_spec: set timeoutlen=10000 This fixes the test on slow CI. Ref: https://ci.appveyor.com/project/neovim/neovim/builds/27600387/job/t468h2b3w9lwtlm5#L10930 --- test/functional/ui/mode_spec.lua | 49 ++++++++++++++---------- test/functional/ui/screen_basic_spec.lua | 1 + 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/test/functional/ui/mode_spec.lua b/test/functional/ui/mode_spec.lua index 200f6eecdb..9390f268b3 100644 --- a/test/functional/ui/mode_spec.lua +++ b/test/functional/ui/mode_spec.lua @@ -3,6 +3,7 @@ local Screen = require('test.functional.ui.screen') local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert local command = helpers.command +local retry = helpers.retry describe('ui mode_change event', function() local screen @@ -61,30 +62,36 @@ describe('ui mode_change event', function() | ]], mode="normal"} + local matchtime = 0 command("set showmatch") - command("set matchtime=2") -- tenths of seconds - feed('a(stuff') - screen:expect{grid=[[ - word(stuff^ | - {0:~ }| - {0:~ }| - {2:-- INSERT --} | - ]], mode="insert"} + retry(nil, nil, function() + matchtime = matchtime + 1 + local screen_timeout = 1000 * matchtime -- fail faster for retry. - feed(')') - screen:expect{grid=[[ - word^(stuff) | - {0:~ }| - {0:~ }| - {2:-- INSERT --} | - ]], mode="showmatch"} + command("set matchtime=" .. matchtime) -- tenths of seconds + feed('a(stuff') + screen:expect{grid=[[ + word(stuff^ | + {0:~ }| + {0:~ }| + {2:-- INSERT --} | + ]], mode="insert", timeout=screen_timeout} - screen:expect{grid=[[ - word(stuff)^ | - {0:~ }| - {0:~ }| - {2:-- INSERT --} | - ]], mode="insert"} + feed(')') + screen:expect{grid=[[ + word^(stuff) | + {0:~ }| + {0:~ }| + {2:-- INSERT --} | + ]], mode="showmatch", timeout=screen_timeout} + + screen:expect{grid=[[ + word(stuff)^ | + {0:~ }| + {0:~ }| + {2:-- INSERT --} | + ]], mode="insert", timeout=screen_timeout} + end) end) it('works in replace mode', function() diff --git a/test/functional/ui/screen_basic_spec.lua b/test/functional/ui/screen_basic_spec.lua index 46f0b5060c..150ee2a103 100644 --- a/test/functional/ui/screen_basic_spec.lua +++ b/test/functional/ui/screen_basic_spec.lua @@ -915,6 +915,7 @@ local function screen_tests(linegrid) -- Regression test for #8357 it('does not have artifacts after temporary chars in insert mode', function() + command('set timeoutlen=10000') command('inoremap jk ') feed('ifooj') screen:expect([[ From fe9d54f418973f59ec88ca05489da6df3609811f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 26 Sep 2019 23:43:49 +0200 Subject: [PATCH 0059/1293] doc: contrib/local.mk.example: include ENABLE_LTO (#11097) Using it takes 30+ additional seconds for me with a ccache-enabled build (43s vs. 12s). While it certainly makes sense to use DEBUG during development, bisecting etc, it should be made clearer what causes this. --- contrib/local.mk.example | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contrib/local.mk.example b/contrib/local.mk.example index 5a31ded59b..4f7f077999 100644 --- a/contrib/local.mk.example +++ b/contrib/local.mk.example @@ -25,6 +25,12 @@ # # CMAKE_BUILD_TYPE := Debug +# With non-Debug builds interprocedural optimization (IPO) (which includes +# link-time optimization (LTO)) is enabled by default, which causes the link +# step to take a significant amout of time, which is relevant when building +# often. You can disable it explicitly: +# CMAKE_EXTRA_FLAGS += -DENABLE_LTO=OFF + # Log levels: 0 (DEBUG), 1 (INFO), 2 (WARNING), 3 (ERROR) # Default is 1 (INFO) unless CMAKE_BUILD_TYPE is Release or RelWithDebInfo. # CMAKE_EXTRA_FLAGS += -DMIN_LOG_LEVEL=1 From 0d9a3c86a1c7143187398e6cb6005ed06a5e2fde Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 28 Sep 2019 00:32:22 +0200 Subject: [PATCH 0060/1293] vim-patch:8.1.2083: multi-byte chars do not work properly with "%.*S" in printf() (#11106) Problem: Multi-byte chars do not work properly with "%.*S" in printf(). Solution: Use mb_ptr2cells(). Daniel Hahler, closes vim/vim#4989) https://github.com/vim/vim/commit/ce0fac28977af31f1dec411d3535b4de2c3169b3 --- src/nvim/strings.c | 14 ++++++++++---- src/nvim/testdir/test_expr.vim | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 3ba9354c67..2f5491fda5 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -953,11 +953,17 @@ int vim_vsnprintf_typval( - mb_string2cells((char_u *)str_arg)); } if (precision) { - const char *p1 = str_arg; - for (size_t i = 0; i < precision && *p1; i++) { - p1 += mb_ptr2len((const char_u *)p1); + char_u *p1; + size_t i = 0; + + for (p1 = (char_u *)str_arg; *p1; + p1 += mb_ptr2len(p1)) { + i += (size_t)utf_ptr2cells(p1); + if (i > precision) { + break; + } } - str_arg_l = precision = (size_t)(p1 - str_arg); + str_arg_l = precision = (size_t)(p1 - (char_u *)str_arg); } } break; diff --git a/src/nvim/testdir/test_expr.vim b/src/nvim/testdir/test_expr.vim index 4f99625e73..dd546dbf71 100644 --- a/src/nvim/testdir/test_expr.vim +++ b/src/nvim/testdir/test_expr.vim @@ -279,6 +279,9 @@ function Test_printf_misc() call assert_equal('abc ', printf('%-4s', 'abc')) call assert_equal('abc ', printf('%-4S', 'abc')) + call assert_equal('🐍', printf('%.2S', '🐍🐍')) + call assert_equal('', printf('%.1S', '🐍🐍')) + call assert_equal('1%', printf('%d%%', 1)) endfunc From 3bddf050230635febc16aabe0ba4f73abeed6663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Thu, 6 Jun 2019 10:34:01 +0200 Subject: [PATCH 0061/1293] tree-sitter: vendor tree-sitter runtime tree-sitter/tree-sitter commit 7685b7861ca475664b6ef57e14d1da9acf741275 Included files are: lib/include/tree-sitter/*.h lib/src/*.[ch] LICENSE --- codecov.yml | 3 + src/tree_sitter/LICENSE | 21 + src/tree_sitter/alloc.h | 81 ++ src/tree_sitter/api.h | 660 +++++++++ src/tree_sitter/array.h | 142 ++ src/tree_sitter/atomic.h | 42 + src/tree_sitter/clock.h | 141 ++ src/tree_sitter/error_costs.h | 11 + src/tree_sitter/get_changed_ranges.c | 482 +++++++ src/tree_sitter/get_changed_ranges.h | 36 + src/tree_sitter/language.c | 107 ++ src/tree_sitter/language.h | 138 ++ src/tree_sitter/length.h | 44 + src/tree_sitter/lexer.c | 322 +++++ src/tree_sitter/lexer.h | 48 + src/tree_sitter/lib.c | 20 + src/tree_sitter/node.c | 673 +++++++++ src/tree_sitter/parser.c | 1887 ++++++++++++++++++++++++++ src/tree_sitter/parser.h | 220 +++ src/tree_sitter/point.h | 53 + src/tree_sitter/reduce_action.h | 34 + src/tree_sitter/reusable_node.h | 88 ++ src/tree_sitter/stack.c | 846 ++++++++++++ src/tree_sitter/stack.h | 135 ++ src/tree_sitter/subtree.c | 996 ++++++++++++++ src/tree_sitter/subtree.h | 281 ++++ src/tree_sitter/tree.c | 149 ++ src/tree_sitter/tree.h | 34 + src/tree_sitter/tree_cursor.c | 302 +++++ src/tree_sitter/tree_cursor.h | 20 + src/tree_sitter/utf16.c | 33 + src/tree_sitter/utf16.h | 21 + 32 files changed, 8070 insertions(+) create mode 100644 src/tree_sitter/LICENSE create mode 100644 src/tree_sitter/alloc.h create mode 100644 src/tree_sitter/api.h create mode 100644 src/tree_sitter/array.h create mode 100644 src/tree_sitter/atomic.h create mode 100644 src/tree_sitter/clock.h create mode 100644 src/tree_sitter/error_costs.h create mode 100644 src/tree_sitter/get_changed_ranges.c create mode 100644 src/tree_sitter/get_changed_ranges.h create mode 100644 src/tree_sitter/language.c create mode 100644 src/tree_sitter/language.h create mode 100644 src/tree_sitter/length.h create mode 100644 src/tree_sitter/lexer.c create mode 100644 src/tree_sitter/lexer.h create mode 100644 src/tree_sitter/lib.c create mode 100644 src/tree_sitter/node.c create mode 100644 src/tree_sitter/parser.c create mode 100644 src/tree_sitter/parser.h create mode 100644 src/tree_sitter/point.h create mode 100644 src/tree_sitter/reduce_action.h create mode 100644 src/tree_sitter/reusable_node.h create mode 100644 src/tree_sitter/stack.c create mode 100644 src/tree_sitter/stack.h create mode 100644 src/tree_sitter/subtree.c create mode 100644 src/tree_sitter/subtree.h create mode 100644 src/tree_sitter/tree.c create mode 100644 src/tree_sitter/tree.h create mode 100644 src/tree_sitter/tree_cursor.c create mode 100644 src/tree_sitter/tree_cursor.h create mode 100644 src/tree_sitter/utf16.c create mode 100644 src/tree_sitter/utf16.h diff --git a/codecov.yml b/codecov.yml index a83fd916ee..0f867db668 100644 --- a/codecov.yml +++ b/codecov.yml @@ -25,3 +25,6 @@ coverage: changes: no comment: off + +ignore: + - "src/tree_sitter" diff --git a/src/tree_sitter/LICENSE b/src/tree_sitter/LICENSE new file mode 100644 index 0000000000..971b81f9a8 --- /dev/null +++ b/src/tree_sitter/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2018 Max Brunsfeld + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/tree_sitter/alloc.h b/src/tree_sitter/alloc.h new file mode 100644 index 0000000000..c8fe6c6e6d --- /dev/null +++ b/src/tree_sitter/alloc.h @@ -0,0 +1,81 @@ +#ifndef TREE_SITTER_ALLOC_H_ +#define TREE_SITTER_ALLOC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#if defined(TREE_SITTER_TEST) + +void *ts_record_malloc(size_t); +void *ts_record_calloc(size_t, size_t); +void *ts_record_realloc(void *, size_t); +void ts_record_free(void *); +bool ts_toggle_allocation_recording(bool); + +static inline void *ts_malloc(size_t size) { + return ts_record_malloc(size); +} + +static inline void *ts_calloc(size_t count, size_t size) { + return ts_record_calloc(count, size); +} + +static inline void *ts_realloc(void *buffer, size_t size) { + return ts_record_realloc(buffer, size); +} + +static inline void ts_free(void *buffer) { + ts_record_free(buffer); +} + +#else + +#include + +static inline bool ts_toggle_allocation_recording(bool value) { + return false; +} + +static inline void *ts_malloc(size_t size) { + void *result = malloc(size); + if (size > 0 && !result) { + fprintf(stderr, "tree-sitter failed to allocate %lu bytes", size); + exit(1); + } + return result; +} + +static inline void *ts_calloc(size_t count, size_t size) { + void *result = calloc(count, size); + if (count > 0 && !result) { + fprintf(stderr, "tree-sitter failed to allocate %lu bytes", count * size); + exit(1); + } + return result; +} + +static inline void *ts_realloc(void *buffer, size_t size) { + void *result = realloc(buffer, size); + if (size > 0 && !result) { + fprintf(stderr, "tree-sitter failed to reallocate %lu bytes", size); + exit(1); + } + return result; +} + +static inline void ts_free(void *buffer) { + free(buffer); +} + +#endif + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_ALLOC_H_ diff --git a/src/tree_sitter/api.h b/src/tree_sitter/api.h new file mode 100644 index 0000000000..d39d0521ee --- /dev/null +++ b/src/tree_sitter/api.h @@ -0,0 +1,660 @@ +#ifndef TREE_SITTER_API_H_ +#define TREE_SITTER_API_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include + +/****************************/ +/* Section - ABI Versioning */ +/****************************/ + +#define TREE_SITTER_LANGUAGE_VERSION 11 +#define TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION 9 + +/*******************/ +/* Section - Types */ +/*******************/ + +typedef uint16_t TSSymbol; +typedef uint16_t TSFieldId; +typedef struct TSLanguage TSLanguage; +typedef struct TSParser TSParser; +typedef struct TSTree TSTree; + +typedef enum { + TSInputEncodingUTF8, + TSInputEncodingUTF16, +} TSInputEncoding; + +typedef enum { + TSSymbolTypeRegular, + TSSymbolTypeAnonymous, + TSSymbolTypeAuxiliary, +} TSSymbolType; + +typedef struct { + uint32_t row; + uint32_t column; +} TSPoint; + +typedef struct { + TSPoint start_point; + TSPoint end_point; + uint32_t start_byte; + uint32_t end_byte; +} TSRange; + +typedef struct { + void *payload; + const char *(*read)(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read); + TSInputEncoding encoding; +} TSInput; + +typedef enum { + TSLogTypeParse, + TSLogTypeLex, +} TSLogType; + +typedef struct { + void *payload; + void (*log)(void *payload, TSLogType, const char *); +} TSLogger; + +typedef struct { + uint32_t start_byte; + uint32_t old_end_byte; + uint32_t new_end_byte; + TSPoint start_point; + TSPoint old_end_point; + TSPoint new_end_point; +} TSInputEdit; + +typedef struct { + uint32_t context[4]; + const void *id; + const TSTree *tree; +} TSNode; + +typedef struct { + const void *tree; + const void *id; + uint32_t context[2]; +} TSTreeCursor; + +/********************/ +/* Section - Parser */ +/********************/ + +/** + * Create a new parser. + */ +TSParser *ts_parser_new(void); + +/** + * Delete the parser, freeing all of the memory that it used. + */ +void ts_parser_delete(TSParser *parser); + +/** + * Set the language that the parser should use for parsing. + * + * Returns a boolean indicating whether or not the language was successfully + * assigned. True means assignment succeeded. False means there was a version + * mismatch: the language was generated with an incompatible version of the + * Tree-sitter CLI. Check the language's version using `ts_language_version` + * and compare it to this library's `TREE_SITTER_LANGUAGE_VERSION` and + * `TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION` constants. + */ +bool ts_parser_set_language(TSParser *self, const TSLanguage *language); + +/** + * Get the parser's current language. + */ +const TSLanguage *ts_parser_language(const TSParser *self); + +/** + * Set the spans of text that the parser should include when parsing. + * + * By default, the parser will always include entire documents. This function + * allows you to parse only a *portion* of a document but still return a syntax + * tree whose ranges match up with the document as a whole. You can also pass + * multiple disjoint ranges. + * + * The second and third parameters specify the location and length of an array + * of ranges. The parser does *not* take ownership of these ranges; it copies + * the data, so it doesn't matter how these ranges are allocated. + */ +void ts_parser_set_included_ranges( + TSParser *self, + const TSRange *ranges, + uint32_t length +); + +/** + * Get the ranges of text that the parser will include when parsing. + * + * The returned pointer is owned by the parser. The caller should not free it + * or write to it. The length of the array will be written to the given + * `length` pointer. + */ +const TSRange *ts_parser_included_ranges( + const TSParser *self, + uint32_t *length +); + +/** + * Use the parser to parse some source code and create a syntax tree. + * + * If you are parsing this document for the first time, pass `NULL` for the + * `old_tree` parameter. Otherwise, if you have already parsed an earlier + * version of this document and the document has since been edited, pass the + * previous syntax tree so that the unchanged parts of it can be reused. + * This will save time and memory. For this to work correctly, you must have + * already edited the old syntax tree using the `ts_tree_edit` function in a + * way that exactly matches the source code changes. + * + * The `TSInput` parameter lets you specify how to read the text. It has the + * following three fields: + * 1. `read`: A function to retrieve a chunk of text at a given byte offset + * and (row, column) position. The function should return a pointer to the + * text and write its length to the the `bytes_read` pointer. The parser + * does not take ownership of this buffer; it just borrows it until it has + * finished reading it. The function should write a zero value to the + * `bytes_read` pointer to indicate the end of the document. + * 2. `payload`: An arbitrary pointer that will be passed to each invocation + * of the `read` function. + * 3. `encoding`: An indication of how the text is encoded. Either + * `TSInputEncodingUTF8` or `TSInputEncodingUTF16`. + * + * This function returns a syntax tree on success, and `NULL` on failure. There + * are three possible reasons for failure: + * 1. The parser does not have a language assigned. Check for this using the + `ts_parser_language` function. + * 2. Parsing was cancelled due to a timeout that was set by an earlier call to + * the `ts_parser_set_timeout_micros` function. You can resume parsing from + * where the parser left out by calling `ts_parser_parse` again with the + * same arguments. Or you can start parsing from scratch by first calling + * `ts_parser_reset`. + * 3. Parsing was cancelled using a cancellation flag that was set by an + * earlier call to `ts_parser_set_cancellation_flag`. You can resume parsing + * from where the parser left out by calling `ts_parser_parse` again with + * the same arguments. + */ +TSTree *ts_parser_parse( + TSParser *self, + const TSTree *old_tree, + TSInput input +); + +/** + * Use the parser to parse some source code stored in one contiguous buffer. + * The first two parameters are the same as in the `ts_parser_parse` function + * above. The second two parameters indicate the location of the buffer and its + * length in bytes. + */ +TSTree *ts_parser_parse_string( + TSParser *self, + const TSTree *old_tree, + const char *string, + uint32_t length +); + +/** + * Use the parser to parse some source code stored in one contiguous buffer with + * a given encoding. The first four parameters work the same as in the + * `ts_parser_parse_string` method above. The final parameter indicates whether + * the text is encoded as UTF8 or UTF16. + */ +TSTree *ts_parser_parse_string_encoding( + TSParser *self, + const TSTree *old_tree, + const char *string, + uint32_t length, + TSInputEncoding encoding +); + +/** + * Instruct the parser to start the next parse from the beginning. + * + * If the parser previously failed because of a timeout or a cancellation, then + * by default, it will resume where it left off on the next call to + * `ts_parser_parse` or other parsing functions. If you don't want to resume, + * and instead intend to use this parser to parse some other document, you must + * call this `ts_parser_reset` first. + */ +void ts_parser_reset(TSParser *self); + +/** + * Set the maximum duration in microseconds that parsing should be allowed to + * take before halting. If parsing takes longer than this, it will halt early, + * returning NULL. See `ts_parser_parse` for more information. + */ +void ts_parser_set_timeout_micros(TSParser *self, uint64_t timeout); + +/** + * Get the duration in microseconds that parsing is allowed to take. + */ +uint64_t ts_parser_timeout_micros(const TSParser *self); + +/** + * Set the parser's current cancellation flag pointer. If a non-null pointer is + * assigned, then the parser will periodically read from this pointer during + * parsing. If it reads a non-zero value, it will halt early, returning NULL. + * See `ts_parser_parse` for more information. + */ +void ts_parser_set_cancellation_flag(TSParser *self, const size_t *flag); + +/** + * Get the parser's current cancellation flag pointer. + */ +const size_t *ts_parser_cancellation_flag(const TSParser *self); + +/** + * Set the logger that a parser should use during parsing. + * + * The parser does not take ownership over the logger payload. If a logger was + * previously assigned, the caller is responsible for releasing any memory + * owned by the previous logger. + */ +void ts_parser_set_logger(TSParser *self, TSLogger logger); + +/** + * Get the parser's current logger. + */ +TSLogger ts_parser_logger(const TSParser *self); + +/** + * Set the file descriptor to which the parser should write debugging graphs + * during parsing. The graphs are formatted in the DOT language. You may want + * to pipe these graphs directly to a `dot(1)` process in order to generate + * SVG output. You can turn off this logging by passing a negative number. + */ +void ts_parser_print_dot_graphs(TSParser *self, int file); + +/** + * Set whether or not the parser should halt immediately upon detecting an + * error. This will generally result in a syntax tree with an error at the + * root, and one or more partial syntax trees within the error. This behavior + * may not be supported long-term. + */ +void ts_parser_halt_on_error(TSParser *self, bool halt); + +/******************/ +/* Section - Tree */ +/******************/ + +/** + * Create a shallow copy of the syntax tree. This is very fast. + * + * You need to copy a syntax tree in order to use it on more than one thread at + * a time, as syntax trees are not thread safe. + */ +TSTree *ts_tree_copy(const TSTree *self); + +/** + * Delete the syntax tree, freeing all of the memory that it used. + */ +void ts_tree_delete(TSTree *self); + +/** + * Get the root node of the syntax tree. + */ +TSNode ts_tree_root_node(const TSTree *self); + +/** + * Get the language that was used to parse the syntax tree. + */ +const TSLanguage *ts_tree_language(const TSTree *); + +/** + * Edit the syntax tree to keep it in sync with source code that has been + * edited. + * + * You must describe the edit both in terms of byte offsets and in terms of + * (row, column) coordinates. + */ +void ts_tree_edit(TSTree *self, const TSInputEdit *edit); + +/** + * Compare a new syntax tree to a previous syntax tree representing the same + * document, returning an array of ranges whose syntactic structure has changed. + * + * For this to work correctly, the old syntax tree must have been edited such + * that its ranges match up to the new tree. Generally, you'll want to call + * this function right after calling one of the `ts_parser_parse` functions, + * passing in the new tree that was returned from `ts_parser_parse` and the old + * tree that was passed as a parameter. + * + * The returned array is allocated using `malloc` and the caller is responsible + * for freeing it using `free`. The length of the array will be written to the + * given `length` pointer. + */ +TSRange *ts_tree_get_changed_ranges( + const TSTree *self, + const TSTree *old_tree, + uint32_t *length +); + +/** + * Write a DOT graph describing the syntax tree to the given file. + */ +void ts_tree_print_dot_graph(const TSTree *, FILE *); + +/******************/ +/* Section - Node */ +/******************/ + +/** + * Get the node's type as a null-terminated string. + */ +const char *ts_node_type(TSNode); + +/** + * Get the node's type as a numerical id. + */ +TSSymbol ts_node_symbol(TSNode); + +/** + * Get the node's start byte. + */ +uint32_t ts_node_start_byte(TSNode); + +/** + * Get the node's start position in terms of rows and columns. + */ +TSPoint ts_node_start_point(TSNode); + +/** + * Get the node's end byte. + */ +uint32_t ts_node_end_byte(TSNode); + +/** + * Get the node's end position in terms of rows and columns. + */ +TSPoint ts_node_end_point(TSNode); + +/** + * Get an S-expression representing the node as a string. + * + * This string is allocated with `malloc` and the caller is responsible for + * freeing it using `free`. + */ +char *ts_node_string(TSNode); + +/** + * Check if the node is null. Functions like `ts_node_child` and + * `ts_node_next_sibling` will return a null node to indicate that no such node + * was found. + */ +bool ts_node_is_null(TSNode); + +/** + * Check if the node is *named*. Named nodes correspond to named rules in the + * grammar, whereas *anonymous* nodes correspond to string literals in the + * grammar. + */ +bool ts_node_is_named(TSNode); + +/** + * Check if the node is *missing*. Missing nodes are inserted by the parser in + * order to recover from certain kinds of syntax errors. + */ +bool ts_node_is_missing(TSNode); + +/** + * Check if the node is *missing*. Missing nodes are inserted by the parser in + * order to recover from certain kinds of syntax errors. + */ +bool ts_node_is_extra(TSNode); + +/** + * Check if a syntax node has been edited. + */ +bool ts_node_has_changes(TSNode); + +/** + * Check if the node is a syntax error or contains any syntax errors. + */ +bool ts_node_has_error(TSNode); + +/** + * Get the node's immediate parent. + */ +TSNode ts_node_parent(TSNode); + +/** + * Get the node's child at the given index, where zero represents the first + * child. + */ +TSNode ts_node_child(TSNode, uint32_t); + +/** + * Get the node's number of children. + */ +uint32_t ts_node_child_count(TSNode); + +/** + * Get the node's *named* child at the given index. + * + * See also `ts_node_is_named`. + */ +TSNode ts_node_named_child(TSNode, uint32_t); + +/** + * Get the node's number of *named* children. + * + * See also `ts_node_is_named`. + */ +uint32_t ts_node_named_child_count(TSNode); + +/** + * Get the node's child with the given field name. + */ +TSNode ts_node_child_by_field_name( + TSNode self, + const char *field_name, + uint32_t field_name_length +); + +/** + * Get the node's child with the given numerical field id. + * + * You can convert a field name to an id using the + * `ts_language_field_id_for_name` function. + */ +TSNode ts_node_child_by_field_id(TSNode, TSFieldId); + +/** + * Get the node's next / previous sibling. + */ +TSNode ts_node_next_sibling(TSNode); +TSNode ts_node_prev_sibling(TSNode); + +/** + * Get the node's next / previous *named* sibling. + */ +TSNode ts_node_next_named_sibling(TSNode); +TSNode ts_node_prev_named_sibling(TSNode); + +/** + * Get the node's first child that extends beyond the given byte offset. + */ +TSNode ts_node_first_child_for_byte(TSNode, uint32_t); + +/** + * Get the node's first named child that extends beyond the given byte offset. + */ +TSNode ts_node_first_named_child_for_byte(TSNode, uint32_t); + +/** + * Get the smallest node within this node that spans the given range of bytes + * or (row, column) positions. + */ +TSNode ts_node_descendant_for_byte_range(TSNode, uint32_t, uint32_t); +TSNode ts_node_descendant_for_point_range(TSNode, TSPoint, TSPoint); + +/** + * Get the smallest named node within this node that spans the given range of + * bytes or (row, column) positions. + */ +TSNode ts_node_named_descendant_for_byte_range(TSNode, uint32_t, uint32_t); +TSNode ts_node_named_descendant_for_point_range(TSNode, TSPoint, TSPoint); + +/** + * Edit the node to keep it in-sync with source code that has been edited. + * + * This function is only rarely needed. When you edit a syntax tree with the + * `ts_tree_edit` function, all of the nodes that you retrieve from the tree + * afterward will already reflect the edit. You only need to use `ts_node_edit` + * when you have a `TSNode` instance that you want to keep and continue to use + * after an edit. + */ +void ts_node_edit(TSNode *, const TSInputEdit *); + +/** + * Check if two nodes are identical. + */ +bool ts_node_eq(TSNode, TSNode); + +/************************/ +/* Section - TreeCursor */ +/************************/ + +/** + * Create a new tree cursor starting from the given node. + * + * A tree cursor allows you to walk a syntax tree more efficiently than is + * possible using the `TSNode` functions. It is a mutable object that is always + * on a certain syntax node, and can be moved imperatively to different nodes. + */ +TSTreeCursor ts_tree_cursor_new(TSNode); + +/** + * Delete a tree cursor, freeing all of the memory that it used. + */ +void ts_tree_cursor_delete(TSTreeCursor *); + +/** + * Re-initialize a tree cursor to start at a different ndoe. + */ +void ts_tree_cursor_reset(TSTreeCursor *, TSNode); + +/** + * Get the tree cursor's current node. + */ +TSNode ts_tree_cursor_current_node(const TSTreeCursor *); + +/** + * Get the field name of the tree cursor's current node. + * + * This returns `NULL` if the current node doesn't have a field. + * See also `ts_node_child_by_field_name`. + */ +const char *ts_tree_cursor_current_field_name(const TSTreeCursor *); + +/** + * Get the field name of the tree cursor's current node. + * + * This returns zero if the current node doesn't have a field. + * See also `ts_node_child_by_field_id`, `ts_language_field_id_for_name`. + */ +TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *); + +/** + * Move the cursor to the parent of its current node. + * + * This returns `true` if the cursor successfully moved, and returns `false` + * if there was no parent node (the cursor was already on the root node). + */ +bool ts_tree_cursor_goto_parent(TSTreeCursor *); + +/** + * Move the cursor to the next sibling of its current node. + * + * This returns `true` if the cursor successfully moved, and returns `false` + * if there was no next sibling node. + */ +bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *); + +/** + * Move the cursor to the first schild of its current node. + * + * This returns `true` if the cursor successfully moved, and returns `false` + * if there were no children. + */ +bool ts_tree_cursor_goto_first_child(TSTreeCursor *); + +/** + * Move the cursor to the first schild of its current node that extends beyond + * the given byte offset. + * + * This returns the index of the child node if one was found, and returns -1 + * if no such child was found. + */ +int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *, uint32_t); + +TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *); + +/**********************/ +/* Section - Language */ +/**********************/ + +/** + * Get the number of distinct node types in the language. + */ +uint32_t ts_language_symbol_count(const TSLanguage *); + +/** + * Get a node type string for the given numerical id. + */ +const char *ts_language_symbol_name(const TSLanguage *, TSSymbol); + +/** + * Get the numerical id for the given node type string. + */ +TSSymbol ts_language_symbol_for_name(const TSLanguage *, const char *); + +/** + * Get the number of distinct field names in the language. + */ +uint32_t ts_language_field_count(const TSLanguage *); + +/** + * Get the field name string for the given numerical id. + */ +const char *ts_language_field_name_for_id(const TSLanguage *, TSFieldId); + +/** + * Get the numerical id for the given field name string. + */ +TSFieldId ts_language_field_id_for_name(const TSLanguage *, const char *, uint32_t); + +/** + * Check whether the given node type id belongs to named nodes, anonymous nodes, + * or a hidden nodes. + * + * See also `ts_node_is_named`. Hidden nodes are never returned from the API. + */ +TSSymbolType ts_language_symbol_type(const TSLanguage *, TSSymbol); + +/** + * Get the ABI version number for this language. This version number is used + * to ensure that languages were generated by a compatible version of + * Tree-sitter. + * + * See also `ts_parser_set_language`. + */ +uint32_t ts_language_version(const TSLanguage *); + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_API_H_ diff --git a/src/tree_sitter/array.h b/src/tree_sitter/array.h new file mode 100644 index 0000000000..bc77e687bf --- /dev/null +++ b/src/tree_sitter/array.h @@ -0,0 +1,142 @@ +#ifndef TREE_SITTER_ARRAY_H_ +#define TREE_SITTER_ARRAY_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include +#include +#include "./alloc.h" + +#define Array(T) \ + struct { \ + T *contents; \ + uint32_t size; \ + uint32_t capacity; \ + } + +#define array_init(self) \ + ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) + +#define array_new() \ + { NULL, 0, 0 } + +#define array_get(self, index) \ + (assert((uint32_t)index < (self)->size), &(self)->contents[index]) + +#define array_front(self) array_get(self, 0) + +#define array_back(self) array_get(self, (self)->size - 1) + +#define array_clear(self) ((self)->size = 0) + +#define array_reserve(self, new_capacity) \ + array__reserve((VoidArray *)(self), array__elem_size(self), new_capacity) + +#define array_erase(self, index) \ + array__erase((VoidArray *)(self), array__elem_size(self), index) + +#define array_delete(self) array__delete((VoidArray *)self) + +#define array_push(self, element) \ + (array__grow((VoidArray *)(self), 1, array__elem_size(self)), \ + (self)->contents[(self)->size++] = (element)) + +#define array_grow_by(self, count) \ + (array__grow((VoidArray *)(self), count, array__elem_size(self)), \ + memset((self)->contents + (self)->size, 0, (count) * array__elem_size(self)), \ + (self)->size += (count)) + +#define array_push_all(self, other) \ + array_splice((self), (self)->size, 0, (other)->size, (other)->contents) + +#define array_splice(self, index, old_count, new_count, new_contents) \ + array__splice((VoidArray *)(self), array__elem_size(self), index, old_count, \ + new_count, new_contents) + +#define array_insert(self, index, element) \ + array__splice((VoidArray *)(self), array__elem_size(self), index, 0, 1, &element) + +#define array_pop(self) ((self)->contents[--(self)->size]) + +#define array_assign(self, other) \ + array__assign((VoidArray *)(self), (const VoidArray *)(other), array__elem_size(self)) + +// Private + +typedef Array(void) VoidArray; + +#define array__elem_size(self) sizeof(*(self)->contents) + +static inline void array__delete(VoidArray *self) { + ts_free(self->contents); + self->contents = NULL; + self->size = 0; + self->capacity = 0; +} + +static inline void array__erase(VoidArray *self, size_t element_size, + uint32_t index) { + assert(index < self->size); + char *contents = (char *)self->contents; + memmove(contents + index * element_size, contents + (index + 1) * element_size, + (self->size - index - 1) * element_size); + self->size--; +} + +static inline void array__reserve(VoidArray *self, size_t element_size, uint32_t new_capacity) { + if (new_capacity > self->capacity) { + if (self->contents) { + self->contents = ts_realloc(self->contents, new_capacity * element_size); + } else { + self->contents = ts_calloc(new_capacity, element_size); + } + self->capacity = new_capacity; + } +} + +static inline void array__assign(VoidArray *self, const VoidArray *other, size_t element_size) { + array__reserve(self, element_size, other->size); + self->size = other->size; + memcpy(self->contents, other->contents, self->size * element_size); +} + +static inline void array__grow(VoidArray *self, size_t count, size_t element_size) { + size_t new_size = self->size + count; + if (new_size > self->capacity) { + size_t new_capacity = self->capacity * 2; + if (new_capacity < 8) new_capacity = 8; + if (new_capacity < new_size) new_capacity = new_size; + array__reserve(self, element_size, new_capacity); + } +} + +static inline void array__splice(VoidArray *self, size_t element_size, + uint32_t index, uint32_t old_count, + uint32_t new_count, const void *elements) { + uint32_t new_size = self->size + new_count - old_count; + uint32_t old_end = index + old_count; + uint32_t new_end = index + new_count; + assert(old_end <= self->size); + + array__reserve(self, element_size, new_size); + + char *contents = (char *)self->contents; + if (self->size > old_end) + memmove(contents + new_end * element_size, contents + old_end * element_size, + (self->size - old_end) * element_size); + if (new_count > 0) + memcpy((contents + index * element_size), elements, + new_count * element_size); + self->size += new_count - old_count; +} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_ARRAY_H_ diff --git a/src/tree_sitter/atomic.h b/src/tree_sitter/atomic.h new file mode 100644 index 0000000000..301ee36700 --- /dev/null +++ b/src/tree_sitter/atomic.h @@ -0,0 +1,42 @@ +#ifndef TREE_SITTER_ATOMIC_H_ +#define TREE_SITTER_ATOMIC_H_ + +#include + +#ifdef _WIN32 + +#include + +static inline size_t atomic_load(const volatile size_t *p) { + return *p; +} + +static inline uint32_t atomic_inc(volatile uint32_t *p) { + return InterlockedIncrement(p); +} + +static inline uint32_t atomic_dec(volatile uint32_t *p) { + return InterlockedDecrement(p); +} + +#else + +static inline size_t atomic_load(const volatile size_t *p) { +#ifdef __ATOMIC_RELAXED + return __atomic_load_n(p, __ATOMIC_RELAXED); +#else + return __sync_fetch_and_add((volatile size_t *)p, 0); +#endif +} + +static inline uint32_t atomic_inc(volatile uint32_t *p) { + return __sync_add_and_fetch(p, 1u); +} + +static inline uint32_t atomic_dec(volatile uint32_t *p) { + return __sync_sub_and_fetch(p, 1u); +} + +#endif + +#endif // TREE_SITTER_ATOMIC_H_ diff --git a/src/tree_sitter/clock.h b/src/tree_sitter/clock.h new file mode 100644 index 0000000000..94545f3566 --- /dev/null +++ b/src/tree_sitter/clock.h @@ -0,0 +1,141 @@ +#ifndef TREE_SITTER_CLOCK_H_ +#define TREE_SITTER_CLOCK_H_ + +#include + +typedef uint64_t TSDuration; + +#ifdef _WIN32 + +// Windows: +// * Represent a time as a performance counter value. +// * Represent a duration as a number of performance counter ticks. + +#include +typedef uint64_t TSClock; + +static inline TSDuration duration_from_micros(uint64_t micros) { + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + return micros * (uint64_t)frequency.QuadPart / 1000000; +} + +static inline uint64_t duration_to_micros(TSDuration self) { + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + return self * 1000000 / (uint64_t)frequency.QuadPart; +} + +static inline TSClock clock_null(void) { + return 0; +} + +static inline TSClock clock_now(void) { + LARGE_INTEGER result; + QueryPerformanceCounter(&result); + return (uint64_t)result.QuadPart; +} + +static inline TSClock clock_after(TSClock base, TSDuration duration) { + return base + duration; +} + +static inline bool clock_is_null(TSClock self) { + return !self; +} + +static inline bool clock_is_gt(TSClock self, TSClock other) { + return self > other; +} + +#elif defined(CLOCK_MONOTONIC) && !defined(__APPLE__) + +// POSIX with monotonic clock support (Linux) +// * Represent a time as a monotonic (seconds, nanoseconds) pair. +// * Represent a duration as a number of microseconds. +// +// On these platforms, parse timeouts will correspond accurately to +// real time, regardless of what other processes are running. + +#include +typedef struct timespec TSClock; + +static inline TSDuration duration_from_micros(uint64_t micros) { + return micros; +} + +static inline uint64_t duration_to_micros(TSDuration self) { + return self; +} + +static inline TSClock clock_now(void) { + TSClock result; + clock_gettime(CLOCK_MONOTONIC, &result); + return result; +} + +static inline TSClock clock_null(void) { + return (TSClock) {0, 0}; +} + +static inline TSClock clock_after(TSClock base, TSDuration duration) { + TSClock result = base; + result.tv_sec += duration / 1000000; + result.tv_nsec += (duration % 1000000) * 1000; + return result; +} + +static inline bool clock_is_null(TSClock self) { + return !self.tv_sec; +} + +static inline bool clock_is_gt(TSClock self, TSClock other) { + if (self.tv_sec > other.tv_sec) return true; + if (self.tv_sec < other.tv_sec) return false; + return self.tv_nsec > other.tv_nsec; +} + +#else + +// macOS or POSIX without monotonic clock support +// * Represent a time as a process clock value. +// * Represent a duration as a number of process clock ticks. +// +// On these platforms, parse timeouts may be affected by other processes, +// which is not ideal, but is better than using a non-monotonic time API +// like `gettimeofday`. + +#include +typedef uint64_t TSClock; + +static inline TSDuration duration_from_micros(uint64_t micros) { + return micros * (uint64_t)CLOCKS_PER_SEC / 1000000; +} + +static inline uint64_t duration_to_micros(TSDuration self) { + return self * 1000000 / (uint64_t)CLOCKS_PER_SEC; +} + +static inline TSClock clock_null(void) { + return 0; +} + +static inline TSClock clock_now(void) { + return (uint64_t)clock(); +} + +static inline TSClock clock_after(TSClock base, TSDuration duration) { + return base + duration; +} + +static inline bool clock_is_null(TSClock self) { + return !self; +} + +static inline bool clock_is_gt(TSClock self, TSClock other) { + return self > other; +} + +#endif + +#endif // TREE_SITTER_CLOCK_H_ diff --git a/src/tree_sitter/error_costs.h b/src/tree_sitter/error_costs.h new file mode 100644 index 0000000000..32d3666a66 --- /dev/null +++ b/src/tree_sitter/error_costs.h @@ -0,0 +1,11 @@ +#ifndef TREE_SITTER_ERROR_COSTS_H_ +#define TREE_SITTER_ERROR_COSTS_H_ + +#define ERROR_STATE 0 +#define ERROR_COST_PER_RECOVERY 500 +#define ERROR_COST_PER_MISSING_TREE 110 +#define ERROR_COST_PER_SKIPPED_TREE 100 +#define ERROR_COST_PER_SKIPPED_LINE 30 +#define ERROR_COST_PER_SKIPPED_CHAR 1 + +#endif diff --git a/src/tree_sitter/get_changed_ranges.c b/src/tree_sitter/get_changed_ranges.c new file mode 100644 index 0000000000..5bd1d814bd --- /dev/null +++ b/src/tree_sitter/get_changed_ranges.c @@ -0,0 +1,482 @@ +#include "./get_changed_ranges.h" +#include "./subtree.h" +#include "./language.h" +#include "./error_costs.h" +#include "./tree_cursor.h" +#include + +// #define DEBUG_GET_CHANGED_RANGES + +static void ts_range_array_add(TSRangeArray *self, Length start, Length end) { + if (self->size > 0) { + TSRange *last_range = array_back(self); + if (start.bytes <= last_range->end_byte) { + last_range->end_byte = end.bytes; + last_range->end_point = end.extent; + return; + } + } + + if (start.bytes < end.bytes) { + TSRange range = { start.extent, end.extent, start.bytes, end.bytes }; + array_push(self, range); + } +} + +bool ts_range_array_intersects(const TSRangeArray *self, unsigned start_index, + uint32_t start_byte, uint32_t end_byte) { + for (unsigned i = start_index; i < self->size; i++) { + TSRange *range = &self->contents[i]; + if (range->end_byte > start_byte) { + if (range->start_byte >= end_byte) break; + return true; + } + } + return false; +} + +void ts_range_array_get_changed_ranges( + const TSRange *old_ranges, unsigned old_range_count, + const TSRange *new_ranges, unsigned new_range_count, + TSRangeArray *differences +) { + unsigned new_index = 0; + unsigned old_index = 0; + Length current_position = length_zero(); + bool in_old_range = false; + bool in_new_range = false; + + while (old_index < old_range_count || new_index < new_range_count) { + const TSRange *old_range = &old_ranges[old_index]; + const TSRange *new_range = &new_ranges[new_index]; + + Length next_old_position; + if (in_old_range) { + next_old_position = (Length) {old_range->end_byte, old_range->end_point}; + } else if (old_index < old_range_count) { + next_old_position = (Length) {old_range->start_byte, old_range->start_point}; + } else { + next_old_position = LENGTH_MAX; + } + + Length next_new_position; + if (in_new_range) { + next_new_position = (Length) {new_range->end_byte, new_range->end_point}; + } else if (new_index < new_range_count) { + next_new_position = (Length) {new_range->start_byte, new_range->start_point}; + } else { + next_new_position = LENGTH_MAX; + } + + if (next_old_position.bytes < next_new_position.bytes) { + if (in_old_range != in_new_range) { + ts_range_array_add(differences, current_position, next_old_position); + } + if (in_old_range) old_index++; + current_position = next_old_position; + in_old_range = !in_old_range; + } else if (next_new_position.bytes < next_old_position.bytes) { + if (in_old_range != in_new_range) { + ts_range_array_add(differences, current_position, next_new_position); + } + if (in_new_range) new_index++; + current_position = next_new_position; + in_new_range = !in_new_range; + } else { + if (in_old_range != in_new_range) { + ts_range_array_add(differences, current_position, next_new_position); + } + if (in_old_range) old_index++; + if (in_new_range) new_index++; + in_old_range = !in_old_range; + in_new_range = !in_new_range; + current_position = next_new_position; + } + } +} + +typedef struct { + TreeCursor cursor; + const TSLanguage *language; + unsigned visible_depth; + bool in_padding; +} Iterator; + +static Iterator iterator_new(TreeCursor *cursor, const Subtree *tree, const TSLanguage *language) { + array_clear(&cursor->stack); + array_push(&cursor->stack, ((TreeCursorEntry){ + .subtree = tree, + .position = length_zero(), + .child_index = 0, + .structural_child_index = 0, + })); + return (Iterator) { + .cursor = *cursor, + .language = language, + .visible_depth = 1, + .in_padding = false, + }; +} + +static bool iterator_done(Iterator *self) { + return self->cursor.stack.size == 0; +} + +static Length iterator_start_position(Iterator *self) { + TreeCursorEntry entry = *array_back(&self->cursor.stack); + if (self->in_padding) { + return entry.position; + } else { + return length_add(entry.position, ts_subtree_padding(*entry.subtree)); + } +} + +static Length iterator_end_position(Iterator *self) { + TreeCursorEntry entry = *array_back(&self->cursor.stack); + Length result = length_add(entry.position, ts_subtree_padding(*entry.subtree)); + if (self->in_padding) { + return result; + } else { + return length_add(result, ts_subtree_size(*entry.subtree)); + } +} + +static bool iterator_tree_is_visible(const Iterator *self) { + TreeCursorEntry entry = *array_back(&self->cursor.stack); + if (ts_subtree_visible(*entry.subtree)) return true; + if (self->cursor.stack.size > 1) { + Subtree parent = *self->cursor.stack.contents[self->cursor.stack.size - 2].subtree; + const TSSymbol *alias_sequence = ts_language_alias_sequence( + self->language, + parent.ptr->production_id + ); + return alias_sequence && alias_sequence[entry.structural_child_index] != 0; + } + return false; +} + +static void iterator_get_visible_state(const Iterator *self, Subtree *tree, + TSSymbol *alias_symbol, uint32_t *start_byte) { + uint32_t i = self->cursor.stack.size - 1; + + if (self->in_padding) { + if (i == 0) return; + i--; + } + + for (; i + 1 > 0; i--) { + TreeCursorEntry entry = self->cursor.stack.contents[i]; + + if (i > 0) { + const Subtree *parent = self->cursor.stack.contents[i - 1].subtree; + const TSSymbol *alias_sequence = ts_language_alias_sequence( + self->language, + parent->ptr->production_id + ); + if (alias_sequence) { + *alias_symbol = alias_sequence[entry.structural_child_index]; + } + } + + if (ts_subtree_visible(*entry.subtree) || *alias_symbol) { + *tree = *entry.subtree; + *start_byte = entry.position.bytes; + break; + } + } +} + +static void iterator_ascend(Iterator *self) { + if (iterator_done(self)) return; + if (iterator_tree_is_visible(self) && !self->in_padding) self->visible_depth--; + if (array_back(&self->cursor.stack)->child_index > 0) self->in_padding = false; + self->cursor.stack.size--; +} + +static bool iterator_descend(Iterator *self, uint32_t goal_position) { + if (self->in_padding) return false; + + bool did_descend; + do { + did_descend = false; + TreeCursorEntry entry = *array_back(&self->cursor.stack); + Length position = entry.position; + uint32_t structural_child_index = 0; + for (uint32_t i = 0, n = ts_subtree_child_count(*entry.subtree); i < n; i++) { + const Subtree *child = &entry.subtree->ptr->children[i]; + Length child_left = length_add(position, ts_subtree_padding(*child)); + Length child_right = length_add(child_left, ts_subtree_size(*child)); + + if (child_right.bytes > goal_position) { + array_push(&self->cursor.stack, ((TreeCursorEntry){ + .subtree = child, + .position = position, + .child_index = i, + .structural_child_index = structural_child_index, + })); + + if (iterator_tree_is_visible(self)) { + if (child_left.bytes > goal_position) { + self->in_padding = true; + } else { + self->visible_depth++; + } + return true; + } + + did_descend = true; + break; + } + + position = child_right; + if (!ts_subtree_extra(*child)) structural_child_index++; + } + } while (did_descend); + + return false; +} + +static void iterator_advance(Iterator *self) { + if (self->in_padding) { + self->in_padding = false; + if (iterator_tree_is_visible(self)) { + self->visible_depth++; + } else { + iterator_descend(self, 0); + } + return; + } + + for (;;) { + if (iterator_tree_is_visible(self)) self->visible_depth--; + TreeCursorEntry entry = array_pop(&self->cursor.stack); + if (iterator_done(self)) return; + + const Subtree *parent = array_back(&self->cursor.stack)->subtree; + uint32_t child_index = entry.child_index + 1; + if (ts_subtree_child_count(*parent) > child_index) { + Length position = length_add(entry.position, ts_subtree_total_size(*entry.subtree)); + uint32_t structural_child_index = entry.structural_child_index; + if (!ts_subtree_extra(*entry.subtree)) structural_child_index++; + const Subtree *next_child = &parent->ptr->children[child_index]; + + array_push(&self->cursor.stack, ((TreeCursorEntry){ + .subtree = next_child, + .position = position, + .child_index = child_index, + .structural_child_index = structural_child_index, + })); + + if (iterator_tree_is_visible(self)) { + if (ts_subtree_padding(*next_child).bytes > 0) { + self->in_padding = true; + } else { + self->visible_depth++; + } + } else { + iterator_descend(self, 0); + } + break; + } + } +} + +typedef enum { + IteratorDiffers, + IteratorMayDiffer, + IteratorMatches, +} IteratorComparison; + +static IteratorComparison iterator_compare(const Iterator *old_iter, const Iterator *new_iter) { + Subtree old_tree = NULL_SUBTREE; + Subtree new_tree = NULL_SUBTREE; + uint32_t old_start = 0; + uint32_t new_start = 0; + TSSymbol old_alias_symbol = 0; + TSSymbol new_alias_symbol = 0; + iterator_get_visible_state(old_iter, &old_tree, &old_alias_symbol, &old_start); + iterator_get_visible_state(new_iter, &new_tree, &new_alias_symbol, &new_start); + + if (!old_tree.ptr && !new_tree.ptr) return IteratorMatches; + if (!old_tree.ptr || !new_tree.ptr) return IteratorDiffers; + + if ( + old_alias_symbol == new_alias_symbol && + ts_subtree_symbol(old_tree) == ts_subtree_symbol(new_tree) + ) { + if (old_start == new_start && + !ts_subtree_has_changes(old_tree) && + ts_subtree_symbol(old_tree) != ts_builtin_sym_error && + ts_subtree_size(old_tree).bytes == ts_subtree_size(new_tree).bytes && + ts_subtree_parse_state(old_tree) != TS_TREE_STATE_NONE && + ts_subtree_parse_state(new_tree) != TS_TREE_STATE_NONE && + (ts_subtree_parse_state(old_tree) == ERROR_STATE) == + (ts_subtree_parse_state(new_tree) == ERROR_STATE)) { + return IteratorMatches; + } else { + return IteratorMayDiffer; + } + } + + return IteratorDiffers; +} + +#ifdef DEBUG_GET_CHANGED_RANGES +static inline void iterator_print_state(Iterator *self) { + TreeCursorEntry entry = *array_back(&self->cursor.stack); + TSPoint start = iterator_start_position(self).extent; + TSPoint end = iterator_end_position(self).extent; + const char *name = ts_language_symbol_name(self->language, ts_subtree_symbol(*entry.subtree)); + printf( + "(%-25s %s\t depth:%u [%u, %u] - [%u, %u])", + name, self->in_padding ? "(p)" : " ", + self->visible_depth, + start.row + 1, start.column, + end.row + 1, end.column + ); +} +#endif + +unsigned ts_subtree_get_changed_ranges(const Subtree *old_tree, const Subtree *new_tree, + TreeCursor *cursor1, TreeCursor *cursor2, + const TSLanguage *language, + const TSRangeArray *included_range_differences, + TSRange **ranges) { + TSRangeArray results = array_new(); + + Iterator old_iter = iterator_new(cursor1, old_tree, language); + Iterator new_iter = iterator_new(cursor2, new_tree, language); + + unsigned included_range_difference_index = 0; + + Length position = iterator_start_position(&old_iter); + Length next_position = iterator_start_position(&new_iter); + if (position.bytes < next_position.bytes) { + ts_range_array_add(&results, position, next_position); + position = next_position; + } else if (position.bytes > next_position.bytes) { + ts_range_array_add(&results, next_position, position); + next_position = position; + } + + do { + #ifdef DEBUG_GET_CHANGED_RANGES + printf("At [%-2u, %-2u] Compare ", position.extent.row + 1, position.extent.column); + iterator_print_state(&old_iter); + printf("\tvs\t"); + iterator_print_state(&new_iter); + puts(""); + #endif + + // Compare the old and new subtrees. + IteratorComparison comparison = iterator_compare(&old_iter, &new_iter); + + // Even if the two subtrees appear to be identical, they could differ + // internally if they contain a range of text that was previously + // excluded from the parse, and is now included, or vice-versa. + if (comparison == IteratorMatches && ts_range_array_intersects( + included_range_differences, + included_range_difference_index, + position.bytes, + iterator_end_position(&old_iter).bytes + )) { + comparison = IteratorMayDiffer; + } + + bool is_changed = false; + switch (comparison) { + // If the subtrees are definitely identical, move to the end + // of both subtrees. + case IteratorMatches: + next_position = iterator_end_position(&old_iter); + break; + + // If the subtrees might differ internally, descend into both + // subtrees, finding the first child that spans the current position. + case IteratorMayDiffer: + if (iterator_descend(&old_iter, position.bytes)) { + if (!iterator_descend(&new_iter, position.bytes)) { + is_changed = true; + next_position = iterator_end_position(&old_iter); + } + } else if (iterator_descend(&new_iter, position.bytes)) { + is_changed = true; + next_position = iterator_end_position(&new_iter); + } else { + next_position = length_min( + iterator_end_position(&old_iter), + iterator_end_position(&new_iter) + ); + } + break; + + // If the subtrees are different, record a change and then move + // to the end of both subtrees. + case IteratorDiffers: + is_changed = true; + next_position = length_min( + iterator_end_position(&old_iter), + iterator_end_position(&new_iter) + ); + break; + } + + // Ensure that both iterators are caught up to the current position. + while ( + !iterator_done(&old_iter) && + iterator_end_position(&old_iter).bytes <= next_position.bytes + ) iterator_advance(&old_iter); + while ( + !iterator_done(&new_iter) && + iterator_end_position(&new_iter).bytes <= next_position.bytes + ) iterator_advance(&new_iter); + + // Ensure that both iterators are at the same depth in the tree. + while (old_iter.visible_depth > new_iter.visible_depth) { + iterator_ascend(&old_iter); + } + while (new_iter.visible_depth > old_iter.visible_depth) { + iterator_ascend(&new_iter); + } + + if (is_changed) { + #ifdef DEBUG_GET_CHANGED_RANGES + printf( + " change: [[%u, %u] - [%u, %u]]\n", + position.extent.row + 1, position.extent.column, + next_position.extent.row + 1, next_position.extent.column + ); + #endif + + ts_range_array_add(&results, position, next_position); + } + + position = next_position; + + // Keep track of the current position in the included range differences + // array in order to avoid scanning the entire array on each iteration. + while (included_range_difference_index < included_range_differences->size) { + const TSRange *range = &included_range_differences->contents[ + included_range_difference_index + ]; + if (range->end_byte <= position.bytes) { + included_range_difference_index++; + } else { + break; + } + } + } while (!iterator_done(&old_iter) && !iterator_done(&new_iter)); + + Length old_size = ts_subtree_total_size(*old_tree); + Length new_size = ts_subtree_total_size(*new_tree); + if (old_size.bytes < new_size.bytes) { + ts_range_array_add(&results, old_size, new_size); + } else if (new_size.bytes < old_size.bytes) { + ts_range_array_add(&results, new_size, old_size); + } + + *cursor1 = old_iter.cursor; + *cursor2 = new_iter.cursor; + *ranges = results.contents; + return results.size; +} diff --git a/src/tree_sitter/get_changed_ranges.h b/src/tree_sitter/get_changed_ranges.h new file mode 100644 index 0000000000..a1f1dbb430 --- /dev/null +++ b/src/tree_sitter/get_changed_ranges.h @@ -0,0 +1,36 @@ +#ifndef TREE_SITTER_GET_CHANGED_RANGES_H_ +#define TREE_SITTER_GET_CHANGED_RANGES_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./tree_cursor.h" +#include "./subtree.h" + +typedef Array(TSRange) TSRangeArray; + +void ts_range_array_get_changed_ranges( + const TSRange *old_ranges, unsigned old_range_count, + const TSRange *new_ranges, unsigned new_range_count, + TSRangeArray *differences +); + +bool ts_range_array_intersects( + const TSRangeArray *self, unsigned start_index, + uint32_t start_byte, uint32_t end_byte +); + +unsigned ts_subtree_get_changed_ranges( + const Subtree *old_tree, const Subtree *new_tree, + TreeCursor *cursor1, TreeCursor *cursor2, + const TSLanguage *language, + const TSRangeArray *included_range_differences, + TSRange **ranges +); + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_GET_CHANGED_RANGES_H_ diff --git a/src/tree_sitter/language.c b/src/tree_sitter/language.c new file mode 100644 index 0000000000..1bfb1a8d03 --- /dev/null +++ b/src/tree_sitter/language.c @@ -0,0 +1,107 @@ +#include "./language.h" +#include "./subtree.h" +#include "./error_costs.h" +#include + +void ts_language_table_entry(const TSLanguage *self, TSStateId state, + TSSymbol symbol, TableEntry *result) { + if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) { + result->action_count = 0; + result->is_reusable = false; + result->actions = NULL; + } else { + assert(symbol < self->token_count); + uint32_t action_index = ts_language_lookup(self, state, symbol); + const TSParseActionEntry *entry = &self->parse_actions[action_index]; + result->action_count = entry->count; + result->is_reusable = entry->reusable; + result->actions = (const TSParseAction *)(entry + 1); + } +} + +uint32_t ts_language_symbol_count(const TSLanguage *language) { + return language->symbol_count + language->alias_count; +} + +uint32_t ts_language_version(const TSLanguage *language) { + return language->version; +} + +TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *language, TSSymbol symbol) { + if (symbol == ts_builtin_sym_error) { + return (TSSymbolMetadata){.visible = true, .named = true}; + } else if (symbol == ts_builtin_sym_error_repeat) { + return (TSSymbolMetadata){.visible = false, .named = false}; + } else { + return language->symbol_metadata[symbol]; + } +} + +const char *ts_language_symbol_name(const TSLanguage *language, TSSymbol symbol) { + if (symbol == ts_builtin_sym_error) { + return "ERROR"; + } else if (symbol == ts_builtin_sym_error_repeat) { + return "_ERROR"; + } else { + return language->symbol_names[symbol]; + } +} + +TSSymbol ts_language_symbol_for_name(const TSLanguage *self, const char *name) { + if (!strcmp(name, "ERROR")) return ts_builtin_sym_error; + + uint32_t count = ts_language_symbol_count(self); + for (TSSymbol i = 0; i < count; i++) { + if (!strcmp(self->symbol_names[i], name)) { + return i; + } + } + return 0; +} + +TSSymbolType ts_language_symbol_type(const TSLanguage *language, TSSymbol symbol) { + TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); + if (metadata.named) { + return TSSymbolTypeRegular; + } else if (metadata.visible) { + return TSSymbolTypeAnonymous; + } else { + return TSSymbolTypeAuxiliary; + } +} + +uint32_t ts_language_field_count(const TSLanguage *self) { + if (self->version >= TREE_SITTER_LANGUAGE_VERSION_WITH_FIELDS) { + return self->field_count; + } else { + return 0; + } +} + +const char *ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id) { + uint32_t count = ts_language_field_count(self); + if (count) { + return self->field_names[id]; + } else { + return NULL; + } +} + +TSFieldId ts_language_field_id_for_name( + const TSLanguage *self, + const char *name, + uint32_t name_length +) { + uint32_t count = ts_language_field_count(self); + for (TSSymbol i = 1; i < count + 1; i++) { + switch (strncmp(name, self->field_names[i], name_length)) { + case 0: + return i; + case -1: + return 0; + default: + break; + } + } + return 0; +} diff --git a/src/tree_sitter/language.h b/src/tree_sitter/language.h new file mode 100644 index 0000000000..0741486a1b --- /dev/null +++ b/src/tree_sitter/language.h @@ -0,0 +1,138 @@ +#ifndef TREE_SITTER_LANGUAGE_H_ +#define TREE_SITTER_LANGUAGE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./subtree.h" +#include "tree_sitter/parser.h" + +#define ts_builtin_sym_error_repeat (ts_builtin_sym_error - 1) +#define TREE_SITTER_LANGUAGE_VERSION_WITH_FIELDS 10 +#define TREE_SITTER_LANGUAGE_VERSION_WITH_SMALL_STATES 11 + +typedef struct { + const TSParseAction *actions; + uint32_t action_count; + bool is_reusable; +} TableEntry; + +void ts_language_table_entry(const TSLanguage *, TSStateId, TSSymbol, TableEntry *); + +TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *, TSSymbol); + +static inline bool ts_language_is_symbol_external(const TSLanguage *self, TSSymbol symbol) { + return 0 < symbol && symbol < self->external_token_count + 1; +} + +static inline const TSParseAction *ts_language_actions(const TSLanguage *self, + TSStateId state, + TSSymbol symbol, + uint32_t *count) { + TableEntry entry; + ts_language_table_entry(self, state, symbol, &entry); + *count = entry.action_count; + return entry.actions; +} + +static inline bool ts_language_has_actions(const TSLanguage *self, + TSStateId state, + TSSymbol symbol) { + TableEntry entry; + ts_language_table_entry(self, state, symbol, &entry); + return entry.action_count > 0; +} + +static inline bool ts_language_has_reduce_action(const TSLanguage *self, + TSStateId state, + TSSymbol symbol) { + TableEntry entry; + ts_language_table_entry(self, state, symbol, &entry); + return entry.action_count > 0 && entry.actions[0].type == TSParseActionTypeReduce; +} + +static inline uint16_t ts_language_lookup( + const TSLanguage *self, + TSStateId state, + TSSymbol symbol +) { + if ( + self->version >= TREE_SITTER_LANGUAGE_VERSION_WITH_SMALL_STATES && + state >= self->large_state_count + ) { + uint32_t index = self->small_parse_table_map[state - self->large_state_count]; + const uint16_t *data = &self->small_parse_table[index]; + uint16_t section_count = *(data++); + for (unsigned i = 0; i < section_count; i++) { + uint16_t section_value = *(data++); + uint16_t symbol_count = *(data++); + for (unsigned i = 0; i < symbol_count; i++) { + if (*(data++) == symbol) return section_value; + } + } + return 0; + } else { + return self->parse_table[state * self->symbol_count + symbol]; + } +} + +static inline TSStateId ts_language_next_state(const TSLanguage *self, + TSStateId state, + TSSymbol symbol) { + if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) { + return 0; + } else if (symbol < self->token_count) { + uint32_t count; + const TSParseAction *actions = ts_language_actions(self, state, symbol, &count); + if (count > 0) { + TSParseAction action = actions[count - 1]; + if (action.type == TSParseActionTypeShift || action.type == TSParseActionTypeRecover) { + return action.params.state; + } + } + return 0; + } else { + return ts_language_lookup(self, state, symbol); + } +} + +static inline const bool * +ts_language_enabled_external_tokens(const TSLanguage *self, + unsigned external_scanner_state) { + if (external_scanner_state == 0) { + return NULL; + } else { + return self->external_scanner.states + self->external_token_count * external_scanner_state; + } +} + +static inline const TSSymbol * +ts_language_alias_sequence(const TSLanguage *self, uint32_t production_id) { + return production_id > 0 ? + self->alias_sequences + production_id * self->max_alias_sequence_length : + NULL; +} + +static inline void ts_language_field_map( + const TSLanguage *self, + uint32_t production_id, + const TSFieldMapEntry **start, + const TSFieldMapEntry **end +) { + if (self->version < TREE_SITTER_LANGUAGE_VERSION_WITH_FIELDS || self->field_count == 0) { + *start = NULL; + *end = NULL; + return; + } + + TSFieldMapSlice slice = self->field_map_slices[production_id]; + *start = &self->field_map_entries[slice.index]; + *end = &self->field_map_entries[slice.index] + slice.length; +} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_LANGUAGE_H_ diff --git a/src/tree_sitter/length.h b/src/tree_sitter/length.h new file mode 100644 index 0000000000..61de9fc1d5 --- /dev/null +++ b/src/tree_sitter/length.h @@ -0,0 +1,44 @@ +#ifndef TREE_SITTER_LENGTH_H_ +#define TREE_SITTER_LENGTH_H_ + +#include +#include +#include "./point.h" +#include "tree_sitter/api.h" + +typedef struct { + uint32_t bytes; + TSPoint extent; +} Length; + +static const Length LENGTH_UNDEFINED = {0, {0, 1}}; +static const Length LENGTH_MAX = {UINT32_MAX, {UINT32_MAX, UINT32_MAX}}; + +static inline bool length_is_undefined(Length length) { + return length.bytes == 0 && length.extent.column != 0; +} + +static inline Length length_min(Length len1, Length len2) { + return (len1.bytes < len2.bytes) ? len1 : len2; +} + +static inline Length length_add(Length len1, Length len2) { + Length result; + result.bytes = len1.bytes + len2.bytes; + result.extent = point_add(len1.extent, len2.extent); + return result; +} + +static inline Length length_sub(Length len1, Length len2) { + Length result; + result.bytes = len1.bytes - len2.bytes; + result.extent = point_sub(len1.extent, len2.extent); + return result; +} + +static inline Length length_zero(void) { + Length result = {0, {0, 0}}; + return result; +} + +#endif diff --git a/src/tree_sitter/lexer.c b/src/tree_sitter/lexer.c new file mode 100644 index 0000000000..fdc127466f --- /dev/null +++ b/src/tree_sitter/lexer.c @@ -0,0 +1,322 @@ +#include +#include "./lexer.h" +#include "./subtree.h" +#include "./length.h" +#include "./utf16.h" +#include "utf8proc.h" + +#define LOG(...) \ + if (self->logger.log) { \ + snprintf(self->debug_buffer, TREE_SITTER_SERIALIZATION_BUFFER_SIZE, __VA_ARGS__); \ + self->logger.log(self->logger.payload, TSLogTypeLex, self->debug_buffer); \ + } + +#define LOG_CHARACTER(message, character) \ + LOG( \ + 32 <= character && character < 127 ? \ + message " character:'%c'" : \ + message " character:%d", character \ + ) + +static const char empty_chunk[3] = { 0, 0 }; + +static const int32_t BYTE_ORDER_MARK = 0xFEFF; + +static void ts_lexer__get_chunk(Lexer *self) { + self->chunk_start = self->current_position.bytes; + self->chunk = self->input.read( + self->input.payload, + self->current_position.bytes, + self->current_position.extent, + &self->chunk_size + ); + if (!self->chunk_size) self->chunk = empty_chunk; +} + +typedef utf8proc_ssize_t (*DecodeFunction)( + const utf8proc_uint8_t *, + utf8proc_ssize_t, + utf8proc_int32_t * +); + +static void ts_lexer__get_lookahead(Lexer *self) { + uint32_t position_in_chunk = self->current_position.bytes - self->chunk_start; + const uint8_t *chunk = (const uint8_t *)self->chunk + position_in_chunk; + uint32_t size = self->chunk_size - position_in_chunk; + + if (size == 0) { + self->lookahead_size = 1; + self->data.lookahead = '\0'; + return; + } + + DecodeFunction decode = + self->input.encoding == TSInputEncodingUTF8 ? utf8proc_iterate : utf16_iterate; + + self->lookahead_size = decode(chunk, size, &self->data.lookahead); + + // If this chunk ended in the middle of a multi-byte character, + // try again with a fresh chunk. + if (self->data.lookahead == -1 && size < 4) { + ts_lexer__get_chunk(self); + chunk = (const uint8_t *)self->chunk; + size = self->chunk_size; + self->lookahead_size = decode(chunk, size, &self->data.lookahead); + } + + if (self->data.lookahead == -1) { + self->lookahead_size = 1; + } +} + +static void ts_lexer__advance(TSLexer *payload, bool skip) { + Lexer *self = (Lexer *)payload; + if (self->chunk == empty_chunk) + return; + + if (self->lookahead_size) { + self->current_position.bytes += self->lookahead_size; + if (self->data.lookahead == '\n') { + self->current_position.extent.row++; + self->current_position.extent.column = 0; + } else { + self->current_position.extent.column += self->lookahead_size; + } + } + + TSRange *current_range = &self->included_ranges[self->current_included_range_index]; + if (self->current_position.bytes == current_range->end_byte) { + self->current_included_range_index++; + if (self->current_included_range_index == self->included_range_count) { + self->data.lookahead = '\0'; + self->lookahead_size = 1; + return; + } else { + current_range++; + self->current_position = (Length) { + current_range->start_byte, + current_range->start_point, + }; + } + } + + if (skip) { + LOG_CHARACTER("skip", self->data.lookahead); + self->token_start_position = self->current_position; + } else { + LOG_CHARACTER("consume", self->data.lookahead); + } + + if (self->current_position.bytes >= self->chunk_start + self->chunk_size) { + ts_lexer__get_chunk(self); + } + + ts_lexer__get_lookahead(self); +} + +static void ts_lexer__mark_end(TSLexer *payload) { + Lexer *self = (Lexer *)payload; + TSRange *current_included_range = &self->included_ranges[self->current_included_range_index]; + if (self->current_included_range_index > 0 && + self->current_position.bytes == current_included_range->start_byte) { + TSRange *previous_included_range = current_included_range - 1; + self->token_end_position = (Length) { + previous_included_range->end_byte, + previous_included_range->end_point, + }; + } else { + self->token_end_position = self->current_position; + } +} + +static uint32_t ts_lexer__get_column(TSLexer *payload) { + Lexer *self = (Lexer *)payload; + uint32_t goal_byte = self->current_position.bytes; + + self->current_position.bytes -= self->current_position.extent.column; + self->current_position.extent.column = 0; + + if (self->current_position.bytes < self->chunk_start) { + ts_lexer__get_chunk(self); + } + + uint32_t result = 0; + while (self->current_position.bytes < goal_byte) { + ts_lexer__advance(payload, false); + result++; + } + + return result; +} + +static bool ts_lexer__is_at_included_range_start(TSLexer *payload) { + const Lexer *self = (const Lexer *)payload; + TSRange *current_range = &self->included_ranges[self->current_included_range_index]; + return self->current_position.bytes == current_range->start_byte; +} + +// The lexer's methods are stored as a struct field so that generated +// parsers can call them without needing to be linked against this library. + +void ts_lexer_init(Lexer *self) { + *self = (Lexer) { + .data = { + .advance = ts_lexer__advance, + .mark_end = ts_lexer__mark_end, + .get_column = ts_lexer__get_column, + .is_at_included_range_start = ts_lexer__is_at_included_range_start, + .lookahead = 0, + .result_symbol = 0, + }, + .chunk = NULL, + .chunk_start = 0, + .current_position = {UINT32_MAX, {0, 0}}, + .logger = { + .payload = NULL, + .log = NULL + }, + .current_included_range_index = 0, + }; + + self->included_ranges = NULL; + ts_lexer_set_included_ranges(self, NULL, 0); + ts_lexer_reset(self, length_zero()); +} + +void ts_lexer_delete(Lexer *self) { + ts_free(self->included_ranges); +} + +void ts_lexer_set_input(Lexer *self, TSInput input) { + self->input = input; + self->data.lookahead = 0; + self->lookahead_size = 0; + self->chunk = 0; + self->chunk_start = 0; + self->chunk_size = 0; +} + +static void ts_lexer_goto(Lexer *self, Length position) { + bool found_included_range = false; + for (unsigned i = 0; i < self->included_range_count; i++) { + TSRange *included_range = &self->included_ranges[i]; + if (included_range->end_byte > position.bytes) { + if (included_range->start_byte > position.bytes) { + position = (Length) { + .bytes = included_range->start_byte, + .extent = included_range->start_point, + }; + } + + self->current_included_range_index = i; + found_included_range = true; + break; + } + } + + if (!found_included_range) { + TSRange *last_included_range = &self->included_ranges[self->included_range_count - 1]; + position = (Length) { + .bytes = last_included_range->end_byte, + .extent = last_included_range->end_point, + }; + self->chunk = empty_chunk; + self->chunk_start = position.bytes; + self->chunk_size = 2; + } + + self->token_start_position = position; + self->token_end_position = LENGTH_UNDEFINED; + self->current_position = position; + + if (self->chunk && (position.bytes < self->chunk_start || + position.bytes >= self->chunk_start + self->chunk_size)) { + self->chunk = 0; + self->chunk_start = 0; + self->chunk_size = 0; + } + + self->lookahead_size = 0; + self->data.lookahead = 0; +} + +void ts_lexer_reset(Lexer *self, Length position) { + if (position.bytes != self->current_position.bytes) ts_lexer_goto(self, position); +} + +void ts_lexer_start(Lexer *self) { + self->token_start_position = self->current_position; + self->token_end_position = LENGTH_UNDEFINED; + self->data.result_symbol = 0; + if (!self->chunk) ts_lexer__get_chunk(self); + if (!self->lookahead_size) ts_lexer__get_lookahead(self); + if ( + self->current_position.bytes == 0 && + self->data.lookahead == BYTE_ORDER_MARK + ) ts_lexer__advance((TSLexer *)self, true); +} + +void ts_lexer_finish(Lexer *self, uint32_t *lookahead_end_byte) { + if (length_is_undefined(self->token_end_position)) { + ts_lexer__mark_end(&self->data); + } + + uint32_t current_lookahead_end_byte = self->current_position.bytes + 1; + + // In order to determine that a byte sequence is invalid UTF8 or UTF16, + // the character decoding algorithm may have looked at the following byte. + // Therefore, the next byte *after* the current (invalid) character + // affects the interpretation of the current character. + if (self->data.lookahead == -1) { + current_lookahead_end_byte++; + } + + if (current_lookahead_end_byte > *lookahead_end_byte) { + *lookahead_end_byte = current_lookahead_end_byte; + } +} + +void ts_lexer_advance_to_end(Lexer *self) { + while (self->data.lookahead != 0) { + ts_lexer__advance((TSLexer *)self, false); + } +} + +void ts_lexer_mark_end(Lexer *self) { + ts_lexer__mark_end(&self->data); +} + +static const TSRange DEFAULT_RANGES[] = { + { + .start_point = { + .row = 0, + .column = 0, + }, + .end_point = { + .row = UINT32_MAX, + .column = UINT32_MAX, + }, + .start_byte = 0, + .end_byte = UINT32_MAX + } +}; + +void ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, uint32_t count) { + if (!ranges) { + ranges = DEFAULT_RANGES; + count = 1; + } + + size_t sz = count * sizeof(TSRange); + self->included_ranges = ts_realloc(self->included_ranges, sz); + memcpy(self->included_ranges, ranges, sz); + self->included_range_count = count; + ts_lexer_goto(self, self->current_position); +} + +TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count) { + *count = self->included_range_count; + return self->included_ranges; +} + +#undef LOG diff --git a/src/tree_sitter/lexer.h b/src/tree_sitter/lexer.h new file mode 100644 index 0000000000..f523d88f65 --- /dev/null +++ b/src/tree_sitter/lexer.h @@ -0,0 +1,48 @@ +#ifndef TREE_SITTER_LEXER_H_ +#define TREE_SITTER_LEXER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./length.h" +#include "./subtree.h" +#include "tree_sitter/api.h" +#include "tree_sitter/parser.h" + +typedef struct { + TSLexer data; + Length current_position; + Length token_start_position; + Length token_end_position; + + TSRange * included_ranges; + size_t included_range_count; + size_t current_included_range_index; + + const char *chunk; + uint32_t chunk_start; + uint32_t chunk_size; + uint32_t lookahead_size; + + TSInput input; + TSLogger logger; + char debug_buffer[TREE_SITTER_SERIALIZATION_BUFFER_SIZE]; +} Lexer; + +void ts_lexer_init(Lexer *); +void ts_lexer_delete(Lexer *); +void ts_lexer_set_input(Lexer *, TSInput); +void ts_lexer_reset(Lexer *, Length); +void ts_lexer_start(Lexer *); +void ts_lexer_finish(Lexer *, uint32_t *); +void ts_lexer_advance_to_end(Lexer *); +void ts_lexer_mark_end(Lexer *); +void ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, uint32_t count); +TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count); + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_LEXER_H_ diff --git a/src/tree_sitter/lib.c b/src/tree_sitter/lib.c new file mode 100644 index 0000000000..fc5fbc9210 --- /dev/null +++ b/src/tree_sitter/lib.c @@ -0,0 +1,20 @@ +// The Tree-sitter library can be built by compiling this one source file. +// +// The following directories must be added to the include path: +// - include +// - utf8proc + +#define _POSIX_C_SOURCE 200112L +#define UTF8PROC_STATIC + +#include "./get_changed_ranges.c" +#include "./language.c" +#include "./lexer.c" +#include "./node.c" +#include "./parser.c" +#include "./stack.c" +#include "./subtree.c" +#include "./tree_cursor.c" +#include "./tree.c" +#include "./utf16.c" +#include "utf8proc.c" diff --git a/src/tree_sitter/node.c b/src/tree_sitter/node.c new file mode 100644 index 0000000000..6b2be36ee5 --- /dev/null +++ b/src/tree_sitter/node.c @@ -0,0 +1,673 @@ +#include +#include "./subtree.h" +#include "./tree.h" +#include "./language.h" + +typedef struct { + Subtree parent; + const TSTree *tree; + Length position; + uint32_t child_index; + uint32_t structural_child_index; + const TSSymbol *alias_sequence; +} NodeChildIterator; + +// TSNode - constructors + +TSNode ts_node_new( + const TSTree *tree, + const Subtree *subtree, + Length position, + TSSymbol alias +) { + return (TSNode) { + {position.bytes, position.extent.row, position.extent.column, alias}, + subtree, + tree, + }; +} + +static inline TSNode ts_node__null(void) { + return ts_node_new(NULL, NULL, length_zero(), 0); +} + +// TSNode - accessors + +uint32_t ts_node_start_byte(TSNode self) { + return self.context[0]; +} + +TSPoint ts_node_start_point(TSNode self) { + return (TSPoint) {self.context[1], self.context[2]}; +} + +static inline uint32_t ts_node__alias(const TSNode *self) { + return self->context[3]; +} + +static inline Subtree ts_node__subtree(TSNode self) { + return *(const Subtree *)self.id; +} + +// NodeChildIterator + +static inline NodeChildIterator ts_node_iterate_children(const TSNode *node) { + Subtree subtree = ts_node__subtree(*node); + if (ts_subtree_child_count(subtree) == 0) { + return (NodeChildIterator) {NULL_SUBTREE, node->tree, length_zero(), 0, 0, NULL}; + } + const TSSymbol *alias_sequence = ts_language_alias_sequence( + node->tree->language, + subtree.ptr->production_id + ); + return (NodeChildIterator) { + .tree = node->tree, + .parent = subtree, + .position = {ts_node_start_byte(*node), ts_node_start_point(*node)}, + .child_index = 0, + .structural_child_index = 0, + .alias_sequence = alias_sequence, + }; +} + +static inline bool ts_node_child_iterator_done(NodeChildIterator *self) { + return self->child_index == self->parent.ptr->child_count; +} + +static inline bool ts_node_child_iterator_next( + NodeChildIterator *self, + TSNode *result +) { + if (!self->parent.ptr || ts_node_child_iterator_done(self)) return false; + const Subtree *child = &self->parent.ptr->children[self->child_index]; + TSSymbol alias_symbol = 0; + if (!ts_subtree_extra(*child)) { + if (self->alias_sequence) { + alias_symbol = self->alias_sequence[self->structural_child_index]; + } + self->structural_child_index++; + } + if (self->child_index > 0) { + self->position = length_add(self->position, ts_subtree_padding(*child)); + } + *result = ts_node_new( + self->tree, + child, + self->position, + alias_symbol + ); + self->position = length_add(self->position, ts_subtree_size(*child)); + self->child_index++; + return true; +} + +// TSNode - private + +static inline bool ts_node__is_relevant(TSNode self, bool include_anonymous) { + Subtree tree = ts_node__subtree(self); + if (include_anonymous) { + return ts_subtree_visible(tree) || ts_node__alias(&self); + } else { + TSSymbol alias = ts_node__alias(&self); + if (alias) { + return ts_language_symbol_metadata(self.tree->language, alias).named; + } else { + return ts_subtree_visible(tree) && ts_subtree_named(tree); + } + } +} + +static inline uint32_t ts_node__relevant_child_count( + TSNode self, + bool include_anonymous +) { + Subtree tree = ts_node__subtree(self); + if (ts_subtree_child_count(tree) > 0) { + if (include_anonymous) { + return tree.ptr->visible_child_count; + } else { + return tree.ptr->named_child_count; + } + } else { + return 0; + } +} + +static inline TSNode ts_node__child( + TSNode self, + uint32_t child_index, + bool include_anonymous +) { + TSNode result = self; + bool did_descend = true; + + while (did_descend) { + did_descend = false; + + TSNode child; + uint32_t index = 0; + NodeChildIterator iterator = ts_node_iterate_children(&result); + while (ts_node_child_iterator_next(&iterator, &child)) { + if (ts_node__is_relevant(child, include_anonymous)) { + if (index == child_index) { + ts_tree_set_cached_parent(self.tree, &child, &self); + return child; + } + index++; + } else { + uint32_t grandchild_index = child_index - index; + uint32_t grandchild_count = ts_node__relevant_child_count(child, include_anonymous); + if (grandchild_index < grandchild_count) { + did_descend = true; + result = child; + child_index = grandchild_index; + break; + } + index += grandchild_count; + } + } + } + + return ts_node__null(); +} + +static bool ts_subtree_has_trailing_empty_descendant( + Subtree self, + Subtree other +) { + for (unsigned i = ts_subtree_child_count(self) - 1; i + 1 > 0; i--) { + Subtree child = self.ptr->children[i]; + if (ts_subtree_total_bytes(child) > 0) break; + if (child.ptr == other.ptr || ts_subtree_has_trailing_empty_descendant(child, other)) { + return true; + } + } + return false; +} + +static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous) { + Subtree self_subtree = ts_node__subtree(self); + bool self_is_empty = ts_subtree_total_bytes(self_subtree) == 0; + uint32_t target_end_byte = ts_node_end_byte(self); + + TSNode node = ts_node_parent(self); + TSNode earlier_node = ts_node__null(); + bool earlier_node_is_relevant = false; + + while (!ts_node_is_null(node)) { + TSNode earlier_child = ts_node__null(); + bool earlier_child_is_relevant = false; + bool found_child_containing_target = false; + + TSNode child; + NodeChildIterator iterator = ts_node_iterate_children(&node); + while (ts_node_child_iterator_next(&iterator, &child)) { + if (child.id == self.id) break; + if (iterator.position.bytes > target_end_byte) { + found_child_containing_target = true; + break; + } + + if (iterator.position.bytes == target_end_byte && + (!self_is_empty || + ts_subtree_has_trailing_empty_descendant(ts_node__subtree(child), self_subtree))) { + found_child_containing_target = true; + break; + } + + if (ts_node__is_relevant(child, include_anonymous)) { + earlier_child = child; + earlier_child_is_relevant = true; + } else if (ts_node__relevant_child_count(child, include_anonymous) > 0) { + earlier_child = child; + earlier_child_is_relevant = false; + } + } + + if (found_child_containing_target) { + if (!ts_node_is_null(earlier_child)) { + earlier_node = earlier_child; + earlier_node_is_relevant = earlier_child_is_relevant; + } + node = child; + } else if (earlier_child_is_relevant) { + return earlier_child; + } else if (!ts_node_is_null(earlier_child)) { + node = earlier_child; + } else if (earlier_node_is_relevant) { + return earlier_node; + } else { + node = earlier_node; + } + } + + return ts_node__null(); +} + +static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) { + uint32_t target_end_byte = ts_node_end_byte(self); + + TSNode node = ts_node_parent(self); + TSNode later_node = ts_node__null(); + bool later_node_is_relevant = false; + + while (!ts_node_is_null(node)) { + TSNode later_child = ts_node__null(); + bool later_child_is_relevant = false; + TSNode child_containing_target = ts_node__null(); + + TSNode child; + NodeChildIterator iterator = ts_node_iterate_children(&node); + while (ts_node_child_iterator_next(&iterator, &child)) { + if (iterator.position.bytes < target_end_byte) continue; + if (ts_node_start_byte(child) <= ts_node_start_byte(self)) { + if (ts_node__subtree(child).ptr != ts_node__subtree(self).ptr) { + child_containing_target = child; + } + } else if (ts_node__is_relevant(child, include_anonymous)) { + later_child = child; + later_child_is_relevant = true; + break; + } else if (ts_node__relevant_child_count(child, include_anonymous) > 0) { + later_child = child; + later_child_is_relevant = false; + break; + } + } + + if (!ts_node_is_null(child_containing_target)) { + if (!ts_node_is_null(later_child)) { + later_node = later_child; + later_node_is_relevant = later_child_is_relevant; + } + node = child_containing_target; + } else if (later_child_is_relevant) { + return later_child; + } else if (!ts_node_is_null(later_child)) { + node = later_child; + } else if (later_node_is_relevant) { + return later_node; + } else { + node = later_node; + } + } + + return ts_node__null(); +} + +static inline TSNode ts_node__first_child_for_byte( + TSNode self, + uint32_t goal, + bool include_anonymous +) { + TSNode node = self; + bool did_descend = true; + + while (did_descend) { + did_descend = false; + + TSNode child; + NodeChildIterator iterator = ts_node_iterate_children(&node); + while (ts_node_child_iterator_next(&iterator, &child)) { + if (ts_node_end_byte(child) > goal) { + if (ts_node__is_relevant(child, include_anonymous)) { + return child; + } else if (ts_node_child_count(child) > 0) { + did_descend = true; + node = child; + break; + } + } + } + } + + return ts_node__null(); +} + +static inline TSNode ts_node__descendant_for_byte_range( + TSNode self, + uint32_t range_start, + uint32_t range_end, + bool include_anonymous +) { + TSNode node = self; + TSNode last_visible_node = self; + + bool did_descend = true; + while (did_descend) { + did_descend = false; + + TSNode child; + NodeChildIterator iterator = ts_node_iterate_children(&node); + while (ts_node_child_iterator_next(&iterator, &child)) { + uint32_t node_end = iterator.position.bytes; + + // The end of this node must extend far enough forward to touch + // the end of the range and exceed the start of the range. + if (node_end < range_end) continue; + if (node_end <= range_start) continue; + + // The start of this node must extend far enough backward to + // touch the start of the range. + if (range_start < ts_node_start_byte(child)) break; + + node = child; + if (ts_node__is_relevant(node, include_anonymous)) { + ts_tree_set_cached_parent(self.tree, &child, &last_visible_node); + last_visible_node = node; + } + did_descend = true; + break; + } + } + + return last_visible_node; +} + +static inline TSNode ts_node__descendant_for_point_range( + TSNode self, + TSPoint range_start, + TSPoint range_end, + bool include_anonymous +) { + TSNode node = self; + TSNode last_visible_node = self; + + bool did_descend = true; + while (did_descend) { + did_descend = false; + + TSNode child; + NodeChildIterator iterator = ts_node_iterate_children(&node); + while (ts_node_child_iterator_next(&iterator, &child)) { + TSPoint node_end = iterator.position.extent; + + // The end of this node must extend far enough forward to touch + // the end of the range and exceed the start of the range. + if (point_lt(node_end, range_end)) continue; + if (point_lte(node_end, range_start)) continue; + + // The start of this node must extend far enough backward to + // touch the start of the range. + if (point_lt(range_start, ts_node_start_point(child))) break; + + node = child; + if (ts_node__is_relevant(node, include_anonymous)) { + ts_tree_set_cached_parent(self.tree, &child, &last_visible_node); + last_visible_node = node; + } + did_descend = true; + break; + } + } + + return last_visible_node; +} + +// TSNode - public + +uint32_t ts_node_end_byte(TSNode self) { + return ts_node_start_byte(self) + ts_subtree_size(ts_node__subtree(self)).bytes; +} + +TSPoint ts_node_end_point(TSNode self) { + return point_add(ts_node_start_point(self), ts_subtree_size(ts_node__subtree(self)).extent); +} + +TSSymbol ts_node_symbol(TSNode self) { + return ts_node__alias(&self) + ? ts_node__alias(&self) + : ts_subtree_symbol(ts_node__subtree(self)); +} + +const char *ts_node_type(TSNode self) { + return ts_language_symbol_name(self.tree->language, ts_node_symbol(self)); +} + +char *ts_node_string(TSNode self) { + return ts_subtree_string(ts_node__subtree(self), self.tree->language, false); +} + +bool ts_node_eq(TSNode self, TSNode other) { + return self.tree == other.tree && self.id == other.id; +} + +bool ts_node_is_null(TSNode self) { + return self.id == 0; +} + +bool ts_node_is_extra(TSNode self) { + return ts_subtree_extra(ts_node__subtree(self)); +} + +bool ts_node_is_named(TSNode self) { + TSSymbol alias = ts_node__alias(&self); + return alias + ? ts_language_symbol_metadata(self.tree->language, alias).named + : ts_subtree_named(ts_node__subtree(self)); +} + +bool ts_node_is_missing(TSNode self) { + return ts_subtree_missing(ts_node__subtree(self)); +} + +bool ts_node_has_changes(TSNode self) { + return ts_subtree_has_changes(ts_node__subtree(self)); +} + +bool ts_node_has_error(TSNode self) { + return ts_subtree_error_cost(ts_node__subtree(self)) > 0; +} + +TSNode ts_node_parent(TSNode self) { + TSNode node = ts_tree_get_cached_parent(self.tree, &self); + if (node.id) return node; + + node = ts_tree_root_node(self.tree); + uint32_t end_byte = ts_node_end_byte(self); + if (node.id == self.id) return ts_node__null(); + + TSNode last_visible_node = node; + bool did_descend = true; + while (did_descend) { + did_descend = false; + + TSNode child; + NodeChildIterator iterator = ts_node_iterate_children(&node); + while (ts_node_child_iterator_next(&iterator, &child)) { + if ( + ts_node_start_byte(child) > ts_node_start_byte(self) || + child.id == self.id + ) break; + if (iterator.position.bytes >= end_byte) { + node = child; + if (ts_node__is_relevant(child, true)) { + ts_tree_set_cached_parent(self.tree, &node, &last_visible_node); + last_visible_node = node; + } + did_descend = true; + break; + } + } + } + + return last_visible_node; +} + +TSNode ts_node_child(TSNode self, uint32_t child_index) { + return ts_node__child(self, child_index, true); +} + +TSNode ts_node_named_child(TSNode self, uint32_t child_index) { + return ts_node__child(self, child_index, false); +} + +TSNode ts_node_child_by_field_id(TSNode self, TSFieldId field_id) { +recur: + if (!field_id || ts_node_child_count(self) == 0) return ts_node__null(); + + const TSFieldMapEntry *field_map, *field_map_end; + ts_language_field_map( + self.tree->language, + ts_node__subtree(self).ptr->production_id, + &field_map, + &field_map_end + ); + if (field_map == field_map_end) return ts_node__null(); + + // The field mappings are sorted by their field id. Scan all + // the mappings to find the ones for the given field id. + while (field_map->field_id < field_id) { + field_map++; + if (field_map == field_map_end) return ts_node__null(); + } + while (field_map_end[-1].field_id > field_id) { + field_map_end--; + if (field_map == field_map_end) return ts_node__null(); + } + + TSNode child; + NodeChildIterator iterator = ts_node_iterate_children(&self); + while (ts_node_child_iterator_next(&iterator, &child)) { + if (!ts_subtree_extra(ts_node__subtree(child))) { + uint32_t index = iterator.structural_child_index - 1; + if (index < field_map->child_index) continue; + + // Hidden nodes' fields are "inherited" by their visible parent. + if (field_map->inherited) { + + // If this is the *last* possible child node for this field, + // then perform a tail call to avoid recursion. + if (field_map + 1 == field_map_end) { + self = child; + goto recur; + } + + // Otherwise, descend into this child, but if it doesn't contain + // the field, continue searching subsequent children. + else { + TSNode result = ts_node_child_by_field_id(child, field_id); + if (result.id) return result; + field_map++; + if (field_map == field_map_end) return ts_node__null(); + } + } + + else if (ts_node__is_relevant(child, true)) { + return child; + } + + // If the field refers to a hidden node, return its first visible + // child. + else { + return ts_node_child(child, 0); + } + } + } + + return ts_node__null(); +} + +TSNode ts_node_child_by_field_name( + TSNode self, + const char *name, + uint32_t name_length +) { + TSFieldId field_id = ts_language_field_id_for_name( + self.tree->language, + name, + name_length + ); + return ts_node_child_by_field_id(self, field_id); +} + +uint32_t ts_node_child_count(TSNode self) { + Subtree tree = ts_node__subtree(self); + if (ts_subtree_child_count(tree) > 0) { + return tree.ptr->visible_child_count; + } else { + return 0; + } +} + +uint32_t ts_node_named_child_count(TSNode self) { + Subtree tree = ts_node__subtree(self); + if (ts_subtree_child_count(tree) > 0) { + return tree.ptr->named_child_count; + } else { + return 0; + } +} + +TSNode ts_node_next_sibling(TSNode self) { + return ts_node__next_sibling(self, true); +} + +TSNode ts_node_next_named_sibling(TSNode self) { + return ts_node__next_sibling(self, false); +} + +TSNode ts_node_prev_sibling(TSNode self) { + return ts_node__prev_sibling(self, true); +} + +TSNode ts_node_prev_named_sibling(TSNode self) { + return ts_node__prev_sibling(self, false); +} + +TSNode ts_node_first_child_for_byte(TSNode self, uint32_t byte) { + return ts_node__first_child_for_byte(self, byte, true); +} + +TSNode ts_node_first_named_child_for_byte(TSNode self, uint32_t byte) { + return ts_node__first_child_for_byte(self, byte, false); +} + +TSNode ts_node_descendant_for_byte_range( + TSNode self, + uint32_t start, + uint32_t end +) { + return ts_node__descendant_for_byte_range(self, start, end, true); +} + +TSNode ts_node_named_descendant_for_byte_range( + TSNode self, + uint32_t start, + uint32_t end +) { + return ts_node__descendant_for_byte_range(self, start, end, false); +} + +TSNode ts_node_descendant_for_point_range( + TSNode self, + TSPoint start, + TSPoint end +) { + return ts_node__descendant_for_point_range(self, start, end, true); +} + +TSNode ts_node_named_descendant_for_point_range( + TSNode self, + TSPoint start, + TSPoint end +) { + return ts_node__descendant_for_point_range(self, start, end, false); +} + +void ts_node_edit(TSNode *self, const TSInputEdit *edit) { + uint32_t start_byte = ts_node_start_byte(*self); + TSPoint start_point = ts_node_start_point(*self); + + if (start_byte >= edit->old_end_byte) { + start_byte = edit->new_end_byte + (start_byte - edit->old_end_byte); + start_point = point_add(edit->new_end_point, point_sub(start_point, edit->old_end_point)); + } else if (start_byte > edit->start_byte) { + start_byte = edit->new_end_byte; + start_point = edit->new_end_point; + } + + self->context[0] = start_byte; + self->context[1] = start_point.row; + self->context[2] = start_point.column; +} diff --git a/src/tree_sitter/parser.c b/src/tree_sitter/parser.c new file mode 100644 index 0000000000..88b20845fd --- /dev/null +++ b/src/tree_sitter/parser.c @@ -0,0 +1,1887 @@ +#include +#include +#include +#include +#include +#include "tree_sitter/api.h" +#include "./alloc.h" +#include "./array.h" +#include "./atomic.h" +#include "./clock.h" +#include "./error_costs.h" +#include "./get_changed_ranges.h" +#include "./language.h" +#include "./length.h" +#include "./lexer.h" +#include "./reduce_action.h" +#include "./reusable_node.h" +#include "./stack.h" +#include "./subtree.h" +#include "./tree.h" + +#define LOG(...) \ + if (self->lexer.logger.log || self->dot_graph_file) { \ + snprintf(self->lexer.debug_buffer, TREE_SITTER_SERIALIZATION_BUFFER_SIZE, __VA_ARGS__); \ + ts_parser__log(self); \ + } + +#define LOG_STACK() \ + if (self->dot_graph_file) { \ + ts_stack_print_dot_graph(self->stack, self->language, self->dot_graph_file); \ + fputs("\n\n", self->dot_graph_file); \ + } + +#define LOG_TREE(tree) \ + if (self->dot_graph_file) { \ + ts_subtree_print_dot_graph(tree, self->language, self->dot_graph_file); \ + fputs("\n", self->dot_graph_file); \ + } + +#define SYM_NAME(symbol) ts_language_symbol_name(self->language, symbol) + +#define TREE_NAME(tree) SYM_NAME(ts_subtree_symbol(tree)) + +static const unsigned MAX_VERSION_COUNT = 6; +static const unsigned MAX_VERSION_COUNT_OVERFLOW = 4; +static const unsigned MAX_SUMMARY_DEPTH = 16; +static const unsigned MAX_COST_DIFFERENCE = 16 * ERROR_COST_PER_SKIPPED_TREE; +static const unsigned OP_COUNT_PER_TIMEOUT_CHECK = 100; + +typedef struct { + Subtree token; + Subtree last_external_token; + uint32_t byte_index; +} TokenCache; + +struct TSParser { + Lexer lexer; + Stack *stack; + SubtreePool tree_pool; + const TSLanguage *language; + ReduceActionSet reduce_actions; + Subtree finished_tree; + SubtreeHeapData scratch_tree_data; + MutableSubtree scratch_tree; + TokenCache token_cache; + ReusableNode reusable_node; + void *external_scanner_payload; + FILE *dot_graph_file; + TSClock end_clock; + TSDuration timeout_duration; + unsigned accept_count; + unsigned operation_count; + const volatile size_t *cancellation_flag; + bool halt_on_error; + Subtree old_tree; + TSRangeArray included_range_differences; + unsigned included_range_difference_index; +}; + +typedef struct { + unsigned cost; + unsigned node_count; + int dynamic_precedence; + bool is_in_error; +} ErrorStatus; + +typedef enum { + ErrorComparisonTakeLeft, + ErrorComparisonPreferLeft, + ErrorComparisonNone, + ErrorComparisonPreferRight, + ErrorComparisonTakeRight, +} ErrorComparison; + +typedef struct { + const char *string; + uint32_t length; +} TSStringInput; + +// StringInput + +static const char *ts_string_input_read( + void *_self, + uint32_t byte, + TSPoint _, + uint32_t *length +) { + TSStringInput *self = (TSStringInput *)_self; + if (byte >= self->length) { + *length = 0; + return ""; + } else { + *length = self->length - byte; + return self->string + byte; + } +} + +// Parser - Private + +static void ts_parser__log(TSParser *self) { + if (self->lexer.logger.log) { + self->lexer.logger.log( + self->lexer.logger.payload, + TSLogTypeParse, + self->lexer.debug_buffer + ); + } + + if (self->dot_graph_file) { + fprintf(self->dot_graph_file, "graph {\nlabel=\""); + for (char *c = &self->lexer.debug_buffer[0]; *c != 0; c++) { + if (*c == '"') fputc('\\', self->dot_graph_file); + fputc(*c, self->dot_graph_file); + } + fprintf(self->dot_graph_file, "\"\n}\n\n"); + } +} + +static bool ts_parser__breakdown_top_of_stack( + TSParser *self, + StackVersion version +) { + bool did_break_down = false; + bool pending = false; + + do { + StackSliceArray pop = ts_stack_pop_pending(self->stack, version); + if (!pop.size) break; + + did_break_down = true; + pending = false; + for (uint32_t i = 0; i < pop.size; i++) { + StackSlice slice = pop.contents[i]; + TSStateId state = ts_stack_state(self->stack, slice.version); + Subtree parent = *array_front(&slice.subtrees); + + for (uint32_t j = 0, n = ts_subtree_child_count(parent); j < n; j++) { + Subtree child = parent.ptr->children[j]; + pending = ts_subtree_child_count(child) > 0; + + if (ts_subtree_is_error(child)) { + state = ERROR_STATE; + } else if (!ts_subtree_extra(child)) { + state = ts_language_next_state(self->language, state, ts_subtree_symbol(child)); + } + + ts_subtree_retain(child); + ts_stack_push(self->stack, slice.version, child, pending, state); + } + + for (uint32_t j = 1; j < slice.subtrees.size; j++) { + Subtree tree = slice.subtrees.contents[j]; + ts_stack_push(self->stack, slice.version, tree, false, state); + } + + ts_subtree_release(&self->tree_pool, parent); + array_delete(&slice.subtrees); + + LOG("breakdown_top_of_stack tree:%s", TREE_NAME(parent)); + LOG_STACK(); + } + } while (pending); + + return did_break_down; +} + +static void ts_parser__breakdown_lookahead( + TSParser *self, + Subtree *lookahead, + TSStateId state, + ReusableNode *reusable_node +) { + bool did_descend = false; + Subtree tree = reusable_node_tree(reusable_node); + while (ts_subtree_child_count(tree) > 0 && ts_subtree_parse_state(tree) != state) { + LOG("state_mismatch sym:%s", TREE_NAME(tree)); + reusable_node_descend(reusable_node); + tree = reusable_node_tree(reusable_node); + did_descend = true; + } + + if (did_descend) { + ts_subtree_release(&self->tree_pool, *lookahead); + *lookahead = tree; + ts_subtree_retain(*lookahead); + } +} + +static ErrorComparison ts_parser__compare_versions( + TSParser *self, + ErrorStatus a, + ErrorStatus b +) { + if (!a.is_in_error && b.is_in_error) { + if (a.cost < b.cost) { + return ErrorComparisonTakeLeft; + } else { + return ErrorComparisonPreferLeft; + } + } + + if (a.is_in_error && !b.is_in_error) { + if (b.cost < a.cost) { + return ErrorComparisonTakeRight; + } else { + return ErrorComparisonPreferRight; + } + } + + if (a.cost < b.cost) { + if ((b.cost - a.cost) * (1 + a.node_count) > MAX_COST_DIFFERENCE) { + return ErrorComparisonTakeLeft; + } else { + return ErrorComparisonPreferLeft; + } + } + + if (b.cost < a.cost) { + if ((a.cost - b.cost) * (1 + b.node_count) > MAX_COST_DIFFERENCE) { + return ErrorComparisonTakeRight; + } else { + return ErrorComparisonPreferRight; + } + } + + if (a.dynamic_precedence > b.dynamic_precedence) return ErrorComparisonPreferLeft; + if (b.dynamic_precedence > a.dynamic_precedence) return ErrorComparisonPreferRight; + return ErrorComparisonNone; +} + +static ErrorStatus ts_parser__version_status( + TSParser *self, + StackVersion version +) { + unsigned cost = ts_stack_error_cost(self->stack, version); + bool is_paused = ts_stack_is_paused(self->stack, version); + if (is_paused) cost += ERROR_COST_PER_SKIPPED_TREE; + return (ErrorStatus) { + .cost = cost, + .node_count = ts_stack_node_count_since_error(self->stack, version), + .dynamic_precedence = ts_stack_dynamic_precedence(self->stack, version), + .is_in_error = is_paused || ts_stack_state(self->stack, version) == ERROR_STATE + }; +} + +static bool ts_parser__better_version_exists( + TSParser *self, + StackVersion version, + bool is_in_error, + unsigned cost +) { + if (self->finished_tree.ptr && ts_subtree_error_cost(self->finished_tree) <= cost) { + return true; + } + + Length position = ts_stack_position(self->stack, version); + ErrorStatus status = { + .cost = cost, + .is_in_error = is_in_error, + .dynamic_precedence = ts_stack_dynamic_precedence(self->stack, version), + .node_count = ts_stack_node_count_since_error(self->stack, version), + }; + + for (StackVersion i = 0, n = ts_stack_version_count(self->stack); i < n; i++) { + if (i == version || + !ts_stack_is_active(self->stack, i) || + ts_stack_position(self->stack, i).bytes < position.bytes) continue; + ErrorStatus status_i = ts_parser__version_status(self, i); + switch (ts_parser__compare_versions(self, status, status_i)) { + case ErrorComparisonTakeRight: + return true; + case ErrorComparisonPreferRight: + if (ts_stack_can_merge(self->stack, i, version)) return true; + default: + break; + } + } + + return false; +} + +static void ts_parser__restore_external_scanner( + TSParser *self, + Subtree external_token +) { + if (external_token.ptr) { + self->language->external_scanner.deserialize( + self->external_scanner_payload, + ts_external_scanner_state_data(&external_token.ptr->external_scanner_state), + external_token.ptr->external_scanner_state.length + ); + } else { + self->language->external_scanner.deserialize(self->external_scanner_payload, NULL, 0); + } +} + +static bool ts_parser__can_reuse_first_leaf( + TSParser *self, + TSStateId state, + Subtree tree, + TableEntry *table_entry +) { + TSLexMode current_lex_mode = self->language->lex_modes[state]; + TSSymbol leaf_symbol = ts_subtree_leaf_symbol(tree); + TSStateId leaf_state = ts_subtree_leaf_parse_state(tree); + TSLexMode leaf_lex_mode = self->language->lex_modes[leaf_state]; + + // If the token was created in a state with the same set of lookaheads, it is reusable. + if ( + table_entry->action_count > 0 && + memcmp(&leaf_lex_mode, ¤t_lex_mode, sizeof(TSLexMode)) == 0 && + ( + leaf_symbol != self->language->keyword_capture_token || + (!ts_subtree_is_keyword(tree) && ts_subtree_parse_state(tree) == state) + ) + ) return true; + + // Empty tokens are not reusable in states with different lookaheads. + if (ts_subtree_size(tree).bytes == 0 && leaf_symbol != ts_builtin_sym_end) return false; + + // If the current state allows external tokens or other tokens that conflict with this + // token, this token is not reusable. + return current_lex_mode.external_lex_state == 0 && table_entry->is_reusable; +} + +static Subtree ts_parser__lex( + TSParser *self, + StackVersion version, + TSStateId parse_state +) { + Length start_position = ts_stack_position(self->stack, version); + Subtree external_token = ts_stack_last_external_token(self->stack, version); + TSLexMode lex_mode = self->language->lex_modes[parse_state]; + const bool *valid_external_tokens = ts_language_enabled_external_tokens( + self->language, + lex_mode.external_lex_state + ); + + bool found_external_token = false; + bool error_mode = parse_state == ERROR_STATE; + bool skipped_error = false; + int32_t first_error_character = 0; + Length error_start_position = length_zero(); + Length error_end_position = length_zero(); + uint32_t lookahead_end_byte = 0; + ts_lexer_reset(&self->lexer, start_position); + + for (;;) { + Length current_position = self->lexer.current_position; + + if (valid_external_tokens) { + LOG( + "lex_external state:%d, row:%u, column:%u", + lex_mode.external_lex_state, + current_position.extent.row + 1, + current_position.extent.column + ); + ts_lexer_start(&self->lexer); + ts_parser__restore_external_scanner(self, external_token); + bool found_token = self->language->external_scanner.scan( + self->external_scanner_payload, + &self->lexer.data, + valid_external_tokens + ); + ts_lexer_finish(&self->lexer, &lookahead_end_byte); + + // Zero-length external tokens are generally allowed, but they're not + // allowed right after a syntax error. This is for two reasons: + // 1. After a syntax error, the lexer is looking for any possible token, + // as opposed to the specific set of tokens that are valid in some + // parse state. In this situation, it's very easy for an external + // scanner to produce unwanted zero-length tokens. + // 2. The parser sometimes inserts *missing* tokens to recover from + // errors. These tokens are also zero-length. If we allow more + // zero-length tokens to be created after missing tokens, it + // can lead to infinite loops. Forbidding zero-length tokens + // right at the point of error recovery is a conservative strategy + // for preventing this kind of infinite loop. + if (found_token && ( + self->lexer.token_end_position.bytes > current_position.bytes || + (!error_mode && ts_stack_has_advanced_since_error(self->stack, version)) + )) { + found_external_token = true; + break; + } + + ts_lexer_reset(&self->lexer, current_position); + } + + LOG( + "lex_internal state:%d, row:%u, column:%u", + lex_mode.lex_state, + current_position.extent.row + 1, + current_position.extent.column + ); + ts_lexer_start(&self->lexer); + bool found_token = self->language->lex_fn(&self->lexer.data, lex_mode.lex_state); + ts_lexer_finish(&self->lexer, &lookahead_end_byte); + if (found_token) break; + + if (!error_mode) { + error_mode = true; + lex_mode = self->language->lex_modes[ERROR_STATE]; + valid_external_tokens = ts_language_enabled_external_tokens( + self->language, + lex_mode.external_lex_state + ); + ts_lexer_reset(&self->lexer, start_position); + continue; + } + + if (!skipped_error) { + LOG("skip_unrecognized_character"); + skipped_error = true; + error_start_position = self->lexer.token_start_position; + error_end_position = self->lexer.token_start_position; + first_error_character = self->lexer.data.lookahead; + } + + if (self->lexer.current_position.bytes == error_end_position.bytes) { + if (self->lexer.data.lookahead == 0) { + self->lexer.data.result_symbol = ts_builtin_sym_error; + break; + } + self->lexer.data.advance(&self->lexer.data, false); + } + + error_end_position = self->lexer.current_position; + } + + Subtree result; + if (skipped_error) { + Length padding = length_sub(error_start_position, start_position); + Length size = length_sub(error_end_position, error_start_position); + uint32_t lookahead_bytes = lookahead_end_byte - error_end_position.bytes; + result = ts_subtree_new_error( + &self->tree_pool, + first_error_character, + padding, + size, + lookahead_bytes, + parse_state, + self->language + ); + + LOG( + "lexed_lookahead sym:%s, size:%u, character:'%c'", + SYM_NAME(ts_subtree_symbol(result)), + ts_subtree_total_size(result).bytes, + first_error_character + ); + } else { + if (self->lexer.token_end_position.bytes < self->lexer.token_start_position.bytes) { + self->lexer.token_start_position = self->lexer.token_end_position; + } + + bool is_keyword = false; + TSSymbol symbol = self->lexer.data.result_symbol; + Length padding = length_sub(self->lexer.token_start_position, start_position); + Length size = length_sub(self->lexer.token_end_position, self->lexer.token_start_position); + uint32_t lookahead_bytes = lookahead_end_byte - self->lexer.token_end_position.bytes; + + if (found_external_token) { + symbol = self->language->external_scanner.symbol_map[symbol]; + } else if (symbol == self->language->keyword_capture_token && symbol != 0) { + uint32_t end_byte = self->lexer.token_end_position.bytes; + ts_lexer_reset(&self->lexer, self->lexer.token_start_position); + ts_lexer_start(&self->lexer); + if ( + self->language->keyword_lex_fn(&self->lexer.data, 0) && + self->lexer.token_end_position.bytes == end_byte && + ts_language_has_actions(self->language, parse_state, self->lexer.data.result_symbol) + ) { + is_keyword = true; + symbol = self->lexer.data.result_symbol; + } + } + + result = ts_subtree_new_leaf( + &self->tree_pool, + symbol, + padding, + size, + lookahead_bytes, + parse_state, + found_external_token, + is_keyword, + self->language + ); + + if (found_external_token) { + unsigned length = self->language->external_scanner.serialize( + self->external_scanner_payload, + self->lexer.debug_buffer + ); + ts_external_scanner_state_init( + &((SubtreeHeapData *)result.ptr)->external_scanner_state, + self->lexer.debug_buffer, + length + ); + } + + LOG( + "lexed_lookahead sym:%s, size:%u", + SYM_NAME(ts_subtree_symbol(result)), + ts_subtree_total_size(result).bytes + ); + } + + return result; +} + +static Subtree ts_parser__get_cached_token( + TSParser *self, + TSStateId state, + size_t position, + Subtree last_external_token, + TableEntry *table_entry +) { + TokenCache *cache = &self->token_cache; + if ( + cache->token.ptr && cache->byte_index == position && + ts_subtree_external_scanner_state_eq(cache->last_external_token, last_external_token) + ) { + ts_language_table_entry(self->language, state, ts_subtree_symbol(cache->token), table_entry); + if (ts_parser__can_reuse_first_leaf(self, state, cache->token, table_entry)) { + ts_subtree_retain(cache->token); + return cache->token; + } + } + return NULL_SUBTREE; +} + +static void ts_parser__set_cached_token( + TSParser *self, + size_t byte_index, + Subtree last_external_token, + Subtree token +) { + TokenCache *cache = &self->token_cache; + if (token.ptr) ts_subtree_retain(token); + if (last_external_token.ptr) ts_subtree_retain(last_external_token); + if (cache->token.ptr) ts_subtree_release(&self->tree_pool, cache->token); + if (cache->last_external_token.ptr) ts_subtree_release(&self->tree_pool, cache->last_external_token); + cache->token = token; + cache->byte_index = byte_index; + cache->last_external_token = last_external_token; +} + +static bool ts_parser__has_included_range_difference( + const TSParser *self, + uint32_t start_position, + uint32_t end_position +) { + return ts_range_array_intersects( + &self->included_range_differences, + self->included_range_difference_index, + start_position, + end_position + ); +} + +static Subtree ts_parser__reuse_node( + TSParser *self, + StackVersion version, + TSStateId *state, + uint32_t position, + Subtree last_external_token, + TableEntry *table_entry +) { + Subtree result; + while ((result = reusable_node_tree(&self->reusable_node)).ptr) { + uint32_t byte_offset = reusable_node_byte_offset(&self->reusable_node); + uint32_t end_byte_offset = byte_offset + ts_subtree_total_bytes(result); + + if (byte_offset > position) { + LOG("before_reusable_node symbol:%s", TREE_NAME(result)); + break; + } + + if (byte_offset < position) { + LOG("past_reusable_node symbol:%s", TREE_NAME(result)); + if (end_byte_offset <= position || !reusable_node_descend(&self->reusable_node)) { + reusable_node_advance(&self->reusable_node); + } + continue; + } + + if (!ts_subtree_external_scanner_state_eq(self->reusable_node.last_external_token, last_external_token)) { + LOG("reusable_node_has_different_external_scanner_state symbol:%s", TREE_NAME(result)); + reusable_node_advance(&self->reusable_node); + continue; + } + + const char *reason = NULL; + if (ts_subtree_has_changes(result)) { + reason = "has_changes"; + } else if (ts_subtree_is_error(result)) { + reason = "is_error"; + } else if (ts_subtree_missing(result)) { + reason = "is_missing"; + } else if (ts_subtree_is_fragile(result)) { + reason = "is_fragile"; + } else if (ts_parser__has_included_range_difference(self, byte_offset, end_byte_offset)) { + reason = "contains_different_included_range"; + } + + if (reason) { + LOG("cant_reuse_node_%s tree:%s", reason, TREE_NAME(result)); + if (!reusable_node_descend(&self->reusable_node)) { + reusable_node_advance(&self->reusable_node); + ts_parser__breakdown_top_of_stack(self, version); + *state = ts_stack_state(self->stack, version); + } + continue; + } + + TSSymbol leaf_symbol = ts_subtree_leaf_symbol(result); + ts_language_table_entry(self->language, *state, leaf_symbol, table_entry); + if (!ts_parser__can_reuse_first_leaf(self, *state, result, table_entry)) { + LOG( + "cant_reuse_node symbol:%s, first_leaf_symbol:%s", + TREE_NAME(result), + SYM_NAME(leaf_symbol) + ); + reusable_node_advance_past_leaf(&self->reusable_node); + break; + } + + LOG("reuse_node symbol:%s", TREE_NAME(result)); + ts_subtree_retain(result); + return result; + } + + return NULL_SUBTREE; +} + +static bool ts_parser__select_tree(TSParser *self, Subtree left, Subtree right) { + if (!left.ptr) return true; + if (!right.ptr) return false; + + if (ts_subtree_error_cost(right) < ts_subtree_error_cost(left)) { + LOG("select_smaller_error symbol:%s, over_symbol:%s", TREE_NAME(right), TREE_NAME(left)); + return true; + } + + if (ts_subtree_error_cost(left) < ts_subtree_error_cost(right)) { + LOG("select_smaller_error symbol:%s, over_symbol:%s", TREE_NAME(left), TREE_NAME(right)); + return false; + } + + if (ts_subtree_dynamic_precedence(right) > ts_subtree_dynamic_precedence(left)) { + LOG("select_higher_precedence symbol:%s, prec:%u, over_symbol:%s, other_prec:%u", + TREE_NAME(right), ts_subtree_dynamic_precedence(right), TREE_NAME(left), + ts_subtree_dynamic_precedence(left)); + return true; + } + + if (ts_subtree_dynamic_precedence(left) > ts_subtree_dynamic_precedence(right)) { + LOG("select_higher_precedence symbol:%s, prec:%u, over_symbol:%s, other_prec:%u", + TREE_NAME(left), ts_subtree_dynamic_precedence(left), TREE_NAME(right), + ts_subtree_dynamic_precedence(right)); + return false; + } + + if (ts_subtree_error_cost(left) > 0) return true; + + int comparison = ts_subtree_compare(left, right); + switch (comparison) { + case -1: + LOG("select_earlier symbol:%s, over_symbol:%s", TREE_NAME(left), TREE_NAME(right)); + return false; + break; + case 1: + LOG("select_earlier symbol:%s, over_symbol:%s", TREE_NAME(right), TREE_NAME(left)); + return true; + default: + LOG("select_existing symbol:%s, over_symbol:%s", TREE_NAME(left), TREE_NAME(right)); + return false; + } +} + +static void ts_parser__shift( + TSParser *self, + StackVersion version, + TSStateId state, + Subtree lookahead, + bool extra +) { + Subtree subtree_to_push; + if (extra != ts_subtree_extra(lookahead)) { + MutableSubtree result = ts_subtree_make_mut(&self->tree_pool, lookahead); + ts_subtree_set_extra(&result); + subtree_to_push = ts_subtree_from_mut(result); + } else { + subtree_to_push = lookahead; + } + + bool is_pending = ts_subtree_child_count(subtree_to_push) > 0; + ts_stack_push(self->stack, version, subtree_to_push, is_pending, state); + if (ts_subtree_has_external_tokens(subtree_to_push)) { + ts_stack_set_last_external_token( + self->stack, version, ts_subtree_last_external_token(subtree_to_push) + ); + } +} + +static bool ts_parser__replace_children( + TSParser *self, + MutableSubtree *tree, + SubtreeArray *children +) { + *self->scratch_tree.ptr = *tree->ptr; + self->scratch_tree.ptr->child_count = 0; + ts_subtree_set_children(self->scratch_tree, children->contents, children->size, self->language); + if (ts_parser__select_tree(self, ts_subtree_from_mut(*tree), ts_subtree_from_mut(self->scratch_tree))) { + *tree->ptr = *self->scratch_tree.ptr; + return true; + } else { + return false; + } +} + +static StackVersion ts_parser__reduce( + TSParser *self, + StackVersion version, + TSSymbol symbol, + uint32_t count, + int dynamic_precedence, + uint16_t production_id, + bool fragile +) { + uint32_t initial_version_count = ts_stack_version_count(self->stack); + uint32_t removed_version_count = 0; + StackSliceArray pop = ts_stack_pop_count(self->stack, version, count); + + for (uint32_t i = 0; i < pop.size; i++) { + StackSlice slice = pop.contents[i]; + StackVersion slice_version = slice.version - removed_version_count; + + // Error recovery can sometimes cause lots of stack versions to merge, + // such that a single pop operation can produce a lots of slices. + // Avoid creating too many stack versions in that situation. + if (i > 0 && slice_version > MAX_VERSION_COUNT + MAX_VERSION_COUNT_OVERFLOW) { + ts_stack_remove_version(self->stack, slice_version); + ts_subtree_array_delete(&self->tree_pool, &slice.subtrees); + removed_version_count++; + while (i + 1 < pop.size) { + StackSlice next_slice = pop.contents[i + 1]; + if (next_slice.version != slice.version) break; + ts_subtree_array_delete(&self->tree_pool, &next_slice.subtrees); + i++; + } + continue; + } + + // Extra tokens on top of the stack should not be included in this new parent + // node. They will be re-pushed onto the stack after the parent node is + // created and pushed. + SubtreeArray children = slice.subtrees; + while (children.size > 0 && ts_subtree_extra(children.contents[children.size - 1])) { + children.size--; + } + + MutableSubtree parent = ts_subtree_new_node(&self->tree_pool, + symbol, &children, production_id, self->language + ); + + // This pop operation may have caused multiple stack versions to collapse + // into one, because they all diverged from a common state. In that case, + // choose one of the arrays of trees to be the parent node's children, and + // delete the rest of the tree arrays. + while (i + 1 < pop.size) { + StackSlice next_slice = pop.contents[i + 1]; + if (next_slice.version != slice.version) break; + i++; + + SubtreeArray children = next_slice.subtrees; + while (children.size > 0 && ts_subtree_extra(children.contents[children.size - 1])) { + children.size--; + } + + if (ts_parser__replace_children(self, &parent, &children)) { + ts_subtree_array_delete(&self->tree_pool, &slice.subtrees); + slice = next_slice; + } else { + ts_subtree_array_delete(&self->tree_pool, &next_slice.subtrees); + } + } + + parent.ptr->dynamic_precedence += dynamic_precedence; + parent.ptr->production_id = production_id; + + TSStateId state = ts_stack_state(self->stack, slice_version); + TSStateId next_state = ts_language_next_state(self->language, state, symbol); + if (fragile || pop.size > 1 || initial_version_count > 1) { + parent.ptr->fragile_left = true; + parent.ptr->fragile_right = true; + parent.ptr->parse_state = TS_TREE_STATE_NONE; + } else { + parent.ptr->parse_state = state; + } + + // Push the parent node onto the stack, along with any extra tokens that + // were previously on top of the stack. + ts_stack_push(self->stack, slice_version, ts_subtree_from_mut(parent), false, next_state); + for (uint32_t j = parent.ptr->child_count; j < slice.subtrees.size; j++) { + ts_stack_push(self->stack, slice_version, slice.subtrees.contents[j], false, next_state); + } + + for (StackVersion j = 0; j < slice_version; j++) { + if (j == version) continue; + if (ts_stack_merge(self->stack, j, slice_version)) { + removed_version_count++; + break; + } + } + } + + // Return the first new stack version that was created. + return ts_stack_version_count(self->stack) > initial_version_count + ? initial_version_count + : STACK_VERSION_NONE; +} + +static void ts_parser__accept( + TSParser *self, + StackVersion version, + Subtree lookahead +) { + assert(ts_subtree_is_eof(lookahead)); + ts_stack_push(self->stack, version, lookahead, false, 1); + + StackSliceArray pop = ts_stack_pop_all(self->stack, version); + for (uint32_t i = 0; i < pop.size; i++) { + SubtreeArray trees = pop.contents[i].subtrees; + + Subtree root = NULL_SUBTREE; + for (uint32_t j = trees.size - 1; j + 1 > 0; j--) { + Subtree child = trees.contents[j]; + if (!ts_subtree_extra(child)) { + assert(!child.data.is_inline); + uint32_t child_count = ts_subtree_child_count(child); + for (uint32_t k = 0; k < child_count; k++) { + ts_subtree_retain(child.ptr->children[k]); + } + array_splice(&trees, j, 1, child_count, child.ptr->children); + root = ts_subtree_from_mut(ts_subtree_new_node( + &self->tree_pool, + ts_subtree_symbol(child), + &trees, + child.ptr->production_id, + self->language + )); + ts_subtree_release(&self->tree_pool, child); + break; + } + } + + assert(root.ptr); + self->accept_count++; + + if (self->finished_tree.ptr) { + if (ts_parser__select_tree(self, self->finished_tree, root)) { + ts_subtree_release(&self->tree_pool, self->finished_tree); + self->finished_tree = root; + } else { + ts_subtree_release(&self->tree_pool, root); + } + } else { + self->finished_tree = root; + } + } + + ts_stack_remove_version(self->stack, pop.contents[0].version); + ts_stack_halt(self->stack, version); +} + +static bool ts_parser__do_all_potential_reductions( + TSParser *self, + StackVersion starting_version, + TSSymbol lookahead_symbol +) { + uint32_t initial_version_count = ts_stack_version_count(self->stack); + + bool can_shift_lookahead_symbol = false; + StackVersion version = starting_version; + for (unsigned i = 0; true; i++) { + uint32_t version_count = ts_stack_version_count(self->stack); + if (version >= version_count) break; + + bool merged = false; + for (StackVersion i = initial_version_count; i < version; i++) { + if (ts_stack_merge(self->stack, i, version)) { + merged = true; + break; + } + } + if (merged) continue; + + TSStateId state = ts_stack_state(self->stack, version); + bool has_shift_action = false; + array_clear(&self->reduce_actions); + + TSSymbol first_symbol, end_symbol; + if (lookahead_symbol != 0) { + first_symbol = lookahead_symbol; + end_symbol = lookahead_symbol + 1; + } else { + first_symbol = 1; + end_symbol = self->language->token_count; + } + + for (TSSymbol symbol = first_symbol; symbol < end_symbol; symbol++) { + TableEntry entry; + ts_language_table_entry(self->language, state, symbol, &entry); + for (uint32_t i = 0; i < entry.action_count; i++) { + TSParseAction action = entry.actions[i]; + switch (action.type) { + case TSParseActionTypeShift: + case TSParseActionTypeRecover: + if (!action.params.extra && !action.params.repetition) has_shift_action = true; + break; + case TSParseActionTypeReduce: + if (action.params.child_count > 0) + ts_reduce_action_set_add(&self->reduce_actions, (ReduceAction){ + .symbol = action.params.symbol, + .count = action.params.child_count, + .dynamic_precedence = action.params.dynamic_precedence, + .production_id = action.params.production_id, + }); + default: + break; + } + } + } + + StackVersion reduction_version = STACK_VERSION_NONE; + for (uint32_t i = 0; i < self->reduce_actions.size; i++) { + ReduceAction action = self->reduce_actions.contents[i]; + + reduction_version = ts_parser__reduce( + self, version, action.symbol, action.count, + action.dynamic_precedence, action.production_id, + true + ); + } + + if (has_shift_action) { + can_shift_lookahead_symbol = true; + } else if (reduction_version != STACK_VERSION_NONE && i < MAX_VERSION_COUNT) { + ts_stack_renumber_version(self->stack, reduction_version, version); + continue; + } else if (lookahead_symbol != 0) { + ts_stack_remove_version(self->stack, version); + } + + if (version == starting_version) { + version = version_count; + } else { + version++; + } + } + + return can_shift_lookahead_symbol; +} + +static void ts_parser__handle_error( + TSParser *self, + StackVersion version, + TSSymbol lookahead_symbol +) { + uint32_t previous_version_count = ts_stack_version_count(self->stack); + + // Perform any reductions that can happen in this state, regardless of the lookahead. After + // skipping one or more invalid tokens, the parser might find a token that would have allowed + // a reduction to take place. + ts_parser__do_all_potential_reductions(self, version, 0); + uint32_t version_count = ts_stack_version_count(self->stack); + Length position = ts_stack_position(self->stack, version); + + // Push a discontinuity onto the stack. Merge all of the stack versions that + // were created in the previous step. + bool did_insert_missing_token = false; + for (StackVersion v = version; v < version_count;) { + if (!did_insert_missing_token) { + TSStateId state = ts_stack_state(self->stack, v); + for (TSSymbol missing_symbol = 1; + missing_symbol < self->language->token_count; + missing_symbol++) { + TSStateId state_after_missing_symbol = ts_language_next_state( + self->language, state, missing_symbol + ); + if (state_after_missing_symbol == 0) continue; + + if (ts_language_has_reduce_action( + self->language, + state_after_missing_symbol, + lookahead_symbol + )) { + // In case the parser is currently outside of any included range, the lexer will + // snap to the beginning of the next included range. The missing token's padding + // must be assigned to position it within the next included range. + ts_lexer_reset(&self->lexer, position); + ts_lexer_mark_end(&self->lexer); + Length padding = length_sub(self->lexer.token_end_position, position); + + StackVersion version_with_missing_tree = ts_stack_copy_version(self->stack, v); + Subtree missing_tree = ts_subtree_new_missing_leaf( + &self->tree_pool, missing_symbol, padding, self->language + ); + ts_stack_push( + self->stack, version_with_missing_tree, + missing_tree, false, + state_after_missing_symbol + ); + + if (ts_parser__do_all_potential_reductions( + self, version_with_missing_tree, + lookahead_symbol + )) { + LOG( + "recover_with_missing symbol:%s, state:%u", + SYM_NAME(missing_symbol), + ts_stack_state(self->stack, version_with_missing_tree) + ); + did_insert_missing_token = true; + break; + } + } + } + } + + ts_stack_push(self->stack, v, NULL_SUBTREE, false, ERROR_STATE); + v = (v == version) ? previous_version_count : v + 1; + } + + for (unsigned i = previous_version_count; i < version_count; i++) { + bool did_merge = ts_stack_merge(self->stack, version, previous_version_count); + assert(did_merge); + } + + ts_stack_record_summary(self->stack, version, MAX_SUMMARY_DEPTH); + LOG_STACK(); +} + +static void ts_parser__halt_parse(TSParser *self) { + LOG("halting_parse"); + LOG_STACK(); + + ts_lexer_advance_to_end(&self->lexer); + Length remaining_length = length_sub( + self->lexer.current_position, + ts_stack_position(self->stack, 0) + ); + + Subtree filler_node = ts_subtree_new_error( + &self->tree_pool, + 0, + length_zero(), + remaining_length, + remaining_length.bytes, + 0, + self->language + ); + ts_subtree_to_mut_unsafe(filler_node).ptr->visible = false; + ts_stack_push(self->stack, 0, filler_node, false, 0); + + SubtreeArray children = array_new(); + Subtree root_error = ts_subtree_new_error_node(&self->tree_pool, &children, false, self->language); + ts_stack_push(self->stack, 0, root_error, false, 0); + + Subtree eof = ts_subtree_new_leaf( + &self->tree_pool, + ts_builtin_sym_end, + length_zero(), + length_zero(), + 0, + 0, + false, + false, + self->language + ); + ts_parser__accept(self, 0, eof); +} + +static bool ts_parser__recover_to_state( + TSParser *self, + StackVersion version, + unsigned depth, + TSStateId goal_state +) { + StackSliceArray pop = ts_stack_pop_count(self->stack, version, depth); + StackVersion previous_version = STACK_VERSION_NONE; + + for (unsigned i = 0; i < pop.size; i++) { + StackSlice slice = pop.contents[i]; + + if (slice.version == previous_version) { + ts_subtree_array_delete(&self->tree_pool, &slice.subtrees); + array_erase(&pop, i--); + continue; + } + + if (ts_stack_state(self->stack, slice.version) != goal_state) { + ts_stack_halt(self->stack, slice.version); + ts_subtree_array_delete(&self->tree_pool, &slice.subtrees); + array_erase(&pop, i--); + continue; + } + + SubtreeArray error_trees = ts_stack_pop_error(self->stack, slice.version); + if (error_trees.size > 0) { + assert(error_trees.size == 1); + Subtree error_tree = error_trees.contents[0]; + uint32_t error_child_count = ts_subtree_child_count(error_tree); + if (error_child_count > 0) { + array_splice(&slice.subtrees, 0, 0, error_child_count, error_tree.ptr->children); + for (unsigned j = 0; j < error_child_count; j++) { + ts_subtree_retain(slice.subtrees.contents[j]); + } + } + ts_subtree_array_delete(&self->tree_pool, &error_trees); + } + + SubtreeArray trailing_extras = ts_subtree_array_remove_trailing_extras(&slice.subtrees); + + if (slice.subtrees.size > 0) { + Subtree error = ts_subtree_new_error_node(&self->tree_pool, &slice.subtrees, true, self->language); + ts_stack_push(self->stack, slice.version, error, false, goal_state); + } else { + array_delete(&slice.subtrees); + } + + for (unsigned j = 0; j < trailing_extras.size; j++) { + Subtree tree = trailing_extras.contents[j]; + ts_stack_push(self->stack, slice.version, tree, false, goal_state); + } + + previous_version = slice.version; + array_delete(&trailing_extras); + } + + return previous_version != STACK_VERSION_NONE; +} + +static void ts_parser__recover( + TSParser *self, + StackVersion version, + Subtree lookahead +) { + bool did_recover = false; + unsigned previous_version_count = ts_stack_version_count(self->stack); + Length position = ts_stack_position(self->stack, version); + StackSummary *summary = ts_stack_get_summary(self->stack, version); + unsigned node_count_since_error = ts_stack_node_count_since_error(self->stack, version); + unsigned current_error_cost = ts_stack_error_cost(self->stack, version); + + // When the parser is in the error state, there are two strategies for recovering with a + // given lookahead token: + // 1. Find a previous state on the stack in which that lookahead token would be valid. Then, + // create a new stack version that is in that state again. This entails popping all of the + // subtrees that have been pushed onto the stack since that previous state, and wrapping + // them in an ERROR node. + // 2. Wrap the lookahead token in an ERROR node, push that ERROR node onto the stack, and + // move on to the next lookahead token, remaining in the error state. + // + // First, try the strategy 1. Upon entering the error state, the parser recorded a summary + // of the previous parse states and their depths. Look at each state in the summary, to see + // if the current lookahead token would be valid in that state. + if (summary && !ts_subtree_is_error(lookahead)) { + for (unsigned i = 0; i < summary->size; i++) { + StackSummaryEntry entry = summary->contents[i]; + + if (entry.state == ERROR_STATE) continue; + if (entry.position.bytes == position.bytes) continue; + unsigned depth = entry.depth; + if (node_count_since_error > 0) depth++; + + // Do not recover in ways that create redundant stack versions. + bool would_merge = false; + for (unsigned j = 0; j < previous_version_count; j++) { + if ( + ts_stack_state(self->stack, j) == entry.state && + ts_stack_position(self->stack, j).bytes == position.bytes + ) { + would_merge = true; + break; + } + } + if (would_merge) continue; + + // Do not recover if the result would clearly be worse than some existing stack version. + unsigned new_cost = + current_error_cost + + entry.depth * ERROR_COST_PER_SKIPPED_TREE + + (position.bytes - entry.position.bytes) * ERROR_COST_PER_SKIPPED_CHAR + + (position.extent.row - entry.position.extent.row) * ERROR_COST_PER_SKIPPED_LINE; + if (ts_parser__better_version_exists(self, version, false, new_cost)) break; + + // If the current lookahead token is valid in some previous state, recover to that state. + // Then stop looking for further recoveries. + if (ts_language_has_actions(self->language, entry.state, ts_subtree_symbol(lookahead))) { + if (ts_parser__recover_to_state(self, version, depth, entry.state)) { + did_recover = true; + LOG("recover_to_previous state:%u, depth:%u", entry.state, depth); + LOG_STACK(); + break; + } + } + } + } + + // In the process of attemping to recover, some stack versions may have been created + // and subsequently halted. Remove those versions. + for (unsigned i = previous_version_count; i < ts_stack_version_count(self->stack); i++) { + if (!ts_stack_is_active(self->stack, i)) { + ts_stack_remove_version(self->stack, i--); + } + } + + // If strategy 1 succeeded, a new stack version will have been created which is able to handle + // the current lookahead token. Now, in addition, try strategy 2 described above: skip the + // current lookahead token by wrapping it in an ERROR node. + + // Don't pursue this additional strategy if there are already too many stack versions. + if (did_recover && ts_stack_version_count(self->stack) > MAX_VERSION_COUNT) { + ts_stack_halt(self->stack, version); + ts_subtree_release(&self->tree_pool, lookahead); + return; + } + + // If the parser is still in the error state at the end of the file, just wrap everything + // in an ERROR node and terminate. + if (ts_subtree_is_eof(lookahead)) { + LOG("recover_eof"); + SubtreeArray children = array_new(); + Subtree parent = ts_subtree_new_error_node(&self->tree_pool, &children, false, self->language); + ts_stack_push(self->stack, version, parent, false, 1); + ts_parser__accept(self, version, lookahead); + return; + } + + // Do not recover if the result would clearly be worse than some existing stack version. + unsigned new_cost = + current_error_cost + ERROR_COST_PER_SKIPPED_TREE + + ts_subtree_total_bytes(lookahead) * ERROR_COST_PER_SKIPPED_CHAR + + ts_subtree_total_size(lookahead).extent.row * ERROR_COST_PER_SKIPPED_LINE; + if (ts_parser__better_version_exists(self, version, false, new_cost)) { + ts_stack_halt(self->stack, version); + ts_subtree_release(&self->tree_pool, lookahead); + return; + } + + // If the current lookahead token is an extra token, mark it as extra. This means it won't + // be counted in error cost calculations. + unsigned n; + const TSParseAction *actions = ts_language_actions(self->language, 1, ts_subtree_symbol(lookahead), &n); + if (n > 0 && actions[n - 1].type == TSParseActionTypeShift && actions[n - 1].params.extra) { + MutableSubtree mutable_lookahead = ts_subtree_make_mut(&self->tree_pool, lookahead); + ts_subtree_set_extra(&mutable_lookahead); + lookahead = ts_subtree_from_mut(mutable_lookahead); + } + + // Wrap the lookahead token in an ERROR. + LOG("skip_token symbol:%s", TREE_NAME(lookahead)); + SubtreeArray children = array_new(); + array_reserve(&children, 1); + array_push(&children, lookahead); + MutableSubtree error_repeat = ts_subtree_new_node( + &self->tree_pool, + ts_builtin_sym_error_repeat, + &children, + 0, + self->language + ); + + // If other tokens have already been skipped, so there is already an ERROR at the top of the + // stack, then pop that ERROR off the stack and wrap the two ERRORs together into one larger + // ERROR. + if (node_count_since_error > 0) { + StackSliceArray pop = ts_stack_pop_count(self->stack, version, 1); + + // TODO: Figure out how to make this condition occur. + // See https://github.com/atom/atom/issues/18450#issuecomment-439579778 + // If multiple stack versions have merged at this point, just pick one of the errors + // arbitrarily and discard the rest. + if (pop.size > 1) { + for (unsigned i = 1; i < pop.size; i++) { + ts_subtree_array_delete(&self->tree_pool, &pop.contents[i].subtrees); + } + while (ts_stack_version_count(self->stack) > pop.contents[0].version + 1) { + ts_stack_remove_version(self->stack, pop.contents[0].version + 1); + } + } + + ts_stack_renumber_version(self->stack, pop.contents[0].version, version); + array_push(&pop.contents[0].subtrees, ts_subtree_from_mut(error_repeat)); + error_repeat = ts_subtree_new_node( + &self->tree_pool, + ts_builtin_sym_error_repeat, + &pop.contents[0].subtrees, + 0, + self->language + ); + } + + // Push the new ERROR onto the stack. + ts_stack_push(self->stack, version, ts_subtree_from_mut(error_repeat), false, ERROR_STATE); + if (ts_subtree_has_external_tokens(lookahead)) { + ts_stack_set_last_external_token( + self->stack, version, ts_subtree_last_external_token(lookahead) + ); + } +} + +static bool ts_parser__advance( + TSParser *self, + StackVersion version, + bool allow_node_reuse +) { + TSStateId state = ts_stack_state(self->stack, version); + uint32_t position = ts_stack_position(self->stack, version).bytes; + Subtree last_external_token = ts_stack_last_external_token(self->stack, version); + + bool did_reuse = true; + Subtree lookahead = NULL_SUBTREE; + TableEntry table_entry = {.action_count = 0}; + + // If possible, reuse a node from the previous syntax tree. + if (allow_node_reuse) { + lookahead = ts_parser__reuse_node( + self, version, &state, position, last_external_token, &table_entry + ); + } + + // If no node from the previous syntax tree could be reused, then try to + // reuse the token previously returned by the lexer. + if (!lookahead.ptr) { + did_reuse = false; + lookahead = ts_parser__get_cached_token( + self, state, position, last_external_token, &table_entry + ); + } + + // Otherwise, re-run the lexer. + if (!lookahead.ptr) { + lookahead = ts_parser__lex(self, version, state); + ts_parser__set_cached_token(self, position, last_external_token, lookahead); + ts_language_table_entry(self->language, state, ts_subtree_symbol(lookahead), &table_entry); + } + + for (;;) { + // If a cancellation flag or a timeout was provided, then check every + // time a fixed number of parse actions has been processed. + if (++self->operation_count == OP_COUNT_PER_TIMEOUT_CHECK) { + self->operation_count = 0; + } + if ( + self->operation_count == 0 && + ((self->cancellation_flag && atomic_load(self->cancellation_flag)) || + (!clock_is_null(self->end_clock) && clock_is_gt(clock_now(), self->end_clock))) + ) { + ts_subtree_release(&self->tree_pool, lookahead); + return false; + } + + // Process each parse action for the current lookahead token in + // the current state. If there are multiple actions, then this is + // an ambiguous state. REDUCE actions always create a new stack + // version, whereas SHIFT actions update the existing stack version + // and terminate this loop. + StackVersion last_reduction_version = STACK_VERSION_NONE; + for (uint32_t i = 0; i < table_entry.action_count; i++) { + TSParseAction action = table_entry.actions[i]; + + switch (action.type) { + case TSParseActionTypeShift: { + if (action.params.repetition) break; + TSStateId next_state; + if (action.params.extra) { + + // TODO: remove when TREE_SITTER_LANGUAGE_VERSION 9 is out. + if (state == ERROR_STATE) continue; + + next_state = state; + LOG("shift_extra"); + } else { + next_state = action.params.state; + LOG("shift state:%u", next_state); + } + + if (ts_subtree_child_count(lookahead) > 0) { + ts_parser__breakdown_lookahead(self, &lookahead, state, &self->reusable_node); + next_state = ts_language_next_state(self->language, state, ts_subtree_symbol(lookahead)); + } + + ts_parser__shift(self, version, next_state, lookahead, action.params.extra); + if (did_reuse) reusable_node_advance(&self->reusable_node); + return true; + } + + case TSParseActionTypeReduce: { + bool is_fragile = table_entry.action_count > 1; + LOG("reduce sym:%s, child_count:%u", SYM_NAME(action.params.symbol), action.params.child_count); + StackVersion reduction_version = ts_parser__reduce( + self, version, action.params.symbol, action.params.child_count, + action.params.dynamic_precedence, action.params.production_id, + is_fragile + ); + if (reduction_version != STACK_VERSION_NONE) { + last_reduction_version = reduction_version; + } + break; + } + + case TSParseActionTypeAccept: { + LOG("accept"); + ts_parser__accept(self, version, lookahead); + return true; + } + + case TSParseActionTypeRecover: { + if (ts_subtree_child_count(lookahead) > 0) { + ts_parser__breakdown_lookahead(self, &lookahead, ERROR_STATE, &self->reusable_node); + } + + ts_parser__recover(self, version, lookahead); + if (did_reuse) reusable_node_advance(&self->reusable_node); + return true; + } + } + } + + // If a reduction was performed, then replace the current stack version + // with one of the stack versions created by a reduction, and continue + // processing this version of the stack with the same lookahead symbol. + if (last_reduction_version != STACK_VERSION_NONE) { + ts_stack_renumber_version(self->stack, last_reduction_version, version); + LOG_STACK(); + state = ts_stack_state(self->stack, version); + ts_language_table_entry( + self->language, + state, + ts_subtree_leaf_symbol(lookahead), + &table_entry + ); + continue; + } + + // If there were no parse actions for the current lookahead token, then + // it is not valid in this state. If the current lookahead token is a + // keyword, then switch to treating it as the normal word token if that + // token is valid in this state. + if ( + ts_subtree_is_keyword(lookahead) && + ts_subtree_symbol(lookahead) != self->language->keyword_capture_token + ) { + ts_language_table_entry(self->language, state, self->language->keyword_capture_token, &table_entry); + if (table_entry.action_count > 0) { + LOG( + "switch from_keyword:%s, to_word_token:%s", + TREE_NAME(lookahead), + SYM_NAME(self->language->keyword_capture_token) + ); + + MutableSubtree mutable_lookahead = ts_subtree_make_mut(&self->tree_pool, lookahead); + ts_subtree_set_symbol(&mutable_lookahead, self->language->keyword_capture_token, self->language); + lookahead = ts_subtree_from_mut(mutable_lookahead); + continue; + } + } + + // If the current lookahead token is not valid and the parser is + // already in the error state, restart the error recovery process. + // TODO - can this be unified with the other `RECOVER` case above? + if (state == ERROR_STATE) { + ts_parser__recover(self, version, lookahead); + return true; + } + + // If the current lookahead token is not valid and the previous + // subtree on the stack was reused from an old tree, it isn't actually + // valid to reuse it. Remove it from the stack, and in its place, + // push each of its children. Then try again to process the current + // lookahead. + if (ts_parser__breakdown_top_of_stack(self, version)) { + continue; + } + + // At this point, the current lookahead token is definitely not valid + // for this parse stack version. Mark this version as paused and continue + // processing any other stack versions that might exist. If some other + // version advances successfully, then this version can simply be removed. + // But if all versions end up paused, then error recovery is needed. + LOG("detect_error"); + ts_stack_pause(self->stack, version, ts_subtree_leaf_symbol(lookahead)); + ts_subtree_release(&self->tree_pool, lookahead); + return true; + } +} + +static unsigned ts_parser__condense_stack(TSParser *self) { + bool made_changes = false; + unsigned min_error_cost = UINT_MAX; + for (StackVersion i = 0; i < ts_stack_version_count(self->stack); i++) { + // Prune any versions that have been marked for removal. + if (ts_stack_is_halted(self->stack, i)) { + ts_stack_remove_version(self->stack, i); + i--; + continue; + } + + // Keep track of the minimum error cost of any stack version so + // that it can be returned. + ErrorStatus status_i = ts_parser__version_status(self, i); + if (!status_i.is_in_error && status_i.cost < min_error_cost) { + min_error_cost = status_i.cost; + } + + // Examine each pair of stack versions, removing any versions that + // are clearly worse than another version. Ensure that the versions + // are ordered from most promising to least promising. + for (StackVersion j = 0; j < i; j++) { + ErrorStatus status_j = ts_parser__version_status(self, j); + + switch (ts_parser__compare_versions(self, status_j, status_i)) { + case ErrorComparisonTakeLeft: + made_changes = true; + ts_stack_remove_version(self->stack, i); + i--; + j = i; + break; + + case ErrorComparisonPreferLeft: + case ErrorComparisonNone: + if (ts_stack_merge(self->stack, j, i)) { + made_changes = true; + i--; + j = i; + } + break; + + case ErrorComparisonPreferRight: + made_changes = true; + if (ts_stack_merge(self->stack, j, i)) { + i--; + j = i; + } else { + ts_stack_swap_versions(self->stack, i, j); + } + break; + + case ErrorComparisonTakeRight: + made_changes = true; + ts_stack_remove_version(self->stack, j); + i--; + j--; + break; + } + } + } + + // Enfore a hard upper bound on the number of stack versions by + // discarding the least promising versions. + while (ts_stack_version_count(self->stack) > MAX_VERSION_COUNT) { + ts_stack_remove_version(self->stack, MAX_VERSION_COUNT); + made_changes = true; + } + + // If the best-performing stack version is currently paused, or all + // versions are paused, then resume the best paused version and begin + // the error recovery process. Otherwise, remove the paused versions. + if (ts_stack_version_count(self->stack) > 0) { + bool has_unpaused_version = false; + for (StackVersion i = 0, n = ts_stack_version_count(self->stack); i < n; i++) { + if (ts_stack_is_paused(self->stack, i)) { + if (!has_unpaused_version && self->accept_count < MAX_VERSION_COUNT) { + LOG("resume version:%u", i); + min_error_cost = ts_stack_error_cost(self->stack, i); + TSSymbol lookahead_symbol = ts_stack_resume(self->stack, i); + ts_parser__handle_error(self, i, lookahead_symbol); + has_unpaused_version = true; + } else { + ts_stack_remove_version(self->stack, i); + i--; + n--; + } + } else { + has_unpaused_version = true; + } + } + } + + if (made_changes) { + LOG("condense"); + LOG_STACK(); + } + + return min_error_cost; +} + +static bool ts_parser_has_outstanding_parse(TSParser *self) { + return ( + self->lexer.current_position.bytes > 0 || + ts_stack_state(self->stack, 0) != 1 + ); +} + +// Parser - Public + +TSParser *ts_parser_new(void) { + TSParser *self = ts_calloc(1, sizeof(TSParser)); + ts_lexer_init(&self->lexer); + array_init(&self->reduce_actions); + array_reserve(&self->reduce_actions, 4); + self->tree_pool = ts_subtree_pool_new(32); + self->stack = ts_stack_new(&self->tree_pool); + self->finished_tree = NULL_SUBTREE; + self->reusable_node = reusable_node_new(); + self->dot_graph_file = NULL; + self->halt_on_error = false; + self->cancellation_flag = NULL; + self->timeout_duration = 0; + self->end_clock = clock_null(); + self->operation_count = 0; + self->old_tree = NULL_SUBTREE; + self->scratch_tree.ptr = &self->scratch_tree_data; + self->included_range_differences = (TSRangeArray) array_new(); + self->included_range_difference_index = 0; + ts_parser__set_cached_token(self, 0, NULL_SUBTREE, NULL_SUBTREE); + return self; +} + +void ts_parser_delete(TSParser *self) { + if (!self) return; + + ts_stack_delete(self->stack); + if (self->reduce_actions.contents) { + array_delete(&self->reduce_actions); + } + if (self->included_range_differences.contents) { + array_delete(&self->included_range_differences); + } + if (self->old_tree.ptr) { + ts_subtree_release(&self->tree_pool, self->old_tree); + self->old_tree = NULL_SUBTREE; + } + ts_lexer_delete(&self->lexer); + ts_parser__set_cached_token(self, 0, NULL_SUBTREE, NULL_SUBTREE); + ts_subtree_pool_delete(&self->tree_pool); + reusable_node_delete(&self->reusable_node); + ts_parser_set_language(self, NULL); + ts_free(self); +} + +const TSLanguage *ts_parser_language(const TSParser *self) { + return self->language; +} + +bool ts_parser_set_language(TSParser *self, const TSLanguage *language) { + if (language) { + if (language->version > TREE_SITTER_LANGUAGE_VERSION) return false; + if (language->version < TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION) return false; + } + + if (self->external_scanner_payload && self->language->external_scanner.destroy) { + self->language->external_scanner.destroy(self->external_scanner_payload); + } + + if (language && language->external_scanner.create) { + self->external_scanner_payload = language->external_scanner.create(); + } else { + self->external_scanner_payload = NULL; + } + + self->language = language; + return true; +} + +TSLogger ts_parser_logger(const TSParser *self) { + return self->lexer.logger; +} + +void ts_parser_set_logger(TSParser *self, TSLogger logger) { + self->lexer.logger = logger; +} + +void ts_parser_print_dot_graphs(TSParser *self, int fd) { + if (self->dot_graph_file) { + fclose(self->dot_graph_file); + } + + if (fd >= 0) { + self->dot_graph_file = fdopen(fd, "a"); + } else { + self->dot_graph_file = NULL; + } +} + +void ts_parser_halt_on_error(TSParser *self, bool should_halt_on_error) { + self->halt_on_error = should_halt_on_error; +} + +const size_t *ts_parser_cancellation_flag(const TSParser *self) { + return (const size_t *)self->cancellation_flag; +} + +void ts_parser_set_cancellation_flag(TSParser *self, const size_t *flag) { + self->cancellation_flag = (const volatile size_t *)flag; +} + +uint64_t ts_parser_timeout_micros(const TSParser *self) { + return duration_to_micros(self->timeout_duration); +} + +void ts_parser_set_timeout_micros(TSParser *self, uint64_t timeout_micros) { + self->timeout_duration = duration_from_micros(timeout_micros); +} + +void ts_parser_set_included_ranges(TSParser *self, const TSRange *ranges, uint32_t count) { + ts_lexer_set_included_ranges(&self->lexer, ranges, count); +} + +const TSRange *ts_parser_included_ranges(const TSParser *self, uint32_t *count) { + return ts_lexer_included_ranges(&self->lexer, count); +} + +void ts_parser_reset(TSParser *self) { + if (self->language->external_scanner.deserialize) { + self->language->external_scanner.deserialize(self->external_scanner_payload, NULL, 0); + } + + if (self->old_tree.ptr) { + ts_subtree_release(&self->tree_pool, self->old_tree); + self->old_tree = NULL_SUBTREE; + } + + reusable_node_clear(&self->reusable_node); + ts_lexer_reset(&self->lexer, length_zero()); + ts_stack_clear(self->stack); + ts_parser__set_cached_token(self, 0, NULL_SUBTREE, NULL_SUBTREE); + if (self->finished_tree.ptr) { + ts_subtree_release(&self->tree_pool, self->finished_tree); + self->finished_tree = NULL_SUBTREE; + } + self->accept_count = 0; +} + +TSTree *ts_parser_parse( + TSParser *self, + const TSTree *old_tree, + TSInput input +) { + if (!self->language || !input.read) return NULL; + + ts_lexer_set_input(&self->lexer, input); + + array_clear(&self->included_range_differences); + self->included_range_difference_index = 0; + + if (ts_parser_has_outstanding_parse(self)) { + LOG("resume_parsing"); + } else if (old_tree) { + ts_subtree_retain(old_tree->root); + self->old_tree = old_tree->root; + ts_range_array_get_changed_ranges( + old_tree->included_ranges, old_tree->included_range_count, + self->lexer.included_ranges, self->lexer.included_range_count, + &self->included_range_differences + ); + reusable_node_reset(&self->reusable_node, old_tree->root); + LOG("parse_after_edit"); + LOG_TREE(self->old_tree); + for (unsigned i = 0; i < self->included_range_differences.size; i++) { + TSRange *range = &self->included_range_differences.contents[i]; + LOG("different_included_range %u - %u", range->start_byte, range->end_byte); + } + } else { + reusable_node_clear(&self->reusable_node); + LOG("new_parse"); + } + + uint32_t position = 0, last_position = 0, version_count = 0; + self->operation_count = 0; + if (self->timeout_duration) { + self->end_clock = clock_after(clock_now(), self->timeout_duration); + } else { + self->end_clock = clock_null(); + } + + do { + for (StackVersion version = 0; + version_count = ts_stack_version_count(self->stack), version < version_count; + version++) { + bool allow_node_reuse = version_count == 1; + while (ts_stack_is_active(self->stack, version)) { + LOG("process version:%d, version_count:%u, state:%d, row:%u, col:%u", + version, ts_stack_version_count(self->stack), + ts_stack_state(self->stack, version), + ts_stack_position(self->stack, version).extent.row + 1, + ts_stack_position(self->stack, version).extent.column); + + if (!ts_parser__advance(self, version, allow_node_reuse)) return NULL; + LOG_STACK(); + + position = ts_stack_position(self->stack, version).bytes; + if (position > last_position || (version > 0 && position == last_position)) { + last_position = position; + break; + } + } + } + + unsigned min_error_cost = ts_parser__condense_stack(self); + if (self->finished_tree.ptr && ts_subtree_error_cost(self->finished_tree) < min_error_cost) { + break; + } else if (self->halt_on_error && min_error_cost > 0) { + ts_parser__halt_parse(self); + break; + } + + while (self->included_range_difference_index < self->included_range_differences.size) { + TSRange *range = &self->included_range_differences.contents[self->included_range_difference_index]; + if (range->end_byte <= position) { + self->included_range_difference_index++; + } else { + break; + } + } + } while (version_count != 0); + + ts_subtree_balance(self->finished_tree, &self->tree_pool, self->language); + LOG("done"); + LOG_TREE(self->finished_tree); + + TSTree *result = ts_tree_new( + self->finished_tree, + self->language, + self->lexer.included_ranges, + self->lexer.included_range_count + ); + self->finished_tree = NULL_SUBTREE; + ts_parser_reset(self); + return result; +} + +TSTree *ts_parser_parse_string( + TSParser *self, + const TSTree *old_tree, + const char *string, + uint32_t length +) { + return ts_parser_parse_string_encoding(self, old_tree, string, length, TSInputEncodingUTF8); +} + +TSTree *ts_parser_parse_string_encoding(TSParser *self, const TSTree *old_tree, + const char *string, uint32_t length, TSInputEncoding encoding) { + TSStringInput input = {string, length}; + return ts_parser_parse(self, old_tree, (TSInput) { + &input, + ts_string_input_read, + encoding, + }); +} + +#undef LOG diff --git a/src/tree_sitter/parser.h b/src/tree_sitter/parser.h new file mode 100644 index 0000000000..974a7ca52f --- /dev/null +++ b/src/tree_sitter/parser.h @@ -0,0 +1,220 @@ +#ifndef TREE_SITTER_PARSER_H_ +#define TREE_SITTER_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#define ts_builtin_sym_error ((TSSymbol)-1) +#define ts_builtin_sym_end 0 +#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 + +#ifndef TREE_SITTER_API_H_ +typedef uint16_t TSSymbol; +typedef uint16_t TSFieldId; +typedef struct TSLanguage TSLanguage; +#endif + +typedef struct { + TSFieldId field_id; + uint8_t child_index; + bool inherited; +} TSFieldMapEntry; + +typedef struct { + uint16_t index; + uint16_t length; +} TSFieldMapSlice; + +typedef uint16_t TSStateId; + +typedef struct { + bool visible : 1; + bool named : 1; +} TSSymbolMetadata; + +typedef struct TSLexer TSLexer; + +struct TSLexer { + int32_t lookahead; + TSSymbol result_symbol; + void (*advance)(TSLexer *, bool); + void (*mark_end)(TSLexer *); + uint32_t (*get_column)(TSLexer *); + bool (*is_at_included_range_start)(TSLexer *); +}; + +typedef enum { + TSParseActionTypeShift, + TSParseActionTypeReduce, + TSParseActionTypeAccept, + TSParseActionTypeRecover, +} TSParseActionType; + +typedef struct { + union { + struct { + TSStateId state; + bool extra : 1; + bool repetition : 1; + }; + struct { + TSSymbol symbol; + int16_t dynamic_precedence; + uint8_t child_count; + uint8_t production_id; + }; + } params; + TSParseActionType type : 4; +} TSParseAction; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; +} TSLexMode; + +typedef union { + TSParseAction action; + struct { + uint8_t count; + bool reusable : 1; + }; +} TSParseActionEntry; + +struct TSLanguage { + uint32_t version; + uint32_t symbol_count; + uint32_t alias_count; + uint32_t token_count; + uint32_t external_token_count; + const char **symbol_names; + const TSSymbolMetadata *symbol_metadata; + const uint16_t *parse_table; + const TSParseActionEntry *parse_actions; + const TSLexMode *lex_modes; + const TSSymbol *alias_sequences; + uint16_t max_alias_sequence_length; + bool (*lex_fn)(TSLexer *, TSStateId); + bool (*keyword_lex_fn)(TSLexer *, TSStateId); + TSSymbol keyword_capture_token; + struct { + const bool *states; + const TSSymbol *symbol_map; + void *(*create)(void); + void (*destroy)(void *); + bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist); + unsigned (*serialize)(void *, char *); + void (*deserialize)(void *, const char *, unsigned); + } external_scanner; + uint32_t field_count; + const TSFieldMapSlice *field_map_slices; + const TSFieldMapEntry *field_map_entries; + const char **field_names; + uint32_t large_state_count; + const uint16_t *small_parse_table; + const uint32_t *small_parse_table_map; +}; + +/* + * Lexer Macros + */ + +#define START_LEXER() \ + bool result = false; \ + bool skip = false; \ + int32_t lookahead; \ + goto start; \ + next_state: \ + lexer->advance(lexer, skip); \ + start: \ + skip = false; \ + lookahead = lexer->lookahead; + +#define ADVANCE(state_value) \ + { \ + state = state_value; \ + goto next_state; \ + } + +#define SKIP(state_value) \ + { \ + skip = true; \ + state = state_value; \ + goto next_state; \ + } + +#define ACCEPT_TOKEN(symbol_value) \ + result = true; \ + lexer->result_symbol = symbol_value; \ + lexer->mark_end(lexer); + +#define END_STATE() return result; + +/* + * Parse Table Macros + */ + +#define SMALL_STATE(id) id - LARGE_STATE_COUNT + +#define STATE(id) id + +#define ACTIONS(id) id + +#define SHIFT(state_value) \ + { \ + { \ + .type = TSParseActionTypeShift, \ + .params = {.state = state_value}, \ + } \ + } + +#define SHIFT_REPEAT(state_value) \ + { \ + { \ + .type = TSParseActionTypeShift, \ + .params = { \ + .state = state_value, \ + .repetition = true \ + }, \ + } \ + } + +#define RECOVER() \ + { \ + { .type = TSParseActionTypeRecover } \ + } + +#define SHIFT_EXTRA() \ + { \ + { \ + .type = TSParseActionTypeShift, \ + .params = {.extra = true} \ + } \ + } + +#define REDUCE(symbol_val, child_count_val, ...) \ + { \ + { \ + .type = TSParseActionTypeReduce, \ + .params = { \ + .symbol = symbol_val, \ + .child_count = child_count_val, \ + __VA_ARGS__ \ + } \ + } \ + } + +#define ACCEPT_INPUT() \ + { \ + { .type = TSParseActionTypeAccept } \ + } + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_PARSER_H_ diff --git a/src/tree_sitter/point.h b/src/tree_sitter/point.h new file mode 100644 index 0000000000..4d0aed18ef --- /dev/null +++ b/src/tree_sitter/point.h @@ -0,0 +1,53 @@ +#ifndef TREE_SITTER_POINT_H_ +#define TREE_SITTER_POINT_H_ + +#include "tree_sitter/api.h" + +#define POINT_MAX ((TSPoint) {UINT32_MAX, UINT32_MAX}) + +static inline TSPoint point__new(unsigned row, unsigned column) { + TSPoint result = {row, column}; + return result; +} + +static inline TSPoint point_add(TSPoint a, TSPoint b) { + if (b.row > 0) + return point__new(a.row + b.row, b.column); + else + return point__new(a.row, a.column + b.column); +} + +static inline TSPoint point_sub(TSPoint a, TSPoint b) { + if (a.row > b.row) + return point__new(a.row - b.row, a.column); + else + return point__new(0, a.column - b.column); +} + +static inline bool point_lte(TSPoint a, TSPoint b) { + return (a.row < b.row) || (a.row == b.row && a.column <= b.column); +} + +static inline bool point_lt(TSPoint a, TSPoint b) { + return (a.row < b.row) || (a.row == b.row && a.column < b.column); +} + +static inline bool point_eq(TSPoint a, TSPoint b) { + return a.row == b.row && a.column == b.column; +} + +static inline TSPoint point_min(TSPoint a, TSPoint b) { + if (a.row < b.row || (a.row == b.row && a.column < b.column)) + return a; + else + return b; +} + +static inline TSPoint point_max(TSPoint a, TSPoint b) { + if (a.row > b.row || (a.row == b.row && a.column > b.column)) + return a; + else + return b; +} + +#endif diff --git a/src/tree_sitter/reduce_action.h b/src/tree_sitter/reduce_action.h new file mode 100644 index 0000000000..72aff08d73 --- /dev/null +++ b/src/tree_sitter/reduce_action.h @@ -0,0 +1,34 @@ +#ifndef TREE_SITTER_REDUCE_ACTION_H_ +#define TREE_SITTER_REDUCE_ACTION_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./array.h" +#include "tree_sitter/api.h" + +typedef struct { + uint32_t count; + TSSymbol symbol; + int dynamic_precedence; + unsigned short production_id; +} ReduceAction; + +typedef Array(ReduceAction) ReduceActionSet; + +static inline void ts_reduce_action_set_add(ReduceActionSet *self, + ReduceAction new_action) { + for (uint32_t i = 0; i < self->size; i++) { + ReduceAction action = self->contents[i]; + if (action.symbol == new_action.symbol && action.count == new_action.count) + return; + } + array_push(self, new_action); +} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_REDUCE_ACTION_H_ diff --git a/src/tree_sitter/reusable_node.h b/src/tree_sitter/reusable_node.h new file mode 100644 index 0000000000..9cba951909 --- /dev/null +++ b/src/tree_sitter/reusable_node.h @@ -0,0 +1,88 @@ +#include "./subtree.h" + +typedef struct { + Subtree tree; + uint32_t child_index; + uint32_t byte_offset; +} StackEntry; + +typedef struct { + Array(StackEntry) stack; + Subtree last_external_token; +} ReusableNode; + +static inline ReusableNode reusable_node_new(void) { + return (ReusableNode) {array_new(), NULL_SUBTREE}; +} + +static inline void reusable_node_clear(ReusableNode *self) { + array_clear(&self->stack); + self->last_external_token = NULL_SUBTREE; +} + +static inline void reusable_node_reset(ReusableNode *self, Subtree tree) { + reusable_node_clear(self); + array_push(&self->stack, ((StackEntry) { + .tree = tree, + .child_index = 0, + .byte_offset = 0, + })); +} + +static inline Subtree reusable_node_tree(ReusableNode *self) { + return self->stack.size > 0 + ? self->stack.contents[self->stack.size - 1].tree + : NULL_SUBTREE; +} + +static inline uint32_t reusable_node_byte_offset(ReusableNode *self) { + return self->stack.size > 0 + ? self->stack.contents[self->stack.size - 1].byte_offset + : UINT32_MAX; +} + +static inline void reusable_node_delete(ReusableNode *self) { + array_delete(&self->stack); +} + +static inline void reusable_node_advance(ReusableNode *self) { + StackEntry last_entry = *array_back(&self->stack); + uint32_t byte_offset = last_entry.byte_offset + ts_subtree_total_bytes(last_entry.tree); + if (ts_subtree_has_external_tokens(last_entry.tree)) { + self->last_external_token = ts_subtree_last_external_token(last_entry.tree); + } + + Subtree tree; + uint32_t next_index; + do { + StackEntry popped_entry = array_pop(&self->stack); + next_index = popped_entry.child_index + 1; + if (self->stack.size == 0) return; + tree = array_back(&self->stack)->tree; + } while (ts_subtree_child_count(tree) <= next_index); + + array_push(&self->stack, ((StackEntry) { + .tree = tree.ptr->children[next_index], + .child_index = next_index, + .byte_offset = byte_offset, + })); +} + +static inline bool reusable_node_descend(ReusableNode *self) { + StackEntry last_entry = *array_back(&self->stack); + if (ts_subtree_child_count(last_entry.tree) > 0) { + array_push(&self->stack, ((StackEntry) { + .tree = last_entry.tree.ptr->children[0], + .child_index = 0, + .byte_offset = last_entry.byte_offset, + })); + return true; + } else { + return false; + } +} + +static inline void reusable_node_advance_past_leaf(ReusableNode *self) { + while (reusable_node_descend(self)) {} + reusable_node_advance(self); +} diff --git a/src/tree_sitter/stack.c b/src/tree_sitter/stack.c new file mode 100644 index 0000000000..3e842c99c3 --- /dev/null +++ b/src/tree_sitter/stack.c @@ -0,0 +1,846 @@ +#include "./alloc.h" +#include "./language.h" +#include "./subtree.h" +#include "./array.h" +#include "./stack.h" +#include "./length.h" +#include +#include + +#define MAX_LINK_COUNT 8 +#define MAX_NODE_POOL_SIZE 50 +#define MAX_ITERATOR_COUNT 64 + +#ifdef _WIN32 +#define inline __forceinline +#else +#define inline static inline __attribute__((always_inline)) +#endif + +typedef struct StackNode StackNode; + +typedef struct { + StackNode *node; + Subtree subtree; + bool is_pending; +} StackLink; + +struct StackNode { + TSStateId state; + Length position; + StackLink links[MAX_LINK_COUNT]; + short unsigned int link_count; + uint32_t ref_count; + unsigned error_cost; + unsigned node_count; + int dynamic_precedence; +}; + +typedef struct { + StackNode *node; + SubtreeArray subtrees; + uint32_t subtree_count; + bool is_pending; +} StackIterator; + +typedef struct { + void *payload; + StackIterateCallback callback; +} StackIterateSession; + +typedef Array(StackNode *) StackNodeArray; + +typedef enum { + StackStatusActive, + StackStatusPaused, + StackStatusHalted, +} StackStatus; + +typedef struct { + StackNode *node; + Subtree last_external_token; + StackSummary *summary; + unsigned node_count_at_last_error; + TSSymbol lookahead_when_paused; + StackStatus status; +} StackHead; + +struct Stack { + Array(StackHead) heads; + StackSliceArray slices; + Array(StackIterator) iterators; + StackNodeArray node_pool; + StackNode *base_node; + SubtreePool *subtree_pool; +}; + +typedef unsigned StackAction; +enum { + StackActionNone, + StackActionStop = 1, + StackActionPop = 2, +}; + +typedef StackAction (*StackCallback)(void *, const StackIterator *); + +static void stack_node_retain(StackNode *self) { + if (!self) + return; + assert(self->ref_count > 0); + self->ref_count++; + assert(self->ref_count != 0); +} + +static void stack_node_release(StackNode *self, StackNodeArray *pool, SubtreePool *subtree_pool) { +recur: + assert(self->ref_count != 0); + self->ref_count--; + if (self->ref_count > 0) return; + + StackNode *first_predecessor = NULL; + if (self->link_count > 0) { + for (unsigned i = self->link_count - 1; i > 0; i--) { + StackLink link = self->links[i]; + if (link.subtree.ptr) ts_subtree_release(subtree_pool, link.subtree); + stack_node_release(link.node, pool, subtree_pool); + } + StackLink link = self->links[0]; + if (link.subtree.ptr) ts_subtree_release(subtree_pool, link.subtree); + first_predecessor = self->links[0].node; + } + + if (pool->size < MAX_NODE_POOL_SIZE) { + array_push(pool, self); + } else { + ts_free(self); + } + + if (first_predecessor) { + self = first_predecessor; + goto recur; + } +} + +static StackNode *stack_node_new(StackNode *previous_node, Subtree subtree, + bool is_pending, TSStateId state, StackNodeArray *pool) { + StackNode *node = pool->size > 0 ? + array_pop(pool) : + ts_malloc(sizeof(StackNode)); + *node = (StackNode){.ref_count = 1, .link_count = 0, .state = state}; + + if (previous_node) { + node->link_count = 1; + node->links[0] = (StackLink){ + .node = previous_node, + .subtree = subtree, + .is_pending = is_pending, + }; + + node->position = previous_node->position; + node->error_cost = previous_node->error_cost; + node->dynamic_precedence = previous_node->dynamic_precedence; + node->node_count = previous_node->node_count; + + if (subtree.ptr) { + node->error_cost += ts_subtree_error_cost(subtree); + node->position = length_add(node->position, ts_subtree_total_size(subtree)); + node->node_count += ts_subtree_node_count(subtree); + node->dynamic_precedence += ts_subtree_dynamic_precedence(subtree); + } + } else { + node->position = length_zero(); + node->error_cost = 0; + } + + return node; +} + +static bool stack__subtree_is_equivalent(Subtree left, Subtree right) { + return + left.ptr == right.ptr || + (left.ptr && right.ptr && + ts_subtree_symbol(left) == ts_subtree_symbol(right) && + ((ts_subtree_error_cost(left) > 0 && ts_subtree_error_cost(right) > 0) || + (ts_subtree_padding(left).bytes == ts_subtree_padding(right).bytes && + ts_subtree_size(left).bytes == ts_subtree_size(right).bytes && + ts_subtree_child_count(left) == ts_subtree_child_count(right) && + ts_subtree_extra(left) == ts_subtree_extra(right) && + ts_subtree_external_scanner_state_eq(left, right)))); +} + +static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *subtree_pool) { + if (link.node == self) return; + + for (int i = 0; i < self->link_count; i++) { + StackLink *existing_link = &self->links[i]; + if (stack__subtree_is_equivalent(existing_link->subtree, link.subtree)) { + // In general, we preserve ambiguities until they are removed from the stack + // during a pop operation where multiple paths lead to the same node. But in + // the special case where two links directly connect the same pair of nodes, + // we can safely remove the ambiguity ahead of time without changing behavior. + if (existing_link->node == link.node) { + if ( + ts_subtree_dynamic_precedence(link.subtree) > + ts_subtree_dynamic_precedence(existing_link->subtree) + ) { + ts_subtree_retain(link.subtree); + ts_subtree_release(subtree_pool, existing_link->subtree); + existing_link->subtree = link.subtree; + self->dynamic_precedence = + link.node->dynamic_precedence + ts_subtree_dynamic_precedence(link.subtree); + } + return; + } + + // If the previous nodes are mergeable, merge them recursively. + if (existing_link->node->state == link.node->state && + existing_link->node->position.bytes == link.node->position.bytes) { + for (int j = 0; j < link.node->link_count; j++) { + stack_node_add_link(existing_link->node, link.node->links[j], subtree_pool); + } + int32_t dynamic_precedence = link.node->dynamic_precedence; + if (link.subtree.ptr) { + dynamic_precedence += ts_subtree_dynamic_precedence(link.subtree); + } + if (dynamic_precedence > self->dynamic_precedence) { + self->dynamic_precedence = dynamic_precedence; + } + return; + } + } + } + + if (self->link_count == MAX_LINK_COUNT) return; + + stack_node_retain(link.node); + unsigned node_count = link.node->node_count; + int dynamic_precedence = link.node->dynamic_precedence; + self->links[self->link_count++] = link; + + if (link.subtree.ptr) { + ts_subtree_retain(link.subtree); + node_count += ts_subtree_node_count(link.subtree); + dynamic_precedence += ts_subtree_dynamic_precedence(link.subtree); + } + + if (node_count > self->node_count) self->node_count = node_count; + if (dynamic_precedence > self->dynamic_precedence) self->dynamic_precedence = dynamic_precedence; +} + +static void stack_head_delete(StackHead *self, StackNodeArray *pool, SubtreePool *subtree_pool) { + if (self->node) { + if (self->last_external_token.ptr) { + ts_subtree_release(subtree_pool, self->last_external_token); + } + if (self->summary) { + array_delete(self->summary); + ts_free(self->summary); + } + stack_node_release(self->node, pool, subtree_pool); + } +} + +static StackVersion ts_stack__add_version(Stack *self, StackVersion original_version, + StackNode *node) { + StackHead head = { + .node = node, + .node_count_at_last_error = self->heads.contents[original_version].node_count_at_last_error, + .last_external_token = self->heads.contents[original_version].last_external_token, + .status = StackStatusActive, + .lookahead_when_paused = 0, + }; + array_push(&self->heads, head); + stack_node_retain(node); + if (head.last_external_token.ptr) ts_subtree_retain(head.last_external_token); + return (StackVersion)(self->heads.size - 1); +} + +static void ts_stack__add_slice(Stack *self, StackVersion original_version, + StackNode *node, SubtreeArray *subtrees) { + for (uint32_t i = self->slices.size - 1; i + 1 > 0; i--) { + StackVersion version = self->slices.contents[i].version; + if (self->heads.contents[version].node == node) { + StackSlice slice = {*subtrees, version}; + array_insert(&self->slices, i + 1, slice); + return; + } + } + + StackVersion version = ts_stack__add_version(self, original_version, node); + StackSlice slice = { *subtrees, version }; + array_push(&self->slices, slice); +} + +inline StackSliceArray stack__iter(Stack *self, StackVersion version, + StackCallback callback, void *payload, + int goal_subtree_count) { + array_clear(&self->slices); + array_clear(&self->iterators); + + StackHead *head = array_get(&self->heads, version); + StackIterator iterator = { + .node = head->node, + .subtrees = array_new(), + .subtree_count = 0, + .is_pending = true, + }; + + bool include_subtrees = false; + if (goal_subtree_count >= 0) { + include_subtrees = true; + array_reserve(&iterator.subtrees, goal_subtree_count); + } + + array_push(&self->iterators, iterator); + + while (self->iterators.size > 0) { + for (uint32_t i = 0, size = self->iterators.size; i < size; i++) { + StackIterator *iterator = &self->iterators.contents[i]; + StackNode *node = iterator->node; + + StackAction action = callback(payload, iterator); + bool should_pop = action & StackActionPop; + bool should_stop = action & StackActionStop || node->link_count == 0; + + if (should_pop) { + SubtreeArray subtrees = iterator->subtrees; + if (!should_stop) + ts_subtree_array_copy(subtrees, &subtrees); + ts_subtree_array_reverse(&subtrees); + ts_stack__add_slice( + self, + version, + node, + &subtrees + ); + } + + if (should_stop) { + if (!should_pop) + ts_subtree_array_delete(self->subtree_pool, &iterator->subtrees); + array_erase(&self->iterators, i); + i--, size--; + continue; + } + + for (uint32_t j = 1; j <= node->link_count; j++) { + StackIterator *next_iterator; + StackLink link; + if (j == node->link_count) { + link = node->links[0]; + next_iterator = &self->iterators.contents[i]; + } else { + if (self->iterators.size >= MAX_ITERATOR_COUNT) continue; + link = node->links[j]; + StackIterator current_iterator = self->iterators.contents[i]; + array_push(&self->iterators, current_iterator); + next_iterator = array_back(&self->iterators); + ts_subtree_array_copy(next_iterator->subtrees, &next_iterator->subtrees); + } + + next_iterator->node = link.node; + if (link.subtree.ptr) { + if (include_subtrees) { + array_push(&next_iterator->subtrees, link.subtree); + ts_subtree_retain(link.subtree); + } + + if (!ts_subtree_extra(link.subtree)) { + next_iterator->subtree_count++; + if (!link.is_pending) { + next_iterator->is_pending = false; + } + } + } else { + next_iterator->subtree_count++; + next_iterator->is_pending = false; + } + } + } + } + + return self->slices; +} + +Stack *ts_stack_new(SubtreePool *subtree_pool) { + Stack *self = ts_calloc(1, sizeof(Stack)); + + array_init(&self->heads); + array_init(&self->slices); + array_init(&self->iterators); + array_init(&self->node_pool); + array_reserve(&self->heads, 4); + array_reserve(&self->slices, 4); + array_reserve(&self->iterators, 4); + array_reserve(&self->node_pool, MAX_NODE_POOL_SIZE); + + self->subtree_pool = subtree_pool; + self->base_node = stack_node_new(NULL, NULL_SUBTREE, false, 1, &self->node_pool); + ts_stack_clear(self); + + return self; +} + +void ts_stack_delete(Stack *self) { + if (self->slices.contents) + array_delete(&self->slices); + if (self->iterators.contents) + array_delete(&self->iterators); + stack_node_release(self->base_node, &self->node_pool, self->subtree_pool); + for (uint32_t i = 0; i < self->heads.size; i++) { + stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); + } + array_clear(&self->heads); + if (self->node_pool.contents) { + for (uint32_t i = 0; i < self->node_pool.size; i++) + ts_free(self->node_pool.contents[i]); + array_delete(&self->node_pool); + } + array_delete(&self->heads); + ts_free(self); +} + +uint32_t ts_stack_version_count(const Stack *self) { + return self->heads.size; +} + +TSStateId ts_stack_state(const Stack *self, StackVersion version) { + return array_get(&self->heads, version)->node->state; +} + +Length ts_stack_position(const Stack *self, StackVersion version) { + return array_get(&self->heads, version)->node->position; +} + +Subtree ts_stack_last_external_token(const Stack *self, StackVersion version) { + return array_get(&self->heads, version)->last_external_token; +} + +void ts_stack_set_last_external_token(Stack *self, StackVersion version, Subtree token) { + StackHead *head = array_get(&self->heads, version); + if (token.ptr) ts_subtree_retain(token); + if (head->last_external_token.ptr) ts_subtree_release(self->subtree_pool, head->last_external_token); + head->last_external_token = token; +} + +unsigned ts_stack_error_cost(const Stack *self, StackVersion version) { + StackHead *head = array_get(&self->heads, version); + unsigned result = head->node->error_cost; + if ( + head->status == StackStatusPaused || + (head->node->state == ERROR_STATE && !head->node->links[0].subtree.ptr)) { + result += ERROR_COST_PER_RECOVERY; + } + return result; +} + +unsigned ts_stack_node_count_since_error(const Stack *self, StackVersion version) { + StackHead *head = array_get(&self->heads, version); + if (head->node->node_count < head->node_count_at_last_error) { + head->node_count_at_last_error = head->node->node_count; + } + return head->node->node_count - head->node_count_at_last_error; +} + +void ts_stack_push(Stack *self, StackVersion version, Subtree subtree, + bool pending, TSStateId state) { + StackHead *head = array_get(&self->heads, version); + StackNode *new_node = stack_node_new(head->node, subtree, pending, state, &self->node_pool); + if (!subtree.ptr) head->node_count_at_last_error = new_node->node_count; + head->node = new_node; +} + +inline StackAction iterate_callback(void *payload, const StackIterator *iterator) { + StackIterateSession *session = payload; + session->callback( + session->payload, + iterator->node->state, + iterator->subtree_count + ); + return StackActionNone; +} + +void ts_stack_iterate(Stack *self, StackVersion version, + StackIterateCallback callback, void *payload) { + StackIterateSession session = {payload, callback}; + stack__iter(self, version, iterate_callback, &session, -1); +} + +inline StackAction pop_count_callback(void *payload, const StackIterator *iterator) { + unsigned *goal_subtree_count = payload; + if (iterator->subtree_count == *goal_subtree_count) { + return StackActionPop | StackActionStop; + } else { + return StackActionNone; + } +} + +StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, uint32_t count) { + return stack__iter(self, version, pop_count_callback, &count, count); +} + +inline StackAction pop_pending_callback(void *payload, const StackIterator *iterator) { + if (iterator->subtree_count >= 1) { + if (iterator->is_pending) { + return StackActionPop | StackActionStop; + } else { + return StackActionStop; + } + } else { + return StackActionNone; + } +} + +StackSliceArray ts_stack_pop_pending(Stack *self, StackVersion version) { + StackSliceArray pop = stack__iter(self, version, pop_pending_callback, NULL, 0); + if (pop.size > 0) { + ts_stack_renumber_version(self, pop.contents[0].version, version); + pop.contents[0].version = version; + } + return pop; +} + +inline StackAction pop_error_callback(void *payload, const StackIterator *iterator) { + if (iterator->subtrees.size > 0) { + bool *found_error = payload; + if (!*found_error && ts_subtree_is_error(iterator->subtrees.contents[0])) { + *found_error = true; + return StackActionPop | StackActionStop; + } else { + return StackActionStop; + } + } else { + return StackActionNone; + } +} + +SubtreeArray ts_stack_pop_error(Stack *self, StackVersion version) { + StackNode *node = array_get(&self->heads, version)->node; + for (unsigned i = 0; i < node->link_count; i++) { + if (node->links[i].subtree.ptr && ts_subtree_is_error(node->links[i].subtree)) { + bool found_error = false; + StackSliceArray pop = stack__iter(self, version, pop_error_callback, &found_error, 1); + if (pop.size > 0) { + assert(pop.size == 1); + ts_stack_renumber_version(self, pop.contents[0].version, version); + return pop.contents[0].subtrees; + } + break; + } + } + return (SubtreeArray){.size = 0}; +} + +inline StackAction pop_all_callback(void *payload, const StackIterator *iterator) { + return iterator->node->link_count == 0 ? StackActionPop : StackActionNone; +} + +StackSliceArray ts_stack_pop_all(Stack *self, StackVersion version) { + return stack__iter(self, version, pop_all_callback, NULL, 0); +} + +typedef struct { + StackSummary *summary; + unsigned max_depth; +} SummarizeStackSession; + +inline StackAction summarize_stack_callback(void *payload, const StackIterator *iterator) { + SummarizeStackSession *session = payload; + TSStateId state = iterator->node->state; + unsigned depth = iterator->subtree_count; + if (depth > session->max_depth) return StackActionStop; + for (unsigned i = session->summary->size - 1; i + 1 > 0; i--) { + StackSummaryEntry entry = session->summary->contents[i]; + if (entry.depth < depth) break; + if (entry.depth == depth && entry.state == state) return StackActionNone; + } + array_push(session->summary, ((StackSummaryEntry){ + .position = iterator->node->position, + .depth = depth, + .state = state, + })); + return StackActionNone; +} + +void ts_stack_record_summary(Stack *self, StackVersion version, unsigned max_depth) { + SummarizeStackSession session = { + .summary = ts_malloc(sizeof(StackSummary)), + .max_depth = max_depth + }; + array_init(session.summary); + stack__iter(self, version, summarize_stack_callback, &session, -1); + self->heads.contents[version].summary = session.summary; +} + +StackSummary *ts_stack_get_summary(Stack *self, StackVersion version) { + return array_get(&self->heads, version)->summary; +} + +int ts_stack_dynamic_precedence(Stack *self, StackVersion version) { + return array_get(&self->heads, version)->node->dynamic_precedence; +} + +bool ts_stack_has_advanced_since_error(const Stack *self, StackVersion version) { + const StackHead *head = array_get(&self->heads, version); + const StackNode *node = head->node; + if (node->error_cost == 0) return true; + while (node) { + if (node->link_count > 0) { + Subtree subtree = node->links[0].subtree; + if (subtree.ptr) { + if (ts_subtree_total_bytes(subtree) > 0) { + return true; + } else if ( + node->node_count > head->node_count_at_last_error && + ts_subtree_error_cost(subtree) == 0 + ) { + node = node->links[0].node; + continue; + } + } + } + break; + } + return false; +} + +void ts_stack_remove_version(Stack *self, StackVersion version) { + stack_head_delete(array_get(&self->heads, version), &self->node_pool, self->subtree_pool); + array_erase(&self->heads, version); +} + +void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2) { + if (v1 == v2) return; + assert(v2 < v1); + assert((uint32_t)v1 < self->heads.size); + StackHead *source_head = &self->heads.contents[v1]; + StackHead *target_head = &self->heads.contents[v2]; + if (target_head->summary && !source_head->summary) { + source_head->summary = target_head->summary; + target_head->summary = NULL; + } + stack_head_delete(target_head, &self->node_pool, self->subtree_pool); + *target_head = *source_head; + array_erase(&self->heads, v1); +} + +void ts_stack_swap_versions(Stack *self, StackVersion v1, StackVersion v2) { + StackHead temporary_head = self->heads.contents[v1]; + self->heads.contents[v1] = self->heads.contents[v2]; + self->heads.contents[v2] = temporary_head; +} + +StackVersion ts_stack_copy_version(Stack *self, StackVersion version) { + assert(version < self->heads.size); + array_push(&self->heads, self->heads.contents[version]); + StackHead *head = array_back(&self->heads); + stack_node_retain(head->node); + if (head->last_external_token.ptr) ts_subtree_retain(head->last_external_token); + head->summary = NULL; + return self->heads.size - 1; +} + +bool ts_stack_merge(Stack *self, StackVersion version1, StackVersion version2) { + if (!ts_stack_can_merge(self, version1, version2)) return false; + StackHead *head1 = &self->heads.contents[version1]; + StackHead *head2 = &self->heads.contents[version2]; + for (uint32_t i = 0; i < head2->node->link_count; i++) { + stack_node_add_link(head1->node, head2->node->links[i], self->subtree_pool); + } + if (head1->node->state == ERROR_STATE) { + head1->node_count_at_last_error = head1->node->node_count; + } + ts_stack_remove_version(self, version2); + return true; +} + +bool ts_stack_can_merge(Stack *self, StackVersion version1, StackVersion version2) { + StackHead *head1 = &self->heads.contents[version1]; + StackHead *head2 = &self->heads.contents[version2]; + return + head1->status == StackStatusActive && + head2->status == StackStatusActive && + head1->node->state == head2->node->state && + head1->node->position.bytes == head2->node->position.bytes && + head1->node->error_cost == head2->node->error_cost && + ts_subtree_external_scanner_state_eq(head1->last_external_token, head2->last_external_token); +} + +void ts_stack_halt(Stack *self, StackVersion version) { + array_get(&self->heads, version)->status = StackStatusHalted; +} + +void ts_stack_pause(Stack *self, StackVersion version, TSSymbol lookahead) { + StackHead *head = array_get(&self->heads, version); + head->status = StackStatusPaused; + head->lookahead_when_paused = lookahead; + head->node_count_at_last_error = head->node->node_count; +} + +bool ts_stack_is_active(const Stack *self, StackVersion version) { + return array_get(&self->heads, version)->status == StackStatusActive; +} + +bool ts_stack_is_halted(const Stack *self, StackVersion version) { + return array_get(&self->heads, version)->status == StackStatusHalted; +} + +bool ts_stack_is_paused(const Stack *self, StackVersion version) { + return array_get(&self->heads, version)->status == StackStatusPaused; +} + +TSSymbol ts_stack_resume(Stack *self, StackVersion version) { + StackHead *head = array_get(&self->heads, version); + assert(head->status == StackStatusPaused); + TSSymbol result = head->lookahead_when_paused; + head->status = StackStatusActive; + head->lookahead_when_paused = 0; + return result; +} + +void ts_stack_clear(Stack *self) { + stack_node_retain(self->base_node); + for (uint32_t i = 0; i < self->heads.size; i++) { + stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); + } + array_clear(&self->heads); + array_push(&self->heads, ((StackHead){ + .node = self->base_node, + .last_external_token = NULL_SUBTREE, + .status = StackStatusActive, + .lookahead_when_paused = 0, + })); +} + +bool ts_stack_print_dot_graph(Stack *self, const TSLanguage *language, FILE *f) { + array_reserve(&self->iterators, 32); + bool was_recording_allocations = ts_toggle_allocation_recording(false); + if (!f) f = stderr; + + fprintf(f, "digraph stack {\n"); + fprintf(f, "rankdir=\"RL\";\n"); + fprintf(f, "edge [arrowhead=none]\n"); + + Array(StackNode *) visited_nodes = array_new(); + + array_clear(&self->iterators); + for (uint32_t i = 0; i < self->heads.size; i++) { + StackHead *head = &self->heads.contents[i]; + if (head->status == StackStatusHalted) continue; + + fprintf(f, "node_head_%u [shape=none, label=\"\"]\n", i); + fprintf(f, "node_head_%u -> node_%p [", i, head->node); + + if (head->status == StackStatusPaused) { + fprintf(f, "color=red "); + } + fprintf(f, + "label=%u, fontcolor=blue, weight=10000, labeltooltip=\"node_count: %u\nerror_cost: %u", + i, + ts_stack_node_count_since_error(self, i), + ts_stack_error_cost(self, i) + ); + + if (head->last_external_token.ptr) { + const ExternalScannerState *state = &head->last_external_token.ptr->external_scanner_state; + const char *data = ts_external_scanner_state_data(state); + fprintf(f, "\nexternal_scanner_state:"); + for (uint32_t j = 0; j < state->length; j++) fprintf(f, " %2X", data[j]); + } + + fprintf(f, "\"]\n"); + array_push(&self->iterators, ((StackIterator){.node = head->node })); + } + + bool all_iterators_done = false; + while (!all_iterators_done) { + all_iterators_done = true; + + for (uint32_t i = 0; i < self->iterators.size; i++) { + StackIterator iterator = self->iterators.contents[i]; + StackNode *node = iterator.node; + + for (uint32_t j = 0; j < visited_nodes.size; j++) { + if (visited_nodes.contents[j] == node) { + node = NULL; + break; + } + } + + if (!node) continue; + all_iterators_done = false; + + fprintf(f, "node_%p [", node); + if (node->state == ERROR_STATE) { + fprintf(f, "label=\"?\""); + } else if ( + node->link_count == 1 && + node->links[0].subtree.ptr && + ts_subtree_extra(node->links[0].subtree) + ) { + fprintf(f, "shape=point margin=0 label=\"\""); + } else { + fprintf(f, "label=\"%d\"", node->state); + } + + fprintf( + f, + " tooltip=\"position: %u,%u\nnode_count:%u\nerror_cost: %u\ndynamic_precedence: %d\"];\n", + node->position.extent.row + 1, + node->position.extent.column, + node->node_count, + node->error_cost, + node->dynamic_precedence + ); + + for (int j = 0; j < node->link_count; j++) { + StackLink link = node->links[j]; + fprintf(f, "node_%p -> node_%p [", node, link.node); + if (link.is_pending) fprintf(f, "style=dashed "); + if (link.subtree.ptr && ts_subtree_extra(link.subtree)) fprintf(f, "fontcolor=gray "); + + if (!link.subtree.ptr) { + fprintf(f, "color=red"); + } else { + fprintf(f, "label=\""); + bool quoted = ts_subtree_visible(link.subtree) && !ts_subtree_named(link.subtree); + if (quoted) fprintf(f, "'"); + const char *name = ts_language_symbol_name(language, ts_subtree_symbol(link.subtree)); + for (const char *c = name; *c; c++) { + if (*c == '\"' || *c == '\\') fprintf(f, "\\"); + fprintf(f, "%c", *c); + } + if (quoted) fprintf(f, "'"); + fprintf(f, "\""); + fprintf( + f, + "labeltooltip=\"error_cost: %u\ndynamic_precedence: %u\"", + ts_subtree_error_cost(link.subtree), + ts_subtree_dynamic_precedence(link.subtree) + ); + } + + fprintf(f, "];\n"); + + StackIterator *next_iterator; + if (j == 0) { + next_iterator = &self->iterators.contents[i]; + } else { + array_push(&self->iterators, iterator); + next_iterator = array_back(&self->iterators); + } + next_iterator->node = link.node; + } + + array_push(&visited_nodes, node); + } + } + + fprintf(f, "}\n"); + + array_delete(&visited_nodes); + ts_toggle_allocation_recording(was_recording_allocations); + return true; +} + +#undef inline diff --git a/src/tree_sitter/stack.h b/src/tree_sitter/stack.h new file mode 100644 index 0000000000..ec7a69d2b4 --- /dev/null +++ b/src/tree_sitter/stack.h @@ -0,0 +1,135 @@ +#ifndef TREE_SITTER_PARSE_STACK_H_ +#define TREE_SITTER_PARSE_STACK_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./array.h" +#include "./subtree.h" +#include "./error_costs.h" +#include + +typedef struct Stack Stack; + +typedef unsigned StackVersion; +#define STACK_VERSION_NONE ((StackVersion)-1) + +typedef struct { + SubtreeArray subtrees; + StackVersion version; +} StackSlice; +typedef Array(StackSlice) StackSliceArray; + +typedef struct { + Length position; + unsigned depth; + TSStateId state; +} StackSummaryEntry; +typedef Array(StackSummaryEntry) StackSummary; + +// Create a stack. +Stack *ts_stack_new(SubtreePool *); + +// Release the memory reserved for a given stack. +void ts_stack_delete(Stack *); + +// Get the stack's current number of versions. +uint32_t ts_stack_version_count(const Stack *); + +// Get the state at the top of the given version of the stack. If the stack is +// empty, this returns the initial state, 0. +TSStateId ts_stack_state(const Stack *, StackVersion); + +// Get the last external token associated with a given version of the stack. +Subtree ts_stack_last_external_token(const Stack *, StackVersion); + +// Set the last external token associated with a given version of the stack. +void ts_stack_set_last_external_token(Stack *, StackVersion, Subtree ); + +// Get the position of the given version of the stack within the document. +Length ts_stack_position(const Stack *, StackVersion); + +// Push a tree and state onto the given version of the stack. +// +// This transfers ownership of the tree to the Stack. Callers that +// need to retain ownership of the tree for their own purposes should +// first retain the tree. +void ts_stack_push(Stack *, StackVersion, Subtree , bool, TSStateId); + +// Pop the given number of entries from the given version of the stack. This +// operation can increase the number of stack versions by revealing multiple +// versions which had previously been merged. It returns an array that +// specifies the index of each revealed version and the trees that were +// removed from that version. +StackSliceArray ts_stack_pop_count(Stack *, StackVersion, uint32_t count); + +// Remove an error at the top of the given version of the stack. +SubtreeArray ts_stack_pop_error(Stack *, StackVersion); + +// Remove any pending trees from the top of the given version of the stack. +StackSliceArray ts_stack_pop_pending(Stack *, StackVersion); + +// Remove any all trees from the given version of the stack. +StackSliceArray ts_stack_pop_all(Stack *, StackVersion); + +// Get the maximum number of tree nodes reachable from this version of the stack +// since the last error was detected. +unsigned ts_stack_node_count_since_error(const Stack *, StackVersion); + +int ts_stack_dynamic_precedence(Stack *, StackVersion); + +bool ts_stack_has_advanced_since_error(const Stack *, StackVersion); + +// Compute a summary of all the parse states near the top of the given +// version of the stack and store the summary for later retrieval. +void ts_stack_record_summary(Stack *, StackVersion, unsigned max_depth); + +// Retrieve a summary of all the parse states near the top of the +// given version of the stack. +StackSummary *ts_stack_get_summary(Stack *, StackVersion); + +// Get the total cost of all errors on the given version of the stack. +unsigned ts_stack_error_cost(const Stack *, StackVersion version); + +// Merge the given two stack versions if possible, returning true +// if they were successfully merged and false otherwise. +bool ts_stack_merge(Stack *, StackVersion, StackVersion); + +// Determine whether the given two stack versions can be merged. +bool ts_stack_can_merge(Stack *, StackVersion, StackVersion); + +TSSymbol ts_stack_resume(Stack *, StackVersion); + +void ts_stack_pause(Stack *, StackVersion, TSSymbol); + +void ts_stack_halt(Stack *, StackVersion); + +bool ts_stack_is_active(const Stack *, StackVersion); + +bool ts_stack_is_paused(const Stack *, StackVersion); + +bool ts_stack_is_halted(const Stack *, StackVersion); + +void ts_stack_renumber_version(Stack *, StackVersion, StackVersion); + +void ts_stack_swap_versions(Stack *, StackVersion, StackVersion); + +StackVersion ts_stack_copy_version(Stack *, StackVersion); + +// Remove the given version from the stack. +void ts_stack_remove_version(Stack *, StackVersion); + +void ts_stack_clear(Stack *); + +bool ts_stack_print_dot_graph(Stack *, const TSLanguage *, FILE *); + +typedef void (*StackIterateCallback)(void *, TSStateId, uint32_t); + +void ts_stack_iterate(Stack *, StackVersion, StackIterateCallback, void *); + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_PARSE_STACK_H_ diff --git a/src/tree_sitter/subtree.c b/src/tree_sitter/subtree.c new file mode 100644 index 0000000000..e95733eb46 --- /dev/null +++ b/src/tree_sitter/subtree.c @@ -0,0 +1,996 @@ +#include +#include +#include +#include +#include +#include +#include "./alloc.h" +#include "./atomic.h" +#include "./subtree.h" +#include "./length.h" +#include "./language.h" +#include "./error_costs.h" +#include + +typedef struct { + Length start; + Length old_end; + Length new_end; +} Edit; + +#ifdef TREE_SITTER_TEST + +#define TS_MAX_INLINE_TREE_LENGTH 2 +#define TS_MAX_TREE_POOL_SIZE 0 + +#else + +#define TS_MAX_INLINE_TREE_LENGTH UINT8_MAX +#define TS_MAX_TREE_POOL_SIZE 32 + +#endif + +static const ExternalScannerState empty_state = {.length = 0, .short_data = {0}}; + +// ExternalScannerState + +void ts_external_scanner_state_init(ExternalScannerState *self, const char *data, unsigned length) { + self->length = length; + if (length > sizeof(self->short_data)) { + self->long_data = ts_malloc(length); + memcpy(self->long_data, data, length); + } else { + memcpy(self->short_data, data, length); + } +} + +ExternalScannerState ts_external_scanner_state_copy(const ExternalScannerState *self) { + ExternalScannerState result = *self; + if (self->length > sizeof(self->short_data)) { + result.long_data = ts_malloc(self->length); + memcpy(result.long_data, self->long_data, self->length); + } + return result; +} + +void ts_external_scanner_state_delete(ExternalScannerState *self) { + if (self->length > sizeof(self->short_data)) { + ts_free(self->long_data); + } +} + +const char *ts_external_scanner_state_data(const ExternalScannerState *self) { + if (self->length > sizeof(self->short_data)) { + return self->long_data; + } else { + return self->short_data; + } +} + +bool ts_external_scanner_state_eq(const ExternalScannerState *a, const ExternalScannerState *b) { + return a == b || ( + a->length == b->length && + !memcmp(ts_external_scanner_state_data(a), ts_external_scanner_state_data(b), a->length) + ); +} + +// SubtreeArray + +void ts_subtree_array_copy(SubtreeArray self, SubtreeArray *dest) { + dest->size = self.size; + dest->capacity = self.capacity; + dest->contents = self.contents; + if (self.capacity > 0) { + dest->contents = ts_calloc(self.capacity, sizeof(Subtree)); + memcpy(dest->contents, self.contents, self.size * sizeof(Subtree)); + for (uint32_t i = 0; i < self.size; i++) { + ts_subtree_retain(dest->contents[i]); + } + } +} + +void ts_subtree_array_delete(SubtreePool *pool, SubtreeArray *self) { + for (uint32_t i = 0; i < self->size; i++) { + ts_subtree_release(pool, self->contents[i]); + } + array_delete(self); +} + +SubtreeArray ts_subtree_array_remove_trailing_extras(SubtreeArray *self) { + SubtreeArray result = array_new(); + + uint32_t i = self->size - 1; + for (; i + 1 > 0; i--) { + Subtree child = self->contents[i]; + if (!ts_subtree_extra(child)) break; + array_push(&result, child); + } + + self->size = i + 1; + ts_subtree_array_reverse(&result); + return result; +} + +void ts_subtree_array_reverse(SubtreeArray *self) { + for (uint32_t i = 0, limit = self->size / 2; i < limit; i++) { + size_t reverse_index = self->size - 1 - i; + Subtree swap = self->contents[i]; + self->contents[i] = self->contents[reverse_index]; + self->contents[reverse_index] = swap; + } +} + +// SubtreePool + +SubtreePool ts_subtree_pool_new(uint32_t capacity) { + SubtreePool self = {array_new(), array_new()}; + array_reserve(&self.free_trees, capacity); + return self; +} + +void ts_subtree_pool_delete(SubtreePool *self) { + if (self->free_trees.contents) { + for (unsigned i = 0; i < self->free_trees.size; i++) { + ts_free(self->free_trees.contents[i].ptr); + } + array_delete(&self->free_trees); + } + if (self->tree_stack.contents) array_delete(&self->tree_stack); +} + +static SubtreeHeapData *ts_subtree_pool_allocate(SubtreePool *self) { + if (self->free_trees.size > 0) { + return array_pop(&self->free_trees).ptr; + } else { + return ts_malloc(sizeof(SubtreeHeapData)); + } +} + +static void ts_subtree_pool_free(SubtreePool *self, SubtreeHeapData *tree) { + if (self->free_trees.capacity > 0 && self->free_trees.size + 1 <= TS_MAX_TREE_POOL_SIZE) { + array_push(&self->free_trees, (MutableSubtree) {.ptr = tree}); + } else { + ts_free(tree); + } +} + +// Subtree + +static inline bool ts_subtree_can_inline(Length padding, Length size, uint32_t lookahead_bytes) { + return + padding.bytes < TS_MAX_INLINE_TREE_LENGTH && + padding.extent.row < 16 && + padding.extent.column < TS_MAX_INLINE_TREE_LENGTH && + size.extent.row == 0 && + size.extent.column < TS_MAX_INLINE_TREE_LENGTH && + lookahead_bytes < 16; +} + +Subtree ts_subtree_new_leaf( + SubtreePool *pool, TSSymbol symbol, Length padding, Length size, + uint32_t lookahead_bytes, TSStateId parse_state, bool has_external_tokens, + bool is_keyword, const TSLanguage *language +) { + TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); + bool extra = symbol == ts_builtin_sym_end; + + bool is_inline = ( + symbol <= UINT8_MAX && + !has_external_tokens && + ts_subtree_can_inline(padding, size, lookahead_bytes) + ); + + if (is_inline) { + return (Subtree) {{ + .parse_state = parse_state, + .symbol = symbol, + .padding_bytes = padding.bytes, + .padding_rows = padding.extent.row, + .padding_columns = padding.extent.column, + .size_bytes = size.bytes, + .lookahead_bytes = lookahead_bytes, + .visible = metadata.visible, + .named = metadata.named, + .extra = extra, + .has_changes = false, + .is_missing = false, + .is_keyword = is_keyword, + .is_inline = true, + }}; + } else { + SubtreeHeapData *data = ts_subtree_pool_allocate(pool); + *data = (SubtreeHeapData) { + .ref_count = 1, + .padding = padding, + .size = size, + .lookahead_bytes = lookahead_bytes, + .error_cost = 0, + .child_count = 0, + .symbol = symbol, + .parse_state = parse_state, + .visible = metadata.visible, + .named = metadata.named, + .extra = extra, + .fragile_left = false, + .fragile_right = false, + .has_changes = false, + .has_external_tokens = has_external_tokens, + .is_missing = false, + .is_keyword = is_keyword, + .first_leaf = {.symbol = 0, .parse_state = 0}, + }; + return (Subtree) {.ptr = data}; + } +} + +void ts_subtree_set_symbol( + MutableSubtree *self, + TSSymbol symbol, + const TSLanguage *language +) { + TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); + if (self->data.is_inline) { + assert(symbol < UINT8_MAX); + self->data.symbol = symbol; + self->data.named = metadata.named; + self->data.visible = metadata.visible; + } else { + self->ptr->symbol = symbol; + self->ptr->named = metadata.named; + self->ptr->visible = metadata.visible; + } +} + +Subtree ts_subtree_new_error( + SubtreePool *pool, int32_t lookahead_char, Length padding, Length size, + uint32_t bytes_scanned, TSStateId parse_state, const TSLanguage *language +) { + Subtree result = ts_subtree_new_leaf( + pool, ts_builtin_sym_error, padding, size, bytes_scanned, + parse_state, false, false, language + ); + SubtreeHeapData *data = (SubtreeHeapData *)result.ptr; + data->fragile_left = true; + data->fragile_right = true; + data->lookahead_char = lookahead_char; + return result; +} + +MutableSubtree ts_subtree_make_mut(SubtreePool *pool, Subtree self) { + if (self.data.is_inline) return (MutableSubtree) {self.data}; + if (self.ptr->ref_count == 1) return ts_subtree_to_mut_unsafe(self); + + SubtreeHeapData *result = ts_subtree_pool_allocate(pool); + memcpy(result, self.ptr, sizeof(SubtreeHeapData)); + if (result->child_count > 0) { + result->children = ts_calloc(self.ptr->child_count, sizeof(Subtree)); + memcpy(result->children, self.ptr->children, result->child_count * sizeof(Subtree)); + for (uint32_t i = 0; i < result->child_count; i++) { + ts_subtree_retain(result->children[i]); + } + } else if (result->has_external_tokens) { + result->external_scanner_state = ts_external_scanner_state_copy(&self.ptr->external_scanner_state); + } + result->ref_count = 1; + ts_subtree_release(pool, self); + return (MutableSubtree) {.ptr = result}; +} + +static void ts_subtree__compress(MutableSubtree self, unsigned count, const TSLanguage *language, + MutableSubtreeArray *stack) { + unsigned initial_stack_size = stack->size; + + MutableSubtree tree = self; + TSSymbol symbol = tree.ptr->symbol; + for (unsigned i = 0; i < count; i++) { + if (tree.ptr->ref_count > 1 || tree.ptr->child_count < 2) break; + + MutableSubtree child = ts_subtree_to_mut_unsafe(tree.ptr->children[0]); + if ( + child.data.is_inline || + child.ptr->child_count < 2 || + child.ptr->ref_count > 1 || + child.ptr->symbol != symbol + ) break; + + MutableSubtree grandchild = ts_subtree_to_mut_unsafe(child.ptr->children[0]); + if ( + grandchild.data.is_inline || + grandchild.ptr->child_count < 2 || + grandchild.ptr->ref_count > 1 || + grandchild.ptr->symbol != symbol + ) break; + + tree.ptr->children[0] = ts_subtree_from_mut(grandchild); + child.ptr->children[0] = grandchild.ptr->children[grandchild.ptr->child_count - 1]; + grandchild.ptr->children[grandchild.ptr->child_count - 1] = ts_subtree_from_mut(child); + array_push(stack, tree); + tree = grandchild; + } + + while (stack->size > initial_stack_size) { + tree = array_pop(stack); + MutableSubtree child = ts_subtree_to_mut_unsafe(tree.ptr->children[0]); + MutableSubtree grandchild = ts_subtree_to_mut_unsafe(child.ptr->children[child.ptr->child_count - 1]); + ts_subtree_set_children(grandchild, grandchild.ptr->children, grandchild.ptr->child_count, language); + ts_subtree_set_children(child, child.ptr->children, child.ptr->child_count, language); + ts_subtree_set_children(tree, tree.ptr->children, tree.ptr->child_count, language); + } +} + +void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *language) { + array_clear(&pool->tree_stack); + + if (ts_subtree_child_count(self) > 0 && self.ptr->ref_count == 1) { + array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self)); + } + + while (pool->tree_stack.size > 0) { + MutableSubtree tree = array_pop(&pool->tree_stack); + + if (tree.ptr->repeat_depth > 0) { + Subtree child1 = tree.ptr->children[0]; + Subtree child2 = tree.ptr->children[tree.ptr->child_count - 1]; + if ( + ts_subtree_child_count(child1) > 0 && + ts_subtree_child_count(child2) > 0 && + child1.ptr->repeat_depth > child2.ptr->repeat_depth + ) { + unsigned n = child1.ptr->repeat_depth - child2.ptr->repeat_depth; + for (unsigned i = n / 2; i > 0; i /= 2) { + ts_subtree__compress(tree, i, language, &pool->tree_stack); + n -= i; + } + } + } + + for (uint32_t i = 0; i < tree.ptr->child_count; i++) { + Subtree child = tree.ptr->children[i]; + if (ts_subtree_child_count(child) > 0 && child.ptr->ref_count == 1) { + array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(child)); + } + } + } +} + +static inline uint32_t ts_subtree_repeat_depth(Subtree self) { + return ts_subtree_child_count(self) ? self.ptr->repeat_depth : 0; +} + +void ts_subtree_set_children( + MutableSubtree self, Subtree *children, uint32_t child_count, const TSLanguage *language +) { + assert(!self.data.is_inline); + + if (self.ptr->child_count > 0 && children != self.ptr->children) { + ts_free(self.ptr->children); + } + + self.ptr->child_count = child_count; + self.ptr->children = children; + self.ptr->named_child_count = 0; + self.ptr->visible_child_count = 0; + self.ptr->error_cost = 0; + self.ptr->repeat_depth = 0; + self.ptr->node_count = 1; + self.ptr->has_external_tokens = false; + self.ptr->dynamic_precedence = 0; + + uint32_t non_extra_index = 0; + const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self.ptr->production_id); + uint32_t lookahead_end_byte = 0; + + for (uint32_t i = 0; i < self.ptr->child_count; i++) { + Subtree child = self.ptr->children[i]; + + if (i == 0) { + self.ptr->padding = ts_subtree_padding(child); + self.ptr->size = ts_subtree_size(child); + } else { + self.ptr->size = length_add(self.ptr->size, ts_subtree_total_size(child)); + } + + uint32_t child_lookahead_end_byte = + self.ptr->padding.bytes + + self.ptr->size.bytes + + ts_subtree_lookahead_bytes(child); + if (child_lookahead_end_byte > lookahead_end_byte) lookahead_end_byte = child_lookahead_end_byte; + + if (ts_subtree_symbol(child) != ts_builtin_sym_error_repeat) { + self.ptr->error_cost += ts_subtree_error_cost(child); + } + + self.ptr->dynamic_precedence += ts_subtree_dynamic_precedence(child); + self.ptr->node_count += ts_subtree_node_count(child); + + if (alias_sequence && alias_sequence[non_extra_index] != 0 && !ts_subtree_extra(child)) { + self.ptr->visible_child_count++; + if (ts_language_symbol_metadata(language, alias_sequence[non_extra_index]).named) { + self.ptr->named_child_count++; + } + } else if (ts_subtree_visible(child)) { + self.ptr->visible_child_count++; + if (ts_subtree_named(child)) self.ptr->named_child_count++; + } else if (ts_subtree_child_count(child) > 0) { + self.ptr->visible_child_count += child.ptr->visible_child_count; + self.ptr->named_child_count += child.ptr->named_child_count; + } + + if (ts_subtree_has_external_tokens(child)) self.ptr->has_external_tokens = true; + + if (ts_subtree_is_error(child)) { + self.ptr->fragile_left = self.ptr->fragile_right = true; + self.ptr->parse_state = TS_TREE_STATE_NONE; + } + + if (!ts_subtree_extra(child)) non_extra_index++; + } + + self.ptr->lookahead_bytes = lookahead_end_byte - self.ptr->size.bytes - self.ptr->padding.bytes; + + if (self.ptr->symbol == ts_builtin_sym_error || self.ptr->symbol == ts_builtin_sym_error_repeat) { + self.ptr->error_cost += + ERROR_COST_PER_RECOVERY + + ERROR_COST_PER_SKIPPED_CHAR * self.ptr->size.bytes + + ERROR_COST_PER_SKIPPED_LINE * self.ptr->size.extent.row; + for (uint32_t i = 0; i < self.ptr->child_count; i++) { + Subtree child = self.ptr->children[i]; + uint32_t grandchild_count = ts_subtree_child_count(child); + if (ts_subtree_extra(child)) continue; + if (ts_subtree_is_error(child) && grandchild_count == 0) continue; + if (ts_subtree_visible(child)) { + self.ptr->error_cost += ERROR_COST_PER_SKIPPED_TREE; + } else if (grandchild_count > 0) { + self.ptr->error_cost += ERROR_COST_PER_SKIPPED_TREE * child.ptr->visible_child_count; + } + } + } + + if (self.ptr->child_count > 0) { + Subtree first_child = self.ptr->children[0]; + Subtree last_child = self.ptr->children[self.ptr->child_count - 1]; + + self.ptr->first_leaf.symbol = ts_subtree_leaf_symbol(first_child); + self.ptr->first_leaf.parse_state = ts_subtree_leaf_parse_state(first_child); + + if (ts_subtree_fragile_left(first_child)) self.ptr->fragile_left = true; + if (ts_subtree_fragile_right(last_child)) self.ptr->fragile_right = true; + + if ( + self.ptr->child_count >= 2 && + !self.ptr->visible && + !self.ptr->named && + ts_subtree_symbol(first_child) == self.ptr->symbol + ) { + if (ts_subtree_repeat_depth(first_child) > ts_subtree_repeat_depth(last_child)) { + self.ptr->repeat_depth = ts_subtree_repeat_depth(first_child) + 1; + } else { + self.ptr->repeat_depth = ts_subtree_repeat_depth(last_child) + 1; + } + } + } +} + +MutableSubtree ts_subtree_new_node(SubtreePool *pool, TSSymbol symbol, + SubtreeArray *children, unsigned production_id, + const TSLanguage *language) { + TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); + bool fragile = symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat; + SubtreeHeapData *data = ts_subtree_pool_allocate(pool); + *data = (SubtreeHeapData) { + .ref_count = 1, + .symbol = symbol, + .production_id = production_id, + .visible = metadata.visible, + .named = metadata.named, + .has_changes = false, + .fragile_left = fragile, + .fragile_right = fragile, + .is_keyword = false, + .node_count = 0, + .first_leaf = {.symbol = 0, .parse_state = 0}, + }; + MutableSubtree result = {.ptr = data}; + ts_subtree_set_children(result, children->contents, children->size, language); + return result; +} + +Subtree ts_subtree_new_error_node(SubtreePool *pool, SubtreeArray *children, + bool extra, const TSLanguage *language) { + MutableSubtree result = ts_subtree_new_node( + pool, ts_builtin_sym_error, children, 0, language + ); + result.ptr->extra = extra; + return ts_subtree_from_mut(result); +} + +Subtree ts_subtree_new_missing_leaf(SubtreePool *pool, TSSymbol symbol, Length padding, + const TSLanguage *language) { + Subtree result = ts_subtree_new_leaf( + pool, symbol, padding, length_zero(), 0, + 0, false, false, language + ); + + if (result.data.is_inline) { + result.data.is_missing = true; + } else { + ((SubtreeHeapData *)result.ptr)->is_missing = true; + } + + return result; +} + +void ts_subtree_retain(Subtree self) { + if (self.data.is_inline) return; + assert(self.ptr->ref_count > 0); + atomic_inc((volatile uint32_t *)&self.ptr->ref_count); + assert(self.ptr->ref_count != 0); +} + +void ts_subtree_release(SubtreePool *pool, Subtree self) { + if (self.data.is_inline) return; + array_clear(&pool->tree_stack); + + assert(self.ptr->ref_count > 0); + if (atomic_dec((volatile uint32_t *)&self.ptr->ref_count) == 0) { + array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self)); + } + + while (pool->tree_stack.size > 0) { + MutableSubtree tree = array_pop(&pool->tree_stack); + if (tree.ptr->child_count > 0) { + for (uint32_t i = 0; i < tree.ptr->child_count; i++) { + Subtree child = tree.ptr->children[i]; + if (child.data.is_inline) continue; + assert(child.ptr->ref_count > 0); + if (atomic_dec((volatile uint32_t *)&child.ptr->ref_count) == 0) { + array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(child)); + } + } + ts_free(tree.ptr->children); + } else if (tree.ptr->has_external_tokens) { + ts_external_scanner_state_delete(&tree.ptr->external_scanner_state); + } + ts_subtree_pool_free(pool, tree.ptr); + } +} + +bool ts_subtree_eq(Subtree self, Subtree other) { + if (self.data.is_inline || other.data.is_inline) { + return memcmp(&self, &other, sizeof(SubtreeInlineData)) == 0; + } + + if (self.ptr) { + if (!other.ptr) return false; + } else { + return !other.ptr; + } + + if (self.ptr->symbol != other.ptr->symbol) return false; + if (self.ptr->visible != other.ptr->visible) return false; + if (self.ptr->named != other.ptr->named) return false; + if (self.ptr->padding.bytes != other.ptr->padding.bytes) return false; + if (self.ptr->size.bytes != other.ptr->size.bytes) return false; + if (self.ptr->symbol == ts_builtin_sym_error) return self.ptr->lookahead_char == other.ptr->lookahead_char; + if (self.ptr->child_count != other.ptr->child_count) return false; + if (self.ptr->child_count > 0) { + if (self.ptr->visible_child_count != other.ptr->visible_child_count) return false; + if (self.ptr->named_child_count != other.ptr->named_child_count) return false; + + for (uint32_t i = 0; i < self.ptr->child_count; i++) { + if (!ts_subtree_eq(self.ptr->children[i], other.ptr->children[i])) { + return false; + } + } + } + return true; +} + +int ts_subtree_compare(Subtree left, Subtree right) { + if (ts_subtree_symbol(left) < ts_subtree_symbol(right)) return -1; + if (ts_subtree_symbol(right) < ts_subtree_symbol(left)) return 1; + if (ts_subtree_child_count(left) < ts_subtree_child_count(right)) return -1; + if (ts_subtree_child_count(right) < ts_subtree_child_count(left)) return 1; + for (uint32_t i = 0, n = ts_subtree_child_count(left); i < n; i++) { + Subtree left_child = left.ptr->children[i]; + Subtree right_child = right.ptr->children[i]; + switch (ts_subtree_compare(left_child, right_child)) { + case -1: return -1; + case 1: return 1; + default: break; + } + } + return 0; +} + +static inline void ts_subtree_set_has_changes(MutableSubtree *self) { + if (self->data.is_inline) { + self->data.has_changes = true; + } else { + self->ptr->has_changes = true; + } +} + +Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool) { + typedef struct { + Subtree *tree; + Edit edit; + } StackEntry; + + Array(StackEntry) stack = array_new(); + array_push(&stack, ((StackEntry) { + .tree = &self, + .edit = (Edit) { + .start = {edit->start_byte, edit->start_point}, + .old_end = {edit->old_end_byte, edit->old_end_point}, + .new_end = {edit->new_end_byte, edit->new_end_point}, + }, + })); + + while (stack.size) { + StackEntry entry = array_pop(&stack); + Edit edit = entry.edit; + bool is_noop = edit.old_end.bytes == edit.start.bytes && edit.new_end.bytes == edit.start.bytes; + bool is_pure_insertion = edit.old_end.bytes == edit.start.bytes; + + Length size = ts_subtree_size(*entry.tree); + Length padding = ts_subtree_padding(*entry.tree); + uint32_t lookahead_bytes = ts_subtree_lookahead_bytes(*entry.tree); + uint32_t end_byte = padding.bytes + size.bytes + lookahead_bytes; + if (edit.start.bytes > end_byte || (is_noop && edit.start.bytes == end_byte)) continue; + + // If the edit is entirely within the space before this subtree, then shift this + // subtree over according to the edit without changing its size. + if (edit.old_end.bytes <= padding.bytes) { + padding = length_add(edit.new_end, length_sub(padding, edit.old_end)); + } + + // If the edit starts in the space before this subtree and extends into this subtree, + // shrink the subtree's content to compensate for the change in the space before it. + else if (edit.start.bytes < padding.bytes) { + size = length_sub(size, length_sub(edit.old_end, padding)); + padding = edit.new_end; + } + + // If the edit is a pure insertion right at the start of the subtree, + // shift the subtree over according to the insertion. + else if (edit.start.bytes == padding.bytes && is_pure_insertion) { + padding = edit.new_end; + } + + // If the edit is within this subtree, resize the subtree to reflect the edit. + else { + uint32_t total_bytes = padding.bytes + size.bytes; + if (edit.start.bytes < total_bytes || + (edit.start.bytes == total_bytes && is_pure_insertion)) { + size = length_add( + length_sub(edit.new_end, padding), + length_sub(size, length_sub(edit.old_end, padding)) + ); + } + } + + MutableSubtree result = ts_subtree_make_mut(pool, *entry.tree); + + if (result.data.is_inline) { + if (ts_subtree_can_inline(padding, size, lookahead_bytes)) { + result.data.padding_bytes = padding.bytes; + result.data.padding_rows = padding.extent.row; + result.data.padding_columns = padding.extent.column; + result.data.size_bytes = size.bytes; + } else { + SubtreeHeapData *data = ts_subtree_pool_allocate(pool); + data->ref_count = 1; + data->padding = padding; + data->size = size; + data->lookahead_bytes = lookahead_bytes; + data->error_cost = 0; + data->child_count = 0; + data->symbol = result.data.symbol; + data->parse_state = result.data.parse_state; + data->visible = result.data.visible; + data->named = result.data.named; + data->extra = result.data.extra; + data->fragile_left = false; + data->fragile_right = false; + data->has_changes = false; + data->has_external_tokens = false; + data->is_missing = result.data.is_missing; + data->is_keyword = result.data.is_keyword; + result.ptr = data; + } + } else { + result.ptr->padding = padding; + result.ptr->size = size; + } + + ts_subtree_set_has_changes(&result); + *entry.tree = ts_subtree_from_mut(result); + + Length child_left, child_right = length_zero(); + for (uint32_t i = 0, n = ts_subtree_child_count(*entry.tree); i < n; i++) { + Subtree *child = &result.ptr->children[i]; + Length child_size = ts_subtree_total_size(*child); + child_left = child_right; + child_right = length_add(child_left, child_size); + + // If this child ends before the edit, it is not affected. + if (child_right.bytes + ts_subtree_lookahead_bytes(*child) < edit.start.bytes) continue; + + // If this child starts after the edit, then we're done processing children. + if (child_left.bytes > edit.old_end.bytes || + (child_left.bytes == edit.old_end.bytes && child_size.bytes > 0 && i > 0)) break; + + // Transform edit into the child's coordinate space. + Edit child_edit = { + .start = length_sub(edit.start, child_left), + .old_end = length_sub(edit.old_end, child_left), + .new_end = length_sub(edit.new_end, child_left), + }; + + // Clamp child_edit to the child's bounds. + if (edit.start.bytes < child_left.bytes) child_edit.start = length_zero(); + if (edit.old_end.bytes < child_left.bytes) child_edit.old_end = length_zero(); + if (edit.new_end.bytes < child_left.bytes) child_edit.new_end = length_zero(); + if (edit.old_end.bytes > child_right.bytes) child_edit.old_end = child_size; + + // Interpret all inserted text as applying to the *first* child that touches the edit. + // Subsequent children are only never have any text inserted into them; they are only + // shrunk to compensate for the edit. + if (child_right.bytes > edit.start.bytes || + (child_right.bytes == edit.start.bytes && is_pure_insertion)) { + edit.new_end = edit.start; + } + + // Children that occur before the edit are not reshaped by the edit. + else { + child_edit.old_end = child_edit.start; + child_edit.new_end = child_edit.start; + } + + // Queue processing of this child's subtree. + array_push(&stack, ((StackEntry) { + .tree = child, + .edit = child_edit, + })); + } + } + + array_delete(&stack); + return self; +} + +Subtree ts_subtree_last_external_token(Subtree tree) { + if (!ts_subtree_has_external_tokens(tree)) return NULL_SUBTREE; + while (tree.ptr->child_count > 0) { + for (uint32_t i = tree.ptr->child_count - 1; i + 1 > 0; i--) { + Subtree child = tree.ptr->children[i]; + if (ts_subtree_has_external_tokens(child)) { + tree = child; + break; + } + } + } + return tree; +} + +static size_t ts_subtree__write_char_to_string(char *s, size_t n, int32_t c) { + if (c == 0) + return snprintf(s, n, "EOF"); + if (c == -1) + return snprintf(s, n, "INVALID"); + else if (c == '\n') + return snprintf(s, n, "'\\n'"); + else if (c == '\t') + return snprintf(s, n, "'\\t'"); + else if (c == '\r') + return snprintf(s, n, "'\\r'"); + else if (0 < c && c < 128 && isprint(c)) + return snprintf(s, n, "'%c'", c); + else + return snprintf(s, n, "%d", c); +} + +static void ts_subtree__write_dot_string(FILE *f, const char *string) { + for (const char *c = string; *c; c++) { + if (*c == '"') { + fputs("\\\"", f); + } else if (*c == '\n') { + fputs("\\n", f); + } else { + fputc(*c, f); + } + } +} + +static const char *ROOT_FIELD = "__ROOT__"; + +static size_t ts_subtree__write_to_string( + Subtree self, char *string, size_t limit, + const TSLanguage *language, bool include_all, + TSSymbol alias_symbol, bool alias_is_named, const char *field_name +) { + if (!self.ptr) return snprintf(string, limit, "(NULL)"); + + char *cursor = string; + char **writer = (limit > 0) ? &cursor : &string; + bool is_root = field_name == ROOT_FIELD; + bool is_visible = + include_all || + ts_subtree_missing(self) || + ( + alias_symbol + ? alias_is_named + : ts_subtree_visible(self) && ts_subtree_named(self) + ); + + if (is_visible) { + if (!is_root) { + cursor += snprintf(*writer, limit, " "); + if (field_name) { + cursor += snprintf(*writer, limit, "%s: ", field_name); + } + } + + if (ts_subtree_is_error(self) && ts_subtree_child_count(self) == 0 && self.ptr->size.bytes > 0) { + cursor += snprintf(*writer, limit, "(UNEXPECTED "); + cursor += ts_subtree__write_char_to_string(*writer, limit, self.ptr->lookahead_char); + } else { + TSSymbol symbol = alias_symbol ? alias_symbol : ts_subtree_symbol(self); + const char *symbol_name = ts_language_symbol_name(language, symbol); + if (ts_subtree_missing(self)) { + cursor += snprintf(*writer, limit, "(MISSING "); + if (alias_is_named || ts_subtree_named(self)) { + cursor += snprintf(*writer, limit, "%s", symbol_name); + } else { + cursor += snprintf(*writer, limit, "\"%s\"", symbol_name); + } + } else { + cursor += snprintf(*writer, limit, "(%s", symbol_name); + } + } + } else if (is_root) { + TSSymbol symbol = ts_subtree_symbol(self); + const char *symbol_name = ts_language_symbol_name(language, symbol); + cursor += snprintf(*writer, limit, "(\"%s\")", symbol_name); + } + + if (ts_subtree_child_count(self)) { + const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self.ptr->production_id); + const TSFieldMapEntry *field_map, *field_map_end; + ts_language_field_map( + language, + self.ptr->production_id, + &field_map, + &field_map_end + ); + + uint32_t structural_child_index = 0; + for (uint32_t i = 0; i < self.ptr->child_count; i++) { + Subtree child = self.ptr->children[i]; + if (ts_subtree_extra(child)) { + cursor += ts_subtree__write_to_string( + child, *writer, limit, + language, include_all, + 0, false, NULL + ); + } else { + TSSymbol alias_symbol = alias_sequence + ? alias_sequence[structural_child_index] + : 0; + bool alias_is_named = alias_symbol + ? ts_language_symbol_metadata(language, alias_symbol).named + : false; + + const char *child_field_name = is_visible ? NULL : field_name; + for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) { + if (!i->inherited && i->child_index == structural_child_index) { + child_field_name = language->field_names[i->field_id]; + break; + } + } + + cursor += ts_subtree__write_to_string( + child, *writer, limit, + language, include_all, + alias_symbol, alias_is_named, child_field_name + ); + structural_child_index++; + } + } + } + + if (is_visible) cursor += snprintf(*writer, limit, ")"); + + return cursor - string; +} + +char *ts_subtree_string( + Subtree self, + const TSLanguage *language, + bool include_all +) { + char scratch_string[1]; + size_t size = ts_subtree__write_to_string( + self, scratch_string, 0, + language, include_all, + 0, false, ROOT_FIELD + ) + 1; + char *result = malloc(size * sizeof(char)); + ts_subtree__write_to_string( + self, result, size, + language, include_all, + 0, false, ROOT_FIELD + ); + return result; +} + +void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset, + const TSLanguage *language, TSSymbol alias_symbol, + FILE *f) { + TSSymbol subtree_symbol = ts_subtree_symbol(*self); + TSSymbol symbol = alias_symbol ? alias_symbol : subtree_symbol; + uint32_t end_offset = start_offset + ts_subtree_total_bytes(*self); + fprintf(f, "tree_%p [label=\"", self); + ts_subtree__write_dot_string(f, ts_language_symbol_name(language, symbol)); + fprintf(f, "\""); + + if (ts_subtree_child_count(*self) == 0) fprintf(f, ", shape=plaintext"); + if (ts_subtree_extra(*self)) fprintf(f, ", fontcolor=gray"); + + fprintf(f, ", tooltip=\"" + "range: %u - %u\n" + "state: %d\n" + "error-cost: %u\n" + "has-changes: %u\n" + "repeat-depth: %u\n" + "lookahead-bytes: %u", + start_offset, end_offset, + ts_subtree_parse_state(*self), + ts_subtree_error_cost(*self), + ts_subtree_has_changes(*self), + ts_subtree_repeat_depth(*self), + ts_subtree_lookahead_bytes(*self) + ); + + if (ts_subtree_is_error(*self) && ts_subtree_child_count(*self) == 0) { + fprintf(f, "\ncharacter: '%c'", self->ptr->lookahead_char); + } + + fprintf(f, "\"]\n"); + + uint32_t child_start_offset = start_offset; + uint32_t child_info_offset = + language->max_alias_sequence_length * + ts_subtree_production_id(*self); + for (uint32_t i = 0, n = ts_subtree_child_count(*self); i < n; i++) { + const Subtree *child = &self->ptr->children[i]; + TSSymbol alias_symbol = 0; + if (!ts_subtree_extra(*child) && child_info_offset) { + alias_symbol = language->alias_sequences[child_info_offset]; + child_info_offset++; + } + ts_subtree__print_dot_graph(child, child_start_offset, language, alias_symbol, f); + fprintf(f, "tree_%p -> tree_%p [tooltip=%u]\n", self, child, i); + child_start_offset += ts_subtree_total_bytes(*child); + } +} + +void ts_subtree_print_dot_graph(Subtree self, const TSLanguage *language, FILE *f) { + fprintf(f, "digraph tree {\n"); + fprintf(f, "edge [arrowhead=none]\n"); + ts_subtree__print_dot_graph(&self, 0, language, 0, f); + fprintf(f, "}\n"); +} + +bool ts_subtree_external_scanner_state_eq(Subtree self, Subtree other) { + const ExternalScannerState *state1 = &empty_state; + const ExternalScannerState *state2 = &empty_state; + if (self.ptr && ts_subtree_has_external_tokens(self) && !self.ptr->child_count) { + state1 = &self.ptr->external_scanner_state; + } + if (other.ptr && ts_subtree_has_external_tokens(other) && !other.ptr->child_count) { + state2 = &other.ptr->external_scanner_state; + } + return ts_external_scanner_state_eq(state1, state2); +} diff --git a/src/tree_sitter/subtree.h b/src/tree_sitter/subtree.h new file mode 100644 index 0000000000..79ccd92390 --- /dev/null +++ b/src/tree_sitter/subtree.h @@ -0,0 +1,281 @@ +#ifndef TREE_SITTER_SUBTREE_H_ +#define TREE_SITTER_SUBTREE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include "./length.h" +#include "./array.h" +#include "./error_costs.h" +#include "tree_sitter/api.h" +#include "tree_sitter/parser.h" + +static const TSStateId TS_TREE_STATE_NONE = USHRT_MAX; +#define NULL_SUBTREE ((Subtree) {.ptr = NULL}) + +typedef union Subtree Subtree; +typedef union MutableSubtree MutableSubtree; + +typedef struct { + union { + char *long_data; + char short_data[24]; + }; + uint32_t length; +} ExternalScannerState; + +typedef struct { + bool is_inline : 1; + bool visible : 1; + bool named : 1; + bool extra : 1; + bool has_changes : 1; + bool is_missing : 1; + bool is_keyword : 1; + uint8_t symbol; + uint8_t padding_bytes; + uint8_t size_bytes; + uint8_t padding_columns; + uint8_t padding_rows : 4; + uint8_t lookahead_bytes : 4; + uint16_t parse_state; +} SubtreeInlineData; + +typedef struct { + volatile uint32_t ref_count; + Length padding; + Length size; + uint32_t lookahead_bytes; + uint32_t error_cost; + uint32_t child_count; + TSSymbol symbol; + TSStateId parse_state; + + bool visible : 1; + bool named : 1; + bool extra : 1; + bool fragile_left : 1; + bool fragile_right : 1; + bool has_changes : 1; + bool has_external_tokens : 1; + bool is_missing : 1; + bool is_keyword : 1; + + union { + // Non-terminal subtrees (`child_count > 0`) + struct { + Subtree *children; + uint32_t visible_child_count; + uint32_t named_child_count; + uint32_t node_count; + uint32_t repeat_depth; + int32_t dynamic_precedence; + uint16_t production_id; + struct { + TSSymbol symbol; + TSStateId parse_state; + } first_leaf; + }; + + // External terminal subtrees (`child_count == 0 && has_external_tokens`) + ExternalScannerState external_scanner_state; + + // Error terminal subtrees (`child_count == 0 && symbol == ts_builtin_sym_error`) + int32_t lookahead_char; + }; +} SubtreeHeapData; + +union Subtree { + SubtreeInlineData data; + const SubtreeHeapData *ptr; +}; + +union MutableSubtree { + SubtreeInlineData data; + SubtreeHeapData *ptr; +}; + +typedef Array(Subtree) SubtreeArray; +typedef Array(MutableSubtree) MutableSubtreeArray; + +typedef struct { + MutableSubtreeArray free_trees; + MutableSubtreeArray tree_stack; +} SubtreePool; + +void ts_external_scanner_state_init(ExternalScannerState *, const char *, unsigned); +const char *ts_external_scanner_state_data(const ExternalScannerState *); + +void ts_subtree_array_copy(SubtreeArray, SubtreeArray *); +void ts_subtree_array_delete(SubtreePool *, SubtreeArray *); +SubtreeArray ts_subtree_array_remove_trailing_extras(SubtreeArray *); +void ts_subtree_array_reverse(SubtreeArray *); + +SubtreePool ts_subtree_pool_new(uint32_t capacity); +void ts_subtree_pool_delete(SubtreePool *); + +Subtree ts_subtree_new_leaf( + SubtreePool *, TSSymbol, Length, Length, uint32_t, + TSStateId, bool, bool, const TSLanguage * +); +Subtree ts_subtree_new_error( + SubtreePool *, int32_t, Length, Length, uint32_t, TSStateId, const TSLanguage * +); +MutableSubtree ts_subtree_new_node(SubtreePool *, TSSymbol, SubtreeArray *, unsigned, const TSLanguage *); +Subtree ts_subtree_new_error_node(SubtreePool *, SubtreeArray *, bool, const TSLanguage *); +Subtree ts_subtree_new_missing_leaf(SubtreePool *, TSSymbol, Length, const TSLanguage *); +MutableSubtree ts_subtree_make_mut(SubtreePool *, Subtree); +void ts_subtree_retain(Subtree); +void ts_subtree_release(SubtreePool *, Subtree); +bool ts_subtree_eq(Subtree, Subtree); +int ts_subtree_compare(Subtree, Subtree); +void ts_subtree_set_symbol(MutableSubtree *, TSSymbol, const TSLanguage *); +void ts_subtree_set_children(MutableSubtree, Subtree *, uint32_t, const TSLanguage *); +void ts_subtree_balance(Subtree, SubtreePool *, const TSLanguage *); +Subtree ts_subtree_edit(Subtree, const TSInputEdit *edit, SubtreePool *); +char *ts_subtree_string(Subtree, const TSLanguage *, bool include_all); +void ts_subtree_print_dot_graph(Subtree, const TSLanguage *, FILE *); +Subtree ts_subtree_last_external_token(Subtree); +bool ts_subtree_external_scanner_state_eq(Subtree, Subtree); + +#define SUBTREE_GET(self, name) (self.data.is_inline ? self.data.name : self.ptr->name) + +static inline TSSymbol ts_subtree_symbol(Subtree self) { return SUBTREE_GET(self, symbol); } +static inline bool ts_subtree_visible(Subtree self) { return SUBTREE_GET(self, visible); } +static inline bool ts_subtree_named(Subtree self) { return SUBTREE_GET(self, named); } +static inline bool ts_subtree_extra(Subtree self) { return SUBTREE_GET(self, extra); } +static inline bool ts_subtree_has_changes(Subtree self) { return SUBTREE_GET(self, has_changes); } +static inline bool ts_subtree_missing(Subtree self) { return SUBTREE_GET(self, is_missing); } +static inline bool ts_subtree_is_keyword(Subtree self) { return SUBTREE_GET(self, is_keyword); } +static inline TSStateId ts_subtree_parse_state(Subtree self) { return SUBTREE_GET(self, parse_state); } +static inline uint32_t ts_subtree_lookahead_bytes(Subtree self) { return SUBTREE_GET(self, lookahead_bytes); } + +#undef SUBTREE_GET + +static inline void ts_subtree_set_extra(MutableSubtree *self) { + if (self->data.is_inline) { + self->data.extra = true; + } else { + self->ptr->extra = true; + } +} + +static inline TSSymbol ts_subtree_leaf_symbol(Subtree self) { + if (self.data.is_inline) return self.data.symbol; + if (self.ptr->child_count == 0) return self.ptr->symbol; + return self.ptr->first_leaf.symbol; +} + +static inline TSStateId ts_subtree_leaf_parse_state(Subtree self) { + if (self.data.is_inline) return self.data.parse_state; + if (self.ptr->child_count == 0) return self.ptr->parse_state; + return self.ptr->first_leaf.parse_state; +} + +static inline Length ts_subtree_padding(Subtree self) { + if (self.data.is_inline) { + Length result = {self.data.padding_bytes, {self.data.padding_rows, self.data.padding_columns}}; + return result; + } else { + return self.ptr->padding; + } +} + +static inline Length ts_subtree_size(Subtree self) { + if (self.data.is_inline) { + Length result = {self.data.size_bytes, {0, self.data.size_bytes}}; + return result; + } else { + return self.ptr->size; + } +} + +static inline Length ts_subtree_total_size(Subtree self) { + return length_add(ts_subtree_padding(self), ts_subtree_size(self)); +} + +static inline uint32_t ts_subtree_total_bytes(Subtree self) { + return ts_subtree_total_size(self).bytes; +} + +static inline uint32_t ts_subtree_child_count(Subtree self) { + return self.data.is_inline ? 0 : self.ptr->child_count; +} + +static inline uint32_t ts_subtree_node_count(Subtree self) { + return (self.data.is_inline || self.ptr->child_count == 0) ? 1 : self.ptr->node_count; +} + +static inline uint32_t ts_subtree_visible_child_count(Subtree self) { + if (ts_subtree_child_count(self) > 0) { + return self.ptr->visible_child_count; + } else { + return 0; + } +} + +static inline uint32_t ts_subtree_error_cost(Subtree self) { + if (ts_subtree_missing(self)) { + return ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY; + } else { + return self.data.is_inline ? 0 : self.ptr->error_cost; + } +} + +static inline int32_t ts_subtree_dynamic_precedence(Subtree self) { + return (self.data.is_inline || self.ptr->child_count == 0) ? 0 : self.ptr->dynamic_precedence; +} + +static inline uint16_t ts_subtree_production_id(Subtree self) { + if (ts_subtree_child_count(self) > 0) { + return self.ptr->production_id; + } else { + return 0; + } +} + +static inline bool ts_subtree_fragile_left(Subtree self) { + return self.data.is_inline ? false : self.ptr->fragile_left; +} + +static inline bool ts_subtree_fragile_right(Subtree self) { + return self.data.is_inline ? false : self.ptr->fragile_right; +} + +static inline bool ts_subtree_has_external_tokens(Subtree self) { + return self.data.is_inline ? false : self.ptr->has_external_tokens; +} + +static inline bool ts_subtree_is_fragile(Subtree self) { + return self.data.is_inline ? false : (self.ptr->fragile_left || self.ptr->fragile_right); +} + +static inline bool ts_subtree_is_error(Subtree self) { + return ts_subtree_symbol(self) == ts_builtin_sym_error; +} + +static inline bool ts_subtree_is_eof(Subtree self) { + return ts_subtree_symbol(self) == ts_builtin_sym_end; +} + +static inline Subtree ts_subtree_from_mut(MutableSubtree self) { + Subtree result; + result.data = self.data; + return result; +} + +static inline MutableSubtree ts_subtree_to_mut_unsafe(Subtree self) { + MutableSubtree result; + result.data = self.data; + return result; +} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_SUBTREE_H_ diff --git a/src/tree_sitter/tree.c b/src/tree_sitter/tree.c new file mode 100644 index 0000000000..04cb1d242f --- /dev/null +++ b/src/tree_sitter/tree.c @@ -0,0 +1,149 @@ +#include "tree_sitter/api.h" +#include "./array.h" +#include "./get_changed_ranges.h" +#include "./subtree.h" +#include "./tree_cursor.h" +#include "./tree.h" + +static const unsigned PARENT_CACHE_CAPACITY = 32; + +TSTree *ts_tree_new( + Subtree root, const TSLanguage *language, + const TSRange *included_ranges, unsigned included_range_count +) { + TSTree *result = ts_malloc(sizeof(TSTree)); + result->root = root; + result->language = language; + result->parent_cache = NULL; + result->parent_cache_start = 0; + result->parent_cache_size = 0; + result->included_ranges = ts_calloc(included_range_count, sizeof(TSRange)); + memcpy(result->included_ranges, included_ranges, included_range_count * sizeof(TSRange)); + result->included_range_count = included_range_count; + return result; +} + +TSTree *ts_tree_copy(const TSTree *self) { + ts_subtree_retain(self->root); + return ts_tree_new(self->root, self->language, self->included_ranges, self->included_range_count); +} + +void ts_tree_delete(TSTree *self) { + if (!self) return; + + SubtreePool pool = ts_subtree_pool_new(0); + ts_subtree_release(&pool, self->root); + ts_subtree_pool_delete(&pool); + ts_free(self->included_ranges); + if (self->parent_cache) ts_free(self->parent_cache); + ts_free(self); +} + +TSNode ts_tree_root_node(const TSTree *self) { + return ts_node_new(self, &self->root, ts_subtree_padding(self->root), 0); +} + +const TSLanguage *ts_tree_language(const TSTree *self) { + return self->language; +} + +void ts_tree_edit(TSTree *self, const TSInputEdit *edit) { + for (unsigned i = 0; i < self->included_range_count; i++) { + TSRange *range = &self->included_ranges[i]; + if (range->end_byte >= edit->old_end_byte) { + if (range->end_byte != UINT32_MAX) { + range->end_byte = edit->new_end_byte + (range->end_byte - edit->old_end_byte); + range->end_point = point_add( + edit->new_end_point, + point_sub(range->end_point, edit->old_end_point) + ); + if (range->end_byte < edit->new_end_byte) { + range->end_byte = UINT32_MAX; + range->end_point = POINT_MAX; + } + } + if (range->start_byte >= edit->old_end_byte) { + range->start_byte = edit->new_end_byte + (range->start_byte - edit->old_end_byte); + range->start_point = point_add( + edit->new_end_point, + point_sub(range->start_point, edit->old_end_point) + ); + if (range->start_byte < edit->new_end_byte) { + range->start_byte = UINT32_MAX; + range->start_point = POINT_MAX; + } + } + } + } + + SubtreePool pool = ts_subtree_pool_new(0); + self->root = ts_subtree_edit(self->root, edit, &pool); + self->parent_cache_start = 0; + self->parent_cache_size = 0; + ts_subtree_pool_delete(&pool); +} + +TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uint32_t *count) { + TSRange *result; + TreeCursor cursor1 = {NULL, array_new()}; + TreeCursor cursor2 = {NULL, array_new()}; + TSNode root = ts_tree_root_node(self); + ts_tree_cursor_init(&cursor1, root); + ts_tree_cursor_init(&cursor2, root); + + TSRangeArray included_range_differences = array_new(); + ts_range_array_get_changed_ranges( + self->included_ranges, self->included_range_count, + other->included_ranges, other->included_range_count, + &included_range_differences + ); + + *count = ts_subtree_get_changed_ranges( + &self->root, &other->root, &cursor1, &cursor2, + self->language, &included_range_differences, &result + ); + + array_delete(&included_range_differences); + array_delete(&cursor1.stack); + array_delete(&cursor2.stack); + return result; +} + +void ts_tree_print_dot_graph(const TSTree *self, FILE *file) { + ts_subtree_print_dot_graph(self->root, self->language, file); +} + +TSNode ts_tree_get_cached_parent(const TSTree *self, const TSNode *node) { + for (uint32_t i = 0; i < self->parent_cache_size; i++) { + uint32_t index = (self->parent_cache_start + i) % PARENT_CACHE_CAPACITY; + ParentCacheEntry *entry = &self->parent_cache[index]; + if (entry->child == node->id) { + return ts_node_new(self, entry->parent, entry->position, entry->alias_symbol); + } + } + return ts_node_new(NULL, NULL, length_zero(), 0); +} + +void ts_tree_set_cached_parent(const TSTree *_self, const TSNode *node, const TSNode *parent) { + TSTree *self = (TSTree *)_self; + if (!self->parent_cache) { + self->parent_cache = ts_calloc(PARENT_CACHE_CAPACITY, sizeof(ParentCacheEntry)); + } + + uint32_t index = (self->parent_cache_start + self->parent_cache_size) % PARENT_CACHE_CAPACITY; + self->parent_cache[index] = (ParentCacheEntry) { + .child = node->id, + .parent = (const Subtree *)parent->id, + .position = { + parent->context[0], + {parent->context[1], parent->context[2]} + }, + .alias_symbol = parent->context[3], + }; + + if (self->parent_cache_size == PARENT_CACHE_CAPACITY) { + self->parent_cache_start++; + } else { + self->parent_cache_size++; + } +} diff --git a/src/tree_sitter/tree.h b/src/tree_sitter/tree.h new file mode 100644 index 0000000000..92a7e64179 --- /dev/null +++ b/src/tree_sitter/tree.h @@ -0,0 +1,34 @@ +#ifndef TREE_SITTER_TREE_H_ +#define TREE_SITTER_TREE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + const Subtree *child; + const Subtree *parent; + Length position; + TSSymbol alias_symbol; +} ParentCacheEntry; + +struct TSTree { + Subtree root; + const TSLanguage *language; + ParentCacheEntry *parent_cache; + uint32_t parent_cache_start; + uint32_t parent_cache_size; + TSRange *included_ranges; + unsigned included_range_count; +}; + +TSTree *ts_tree_new(Subtree root, const TSLanguage *language, const TSRange *, unsigned); +TSNode ts_node_new(const TSTree *, const Subtree *, Length, TSSymbol); +TSNode ts_tree_get_cached_parent(const TSTree *, const TSNode *); +void ts_tree_set_cached_parent(const TSTree *, const TSNode *, const TSNode *); + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_TREE_H_ diff --git a/src/tree_sitter/tree_cursor.c b/src/tree_sitter/tree_cursor.c new file mode 100644 index 0000000000..7103fc411d --- /dev/null +++ b/src/tree_sitter/tree_cursor.c @@ -0,0 +1,302 @@ +#include "tree_sitter/api.h" +#include "./alloc.h" +#include "./tree_cursor.h" +#include "./language.h" +#include "./tree.h" + +typedef struct { + Subtree parent; + const TSTree *tree; + Length position; + uint32_t child_index; + uint32_t structural_child_index; + const TSSymbol *alias_sequence; +} CursorChildIterator; + +// CursorChildIterator + +static inline CursorChildIterator ts_tree_cursor_iterate_children(const TreeCursor *self) { + TreeCursorEntry *last_entry = array_back(&self->stack); + if (ts_subtree_child_count(*last_entry->subtree) == 0) { + return (CursorChildIterator) {NULL_SUBTREE, self->tree, length_zero(), 0, 0, NULL}; + } + const TSSymbol *alias_sequence = ts_language_alias_sequence( + self->tree->language, + last_entry->subtree->ptr->production_id + ); + return (CursorChildIterator) { + .tree = self->tree, + .parent = *last_entry->subtree, + .position = last_entry->position, + .child_index = 0, + .structural_child_index = 0, + .alias_sequence = alias_sequence, + }; +} + +static inline bool ts_tree_cursor_child_iterator_next(CursorChildIterator *self, + TreeCursorEntry *result, + bool *visible) { + if (!self->parent.ptr || self->child_index == self->parent.ptr->child_count) return false; + const Subtree *child = &self->parent.ptr->children[self->child_index]; + *result = (TreeCursorEntry) { + .subtree = child, + .position = self->position, + .child_index = self->child_index, + .structural_child_index = self->structural_child_index, + }; + *visible = ts_subtree_visible(*child); + bool extra = ts_subtree_extra(*child); + if (!extra && self->alias_sequence) { + *visible |= self->alias_sequence[self->structural_child_index]; + self->structural_child_index++; + } + + self->position = length_add(self->position, ts_subtree_size(*child)); + self->child_index++; + + if (self->child_index < self->parent.ptr->child_count) { + Subtree next_child = self->parent.ptr->children[self->child_index]; + self->position = length_add(self->position, ts_subtree_padding(next_child)); + } + + return true; +} + +// TSTreeCursor - lifecycle + +TSTreeCursor ts_tree_cursor_new(TSNode node) { + TSTreeCursor self = {NULL, NULL, {0, 0}}; + ts_tree_cursor_init((TreeCursor *)&self, node); + return self; +} + +void ts_tree_cursor_reset(TSTreeCursor *_self, TSNode node) { + ts_tree_cursor_init((TreeCursor *)_self, node); +} + +void ts_tree_cursor_init(TreeCursor *self, TSNode node) { + self->tree = node.tree; + array_clear(&self->stack); + array_push(&self->stack, ((TreeCursorEntry) { + .subtree = (const Subtree *)node.id, + .position = { + ts_node_start_byte(node), + ts_node_start_point(node) + }, + .child_index = 0, + .structural_child_index = 0, + })); +} + +void ts_tree_cursor_delete(TSTreeCursor *_self) { + TreeCursor *self = (TreeCursor *)_self; + array_delete(&self->stack); +} + +// TSTreeCursor - walking the tree + +bool ts_tree_cursor_goto_first_child(TSTreeCursor *_self) { + TreeCursor *self = (TreeCursor *)_self; + + bool did_descend; + do { + did_descend = false; + + bool visible; + TreeCursorEntry entry; + CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); + while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { + if (visible) { + array_push(&self->stack, entry); + return true; + } + + if (ts_subtree_visible_child_count(*entry.subtree) > 0) { + array_push(&self->stack, entry); + did_descend = true; + break; + } + } + } while (did_descend); + + return false; +} + +int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *_self, uint32_t goal_byte) { + TreeCursor *self = (TreeCursor *)_self; + uint32_t initial_size = self->stack.size; + uint32_t visible_child_index = 0; + + bool did_descend; + do { + did_descend = false; + + bool visible; + TreeCursorEntry entry; + CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); + while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { + uint32_t end_byte = entry.position.bytes + ts_subtree_size(*entry.subtree).bytes; + bool at_goal = end_byte > goal_byte; + uint32_t visible_child_count = ts_subtree_visible_child_count(*entry.subtree); + + if (at_goal) { + if (visible) { + array_push(&self->stack, entry); + return visible_child_index; + } + + if (visible_child_count > 0) { + array_push(&self->stack, entry); + did_descend = true; + break; + } + } else if (visible) { + visible_child_index++; + } else { + visible_child_index += visible_child_count; + } + } + } while (did_descend); + + if (self->stack.size > initial_size && + ts_tree_cursor_goto_next_sibling((TSTreeCursor *)self)) { + return visible_child_index; + } + + self->stack.size = initial_size; + return -1; +} + +bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *_self) { + TreeCursor *self = (TreeCursor *)_self; + uint32_t initial_size = self->stack.size; + + while (self->stack.size > 1) { + TreeCursorEntry entry = array_pop(&self->stack); + CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); + iterator.child_index = entry.child_index; + iterator.structural_child_index = entry.structural_child_index; + iterator.position = entry.position; + + bool visible = false; + ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible); + if (visible && self->stack.size + 1 < initial_size) break; + + while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { + if (visible) { + array_push(&self->stack, entry); + return true; + } + + if (ts_subtree_visible_child_count(*entry.subtree)) { + array_push(&self->stack, entry); + ts_tree_cursor_goto_first_child(_self); + return true; + } + } + } + + self->stack.size = initial_size; + return false; +} + +bool ts_tree_cursor_goto_parent(TSTreeCursor *_self) { + TreeCursor *self = (TreeCursor *)_self; + for (unsigned i = self->stack.size - 2; i + 1 > 0; i--) { + TreeCursorEntry *entry = &self->stack.contents[i]; + bool is_aliased = false; + if (i > 0) { + TreeCursorEntry *parent_entry = &self->stack.contents[i - 1]; + const TSSymbol *alias_sequence = ts_language_alias_sequence( + self->tree->language, + parent_entry->subtree->ptr->production_id + ); + is_aliased = alias_sequence && alias_sequence[entry->structural_child_index]; + } + if (ts_subtree_visible(*entry->subtree) || is_aliased) { + self->stack.size = i + 1; + return true; + } + } + return false; +} + +TSNode ts_tree_cursor_current_node(const TSTreeCursor *_self) { + const TreeCursor *self = (const TreeCursor *)_self; + TreeCursorEntry *last_entry = array_back(&self->stack); + TSSymbol alias_symbol = 0; + if (self->stack.size > 1) { + TreeCursorEntry *parent_entry = &self->stack.contents[self->stack.size - 2]; + const TSSymbol *alias_sequence = ts_language_alias_sequence( + self->tree->language, + parent_entry->subtree->ptr->production_id + ); + if (alias_sequence && !ts_subtree_extra(*last_entry->subtree)) { + alias_symbol = alias_sequence[last_entry->structural_child_index]; + } + } + return ts_node_new( + self->tree, + last_entry->subtree, + last_entry->position, + alias_symbol + ); +} + +TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *_self) { + const TreeCursor *self = (const TreeCursor *)_self; + + // Walk up the tree, visiting the current node and its invisible ancestors. + for (unsigned i = self->stack.size - 1; i > 0; i--) { + TreeCursorEntry *entry = &self->stack.contents[i]; + TreeCursorEntry *parent_entry = &self->stack.contents[i - 1]; + + // Stop walking up when another visible node is found. + if (i != self->stack.size - 1) { + if (ts_subtree_visible(*entry->subtree)) break; + const TSSymbol *alias_sequence = ts_language_alias_sequence( + self->tree->language, + parent_entry->subtree->ptr->production_id + ); + if (alias_sequence && alias_sequence[entry->structural_child_index]) { + break; + } + } + + const TSFieldMapEntry *field_map, *field_map_end; + ts_language_field_map( + self->tree->language, + parent_entry->subtree->ptr->production_id, + &field_map, &field_map_end + ); + + while (field_map < field_map_end) { + if ( + !field_map->inherited && + field_map->child_index == entry->structural_child_index + ) return field_map->field_id; + field_map++; + } + } + return 0; +} + +const char *ts_tree_cursor_current_field_name(const TSTreeCursor *_self) { + TSFieldId id = ts_tree_cursor_current_field_id(_self); + if (id) { + const TreeCursor *self = (const TreeCursor *)_self; + return self->tree->language->field_names[id]; + } else { + return NULL; + } +} + +TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *_cursor) { + const TreeCursor *cursor = (const TreeCursor *)_cursor; + TSTreeCursor res = {NULL, NULL, {0, 0}}; + TreeCursor *copy = (TreeCursor *)&res; + copy->tree = cursor->tree; + array_push_all(©->stack, &cursor->stack); + return res; +} diff --git a/src/tree_sitter/tree_cursor.h b/src/tree_sitter/tree_cursor.h new file mode 100644 index 0000000000..55bdad86da --- /dev/null +++ b/src/tree_sitter/tree_cursor.h @@ -0,0 +1,20 @@ +#ifndef TREE_SITTER_TREE_CURSOR_H_ +#define TREE_SITTER_TREE_CURSOR_H_ + +#include "./subtree.h" + +typedef struct { + const Subtree *subtree; + Length position; + uint32_t child_index; + uint32_t structural_child_index; +} TreeCursorEntry; + +typedef struct { + const TSTree *tree; + Array(TreeCursorEntry) stack; +} TreeCursor; + +void ts_tree_cursor_init(TreeCursor *, TSNode); + +#endif // TREE_SITTER_TREE_CURSOR_H_ diff --git a/src/tree_sitter/utf16.c b/src/tree_sitter/utf16.c new file mode 100644 index 0000000000..3956c01cb9 --- /dev/null +++ b/src/tree_sitter/utf16.c @@ -0,0 +1,33 @@ +#include "./utf16.h" + +utf8proc_ssize_t utf16_iterate( + const utf8proc_uint8_t *string, + utf8proc_ssize_t length, + utf8proc_int32_t *code_point +) { + if (length < 2) { + *code_point = -1; + return 0; + } + + uint16_t *units = (uint16_t *)string; + uint16_t unit = units[0]; + + if (unit < 0xd800 || unit >= 0xe000) { + *code_point = unit; + return 2; + } + + if (unit < 0xdc00) { + if (length >= 4) { + uint16_t next_unit = units[1]; + if (next_unit >= 0xdc00 && next_unit < 0xe000) { + *code_point = 0x10000 + ((unit - 0xd800) << 10) + (next_unit - 0xdc00); + return 4; + } + } + } + + *code_point = -1; + return 2; +} diff --git a/src/tree_sitter/utf16.h b/src/tree_sitter/utf16.h new file mode 100644 index 0000000000..32fd05e6db --- /dev/null +++ b/src/tree_sitter/utf16.h @@ -0,0 +1,21 @@ +#ifndef TREE_SITTER_UTF16_H_ +#define TREE_SITTER_UTF16_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include "utf8proc.h" + +// Analogous to utf8proc's utf8proc_iterate function. Reads one code point from +// the given UTF16 string and stores it in the location pointed to by `code_point`. +// Returns the number of bytes in `string` that were read. +utf8proc_ssize_t utf16_iterate(const utf8proc_uint8_t *, utf8proc_ssize_t, utf8proc_int32_t *); + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_UTF16_H_ From 8ff2f193bb3ed94ee215c83c13431d45d382949b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Fri, 7 Jun 2019 14:05:26 +0200 Subject: [PATCH 0062/1293] tree-sitter: change vendored tree-sitter to use nvim memory management --- src/tree_sitter/alloc.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/tree_sitter/alloc.h b/src/tree_sitter/alloc.h index c8fe6c6e6d..2229995bd1 100644 --- a/src/tree_sitter/alloc.h +++ b/src/tree_sitter/alloc.h @@ -9,7 +9,20 @@ extern "C" { #include #include -#if defined(TREE_SITTER_TEST) +#include "nvim/memory.h" + +#if 1 + +static inline bool ts_toggle_allocation_recording(bool value) { + return false; +} + +#define ts_malloc xmalloc +#define ts_calloc xcalloc +#define ts_realloc xrealloc +#define ts_free xfree + +#elif defined(TREE_SITTER_TEST) void *ts_record_malloc(size_t); void *ts_record_calloc(size_t, size_t); From cd100963866b2c33a286cbf6aac8e42cd16fd248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Mon, 29 Oct 2018 19:11:41 +0100 Subject: [PATCH 0063/1293] tree-sitter: initial tree-sitter support --- CMakeLists.txt | 5 + runtime/lua/treesitter_rt.lua | 119 +++++++ runtime/plugin/ts_test.vim | 32 ++ src/nvim/CMakeLists.txt | 15 +- src/nvim/lua/executor.c | 42 +++ src/nvim/lua/tree_sitter.c | 472 ++++++++++++++++++++++++++ src/nvim/lua/tree_sitter.h | 10 + third-party/CMakeLists.txt | 6 + third-party/cmake/BuildUtf8proc.cmake | 16 + 9 files changed, 713 insertions(+), 4 deletions(-) create mode 100644 runtime/lua/treesitter_rt.lua create mode 100644 runtime/plugin/ts_test.vim create mode 100644 src/nvim/lua/tree_sitter.c create mode 100644 src/nvim/lua/tree_sitter.h create mode 100644 third-party/cmake/BuildUtf8proc.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f9bd87c085..2b03a1cc1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -405,6 +405,11 @@ if(MSGPACK_HAS_FLOAT32) add_definitions(-DNVIM_MSGPACK_HAS_FLOAT32) endif() +set(TREESITTER_PATH "${DEPS_BUILD_DIR}/build/src/treesitter") +set(TREESITTER_C_PATH "${DEPS_BUILD_DIR}/build/src/treesitter-c") +set(TREESITTER_JAVASCRIPT_PATH "${DEPS_BUILD_DIR}/build/src/treesitter-javascript") +set(LIBUTF8PROC_INCLUDE_DIRS ${DEPS_BUILD_DIR}/build/src/utf8proc) + option(FEAT_TUI "Enable the Terminal UI" ON) if(FEAT_TUI) diff --git a/runtime/lua/treesitter_rt.lua b/runtime/lua/treesitter_rt.lua new file mode 100644 index 0000000000..ba7849abd6 --- /dev/null +++ b/runtime/lua/treesitter_rt.lua @@ -0,0 +1,119 @@ +local a = vim.api + +if __treesitter_rt_ns == nil then + __treesitter_rt_ns = a.nvim_buf_add_highlight(0, 0, "", 0, 0, 0) + __treesitter_rt_syn_ns = a.nvim_buf_add_highlight(0, 0, "", 0, 0, 0) +end +local my_ns = __treesitter_rt_ns +local my_syn_ns = __treesitter_rt_syn_ns + +local path = '.deps/build/src/treesitter-javascript/src/highlights.json' +a.nvim_set_var("_ts_path", path) +obj = a.nvim_eval("json_decode(readfile(g:_ts_path,'b'))") + + +--luadev = require'luadev' +--i = require'inspect' + + +function parse_tree(tsstate, force) + if tsstate.valid and not force then + return tsstate.tree + end + tsstate.tree = tsstate.parser:parse_buf(tsstate.bufnr) + tsstate.valid = true + return tsstate.tree +end + +function the_cb(tsstate, ev, bufnr, tick, start_row, oldstopline, stop_row) + local start_byte = a.nvim_buf_get_offset(bufnr,start_row) + -- a bit messy, should we expose edited but not reparsed tree? + -- are multiple edits safe in general? + local root = tsstate.parser:tree():root() + -- TODO: add proper lookup function! + local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0) + local edit + if inode == nil then + local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) + tsstate.parser:edit(start_byte,stop_byte,stop_byte,start_row,0,stop_row,0,stop_row,0) + else + local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start() + local fake_rows = fakeoldstoprow-oldstopline + local fakestop = stop_row+fake_rows + local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol + tsstate.parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol) + end + tsstate.valid = false + --luadev.append_buf({i{edit.start_byte,edit.old_end_byte,edit.new_end_byte}, + -- i{edit.start_point, edit.old_end_point, edit.new_end_point}}) +end + +function attach_buf(tsstate) + local function cb(ev, ...) + return the_cb(tsstate, ev, ...) + end + a.nvim_buf_attach(tsstate.bufnr, false, {on_lines=cb}) +end + +function create_parser(bufnr) + if bufnr == 0 then + bufnr = a.nvim_get_current_buf() + end + local ft = a.nvim_buf_get_option(bufnr, "filetype") + local tsstate = {} + tsstate.bufnr = bufnr + tsstate.parser = vim.ts_parser(ft) + parse_tree(tsstate) + attach_buf(tsstate) + return tsstate +end + +function ts_inspect_pos(row,col) + local tree = parse_tree(theparser) + local root = tree:root() + local node = root:descendant_for_point_range(row,col,row,col) + show_node(node) +end + +function show_node(node) + if node == nil then + return + end + a.nvim_buf_clear_highlight(0, my_ns, 0, -1) + shown_node = node + print(node:type()) + local start_row, start_col, end_row, end_col = node:range() + + a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", start_row, start_col, start_col+1) + + if end_col >= 1 then + end_col = end_col - 1 + end + a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", end_row, end_col, end_col+1) +end + +function ts_expand_node() + if shown_node == nil then + return + end + parent = shown_node:parent() + show_node(parent) +end + +function ts_cursor() + local row, col = unpack(a.nvim_win_get_cursor(0)) + ts_inspect_pos(row-1, col) +end + +if false then + ctree = theparser.tree + root = ctree:root() + cursor = root:to_cursor() + node = cursor:forward(5000) if true then return node end + print(#root) + c = root:child(50) + print(require'inspect'{c:extent()}) + type(ctree.__tostring) + root:__tostring() + print(_tslua_debug()) +end diff --git a/runtime/plugin/ts_test.vim b/runtime/plugin/ts_test.vim new file mode 100644 index 0000000000..e40e2792e1 --- /dev/null +++ b/runtime/plugin/ts_test.vim @@ -0,0 +1,32 @@ +let g:ts_test_path = expand(":p:h:h") +let g:has_ts = v:false + +func! TSTest() + if g:has_ts + return + end + " TODO: module! + lua require'treesitter_rt' + lua theparser = create_parser(vim.api.nvim_get_current_buf()) + let g:has_ts = v:true +endfunc + +func! TSCursor() + " disable matchparen + NoMatchParen + call TSTest() + au CursorMoved lua ts_cursor() + au CursorMovedI lua ts_cursor() + map (ts-expand) lua ts_expand_node() +endfunc + +func! TSSyntax() + " disable matchparen + set syntax= + call TSTest() + lua ts_syntax() +endfunc + +command! TSTest call TSTest() +command! TSCursor call TSCursor() +command! TSSyntax call TSSyntax() diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index aa8100873b..ebc7e96d66 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -74,6 +74,8 @@ include_directories(${GENERATED_DIR}) include_directories(${CACHED_GENERATED_DIR}) include_directories(${GENERATED_INCLUDES_DIR}) +include_directories(${LIBUTF8PROC_INCLUDE_DIRS}) + file(MAKE_DIRECTORY ${TOUCHES_DIR}) file(MAKE_DIRECTORY ${GENERATED_DIR}) file(MAKE_DIRECTORY ${GENERATED_INCLUDES_DIR}) @@ -85,6 +87,10 @@ file(GLOB NVIM_HEADERS *.h) file(GLOB XDIFF_SOURCES xdiff/*.c) file(GLOB XDIFF_HEADERS xdiff/*.h) +# when LIBUTF8PROC build is fixed, don't use lib.c with amalgamated utf8proc.c +#file(GLOB TS_SOURCES tree_sitter/*.c) +file(GLOB TS_SOURCES ../tree_sitter/lib.c) + foreach(subdir os api @@ -141,6 +147,7 @@ set(CONV_SOURCES ex_cmds.c ex_docmd.c fileio.c + lua/tree_sitter.c mbyte.c memline.c message.c @@ -158,7 +165,7 @@ foreach(sfile ${CONV_SOURCES}) endif() endforeach() # xdiff: inlined external project, we don't maintain it. #9306 -list(APPEND CONV_SOURCES ${XDIFF_SOURCES}) +list(APPEND CONV_SOURCES ${XDIFF_SOURCES} ${TS_SOURCES}) if(NOT MSVC) set_source_files_properties( @@ -414,7 +421,7 @@ endif() add_executable(nvim ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS} ${NVIM_GENERATED_SOURCES} ${NVIM_SOURCES} ${NVIM_HEADERS} - ${XDIFF_SOURCES} ${XDIFF_HEADERS}) + ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TS_SOURCES}) target_link_libraries(nvim ${NVIM_EXEC_LINK_LIBRARIES}) install_helper(TARGETS nvim) @@ -500,7 +507,7 @@ add_library( EXCLUDE_FROM_ALL ${NVIM_SOURCES} ${NVIM_GENERATED_SOURCES} ${NVIM_HEADERS} ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS} - ${XDIFF_SOURCES} ${XDIFF_HEADERS} + ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TS_SOURCES} ) set_property(TARGET libnvim APPEND PROPERTY INCLUDE_DIRECTORIES ${LUA_PREFERRED_INCLUDE_DIRS}) @@ -525,7 +532,7 @@ else() EXCLUDE_FROM_ALL ${NVIM_SOURCES} ${NVIM_GENERATED_SOURCES} ${NVIM_HEADERS} ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS} - ${XDIFF_SOURCES} ${XDIFF_HEADERS} + ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TS_SOURCES} ${UNIT_TEST_FIXTURES} ) target_link_libraries(nvim-test ${NVIM_TEST_LINK_LIBRARIES}) diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index f51aa3c6d4..8b0140f794 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -31,6 +31,7 @@ #include "nvim/lua/executor.h" #include "nvim/lua/converter.h" +#include "nvim/lua/tree_sitter.h" #include "luv/luv.h" @@ -310,7 +311,11 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_setfield(lstate, -2, "luv"); lua_pop(lstate, 3); + // internal vim._treesitter... API + nlua_add_treesitter(lstate); + lua_setglobal(lstate, "vim"); + return 0; } @@ -816,3 +821,40 @@ void ex_luafile(exarg_T *const eap) return; } } + +static int unsafe_ptr_to_ts_tree(lua_State *L) +{ + if (!lua_gettop(L)) { + return 0; + } + TSTree *const *ptr = lua_topointer(L,1); + tslua_push_tree(L, *ptr); + return 1; +} + +static int create_tslua_parser(lua_State *L) +{ + TSLanguage *tree_sitter_c(void), *tree_sitter_javascript(void); + + if (!lua_gettop(L)) { + return 0; + } + char *str = lua_tostring(L,1); + + TSLanguage *lang = tree_sitter_c(); + if (str && striequal(str, "javascript")) { + lang = tree_sitter_javascript(); + } + tslua_push_parser(L, lang); + return 1; +} + +static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL +{ + tslua_init(lstate); + lua_pushcfunction(lstate, unsafe_ptr_to_ts_tree); + lua_setfield(lstate, -2, "unsafe_ts_tree"); + + lua_pushcfunction(lstate, create_tslua_parser); + lua_setfield(lstate, -2, "ts_parser"); +} diff --git a/src/nvim/lua/tree_sitter.c b/src/nvim/lua/tree_sitter.c new file mode 100644 index 0000000000..1ecb2bcac4 --- /dev/null +++ b/src/nvim/lua/tree_sitter.c @@ -0,0 +1,472 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +// lua bindings for tree-siter. +// NB: this file should contain a generic lua interface for +// tree-sitter trees and nodes, and could be broken out as a reusable library + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "tree_sitter/api.h" + +// NOT state-safe, delete when GC is confimed working: +static int debug_n_trees = 0, debug_n_cursors = 0; + +#define REG_KEY "tree_sitter-private" + +#include "nvim/lua/tree_sitter.h" +#include "nvim/api/private/handle.h" +#include "nvim/memline.h" + +typedef struct { + TSParser *parser; + TSTree *tree; +} Tslua_parser; + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "lua/tree_sitter.c.generated.h" +#endif + +static struct luaL_Reg parser_meta[] = { + {"__gc", parser_gc}, + {"__tostring", parser_tostring}, + {"parse_buf", parser_parse_buf}, + {"edit", parser_edit}, + {"tree", parser_tree}, + {NULL, NULL} +}; + +static struct luaL_Reg tree_meta[] = { + {"__gc", tree_gc}, + {"__tostring", tree_tostring}, + {"root", tree_root}, + {NULL, NULL} +}; + +static struct luaL_Reg node_meta[] = { + {"__tostring", node_tostring}, + {"__len", node_child_count}, + {"range", node_range}, + {"start", node_start}, + {"type", node_type}, + {"symbol", node_symbol}, + {"child_count", node_child_count}, + {"child", node_child}, + {"descendant_for_point_range", node_descendant_for_point_range}, + {"parent", node_parent}, + {NULL, NULL} +}; + +void build_meta(lua_State *L, const luaL_Reg *meta) +{ + // [env, target] + for (size_t i = 0; meta[i].name != NULL; i++) { + lua_pushcfunction(L, meta[i].func); // [env, target, func] + lua_pushvalue(L, -3); // [env, target, func, env] + lua_setfenv(L, -2); // [env, target, func] + lua_setfield(L, -2, meta[i].name); // [env, target] + } + + lua_pushvalue(L, -1); // [env, target, target] + lua_setfield(L, -2, "__index"); // [env, target] +} + + + +/// init the tslua library +/// +/// all global state is stored in the regirstry of the lua_State +void tslua_init(lua_State *L) +{ + lua_createtable(L, 0, 0); + + // type metatables + lua_createtable(L, 0, 0); + build_meta(L, parser_meta); + lua_setfield(L, -2, "parser-meta"); + + lua_createtable(L, 0, 0); + build_meta(L, tree_meta); + lua_setfield(L, -2, "tree-meta"); + + lua_createtable(L, 0, 0); + build_meta(L, node_meta); + lua_setfield(L, -2, "node-meta"); + + lua_setfield(L, LUA_REGISTRYINDEX, REG_KEY); + + lua_pushcfunction(L, tslua_debug); + lua_setglobal(L, "_tslua_debug"); +} + +static int tslua_debug(lua_State *L) +{ + lua_pushinteger(L, debug_n_trees); + lua_pushinteger(L, debug_n_cursors); + return 2; +} + +void tslua_push_parser(lua_State *L, TSLanguage *lang) +{ + TSParser *parser = ts_parser_new(); + ts_parser_set_language(parser, lang); + Tslua_parser *p = lua_newuserdata(L, sizeof(Tslua_parser)); // [udata] + p->parser = parser; + p->tree = NULL; + + lua_getfield(L, LUA_REGISTRYINDEX, REG_KEY); // [udata, env] + lua_getfield(L, -1, "parser-meta"); // [udata, env, meta] + lua_setmetatable(L, -3); // [udata, env] + lua_pop(L, 1); // [udata] +} + +static Tslua_parser *parser_check(lua_State *L) +{ + if (!lua_gettop(L)) { + return 0; + } + if (!lua_isuserdata(L, 1)) { + return 0; + } + // TODO: typecheck! + return lua_touserdata(L, 1); +} + +static int parser_gc(lua_State *L) +{ + Tslua_parser *p = parser_check(L); + if (!p) { + return 0; + } + + ts_parser_delete(p->parser); + if (p->tree) { + ts_tree_delete(p->tree); + } + + return 0; +} + +static int parser_tostring(lua_State *L) +{ + lua_pushstring(L, ""); + return 1; +} + +static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read) +{ + buf_T *bp = payload; + static char buf[200]; + if ((linenr_T)position.row >= bp->b_ml.ml_line_count) { + *bytes_read = 0; + return ""; + } + char_u *line = ml_get_buf(bp, position.row+1, false); + size_t len = STRLEN(line); + size_t tocopy = MIN(len-position.column,200); + + // TODO: translate embedded \n to \000 + memcpy(buf, line+position.column, tocopy); + *bytes_read = (uint32_t)tocopy; + if (tocopy < 200) { + buf[tocopy] = '\n'; + (*bytes_read)++; + } + return buf; +} + +static int parser_parse_buf(lua_State *L) +{ + Tslua_parser *p = parser_check(L); + if (!p) { + return 0; + } + + long bufnr = lua_tointeger(L, 2); + void *payload = handle_get_buffer(bufnr); + TSInput input = {payload, input_cb, TSInputEncodingUTF8}; + TSTree *new_tree = ts_parser_parse(p->parser, p->tree, input); + if (p->tree) { + ts_tree_delete(p->tree); + } + p->tree = new_tree; + + tslua_push_tree(L, ts_tree_copy(p->tree)); + return 1; +} + +static int parser_tree(lua_State *L) +{ + Tslua_parser *p = parser_check(L); + if (!p) { + return 0; + } + + if (p->tree) { + tslua_push_tree(L, ts_tree_copy(p->tree)); + } else { + lua_pushnil(L); + } + return 1; +} + +static int parser_edit(lua_State *L) +{ + if(lua_gettop(L) < 10) { + lua_pushstring(L, "not enough args to parser:edit()"); + lua_error(L); + return 0; // unreachable + } + + Tslua_parser *p = parser_check(L); + if (!p) { + return 0; + } + + if (!p->tree) { + return 0; + } + + long start_byte = lua_tointeger(L, 2); + long old_end_byte = lua_tointeger(L, 3); + long new_end_byte = lua_tointeger(L, 4); + TSPoint start_point = { lua_tointeger(L, 5), lua_tointeger(L, 6) }; + TSPoint old_end_point = { lua_tointeger(L, 7), lua_tointeger(L, 8) }; + TSPoint new_end_point = { lua_tointeger(L, 9), lua_tointeger(L, 10) }; + + TSInputEdit edit = { start_byte, old_end_byte, new_end_byte, + start_point, old_end_point, new_end_point }; + + ts_tree_edit(p->tree, &edit); + + return 0; +} + + +// Tree methods + +/// push tree interface on lua stack. +/// +/// This takes "ownership" of the tree and will free it +/// when the wrapper object is garbage collected +void tslua_push_tree(lua_State *L, TSTree *tree) +{ + TSTree **ud = lua_newuserdata(L, sizeof(TSTree *)); // [udata] + *ud = tree; + lua_getfield(L, LUA_REGISTRYINDEX, REG_KEY); // [udata, env] + lua_getfield(L, -1, "tree-meta"); // [udata, env, meta] + lua_setmetatable(L, -3); // [udata, env] + lua_pop(L, 1); // [udata] + + // table used for node wrappers to keep a reference to tree wrapper + // NB: in lua 5.3 the uservalue for the node could just be the tree, but + // in lua 5.1 the uservalue (fenv) must be a table. + lua_createtable(L, 1, 0); // [udata, reftable] + lua_pushvalue(L, -2); // [udata, reftable, udata] + lua_rawseti(L, -2, 1); // [udata, reftable] + lua_setfenv(L, -2); // [udata] + debug_n_trees++; +} + +static TSTree *tree_check(lua_State *L) +{ + if (!lua_gettop(L)) { + return 0; + } + if (!lua_isuserdata(L, 1)) { + return 0; + } + // TODO: typecheck! + TSTree **ud = lua_touserdata(L, 1); + return *ud; +} + +static int tree_gc(lua_State *L) +{ + TSTree *tree = tree_check(L); + if (!tree) { + return 0; + } + + ts_tree_delete(tree); + debug_n_trees--; + return 0; +} + +static int tree_tostring(lua_State *L) +{ + lua_pushstring(L, ""); + return 1; +} + +static int tree_root(lua_State *L) +{ + TSTree *tree = tree_check(L); + if (!tree) { + return 0; + } + TSNode root = ts_tree_root_node(tree); + push_node(L, root); + return 1; +} + +// Node methods + +/// push node interface on lua stack +/// +/// top of stack must either be the tree this node belongs to or another node +/// of the same tree! This value is not popped. Can only be called inside a +/// cfunction with the tslua environment. +static void push_node(lua_State *L, TSNode node) +{ + if (ts_node_is_null(node)) { + lua_pushnil(L); // [src, nil] + return; + } + TSNode *ud = lua_newuserdata(L, sizeof(TSNode)); // [src, udata] + *ud = node; + lua_getfield(L, LUA_ENVIRONINDEX, "node-meta"); // [src, udata, meta] + lua_setmetatable(L, -2); // [src, udata] + lua_getfenv(L, -2); // [src, udata, reftable] + lua_setfenv(L, -2); // [src, udata] +} + +static bool node_check(lua_State *L, TSNode *res) +{ + if (!lua_gettop(L)) { + return 0; + } + if (!lua_isuserdata(L, 1)) { + return 0; + } + // TODO: typecheck! + TSNode *ud = lua_touserdata(L, 1); + *res = *ud; + return true; +} + + +static int node_tostring(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + lua_pushstring(L, ""); + lua_concat(L, 3); + return 1; +} + +static int node_range(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSPoint start = ts_node_start_point(node); + TSPoint end = ts_node_end_point(node); + lua_pushnumber(L, start.row); + lua_pushnumber(L, start.column); + lua_pushnumber(L, end.row); + lua_pushnumber(L, end.column); + return 4; +} + +static int node_start(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSPoint start = ts_node_start_point(node); + uint32_t start_byte = ts_node_start_byte(node); + lua_pushnumber(L, start.row); + lua_pushnumber(L, start.column); + lua_pushnumber(L, start_byte); + return 3; +} + +static int node_child_count(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + uint32_t count = ts_node_child_count(node); + lua_pushnumber(L, count); + return 1; +} + +static int node_type(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + lua_pushstring(L, ts_node_type(node)); + return 1; +} + +static int node_symbol(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSSymbol symbol = ts_node_symbol(node); + lua_pushnumber(L, symbol); + return 1; +} + +static int node_child(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + long num = lua_tointeger(L, 2); + TSNode child = ts_node_child(node, (uint32_t)num); + + lua_pushvalue(L, 1); + push_node(L, child); + return 1; +} + +static int node_descendant_for_point_range(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSPoint start = {(uint32_t)lua_tointeger(L, 2), + (uint32_t)lua_tointeger(L, 3)}; + TSPoint end = {(uint32_t)lua_tointeger(L, 4), + (uint32_t)lua_tointeger(L, 5)}; + TSNode child = ts_node_descendant_for_point_range(node, start, end); + + lua_pushvalue(L, 1); + push_node(L, child); + return 1; +} + +static int node_parent(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSNode parent = ts_node_parent(node); + push_node(L, parent); + return 1; +} + diff --git a/src/nvim/lua/tree_sitter.h b/src/nvim/lua/tree_sitter.h new file mode 100644 index 0000000000..2ae0ec8371 --- /dev/null +++ b/src/nvim/lua/tree_sitter.h @@ -0,0 +1,10 @@ +#ifndef NVIM_LUA_TREE_SITTER_H +#define NVIM_LUA_TREE_SITTER_H + +#include "tree_sitter/api.h" + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "lua/tree_sitter.h.generated.h" +#endif + +#endif // NVIM_LUA_TREE_SITTER_H diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index c555151c35..a848a57047 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -195,6 +195,9 @@ set(GETTEXT_SHA256 ff942af0e438ced4a8b0ea4b0b6e0d6d657157c5e2364de57baa279c1c125 set(LIBICONV_URL https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz) set(LIBICONV_SHA256 ccf536620a45458d26ba83887a983b96827001e92a13847b45e4925cc8913178) +set(UTF8PROC_URL https://github.com/JuliaStrings/utf8proc/archive/v2.2.0.tar.gz) +set(UTF8PROC_SHA256 3f8fd1dbdb057ee5ba584a539d5cd1b3952141c0338557cb0bdf8cb9cfed5dbf) + if(USE_BUNDLED_UNIBILIUM) include(BuildUnibilium) endif() @@ -246,6 +249,9 @@ if(USE_BUNDLED_LIBICONV) include(BuildLibiconv) endif() +# TODO: build it as a normal lib, so it can be used as a distro lib when available +include(BuildUtf8proc) + if(WIN32) include(GetBinaryDeps) diff --git a/third-party/cmake/BuildUtf8proc.cmake b/third-party/cmake/BuildUtf8proc.cmake new file mode 100644 index 0000000000..df287ea459 --- /dev/null +++ b/third-party/cmake/BuildUtf8proc.cmake @@ -0,0 +1,16 @@ +ExternalProject_Add(utf8proc +PREFIX ${DEPS_BUILD_DIR} +URL ${UTF8PROC_URL} +DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/utf8proc +DOWNLOAD_COMMAND ${CMAKE_COMMAND} + -DPREFIX=${DEPS_BUILD_DIR} + -DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/utf8proc + -DURL=${UTF8PROC_URL} + -DEXPECTED_SHA256=${UTF8PROC_SHA256} + -DTARGET=utf8proc + -DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR} + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake +CONFIGURE_COMMAND true +BUILD_COMMAND true +INSTALL_COMMAND true +) From 0e0beef85e4d3932e0d49528d8474794f7b69b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Thu, 6 Jun 2019 12:20:07 +0200 Subject: [PATCH 0064/1293] tree-sitter: load parsers as .so files --- runtime/lua/treesitter_rt.lua | 6 +----- src/nvim/lua/executor.c | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/runtime/lua/treesitter_rt.lua b/runtime/lua/treesitter_rt.lua index ba7849abd6..1dea22000a 100644 --- a/runtime/lua/treesitter_rt.lua +++ b/runtime/lua/treesitter_rt.lua @@ -7,10 +7,6 @@ end local my_ns = __treesitter_rt_ns local my_syn_ns = __treesitter_rt_syn_ns -local path = '.deps/build/src/treesitter-javascript/src/highlights.json' -a.nvim_set_var("_ts_path", path) -obj = a.nvim_eval("json_decode(readfile(g:_ts_path,'b'))") - --luadev = require'luadev' --i = require'inspect' @@ -62,7 +58,7 @@ function create_parser(bufnr) local ft = a.nvim_buf_get_option(bufnr, "filetype") local tsstate = {} tsstate.bufnr = bufnr - tsstate.parser = vim.ts_parser(ft) + tsstate.parser = vim.ts_parser(ft.."_parser.so", ft) parse_tree(tsstate) attach_buf(tsstate) return tsstate diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 8b0140f794..1794cee8d8 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -834,16 +834,31 @@ static int unsafe_ptr_to_ts_tree(lua_State *L) static int create_tslua_parser(lua_State *L) { - TSLanguage *tree_sitter_c(void), *tree_sitter_javascript(void); - - if (!lua_gettop(L)) { + if (lua_gettop(L) < 2) { return 0; } - char *str = lua_tostring(L,1); + const char *path = lua_tostring(L,1); + const char *lang_name = lua_tostring(L,2); - TSLanguage *lang = tree_sitter_c(); - if (str && striequal(str, "javascript")) { - lang = tree_sitter_javascript(); + // TODO: unsafe! + char symbol_buf[128] = "tree_sitter_"; + STRCAT(symbol_buf, lang_name); + + // TODO: we should maybe keep the uv_lib_t around, and close them + // at exit, to keep LeakSanitizer happy. + uv_lib_t lib; + if (uv_dlopen(path, &lib)) { + return luaL_error(L, "uv_dlopen: %s", uv_dlerror(&lib)); + } + + TSLanguage *(*lang_parser)(void); + if (uv_dlsym(&lib, symbol_buf, (void **)&lang_parser)) { + return luaL_error(L, "uv_dlsym: %s", uv_dlerror(&lib)); + } + + TSLanguage *lang = lang_parser(); + if (lang == NULL) { + return luaL_error(L, "failed to load parser"); } tslua_push_parser(L, lang); return 1; From b871100be7a6ae5ce1db23a688c593ecaa2390b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Thu, 6 Jun 2019 14:13:10 +0200 Subject: [PATCH 0065/1293] Create BuildUtf8proc.cmake and FindUtf8proc.cmake Using advanced search and replace technology --- CMakeLists.txt | 12 ++-- cmake/FindUtf8proc.cmake | 54 +++++++++++++++++ src/nvim/CMakeLists.txt | 8 +-- third-party/CMakeLists.txt | 6 +- third-party/cmake/BuildUtf8proc.cmake | 84 ++++++++++++++++++++++----- 5 files changed, 137 insertions(+), 27 deletions(-) create mode 100644 cmake/FindUtf8proc.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b03a1cc1a..8a4b21f4f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -375,6 +375,13 @@ include_directories(SYSTEM ${MSGPACK_INCLUDE_DIRS}) find_package(LibLUV 1.30.0 REQUIRED) include_directories(SYSTEM ${LIBLUV_INCLUDE_DIRS}) +find_package(Utf8proc REQUIRED) +include_directories(SYSTEM ${UTF8PROC_INCLUDE_DIRS}) +if(WIN32) + add_definitions(-DUTF8PROC_STATIC) +endif() + + # Note: The test lib requires LuaJIT; it will be skipped if LuaJIT is missing. option(PREFER_LUA "Prefer Lua over LuaJIT in the nvim executable." OFF) @@ -405,11 +412,6 @@ if(MSGPACK_HAS_FLOAT32) add_definitions(-DNVIM_MSGPACK_HAS_FLOAT32) endif() -set(TREESITTER_PATH "${DEPS_BUILD_DIR}/build/src/treesitter") -set(TREESITTER_C_PATH "${DEPS_BUILD_DIR}/build/src/treesitter-c") -set(TREESITTER_JAVASCRIPT_PATH "${DEPS_BUILD_DIR}/build/src/treesitter-javascript") -set(LIBUTF8PROC_INCLUDE_DIRS ${DEPS_BUILD_DIR}/build/src/utf8proc) - option(FEAT_TUI "Enable the Terminal UI" ON) if(FEAT_TUI) diff --git a/cmake/FindUtf8proc.cmake b/cmake/FindUtf8proc.cmake new file mode 100644 index 0000000000..dc4f7016a1 --- /dev/null +++ b/cmake/FindUtf8proc.cmake @@ -0,0 +1,54 @@ +# - Try to find utf8proc +# Once done this will define +# UTF8PROC_FOUND - System has utf8proc +# UTF8PROC_INCLUDE_DIRS - The utf8proc include directories +# UTF8PROC_LIBRARIES - The libraries needed to use utf8proc + +if(NOT USE_BUNDLED_UTF8PROC) + find_package(PkgConfig) + if (PKG_CONFIG_FOUND) + pkg_check_modules(PC_UTF8PROC QUIET utf8proc) + endif() +else() + set(PC_UTF8PROC_INCLUDEDIR) + set(PC_UTF8PROC_INCLUDE_DIRS) + set(PC_UTF8PROC_LIBDIR) + set(PC_UTF8PROC_LIBRARY_DIRS) + set(LIMIT_SEARCH NO_DEFAULT_PATH) +endif() + +set(UTF8PROC_DEFINITIONS ${PC_UTF8PROC_CFLAGS_OTHER}) + +find_path(UTF8PROC_INCLUDE_DIR utf8proc.h + PATHS ${PC_UTF8PROC_INCLUDEDIR} ${PC_UTF8PROC_INCLUDE_DIRS} + ${LIMIT_SEARCH}) + +# If we're asked to use static linkage, add libutf8proc.a as a preferred library name. +if(UTF8PROC_USE_STATIC) + list(APPEND UTF8PROC_NAMES + "${CMAKE_STATIC_LIBRARY_PREFIX}utf8proc${CMAKE_STATIC_LIBRARY_SUFFIX}") +if(MSVC) + list(APPEND UTF8PROC_NAMES + "${CMAKE_STATIC_LIBRARY_PREFIX}utf8proc_static${CMAKE_STATIC_LIBRARY_SUFFIX}") +endif() +endif() + +list(APPEND UTF8PROC_NAMES utf8proc) +if(MSVC) + list(APPEND UTF8PROC_NAMES utf8proc_static) +endif() + +find_library(UTF8PROC_LIBRARY NAMES ${UTF8PROC_NAMES} + HINTS ${PC_UTF8PROC_LIBDIR} ${PC_UTF8PROC_LIBRARY_DIRS} + ${LIMIT_SEARCH}) + +set(UTF8PROC_LIBRARIES ${UTF8PROC_LIBRARY}) +set(UTF8PROC_INCLUDE_DIRS ${UTF8PROC_INCLUDE_DIR}) + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set UTF8PROC_FOUND to TRUE +# if all listed variables are TRUE +find_package_handle_standard_args(Utf8proc DEFAULT_MSG + UTF8PROC_LIBRARY UTF8PROC_INCLUDE_DIR) + +mark_as_advanced(UTF8PROC_INCLUDE_DIR UTF8PROC_LIBRARY) diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index ebc7e96d66..53a4089e10 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -74,8 +74,6 @@ include_directories(${GENERATED_DIR}) include_directories(${CACHED_GENERATED_DIR}) include_directories(${GENERATED_INCLUDES_DIR}) -include_directories(${LIBUTF8PROC_INCLUDE_DIRS}) - file(MAKE_DIRECTORY ${TOUCHES_DIR}) file(MAKE_DIRECTORY ${GENERATED_DIR}) file(MAKE_DIRECTORY ${GENERATED_INCLUDES_DIR}) @@ -88,8 +86,9 @@ file(GLOB XDIFF_SOURCES xdiff/*.c) file(GLOB XDIFF_HEADERS xdiff/*.h) # when LIBUTF8PROC build is fixed, don't use lib.c with amalgamated utf8proc.c -#file(GLOB TS_SOURCES tree_sitter/*.c) -file(GLOB TS_SOURCES ../tree_sitter/lib.c) +file(GLOB TS_SOURCES ../tree_sitter/*.c) +file(GLOB TS_SOURCE_AMALGAM ../tree_sitter/lib.c) +list(REMOVE_ITEM TS_SOURCES ${TS_SOURCE_AMALGAM}) foreach(subdir os @@ -402,6 +401,7 @@ list(APPEND NVIM_LINK_LIBRARIES ${LIBVTERM_LIBRARIES} ${LIBTERMKEY_LIBRARIES} ${UNIBILIUM_LIBRARIES} + ${UTF8PROC_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index a848a57047..83692ff587 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -35,6 +35,7 @@ option(USE_BUNDLED_LIBTERMKEY "Use the bundled libtermkey." ${USE_BUNDLED}) option(USE_BUNDLED_LIBVTERM "Use the bundled libvterm." ${USE_BUNDLED}) option(USE_BUNDLED_LIBUV "Use the bundled libuv." ${USE_BUNDLED}) option(USE_BUNDLED_MSGPACK "Use the bundled msgpack." ${USE_BUNDLED}) +option(USE_BUNDLED_UTF8PROC "Use the bundled utf8proc." ${USE_BUNDLED}) option(USE_BUNDLED_LUAJIT "Use the bundled version of luajit." ${USE_BUNDLED}) option(USE_BUNDLED_LUAROCKS "Use the bundled version of luarocks." ${USE_BUNDLED}) option(USE_BUNDLED_LUV "Use the bundled version of luv." ${USE_BUNDLED}) @@ -249,8 +250,9 @@ if(USE_BUNDLED_LIBICONV) include(BuildLibiconv) endif() -# TODO: build it as a normal lib, so it can be used as a distro lib when available -include(BuildUtf8proc) +if(USE_BUNDLED_UTF8PROC) + include(BuildUtf8proc) +endif() if(WIN32) include(GetBinaryDeps) diff --git a/third-party/cmake/BuildUtf8proc.cmake b/third-party/cmake/BuildUtf8proc.cmake index df287ea459..7297913f87 100644 --- a/third-party/cmake/BuildUtf8proc.cmake +++ b/third-party/cmake/BuildUtf8proc.cmake @@ -1,16 +1,68 @@ -ExternalProject_Add(utf8proc -PREFIX ${DEPS_BUILD_DIR} -URL ${UTF8PROC_URL} -DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/utf8proc -DOWNLOAD_COMMAND ${CMAKE_COMMAND} - -DPREFIX=${DEPS_BUILD_DIR} - -DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/utf8proc - -DURL=${UTF8PROC_URL} - -DEXPECTED_SHA256=${UTF8PROC_SHA256} - -DTARGET=utf8proc - -DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR} - -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake -CONFIGURE_COMMAND true -BUILD_COMMAND true -INSTALL_COMMAND true -) +include(CMakeParseArguments) + +# BuildUtf8proc(CONFIGURE_COMMAND ... BUILD_COMMAND ... INSTALL_COMMAND ...) +# Reusable function to build utf8proc, wraps ExternalProject_Add. +# Failing to pass a command argument will result in no command being run +function(BuildUtf8proc) + cmake_parse_arguments(_utf8proc + "" + "" + "CONFIGURE_COMMAND;BUILD_COMMAND;INSTALL_COMMAND" + ${ARGN}) + + if(NOT _utf8proc_CONFIGURE_COMMAND AND NOT _utf8proc_BUILD_COMMAND + AND NOT _utf8proc_INSTALL_COMMAND) + message(FATAL_ERROR "Must pass at least one of CONFIGURE_COMMAND, BUILD_COMMAND, INSTALL_COMMAND") + endif() + + ExternalProject_Add(utf8proc + PREFIX ${DEPS_BUILD_DIR} + URL ${UTF8PROC_URL} + DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/utf8proc + DOWNLOAD_COMMAND ${CMAKE_COMMAND} + -DPREFIX=${DEPS_BUILD_DIR} + -DDOWNLOAD_DIR=${DEPS_DOWNLOAD_DIR}/utf8proc + -DURL=${UTF8PROC_URL} + -DEXPECTED_SHA256=${UTF8PROC_SHA256} + -DTARGET=utf8proc + -DUSE_EXISTING_SRC_DIR=${USE_EXISTING_SRC_DIR} + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake + CONFIGURE_COMMAND "${_utf8proc_CONFIGURE_COMMAND}" + BUILD_COMMAND "${_utf8proc_BUILD_COMMAND}" + INSTALL_COMMAND "${_utf8proc_INSTALL_COMMAND}") +endfunction() + +set(UTF8PROC_CONFIGURE_COMMAND ${CMAKE_COMMAND} ${DEPS_BUILD_DIR}/src/utf8proc + -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1} -fPIC" + -DCMAKE_GENERATOR=${CMAKE_GENERATOR}) + +set(UTF8PROC_BUILD_COMMAND ${CMAKE_COMMAND} --build . --config ${CMAKE_BUILD_TYPE}) +set(UTF8PROC_INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config ${CMAKE_BUILD_TYPE}) + +if(MINGW AND CMAKE_CROSSCOMPILING) + get_filename_component(TOOLCHAIN ${CMAKE_TOOLCHAIN_FILE} REALPATH) + set(UTF8PROC_CONFIGURE_COMMAND ${CMAKE_COMMAND} ${DEPS_BUILD_DIR}/src/utf8proc + -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR} + # Pass toolchain + -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + # Hack to avoid -rdynamic in Mingw + -DCMAKE_SHARED_LIBRARY_LINK_C_FLAGS="") +elseif(MSVC) + # Same as Unix without fPIC + set(UTF8PROC_CONFIGURE_COMMAND ${CMAKE_COMMAND} ${DEPS_BUILD_DIR}/src/utf8proc + -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1}" + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + # Make sure we use the same generator, otherwise we may + # accidentaly end up using different MSVC runtimes + -DCMAKE_GENERATOR=${CMAKE_GENERATOR}) +endif() + +BuildUtf8proc(CONFIGURE_COMMAND ${UTF8PROC_CONFIGURE_COMMAND} + BUILD_COMMAND ${UTF8PROC_BUILD_COMMAND} + INSTALL_COMMAND ${UTF8PROC_INSTALL_COMMAND}) From c07e1e8696826579f017c9b401fe264d70384ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Fri, 7 Jun 2019 13:15:23 +0200 Subject: [PATCH 0066/1293] tree-sitter: cleanup build code --- src/nvim/CMakeLists.txt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 53a4089e10..3056c108b4 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -85,10 +85,9 @@ file(GLOB NVIM_HEADERS *.h) file(GLOB XDIFF_SOURCES xdiff/*.c) file(GLOB XDIFF_HEADERS xdiff/*.h) -# when LIBUTF8PROC build is fixed, don't use lib.c with amalgamated utf8proc.c -file(GLOB TS_SOURCES ../tree_sitter/*.c) +file(GLOB TREESITTER_SOURCES ../tree_sitter/*.c) file(GLOB TS_SOURCE_AMALGAM ../tree_sitter/lib.c) -list(REMOVE_ITEM TS_SOURCES ${TS_SOURCE_AMALGAM}) +list(REMOVE_ITEM TREESITTER_SOURCES ${TS_SOURCE_AMALGAM}) foreach(subdir os @@ -164,7 +163,7 @@ foreach(sfile ${CONV_SOURCES}) endif() endforeach() # xdiff: inlined external project, we don't maintain it. #9306 -list(APPEND CONV_SOURCES ${XDIFF_SOURCES} ${TS_SOURCES}) +list(APPEND CONV_SOURCES ${XDIFF_SOURCES}) if(NOT MSVC) set_source_files_properties( @@ -178,6 +177,9 @@ if(NOT MSVC) set_source_files_properties( eval.c PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS} -Wno-conversion") endif() + + # tree-sitter: inlined external project, we don't maintain it. #10124 + set_source_files_properties(${TREESITTER_SOURCES} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS} -Wno-conversion -Wno-pedantic -Wno-shadow -Wno-missing-prototypes -Wno-unused-variable") endif() if(NOT "${MIN_LOG_LEVEL}" MATCHES "^$") @@ -421,7 +423,7 @@ endif() add_executable(nvim ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS} ${NVIM_GENERATED_SOURCES} ${NVIM_SOURCES} ${NVIM_HEADERS} - ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TS_SOURCES}) + ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TREESITTER_SOURCES}) target_link_libraries(nvim ${NVIM_EXEC_LINK_LIBRARIES}) install_helper(TARGETS nvim) @@ -507,7 +509,7 @@ add_library( EXCLUDE_FROM_ALL ${NVIM_SOURCES} ${NVIM_GENERATED_SOURCES} ${NVIM_HEADERS} ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS} - ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TS_SOURCES} + ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TREESITTER_SOURCES} ) set_property(TARGET libnvim APPEND PROPERTY INCLUDE_DIRECTORIES ${LUA_PREFERRED_INCLUDE_DIRS}) @@ -532,7 +534,7 @@ else() EXCLUDE_FROM_ALL ${NVIM_SOURCES} ${NVIM_GENERATED_SOURCES} ${NVIM_HEADERS} ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS} - ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TS_SOURCES} + ${XDIFF_SOURCES} ${XDIFF_HEADERS} ${TREESITTER_SOURCES} ${UNIT_TEST_FIXTURES} ) target_link_libraries(nvim-test ${NVIM_TEST_LINK_LIBRARIES}) From 005b6d638caa200711bf5960e0c0d70ba5721c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Fri, 7 Jun 2019 14:21:00 +0200 Subject: [PATCH 0067/1293] tree-sitter: split tree-sitter lua interface from demo code --- runtime/lua/tree_sitter_demo.lua | 57 +++++++++++++++ runtime/lua/treesitter_rt.lua | 115 ------------------------------- runtime/lua/vim/tree_sitter.lua | 47 +++++++++++++ runtime/plugin/ts_test.vim | 3 +- 4 files changed, 106 insertions(+), 116 deletions(-) create mode 100644 runtime/lua/tree_sitter_demo.lua delete mode 100644 runtime/lua/treesitter_rt.lua create mode 100644 runtime/lua/vim/tree_sitter.lua diff --git a/runtime/lua/tree_sitter_demo.lua b/runtime/lua/tree_sitter_demo.lua new file mode 100644 index 0000000000..24a0f3d622 --- /dev/null +++ b/runtime/lua/tree_sitter_demo.lua @@ -0,0 +1,57 @@ +local a = vim.api +_G.a = vim.api + +if __treesitter_rt_ns == nil then + __treesitter_rt_ns = a.nvim_create_namespace("treesitter_demp") +end +local my_ns = __treesitter_rt_ns + +function ts_inspect_pos(row,col) + local tree = parse_tree(theparser) + local root = tree:root() + local node = root:descendant_for_point_range(row,col,row,col) + show_node(node) +end + +function show_node(node) + if node == nil then + return + end + a.nvim_buf_clear_highlight(0, my_ns, 0, -1) + shown_node = node + print(node:type()) + local start_row, start_col, end_row, end_col = node:range() + + a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", start_row, start_col, start_col+1) + + if end_col >= 1 then + end_col = end_col - 1 + end + a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", end_row, end_col, end_col+1) +end + +function ts_expand_node() + if shown_node == nil then + return + end + parent = shown_node:parent() + show_node(parent) +end + +function ts_cursor() + local row, col = unpack(a.nvim_win_get_cursor(0)) + ts_inspect_pos(row-1, col) +end + +if false then + ctree = theparser.tree + root = ctree:root() + cursor = root:to_cursor() + node = cursor:forward(5000) if true then return node end + print(#root) + c = root:child(50) + print(require'inspect'{c:extent()}) + type(ctree.__tostring) + root:__tostring() + print(_tslua_debug()) +end diff --git a/runtime/lua/treesitter_rt.lua b/runtime/lua/treesitter_rt.lua deleted file mode 100644 index 1dea22000a..0000000000 --- a/runtime/lua/treesitter_rt.lua +++ /dev/null @@ -1,115 +0,0 @@ -local a = vim.api - -if __treesitter_rt_ns == nil then - __treesitter_rt_ns = a.nvim_buf_add_highlight(0, 0, "", 0, 0, 0) - __treesitter_rt_syn_ns = a.nvim_buf_add_highlight(0, 0, "", 0, 0, 0) -end -local my_ns = __treesitter_rt_ns -local my_syn_ns = __treesitter_rt_syn_ns - - ---luadev = require'luadev' ---i = require'inspect' - - -function parse_tree(tsstate, force) - if tsstate.valid and not force then - return tsstate.tree - end - tsstate.tree = tsstate.parser:parse_buf(tsstate.bufnr) - tsstate.valid = true - return tsstate.tree -end - -function the_cb(tsstate, ev, bufnr, tick, start_row, oldstopline, stop_row) - local start_byte = a.nvim_buf_get_offset(bufnr,start_row) - -- a bit messy, should we expose edited but not reparsed tree? - -- are multiple edits safe in general? - local root = tsstate.parser:tree():root() - -- TODO: add proper lookup function! - local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0) - local edit - if inode == nil then - local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) - tsstate.parser:edit(start_byte,stop_byte,stop_byte,start_row,0,stop_row,0,stop_row,0) - else - local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start() - local fake_rows = fakeoldstoprow-oldstopline - local fakestop = stop_row+fake_rows - local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol - tsstate.parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol) - end - tsstate.valid = false - --luadev.append_buf({i{edit.start_byte,edit.old_end_byte,edit.new_end_byte}, - -- i{edit.start_point, edit.old_end_point, edit.new_end_point}}) -end - -function attach_buf(tsstate) - local function cb(ev, ...) - return the_cb(tsstate, ev, ...) - end - a.nvim_buf_attach(tsstate.bufnr, false, {on_lines=cb}) -end - -function create_parser(bufnr) - if bufnr == 0 then - bufnr = a.nvim_get_current_buf() - end - local ft = a.nvim_buf_get_option(bufnr, "filetype") - local tsstate = {} - tsstate.bufnr = bufnr - tsstate.parser = vim.ts_parser(ft.."_parser.so", ft) - parse_tree(tsstate) - attach_buf(tsstate) - return tsstate -end - -function ts_inspect_pos(row,col) - local tree = parse_tree(theparser) - local root = tree:root() - local node = root:descendant_for_point_range(row,col,row,col) - show_node(node) -end - -function show_node(node) - if node == nil then - return - end - a.nvim_buf_clear_highlight(0, my_ns, 0, -1) - shown_node = node - print(node:type()) - local start_row, start_col, end_row, end_col = node:range() - - a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", start_row, start_col, start_col+1) - - if end_col >= 1 then - end_col = end_col - 1 - end - a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", end_row, end_col, end_col+1) -end - -function ts_expand_node() - if shown_node == nil then - return - end - parent = shown_node:parent() - show_node(parent) -end - -function ts_cursor() - local row, col = unpack(a.nvim_win_get_cursor(0)) - ts_inspect_pos(row-1, col) -end - -if false then - ctree = theparser.tree - root = ctree:root() - cursor = root:to_cursor() - node = cursor:forward(5000) if true then return node end - print(#root) - c = root:child(50) - print(require'inspect'{c:extent()}) - type(ctree.__tostring) - root:__tostring() - print(_tslua_debug()) -end diff --git a/runtime/lua/vim/tree_sitter.lua b/runtime/lua/vim/tree_sitter.lua new file mode 100644 index 0000000000..a4cb3f3db6 --- /dev/null +++ b/runtime/lua/vim/tree_sitter.lua @@ -0,0 +1,47 @@ +local a = vim.api + +function parse_tree(tsstate, force) + if tsstate.valid and not force then + return tsstate.tree + end + tsstate.tree = tsstate.parser:parse_buf(tsstate.bufnr) + tsstate.valid = true + return tsstate.tree +end + +local function change_cb(tsstate, ev, bufnr, tick, start_row, oldstopline, stop_row) + local start_byte = a.nvim_buf_get_offset(bufnr,start_row) + -- a bit messy, should we expose edited but not reparsed tree? + -- are multiple edits safe in general? + local root = tsstate.parser:tree():root() + -- TODO: add proper lookup function! + local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0) + if inode == nil then + local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) + tsstate.parser:edit(start_byte,stop_byte,stop_byte,start_row,0,stop_row,0,stop_row,0) + else + local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start() + local fake_rows = fakeoldstoprow-oldstopline + local fakestop = stop_row+fake_rows + local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol + tsstate.parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol) + end + tsstate.valid = false +end + +function create_parser(bufnr) + if bufnr == 0 then + bufnr = a.nvim_get_current_buf() + end + local ft = a.nvim_buf_get_option(bufnr, "filetype") + local tsstate = {} + tsstate.bufnr = bufnr + tsstate.parser = vim.ts_parser(ft.."_parser.so", ft) + parse_tree(tsstate) + local function cb(ev, ...) + return change_cb(tsstate, ev, ...) + end + a.nvim_buf_attach(tsstate.bufnr, false, {on_lines=cb}) + return tsstate +end + diff --git a/runtime/plugin/ts_test.vim b/runtime/plugin/ts_test.vim index e40e2792e1..d048dbe7c8 100644 --- a/runtime/plugin/ts_test.vim +++ b/runtime/plugin/ts_test.vim @@ -6,7 +6,8 @@ func! TSTest() return end " TODO: module! - lua require'treesitter_rt' + lua require'vim.tree_sitter' + lua require'tree_sitter_demo' lua theparser = create_parser(vim.api.nvim_get_current_buf()) let g:has_ts = v:true endfunc From 1e9e2451bef21ff705e677802d1b0980356f1f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Fri, 7 Jun 2019 18:19:59 +0200 Subject: [PATCH 0068/1293] tree-sitter: objectify API --- runtime/lua/tree_sitter_demo.lua | 2 +- runtime/lua/vim/tree_sitter.lua | 46 +++++++++++++++++++------------- runtime/plugin/ts_test.vim | 3 +-- src/nvim/lua/executor.c | 14 +--------- 4 files changed, 30 insertions(+), 35 deletions(-) diff --git a/runtime/lua/tree_sitter_demo.lua b/runtime/lua/tree_sitter_demo.lua index 24a0f3d622..bbfd69109d 100644 --- a/runtime/lua/tree_sitter_demo.lua +++ b/runtime/lua/tree_sitter_demo.lua @@ -7,7 +7,7 @@ end local my_ns = __treesitter_rt_ns function ts_inspect_pos(row,col) - local tree = parse_tree(theparser) + local tree = theparser:parse_tree() local root = tree:root() local node = root:descendant_for_point_range(row,col,row,col) show_node(node) diff --git a/runtime/lua/vim/tree_sitter.lua b/runtime/lua/vim/tree_sitter.lua index a4cb3f3db6..bbc4db5f29 100644 --- a/runtime/lua/vim/tree_sitter.lua +++ b/runtime/lua/vim/tree_sitter.lua @@ -1,47 +1,55 @@ local a = vim.api -function parse_tree(tsstate, force) - if tsstate.valid and not force then - return tsstate.tree +local Parser = {} +Parser.__index = Parser + +function Parser:parse_tree(force) + if self.valid and not force then + return self.tree end - tsstate.tree = tsstate.parser:parse_buf(tsstate.bufnr) - tsstate.valid = true - return tsstate.tree + self.tree = self._parser:parse_buf(self.bufnr) + self.valid = true + return self.tree end -local function change_cb(tsstate, ev, bufnr, tick, start_row, oldstopline, stop_row) +local function change_cb(self, ev, bufnr, tick, start_row, oldstopline, stop_row) local start_byte = a.nvim_buf_get_offset(bufnr,start_row) -- a bit messy, should we expose edited but not reparsed tree? -- are multiple edits safe in general? - local root = tsstate.parser:tree():root() + local root = self._parser:tree():root() -- TODO: add proper lookup function! local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0) if inode == nil then local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) - tsstate.parser:edit(start_byte,stop_byte,stop_byte,start_row,0,stop_row,0,stop_row,0) + self._parser:edit(start_byte,stop_byte,stop_byte,start_row,0,stop_row,0,stop_row,0) else local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start() local fake_rows = fakeoldstoprow-oldstopline local fakestop = stop_row+fake_rows local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol - tsstate.parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol) + self._parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol) end - tsstate.valid = false + self.valid = false end -function create_parser(bufnr) +local function create_parser(bufnr) if bufnr == 0 then bufnr = a.nvim_get_current_buf() end local ft = a.nvim_buf_get_option(bufnr, "filetype") - local tsstate = {} - tsstate.bufnr = bufnr - tsstate.parser = vim.ts_parser(ft.."_parser.so", ft) - parse_tree(tsstate) + local self = setmetatable({bufnr=bufnr, valid=false}, Parser) + self._parser = vim._create_ts_parser(ft.."_parser.so", ft) + self:parse_tree() local function cb(ev, ...) - return change_cb(tsstate, ev, ...) + -- TODO: use weakref to self, so that the parser is free'd is no plugin is + -- using it. + return change_cb(self, ev, ...) end - a.nvim_buf_attach(tsstate.bufnr, false, {on_lines=cb}) - return tsstate + a.nvim_buf_attach(self.bufnr, false, {on_lines=cb}) + return self end +-- TODO: weak table with reusable parser per buffer. + +return {create_parser=create_parser} + diff --git a/runtime/plugin/ts_test.vim b/runtime/plugin/ts_test.vim index d048dbe7c8..76318163f6 100644 --- a/runtime/plugin/ts_test.vim +++ b/runtime/plugin/ts_test.vim @@ -6,9 +6,8 @@ func! TSTest() return end " TODO: module! - lua require'vim.tree_sitter' + lua theparser = require'vim.tree_sitter'.create_parser(0) lua require'tree_sitter_demo' - lua theparser = create_parser(vim.api.nvim_get_current_buf()) let g:has_ts = v:true endfunc diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 1794cee8d8..a6447ebb2b 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -822,16 +822,6 @@ void ex_luafile(exarg_T *const eap) } } -static int unsafe_ptr_to_ts_tree(lua_State *L) -{ - if (!lua_gettop(L)) { - return 0; - } - TSTree *const *ptr = lua_topointer(L,1); - tslua_push_tree(L, *ptr); - return 1; -} - static int create_tslua_parser(lua_State *L) { if (lua_gettop(L) < 2) { @@ -867,9 +857,7 @@ static int create_tslua_parser(lua_State *L) static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { tslua_init(lstate); - lua_pushcfunction(lstate, unsafe_ptr_to_ts_tree); - lua_setfield(lstate, -2, "unsafe_ts_tree"); lua_pushcfunction(lstate, create_tslua_parser); - lua_setfield(lstate, -2, "ts_parser"); + lua_setfield(lstate, -2, "_create_ts_parser"); } From afba23099fccc929fd0319a9a965a7b727407c7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 8 Jun 2019 15:51:38 +0200 Subject: [PATCH 0069/1293] tree-sitter: support pre-registration of languages --- runtime/lua/vim/tree_sitter.lua | 2 +- src/nvim/lua/executor.c | 32 +++++-------------- src/nvim/lua/tree_sitter.c | 54 ++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 27 deletions(-) diff --git a/runtime/lua/vim/tree_sitter.lua b/runtime/lua/vim/tree_sitter.lua index bbc4db5f29..a7830bc312 100644 --- a/runtime/lua/vim/tree_sitter.lua +++ b/runtime/lua/vim/tree_sitter.lua @@ -38,7 +38,7 @@ local function create_parser(bufnr) end local ft = a.nvim_buf_get_option(bufnr, "filetype") local self = setmetatable({bufnr=bufnr, valid=false}, Parser) - self._parser = vim._create_ts_parser(ft.."_parser.so", ft) + self._parser = vim._create_ts_parser(ft) self:parse_tree() local function cb(ev, ...) -- TODO: use weakref to self, so that the parser is free'd is no plugin is diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index a6447ebb2b..ae53bfce6a 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -824,34 +824,13 @@ void ex_luafile(exarg_T *const eap) static int create_tslua_parser(lua_State *L) { - if (lua_gettop(L) < 2) { - return 0; - } - const char *path = lua_tostring(L,1); - const char *lang_name = lua_tostring(L,2); - - // TODO: unsafe! - char symbol_buf[128] = "tree_sitter_"; - STRCAT(symbol_buf, lang_name); - - // TODO: we should maybe keep the uv_lib_t around, and close them - // at exit, to keep LeakSanitizer happy. - uv_lib_t lib; - if (uv_dlopen(path, &lib)) { - return luaL_error(L, "uv_dlopen: %s", uv_dlerror(&lib)); + if (lua_gettop(L) < 1 || !lua_isstring(L, 1)) { + return luaL_error(L, "string expected"); } - TSLanguage *(*lang_parser)(void); - if (uv_dlsym(&lib, symbol_buf, (void **)&lang_parser)) { - return luaL_error(L, "uv_dlsym: %s", uv_dlerror(&lib)); - } + const char *lang_name = lua_tostring(L,1); - TSLanguage *lang = lang_parser(); - if (lang == NULL) { - return luaL_error(L, "failed to load parser"); - } - tslua_push_parser(L, lang); - return 1; + return tslua_push_parser(L, lang_name); } static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL @@ -860,4 +839,7 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_pushcfunction(lstate, create_tslua_parser); lua_setfield(lstate, -2, "_create_ts_parser"); + + lua_pushcfunction(lstate, ts_lua_register_lang); + lua_setfield(lstate, -2, "ts_add_language"); } diff --git a/src/nvim/lua/tree_sitter.c b/src/nvim/lua/tree_sitter.c index 1ecb2bcac4..f992639955 100644 --- a/src/nvim/lua/tree_sitter.c +++ b/src/nvim/lua/tree_sitter.c @@ -65,6 +65,8 @@ static struct luaL_Reg node_meta[] = { {NULL, NULL} }; +PMap(cstr_t) *langs; + void build_meta(lua_State *L, const luaL_Reg *meta) { // [env, target] @@ -86,6 +88,9 @@ void build_meta(lua_State *L, const luaL_Reg *meta) /// all global state is stored in the regirstry of the lua_State void tslua_init(lua_State *L) { + + langs = pmap_new(cstr_t)(); + lua_createtable(L, 0, 0); // type metatables @@ -114,9 +119,55 @@ static int tslua_debug(lua_State *L) return 2; } -void tslua_push_parser(lua_State *L, TSLanguage *lang) + +int ts_lua_register_lang(lua_State *L) +{ + if (lua_gettop(L) < 2 || !lua_isstring(L, 1) || !lua_isstring(L, 2)) { + return luaL_error(L, "string expected"); + } + + const char *path = lua_tostring(L,1); + const char *lang_name = lua_tostring(L,2); + + if (pmap_has(cstr_t)(langs, lang_name)) { + return 0; + } + + // TODO: unsafe! + char symbol_buf[128] = "tree_sitter_"; + STRCAT(symbol_buf, lang_name); + + // TODO: we should maybe keep the uv_lib_t around, and close them + // at exit, to keep LeakSanitizer happy. + uv_lib_t lib; + if (uv_dlopen(path, &lib)) { + return luaL_error(L, "Failed to load parser: uv_dlopen: %s", uv_dlerror(&lib)); + } + + TSLanguage *(*lang_parser)(void); + if (uv_dlsym(&lib, symbol_buf, (void **)&lang_parser)) { + return luaL_error(L, "Failed to load parser: uv_dlsym: %s", uv_dlerror(&lib)); + } + + TSLanguage *lang = lang_parser(); + if (lang == NULL) { + return luaL_error(L, "Failed to load parser: internal error"); + } + + pmap_put(cstr_t)(langs, xstrdup(lang_name), lang); + + lua_pushboolean(L, true); + return 1; +} + +int tslua_push_parser(lua_State *L, const char *lang_name) { TSParser *parser = ts_parser_new(); + TSLanguage *lang = pmap_get(cstr_t)(langs, lang_name); + if (!lang) { + return luaL_error(L, "no such language: %s", lang_name); + } + ts_parser_set_language(parser, lang); Tslua_parser *p = lua_newuserdata(L, sizeof(Tslua_parser)); // [udata] p->parser = parser; @@ -126,6 +177,7 @@ void tslua_push_parser(lua_State *L, TSLanguage *lang) lua_getfield(L, -1, "parser-meta"); // [udata, env, meta] lua_setmetatable(L, -3); // [udata, env] lua_pop(L, 1); // [udata] + return 1; } static Tslua_parser *parser_check(lua_State *L) From 4ea5e63aa8c866b4fcc9d10f1a26078d2517f96a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sun, 9 Jun 2019 13:26:48 +0200 Subject: [PATCH 0070/1293] tree-sitter: add basic testing on ci build tree-sitter c parser on ci for testing purposes --- ci/build.ps1 | 22 ++++++++ ci/install.sh | 20 +++++++ ci/run_tests.sh | 2 + runtime/lua/vim/tree_sitter.lua | 8 +-- runtime/plugin/ts_test.vim | 2 +- src/nvim/lua/executor.c | 2 +- src/nvim/lua/vim.lua | 3 ++ test/functional/lua/tree_sitter_spec.lua | 67 ++++++++++++++++++++++++ 8 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 test/functional/lua/tree_sitter_spec.lua diff --git a/ci/build.ps1 b/ci/build.ps1 index d533d7b4e0..4e1a69376b 100644 --- a/ci/build.ps1 +++ b/ci/build.ps1 @@ -94,6 +94,28 @@ npm.cmd install -g neovim Get-Command -CommandType Application neovim-node-host.cmd npm.cmd link neovim +#npm.cmd install -g tree-sitter-cli +#npm.cmd link tree-sitter-cli + +mkdir c:\treesitter +$env:TREE_SITTER_DIR = "c:\treesitter" +#$env:PATH = "c:\treesitter;$env:PATH" +$client = new-object System.Net.WebClient +cd c:\treesitter + +if ($bits -eq 32) { + $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.5/tree-sitter-windows-x86.gz","c:\treesitter\tree-sitter-cli.gz") +} +elseif ($bits -eq 64) { + $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.5/tree-sitter-windows-x64.gz","c:\treesitter\tree-sitter-cli.gz") +} +python -c "import gzip, shutil; f1,f2 = gzip.open('tree-sitter-cli.gz', 'rb'), open('tree-sitter.exe', 'wb'); shutil.copyfileobj(f1, f2); f2.close()" + +$client.DownloadFile("https://codeload.github.com/tree-sitter/tree-sitter-c/zip/v0.15.2","c:\treesitter\tree_sitter_c.zip") +Expand-Archive c:\treesitter\tree_sitter_c.zip -DestinationPath c:\treesitter\ +cd c:\treesitter\tree-sitter-c-0.15.2 +c:\treesitter\tree-sitter.exe test + function convertToCmakeArgs($vars) { return $vars.GetEnumerator() | foreach { "-D$($_.Key)=$($_.Value)" } } diff --git a/ci/install.sh b/ci/install.sh index cda9a11f08..b96cf3c073 100755 --- a/ci/install.sh +++ b/ci/install.sh @@ -24,3 +24,23 @@ gem install --no-document --version ">= 0.8.0" neovim echo "Install neovim npm package" npm install -g neovim npm link neovim + +echo "Install tree-sitter npm package" +npm install -g tree-sitter-cli +npm link tree-sitter-cli + +echo "Install tree-sitter c parser" +curl "https://codeload.github.com/tree-sitter/tree-sitter-c/tar.gz/v0.15.2" -o tree_sitter_c.tar.gz +tar xf tree_sitter_c.tar.gz +cd tree-sitter-c-0.15.2 +export TREE_SITTER_DIR=$HOME/tree-sitter-build/ +mkdir -p $TREE_SITTER_DIR/bin + +if [[ "$BUILD_32BIT" != "ON" ]]; then + # builds c parser in $HOME/tree-sitter-build/bin/c.(so|dylib) + tree-sitter test +else + # no tree-sitter binary for 32bit linux, so fake it (no tree-sitter unit tests) + cd src/ + gcc -m32 -o $TREE_SITTER_DIR/bin/c.so -shared parser.c -I. +fi diff --git a/ci/run_tests.sh b/ci/run_tests.sh index c175910da5..6b2f69293c 100755 --- a/ci/run_tests.sh +++ b/ci/run_tests.sh @@ -19,6 +19,8 @@ exit_suite --continue enter_suite tests +export TREE_SITTER_DIR=$HOME/tree-sitter-build/ + if test "$CLANG_SANITIZER" != "TSAN" ; then # Additional threads are only created when the builtin UI starts, which # doesn't happen in the unit/functional tests diff --git a/runtime/lua/vim/tree_sitter.lua b/runtime/lua/vim/tree_sitter.lua index a7830bc312..1b5f416b67 100644 --- a/runtime/lua/vim/tree_sitter.lua +++ b/runtime/lua/vim/tree_sitter.lua @@ -32,11 +32,13 @@ local function change_cb(self, ev, bufnr, tick, start_row, oldstopline, stop_row self.valid = false end -local function create_parser(bufnr) +local function create_parser(bufnr, ft) if bufnr == 0 then bufnr = a.nvim_get_current_buf() end - local ft = a.nvim_buf_get_option(bufnr, "filetype") + if ft == nil then + ft = a.nvim_buf_get_option(bufnr, "filetype") + end local self = setmetatable({bufnr=bufnr, valid=false}, Parser) self._parser = vim._create_ts_parser(ft) self:parse_tree() @@ -51,5 +53,5 @@ end -- TODO: weak table with reusable parser per buffer. -return {create_parser=create_parser} +return {create_parser=create_parser, add_language=vim._ts_add_language} diff --git a/runtime/plugin/ts_test.vim b/runtime/plugin/ts_test.vim index 76318163f6..9420c2c9d3 100644 --- a/runtime/plugin/ts_test.vim +++ b/runtime/plugin/ts_test.vim @@ -6,7 +6,7 @@ func! TSTest() return end " TODO: module! - lua theparser = require'vim.tree_sitter'.create_parser(0) + lua theparser = vim.tree_sitter.create_parser(0) lua require'tree_sitter_demo' let g:has_ts = v:true endfunc diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index ae53bfce6a..23c4fcabbc 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -841,5 +841,5 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_setfield(lstate, -2, "_create_ts_parser"); lua_pushcfunction(lstate, ts_lua_register_lang); - lua_setfield(lstate, -2, "ts_add_language"); + lua_setfield(lstate, -2, "_ts_add_language"); } diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index b1a684b977..c38926fe24 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -232,6 +232,9 @@ local function __index(t, key) if key == 'inspect' then t.inspect = require('vim.inspect') return t.inspect + elseif key == 'tree_sitter' then + t.tree_sitter = require('vim.tree_sitter') + return t.tree_sitter elseif require('vim.shared')[key] ~= nil then -- Expose all `vim.shared` functions on the `vim` module. t[key] = require('vim.shared')[key] diff --git a/test/functional/lua/tree_sitter_spec.lua b/test/functional/lua/tree_sitter_spec.lua new file mode 100644 index 0000000000..e25eb47a60 --- /dev/null +++ b/test/functional/lua/tree_sitter_spec.lua @@ -0,0 +1,67 @@ +-- Test suite for testing interactions with API bindings +local helpers = require('test.functional.helpers')(after_each) + +local meths = helpers.meths +local clear = helpers.clear +local eq = helpers.eq +local insert = helpers.insert +local meth_pcall = helpers.meth_pcall +local exec_lua = helpers.exec_lua +local iswin = helpers.iswin + +before_each(clear) + +describe('tree-sitter API', function() + -- error tests not requiring a parser library + it('handles basic errors', function() + --eq({false, 'Error executing lua: vim.schedule: expected function'}, + -- meth_pcall(meths.execute_lua, "parser = vim.tree_sitter.create_parser(0, 'nosuchlang')", {})) + + + + end) + + local ts_path = os.getenv("TREE_SITTER_DIR") + + describe('with C parser', function() + if ts_path == nil then + it("works", function() pending("TREE_SITTER_PATH not set, skipping tree-sitter parser tests") end) + return + end + + before_each(function() + -- TODO the .so/.dylib/.dll thingie + local path = ts_path .. '/bin/c'..(iswin() and '.dll' or '.so') + exec_lua([[ + local path = ... + vim.tree_sitter.add_language(path,'c') + + ]], path) + end) + + it('parses buffer', function() + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + parser = vim.tree_sitter.create_parser(0, "c") + tree = parser:parse_tree() + root = tree:root() + ]]) + + --eq("", exec_lua("return tostring(parser)")) + eq("", exec_lua("return tostring(tree)")) + eq("", exec_lua("return tostring(root)")) + eq({0,0,3,0}, exec_lua("return {root:range()}")) + + eq(1, exec_lua("return root:child_count()")) + exec_lua("child = root:child(0)") + eq("", exec_lua("return tostring(child)")) + eq({0,0,2,1}, exec_lua("return {child:range()}")) + end) + + end) +end) + From c8f861b739b4703b1198dc1f88b09edbeb0d9f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 15 Jun 2019 12:10:12 +0200 Subject: [PATCH 0071/1293] tree-sitter: rename tree_sitter => treesitter for consistency --- .../lua/{tree_sitter_demo.lua => treesitter_demo.lua} | 1 + runtime/lua/vim/{tree_sitter.lua => treesitter.lua} | 0 runtime/plugin/ts_test.vim | 4 ++-- src/nvim/CMakeLists.txt | 2 +- src/nvim/lua/executor.c | 2 +- src/nvim/lua/tree_sitter.h | 10 ---------- src/nvim/lua/{tree_sitter.c => treesitter.c} | 6 +++--- src/nvim/lua/treesitter.h | 10 ++++++++++ src/nvim/lua/vim.lua | 6 +++--- .../lua/{tree_sitter_spec.lua => treesitter_spec.lua} | 6 +++--- 10 files changed, 24 insertions(+), 23 deletions(-) rename runtime/lua/{tree_sitter_demo.lua => treesitter_demo.lua} (98%) rename runtime/lua/vim/{tree_sitter.lua => treesitter.lua} (100%) delete mode 100644 src/nvim/lua/tree_sitter.h rename src/nvim/lua/{tree_sitter.c => treesitter.c} (99%) create mode 100644 src/nvim/lua/treesitter.h rename test/functional/lua/{tree_sitter_spec.lua => treesitter_spec.lua} (89%) diff --git a/runtime/lua/tree_sitter_demo.lua b/runtime/lua/treesitter_demo.lua similarity index 98% rename from runtime/lua/tree_sitter_demo.lua rename to runtime/lua/treesitter_demo.lua index bbfd69109d..82c36f94c0 100644 --- a/runtime/lua/tree_sitter_demo.lua +++ b/runtime/lua/treesitter_demo.lua @@ -1,3 +1,4 @@ +-- TODO: externalize this local a = vim.api _G.a = vim.api diff --git a/runtime/lua/vim/tree_sitter.lua b/runtime/lua/vim/treesitter.lua similarity index 100% rename from runtime/lua/vim/tree_sitter.lua rename to runtime/lua/vim/treesitter.lua diff --git a/runtime/plugin/ts_test.vim b/runtime/plugin/ts_test.vim index 9420c2c9d3..15192d8dda 100644 --- a/runtime/plugin/ts_test.vim +++ b/runtime/plugin/ts_test.vim @@ -6,8 +6,8 @@ func! TSTest() return end " TODO: module! - lua theparser = vim.tree_sitter.create_parser(0) - lua require'tree_sitter_demo' + lua theparser = vim.treesitter.create_parser(0) + lua require'treesitter_demo' let g:has_ts = v:true endfunc diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 3056c108b4..27977e3a40 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -145,7 +145,7 @@ set(CONV_SOURCES ex_cmds.c ex_docmd.c fileio.c - lua/tree_sitter.c + lua/treesitter.c mbyte.c memline.c message.c diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 23c4fcabbc..8a35f11c71 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -31,7 +31,7 @@ #include "nvim/lua/executor.h" #include "nvim/lua/converter.h" -#include "nvim/lua/tree_sitter.h" +#include "nvim/lua/treesitter.h" #include "luv/luv.h" diff --git a/src/nvim/lua/tree_sitter.h b/src/nvim/lua/tree_sitter.h deleted file mode 100644 index 2ae0ec8371..0000000000 --- a/src/nvim/lua/tree_sitter.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef NVIM_LUA_TREE_SITTER_H -#define NVIM_LUA_TREE_SITTER_H - -#include "tree_sitter/api.h" - -#ifdef INCLUDE_GENERATED_DECLARATIONS -# include "lua/tree_sitter.h.generated.h" -#endif - -#endif // NVIM_LUA_TREE_SITTER_H diff --git a/src/nvim/lua/tree_sitter.c b/src/nvim/lua/treesitter.c similarity index 99% rename from src/nvim/lua/tree_sitter.c rename to src/nvim/lua/treesitter.c index f992639955..794bdc6749 100644 --- a/src/nvim/lua/tree_sitter.c +++ b/src/nvim/lua/treesitter.c @@ -20,9 +20,9 @@ // NOT state-safe, delete when GC is confimed working: static int debug_n_trees = 0, debug_n_cursors = 0; -#define REG_KEY "tree_sitter-private" +#define REG_KEY "treesitter-private" -#include "nvim/lua/tree_sitter.h" +#include "nvim/lua/treesitter.h" #include "nvim/api/private/handle.h" #include "nvim/memline.h" @@ -32,7 +32,7 @@ typedef struct { } Tslua_parser; #ifdef INCLUDE_GENERATED_DECLARATIONS -# include "lua/tree_sitter.c.generated.h" +# include "lua/treesitter.c.generated.h" #endif static struct luaL_Reg parser_meta[] = { diff --git a/src/nvim/lua/treesitter.h b/src/nvim/lua/treesitter.h new file mode 100644 index 0000000000..d59b5e47a0 --- /dev/null +++ b/src/nvim/lua/treesitter.h @@ -0,0 +1,10 @@ +#ifndef NVIM_LUA_TREESITTER_H +#define NVIM_LUA_TREESITTER_H + +#include "tree_sitter/api.h" + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "lua/treesitter.h.generated.h" +#endif + +#endif // NVIM_LUA_TREESITTER_H diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index c38926fe24..b67762e48e 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -232,9 +232,9 @@ local function __index(t, key) if key == 'inspect' then t.inspect = require('vim.inspect') return t.inspect - elseif key == 'tree_sitter' then - t.tree_sitter = require('vim.tree_sitter') - return t.tree_sitter + elseif key == 'treesitter' then + t.treesitter = require('vim.treesitter') + return t.treesitter elseif require('vim.shared')[key] ~= nil then -- Expose all `vim.shared` functions on the `vim` module. t[key] = require('vim.shared')[key] diff --git a/test/functional/lua/tree_sitter_spec.lua b/test/functional/lua/treesitter_spec.lua similarity index 89% rename from test/functional/lua/tree_sitter_spec.lua rename to test/functional/lua/treesitter_spec.lua index e25eb47a60..8916e59563 100644 --- a/test/functional/lua/tree_sitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -15,7 +15,7 @@ describe('tree-sitter API', function() -- error tests not requiring a parser library it('handles basic errors', function() --eq({false, 'Error executing lua: vim.schedule: expected function'}, - -- meth_pcall(meths.execute_lua, "parser = vim.tree_sitter.create_parser(0, 'nosuchlang')", {})) + -- meth_pcall(meths.execute_lua, "parser = vim.treesitter.create_parser(0, 'nosuchlang')", {})) @@ -34,7 +34,7 @@ describe('tree-sitter API', function() local path = ts_path .. '/bin/c'..(iswin() and '.dll' or '.so') exec_lua([[ local path = ... - vim.tree_sitter.add_language(path,'c') + vim.treesitter.add_language(path,'c') ]], path) end) @@ -46,7 +46,7 @@ describe('tree-sitter API', function() }]]) exec_lua([[ - parser = vim.tree_sitter.create_parser(0, "c") + parser = vim.treesitter.create_parser(0, "c") tree = parser:parse_tree() root = tree:root() ]]) From a361e09cc531caf9dfb41bf860ca2d540ac2789d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 15 Jun 2019 12:50:41 +0200 Subject: [PATCH 0072/1293] tree-sitter: use standard luaL_newmetatable and luaL_checkudata pattern --- src/nvim/lua/treesitter.c | 94 +++++++++++++-------------------------- 1 file changed, 30 insertions(+), 64 deletions(-) diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 794bdc6749..8d24d15ccd 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -20,15 +20,13 @@ // NOT state-safe, delete when GC is confimed working: static int debug_n_trees = 0, debug_n_cursors = 0; -#define REG_KEY "treesitter-private" - #include "nvim/lua/treesitter.h" #include "nvim/api/private/handle.h" #include "nvim/memline.h" typedef struct { TSParser *parser; - TSTree *tree; + TSTree *tree; // internal tree, used for editing/reparsing } Tslua_parser; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -67,18 +65,18 @@ static struct luaL_Reg node_meta[] = { PMap(cstr_t) *langs; -void build_meta(lua_State *L, const luaL_Reg *meta) +void build_meta(lua_State *L, const char* tname, const luaL_Reg *meta) { - // [env, target] - for (size_t i = 0; meta[i].name != NULL; i++) { - lua_pushcfunction(L, meta[i].func); // [env, target, func] - lua_pushvalue(L, -3); // [env, target, func, env] - lua_setfenv(L, -2); // [env, target, func] - lua_setfield(L, -2, meta[i].name); // [env, target] - } + if (luaL_newmetatable(L, tname)) { // [meta] + for (size_t i = 0; meta[i].name != NULL; i++) { + lua_pushcfunction(L, meta[i].func); // [meta, func] + lua_setfield(L, -2, meta[i].name); // [meta] + } - lua_pushvalue(L, -1); // [env, target, target] - lua_setfield(L, -2, "__index"); // [env, target] + lua_pushvalue(L, -1); // [meta, meta] + lua_setfield(L, -2, "__index"); // [meta] + } + lua_pop(L, 1); // [] (don't use it now) } @@ -91,22 +89,12 @@ void tslua_init(lua_State *L) langs = pmap_new(cstr_t)(); - lua_createtable(L, 0, 0); - // type metatables - lua_createtable(L, 0, 0); - build_meta(L, parser_meta); - lua_setfield(L, -2, "parser-meta"); + build_meta(L, "treesitter_parser", parser_meta); - lua_createtable(L, 0, 0); - build_meta(L, tree_meta); - lua_setfield(L, -2, "tree-meta"); + build_meta(L, "treesitter_tree", tree_meta); - lua_createtable(L, 0, 0); - build_meta(L, node_meta); - lua_setfield(L, -2, "node-meta"); - - lua_setfield(L, LUA_REGISTRYINDEX, REG_KEY); + build_meta(L, "treesitter_node", node_meta); lua_pushcfunction(L, tslua_debug); lua_setglobal(L, "_tslua_debug"); @@ -173,23 +161,14 @@ int tslua_push_parser(lua_State *L, const char *lang_name) p->parser = parser; p->tree = NULL; - lua_getfield(L, LUA_REGISTRYINDEX, REG_KEY); // [udata, env] - lua_getfield(L, -1, "parser-meta"); // [udata, env, meta] - lua_setmetatable(L, -3); // [udata, env] - lua_pop(L, 1); // [udata] + lua_getfield(L, LUA_REGISTRYINDEX, "treesitter_parser"); // [udata, meta] + lua_setmetatable(L, -2); // [udata] return 1; } static Tslua_parser *parser_check(lua_State *L) { - if (!lua_gettop(L)) { - return 0; - } - if (!lua_isuserdata(L, 1)) { - return 0; - } - // TODO: typecheck! - return lua_touserdata(L, 1); + return luaL_checkudata(L, 1, "treesitter_parser"); } static int parser_gc(lua_State *L) @@ -313,31 +292,22 @@ void tslua_push_tree(lua_State *L, TSTree *tree) { TSTree **ud = lua_newuserdata(L, sizeof(TSTree *)); // [udata] *ud = tree; - lua_getfield(L, LUA_REGISTRYINDEX, REG_KEY); // [udata, env] - lua_getfield(L, -1, "tree-meta"); // [udata, env, meta] - lua_setmetatable(L, -3); // [udata, env] - lua_pop(L, 1); // [udata] + lua_getfield(L, LUA_REGISTRYINDEX, "treesitter_tree"); // [udata, meta] + lua_setmetatable(L, -2); // [udata] // table used for node wrappers to keep a reference to tree wrapper // NB: in lua 5.3 the uservalue for the node could just be the tree, but // in lua 5.1 the uservalue (fenv) must be a table. - lua_createtable(L, 1, 0); // [udata, reftable] - lua_pushvalue(L, -2); // [udata, reftable, udata] - lua_rawseti(L, -2, 1); // [udata, reftable] - lua_setfenv(L, -2); // [udata] + lua_createtable(L, 1, 0); // [udata, reftable] + lua_pushvalue(L, -2); // [udata, reftable, udata] + lua_rawseti(L, -2, 1); // [udata, reftable] + lua_setfenv(L, -2); // [udata] debug_n_trees++; } static TSTree *tree_check(lua_State *L) { - if (!lua_gettop(L)) { - return 0; - } - if (!lua_isuserdata(L, 1)) { - return 0; - } - // TODO: typecheck! - TSTree **ud = lua_touserdata(L, 1); + TSTree **ud = luaL_checkudata(L, 1, "treesitter_tree"); return *ud; } @@ -385,7 +355,7 @@ static void push_node(lua_State *L, TSNode node) } TSNode *ud = lua_newuserdata(L, sizeof(TSNode)); // [src, udata] *ud = node; - lua_getfield(L, LUA_ENVIRONINDEX, "node-meta"); // [src, udata, meta] + lua_getfield(L, LUA_REGISTRYINDEX, "treesitter_node"); // [src, udata, meta] lua_setmetatable(L, -2); // [src, udata] lua_getfenv(L, -2); // [src, udata, reftable] lua_setfenv(L, -2); // [src, udata] @@ -393,16 +363,12 @@ static void push_node(lua_State *L, TSNode node) static bool node_check(lua_State *L, TSNode *res) { - if (!lua_gettop(L)) { - return 0; + TSNode *ud = luaL_checkudata(L, 1, "treesitter_node"); + if (ud) { + *res = *ud; + return true; } - if (!lua_isuserdata(L, 1)) { - return 0; - } - // TODO: typecheck! - TSNode *ud = lua_touserdata(L, 1); - *res = *ud; - return true; + return false; } From c1dc1bedba4e0e3db2cd2e52d9241991567f8218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 15 Jun 2019 13:12:59 +0200 Subject: [PATCH 0073/1293] tree-sitter: style --- src/nvim/lua/executor.c | 3 +- src/nvim/lua/treesitter.c | 111 +++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 8a35f11c71..c208711985 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -828,8 +828,7 @@ static int create_tslua_parser(lua_State *L) return luaL_error(L, "string expected"); } - const char *lang_name = lua_tostring(L,1); - + const char *lang_name = lua_tostring(L, 1); return tslua_push_parser(L, lang_name); } diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 8d24d15ccd..2337d598b3 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -34,38 +34,38 @@ typedef struct { #endif static struct luaL_Reg parser_meta[] = { - {"__gc", parser_gc}, - {"__tostring", parser_tostring}, - {"parse_buf", parser_parse_buf}, - {"edit", parser_edit}, - {"tree", parser_tree}, - {NULL, NULL} + { "__gc", parser_gc }, + { "__tostring", parser_tostring }, + { "parse_buf", parser_parse_buf }, + { "edit", parser_edit }, + { "tree", parser_tree }, + { NULL, NULL } }; static struct luaL_Reg tree_meta[] = { - {"__gc", tree_gc}, - {"__tostring", tree_tostring}, - {"root", tree_root}, - {NULL, NULL} + { "__gc", tree_gc }, + { "__tostring", tree_tostring }, + { "root", tree_root }, + { NULL, NULL } }; static struct luaL_Reg node_meta[] = { - {"__tostring", node_tostring}, - {"__len", node_child_count}, - {"range", node_range}, - {"start", node_start}, - {"type", node_type}, - {"symbol", node_symbol}, - {"child_count", node_child_count}, - {"child", node_child}, - {"descendant_for_point_range", node_descendant_for_point_range}, - {"parent", node_parent}, - {NULL, NULL} + { "__tostring", node_tostring }, + { "__len", node_child_count }, + { "range", node_range }, + { "start", node_start }, + { "type", node_type }, + { "symbol", node_symbol }, + { "child_count", node_child_count }, + { "child", node_child }, + { "descendant_for_point_range", node_descendant_for_point_range }, + { "parent", node_parent }, + { NULL, NULL } }; PMap(cstr_t) *langs; -void build_meta(lua_State *L, const char* tname, const luaL_Reg *meta) +void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta) { if (luaL_newmetatable(L, tname)) { // [meta] for (size_t i = 0; meta[i].name != NULL; i++) { @@ -86,7 +86,6 @@ void build_meta(lua_State *L, const char* tname, const luaL_Reg *meta) /// all global state is stored in the regirstry of the lua_State void tslua_init(lua_State *L) { - langs = pmap_new(cstr_t)(); // type metatables @@ -114,8 +113,8 @@ int ts_lua_register_lang(lua_State *L) return luaL_error(L, "string expected"); } - const char *path = lua_tostring(L,1); - const char *lang_name = lua_tostring(L,2); + const char *path = lua_tostring(L, 1); + const char *lang_name = lua_tostring(L, 2); if (pmap_has(cstr_t)(langs, lang_name)) { return 0; @@ -129,12 +128,14 @@ int ts_lua_register_lang(lua_State *L) // at exit, to keep LeakSanitizer happy. uv_lib_t lib; if (uv_dlopen(path, &lib)) { - return luaL_error(L, "Failed to load parser: uv_dlopen: %s", uv_dlerror(&lib)); + return luaL_error(L, "Failed to load parser: uv_dlopen: %s", + uv_dlerror(&lib)); } TSLanguage *(*lang_parser)(void); if (uv_dlsym(&lib, symbol_buf, (void **)&lang_parser)) { - return luaL_error(L, "Failed to load parser: uv_dlsym: %s", uv_dlerror(&lib)); + return luaL_error(L, "Failed to load parser: uv_dlsym: %s", + uv_dlerror(&lib)); } TSLanguage *lang = lang_parser(); @@ -192,26 +193,29 @@ static int parser_tostring(lua_State *L) return 1; } -static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read) +static const char *input_cb(void *payload, uint32_t byte_index, + TSPoint position, uint32_t *bytes_read) { - buf_T *bp = payload; - static char buf[200]; - if ((linenr_T)position.row >= bp->b_ml.ml_line_count) { - *bytes_read = 0; - return ""; - } - char_u *line = ml_get_buf(bp, position.row+1, false); - size_t len = STRLEN(line); - size_t tocopy = MIN(len-position.column,200); + buf_T *bp = payload; +#define BUFSIZE 256 + static char buf[BUFSIZE]; + if ((linenr_T)position.row >= bp->b_ml.ml_line_count) { + *bytes_read = 0; + return ""; + } + char_u *line = ml_get_buf(bp, position.row+1, false); + size_t len = STRLEN(line); + size_t tocopy = MIN(len-position.column, BUFSIZE); - // TODO: translate embedded \n to \000 - memcpy(buf, line+position.column, tocopy); - *bytes_read = (uint32_t)tocopy; - if (tocopy < 200) { - buf[tocopy] = '\n'; - (*bytes_read)++; - } - return buf; + // TODO: translate embedded \n to \000 + memcpy(buf, line+position.column, tocopy); + *bytes_read = (uint32_t)tocopy; + if (tocopy < 200) { + buf[tocopy] = '\n'; + (*bytes_read)++; + } + return buf; +#undef BUFSIZE } static int parser_parse_buf(lua_State *L) @@ -223,7 +227,7 @@ static int parser_parse_buf(lua_State *L) long bufnr = lua_tointeger(L, 2); void *payload = handle_get_buffer(bufnr); - TSInput input = {payload, input_cb, TSInputEncodingUTF8}; + TSInput input = { payload, input_cb, TSInputEncodingUTF8 }; TSTree *new_tree = ts_parser_parse(p->parser, p->tree, input); if (p->tree) { ts_tree_delete(p->tree); @@ -251,10 +255,9 @@ static int parser_tree(lua_State *L) static int parser_edit(lua_State *L) { - if(lua_gettop(L) < 10) { + if (lua_gettop(L) < 10) { lua_pushstring(L, "not enough args to parser:edit()"); - lua_error(L); - return 0; // unreachable + return lua_error(L); } Tslua_parser *p = parser_check(L); @@ -350,7 +353,7 @@ static int tree_root(lua_State *L) static void push_node(lua_State *L, TSNode node) { if (ts_node_is_null(node)) { - lua_pushnil(L); // [src, nil] + lua_pushnil(L); // [src, nil] return; } TSNode *ud = lua_newuserdata(L, sizeof(TSNode)); // [src, udata] @@ -466,10 +469,10 @@ static int node_descendant_for_point_range(lua_State *L) if (!node_check(L, &node)) { return 0; } - TSPoint start = {(uint32_t)lua_tointeger(L, 2), - (uint32_t)lua_tointeger(L, 3)}; - TSPoint end = {(uint32_t)lua_tointeger(L, 4), - (uint32_t)lua_tointeger(L, 5)}; + TSPoint start = { (uint32_t)lua_tointeger(L, 2), + (uint32_t)lua_tointeger(L, 3) }; + TSPoint end = { (uint32_t)lua_tointeger(L, 4), + (uint32_t)lua_tointeger(L, 5) }; TSNode child = ts_node_descendant_for_point_range(node, start, end); lua_pushvalue(L, 1); From a88a9f128e29b27315a87d0119fbc649196999bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 15 Jun 2019 13:43:30 +0200 Subject: [PATCH 0074/1293] tree-sitter: add some more API --- src/nvim/lua/treesitter.c | 106 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 2337d598b3..9d599da85f 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -54,11 +54,19 @@ static struct luaL_Reg node_meta[] = { { "__len", node_child_count }, { "range", node_range }, { "start", node_start }, + { "end_", node_end }, { "type", node_type }, { "symbol", node_symbol }, + { "named", node_named }, + { "missing", node_missing }, + { "has_error", node_has_error }, + { "sexpr", node_sexpr }, { "child_count", node_child_count }, + { "named_child_count", node_named_child_count }, { "child", node_child }, + { "named_child", node_named_child }, { "descendant_for_point_range", node_descendant_for_point_range }, + { "named_descendant_for_point_range", node_named_descendant_for_point_range }, { "parent", node_parent }, { NULL, NULL } }; @@ -417,6 +425,20 @@ static int node_start(lua_State *L) return 3; } +static int node_end(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSPoint end = ts_node_end_point(node); + uint32_t end_byte = ts_node_end_byte(node); + lua_pushnumber(L, end.row); + lua_pushnumber(L, end.column); + lua_pushnumber(L, end_byte); + return 3; +} + static int node_child_count(lua_State *L) { TSNode node; @@ -428,6 +450,17 @@ static int node_child_count(lua_State *L) return 1; } +static int node_named_child_count(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + uint32_t count = ts_node_named_child_count(node); + lua_pushnumber(L, count); + return 1; +} + static int node_type(lua_State *L) { TSNode node; @@ -449,6 +482,48 @@ static int node_symbol(lua_State *L) return 1; } +static int node_named(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + lua_pushboolean(L, ts_node_is_named(node)); + return 1; +} + +static int node_sexpr(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + char *allocated = ts_node_string(node); + lua_pushstring(L, allocated); + xfree(allocated); + return 1; +} + +static int node_missing(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + lua_pushboolean(L, ts_node_is_missing(node)); + return 1; +} + +static int node_has_error(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + lua_pushboolean(L, ts_node_has_error(node)); + return 1; +} + static int node_child(lua_State *L) { TSNode node; @@ -463,6 +538,20 @@ static int node_child(lua_State *L) return 1; } +static int node_named_child(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + long num = lua_tointeger(L, 2); + TSNode child = ts_node_named_child(node, (uint32_t)num); + + lua_pushvalue(L, 1); + push_node(L, child); + return 1; +} + static int node_descendant_for_point_range(lua_State *L) { TSNode node; @@ -480,6 +569,23 @@ static int node_descendant_for_point_range(lua_State *L) return 1; } +static int node_named_descendant_for_point_range(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSPoint start = { (uint32_t)lua_tointeger(L, 2), + (uint32_t)lua_tointeger(L, 3) }; + TSPoint end = { (uint32_t)lua_tointeger(L, 4), + (uint32_t)lua_tointeger(L, 5) }; + TSNode child = ts_node_named_descendant_for_point_range(node, start, end); + + lua_pushvalue(L, 1); + push_node(L, child); + return 1; +} + static int node_parent(lua_State *L) { TSNode node; From d24dec596c25690aba0aca658546ffdfcc6a952c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 15 Jun 2019 14:05:35 +0200 Subject: [PATCH 0075/1293] tree-sitter: inspect language --- runtime/lua/vim/treesitter.lua | 6 ++++- src/nvim/lua/executor.c | 3 +++ src/nvim/lua/treesitter.c | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 1b5f416b67..3a1b1fc4b3 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -53,5 +53,9 @@ end -- TODO: weak table with reusable parser per buffer. -return {create_parser=create_parser, add_language=vim._ts_add_language} +return { + create_parser=create_parser, + add_language=vim._ts_add_language, + inspect_language=vim._ts_inspect_language, +} diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index c208711985..aa83e3c1ba 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -841,4 +841,7 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_pushcfunction(lstate, ts_lua_register_lang); lua_setfield(lstate, -2, "_ts_add_language"); + + lua_pushcfunction(lstate, ts_lua_inspect_lang); + lua_setfield(lstate, -2, "_ts_inspect_language"); } diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 9d599da85f..db337db533 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -157,6 +157,51 @@ int ts_lua_register_lang(lua_State *L) return 1; } +int ts_lua_inspect_lang(lua_State *L) +{ + if (lua_gettop(L) < 1 || !lua_isstring(L, 1)) { + return luaL_error(L, "string expected"); + } + const char *lang_name = lua_tostring(L, 1); + + TSLanguage *lang = pmap_get(cstr_t)(langs, lang_name); + if (!lang) { + return luaL_error(L, "no such language: %s", lang_name); + } + + lua_createtable(L, 0, 2); // [retval] + + size_t nsymbols = (size_t)ts_language_symbol_count(lang); + + lua_createtable(L, nsymbols-1, 1); // [retval, symbols] + for (size_t i = 0; i < nsymbols; i++) { + TSSymbolType t = ts_language_symbol_type(lang, i); + if (t == TSSymbolTypeAuxiliary) { + // not used by the API + continue; + } + lua_createtable(L, 2, 0); // [retval, symbols, elem] + lua_pushstring(L, ts_language_symbol_name(lang, i)); + lua_rawseti(L, -2, 1); + lua_pushboolean(L, t == TSSymbolTypeRegular); + lua_rawseti(L, -2, 2); // [retval, symbols, elem] + lua_rawseti(L, -2, i); // [retval, symbols] + } + + lua_setfield(L, -2, "symbols"); // [retval] + + // TODO: this seems to be empty, what langs have fields? + size_t nfields = (size_t)ts_language_field_count(lang); + lua_createtable(L, nfields-1, 1); // [retval, fields] + for (size_t i = 0; i < nfields; i++) { + lua_pushstring(L, ts_language_field_name_for_id(lang, i)); + lua_rawseti(L, -2, i); // [retval, fields] + } + + lua_setfield(L, -2, "fields"); // [retval] + return 1; +} + int tslua_push_parser(lua_State *L, const char *lang_name) { TSParser *parser = ts_parser_new(); From d697841a9d3030efaf10dbddaee9f3c0a8fe1b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sun, 16 Jun 2019 14:09:18 +0200 Subject: [PATCH 0076/1293] tree-sitter: cleanup tree refcounting --- src/nvim/lua/treesitter.c | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index db337db533..016c175b59 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -17,9 +17,6 @@ #include "tree_sitter/api.h" -// NOT state-safe, delete when GC is confimed working: -static int debug_n_trees = 0, debug_n_cursors = 0; - #include "nvim/lua/treesitter.h" #include "nvim/api/private/handle.h" #include "nvim/memline.h" @@ -102,19 +99,8 @@ void tslua_init(lua_State *L) build_meta(L, "treesitter_tree", tree_meta); build_meta(L, "treesitter_node", node_meta); - - lua_pushcfunction(L, tslua_debug); - lua_setglobal(L, "_tslua_debug"); } -static int tslua_debug(lua_State *L) -{ - lua_pushinteger(L, debug_n_trees); - lua_pushinteger(L, debug_n_cursors); - return 2; -} - - int ts_lua_register_lang(lua_State *L) { if (lua_gettop(L) < 2 || !lua_isstring(L, 1) || !lua_isstring(L, 2)) { @@ -280,6 +266,9 @@ static int parser_parse_buf(lua_State *L) long bufnr = lua_tointeger(L, 2); void *payload = handle_get_buffer(bufnr); + if (!payload) { + return luaL_error(L, "invalid buffer handle: %d", bufnr); + } TSInput input = { payload, input_cb, TSInputEncodingUTF8 }; TSTree *new_tree = ts_parser_parse(p->parser, p->tree, input); if (p->tree) { @@ -287,7 +276,7 @@ static int parser_parse_buf(lua_State *L) } p->tree = new_tree; - tslua_push_tree(L, ts_tree_copy(p->tree)); + tslua_push_tree(L, p->tree); return 1; } @@ -298,11 +287,7 @@ static int parser_tree(lua_State *L) return 0; } - if (p->tree) { - tslua_push_tree(L, ts_tree_copy(p->tree)); - } else { - lua_pushnil(L); - } + tslua_push_tree(L, p->tree); return 1; } @@ -342,12 +327,15 @@ static int parser_edit(lua_State *L) /// push tree interface on lua stack. /// -/// This takes "ownership" of the tree and will free it -/// when the wrapper object is garbage collected +/// This makes a copy of the tree, so ownership of the argument is unaffected. void tslua_push_tree(lua_State *L, TSTree *tree) { + if (tree == NULL) { + lua_pushnil(L); + return; + } TSTree **ud = lua_newuserdata(L, sizeof(TSTree *)); // [udata] - *ud = tree; + *ud = ts_tree_copy(tree); lua_getfield(L, LUA_REGISTRYINDEX, "treesitter_tree"); // [udata, meta] lua_setmetatable(L, -2); // [udata] @@ -358,7 +346,6 @@ void tslua_push_tree(lua_State *L, TSTree *tree) lua_pushvalue(L, -2); // [udata, reftable, udata] lua_rawseti(L, -2, 1); // [udata, reftable] lua_setfenv(L, -2); // [udata] - debug_n_trees++; } static TSTree *tree_check(lua_State *L) @@ -375,7 +362,6 @@ static int tree_gc(lua_State *L) } ts_tree_delete(tree); - debug_n_trees--; return 0; } From 167a1cfdef0c4b3526830ad0356f06bf480df6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Mon, 17 Jun 2019 21:46:31 +0200 Subject: [PATCH 0077/1293] tree-sitter: improve parser API (shared parser between plugins) --- runtime/lua/vim/treesitter.lua | 63 ++++++++++++++++++------- test/functional/lua/treesitter_spec.lua | 27 +++++++++-- 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 3a1b1fc4b3..f26d63d6ce 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -3,8 +3,13 @@ local a = vim.api local Parser = {} Parser.__index = Parser -function Parser:parse_tree(force) - if self.valid and not force then +-- TODO(bfredl): currently we retain parsers for the lifetime of the buffer. +-- Consider use weak references to release parser if all plugins are done with +-- it. +local parsers = {} + +function Parser:parse() + if self.valid then return self.tree end self.tree = self._parser:parse_buf(self.bufnr) @@ -12,7 +17,7 @@ function Parser:parse_tree(force) return self.tree end -local function change_cb(self, ev, bufnr, tick, start_row, oldstopline, stop_row) +local function on_lines(self, bufnr, _, start_row, oldstopline, stop_row) local start_byte = a.nvim_buf_get_offset(bufnr,start_row) -- a bit messy, should we expose edited but not reparsed tree? -- are multiple edits safe in general? @@ -21,41 +26,63 @@ local function change_cb(self, ev, bufnr, tick, start_row, oldstopline, stop_row local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0) if inode == nil then local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) - self._parser:edit(start_byte,stop_byte,stop_byte,start_row,0,stop_row,0,stop_row,0) + self._parser:edit(start_byte,stop_byte,stop_byte, + start_row,0,stop_row,0,stop_row,0) else local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start() local fake_rows = fakeoldstoprow-oldstopline local fakestop = stop_row+fake_rows local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol - self._parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol) + self._parser:edit(start_byte, fakebyteoldstop, fakebytestop, + start_row, 0, + fakeoldstoprow, fakeoldstopcol, + fakestop, fakeoldstopcol) end self.valid = false end -local function create_parser(bufnr, ft) +local function create_parser(bufnr, ft, id) if bufnr == 0 then bufnr = a.nvim_get_current_buf() end + local self = setmetatable({bufnr=bufnr, valid=false}, Parser) + self._parser = vim._create_ts_parser(ft) + self:parse() + -- TODO: use weakref to self, so that the parser is free'd is no plugin is + -- using it. + local function lines_cb(ev, ...) + return on_lines(self, ...) + end + local detach_cb = nil + if id ~= nil then + detach_cb = function() + if parsers[id] == self then + parsers[id] = nil + end + end + end + a.nvim_buf_attach(self.bufnr, false, {on_lines=lines_cb, on_detach=detach_cb}) + return self +end + +local function get_parser(bufnr, ft) + if bufnr == nil or bufnr == 0 then + bufnr = a.nvim_get_current_buf() + end if ft == nil then ft = a.nvim_buf_get_option(bufnr, "filetype") end - local self = setmetatable({bufnr=bufnr, valid=false}, Parser) - self._parser = vim._create_ts_parser(ft) - self:parse_tree() - local function cb(ev, ...) - -- TODO: use weakref to self, so that the parser is free'd is no plugin is - -- using it. - return change_cb(self, ev, ...) + local id = tostring(bufnr)..'_'..ft + + if parsers[id] == nil then + parsers[id] = create_parser(bufnr, ft, id) end - a.nvim_buf_attach(self.bufnr, false, {on_lines=cb}) - return self + return parsers[id] end --- TODO: weak table with reusable parser per buffer. - return { + get_parser=get_parser, create_parser=create_parser, add_language=vim._ts_add_language, inspect_language=vim._ts_inspect_language, } - diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 8916e59563..f3f7f4fd0a 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -8,6 +8,7 @@ local insert = helpers.insert local meth_pcall = helpers.meth_pcall local exec_lua = helpers.exec_lua local iswin = helpers.iswin +local feed = helpers.feed before_each(clear) @@ -46,12 +47,11 @@ describe('tree-sitter API', function() }]]) exec_lua([[ - parser = vim.treesitter.create_parser(0, "c") - tree = parser:parse_tree() + parser = vim.treesitter.get_parser(0, "c") + tree = parser:parse() root = tree:root() ]]) - --eq("", exec_lua("return tostring(parser)")) eq("", exec_lua("return tostring(tree)")) eq("", exec_lua("return tostring(root)")) eq({0,0,3,0}, exec_lua("return {root:range()}")) @@ -60,6 +60,27 @@ describe('tree-sitter API', function() exec_lua("child = root:child(0)") eq("", exec_lua("return tostring(child)")) eq({0,0,2,1}, exec_lua("return {child:range()}")) + + exec_lua("descendant = root:descendant_for_point_range(1,2,1,12)") + eq("", exec_lua("return tostring(descendant)")) + eq({1,2,1,12}, exec_lua("return {descendant:range()}")) + eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()")) + + feed("2G7|ay") + exec_lua([[ + tree2 = parser:parse() + root2 = tree2:root() + descendant2 = root2:descendant_for_point_range(1,2,1,13) + ]]) + eq(false, exec_lua("return tree2 == tree1")) + eq("", exec_lua("return tostring(descendant2)")) + eq({1,2,1,13}, exec_lua("return {descendant2:range()}")) + + -- orginal tree did not change + eq({1,2,1,12}, exec_lua("return {descendant:range()}")) + + -- unchanged buffer: return the same tree + eq(true, exec_lua("return parser:parse() == tree2")) end) end) From 06ee45b9b1c14c7ce6cb23403cdbe2852d495cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Fri, 21 Jun 2019 14:14:51 +0200 Subject: [PATCH 0078/1293] tree-sitter: fix lint, delete "demo" plugin (replaced by functional tests) --- runtime/lua/treesitter_demo.lua | 58 -------------------- runtime/lua/vim/treesitter.lua | 2 +- runtime/plugin/ts_test.vim | 32 ----------- src/nvim/lua/executor.c | 4 +- src/nvim/lua/treesitter.c | 70 +++++++++++++------------ src/nvim/lua/treesitter.h | 4 ++ test/functional/lua/treesitter_spec.lua | 2 - 7 files changed, 44 insertions(+), 128 deletions(-) delete mode 100644 runtime/lua/treesitter_demo.lua delete mode 100644 runtime/plugin/ts_test.vim diff --git a/runtime/lua/treesitter_demo.lua b/runtime/lua/treesitter_demo.lua deleted file mode 100644 index 82c36f94c0..0000000000 --- a/runtime/lua/treesitter_demo.lua +++ /dev/null @@ -1,58 +0,0 @@ --- TODO: externalize this -local a = vim.api -_G.a = vim.api - -if __treesitter_rt_ns == nil then - __treesitter_rt_ns = a.nvim_create_namespace("treesitter_demp") -end -local my_ns = __treesitter_rt_ns - -function ts_inspect_pos(row,col) - local tree = theparser:parse_tree() - local root = tree:root() - local node = root:descendant_for_point_range(row,col,row,col) - show_node(node) -end - -function show_node(node) - if node == nil then - return - end - a.nvim_buf_clear_highlight(0, my_ns, 0, -1) - shown_node = node - print(node:type()) - local start_row, start_col, end_row, end_col = node:range() - - a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", start_row, start_col, start_col+1) - - if end_col >= 1 then - end_col = end_col - 1 - end - a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", end_row, end_col, end_col+1) -end - -function ts_expand_node() - if shown_node == nil then - return - end - parent = shown_node:parent() - show_node(parent) -end - -function ts_cursor() - local row, col = unpack(a.nvim_win_get_cursor(0)) - ts_inspect_pos(row-1, col) -end - -if false then - ctree = theparser.tree - root = ctree:root() - cursor = root:to_cursor() - node = cursor:forward(5000) if true then return node end - print(#root) - c = root:child(50) - print(require'inspect'{c:extent()}) - type(ctree.__tostring) - root:__tostring() - print(_tslua_debug()) -end diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index f26d63d6ce..8aa170061b 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -50,7 +50,7 @@ local function create_parser(bufnr, ft, id) self:parse() -- TODO: use weakref to self, so that the parser is free'd is no plugin is -- using it. - local function lines_cb(ev, ...) + local function lines_cb(_, ...) return on_lines(self, ...) end local detach_cb = nil diff --git a/runtime/plugin/ts_test.vim b/runtime/plugin/ts_test.vim deleted file mode 100644 index 15192d8dda..0000000000 --- a/runtime/plugin/ts_test.vim +++ /dev/null @@ -1,32 +0,0 @@ -let g:ts_test_path = expand(":p:h:h") -let g:has_ts = v:false - -func! TSTest() - if g:has_ts - return - end - " TODO: module! - lua theparser = vim.treesitter.create_parser(0) - lua require'treesitter_demo' - let g:has_ts = v:true -endfunc - -func! TSCursor() - " disable matchparen - NoMatchParen - call TSTest() - au CursorMoved lua ts_cursor() - au CursorMovedI lua ts_cursor() - map (ts-expand) lua ts_expand_node() -endfunc - -func! TSSyntax() - " disable matchparen - set syntax= - call TSTest() - lua ts_syntax() -endfunc - -command! TSTest call TSTest() -command! TSCursor call TSCursor() -command! TSSyntax call TSSyntax() diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index aa83e3c1ba..127458fe39 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -839,9 +839,9 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_pushcfunction(lstate, create_tslua_parser); lua_setfield(lstate, -2, "_create_ts_parser"); - lua_pushcfunction(lstate, ts_lua_register_lang); + lua_pushcfunction(lstate, tslua_register_lang); lua_setfield(lstate, -2, "_ts_add_language"); - lua_pushcfunction(lstate, ts_lua_inspect_lang); + lua_pushcfunction(lstate, tslua_inspect_lang); lua_setfield(lstate, -2, "_ts_inspect_language"); } diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 016c175b59..c27ae8c877 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -1,9 +1,9 @@ // This is an open source non-commercial project. Dear PVS-Studio, please check // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com -// lua bindings for tree-siter. -// NB: this file should contain a generic lua interface for -// tree-sitter trees and nodes, and could be broken out as a reusable library +// lua bindings for tree-sitter. +// NB: this file mostly contains a generic lua interface for tree-sitter +// trees and nodes, and could be broken out as a reusable lua package #include #include @@ -22,9 +22,9 @@ #include "nvim/memline.h" typedef struct { - TSParser *parser; - TSTree *tree; // internal tree, used for editing/reparsing -} Tslua_parser; + TSParser *parser; + TSTree *tree; // internal tree, used for editing/reparsing +} TSLua_parser; #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/treesitter.c.generated.h" @@ -68,9 +68,9 @@ static struct luaL_Reg node_meta[] = { { NULL, NULL } }; -PMap(cstr_t) *langs; +static PMap(cstr_t) *langs; -void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta) +static void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta) { if (luaL_newmetatable(L, tname)) { // [meta] for (size_t i = 0; meta[i].name != NULL; i++) { @@ -84,8 +84,6 @@ void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta) lua_pop(L, 1); // [] (don't use it now) } - - /// init the tslua library /// /// all global state is stored in the regirstry of the lua_State @@ -95,13 +93,11 @@ void tslua_init(lua_State *L) // type metatables build_meta(L, "treesitter_parser", parser_meta); - build_meta(L, "treesitter_tree", tree_meta); - build_meta(L, "treesitter_node", node_meta); } -int ts_lua_register_lang(lua_State *L) +int tslua_register_lang(lua_State *L) { if (lua_gettop(L) < 2 || !lua_isstring(L, 1) || !lua_isstring(L, 2)) { return luaL_error(L, "string expected"); @@ -114,22 +110,27 @@ int ts_lua_register_lang(lua_State *L) return 0; } - // TODO: unsafe! - char symbol_buf[128] = "tree_sitter_"; - STRCAT(symbol_buf, lang_name); +#define BUFSIZE 128 + char symbol_buf[BUFSIZE]; + snprintf(symbol_buf, BUFSIZE, "tree_sitter_%s", lang_name); +#undef BUFSIZE - // TODO: we should maybe keep the uv_lib_t around, and close them - // at exit, to keep LeakSanitizer happy. uv_lib_t lib; if (uv_dlopen(path, &lib)) { - return luaL_error(L, "Failed to load parser: uv_dlopen: %s", - uv_dlerror(&lib)); + snprintf((char *)IObuff, IOSIZE, "Failed to load parser: uv_dlopen: %s", + uv_dlerror(&lib)); + uv_dlclose(&lib); + lua_pushstring(L, (char *)IObuff); + return lua_error(L); } TSLanguage *(*lang_parser)(void); if (uv_dlsym(&lib, symbol_buf, (void **)&lang_parser)) { - return luaL_error(L, "Failed to load parser: uv_dlsym: %s", - uv_dlerror(&lib)); + snprintf((char *)IObuff, IOSIZE, "Failed to load parser: uv_dlsym: %s", + uv_dlerror(&lib)); + uv_dlclose(&lib); + lua_pushstring(L, (char *)IObuff); + return lua_error(L); } TSLanguage *lang = lang_parser(); @@ -143,7 +144,7 @@ int ts_lua_register_lang(lua_State *L) return 1; } -int ts_lua_inspect_lang(lua_State *L) +int tslua_inspect_lang(lua_State *L) { if (lua_gettop(L) < 1 || !lua_isstring(L, 1)) { return luaL_error(L, "string expected"); @@ -176,7 +177,6 @@ int ts_lua_inspect_lang(lua_State *L) lua_setfield(L, -2, "symbols"); // [retval] - // TODO: this seems to be empty, what langs have fields? size_t nfields = (size_t)ts_language_field_count(lang); lua_createtable(L, nfields-1, 1); // [retval, fields] for (size_t i = 0; i < nfields; i++) { @@ -190,14 +190,14 @@ int ts_lua_inspect_lang(lua_State *L) int tslua_push_parser(lua_State *L, const char *lang_name) { - TSParser *parser = ts_parser_new(); TSLanguage *lang = pmap_get(cstr_t)(langs, lang_name); if (!lang) { return luaL_error(L, "no such language: %s", lang_name); } + TSParser *parser = ts_parser_new(); ts_parser_set_language(parser, lang); - Tslua_parser *p = lua_newuserdata(L, sizeof(Tslua_parser)); // [udata] + TSLua_parser *p = lua_newuserdata(L, sizeof(TSLua_parser)); // [udata] p->parser = parser; p->tree = NULL; @@ -206,14 +206,14 @@ int tslua_push_parser(lua_State *L, const char *lang_name) return 1; } -static Tslua_parser *parser_check(lua_State *L) +static TSLua_parser *parser_check(lua_State *L) { return luaL_checkudata(L, 1, "treesitter_parser"); } static int parser_gc(lua_State *L) { - Tslua_parser *p = parser_check(L); + TSLua_parser *p = parser_check(L); if (!p) { return 0; } @@ -238,6 +238,7 @@ static const char *input_cb(void *payload, uint32_t byte_index, buf_T *bp = payload; #define BUFSIZE 256 static char buf[BUFSIZE]; + if ((linenr_T)position.row >= bp->b_ml.ml_line_count) { *bytes_read = 0; return ""; @@ -246,10 +247,13 @@ static const char *input_cb(void *payload, uint32_t byte_index, size_t len = STRLEN(line); size_t tocopy = MIN(len-position.column, BUFSIZE); - // TODO: translate embedded \n to \000 memcpy(buf, line+position.column, tocopy); + // Translate embedded \n to NUL + memchrsub(buf, '\n', '\0', tocopy); *bytes_read = (uint32_t)tocopy; - if (tocopy < 200) { + if (tocopy < BUFSIZE) { + // now add the final \n. If it didn't fit, input_cb will be called again + // on the same line with advanced column. buf[tocopy] = '\n'; (*bytes_read)++; } @@ -259,7 +263,7 @@ static const char *input_cb(void *payload, uint32_t byte_index, static int parser_parse_buf(lua_State *L) { - Tslua_parser *p = parser_check(L); + TSLua_parser *p = parser_check(L); if (!p) { return 0; } @@ -282,7 +286,7 @@ static int parser_parse_buf(lua_State *L) static int parser_tree(lua_State *L) { - Tslua_parser *p = parser_check(L); + TSLua_parser *p = parser_check(L); if (!p) { return 0; } @@ -298,7 +302,7 @@ static int parser_edit(lua_State *L) return lua_error(L); } - Tslua_parser *p = parser_check(L); + TSLua_parser *p = parser_check(L); if (!p) { return 0; } diff --git a/src/nvim/lua/treesitter.h b/src/nvim/lua/treesitter.h index d59b5e47a0..812166f67b 100644 --- a/src/nvim/lua/treesitter.h +++ b/src/nvim/lua/treesitter.h @@ -1,6 +1,10 @@ #ifndef NVIM_LUA_TREESITTER_H #define NVIM_LUA_TREESITTER_H +#include +#include +#include + #include "tree_sitter/api.h" #ifdef INCLUDE_GENERATED_DECLARATIONS diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index f3f7f4fd0a..5aaaa80868 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -1,11 +1,9 @@ -- Test suite for testing interactions with API bindings local helpers = require('test.functional.helpers')(after_each) -local meths = helpers.meths local clear = helpers.clear local eq = helpers.eq local insert = helpers.insert -local meth_pcall = helpers.meth_pcall local exec_lua = helpers.exec_lua local iswin = helpers.iswin local feed = helpers.feed From f86a2c33a2e54193598be1d8c856363e02b91e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Mon, 5 Aug 2019 19:37:17 +0200 Subject: [PATCH 0079/1293] tree-sitter: simplify editing using the new old_byte_size parameter --- runtime/lua/vim/treesitter.lua | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 8aa170061b..69b1ac8716 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -17,27 +17,12 @@ function Parser:parse() return self.tree end -local function on_lines(self, bufnr, _, start_row, oldstopline, stop_row) +local function on_lines(self, bufnr, _, start_row, old_stop_row, stop_row, old_byte_size) local start_byte = a.nvim_buf_get_offset(bufnr,start_row) - -- a bit messy, should we expose edited but not reparsed tree? - -- are multiple edits safe in general? - local root = self._parser:tree():root() - -- TODO: add proper lookup function! - local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0) - if inode == nil then - local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) - self._parser:edit(start_byte,stop_byte,stop_byte, - start_row,0,stop_row,0,stop_row,0) - else - local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start() - local fake_rows = fakeoldstoprow-oldstopline - local fakestop = stop_row+fake_rows - local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol - self._parser:edit(start_byte, fakebyteoldstop, fakebytestop, - start_row, 0, - fakeoldstoprow, fakeoldstopcol, - fakestop, fakeoldstopcol) - end + local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) + local old_stop_byte = start_byte + old_byte_size + self._parser:edit(start_byte,old_stop_byte,stop_byte, + start_row,0,old_stop_row,0,stop_row,0) self.valid = false end From e0d6228978dd1389f75a3e0351f6e6d5625643ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 21 Sep 2019 10:10:47 +0200 Subject: [PATCH 0080/1293] tree-sitter: use "range" instead of "point_range" consistently in lua API --- src/nvim/lua/treesitter.c | 8 ++++---- test/functional/lua/treesitter_spec.lua | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index c27ae8c877..a71234d2c4 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -62,8 +62,8 @@ static struct luaL_Reg node_meta[] = { { "named_child_count", node_named_child_count }, { "child", node_child }, { "named_child", node_named_child }, - { "descendant_for_point_range", node_descendant_for_point_range }, - { "named_descendant_for_point_range", node_named_descendant_for_point_range }, + { "descendant_for_range", node_descendant_for_range }, + { "named_descendant_for_range", node_named_descendant_for_range }, { "parent", node_parent }, { NULL, NULL } }; @@ -587,7 +587,7 @@ static int node_named_child(lua_State *L) return 1; } -static int node_descendant_for_point_range(lua_State *L) +static int node_descendant_for_range(lua_State *L) { TSNode node; if (!node_check(L, &node)) { @@ -604,7 +604,7 @@ static int node_descendant_for_point_range(lua_State *L) return 1; } -static int node_named_descendant_for_point_range(lua_State *L) +static int node_named_descendant_for_range(lua_State *L) { TSNode node; if (!node_check(L, &node)) { diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 5aaaa80868..d566f15649 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -59,7 +59,7 @@ describe('tree-sitter API', function() eq("", exec_lua("return tostring(child)")) eq({0,0,2,1}, exec_lua("return {child:range()}")) - exec_lua("descendant = root:descendant_for_point_range(1,2,1,12)") + exec_lua("descendant = root:descendant_for_range(1,2,1,12)") eq("", exec_lua("return tostring(descendant)")) eq({1,2,1,12}, exec_lua("return {descendant:range()}")) eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()")) @@ -68,7 +68,7 @@ describe('tree-sitter API', function() exec_lua([[ tree2 = parser:parse() root2 = tree2:root() - descendant2 = root2:descendant_for_point_range(1,2,1,13) + descendant2 = root2:descendant_for_range(1,2,1,13) ]]) eq(false, exec_lua("return tree2 == tree1")) eq("", exec_lua("return tostring(descendant2)")) From 3ffcb477ef1e4ae0a7183a934382ffd2c449d818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 21 Sep 2019 10:35:49 +0200 Subject: [PATCH 0081/1293] tree-sitter: start docs --- runtime/doc/if_lua.txt | 106 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/runtime/doc/if_lua.txt b/runtime/doc/if_lua.txt index aa2d0a03c6..0ba35aeae6 100644 --- a/runtime/doc/if_lua.txt +++ b/runtime/doc/if_lua.txt @@ -445,6 +445,112 @@ Example: TCP echo-server *tcp-server* end) print('TCP echo-server listening on port: '..server:getsockname().port) +------------------------------------------------------------------------------ +VIM.TREESITTER *lua-treesitter* + +Nvim integrates the tree-sitter library for incremental parsing of buffers. + +Currently Nvim does not provide the tree-sitter parsers, instead these must +be built separately, for instance using the tree-sitter utility. +The parser is loaded into nvim using > + + vim.treesitter.add_language("/path/to/c_parser.so", "c") + + + + parser = vim.treesitter.get_parser(bufnr, lang) + +<`bufnr=0` can be used for current buffer. `lang` will default to 'filetype' (this +doesn't work yet for some filetypes like "cpp") Currently, the parser will be +retained for the lifetime of a buffer but this is subject to change. A plugin +should keep a reference to the parser object as long as it wants incremental +updates. + +Whenever you need to access the current syntax tree, parse the buffer: > + + tstree = parser:parse() + + Date: Sun, 22 Sep 2019 11:33:55 +0200 Subject: [PATCH 0082/1293] tree-sitter: handle node equality --- src/nvim/lua/treesitter.c | 18 ++++++++++++++++++ test/functional/lua/treesitter_spec.lua | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index a71234d2c4..d2072402bb 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -48,6 +48,7 @@ static struct luaL_Reg tree_meta[] = { static struct luaL_Reg node_meta[] = { { "__tostring", node_tostring }, + { "__eq", node_eq }, { "__len", node_child_count }, { "range", node_range }, { "start", node_start }, @@ -431,6 +432,23 @@ static int node_tostring(lua_State *L) return 1; } +static int node_eq(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + // This should only be called if both x and y in "x == y" has the + // treesitter_node metatable. So it is ok to error out otherwise. + TSNode *ud = luaL_checkudata(L, 2, "treesitter_node"); + if (!ud) { + return 0; + } + TSNode node2 = *ud; + lua_pushboolean(L, ts_node_eq(node, node2)); + return 1; +} + static int node_range(lua_State *L) { TSNode node; diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index d566f15649..8e21faca12 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -64,6 +64,13 @@ describe('tree-sitter API', function() eq({1,2,1,12}, exec_lua("return {descendant:range()}")) eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()")) + eq(true, exec_lua("return child == child")) + -- separate lua object, but represents same node + eq(true, exec_lua("return child == root:child(0)")) + eq(false, exec_lua("return child == descendant2")) + eq(false, exec_lua("return child == nil")) + eq(false, exec_lua("return child == tree")) + feed("2G7|ay") exec_lua([[ tree2 = parser:parse() @@ -71,6 +78,7 @@ describe('tree-sitter API', function() descendant2 = root2:descendant_for_range(1,2,1,13) ]]) eq(false, exec_lua("return tree2 == tree1")) + eq(false, exec_lua("return root2 == root")) eq("", exec_lua("return tostring(descendant2)")) eq({1,2,1,13}, exec_lua("return {descendant2:range()}")) From c844f986d4c8f34bbe60591270178504b7045a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 28 Sep 2019 14:04:05 +0200 Subject: [PATCH 0083/1293] tree-sitter: use "module" pattern in lua source --- runtime/lua/vim/treesitter.lua | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 69b1ac8716..e0202927bb 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -1,13 +1,13 @@ local a = vim.api -local Parser = {} -Parser.__index = Parser - -- TODO(bfredl): currently we retain parsers for the lifetime of the buffer. -- Consider use weak references to release parser if all plugins are done with -- it. local parsers = {} +local Parser = {} +Parser.__index = Parser + function Parser:parse() if self.valid then return self.tree @@ -17,7 +17,7 @@ function Parser:parse() return self.tree end -local function on_lines(self, bufnr, _, start_row, old_stop_row, stop_row, old_byte_size) +function Parser:_on_lines(bufnr, _, start_row, old_stop_row, stop_row, old_byte_size) local start_byte = a.nvim_buf_get_offset(bufnr,start_row) local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row) local old_stop_byte = start_byte + old_byte_size @@ -26,17 +26,22 @@ local function on_lines(self, bufnr, _, start_row, old_stop_row, stop_row, old_b self.valid = false end -local function create_parser(bufnr, ft, id) +local module = { + add_language=vim._ts_add_language, + inspect_language=vim._ts_inspect_language, +} + +function module.create_parser(bufnr, ft, id) if bufnr == 0 then bufnr = a.nvim_get_current_buf() end local self = setmetatable({bufnr=bufnr, valid=false}, Parser) self._parser = vim._create_ts_parser(ft) self:parse() - -- TODO: use weakref to self, so that the parser is free'd is no plugin is + -- TODO(bfredl): use weakref to self, so that the parser is free'd is no plugin is -- using it. local function lines_cb(_, ...) - return on_lines(self, ...) + return self:_on_lines(...) end local detach_cb = nil if id ~= nil then @@ -50,7 +55,7 @@ local function create_parser(bufnr, ft, id) return self end -local function get_parser(bufnr, ft) +function module.get_parser(bufnr, ft) if bufnr == nil or bufnr == 0 then bufnr = a.nvim_get_current_buf() end @@ -60,14 +65,9 @@ local function get_parser(bufnr, ft) local id = tostring(bufnr)..'_'..ft if parsers[id] == nil then - parsers[id] = create_parser(bufnr, ft, id) + parsers[id] = module.create_parser(bufnr, ft, id) end return parsers[id] end -return { - get_parser=get_parser, - create_parser=create_parser, - add_language=vim._ts_add_language, - inspect_language=vim._ts_inspect_language, -} +return module From 9fa850991dbe8984996afdc149b5b32dc248197e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 28 Sep 2019 09:32:55 +0200 Subject: [PATCH 0084/1293] tree-sitter: improve and cleanup tests --- test/functional/lua/treesitter_spec.lua | 73 ++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 8e21faca12..700e4599f2 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -7,34 +7,40 @@ local insert = helpers.insert local exec_lua = helpers.exec_lua local iswin = helpers.iswin local feed = helpers.feed +local pcall_err = helpers.pcall_err +local matches = helpers.matches before_each(clear) -describe('tree-sitter API', function() +describe('treesitter API', function() -- error tests not requiring a parser library - it('handles basic errors', function() - --eq({false, 'Error executing lua: vim.schedule: expected function'}, - -- meth_pcall(meths.execute_lua, "parser = vim.treesitter.create_parser(0, 'nosuchlang')", {})) + it('handles missing language', function() + local path_pat = 'Error executing lua: '..(iswin() and '.+\\vim\\' or '.+/vim/') + matches(path_pat..'treesitter.lua:39: no such language: borklang', + pcall_err(exec_lua, "parser = vim.treesitter.create_parser(0, 'borklang')")) + -- actual message depends on platform + matches('Error executing lua: Failed to load parser: uv_dlopen: .+', + pcall_err(exec_lua, "parser = vim.treesitter.add_language('borkbork.so', 'borklang')")) + eq('Error executing lua: [string ""]:1: no such language: borklang', + pcall_err(exec_lua, "parser = vim.treesitter.inspect_language('borklang')")) end) local ts_path = os.getenv("TREE_SITTER_DIR") describe('with C parser', function() if ts_path == nil then - it("works", function() pending("TREE_SITTER_PATH not set, skipping tree-sitter parser tests") end) + it("works", function() pending("TREE_SITTER_PATH not set, skipping treesitter parser tests") end) return end before_each(function() - -- TODO the .so/.dylib/.dll thingie local path = ts_path .. '/bin/c'..(iswin() and '.dll' or '.so') exec_lua([[ local path = ... vim.treesitter.add_language(path,'c') - ]], path) end) @@ -48,6 +54,7 @@ describe('tree-sitter API', function() parser = vim.treesitter.get_parser(0, "c") tree = parser:parse() root = tree:root() + lang = vim.treesitter.inspect_language('c') ]]) eq("", exec_lua("return tostring(tree)")) @@ -59,10 +66,21 @@ describe('tree-sitter API', function() eq("", exec_lua("return tostring(child)")) eq({0,0,2,1}, exec_lua("return {child:range()}")) + eq("function_definition", exec_lua("return child:type()")) + eq(true, exec_lua("return child:named()")) + eq("number", type(exec_lua("return child:symbol()"))) + eq({'function_definition', true}, exec_lua("return lang.symbols[child:symbol()]")) + + exec_lua("anon = root:descendant_for_range(0,8,0,9)") + eq("(", exec_lua("return anon:type()")) + eq(false, exec_lua("return anon:named()")) + eq("number", type(exec_lua("return anon:symbol()"))) + eq({'(', false}, exec_lua("return lang.symbols[anon:symbol()]")) + exec_lua("descendant = root:descendant_for_range(1,2,1,12)") eq("", exec_lua("return tostring(descendant)")) eq({1,2,1,12}, exec_lua("return {descendant:range()}")) - eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()")) + eq("(declaration type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))", exec_lua("return descendant:sexpr()")) eq(true, exec_lua("return child == child")) -- separate lua object, but represents same node @@ -89,6 +107,43 @@ describe('tree-sitter API', function() eq(true, exec_lua("return parser:parse() == tree2")) end) + it('inspects language', function() + local keys, fields, symbols = unpack(exec_lua([[ + local lang = vim.treesitter.inspect_language('c') + local keys, symbols = {}, {} + for k,_ in pairs(lang) do + keys[k] = true + end + + -- symbols array can have "holes" and is thus not a valid msgpack array + -- but we don't care about the numbers here (checked in the parser test) + for _, v in pairs(lang.symbols) do + table.insert(symbols, v) + end + return {keys, lang.fields, symbols} + ]])) + + eq({fields=true, symbols=true}, keys) + + local fset = {} + for _,f in pairs(fields) do + eq("string", type(f)) + fset[f] = true + end + eq(true, fset["directive"]) + eq(true, fset["initializer"]) + + local has_named, has_anonymous + for _,s in pairs(symbols) do + eq("string", type(s[1])) + eq("boolean", type(s[2])) + if s[1] == "for_statement" and s[2] == true then + has_named = true + elseif s[1] == "|=" and s[2] == false then + has_anonymous = true + end + end + eq({true,true}, {has_named,has_anonymous}) + end) end) end) - From 0636b25f28e408c8b16026354db7edfef079614a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 28 Sep 2019 21:00:27 +0200 Subject: [PATCH 0085/1293] cmdline: wildmenumode() should be true with wildoptions+=pum --- src/nvim/eval.c | 3 ++- test/functional/ui/popupmenu_spec.lua | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 2ddcd389fe..cb1dd1d631 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -18845,8 +18845,9 @@ static void f_visualmode(typval_T *argvars, typval_T *rettv, FunPtr fptr) */ static void f_wildmenumode(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - if (wild_menu_showing) + if (wild_menu_showing || ((State & CMDLINE) && pum_visible())) { rettv->vval.v_number = 1; + } } /// "win_findbuf()" function diff --git a/test/functional/ui/popupmenu_spec.lua b/test/functional/ui/popupmenu_spec.lua index ae2136f451..37eb550835 100644 --- a/test/functional/ui/popupmenu_spec.lua +++ b/test/functional/ui/popupmenu_spec.lua @@ -516,6 +516,7 @@ describe('ui/ext_popupmenu', function() {1:~ }| :sign ^ | ]]) + eq(0, funcs.wildmenumode()) feed('') screen:expect{grid=[[ @@ -530,6 +531,7 @@ describe('ui/ext_popupmenu', function() {1:~ }| :sign define^ | ]], popupmenu={items=wild_expected, pos=0, anchor={1, 9, 6}}} + eq(1, funcs.wildmenumode()) feed('') screen:expect{grid=[[ @@ -589,6 +591,7 @@ describe('ui/ext_popupmenu', function() :sign unplace^ | ]], popupmenu={items=wild_expected, pos=5, anchor={1, 9, 6}}} feed('') + eq(0, funcs.wildmenumode()) -- check positioning with multibyte char in pattern command("e långfile1") From 34d55f86077e8a937c1ac1b0a0c551a5968fa7f8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 29 Sep 2019 03:06:36 +0200 Subject: [PATCH 0086/1293] terminfo_start: keep first flushing of ui buffer (#11118) Initially done in 3626d2107 (#11074, for #11062), it was reverted then in 445f2f409 (#11083, which added flushing later). But it is still required here to avoid the reporting of the background response with urxvt/kitty (`nvim -u NONE -cq`). Apparently I've tested this not enough with 445f2f409 (probably only within tmux). --- src/nvim/tui/tui.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index 55c4a955c2..0c282a3f1f 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -312,6 +312,7 @@ static void terminfo_start(UI *ui) uv_pipe_init(&data->write_loop, &data->output_handle.pipe, 0); uv_pipe_open(&data->output_handle.pipe, data->out_fd); } + flush_buf(ui); } static void terminfo_stop(UI *ui) From 0a2a1e241fee64d8ceeca023dab4aca71b62dfbb Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 29 Sep 2019 04:44:02 +0200 Subject: [PATCH 0087/1293] cmake: use LibFindMacros for utf8proc (#11114) Also update doc. --- CMakeLists.txt | 2 +- README.md | 2 +- cmake/FindUTF8PROC.cmake | 16 ++++++++++++ cmake/FindUtf8proc.cmake | 54 ---------------------------------------- contrib/local.mk.example | 1 + 5 files changed, 19 insertions(+), 56 deletions(-) create mode 100644 cmake/FindUTF8PROC.cmake delete mode 100644 cmake/FindUtf8proc.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a4b21f4f6..26c82449b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -375,7 +375,7 @@ include_directories(SYSTEM ${MSGPACK_INCLUDE_DIRS}) find_package(LibLUV 1.30.0 REQUIRED) include_directories(SYSTEM ${LIBLUV_INCLUDE_DIRS}) -find_package(Utf8proc REQUIRED) +find_package(UTF8PROC REQUIRED) include_directories(SYSTEM ${UTF8PROC_INCLUDE_DIRS}) if(WIN32) add_definitions(-DUTF8PROC_STATIC) diff --git a/README.md b/README.md index 1bdf33d6bb..338c8df70e 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ To skip bundled (`third-party/*`) dependencies: 1. Install the dependencies using a package manager. ``` - sudo apt install gperf luajit luarocks libuv1-dev libluajit-5.1-dev libunibilium-dev libmsgpack-dev libtermkey-dev libvterm-dev + sudo apt install gperf luajit luarocks libuv1-dev libluajit-5.1-dev libunibilium-dev libmsgpack-dev libtermkey-dev libvterm-dev libutf8proc-dev sudo luarocks build mpack sudo luarocks build lpeg sudo luarocks build inspect diff --git a/cmake/FindUTF8PROC.cmake b/cmake/FindUTF8PROC.cmake new file mode 100644 index 0000000000..fdb462b779 --- /dev/null +++ b/cmake/FindUTF8PROC.cmake @@ -0,0 +1,16 @@ +# - Try to find utf8proc +# Once done this will define +# UTF8PROC_FOUND - System has utf8proc +# UTF8PROC_INCLUDE_DIRS - The utf8proc include directories +# UTF8PROC_LIBRARIES - The libraries needed to use utf8proc + +include(LibFindMacros) + +set(UTF8PROC_NAMES utf8proc) +if(MSVC) + # "utf8proc_static" is used for MSVC (when built statically from third-party). + # https://github.com/JuliaStrings/utf8proc/commit/0975bf9b6. + list(APPEND UTF8PROC_NAMES utf8proc_static) +endif() +libfind_pkg_detect(UTF8PROC utf8proc FIND_PATH utf8proc.h FIND_LIBRARY ${UTF8PROC_NAMES}) +libfind_process(UTF8PROC REQUIRED) diff --git a/cmake/FindUtf8proc.cmake b/cmake/FindUtf8proc.cmake deleted file mode 100644 index dc4f7016a1..0000000000 --- a/cmake/FindUtf8proc.cmake +++ /dev/null @@ -1,54 +0,0 @@ -# - Try to find utf8proc -# Once done this will define -# UTF8PROC_FOUND - System has utf8proc -# UTF8PROC_INCLUDE_DIRS - The utf8proc include directories -# UTF8PROC_LIBRARIES - The libraries needed to use utf8proc - -if(NOT USE_BUNDLED_UTF8PROC) - find_package(PkgConfig) - if (PKG_CONFIG_FOUND) - pkg_check_modules(PC_UTF8PROC QUIET utf8proc) - endif() -else() - set(PC_UTF8PROC_INCLUDEDIR) - set(PC_UTF8PROC_INCLUDE_DIRS) - set(PC_UTF8PROC_LIBDIR) - set(PC_UTF8PROC_LIBRARY_DIRS) - set(LIMIT_SEARCH NO_DEFAULT_PATH) -endif() - -set(UTF8PROC_DEFINITIONS ${PC_UTF8PROC_CFLAGS_OTHER}) - -find_path(UTF8PROC_INCLUDE_DIR utf8proc.h - PATHS ${PC_UTF8PROC_INCLUDEDIR} ${PC_UTF8PROC_INCLUDE_DIRS} - ${LIMIT_SEARCH}) - -# If we're asked to use static linkage, add libutf8proc.a as a preferred library name. -if(UTF8PROC_USE_STATIC) - list(APPEND UTF8PROC_NAMES - "${CMAKE_STATIC_LIBRARY_PREFIX}utf8proc${CMAKE_STATIC_LIBRARY_SUFFIX}") -if(MSVC) - list(APPEND UTF8PROC_NAMES - "${CMAKE_STATIC_LIBRARY_PREFIX}utf8proc_static${CMAKE_STATIC_LIBRARY_SUFFIX}") -endif() -endif() - -list(APPEND UTF8PROC_NAMES utf8proc) -if(MSVC) - list(APPEND UTF8PROC_NAMES utf8proc_static) -endif() - -find_library(UTF8PROC_LIBRARY NAMES ${UTF8PROC_NAMES} - HINTS ${PC_UTF8PROC_LIBDIR} ${PC_UTF8PROC_LIBRARY_DIRS} - ${LIMIT_SEARCH}) - -set(UTF8PROC_LIBRARIES ${UTF8PROC_LIBRARY}) -set(UTF8PROC_INCLUDE_DIRS ${UTF8PROC_INCLUDE_DIR}) - -include(FindPackageHandleStandardArgs) -# handle the QUIETLY and REQUIRED arguments and set UTF8PROC_FOUND to TRUE -# if all listed variables are TRUE -find_package_handle_standard_args(Utf8proc DEFAULT_MSG - UTF8PROC_LIBRARY UTF8PROC_INCLUDE_DIR) - -mark_as_advanced(UTF8PROC_INCLUDE_DIR UTF8PROC_LIBRARY) diff --git a/contrib/local.mk.example b/contrib/local.mk.example index 4f7f077999..778e848d60 100644 --- a/contrib/local.mk.example +++ b/contrib/local.mk.example @@ -48,6 +48,7 @@ # DEPS_CMAKE_FLAGS += -DUSE_BUNDLED_LUAROCKS=OFF # DEPS_CMAKE_FLAGS += -DUSE_BUNDLED_MSGPACK=OFF # DEPS_CMAKE_FLAGS += -DUSE_BUNDLED_UNIBILIUM=OFF +# DEPS_CMAKE_FLAGS += -DUSE_BUNDLED_UTF8PROC=OFF # # Or disable all bundled dependencies at once. # From b18b84df5eab9829ecbef644ef0af226becf881d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 30 Sep 2019 00:10:29 +0200 Subject: [PATCH 0088/1293] build: run git-describe for dev version during build (#11117) This avoids invoking CMake after a new commit, which might take 15s on some systems. Skipped on CMake < 3.2.0 (missing BYPRODUCTS support). Co-Authored-By: Justin M. Keyes --- CMakeLists.txt | 13 -- ci/before_install.sh | 2 +- cmake/GetGitRevisionDescription.cmake | 180 ----------------------- cmake/GetGitRevisionDescription.cmake.in | 38 ----- config/CMakeLists.txt | 29 ++++ scripts/update_version_stamp.lua | 45 ++++++ 6 files changed, 75 insertions(+), 232 deletions(-) delete mode 100644 cmake/GetGitRevisionDescription.cmake delete mode 100644 cmake/GetGitRevisionDescription.cmake.in create mode 100644 scripts/update_version_stamp.lua diff --git a/CMakeLists.txt b/CMakeLists.txt index 26c82449b3..83fd67837d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,19 +122,6 @@ set(NVIM_API_LEVEL 6) # Bump this after any API change. set(NVIM_API_LEVEL_COMPAT 0) # Adjust this after a _breaking_ API change. set(NVIM_API_PRERELEASE false) -file(TO_CMAKE_PATH ${CMAKE_CURRENT_LIST_DIR}/.git FORCED_GIT_DIR) -include(GetGitRevisionDescription) -get_git_head_revision(GIT_REFSPEC NVIM_VERSION_COMMIT) -if(NVIM_VERSION_COMMIT) # is a git repo - git_describe(NVIM_VERSION_MEDIUM) - # `git describe` annotates the most recent tagged release; for pre-release - # builds we must replace that with the unreleased version. - string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+" - "v${NVIM_VERSION_MAJOR}.${NVIM_VERSION_MINOR}.${NVIM_VERSION_PATCH}" - NVIM_VERSION_MEDIUM - ${NVIM_VERSION_MEDIUM}) -endif() - set(NVIM_VERSION_BUILD_TYPE "${CMAKE_BUILD_TYPE}") # NVIM_VERSION_CFLAGS set further below. diff --git a/ci/before_install.sh b/ci/before_install.sh index 5cb6894b8c..283605e113 100755 --- a/ci/before_install.sh +++ b/ci/before_install.sh @@ -52,7 +52,7 @@ nvm use --lts if [[ -n "$CMAKE_URL" ]]; then echo "Installing custom CMake: $CMAKE_URL" - curl --retry 5 --silent --fail -o /tmp/cmake-installer.sh "$CMAKE_URL" + curl --retry 5 --silent --show-error --fail -o /tmp/cmake-installer.sh "$CMAKE_URL" mkdir -p "$HOME/.local/bin" /opt/cmake-custom bash /tmp/cmake-installer.sh --prefix=/opt/cmake-custom --skip-license ln -sfn /opt/cmake-custom/bin/cmake "$HOME/.local/bin/cmake" diff --git a/cmake/GetGitRevisionDescription.cmake b/cmake/GetGitRevisionDescription.cmake deleted file mode 100644 index 5044c682e4..0000000000 --- a/cmake/GetGitRevisionDescription.cmake +++ /dev/null @@ -1,180 +0,0 @@ -# https://github.com/rpavlik/cmake-modules -# -# - Returns a version string from Git -# -# These functions force a re-configure on each git commit so that you can -# trust the values of the variables in your build system. -# -# get_git_head_revision( [ ...]) -# -# Returns the refspec and sha hash of the current head revision -# -# git_describe( [ ...]) -# -# Returns the results of git describe on the source tree, and adjusting -# the output so that it tests false if an error occurs. -# -# git_get_exact_tag( [ ...]) -# -# Returns the results of git describe --exact-match on the source tree, -# and adjusting the output so that it tests false if there was no exact -# matching tag. -# -# Requires CMake 2.6 or newer (uses the 'function' command) -# -# Original Author: -# 2009-2010 Ryan Pavlik -# http://academic.cleardefinition.com -# Iowa State University HCI Graduate Program/VRAC -# -# Copyright Iowa State University 2009-2010. -# Distributed under the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -if(__get_git_revision_description) - return() -endif() -set(__get_git_revision_description YES) - -# We must run the following at "include" time, not at function call time, -# to find the path to this module rather than the path to a calling list file -get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH) - -function(get_git_dir _gitdir) - # check FORCED_GIT_DIR first - if(FORCED_GIT_DIR) - set(${_gitdir} ${FORCED_GIT_DIR} PARENT_SCOPE) - return() - endif() - - # check GIT_DIR in environment - set(GIT_DIR $ENV{GIT_DIR}) - if(NOT GIT_DIR) - set(GIT_PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) - set(GIT_DIR ${GIT_PARENT_DIR}/.git) - endif() - # .git dir not found, search parent directories - while(NOT EXISTS ${GIT_DIR}) - set(GIT_PREVIOUS_PARENT ${GIT_PARENT_DIR}) - get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH) - if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT) - return() - endif() - set(GIT_DIR ${GIT_PARENT_DIR}/.git) - endwhile() - # check if this is a submodule - if(NOT IS_DIRECTORY ${GIT_DIR}) - file(READ ${GIT_DIR} submodule) - string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule}) - get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH) - get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE) - endif() - set(${_gitdir} ${GIT_DIR} PARENT_SCOPE) -endfunction() - -function(get_git_head_revision _refspecvar _hashvar) - get_git_dir(GIT_DIR) - if(NOT GIT_DIR) - return() - endif() - - set(GIT_DATA ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data) - if(NOT EXISTS ${GIT_DATA}) - file(MAKE_DIRECTORY ${GIT_DATA}) - endif() - - if(NOT EXISTS ${GIT_DIR}/HEAD) - return() - endif() - set(HEAD_FILE ${GIT_DATA}/HEAD) - configure_file(${GIT_DIR}/HEAD ${HEAD_FILE} COPYONLY) - - configure_file(${_gitdescmoddir}/GetGitRevisionDescription.cmake.in - ${GIT_DATA}/grabRef.cmake - @ONLY) - include(${GIT_DATA}/grabRef.cmake) - - set(${_refspecvar} ${HEAD_REF} PARENT_SCOPE) - set(${_hashvar} ${HEAD_HASH} PARENT_SCOPE) -endfunction() - -function(git_describe _var) - get_git_dir(GIT_DIR) - if(NOT GIT_DIR) - return() - endif() - - if(NOT GIT_FOUND) - find_package(Git QUIET) - endif() - if(NOT GIT_FOUND) - set(${_var} "GIT-NOTFOUND" PARENT_SCOPE) - return() - endif() - - get_git_head_revision(refspec hash) - if(NOT hash) - set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE) - return() - endif() - - execute_process(COMMAND - ${GIT_EXECUTABLE} - describe - ${hash} - ${ARGN} - WORKING_DIRECTORY - ${GIT_DIR} - RESULT_VARIABLE - res - OUTPUT_VARIABLE - out - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE) - if(NOT res EQUAL 0) - set(out "${out}-${res}-NOTFOUND") - endif() - - set(${_var} ${out} PARENT_SCOPE) -endfunction() - -function(git_timestamp _var) - get_git_dir(GIT_DIR) - if(NOT GIT_DIR) - return() - endif() - - if(NOT GIT_FOUND) - find_package(Git QUIET) - endif() - if(NOT GIT_FOUND) - return() - endif() - - get_git_head_revision(refspec hash) - if(NOT hash) - set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE) - return() - endif() - - execute_process(COMMAND ${GIT_EXECUTABLE} log -1 --format="%ci" ${hash} ${ARGN} - WORKING_DIRECTORY ${GIT_DIR} - RESULT_VARIABLE res - OUTPUT_VARIABLE out - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE) - if(res EQUAL 0) - string(REGEX REPLACE "[-\" :]" "" out ${out}) - string(SUBSTRING ${out} 0 12 out) - else() - set(out "${out}-${res}-NOTFOUND") - endif() - - set(${_var} ${out} PARENT_SCOPE) -endfunction() - -function(git_get_exact_tag _var) - git_describe(out --exact-match ${ARGN}) - set(${_var} ${out} PARENT_SCOPE) -endfunction() diff --git a/cmake/GetGitRevisionDescription.cmake.in b/cmake/GetGitRevisionDescription.cmake.in deleted file mode 100644 index 8a085b2671..0000000000 --- a/cmake/GetGitRevisionDescription.cmake.in +++ /dev/null @@ -1,38 +0,0 @@ -# -# Internal file for GetGitRevisionDescription.cmake -# -# Requires CMake 2.6 or newer (uses the 'function' command) -# -# Original Author: -# 2009-2010 Ryan Pavlik -# http://academic.cleardefinition.com -# Iowa State University HCI Graduate Program/VRAC -# -# Copyright Iowa State University 2009-2010. -# Distributed under the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -set(HEAD_HASH) - -file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024) - -string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS) -if(HEAD_CONTENTS MATCHES "ref") - # named branch - string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}") - if(EXISTS "@GIT_DIR@/${HEAD_REF}") - configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY) - elseif(EXISTS "@GIT_DIR@/logs/${HEAD_REF}") - configure_file("@GIT_DIR@/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY) - set(HEAD_HASH "${HEAD_REF}") - endif() -else() - # detached HEAD - configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY) -endif() - -if(NOT HEAD_HASH) - file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024) - string(STRIP "${HEAD_HASH}" HEAD_HASH) -endif() diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt index 0ca41d5dfd..7bd48a1f1e 100644 --- a/config/CMakeLists.txt +++ b/config/CMakeLists.txt @@ -121,6 +121,35 @@ configure_file ( ) # generate version definitions +if(NVIM_VERSION_MEDIUM) + message(STATUS "NVIM_VERSION_MEDIUM: ${NVIM_VERSION_MEDIUM}") +elseif(${CMAKE_VERSION} VERSION_LESS "3.2.0") + message(STATUS "Skipping version-string generation (requires CMake 3.2.0+)") +elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git) + find_program(GIT_EXECUTABLE git) + if(GIT_EXECUTABLE) + # Get current version. + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --dirty + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + OUTPUT_VARIABLE NVIM_VERSION_MEDIUM + OUTPUT_STRIP_TRAILING_WHITESPACE) + message(STATUS "NVIM_VERSION_MEDIUM (from git): ${NVIM_VERSION_MEDIUM}") + + # Create a update_version_stamp target to update the version during build. + file(RELATIVE_PATH relbuild "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}") + add_custom_target(update_version_stamp ALL + COMMAND ${LUA_PRG} scripts/update_version_stamp.lua + ${relbuild}/.version_stamp + ${relbuild}/config/auto/versiondef.h + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + BYPRODUCTS ${CMAKE_BINARY_DIR}/config/auto/versiondef.h) + add_dependencies(nvim update_version_stamp) + else() + message(STATUS "Skipping version-string generation (cannot find git)") + endif() +endif() + configure_file ( "${PROJECT_SOURCE_DIR}/config/versiondef.h.in" "${PROJECT_BINARY_DIR}/config/auto/versiondef.h" diff --git a/scripts/update_version_stamp.lua b/scripts/update_version_stamp.lua new file mode 100644 index 0000000000..f01642043a --- /dev/null +++ b/scripts/update_version_stamp.lua @@ -0,0 +1,45 @@ +#!/usr/bin/env lua +-- +-- Script to update the Git version stamp during build. +-- This is called via the custom update_version_stamp target in +-- src/nvim/CMakeLists.txt. +-- +-- arg[1]: file containing the last git-describe output +-- arg[2]: file in which to update the version string + +local function die(msg) + print(string.format('%s: %s', arg[0], msg)) + -- No error, fall back to using generated "-dev" version. + os.exit(0) +end + +if #arg ~= 2 then + die(string.format("Expected two args, got %d", #arg)) +end + +local stampfile = arg[1] +local stamp = io.open(stampfile, 'r') +if stamp then + stamp = stamp:read('*l') +end + +local current = io.popen('git describe --dirty'):read('*l') +if not current then + die('git-describe failed') +end + +if stamp ~= current then + if stamp then + print(string.format('git version changed: %s -> %s', stamp, current)) + end + local new_lines = {} + local versiondeffile = arg[2] + for line in io.lines(versiondeffile) do + if line:match("NVIM_VERSION_MEDIUM") then + line = '#define NVIM_VERSION_MEDIUM "'..current..'"' + end + new_lines[#new_lines + 1] = line + end + io.open(versiondeffile, 'w'):write(table.concat(new_lines, '\n') .. '\n') + io.open(stampfile, 'w'):write(current) +end From b0f5441c5eb2e3f4a3b847c48583e43425cd97e2 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 30 Sep 2019 12:50:22 +0200 Subject: [PATCH 0089/1293] bundle: upgrade LuaJIT to latest v2.1 (#10321) --- third-party/CMakeLists.txt | 6 +++--- third-party/cmake/BuildLuajit.cmake | 15 +++++++++++---- third-party/cmake/BuildLuarocks.cmake | 4 ++-- third-party/cmake/BuildLuv.cmake | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index 83692ff587..bf709c9311 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -146,9 +146,9 @@ endif() set(MSGPACK_URL https://github.com/msgpack/msgpack-c/releases/download/cpp-3.0.0/msgpack-3.0.0.tar.gz) set(MSGPACK_SHA256 bfbb71b7c02f806393bc3cbc491b40523b89e64f83860c58e3e54af47de176e4) -# https://github.com/LuaJIT/LuaJIT/tree/v2.0 -set(LUAJIT_URL https://github.com/LuaJIT/LuaJIT/archive/61464b0a5b685489bee7b6680c0e9663f2143a84.tar.gz) -set(LUAJIT_SHA256 033fa4ef19f559ef18a9b9fb017d0cb8be58befe05ab604e92814234910f1c68) +# https://github.com/LuaJIT/LuaJIT/tree/v2.1 +set(LUAJIT_URL https://github.com/LuaJIT/LuaJIT/archive/f0e865dd4861520258299d0f2a56491bd9d602e1.tar.gz) +set(LUAJIT_SHA256 ad5077bd861241bf5e50ae4bf543d291c5fcffab95ccc3218401131f503e45bd) set(LUA_URL https://www.lua.org/ftp/lua-5.1.5.tar.gz) set(LUA_SHA256 2640fc56a795f29d28ef15e13c34a47e223960b0240e8cb0a82d9b0738695333) diff --git a/third-party/cmake/BuildLuajit.cmake b/third-party/cmake/BuildLuajit.cmake index 791da4b7df..458cfeafda 100644 --- a/third-party/cmake/BuildLuajit.cmake +++ b/third-party/cmake/BuildLuajit.cmake @@ -33,6 +33,13 @@ function(BuildLuajit) BUILD_IN_SOURCE 1 BUILD_COMMAND "${_luajit_BUILD_COMMAND}" INSTALL_COMMAND "${_luajit_INSTALL_COMMAND}") + + # Create symlink for development version manually. + if(UNIX) + add_custom_command( + TARGET ${_luajit_TARGET} + COMMAND ${CMAKE_COMMAND} -E create_symlink luajit-2.1.0-beta3 ${DEPS_BIN_DIR}/luajit) + endif() endfunction() if(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") @@ -107,8 +114,8 @@ elseif(MINGW) # Luarocks searches for lua51.dll in lib COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.dll ${DEPS_INSTALL_DIR}/lib COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/libluajit.a ${DEPS_INSTALL_DIR}/lib - COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.0 - COMMAND ${CMAKE_COMMAND} -DFROM_GLOB=${DEPS_BUILD_DIR}/src/luajit/src/*.h -DTO=${DEPS_INSTALL_DIR}/include/luajit-2.0 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyFilesGlob.cmake + COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.1 + COMMAND ${CMAKE_COMMAND} -DFROM_GLOB=${DEPS_BUILD_DIR}/src/luajit/src/*.h -DTO=${DEPS_INSTALL_DIR}/include/luajit-2.1 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyFilesGlob.cmake ) elseif(MSVC) @@ -122,8 +129,8 @@ elseif(MSVC) COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.lib ${DEPS_INSTALL_DIR}/lib/lua51.lib # Luv searches for luajit.lib COMMAND ${CMAKE_COMMAND} -E copy ${DEPS_BUILD_DIR}/src/luajit/src/lua51.lib ${DEPS_INSTALL_DIR}/lib/luajit.lib - COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.0 - COMMAND ${CMAKE_COMMAND} -DFROM_GLOB=${DEPS_BUILD_DIR}/src/luajit/src/*.h -DTO=${DEPS_INSTALL_DIR}/include/luajit-2.0 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyFilesGlob.cmake) + COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL_DIR}/include/luajit-2.1 + COMMAND ${CMAKE_COMMAND} -DFROM_GLOB=${DEPS_BUILD_DIR}/src/luajit/src/*.h -DTO=${DEPS_INSTALL_DIR}/include/luajit-2.1 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CopyFilesGlob.cmake) else() message(FATAL_ERROR "Trying to build luajit in an unsupported system ${CMAKE_SYSTEM_NAME}/${CMAKE_C_COMPILER_ID}") diff --git a/third-party/cmake/BuildLuarocks.cmake b/third-party/cmake/BuildLuarocks.cmake index 87e2946c96..7551c52ecc 100644 --- a/third-party/cmake/BuildLuarocks.cmake +++ b/third-party/cmake/BuildLuarocks.cmake @@ -57,7 +57,7 @@ if(UNIX OR (MINGW AND CMAKE_CROSSCOMPILING)) if(USE_BUNDLED_LUAJIT) list(APPEND LUAROCKS_OPTS --with-lua=${HOSTDEPS_INSTALL_DIR} - --with-lua-include=${HOSTDEPS_INSTALL_DIR}/include/luajit-2.0 + --with-lua-include=${HOSTDEPS_INSTALL_DIR}/include/luajit-2.1 --lua-suffix=jit) elseif(USE_BUNDLED_LUA) list(APPEND LUAROCKS_OPTS @@ -89,7 +89,7 @@ elseif(MSVC OR MINGW) /LUA ${DEPS_INSTALL_DIR} /LIB ${DEPS_LIB_DIR} /BIN ${DEPS_BIN_DIR} - /INC ${DEPS_INSTALL_DIR}/include/luajit-2.0 + /INC ${DEPS_INSTALL_DIR}/include/luajit-2.1 /P ${DEPS_INSTALL_DIR}/luarocks /TREE ${DEPS_INSTALL_DIR} /SCRIPTS ${DEPS_BIN_DIR} /CMOD ${DEPS_BIN_DIR} diff --git a/third-party/cmake/BuildLuv.cmake b/third-party/cmake/BuildLuv.cmake index 967e0a1711..c2a2bbf083 100644 --- a/third-party/cmake/BuildLuv.cmake +++ b/third-party/cmake/BuildLuv.cmake @@ -55,7 +55,7 @@ endfunction() set(LUV_SRC_DIR ${DEPS_BUILD_DIR}/src/luv) set(LUV_INCLUDE_FLAGS - "-I${DEPS_INSTALL_DIR}/include -I${DEPS_INSTALL_DIR}/include/luajit-2.0") + "-I${DEPS_INSTALL_DIR}/include -I${DEPS_INSTALL_DIR}/include/luajit-2.1") # Replace luv default rockspec with the alternate one under the "rockspecs" # directory From 179c46a016388c2acead17e56d5860e667748561 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 30 Sep 2019 12:52:04 +0200 Subject: [PATCH 0090/1293] provider#pythonx: resolve/expand exe from host var (#11047) This reverts part of ade88fe4c [1]. This is required for `let g:python3_host_prog = 'python'` etc, where it should get picked up from PATH. Without this it would show: ``` - INFO: pyenv: Path: /home/user/.pyenv/libexec/pyenv - INFO: pyenv: Root: /home/user/.pyenv - INFO: Using: g:python3_host_prog = "python" - ERROR: "python" was not found. - INFO: Executable: Not found - ERROR: Detected pip upgrade failure: Python executable can import "pynvim" but not "neovim": python - ADVICE: - Use that Python version to reinstall "pynvim" and optionally "neovim". pip3 uninstall pynvim neovim pip3 install pynvim pip3 install neovim # only if needed by third-party software ``` Note that it additionally causes a weird error ("Detected pip upgrade failure"), due to `s:check_bin` emptying `python_exe` (because the non-absolute file not being readable), and `provider#pythonx#DetectByModule('pynvim', a:version)` from 75593e6fce then just getting the value from the host var again (without actual checks). This is implicitly fixed via this patch now (because it is skipped), but could need some improvement in this regard probably. With this patch it resolves it (for a virtualenv where pynvim is not made available intentionally): ``` - INFO: pyenv: Path: /home/daniel/.pyenv/libexec/pyenv - INFO: pyenv: Root: /home/daniel/.pyenv - INFO: Using: g:python3_host_prog = "python" - WARNING: $VIRTUAL_ENV exists but appears to be inactive. This could lead to unexpected results. - ADVICE: - If you are using Zsh, see: http://vi.stackexchange.com/a/7654 - INFO: Executable: /home/daniel/.pyenv/shims/tmp-system-deoplete.nvim-f205aF/python - ERROR: Command error (job=11, exit code 1): `'/home/daniel/.pyenv/shims/tmp-system-deoplete.nvim-f205aF/python' -c 'import sys; sys.path.remove(""); import neovim; print(neovim.__file__)'` (in '/home/daniel/.dotfiles/vim/plugged/deoplete.nvim') Output: Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'neovim' Stderr: Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'neovim' - INFO: Python version: 3.7.4 - INFO: pynvim version: unable to load neovim Python module - ERROR: pynvim is not installed. Error: unable to load neovim Python module - ADVICE: - Run in shell: pip3 install pynvim ``` Note: this appears to display the error twice via "Output:" and "Stderr:". 1: https://github.com/neovim/neovim/pull/8784 --- runtime/autoload/health/provider.vim | 2 +- runtime/autoload/provider/pythonx.vim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/autoload/health/provider.vim b/runtime/autoload/health/provider.vim index f52c2c2cbf..61858193c3 100644 --- a/runtime/autoload/health/provider.vim +++ b/runtime/autoload/health/provider.vim @@ -298,7 +298,7 @@ function! s:check_python(version) abort let python_exe = pyname endif - " No Python executable could `import neovim`. + " No Python executable could `import neovim`, or host_prog_var was used. if !empty(pythonx_errors) call health#report_error('Python provider error:', pythonx_errors) diff --git a/runtime/autoload/provider/pythonx.vim b/runtime/autoload/provider/pythonx.vim index 59b1c27b72..6ce7165467 100644 --- a/runtime/autoload/provider/pythonx.vim +++ b/runtime/autoload/provider/pythonx.vim @@ -43,7 +43,7 @@ function! provider#pythonx#DetectByModule(module, major_version) abort let python_exe = s:get_python_executable_from_host_var(a:major_version) if !empty(python_exe) - return [python_exe, ''] + return [exepath(expand(python_exe)), ''] endif let candidates = s:get_python_candidates(a:major_version) From dc52458522f23a0d50c7852af7191f7a92c263cb Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 30 Sep 2019 12:55:47 +0200 Subject: [PATCH 0091/1293] third-party: upgrade libluv: 1.30.0-0 => 1.30.1-1 (#11092) Changes: https://github.com/luvit/luv/compare/1.30.0-0...1.30.1-1 --- third-party/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index bf709c9311..2dbb444aa5 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -165,9 +165,9 @@ set(LIBTERMKEY_SHA256 cecbf737f35d18f433c8d7864f63c0f878af41f8bd0255a3ebb16010dc set(LIBVTERM_URL https://github.com/neovim/libvterm/archive/4a5fa43e0dbc0db4fe67d40d788d60852864df9e.tar.gz) set(LIBVTERM_SHA256 49b3cf2dcb988b887671b1011cfeac98ff81bb5c23fb4ac34b91a59524992935) -set(LUV_VERSION 1.30.0-0) +set(LUV_VERSION 1.30.1-1) set(LUV_URL https://github.com/luvit/luv/archive/${LUV_VERSION}.tar.gz) -set(LUV_SHA256 6e468fa17bf222ca8ce0bfffdbdf947fc897da48643a12955db92f80e2c852f5) +set(LUV_SHA256 2b17921e2e18094df6221e3cd291c82d4569e50d8c081518d3e775dceae267cf) set(LUA_COMPAT53_URL https://github.com/keplerproject/lua-compat-5.3/archive/v0.7.tar.gz) set(LUA_COMPAT53_SHA256 bec3a23114a3d9b3218038309657f0f506ad10dfbc03bb54e91da7e5ffdba0a2) From dd26bd59745c9fd358624312feb315ec0f106de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sun, 1 Sep 2019 11:13:13 +0200 Subject: [PATCH 0092/1293] screen: don't crash on invalid grid cells being recomposed --- runtime/doc/options.txt | 8 ++++++++ src/nvim/option_defs.h | 3 ++- src/nvim/ui_compositor.c | 14 ++++++++++++++ test/functional/helpers.lua | 2 +- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index d87898bb89..6b9f0380f6 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -4583,6 +4583,14 @@ A jump table for the options with a short description can be found at |Q_op|. RedrawDebugRecompose guibg=Red redraw generated by the compositor itself, due to a grid being moved or deleted. + nothrottle Turn off throttling of the message grid. This is an + optimization that joins many small scrolls to one + larger scroll when drawing the message area (with + 'display' msgsep flag active). + invalid Enable stricter checking (abort) of inconsistencies + of the internal screen state. This is mosly + useful when running nvim inside a debugger (and + the test suite). *'redrawtime'* *'rdt'* 'redrawtime' 'rdt' number (default 2000) diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index 08df495250..108a3dde7c 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -518,10 +518,11 @@ EXTERN long p_pyx; // 'pyxversion' EXTERN char_u *p_rdb; // 'redrawdebug' EXTERN unsigned rdb_flags; # ifdef IN_OPTION_C -static char *(p_rdb_values[]) = { "compositor", "nothrottle", NULL }; +static char *(p_rdb_values[]) = { "compositor", "nothrottle", "invalid", NULL }; # endif # define RDB_COMPOSITOR 0x001 # define RDB_NOTHROTTLE 0x002 +# define RDB_INVALID 0x004 EXTERN long p_rdt; // 'redrawtime' EXTERN int p_remap; // 'remap' diff --git a/src/nvim/ui_compositor.c b/src/nvim/ui_compositor.c index 20ffc1b88e..7d3ecfa0b8 100644 --- a/src/nvim/ui_compositor.c +++ b/src/nvim/ui_compositor.c @@ -425,6 +425,15 @@ static void compose_line(Integer row, Integer startcol, Integer endcol, flags = flags & ~kLineFlagWrap; } + for (int i = skipstart; i < (endcol-skipend)-startcol; i++) { + if (attrbuf[i] < 0) { + if (rdb_flags & RDB_INVALID) { + abort(); + } else { + attrbuf[i] = 0; + } + } + } ui_composed_call_raw_line(1, row, startcol+skipstart, endcol-skipend, endcol-skipend, 0, flags, (const schar_T *)linebuf+skipstart, @@ -535,6 +544,11 @@ static void ui_comp_raw_line(UI *ui, Integer grid, Integer row, } else { compose_debug(row, row+1, startcol, endcol, dbghl_normal, false); compose_debug(row, row+1, endcol, clearcol, dbghl_clear, true); +#ifndef NDEBUG + for (int i = 0; i < endcol-startcol; i++) { + assert(attrs[i] >= 0); + } +#endif ui_composed_call_raw_line(1, row, startcol, endcol, clearcol, clearattr, flags, chunk, attrs); } diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index b29161e34c..bd36bac062 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -38,7 +38,7 @@ module.nvim_prog = ( module.nvim_set = ( 'set shortmess+=IS background=light noswapfile noautoindent' ..' laststatus=1 undodir=. directory=. viewdir=. backupdir=.' - ..' belloff= wildoptions-=pum noshowcmd noruler nomore') + ..' belloff= wildoptions-=pum noshowcmd noruler nomore redrawdebug=invalid') module.nvim_argv = { module.nvim_prog, '-u', 'NONE', '-i', 'NONE', '--cmd', module.nvim_set, '--embed'} From 8a4ae3d664a22cfaa3ec05635d26a8d662458c7e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 30 Sep 2019 22:00:55 +0200 Subject: [PATCH 0093/1293] tui: improve handle_background_color: short-circuit (#11067) * handle_background_color: short-circuit if handled already * Unit tests for handle_background_color * set waiting_for_bg_response to false in tui_terminal_after_startup By then it should have been received. --- src/nvim/tui/input.c | 11 ++ src/nvim/tui/input.h | 5 + src/nvim/tui/tui.c | 6 ++ test/functional/terminal/tui_spec.lua | 74 +------------- test/unit/tui_spec.lua | 139 ++++++++++++++++++++++++++ 5 files changed, 165 insertions(+), 70 deletions(-) create mode 100644 test/unit/tui_spec.lua diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c index ed9b410a19..876f00e03e 100644 --- a/src/nvim/tui/input.c +++ b/src/nvim/tui/input.c @@ -27,6 +27,7 @@ void tinput_init(TermInput *input, Loop *loop) input->loop = loop; input->paste = 0; input->in_fd = 0; + input->waiting_for_bg_response = false; input->key_buffer = rbuffer_new(KEY_BUFFER_SIZE); uv_mutex_init(&input->key_buffer_mutex); uv_cond_init(&input->key_buffer_cond); @@ -443,6 +444,9 @@ static void set_bg_deferred(void **argv) // [1] https://en.wikipedia.org/wiki/Luma_%28video%29 static bool handle_background_color(TermInput *input) { + if (!input->waiting_for_bg_response) { + return false; + } size_t count = 0; size_t component = 0; size_t header_size = 0; @@ -463,6 +467,7 @@ static bool handle_background_color(TermInput *input) } else { return false; } + input->waiting_for_bg_response = false; rbuffer_consumed(input->read_stream.buffer, header_size); RBUFFER_EACH(input->read_stream.buffer, c, i) { count = i + 1; @@ -503,6 +508,12 @@ static bool handle_background_color(TermInput *input) } return true; } +#ifdef UNIT_TESTING +bool ut_handle_background_color(TermInput *input) +{ + return handle_background_color(input); +} +#endif static void tinput_read_cb(Stream *stream, RBuffer *buf, size_t count_, void *data, bool eof) diff --git a/src/nvim/tui/input.h b/src/nvim/tui/input.h index a4071fab40..49ae32f00e 100644 --- a/src/nvim/tui/input.h +++ b/src/nvim/tui/input.h @@ -12,6 +12,7 @@ typedef struct term_input { // Phases: -1=all 0=disabled 1=first-chunk 2=continue 3=last-chunk int8_t paste; bool waiting; + bool waiting_for_bg_response; TermKey *tk; #if TERMKEY_VERSION_MAJOR > 0 || TERMKEY_VERSION_MINOR > 18 TermKey_Terminfo_Getstr_Hook *tk_ti_hook_fn; ///< libtermkey terminfo hook @@ -28,4 +29,8 @@ typedef struct term_input { # include "tui/input.h.generated.h" #endif +#ifdef UNIT_TESTING +bool ut_handle_background_color(TermInput *input); +#endif + #endif // NVIM_TUI_INPUT_H diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index 0c282a3f1f..4ca44b25f0 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -296,6 +296,7 @@ static void terminfo_start(UI *ui) unibi_out(ui, unibi_keypad_xmit); unibi_out(ui, unibi_clear_screen); // Ask the terminal to send us the background color. + data->input.waiting_for_bg_response = true; unibi_out_ext(ui, data->unibi_ext.get_bg); // Enable bracketed paste unibi_out_ext(ui, data->unibi_ext.enable_bracketed_paste); @@ -365,6 +366,11 @@ static void tui_terminal_after_startup(UI *ui) // 2.3 bug(?) which caused slow drawing during startup. #7649 unibi_out_ext(ui, data->unibi_ext.enable_focus_reporting); flush_buf(ui); + + if (data->input.waiting_for_bg_response) { + DLOG("did not get a response for terminal background query"); + data->input.waiting_for_bg_response = false; + } } static void tui_terminal_stop(UI *ui) diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index 89468359ef..ed904f27ca 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -1376,7 +1376,6 @@ describe('TUI background color', function() end) it("handles deferred background color", function() - local last_bg = 'dark' local function wait_for_bg(bg) -- Retry until the terminal response is handled. retry(100, nil, function() @@ -1394,76 +1393,11 @@ describe('TUI background color', function() ]], bg) }) end) - last_bg = bg end - local function assert_bg(colorspace, color, bg) - -- Ensure the opposite of the expected bg is active. - local other_bg = (bg == 'dark' and 'light' or 'dark') - if last_bg ~= other_bg then - feed_data(other_bg == 'light' and '\027]11;rgb:f/f/f\007' - or '\027]11;rgb:0/0/0\007') - wait_for_bg(other_bg) - end - - feed_data('\027]11;'..colorspace..':'..color..'\007') - wait_for_bg(bg) - end - - assert_bg('rgb', '0000/0000/0000', 'dark') - assert_bg('rgb', 'ffff/ffff/ffff', 'light') - assert_bg('rgb', '000/000/000', 'dark') - assert_bg('rgb', 'fff/fff/fff', 'light') - assert_bg('rgb', '00/00/00', 'dark') - assert_bg('rgb', 'ff/ff/ff', 'light') - assert_bg('rgb', '0/0/0', 'dark') - assert_bg('rgb', 'f/f/f', 'light') - - assert_bg('rgb', 'f/0/0', 'dark') - assert_bg('rgb', '0/f/0', 'light') - assert_bg('rgb', '0/0/f', 'dark') - - assert_bg('rgb', '1/1/1', 'dark') - assert_bg('rgb', '2/2/2', 'dark') - assert_bg('rgb', '3/3/3', 'dark') - assert_bg('rgb', '4/4/4', 'dark') - assert_bg('rgb', '5/5/5', 'dark') - assert_bg('rgb', '6/6/6', 'dark') - assert_bg('rgb', '7/7/7', 'dark') - assert_bg('rgb', '8/8/8', 'light') - assert_bg('rgb', '9/9/9', 'light') - assert_bg('rgb', 'a/a/a', 'light') - assert_bg('rgb', 'b/b/b', 'light') - assert_bg('rgb', 'c/c/c', 'light') - assert_bg('rgb', 'd/d/d', 'light') - assert_bg('rgb', 'e/e/e', 'light') - - assert_bg('rgb', '0/e/0', 'light') - assert_bg('rgb', '0/d/0', 'light') - assert_bg('rgb', '0/c/0', 'dark') - assert_bg('rgb', '0/b/0', 'dark') - - assert_bg('rgb', 'f/0/f', 'dark') - assert_bg('rgb', 'f/1/f', 'dark') - assert_bg('rgb', 'f/2/f', 'dark') - assert_bg('rgb', 'f/3/f', 'light') - assert_bg('rgb', 'f/4/f', 'light') - - assert_bg('rgba', '0000/0000/0000/0000', 'dark') - assert_bg('rgba', '0000/0000/0000/ffff', 'dark') - assert_bg('rgba', 'ffff/ffff/ffff/0000', 'light') - assert_bg('rgba', 'ffff/ffff/ffff/ffff', 'light') - assert_bg('rgba', '000/000/000/000', 'dark') - assert_bg('rgba', '000/000/000/fff', 'dark') - assert_bg('rgba', 'fff/fff/fff/000', 'light') - assert_bg('rgba', 'fff/fff/fff/fff', 'light') - assert_bg('rgba', '00/00/00/00', 'dark') - assert_bg('rgba', '00/00/00/ff', 'dark') - assert_bg('rgba', 'ff/ff/ff/00', 'light') - assert_bg('rgba', 'ff/ff/ff/ff', 'light') - assert_bg('rgba', '0/0/0/0', 'dark') - assert_bg('rgba', '0/0/0/f', 'dark') - assert_bg('rgba', 'f/f/f/0', 'light') - assert_bg('rgba', 'f/f/f/f', 'light') + -- Only single integration test. + -- See test/unit/tui_spec.lua for unit tests. + feed_data('\027]11;rgb:ffff/ffff/ffff\007') + wait_for_bg('light') end) end) diff --git a/test/unit/tui_spec.lua b/test/unit/tui_spec.lua new file mode 100644 index 0000000000..47dbd87b71 --- /dev/null +++ b/test/unit/tui_spec.lua @@ -0,0 +1,139 @@ +local helpers = require("test.unit.helpers")(after_each) +local cimport = helpers.cimport +local eq = helpers.eq +local ffi = helpers.ffi +local itp = helpers.gen_itp(it) +local to_cstr = helpers.to_cstr + +local cinput = cimport("./src/nvim/tui/input.h") +local rbuffer = cimport("./test/unit/fixtures/rbuffer.h") +local globals = cimport("./src/nvim/globals.h") +local multiqueue = cimport("./test/unit/fixtures/multiqueue.h") + +itp('handle_background_color', function() + local handle_background_color = cinput.ut_handle_background_color + local term_input = ffi.new('TermInput', {}) + local events = globals.main_loop.thread_events + + -- Short-circuit when not waiting for response. + term_input.waiting_for_bg_response = false + eq(false, handle_background_color(term_input)) + + local capacity = 100 + local rbuf = ffi.gc(rbuffer.rbuffer_new(capacity), rbuffer.rbuffer_free) + term_input.read_stream.buffer = rbuf + + local function assert_bg(colorspace, color, bg) + local term_response = '\027]11;'..colorspace..':'..color..'\007' + rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response) + + term_input.waiting_for_bg_response = true + eq(true, handle_background_color(term_input)) + eq(false, term_input.waiting_for_bg_response) + eq(1, multiqueue.multiqueue_size(events)) + + local event = multiqueue.multiqueue_get(events) + local bg_event = ffi.cast("Event*", event.argv[1]) + eq(bg, ffi.string(bg_event.argv[0])) + + -- Buffer has been consumed. + eq(0, rbuf.size) + end + + assert_bg('rgb', '0000/0000/0000', 'dark') + assert_bg('rgb', 'ffff/ffff/ffff', 'light') + assert_bg('rgb', '000/000/000', 'dark') + assert_bg('rgb', 'fff/fff/fff', 'light') + assert_bg('rgb', '00/00/00', 'dark') + assert_bg('rgb', 'ff/ff/ff', 'light') + assert_bg('rgb', '0/0/0', 'dark') + assert_bg('rgb', 'f/f/f', 'light') + + assert_bg('rgb', 'f/0/0', 'dark') + assert_bg('rgb', '0/f/0', 'light') + assert_bg('rgb', '0/0/f', 'dark') + + assert_bg('rgb', '1/1/1', 'dark') + assert_bg('rgb', '2/2/2', 'dark') + assert_bg('rgb', '3/3/3', 'dark') + assert_bg('rgb', '4/4/4', 'dark') + assert_bg('rgb', '5/5/5', 'dark') + assert_bg('rgb', '6/6/6', 'dark') + assert_bg('rgb', '7/7/7', 'dark') + assert_bg('rgb', '8/8/8', 'light') + assert_bg('rgb', '9/9/9', 'light') + assert_bg('rgb', 'a/a/a', 'light') + assert_bg('rgb', 'b/b/b', 'light') + assert_bg('rgb', 'c/c/c', 'light') + assert_bg('rgb', 'd/d/d', 'light') + assert_bg('rgb', 'e/e/e', 'light') + + assert_bg('rgb', '0/e/0', 'light') + assert_bg('rgb', '0/d/0', 'light') + assert_bg('rgb', '0/c/0', 'dark') + assert_bg('rgb', '0/b/0', 'dark') + + assert_bg('rgb', 'f/0/f', 'dark') + assert_bg('rgb', 'f/1/f', 'dark') + assert_bg('rgb', 'f/2/f', 'dark') + assert_bg('rgb', 'f/3/f', 'light') + assert_bg('rgb', 'f/4/f', 'light') + + assert_bg('rgba', '0000/0000/0000/0000', 'dark') + assert_bg('rgba', '0000/0000/0000/ffff', 'dark') + assert_bg('rgba', 'ffff/ffff/ffff/0000', 'light') + assert_bg('rgba', 'ffff/ffff/ffff/ffff', 'light') + assert_bg('rgba', '000/000/000/000', 'dark') + assert_bg('rgba', '000/000/000/fff', 'dark') + assert_bg('rgba', 'fff/fff/fff/000', 'light') + assert_bg('rgba', 'fff/fff/fff/fff', 'light') + assert_bg('rgba', '00/00/00/00', 'dark') + assert_bg('rgba', '00/00/00/ff', 'dark') + assert_bg('rgba', 'ff/ff/ff/00', 'light') + assert_bg('rgba', 'ff/ff/ff/ff', 'light') + assert_bg('rgba', '0/0/0/0', 'dark') + assert_bg('rgba', '0/0/0/f', 'dark') + assert_bg('rgba', 'f/f/f/0', 'light') + assert_bg('rgba', 'f/f/f/f', 'light') + + + -- Incomplete sequence: not necessarily correct behavior, but tests it. + local term_response = '\027]11;rgba:f/f/f/f' -- missing '\007 + rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response) + + term_input.waiting_for_bg_response = true + eq(true, term_input.waiting_for_bg_response) + eq(false, handle_background_color(term_input)) + eq(false, term_input.waiting_for_bg_response) + + eq(0, multiqueue.multiqueue_size(events)) + eq(0, rbuf.size) + + + -- Does nothing when not at start of buffer. + term_response = '123\027]11;rgba:f/f/f/f\007456' + rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response) + + term_input.waiting_for_bg_response = true + eq(true, term_input.waiting_for_bg_response) + eq(false, handle_background_color(term_input)) + eq(true, term_input.waiting_for_bg_response) + + eq(0, multiqueue.multiqueue_size(events)) + eq(#term_response, rbuf.size) + rbuffer.rbuffer_consumed(rbuf, #term_response) + + + -- Keeps trailing buffer. + term_response = '\027]11;rgba:f/f/f/f\007456' + rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response) + + term_input.waiting_for_bg_response = true + eq(true, term_input.waiting_for_bg_response) + eq(true, handle_background_color(term_input)) + eq(false, term_input.waiting_for_bg_response) + + eq(1, multiqueue.multiqueue_size(events)) + eq(3, rbuf.size) + rbuffer.rbuffer_consumed(rbuf, rbuf.size) +end) From ac32426b94deabb6843fab797957f0064a54c5a5 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 2 Oct 2019 03:44:57 +0200 Subject: [PATCH 0094/1293] build: get rid of warnings with `cmake --debug-output` (#11131) --- CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 83fd67837d..1b6ba45820 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,13 @@ cmake_minimum_required(VERSION 2.8.12) project(nvim C) +if(POLICY CMP0065) + cmake_policy(SET CMP0065 NEW) +endif() +if(POLICY CMP0060) + cmake_policy(SET CMP0060 NEW) +endif() + # Point CMake at any custom modules we may ship list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") From 30ae60e7cac7e77005aa429bc13f8ffa3ce64eb1 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 2 Oct 2019 03:45:59 +0200 Subject: [PATCH 0095/1293] Fix/revisit git-describe enhancement (#11124) * Fix/keep massaging git-describe result Ref: https://github.com/neovim/neovim/pull/11117#issuecomment-536416223 * build: revisit generation of version from Git Fixes "make clean && make", where "auto/versiondef.h" would be missing since b18b84d - because BYPRODUCTS are apparently removed when cleaning. This includes the following improvements/changes: - do not run git-describe during CMake's configure phase just for reporting - do not print with changed Git version (too noisy, simplifies code) * Move to src/nvim (included before config) for easier flow * fallback to describe always, write empty include file * update_version_stamp.lua: use prefix always --- config/CMakeLists.txt | 29 ----------------- config/versiondef.h.in | 4 +++ scripts/update_version_stamp.lua | 53 +++++++++++++++++--------------- src/nvim/CMakeLists.txt | 36 ++++++++++++++++++++-- 4 files changed, 67 insertions(+), 55 deletions(-) mode change 100644 => 100755 scripts/update_version_stamp.lua diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt index 7bd48a1f1e..0ca41d5dfd 100644 --- a/config/CMakeLists.txt +++ b/config/CMakeLists.txt @@ -121,35 +121,6 @@ configure_file ( ) # generate version definitions -if(NVIM_VERSION_MEDIUM) - message(STATUS "NVIM_VERSION_MEDIUM: ${NVIM_VERSION_MEDIUM}") -elseif(${CMAKE_VERSION} VERSION_LESS "3.2.0") - message(STATUS "Skipping version-string generation (requires CMake 3.2.0+)") -elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git) - find_program(GIT_EXECUTABLE git) - if(GIT_EXECUTABLE) - # Get current version. - execute_process( - COMMAND ${GIT_EXECUTABLE} describe --dirty - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_VARIABLE NVIM_VERSION_MEDIUM - OUTPUT_STRIP_TRAILING_WHITESPACE) - message(STATUS "NVIM_VERSION_MEDIUM (from git): ${NVIM_VERSION_MEDIUM}") - - # Create a update_version_stamp target to update the version during build. - file(RELATIVE_PATH relbuild "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}") - add_custom_target(update_version_stamp ALL - COMMAND ${LUA_PRG} scripts/update_version_stamp.lua - ${relbuild}/.version_stamp - ${relbuild}/config/auto/versiondef.h - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - BYPRODUCTS ${CMAKE_BINARY_DIR}/config/auto/versiondef.h) - add_dependencies(nvim update_version_stamp) - else() - message(STATUS "Skipping version-string generation (cannot find git)") - endif() -endif() - configure_file ( "${PROJECT_SOURCE_DIR}/config/versiondef.h.in" "${PROJECT_BINARY_DIR}/config/auto/versiondef.h" diff --git a/config/versiondef.h.in b/config/versiondef.h.in index b9565735b3..22cad87249 100644 --- a/config/versiondef.h.in +++ b/config/versiondef.h.in @@ -5,7 +5,11 @@ #define NVIM_VERSION_MINOR @NVIM_VERSION_MINOR@ #define NVIM_VERSION_PATCH @NVIM_VERSION_PATCH@ #define NVIM_VERSION_PRERELEASE "@NVIM_VERSION_PRERELEASE@" + #cmakedefine NVIM_VERSION_MEDIUM "@NVIM_VERSION_MEDIUM@" +#ifndef NVIM_VERSION_MEDIUM +# include "auto/versiondef_git.h" +#endif #define NVIM_API_LEVEL @NVIM_API_LEVEL@ #define NVIM_API_LEVEL_COMPAT @NVIM_API_LEVEL_COMPAT@ diff --git a/scripts/update_version_stamp.lua b/scripts/update_version_stamp.lua old mode 100644 new mode 100755 index f01642043a..509e1f6fad --- a/scripts/update_version_stamp.lua +++ b/scripts/update_version_stamp.lua @@ -4,11 +4,11 @@ -- This is called via the custom update_version_stamp target in -- src/nvim/CMakeLists.txt. -- --- arg[1]: file containing the last git-describe output --- arg[2]: file in which to update the version string +-- arg[1]: file in which to update the version string +-- arg[2]: prefix to use always ("vX.Y.Z") local function die(msg) - print(string.format('%s: %s', arg[0], msg)) + io.stderr:write(string.format('%s: %s\n', arg[0], msg)) -- No error, fall back to using generated "-dev" version. os.exit(0) end @@ -17,29 +17,34 @@ if #arg ~= 2 then die(string.format("Expected two args, got %d", #arg)) end -local stampfile = arg[1] -local stamp = io.open(stampfile, 'r') -if stamp then - stamp = stamp:read('*l') +local versiondeffile = arg[1] +local prefix = arg[2] + +local described = io.popen('git describe --dirty'):read('*l') +if not described then + described = io.popen('git describe --tags --always --dirty'):read('*l') +end +if not described then + io.open(versiondeffile, 'w'):write('\n') + die('git-describe failed, using empty include file.') end -local current = io.popen('git describe --dirty'):read('*l') -if not current then - die('git-describe failed') +-- `git describe` annotates the most recent tagged release; for pre-release +-- builds we must replace that with the unreleased version. +local with_prefix = described:gsub("^v%d+%.%d+%.%d+", prefix) +if described == with_prefix then + -- Prepend the prefix always, e.g. with "nightly-12208-g4041b62b9". + with_prefix = prefix .. "-" .. described end -if stamp ~= current then - if stamp then - print(string.format('git version changed: %s -> %s', stamp, current)) - end - local new_lines = {} - local versiondeffile = arg[2] - for line in io.lines(versiondeffile) do - if line:match("NVIM_VERSION_MEDIUM") then - line = '#define NVIM_VERSION_MEDIUM "'..current..'"' - end - new_lines[#new_lines + 1] = line - end - io.open(versiondeffile, 'w'):write(table.concat(new_lines, '\n') .. '\n') - io.open(stampfile, 'w'):write(current) +-- Read existing include file. +local current = io.open(versiondeffile, 'r') +if current then + current = current:read('*l') +end + +-- Write new include file, if different. +local new = '#define NVIM_VERSION_MEDIUM "'..with_prefix..'"' +if current ~= new then + io.open(versiondeffile, 'w'):write(new .. '\n') end diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 27977e3a40..a64944ab0d 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -217,6 +217,34 @@ function(get_preproc_output varname iname) endif() endfunction() +# Handle generating version from Git. +set(use_git_version 0) +if(NVIM_VERSION_MEDIUM) + message(STATUS "NVIM_VERSION_MEDIUM: ${NVIM_VERSION_MEDIUM}") +elseif(${CMAKE_VERSION} VERSION_LESS "3.2.0") + message(STATUS "Skipping version-string generation (requires CMake 3.2.0+)") +elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git) + find_program(GIT_EXECUTABLE git) + if(GIT_EXECUTABLE) + message(STATUS "Using NVIM_VERSION_MEDIUM from Git") + set(use_git_version 1) + else() + message(STATUS "Skipping version-string generation (cannot find git)") + endif() +endif() +if(use_git_version) + # Create a update_version_stamp target to update the version during build. + file(RELATIVE_PATH relbuild "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}") + add_custom_target(update_version_stamp ALL + COMMAND ${LUA_PRG} scripts/update_version_stamp.lua + ${relbuild}/config/auto/versiondef_git.h + "v${NVIM_VERSION_MAJOR}.${NVIM_VERSION_MINOR}.${NVIM_VERSION_PATCH}" + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + BYPRODUCTS ${CMAKE_BINARY_DIR}/config/auto/versiondef_git.h) +else() + file(WRITE ${CMAKE_BINARY_DIR}/config/auto/versiondef_git.h "") +endif() + # NVIM_GENERATED_FOR_HEADERS: generated headers to be included in headers # NVIM_GENERATED_FOR_SOURCES: generated headers to be included in sources # NVIM_GENERATED_SOURCES: generated source files @@ -245,12 +273,16 @@ foreach(sfile ${NVIM_SOURCES} get_preproc_output(PREPROC_OUTPUT ${gf_i}) + set(depends "${HEADER_GENERATOR}" "${sfile}") + if(use_git_version AND "${f}" STREQUAL "version.c") + # Ensure auto/versiondef_git.h exists after "make clean". + list(APPEND depends update_version_stamp) + endif() add_custom_command( OUTPUT "${gf_c_h}" "${gf_h_h}" COMMAND ${CMAKE_C_COMPILER} ${sfile} ${PREPROC_OUTPUT} ${gen_cflags} ${C_FLAGS_ARRAY} COMMAND "${LUA_PRG}" "${HEADER_GENERATOR}" "${sfile}" "${gf_c_h}" "${gf_h_h}" "${gf_i}" - DEPENDS "${HEADER_GENERATOR}" "${sfile}" - ) + DEPENDS ${depends}) list(APPEND NVIM_GENERATED_FOR_SOURCES "${gf_c_h}") list(APPEND NVIM_GENERATED_FOR_HEADERS "${gf_h_h}") if(${d} MATCHES "^api$" AND NOT ${f} MATCHES "^api/helpers.c$") From b7d6caaa036c3d1be716bb6e4b0f56c08fb8dcf5 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 2 Oct 2019 03:51:46 +0200 Subject: [PATCH 0096/1293] Fix redraw regression with w_p_cole in visual mode Fixes https://github.com/neovim/neovim/issues/11024, regressed in 23c71d51. Closes https://github.com/neovim/neovim/pull/11120. --- src/nvim/screen.c | 5 ++- test/functional/ui/syntax_conceal_spec.lua | 46 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 17a91f69d5..187c89b28c 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -578,10 +578,13 @@ void conceal_check_cursor_line(void) /// /// If true, both old and new cursorline will need /// need to be redrawn when moving cursor within windows. +/// TODO(bfredl): VIsual_active shouldn't be needed, but is used to fix a glitch +/// caused by scrolling. bool win_cursorline_standout(const win_T *wp) FUNC_ATTR_NONNULL_ALL { - return wp->w_p_cul || (wp->w_p_cole > 0 && !conceal_cursor_line(wp)); + return wp->w_p_cul + || (wp->w_p_cole > 0 && (VIsual_active || !conceal_cursor_line(wp))); } /* diff --git a/test/functional/ui/syntax_conceal_spec.lua b/test/functional/ui/syntax_conceal_spec.lua index 00e94ef94b..7079b43414 100644 --- a/test/functional/ui/syntax_conceal_spec.lua +++ b/test/functional/ui/syntax_conceal_spec.lua @@ -17,6 +17,7 @@ describe('Screen', function() [3] = {reverse = true}, [4] = {bold = true}, [5] = {background = Screen.colors.Yellow}, + [6] = {background = Screen.colors.LightGrey}, } ) end) @@ -823,5 +824,50 @@ describe('Screen', function() ]]) end) end) + + it('redraws properly with concealcursor in visual mode', function() + command('set concealcursor=v conceallevel=2') + + feed('10Ofoo barf bar barf eggs') + feed(':3o aggV') + screen:expect{grid=[[ + ^f{6:oo }{1:b}{6: bar }{1:b}{6: eggs} | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + a | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + {4:-- VISUAL LINE --} | + ]]} + feed(string.rep('j', 15)) + screen:expect{grid=[[ + {6:foo }{1:b}{6: bar }{1:b}{6: eggs} | + {6:foo }{1:b}{6: bar }{1:b}{6: eggs} | + {6:foo }{1:b}{6: bar }{1:b}{6: eggs} | + {6:foo }{1:b}{6: bar }{1:b}{6: eggs} | + {6:foo }{1:b}{6: bar }{1:b}{6: eggs} | + {6:foo }{1:b}{6: bar }{1:b}{6: eggs} | + {6:foo }{1:b}{6: bar }{1:b}{6: eggs} | + {6:foo }{1:b}{6: bar }{1:b}{6: eggs} | + ^f{6:oo }{1:b}{6: bar }{1:b}{6: eggs} | + {4:-- VISUAL LINE --} | + ]]} + feed(string.rep('k', 15)) + screen:expect{grid=[[ + ^f{6:oo }{1:b}{6: bar }{1:b}{6: eggs} | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + a | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + foo {1:b} bar {1:b} eggs | + {4:-- VISUAL LINE --} | + ]]} + end) end) end) From 8e25cf3881bbc3d65645d1b2abc6fa46863b1765 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 2 Oct 2019 04:07:10 +0200 Subject: [PATCH 0097/1293] patch_terminfo_bugs: TERM=xterm with non-xterm: ignore smglr (#11132) "smglr" was added for TERM=xterm recently to the terminfo database, which causes display issues with terminals that use `TERM=xterm` by default for themselves, although not supporting it. This patch makes "smglr" to be ignored then. Fixes https://github.com/neovim/neovim/issues/10562 --- src/nvim/tui/tui.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index 4ca44b25f0..956d4eb9da 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -93,7 +93,7 @@ typedef struct { int out_fd; bool scroll_region_is_full_screen; bool can_change_scroll_region; - bool can_set_lr_margin; + bool can_set_lr_margin; // smglr bool can_set_left_right_margin; bool can_scroll; bool can_erase_chars; @@ -1603,6 +1603,12 @@ static void patch_terminfo_bugs(TUIData *data, const char *term, unibi_set_if_empty(ut, unibi_set_lr_margin, "\x1b[%i%p1%d;%p2%ds"); unibi_set_if_empty(ut, unibi_set_left_margin_parm, "\x1b[%i%p1%ds"); unibi_set_if_empty(ut, unibi_set_right_margin_parm, "\x1b[%i;%p2%ds"); + } else { + // Fix things advertised via TERM=xterm, for non-xterm. + if (unibi_get_str(ut, unibi_set_lr_margin)) { + ILOG("Disabling smglr with TERM=xterm for non-xterm."); + unibi_set_str(ut, unibi_set_lr_margin, NULL); + } } #ifdef WIN32 From 0253f0cd929a59e1516359eab9ae84ce39643a7b Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 24 Sep 2019 04:22:54 -0400 Subject: [PATCH 0098/1293] vim-patch:8.1.0010: efm_to_regpat() is too long Problem: efm_to_regpat() is too long. Solution: Split off three functions. (Yegappan Lakshmanan, closes vim/vim#2924) https://github.com/vim/vim/commit/6bff719f7e472e918c60aa336de03e799b806c4f --- src/nvim/quickfix.c | 239 +++++++++++++++++++++++++++----------------- 1 file changed, 147 insertions(+), 92 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 528829e63d..5083e573d3 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -264,15 +264,152 @@ static struct fmtpattern { 'o', ".\\+" } }; +// Convert an errorformat pattern to a regular expression pattern. +// See fmt_pat definition above for the list of supported patterns. +static char_u *fmtpat_to_regpat( + const char_u *efmp, + efm_T *fmt_ptr, + int idx, + int round, + char_u *ptr, + char_u *errmsg, + size_t errmsglen) + FUNC_ATTR_NONNULL_ALL +{ + if (fmt_ptr->addr[idx]) { + // Each errorformat pattern can occur only once + snprintf((char *)errmsg, errmsglen, + _("E372: Too many %%%c in format string"), *efmp); + EMSG(errmsg); + return NULL; + } + if ((idx && idx < 6 + && vim_strchr((char_u *)"DXOPQ", fmt_ptr->prefix) != NULL) + || (idx == 6 + && vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)) { + snprintf((char *)errmsg, errmsglen, + _("E373: Unexpected %%%c in format string"), *efmp); + EMSG(errmsg); + return NULL; + } + fmt_ptr->addr[idx] = (char_u)++round; + *ptr++ = '\\'; + *ptr++ = '('; +#ifdef BACKSLASH_IN_FILENAME + if (*efmp == 'f') { + // Also match "c:" in the file name, even when + // checking for a colon next: "%f:". + // "\%(\a:\)\=" + STRCPY(ptr, "\\%(\\a:\\)\\="); + ptr += 10; + } +#endif + if (*efmp == 'f' && efmp[1] != NUL) { + if (efmp[1] != '\\' && efmp[1] != '%') { + // A file name may contain spaces, but this isn't + // in "\f". For "%f:%l:%m" there may be a ":" in + // the file name. Use ".\{-1,}x" instead (x is + // the next character), the requirement that :999: + // follows should work. + STRCPY(ptr, ".\\{-1,}"); + ptr += 7; + } else { + // File name followed by '\\' or '%': include as + // many file name chars as possible. + STRCPY(ptr, "\\f\\+"); + ptr += 4; + } + } else { + char_u *srcptr = (char_u *)fmt_pat[idx].pattern; + while ((*ptr = *srcptr++) != NUL) { + ptr++; + } + } + *ptr++ = '\\'; + *ptr++ = ')'; + + return ptr; +} + +// Convert a scanf like format in 'errorformat' to a regular expression. +static char_u *scanf_fmt_to_regpat( + const char_u *efm, + int len, + const char_u **pefmp, + char_u *ptr, + char_u *errmsg, + size_t errmsglen) + FUNC_ATTR_NONNULL_ALL +{ + const char_u *efmp = *pefmp; + + if (*++efmp == '[' || *efmp == '\\') { + if ((*ptr++ = *efmp) == '[') { // %*[^a-z0-9] etc. + if (efmp[1] == '^') { + *ptr++ = *++efmp; + } + if (efmp < efm + len) { + *ptr++ = *++efmp; // could be ']' + while (efmp < efm + len && (*ptr++ = *++efmp) != ']') { + } + if (efmp == efm + len) { + EMSG(_("E374: Missing ] in format string")); + return NULL; + } + } + } else if (efmp < efm + len) { // %*\D, %*\s etc. + *ptr++ = *++efmp; + } + *ptr++ = '\\'; + *ptr++ = '+'; + } else { + // TODO(vim): scanf()-like: %*ud, %*3c, %*f, ... ? + snprintf((char *)errmsg, errmsglen, + _("E375: Unsupported %%%c in format string"), *efmp); + EMSG(errmsg); + return NULL; + } + + *pefmp = efmp; + + return ptr; +} + +// Analyze/parse an errorformat prefix. +static int efm_analyze_prefix(const char_u **pefmp, efm_T *fmt_ptr, + char_u *errmsg, size_t errmsglen) + FUNC_ATTR_NONNULL_ALL +{ + const char_u *efmp = *pefmp; + + if (vim_strchr((char_u *)"+-", *efmp) != NULL) { + fmt_ptr->flags = *efmp++; + } + if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL) { + fmt_ptr->prefix = *efmp; + } else { + snprintf((char *)errmsg, errmsglen, + _("E376: Invalid %%%c in format string prefix"), *efmp); + EMSG(errmsg); + return FAIL; + } + + *pefmp = efmp; + + return OK; +} + + // Converts a 'errorformat' string to regular expression pattern -static int efm_to_regpat(char_u *efm, int len, efm_T *fmt_ptr, - char_u *regpat, char_u *errmsg) +static int efm_to_regpat(const char_u *efm, int len, efm_T *fmt_ptr, + char_u *regpat, char_u *errmsg, size_t errmsglen) + FUNC_ATTR_NONNULL_ALL { // Build regexp pattern from current 'errorformat' option char_u *ptr = regpat; *ptr++ = '^'; int round = 0; - for (char_u *efmp = efm; efmp < efm + len; efmp++) { + for (const char_u *efmp = efm; efmp < efm + len; efmp++) { if (*efmp == '%') { efmp++; int idx; @@ -282,89 +419,15 @@ static int efm_to_regpat(char_u *efm, int len, efm_T *fmt_ptr, } } if (idx < FMT_PATTERNS) { - if (fmt_ptr->addr[idx]) { - snprintf((char *)errmsg, CMDBUFFSIZE + 1, - _("E372: Too many %%%c in format string"), *efmp); - EMSG(errmsg); - return -1; - } - if ((idx - && idx < 6 - && vim_strchr((char_u *)"DXOPQ", fmt_ptr->prefix) != NULL) - || (idx == 6 - && vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)) { - snprintf((char *)errmsg, CMDBUFFSIZE + 1, - _("E373: Unexpected %%%c in format string"), *efmp); - EMSG(errmsg); + ptr = fmtpat_to_regpat(efmp, fmt_ptr, idx, round, ptr, + errmsg, errmsglen); + if (ptr == NULL) { return -1; } round++; - fmt_ptr->addr[idx] = (char_u)round; - *ptr++ = '\\'; - *ptr++ = '('; -#ifdef BACKSLASH_IN_FILENAME - if (*efmp == 'f') { - // Also match "c:" in the file name, even when - // checking for a colon next: "%f:". - // "\%(\a:\)\=" - STRCPY(ptr, "\\%(\\a:\\)\\="); - ptr += 10; - } -#endif - if (*efmp == 'f' && efmp[1] != NUL) { - if (efmp[1] != '\\' && efmp[1] != '%') { - // A file name may contain spaces, but this isn't - // in "\f". For "%f:%l:%m" there may be a ":" in - // the file name. Use ".\{-1,}x" instead (x is - // the next character), the requirement that :999: - // follows should work. - STRCPY(ptr, ".\\{-1,}"); - ptr += 7; - } else { - // File name followed by '\\' or '%': include as - // many file name chars as possible. - STRCPY(ptr, "\\f\\+"); - ptr += 4; - } - } else { - char_u *srcptr = (char_u *)fmt_pat[idx].pattern; - while ((*ptr = *srcptr++) != NUL) { - ptr++; - } - } - *ptr++ = '\\'; - *ptr++ = ')'; } else if (*efmp == '*') { - if (*++efmp == '[' || *efmp == '\\') { - if ((*ptr++ = *efmp) == '[') { // %*[^a-z0-9] etc. - if (efmp[1] == '^') { - *ptr++ = *++efmp; - } - if (efmp < efm + len) { - efmp++; - *ptr++ = *efmp; // could be ']' - while (efmp < efm + len) { - efmp++; - if ((*ptr++ = *efmp) == ']') { - break; - } - } - if (efmp == efm + len) { - EMSG(_("E374: Missing ] in format string")); - return -1; - } - } - } else if (efmp < efm + len) { // %*\D, %*\s etc. - efmp++; - *ptr++ = *efmp; - } - *ptr++ = '\\'; - *ptr++ = '+'; - } else { - // TODO(vim): scanf()-like: %*ud, %*3c, %*f, ... ? - snprintf((char *)errmsg, CMDBUFFSIZE + 1, - _("E375: Unsupported %%%c in format string"), *efmp); - EMSG(errmsg); + ptr = scanf_fmt_to_regpat(efm, len, &efmp, ptr, errmsg, errmsglen); + if (ptr == NULL) { return -1; } } else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL) { @@ -374,15 +437,7 @@ static int efm_to_regpat(char_u *efm, int len, efm_T *fmt_ptr, } else if (*efmp == '>') { fmt_ptr->conthere = true; } else if (efmp == efm + 1) { // analyse prefix - if (vim_strchr((char_u *)"+-", *efmp) != NULL) { - fmt_ptr->flags = *efmp++; - } - if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL) { - fmt_ptr->prefix = *efmp; - } else { - snprintf((char *)errmsg, CMDBUFFSIZE + 1, - _("E376: Invalid %%%c in format string prefix"), *efmp); - EMSG(errmsg); + if (efm_analyze_prefix(&efmp, fmt_ptr, errmsg, errmsglen) == FAIL) { return -1; } } else { @@ -461,7 +516,7 @@ static efm_T * parse_efm_option(char_u *efm) } } - if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1) { + if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg, errmsglen) == -1) { goto parse_efm_error; } if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL) { From 24c4d4e1258f8ca34eb581550776ef613c27a689 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 24 Sep 2019 20:01:58 -0400 Subject: [PATCH 0099/1293] vim-patch:8.1.2072: "gk" moves to start of line instead of upwards Problem: "gk" moves to start of line instead of upwards. Solution: Fix off-by-one error. (Christian Brabandt, closes vim/vim#4969) https://github.com/vim/vim/commit/03ac52fc025790c474030ea556cec799400aa046 --- src/nvim/normal.c | 8 ++++---- src/nvim/testdir/test_normal.vim | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 4dfde96e94..d4065cc06e 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -3932,11 +3932,11 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) while (dist--) { if (dir == BACKWARD) { - if ((long)curwin->w_curswant >= width2) - /* move back within line */ + if (curwin->w_curswant > width2) { + // move back within line curwin->w_curswant -= width2; - else { - /* to previous line */ + } else { + // to previous line if (curwin->w_cursor.lnum == 1) { retval = false; break; diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index 532beb9c39..5ff2cf66c9 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -2617,3 +2617,25 @@ Piece of Java close! endfunc + +func Test_normal_gk() + " needs 80 column new window + new + vert 80new + put =[repeat('x',90)..' {{{1', 'x {{{1'] + norm! gk + " In a 80 column wide terminal the window will be only 78 char + " (because Vim will leave space for the other window), + " but if the terminal is larger, it will be 80 chars, so verify the + " cursor column correctly. + call assert_equal(winwidth(0)+1, col('.')) + call assert_equal(winwidth(0)+1, virtcol('.')) + norm! j + call assert_equal(6, col('.')) + call assert_equal(6, virtcol('.')) + norm! gk + call assert_equal(95, col('.')) + call assert_equal(95, virtcol('.')) + bw! + bw! +endfunc From 8d0bc3c18964db51fec4b204a122e946393f2d6d Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 24 Sep 2019 20:11:30 -0400 Subject: [PATCH 0100/1293] vim-patch:8.1.1758: count of g$ not used correctly when text is not wrapped Problem: Count of g$ not used correctly when text is not wrapped. Solution: Do use the count. (Christian Brabandt, closes vim/vim#4729, closes vim/vim#4566) https://github.com/vim/vim/commit/d5c8234517c18fa059b78f59eb96c35eda323dae --- src/nvim/normal.c | 6 +++- src/nvim/testdir/test_normal.vim | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/nvim/normal.c b/src/nvim/normal.c index d4065cc06e..6b76082772 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -6795,10 +6795,14 @@ static void nv_g_cmd(cmdarg_T *cap) } else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == false) clearopbeep(oap); } else { + if (cap->count1 > 1) { + // if it fails, let the cursor still move to the last char + cursor_down(cap->count1 - 1, false); + } i = curwin->w_leftcol + curwin->w_width_inner - col_off - 1; coladvance((colnr_T)i); - /* Make sure we stick in this column. */ + // Make sure we stick in this column. validate_virtcol(); curwin->w_curswant = curwin->w_virtcol; curwin->w_set_curswant = false; diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index 5ff2cf66c9..0c71e2af4c 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -2618,6 +2618,61 @@ Piece of Java close! endfunc +fun! Test_normal_gdollar_cmd() + if !has("jumplist") + return + endif + " Tests for g cmds + call Setup_NewWindow() + " Make long lines that will wrap + %s/$/\=repeat(' foobar', 10)/ + 20vsp + set wrap + " Test for g$ with count + norm! gg + norm! 0vg$y + call assert_equal(20, col("'>")) + call assert_equal('1 foobar foobar foob', getreg(0)) + norm! gg + norm! 0v4g$y + call assert_equal(72, col("'>")) + call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.."\n", getreg(0)) + norm! gg + norm! 0v6g$y + call assert_equal(40, col("'>")) + call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '2 foobar foobar foobar foobar foobar foo', getreg(0)) + set nowrap + " clean up + norm! gg + norm! 0vg$y + call assert_equal(20, col("'>")) + call assert_equal('1 foobar foobar foob', getreg(0)) + norm! gg + norm! 0v4g$y + call assert_equal(20, col("'>")) + call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '4 foobar foobar foob', getreg(0)) + norm! gg + norm! 0v6g$y + call assert_equal(20, col("'>")) + call assert_equal('1 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '2 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '3 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '4 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '5 foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar'.. "\n".. + \ '6 foobar foobar foob', getreg(0)) + " Move to last line, also down movement is not possible, should still move + " the cursor to the last visible char + norm! G + norm! 0v6g$y + call assert_equal(20, col("'>")) + call assert_equal('100 foobar foobar fo', getreg(0)) + bw! +endfunc + func Test_normal_gk() " needs 80 column new window new From 17e96d96bac4ea9074a337b263fe85d4755106b6 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 24 Sep 2019 22:53:20 -0400 Subject: [PATCH 0101/1293] vim-patch:8.1.0514: CTRL-W ^ does not work when alternate buffer has no name Problem: CTRL-W ^ does not work when alternate buffer has no name. Solution: Use another method to split and edit the alternate buffer. (Jason Franklin) https://github.com/vim/vim/commit/1bbb61948342b5cf6e363629f145c65eb455c388 --- runtime/doc/windows.txt | 8 +- src/nvim/normal.c | 5 +- src/nvim/testdir/test_normal.vim | 163 ++++++++++++++++----------- src/nvim/testdir/test_window_cmd.vim | 60 +++++++++- src/nvim/window.c | 16 ++- 5 files changed, 176 insertions(+), 76 deletions(-) diff --git a/runtime/doc/windows.txt b/runtime/doc/windows.txt index 76bb096ee3..977e0daef7 100644 --- a/runtime/doc/windows.txt +++ b/runtime/doc/windows.txt @@ -201,9 +201,11 @@ CTRL-W CTRL_N *CTRL-W_CTRL-N* |:find|. Doesn't split if {file} is not found. CTRL-W CTRL-^ *CTRL-W_CTRL-^* *CTRL-W_^* -CTRL-W ^ Does ":split #", split window in two and edit alternate file. - When a count is given, it becomes ":split #N", split window - and edit buffer N. +CTRL-W ^ Split the current window in two and edit the alternate file. + When a count N is given, split the current window and edit + buffer N. Similar to ":sp #" and ":sp #N", but it allows the + other buffer to be unnamed. This command matches the behavior + of |CTRL-^|, except that it splits a window first. CTRL-W ge *CTRL-W_ge* Detach the current window as an external window. diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 6b76082772..e32b738c7e 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -4680,9 +4680,8 @@ static void nv_ctrlo(cmdarg_T *cap) } } -/* - * CTRL-^ command, short for ":e #" - */ +// CTRL-^ command, short for ":e #". Works even when the alternate buffer is +// not named. static void nv_hat(cmdarg_T *cap) { if (!checkclearopq(cap->oap)) diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index 0c71e2af4c..b3e43640bb 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -2,12 +2,12 @@ source shared.vim -func! Setup_NewWindow() +func Setup_NewWindow() 10new call setline(1, range(1,100)) endfunc -func! MyFormatExpr() +func MyFormatExpr() " Adds '->$' at lines having numbers followed by trailing whitespace for ln in range(v:lnum, v:lnum+v:count-1) let line = getline(ln) @@ -17,7 +17,7 @@ func! MyFormatExpr() endfor endfunc -func! CountSpaces(type, ...) +func CountSpaces(type, ...) " for testing operatorfunc " will count the number of spaces " and return the result in g:a @@ -37,7 +37,7 @@ func! CountSpaces(type, ...) let @@ = reg_save endfunc -func! OpfuncDummy(type, ...) +func OpfuncDummy(type, ...) " for testing operatorfunc let g:opt=&linebreak @@ -81,7 +81,7 @@ fun! Test_normal00_optrans() bw! endfunc -func! Test_normal01_keymodel() +func Test_normal01_keymodel() call Setup_NewWindow() " Test 1: depending on 'keymodel' does something different 50 @@ -115,7 +115,7 @@ func! Test_normal01_keymodel() bw! endfunc -func! Test_normal02_selectmode() +func Test_normal02_selectmode() " some basic select mode tests call Setup_NewWindow() 50 @@ -129,7 +129,7 @@ func! Test_normal02_selectmode() bw! endfunc -func! Test_normal02_selectmode2() +func Test_normal02_selectmode2() " some basic select mode tests call Setup_NewWindow() 50 @@ -139,7 +139,7 @@ func! Test_normal02_selectmode2() bw! endfunc -func! Test_normal03_join() +func Test_normal03_join() " basic join test call Setup_NewWindow() 50 @@ -159,7 +159,7 @@ func! Test_normal03_join() bw! endfunc -func! Test_normal04_filter() +func Test_normal04_filter() " basic filter test " only test on non windows platform if has('win32') @@ -185,7 +185,7 @@ func! Test_normal04_filter() bw! endfunc -func! Test_normal05_formatexpr() +func Test_normal05_formatexpr() " basic formatexpr test call Setup_NewWindow() %d_ @@ -222,7 +222,7 @@ func Test_normal05_formatexpr_setopt() set formatexpr= endfunc -func! Test_normal06_formatprg() +func Test_normal06_formatprg() " basic test for formatprg " only test on non windows platform if has('win32') @@ -256,7 +256,7 @@ func! Test_normal06_formatprg() call delete('Xsed_format.sh') endfunc -func! Test_normal07_internalfmt() +func Test_normal07_internalfmt() " basic test for internal formmatter to textwidth of 12 let list=range(1,11) call map(list, 'v:val." "') @@ -270,7 +270,7 @@ func! Test_normal07_internalfmt() bw! endfunc -func! Test_normal08_fold() +func Test_normal08_fold() " basic tests for foldopen/folddelete if !has("folding") return @@ -329,7 +329,7 @@ func! Test_normal08_fold() bw! endfunc -func! Test_normal09_operatorfunc() +func Test_normal09_operatorfunc() " Test operatorfunc call Setup_NewWindow() " Add some spaces for counting @@ -359,7 +359,7 @@ func! Test_normal09_operatorfunc() bw! endfunc -func! Test_normal09a_operatorfunc() +func Test_normal09a_operatorfunc() " Test operatorfunc call Setup_NewWindow() " Add some spaces for counting @@ -385,7 +385,7 @@ func! Test_normal09a_operatorfunc() unlet! g:opt endfunc -func! Test_normal10_expand() +func Test_normal10_expand() " Test for expand() 10new call setline(1, ['1', 'ifooar,,cbar']) @@ -420,7 +420,7 @@ func! Test_normal10_expand() bw! endfunc -func! Test_normal11_showcmd() +func Test_normal11_showcmd() " test for 'showcmd' 10new exe "norm! ofoobar\" @@ -435,7 +435,7 @@ func! Test_normal11_showcmd() bw! endfunc -func! Test_normal12_nv_error() +func Test_normal12_nv_error() " Test for nv_error 10new call setline(1, range(1,5)) @@ -445,7 +445,7 @@ func! Test_normal12_nv_error() bw! endfunc -func! Test_normal13_help() +func Test_normal13_help() " Test for F1 call assert_equal(1, winnr()) call feedkeys("\", 'txi') @@ -454,7 +454,7 @@ func! Test_normal13_help() bw! endfunc -func! Test_normal14_page() +func Test_normal14_page() " basic test for Ctrl-F and Ctrl-B call Setup_NewWindow() exe "norm! \" @@ -488,7 +488,7 @@ func! Test_normal14_page() bw! endfunc -func! Test_normal14_page_eol() +func Test_normal14_page_eol() 10new norm oxxxxxxx exe "norm 2\" @@ -497,7 +497,7 @@ func! Test_normal14_page_eol() bw! endfunc -func! Test_normal15_z_scroll_vert() +func Test_normal15_z_scroll_vert() " basic test for z commands that scroll the window call Setup_NewWindow() 100 @@ -586,7 +586,7 @@ func! Test_normal15_z_scroll_vert() bw! endfunc -func! Test_normal16_z_scroll_hor() +func Test_normal16_z_scroll_hor() " basic test for z commands that scroll the window 10new 15vsp @@ -652,7 +652,7 @@ func! Test_normal16_z_scroll_hor() bw! endfunc -func! Test_normal17_z_scroll_hor2() +func Test_normal17_z_scroll_hor2() " basic test for z commands that scroll the window " using 'sidescrolloff' setting 10new @@ -719,7 +719,7 @@ func! Test_normal17_z_scroll_hor2() bw! endfunc -func! Test_normal18_z_fold() +func Test_normal18_z_fold() " basic tests for foldopen/folddelete if !has("folding") return @@ -1090,7 +1090,7 @@ func! Test_normal18_z_fold() bw! endfunc -func! Test_normal19_z_spell() +func Test_normal19_z_spell() if !has("spell") || !has('syntax') return endif @@ -1245,7 +1245,7 @@ func! Test_normal19_z_spell() bw! endfunc -func! Test_normal20_exmode() +func Test_normal20_exmode() if !has("unix") " Reading from redirected file doesn't work on MS-Windows return @@ -1263,24 +1263,38 @@ func! Test_normal20_exmode() bw! endfunc -func! Test_normal21_nv_hat() - set hidden - new - " to many buffers opened already, will not work - "call assert_fails(":b#", 'E23') - "call assert_equal('', @#) - e Xfoobar - e Xfile2 - call feedkeys("\", 't') - call assert_equal("Xfile2", fnamemodify(bufname('%'), ':t')) - call feedkeys("f\", 't') - call assert_equal("Xfile2", fnamemodify(bufname('%'), ':t')) - " clean up - set nohidden - bw! +func Test_normal21_nv_hat() + + " Edit a fresh file and wipe the buffer list so that there is no alternate + " file present. Next, check for the expected command failures. + edit Xfoo | %bw + call assert_fails(':buffer #', 'E86') + call assert_fails(':execute "normal! \"', 'E23') + + " Test for the expected behavior when switching between two named buffers. + edit Xfoo | edit Xbar + call feedkeys("\", 'tx') + call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t')) + call feedkeys("\", 'tx') + call assert_equal('Xbar', fnamemodify(bufname('%'), ':t')) + + " Test for the expected behavior when only one buffer is named. + enew | let l:nr = bufnr('%') + call feedkeys("\", 'tx') + call assert_equal('Xbar', fnamemodify(bufname('%'), ':t')) + call feedkeys("\", 'tx') + call assert_equal('', bufname('%')) + call assert_equal(l:nr, bufnr('%')) + + " Test that no action is taken by "" when an operator is pending. + edit Xfoo + call feedkeys("ci\", 'tx') + call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t')) + + %bw! endfunc -func! Test_normal22_zet() +func Test_normal22_zet() " Test for ZZ " let shell = &shell " let &shell = 'sh' @@ -1308,7 +1322,7 @@ func! Test_normal22_zet() " let &shell = shell endfunc -func! Test_normal23_K() +func Test_normal23_K() " Test for K command new call append(0, ['helphelp.txt', 'man', 'aa%bb', 'cc|dd']) @@ -1373,7 +1387,7 @@ func! Test_normal23_K() bw! endfunc -func! Test_normal24_rot13() +func Test_normal24_rot13() " Testing for g?? g?g? new call append(0, 'abcdefghijklmnopqrstuvwxyzäüö') @@ -1387,7 +1401,7 @@ func! Test_normal24_rot13() bw! endfunc -func! Test_normal25_tag() +func Test_normal25_tag() " Testing for CTRL-] g CTRL-] g] " CTRL-W g] CTRL-W CTRL-] CTRL-W g CTRL-] h @@ -1454,7 +1468,7 @@ func! Test_normal25_tag() helpclose endfunc -func! Test_normal26_put() +func Test_normal26_put() " Test for ]p ]P [p and [P new call append(0, ['while read LINE', 'do', ' ((count++))', ' if [ $? -ne 0 ]; then', " echo 'Error writing file'", ' fi', 'done']) @@ -1473,7 +1487,7 @@ func! Test_normal26_put() bw! endfunc -func! Test_normal27_bracket() +func Test_normal27_bracket() " Test for [' [` ]' ]` call Setup_NewWindow() 1,21s/.\+/ & b/ @@ -1524,7 +1538,7 @@ func! Test_normal27_bracket() bw! endfunc -func! Test_normal28_parenthesis() +func Test_normal28_parenthesis() " basic testing for ( and ) new call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here']) @@ -1718,7 +1732,7 @@ fun! Test_normal31_r_cmd() bw! endfunc -func! Test_normal32_g_cmd1() +func Test_normal32_g_cmd1() " Test for g*, g# new call append(0, ['abc.x_foo', 'x_foobar.abc']) @@ -1849,7 +1863,7 @@ fun! Test_normal33_g_cmd2() bw! endfunc -func! Test_g_ctrl_g() +func Test_g_ctrl_g() new let a = execute(":norm! g\") @@ -2139,7 +2153,7 @@ fun! Test_normal41_insert_reg() bw! endfunc -func! Test_normal42_halfpage() +func Test_normal42_halfpage() " basic test for Ctrl-D and Ctrl-U call Setup_NewWindow() call assert_equal(5, &scroll) @@ -2207,7 +2221,7 @@ fun! Test_normal43_textobject1() bw! endfunc -func! Test_normal44_textobjects2() +func Test_normal44_textobjects2() " basic testing for is and as text objects new call append(0, ['This is a test. With some sentences!', '', 'Even with a question? And one more. And no sentence here']) @@ -2262,7 +2276,7 @@ func! Test_normal44_textobjects2() bw! endfunc -func! Test_normal45_drop() +func Test_normal45_drop() if !has('dnd') " The ~ register does not exist call assert_beeps('norm! "~') @@ -2280,7 +2294,7 @@ func! Test_normal45_drop() bw! endfunc -func! Test_normal46_ignore() +func Test_normal46_ignore() new " How to test this? " let's just for now test, that the buffer @@ -2299,7 +2313,7 @@ func! Test_normal46_ignore() bw! endfunc -func! Test_normal47_visual_buf_wipe() +func Test_normal47_visual_buf_wipe() " This was causing a crash or ml_get error. enew! call setline(1,'xxx') @@ -2313,7 +2327,7 @@ func! Test_normal47_visual_buf_wipe() set nomodified endfunc -func! Test_normal47_autocmd() +func Test_normal47_autocmd() " disabled, does not seem to be possible currently throw "Skipped: not possible to test cursorhold autocmd while waiting for input in normal_cmd" new @@ -2331,14 +2345,14 @@ func! Test_normal47_autocmd() bw! endfunc -func! Test_normal48_wincmd() +func Test_normal48_wincmd() new exe "norm! \c" call assert_equal(1, winnr('$')) call assert_fails(":norm! \c", "E444") endfunc -func! Test_normal49_counts() +func Test_normal49_counts() new call setline(1, 'one two three four five six seven eight nine ten') 1 @@ -2347,7 +2361,7 @@ func! Test_normal49_counts() bw! endfunc -func! Test_normal50_commandline() +func Test_normal50_commandline() if !has("timers") || !has("cmdline_hist") || !has("vertsplit") return endif @@ -2378,7 +2392,7 @@ func! Test_normal50_commandline() bw! endfunc -func! Test_normal51_FileChangedRO() +func Test_normal51_FileChangedRO() if !has("autocmd") return endif @@ -2395,7 +2409,7 @@ func! Test_normal51_FileChangedRO() call delete("Xreadonly.log") endfunc -func! Test_normal52_rl() +func Test_normal52_rl() if !has("rightleft") return endif @@ -2428,7 +2442,7 @@ func! Test_normal52_rl() bw! endfunc -func! Test_normal53_digraph() +func Test_normal53_digraph() if !has('digraphs') return endif @@ -2516,6 +2530,29 @@ func Test_changelist() let &ul = save_ul endfunc +func Test_nv_hat_count() + %bwipeout! + let l:nr = bufnr('%') + 1 + call assert_fails(':execute "normal! ' . l:nr . '\"', 'E92') + + edit Xfoo + let l:foo_nr = bufnr('Xfoo') + + edit Xbar + let l:bar_nr = bufnr('Xbar') + + " Make sure we are not just using the alternate file. + edit Xbaz + + call feedkeys(l:foo_nr . "\", 'tx') + call assert_equal('Xfoo', fnamemodify(bufname('%'), ':t')) + + call feedkeys(l:bar_nr . "\", 'tx') + call assert_equal('Xbar', fnamemodify(bufname('%'), ':t')) + + %bwipeout! +endfunc + func Test_delete_until_paragraph() new normal grádv} diff --git a/src/nvim/testdir/test_window_cmd.vim b/src/nvim/testdir/test_window_cmd.vim index c87c0a0af4..9f899fba04 100644 --- a/src/nvim/testdir/test_window_cmd.vim +++ b/src/nvim/testdir/test_window_cmd.vim @@ -117,15 +117,65 @@ func Test_window_vertical_split() bw endfunc +" Test the ":wincmd ^" and "^" commands. func Test_window_split_edit_alternate() - e Xa - e Xb + " Test for failure when the alternate buffer/file no longer exists. + edit Xfoo | %bw + call assert_fails(':wincmd ^', 'E23') + + " Test for the expected behavior when we have two named buffers. + edit Xfoo | edit Xbar wincmd ^ - call assert_equal('Xa', bufname(winbufnr(1))) - call assert_equal('Xb', bufname(winbufnr(2))) + call assert_equal('Xfoo', bufname(winbufnr(1))) + call assert_equal('Xbar', bufname(winbufnr(2))) + only - bw Xa Xb + " Test for the expected behavior when the alternate buffer is not named. + enew | let l:nr1 = bufnr('%') + edit Xfoo | let l:nr2 = bufnr('%') + wincmd ^ + call assert_equal(l:nr1, winbufnr(1)) + call assert_equal(l:nr2, winbufnr(2)) + only + + " Test the Normal mode command. + call feedkeys("\\", 'tx') + call assert_equal(l:nr2, winbufnr(1)) + call assert_equal(l:nr1, winbufnr(2)) + + %bw! +endfunc + +" Test the ":[count]wincmd ^" and "[count]^" commands. +func Test_window_split_edit_bufnr() + + %bwipeout + let l:nr = bufnr('%') + 1 + call assert_fails(':execute "normal! ' . l:nr . '\\"', 'E92') + call assert_fails(':' . l:nr . 'wincmd ^', 'E16') + call assert_fails(':0wincmd ^', 'E16') + + edit Xfoo | edit Xbar | edit Xbaz + let l:foo_nr = bufnr('Xfoo') + let l:bar_nr = bufnr('Xbar') + let l:baz_nr = bufnr('Xbaz') + + call feedkeys(l:foo_nr . "\\", 'tx') + call assert_equal('Xfoo', bufname(winbufnr(1))) + call assert_equal('Xbaz', bufname(winbufnr(2))) + only + + call feedkeys(l:bar_nr . "\\", 'tx') + call assert_equal('Xbar', bufname(winbufnr(1))) + call assert_equal('Xfoo', bufname(winbufnr(2))) + only + + execute l:baz_nr . 'wincmd ^' + call assert_equal('Xbaz', bufname(winbufnr(1))) + call assert_equal('Xbar', bufname(winbufnr(2))) + + %bw! endfunc func Test_window_preview() diff --git a/src/nvim/window.c b/src/nvim/window.c index 1e6de73549..d7df048588 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -131,8 +131,20 @@ do_window ( case '^': CHECK_CMDWIN; reset_VIsual_and_resel(); // stop Visual mode - cmd_with_count("split #", (char_u *)cbuf, sizeof(cbuf), Prenum); - do_cmdline_cmd(cbuf); + + if (buflist_findnr(Prenum == 0 ? curwin->w_alt_fnum : Prenum) == NULL) { + if (Prenum == 0) { + EMSG(_(e_noalt)); + } else { + EMSGN(_("E92: Buffer %" PRId64 " not found"), Prenum); + } + break; + } + + if (!curbuf_locked() && win_split(0, 0) == OK) { + (void)buflist_getfile(Prenum == 0 ? curwin->w_alt_fnum : Prenum, + (linenr_T)0, GETF_ALT, false); + } break; /* open new window */ From 14f3287b9813f5a3bb88596e859a2fb6c6b2e4ae Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 24 Sep 2019 23:28:30 -0400 Subject: [PATCH 0102/1293] vim-patch:8.1.0517: Test_window_split_edit_alternate() fails on AppVeyor Problem: Test_window_split_edit_alternate() fails on AppVeyor. Solution: Disable the failing part for now. https://github.com/vim/vim/commit/d42333d8e9f6c157884f4f1acb458aa992f94f3d --- src/nvim/testdir/test_window_cmd.vim | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/nvim/testdir/test_window_cmd.vim b/src/nvim/testdir/test_window_cmd.vim index 9f899fba04..4c93e990ee 100644 --- a/src/nvim/testdir/test_window_cmd.vim +++ b/src/nvim/testdir/test_window_cmd.vim @@ -139,10 +139,13 @@ func Test_window_split_edit_alternate() call assert_equal(l:nr2, winbufnr(2)) only - " Test the Normal mode command. - call feedkeys("\\", 'tx') - call assert_equal(l:nr2, winbufnr(1)) - call assert_equal(l:nr1, winbufnr(2)) + " FIXME: this currently fails on AppVeyor, but passes locally + if !has('win32') + " Test the Normal mode command. + call feedkeys("\\", 'tx') + call assert_equal(l:nr2, winbufnr(1)) + call assert_equal(l:nr1, winbufnr(2)) + endif %bw! endfunc From e8144d204c02be1fb4e248420610ec98074ae9f9 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 24 Sep 2019 23:30:23 -0400 Subject: [PATCH 0103/1293] vim-patch:8.1.0518: Test_window_split_edit_bufnr() fails on AppVeyor Problem: Test_window_split_edit_bufnr() fails on AppVeyor. Solution: Disable the failing part for now. https://github.com/vim/vim/commit/8617b401599451187fa0c0561a84944978536a90 --- src/nvim/testdir/test_window_cmd.vim | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/nvim/testdir/test_window_cmd.vim b/src/nvim/testdir/test_window_cmd.vim index 4c93e990ee..72f1baf39e 100644 --- a/src/nvim/testdir/test_window_cmd.vim +++ b/src/nvim/testdir/test_window_cmd.vim @@ -164,19 +164,22 @@ func Test_window_split_edit_bufnr() let l:bar_nr = bufnr('Xbar') let l:baz_nr = bufnr('Xbaz') - call feedkeys(l:foo_nr . "\\", 'tx') - call assert_equal('Xfoo', bufname(winbufnr(1))) - call assert_equal('Xbaz', bufname(winbufnr(2))) - only + " FIXME: this currently fails on AppVeyor, but passes locally + if !has('win32') + call feedkeys(l:foo_nr . "\\", 'tx') + call assert_equal('Xfoo', bufname(winbufnr(1))) + call assert_equal('Xbaz', bufname(winbufnr(2))) + only - call feedkeys(l:bar_nr . "\\", 'tx') - call assert_equal('Xbar', bufname(winbufnr(1))) - call assert_equal('Xfoo', bufname(winbufnr(2))) - only + call feedkeys(l:bar_nr . "\\", 'tx') + call assert_equal('Xbar', bufname(winbufnr(1))) + call assert_equal('Xfoo', bufname(winbufnr(2))) + only - execute l:baz_nr . 'wincmd ^' - call assert_equal('Xbaz', bufname(winbufnr(1))) - call assert_equal('Xbar', bufname(winbufnr(2))) + execute l:baz_nr . 'wincmd ^' + call assert_equal('Xbaz', bufname(winbufnr(1))) + call assert_equal('Xbar', bufname(winbufnr(2))) + endif %bw! endfunc From 6ed20ff25cd738ab2b9e79af8e3a9c37ba52dbcf Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 12 May 2019 09:08:31 -0400 Subject: [PATCH 0104/1293] vim-patch:8.1.1327: unnecessary scroll after horizontal split Problem: Unnecessary scroll after horizontal split. Solution: Don't adjust to fraction if all the text fits in the window. (Martin Kunev, closes vim/vim#4367) https://github.com/vim/vim/commit/a9b2535f44f3265940a18d08520a9ad4ef7bda82 --- src/nvim/testdir/test_window_cmd.vim | 36 ++++++++++++++++++++++++++++ src/nvim/window.c | 7 ++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/nvim/testdir/test_window_cmd.vim b/src/nvim/testdir/test_window_cmd.vim index 72f1baf39e..c41f4f9412 100644 --- a/src/nvim/testdir/test_window_cmd.vim +++ b/src/nvim/testdir/test_window_cmd.vim @@ -756,6 +756,42 @@ func Test_relative_cursor_second_line_after_resize() let &so = so_save endfunc +func Test_split_noscroll() + let so_save = &so + new + only + + " Make sure windows can hold all content after split. + for i in range(1, 20) + wincmd + + redraw! + endfor + + call setline (1, range(1, 8)) + normal 100% + split + + 1wincmd w + let winid1 = win_getid() + let info1 = getwininfo(winid1)[0] + + 2wincmd w + let winid2 = win_getid() + let info2 = getwininfo(winid2)[0] + + call assert_equal(1, info1.topline) + call assert_equal(1, info2.topline) + + " Restore original state. + for i in range(1, 20) + wincmd - + redraw! + endfor + only! + bwipe! + let &so = so_save +endfunc + " Tests for the winnr() function func Test_winnr() only | tabonly diff --git a/src/nvim/window.c b/src/nvim/window.c index d7df048588..1f23646bdf 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -5610,10 +5610,13 @@ void scroll_to_fraction(win_T *wp, int prev_height) int sline, line_size; int height = wp->w_height_inner; - // Don't change w_topline when height is zero. Don't set w_topline when - // 'scrollbind' is set and this isn't the current window. + // Don't change w_topline in any of these cases: + // - window height is 0 + // - 'scrollbind' is set and this isn't the current window + // - window height is sufficient to display the whole buffer if (height > 0 && (!wp->w_p_scb || wp == curwin) + && (height < wp->w_buffer->b_ml.ml_line_count) ) { /* * Find a value for w_topline that shows the cursor at the same From 90c2abc53faed9aab8ad71395068e7b09d6dea85 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 18 May 2019 18:54:25 -0400 Subject: [PATCH 0105/1293] vim-patch:8.1.1347: fractional scroll position not restored after closing window Problem: Fractional scroll position not restored after closing window. Solution: Do restore fraction if topline is not one. https://github.com/vim/vim/commit/bd2d68c2f42c7689f681aeaf82606d17f8a0312f --- src/nvim/testdir/test_window_cmd.vim | 30 ++++++++++++++-------------- src/nvim/window.c | 5 +++-- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/nvim/testdir/test_window_cmd.vim b/src/nvim/testdir/test_window_cmd.vim index c41f4f9412..43c1f06c44 100644 --- a/src/nvim/testdir/test_window_cmd.vim +++ b/src/nvim/testdir/test_window_cmd.vim @@ -758,16 +758,8 @@ endfunc func Test_split_noscroll() let so_save = &so - new - only - - " Make sure windows can hold all content after split. - for i in range(1, 20) - wincmd + - redraw! - endfor - - call setline (1, range(1, 8)) + enew + call setline(1, range(1, 8)) normal 100% split @@ -782,12 +774,20 @@ func Test_split_noscroll() call assert_equal(1, info1.topline) call assert_equal(1, info2.topline) - " Restore original state. - for i in range(1, 20) - wincmd - - redraw! - endfor + " window that fits all lines by itself, but not when split: closing other + " window should restore fraction. only! + call setline(1, range(1, &lines - 10)) + exe &lines / 4 + let winid1 = win_getid() + let info1 = getwininfo(winid1)[0] + call assert_equal(1, info1.topline) + new + redraw + close + let info1 = getwininfo(winid1)[0] + call assert_equal(1, info1.topline) + bwipe! let &so = so_save endfunc diff --git a/src/nvim/window.c b/src/nvim/window.c index 1f23646bdf..4d8eaa9dcc 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -5613,10 +5613,11 @@ void scroll_to_fraction(win_T *wp, int prev_height) // Don't change w_topline in any of these cases: // - window height is 0 // - 'scrollbind' is set and this isn't the current window - // - window height is sufficient to display the whole buffer + // - window height is sufficient to display the whole buffer and first line + // is visible. if (height > 0 && (!wp->w_p_scb || wp == curwin) - && (height < wp->w_buffer->b_ml.ml_line_count) + && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1) ) { /* * Find a value for w_topline that shows the cursor at the same From 1f4c9da9c60f3cf5573c2f35e74c48a0f56056b4 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 25 Sep 2019 01:56:29 -0400 Subject: [PATCH 0106/1293] test: fix screen assertions --- test/functional/ui/inccommand_spec.lua | 36 +++++++++++++------------- test/functional/ui/mouse_spec.lua | 4 +-- test/functional/ui/popupmenu_spec.lua | 6 ++--- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/test/functional/ui/inccommand_spec.lua b/test/functional/ui/inccommand_spec.lua index 351c4b4bcf..e9a7c8c2df 100644 --- a/test/functional/ui/inccommand_spec.lua +++ b/test/functional/ui/inccommand_spec.lua @@ -734,11 +734,11 @@ describe(":substitute, inccommand=split", function() feed_command("set nomodified") feed(":%s/tw") screen:expect([[ + Inc substitution on | + {12:tw}o lines | Inc substitution on | {12:tw}o lines | | - {15:~ }| - {15:~ }| {11:[No Name] }| |2| {12:tw}o lines | |4| {12:tw}o lines | @@ -791,11 +791,11 @@ describe(":substitute, inccommand=split", function() it('shows split window when typing the pattern', function() feed(":%s/tw") screen:expect([[ + Inc substitution on | + {12:tw}o lines | Inc substitution on | {12:tw}o lines | | - {15:~ }| - {15:~ }| {11:[No Name] [+] }| |2| {12:tw}o lines | |4| {12:tw}o lines | @@ -812,11 +812,11 @@ describe(":substitute, inccommand=split", function() it('shows preview with empty replacement', function() feed(":%s/tw/") screen:expect([[ + Inc substitution on | + o lines | Inc substitution on | o lines | | - {15:~ }| - {15:~ }| {11:[No Name] [+] }| |2| o lines | |4| o lines | @@ -831,11 +831,11 @@ describe(":substitute, inccommand=split", function() feed("x") screen:expect([[ + Inc substitution on | + {12:x}o lines | Inc substitution on | {12:x}o lines | | - {15:~ }| - {15:~ }| {11:[No Name] [+] }| |2| {12:x}o lines | |4| {12:x}o lines | @@ -850,11 +850,11 @@ describe(":substitute, inccommand=split", function() feed("") screen:expect([[ + Inc substitution on | + o lines | Inc substitution on | o lines | | - {15:~ }| - {15:~ }| {11:[No Name] [+] }| |2| o lines | |4| o lines | @@ -872,11 +872,11 @@ describe(":substitute, inccommand=split", function() it('shows split window when typing replacement', function() feed(":%s/tw/XX") screen:expect([[ + Inc substitution on | + {12:XX}o lines | Inc substitution on | {12:XX}o lines | | - {15:~ }| - {15:~ }| {11:[No Name] [+] }| |2| {12:XX}o lines | |4| {12:XX}o lines | @@ -938,11 +938,11 @@ describe(":substitute, inccommand=split", function() feed(":%s/tw") -- 'cursorline' is NOT active during preview. screen:expect([[ + Inc substitution on | {12:tw}o lines | Inc substitution on | {12:tw}o lines | | - {15:~ }| {11:[No Name] [+] }| |2| {12:tw}o lines | |4| {12:tw}o lines | @@ -2205,11 +2205,11 @@ describe(":substitute", function() feed("/KKK") screen:expect([[ + T T123 T T123 T2T TT T23423424| + x | afa {12:KKK}adf la;lkd {12:KKK}alx | | {15:~ }| - {15:~ }| - {15:~ }| {11:[No Name] [+] }| |3| afa {12:KKK}adf la;lkd {12:KKK}alx | {15:~ }| @@ -2485,11 +2485,11 @@ describe(":substitute", function() wait() feed([[:%s/\(some\)\@!thing/one/]]) screen:expect([[ + something | every{12:one} | someone | {15:~ }| {15:~ }| - {15:~ }| {11:[No Name] [+] }| |2| every{12:one} | {15:~ }| @@ -2527,11 +2527,11 @@ describe(":substitute", function() wait() feed([[:%s/some\(thing\)\@!/every/]]) screen:expect([[ + something | + everything | {12:every}one | {15:~ }| {15:~ }| - {15:~ }| - {15:~ }| {11:[No Name] [+] }| |3| {12:every}one | {15:~ }| diff --git a/test/functional/ui/mouse_spec.lua b/test/functional/ui/mouse_spec.lua index 3bd6b81ff1..440bae58e0 100644 --- a/test/functional/ui/mouse_spec.lua +++ b/test/functional/ui/mouse_spec.lua @@ -419,9 +419,9 @@ describe('ui/mouse/input', function() meths.set_option('showtabline', 2) screen:expect([[ {fill:test-test2 }| + testing | mouse | support and selectio^n | - {0:~ }| | ]]) meths.set_var('reply', {}) @@ -539,9 +539,9 @@ describe('ui/mouse/input', function() feed_command('tabprevious') -- go to first tab screen:expect([[ {sel: + foo }{tab: + bar }{fill: }{tab:X}| + testing | mouse | support and selectio^n | - {0:~ }| :tabprevious | ]]) feed('<10,0>') -- go to second tab diff --git a/test/functional/ui/popupmenu_spec.lua b/test/functional/ui/popupmenu_spec.lua index 37eb550835..fabcc05ce6 100644 --- a/test/functional/ui/popupmenu_spec.lua +++ b/test/functional/ui/popupmenu_spec.lua @@ -1491,20 +1491,20 @@ describe('builtin popupmenu', function() command("split") screen:expect([[ + xx | choice^ | - {1:~ }| {n:word }{1: }| {s:choice }{4: }| {n:text } | - {n:thing }{1: }| + {n:thing } | {3:[No Name] [+] }| {2:-- INSERT --} | ]]) meths.input_mouse('wheel', 'down', '', 0, 6, 15) screen:expect{grid=[[ + xx | choice^ | - {1:~ }| {n:word }{1: }| {s:choice }{4: }| {n:text } | From 8732cce3150869d116a912ffea7686bfa73a6e0b Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 25 Sep 2019 20:06:01 -0400 Subject: [PATCH 0107/1293] vim-patch:8.1.2074: test for SafeState autocommand is a bit flaky Problem: Test for SafeState autocommand is a bit flaky. Solution: Add to list of flaky tests. https://github.com/vim/vim/commit/0d0c3ca007940cdb64ccbfd0e70846eedfe6a4a6 --- src/nvim/testdir/runtest.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/testdir/runtest.vim b/src/nvim/testdir/runtest.vim index 593ce6fcdc..8f5f3f82e7 100644 --- a/src/nvim/testdir/runtest.vim +++ b/src/nvim/testdir/runtest.vim @@ -282,6 +282,7 @@ endif " Names of flaky tests. let s:flaky_tests = [ + \ 'Test_autocmd_SafeState()', \ 'Test_cursorhold_insert()', \ 'Test_exit_callback_interval()', \ 'Test_map_timeout_with_timer_interrupt()', From 56c860ac4a8897cdce8c7e6a9d238f0775de1979 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Thu, 26 Sep 2019 00:56:56 -0400 Subject: [PATCH 0108/1293] quickfix: fix pvs/v547 errors --- src/nvim/quickfix.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 5083e573d3..310b074cfb 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -1554,6 +1554,7 @@ static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, * Allocate a new location list */ static qf_info_T *ll_new_list(void) + FUNC_ATTR_NONNULL_RET { qf_info_T *qi = xcalloc(1, sizeof(qf_info_T)); qi->qf_refcount++; @@ -5650,7 +5651,7 @@ void ex_cexpr(exarg_T *eap) // Get the location list for ":lhelpgrep" static qf_info_T *hgr_get_ll(bool *new_ll) - FUNC_ATTR_NONNULL_ALL + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { win_T *wp; qf_info_T *qi; @@ -5670,9 +5671,7 @@ static qf_info_T *hgr_get_ll(bool *new_ll) } if (qi == NULL) { // Allocate a new location list for help text matches - if ((qi = ll_new_list()) == NULL) { - return NULL; - } + qi = ll_new_list(); *new_ll = true; } @@ -5810,9 +5809,6 @@ void ex_helpgrep(exarg_T *eap) if (eap->cmdidx == CMD_lhelpgrep) { qi = hgr_get_ll(&new_qi); - if (qi == NULL) { - return; - } } regmatch_T regmatch = { From 74947203afedd0227ee383ce4396745b2d5f6c4f Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 28 Sep 2019 14:45:35 -0400 Subject: [PATCH 0109/1293] vim-patch:8.1.2091: double free when memory allocation fails Problem: Double free when memory allocation fails. (Zu-Ming Jiang) Solution: Use VIM_CLEAR() instead of vim_free(). (closes vim/vim#4991) https://github.com/vim/vim/commit/0f1c6708fdf17bb9c7305b8af5d12189956195b6 --- src/nvim/getchar.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 2469bb5baa..af642a8e11 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -1165,12 +1165,12 @@ void free_typebuf(void) if (typebuf.tb_buf == typebuf_init) { internal_error("Free typebuf 1"); } else { - xfree(typebuf.tb_buf); + XFREE_CLEAR(typebuf.tb_buf); } if (typebuf.tb_noremap == noremapbuf_init) { internal_error("Free typebuf 2"); } else { - xfree(typebuf.tb_noremap); + XFREE_CLEAR(typebuf.tb_noremap); } } From 8b67c8f8c6538f22c6b6868a2603109b6670874e Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 28 Sep 2019 14:52:17 -0400 Subject: [PATCH 0110/1293] vim-patch:8.1.2095: leaking memory when getting item from dict Problem: Leaking memory when getting item from dict. Solution: Also free the key when not evaluating. https://github.com/vim/vim/commit/a893194d91a2942d4d54085d746ed137a9251b69 --- src/nvim/eval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index cb1dd1d631..e3e5bb9a90 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -5746,13 +5746,13 @@ static int dict_get_tv(char_u **arg, typval_T *rettv, int evaluate) goto failret; } item = tv_dict_item_alloc((const char *)key); - tv_clear(&tvkey); item->di_tv = tv; item->di_tv.v_lock = 0; if (tv_dict_add(d, item) == FAIL) { tv_dict_item_free(item); } } + tv_clear(&tvkey); if (**arg == '}') break; From 655085204e36cdf91750db5e09ae3feb4884c670 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 29 Sep 2019 09:29:16 -0400 Subject: [PATCH 0111/1293] vim-patch:8.1.0230: directly checking 'buftype' value Problem: Directly checking 'buftype' value. Solution: Add the bt_normal() function. (Yegappan Lakshmanan) https://github.com/vim/vim/commit/91335e5a67aaa9937e65f1e779b9f3f10fd33ee4 --- src/nvim/buffer.c | 7 +++++++ src/nvim/ex_docmd.c | 2 +- src/nvim/fileio.c | 2 +- src/nvim/quickfix.c | 6 +++--- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index b50c764ac3..e5b80693a4 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -5179,6 +5179,13 @@ bool bt_help(const buf_T *const buf) return buf != NULL && buf->b_help; } +// Return true if "buf" is a normal buffer, 'buftype' is empty. +bool bt_normal(const buf_T *const buf) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + return buf != NULL && buf->b_p_bt[0] == NUL; +} + // Return true if "buf" is the quickfix buffer. bool bt_quickfix(const buf_T *const buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index a6931f3acd..34b4c10d3e 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -9553,7 +9553,7 @@ put_view( */ if ((*flagp & SSOP_FOLDS) && wp->w_buffer->b_ffname != NULL - && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)) + && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)) ) { if (put_folds(fd, wp) == FAIL) return FAIL; diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 4cf42b41e9..0394639a16 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -4834,7 +4834,7 @@ buf_check_timestamp( if (buf->terminal || buf->b_ffname == NULL || buf->b_ml.ml_mfp == NULL - || *buf->b_p_bt != NUL + || !bt_normal(buf) || buf->b_saving || busy ) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 310b074cfb..0897367b2d 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -2140,7 +2140,7 @@ static win_T *qf_find_win_with_normal_buf(void) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { - if (wp->w_buffer->b_p_bt[0] == NUL) { + if (bt_normal(wp->w_buffer)) { return wp; } } @@ -2204,7 +2204,7 @@ static void qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, // Find a previous usable window win = curwin; do { - if (win->w_buffer->b_p_bt[0] == NUL) { + if (bt_normal(win->w_buffer)) { break; } if (win->w_prev == NULL) { @@ -2258,7 +2258,7 @@ static void qf_goto_win_with_qfl_file(int qf_fnum) // Remember a usable window. if (altwin == NULL && !win->w_p_pvw - && win->w_buffer->b_p_bt[0] == NUL) { + && bt_normal(win->w_buffer)) { altwin = win; } } From efef797126506e84fadfc7214ffc55aa7c084e80 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Mon, 30 Sep 2019 20:17:11 -0400 Subject: [PATCH 0112/1293] vim-patch:8.1.2103: wrong error message if "termdebugger" is not executable Problem: wrong error message if "termdebugger" is not executable. Solution: Check if "termdebugger" is executable and give a clear error message. (Ozaki Kiichi, closes vim/vim#5000) Fix indents. https://github.com/vim/vim/commit/18223a592efa4399e3951c86deeb712a13b05ca5 --- runtime/pack/dist/opt/termdebug/plugin/termdebug.vim | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim index a97461ad69..52b4829f5f 100644 --- a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim +++ b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim @@ -67,8 +67,8 @@ command -nargs=* -complete=file -bang Termdebug call s:StartDebug(0, 0, ) " Name of the gdb command, defaults to "gdb". -if !exists('termdebugger') - let termdebugger = 'gdb' +if !exists('g:termdebugger') + let g:termdebugger = 'gdb' endif let s:pc_id = 12 @@ -106,9 +106,14 @@ endfunc func s:StartDebug_internal(dict) if exists('s:gdbwin') - echoerr 'Terminal debugger already running' + echoerr 'Terminal debugger already running, cannot run two' return endif + if !executable(g:termdebugger) + echoerr 'Cannot execute debugger program "' .. g:termdebugger .. '"' + return + endif + let s:ptywin = 0 let s:pid = 0 From ada2ec441617077110e503c550dd3227eb9da072 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Mon, 30 Sep 2019 22:58:42 -0400 Subject: [PATCH 0113/1293] vim-patch:8.1.0315: helpgrep with language doesn't work properly Problem: Helpgrep with language doesn't work properly. (Takuya Fujiwara) Solution: Check for the language earlier. (Hirohito Higashi) https://github.com/vim/vim/commit/c631f2df624954184509df49479d52ad7fe5233b --- src/nvim/quickfix.c | 29 ++++++++++++++++------------- src/nvim/testdir/test_quickfix.vim | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 0897367b2d..511fb037fb 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -4468,7 +4468,7 @@ void ex_vimgrep(exarg_T *eap) goto theend; } - /* Jump to first match. */ + // Jump to first match. if (qi->qf_lists[qi->qf_curlist].qf_count > 0) { if ((flags & VGR_NOJUMP) == 0) { vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy, first_match_buf, @@ -5767,14 +5767,14 @@ static void hgr_search_files_in_dir( } } -// Search for a pattern in all the help files in the 'runtimepath'. +// Search for a pattern in all the help files in the 'runtimepath' +// and add the matches to a quickfix list. +// 'lang' is the language specifier. If supplied, then only matches in the +// specified language are found. static void hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, - char_u *arg) - FUNC_ATTR_NONNULL_ALL + const char_u *lang) + FUNC_ATTR_NONNULL_ARG(1, 2) { - // Check for a specified language - char_u *const lang = check_help_lang(arg); - // Go through all directories in 'runtimepath' char_u *p = p_rtp; while (*p != NUL && !got_int) { @@ -5811,6 +5811,8 @@ void ex_helpgrep(exarg_T *eap) qi = hgr_get_ll(&new_qi); } + // Check for a specified language + char_u *const lang = check_help_lang(eap->arg); regmatch_T regmatch = { .regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING), .rm_ic = false, @@ -5819,7 +5821,7 @@ void ex_helpgrep(exarg_T *eap) // Create a new quickfix list. qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep)); - hgr_search_in_rtp(qi, ®match, eap->arg); + hgr_search_in_rtp(qi, ®match, lang); vim_regfree(regmatch.regprog); @@ -5829,11 +5831,12 @@ void ex_helpgrep(exarg_T *eap) qi->qf_lists[qi->qf_curlist].qf_index = 1; } - if (p_cpo == empty_option) + if (p_cpo == empty_option) { p_cpo = save_cpo; - else - /* Darn, some plugin changed the value. */ + } else { + // Darn, some plugin changed the value. free_string_option(save_cpo); + } qf_list_changed(qi, qi->qf_curlist); qf_update_buffer(qi, NULL); @@ -5854,8 +5857,8 @@ void ex_helpgrep(exarg_T *eap) EMSG2(_(e_nomatch2), eap->arg); if (eap->cmdidx == CMD_lhelpgrep) { - /* If the help window is not opened or if it already points to the - * correct location list, then free the new location list. */ + // If the help window is not opened or if it already points to the + // correct location list, then free the new location list. if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi) { if (new_qi) { ll_free_all(&qi); diff --git a/src/nvim/testdir/test_quickfix.vim b/src/nvim/testdir/test_quickfix.vim index 83ef3c2fce..597be0aa89 100644 --- a/src/nvim/testdir/test_quickfix.vim +++ b/src/nvim/testdir/test_quickfix.vim @@ -3315,6 +3315,20 @@ func Test_qfjump() call Xqfjump_tests('l') endfunc +" Test helpgrep with lang specifier +func Xtest_helpgrep_with_lang_specifier(cchar) + call s:setup_commands(a:cchar) + Xhelpgrep Vim@en + call assert_equal('help', &filetype) + call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr) + new | only +endfunc + +func Test_helpgrep_with_lang_specifier() + call Xtest_helpgrep_with_lang_specifier('c') + call Xtest_helpgrep_with_lang_specifier('l') +endfunc + " The following test used to crash Vim. " Open the location list window and close the regular window associated with " the location list. When the garbage collection runs now, it incorrectly From 333dc3d1381f5d3c8f0770141c0a989cfff93f57 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 2 Oct 2019 04:56:22 +0200 Subject: [PATCH 0114/1293] Fix flaky test: tui_spec: increase timeout (#11134) Meant to fix: [ ERROR ] test/functional/terminal/tui_spec.lua @ 925: TUI FocusGained/FocusLost in terminal-mode test/functional/ui/screen.lua:587: Row 6 did not match. Expected: |{1:r}eady $ | |[Process exited 0] | | | | | | | |*gained | |{3:-- TERMINAL --} | Actual: |{1:r}eady $ | |[Process exited 0] | | | | | | | |*:terminal | |{3:-- TERMINAL --} | To print the expect() call that would assert the current screen state, use screen:snapshot_util(). In case of non-deterministic failures, use screen:redraw_debug() to show all intermediate screen states. stack traceback: test/functional/ui/screen.lua:587: in function '_wait' test/functional/ui/screen.lua:370: in function 'expect' test/functional/terminal/tui_spec.lua:934: in function I've thought about adding this, but it might not be really relevant, and slows down the tests a bit (and a warning "warning: Screen test succeeded immediately" with another test): ```diff diff --git i/test/functional/terminal/tui_spec.lua w/test/functional/terminal/tui_spec.lua index ada073c4e..4bc2ab4e0 100644 --- i/test/functional/terminal/tui_spec.lua +++ w/test/functional/terminal/tui_spec.lua @@ -818,6 +818,11 @@ describe('TUI FocusGained/FocusLost', function() ..'", "-u", "NONE", "-i", "NONE", "--cmd", "set noswapfile noshowcmd noruler"]') feed_data(":autocmd FocusGained * echo 'gained'\n") feed_data(":autocmd FocusLost * echo 'lost'\n") + -- Wait for autocommand to be registered. + retry(nil, nil, function() + feed_data(":autocmd FocusLost\n") + screen:expect{any=" echo 'lost'"} + end) feed_data("\034\016") -- CTRL-\ CTRL-N end) ``` --- test/functional/terminal/tui_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index ed904f27ca..ada073c4e6 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -939,7 +939,7 @@ describe('TUI FocusGained/FocusLost', function() | gained | {3:-- TERMINAL --} | - ]], timeout=(3 * screen.timeout)} + ]], timeout=(4 * screen.timeout)} feed_data('\027[O') screen:expect([[ From 0c1be45ea0b7f6702816e18f7d02641a2df47970 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 2 Oct 2019 06:26:57 +0200 Subject: [PATCH 0115/1293] shell: improve displaying of pulse (#11130) - output "[...]" to indicate throttling is being used, instead of just an empty line - go to beginning of line after displaying the pulse, so that following output is displayed over it --- src/nvim/os/shell.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/nvim/os/shell.c b/src/nvim/os/shell.c index 6956410401..f4377b1457 100644 --- a/src/nvim/os/shell.c +++ b/src/nvim/os/shell.c @@ -423,10 +423,11 @@ static bool out_data_decide_throttle(size_t size) pulse_msg[1] = (tick > 1) ? '.' : ' '; pulse_msg[2] = (tick > 2) ? '.' : ' '; if (visit == 1) { - msg_putchar('\n'); + msg_puts("[...]\n"); } msg_putchar('\r'); // put cursor at start of line msg_puts(pulse_msg); + msg_putchar('\r'); ui_flush(); return true; } From 4518f230fa84e66737f6fc313fb669984974a1fd Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 2 Oct 2019 00:06:37 -0400 Subject: [PATCH 0116/1293] vim-patch:8.1.0330: the qf_add_entries() function is too long Problem: The qf_add_entries() function is too long. Solution: Split in two parts. (Yegappan Lakshmanan) https://github.com/vim/vim/commit/6f6ef7c1951b080843f3da049d3f5d0679de7348 --- src/nvim/quickfix.c | 136 +++++++++++++++++++++++++------------------- 1 file changed, 76 insertions(+), 60 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 511fb037fb..c5e8d4b490 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -5030,15 +5030,86 @@ int qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict) return status; } +// Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the +// items in the dict 'd'. +static int qf_add_entry_from_dict( + qf_info_T *qi, + int qf_idx, + const dict_T *d, + bool first_entry) + FUNC_ATTR_NONNULL_ALL +{ + static bool did_bufnr_emsg; + + if (first_entry) { + did_bufnr_emsg = false; + } + + char *const filename = tv_dict_get_string(d, "filename", true); + char *const module = tv_dict_get_string(d, "module", true); + int bufnum = (int)tv_dict_get_number(d, "bufnr"); + const long lnum = (long)tv_dict_get_number(d, "lnum"); + const int col = (int)tv_dict_get_number(d, "col"); + const char_u vcol = (char_u)tv_dict_get_number(d, "vcol"); + const int nr = (int)tv_dict_get_number(d, "nr"); + const char *const type = tv_dict_get_string(d, "type", false); + char *const pattern = tv_dict_get_string(d, "pattern", true); + char *text = tv_dict_get_string(d, "text", true); + if (text == NULL) { + text = xcalloc(1, 1); + } + bool valid = true; + if ((filename == NULL && bufnum == 0) + || (lnum == 0 && pattern == NULL)) { + valid = false; + } + + // Mark entries with non-existing buffer number as not valid. Give the + // error message only once. + if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) { + if (!did_bufnr_emsg) { + did_bufnr_emsg = true; + EMSGN(_("E92: Buffer %" PRId64 " not found"), bufnum); + } + valid = false; + bufnum = 0; + } + + // If the 'valid' field is present it overrules the detected value. + if (tv_dict_find(d, "valid", -1) != NULL) { + valid = tv_dict_get_number(d, "valid"); + } + + const int status = qf_add_entry(qi, + qf_idx, + NULL, // dir + (char_u *)filename, + (char_u *)module, + bufnum, + (char_u *)text, + lnum, + col, + vcol, // vis_col + (char_u *)pattern, // search pattern + nr, + (char_u)(type == NULL ? NUL : *type), + valid); + + xfree(filename); + xfree(module); + xfree(pattern); + xfree(text); + + return status; +} + /// Add list of entries to quickfix/location list. Each list entry is /// a dictionary with item information. static int qf_add_entries(qf_info_T *qi, int qf_idx, list_T *list, char_u *title, int action) { - dict_T *d; qfline_T *old_last = NULL; int retval = OK; - bool did_bufnr_emsg = false; if (action == ' ' || qf_idx == qi->qf_listcount) { // make place for a new list @@ -5057,68 +5128,13 @@ static int qf_add_entries(qf_info_T *qi, int qf_idx, list_T *list, continue; // Skip non-dict items. } - d = TV_LIST_ITEM_TV(li)->vval.v_dict; + const dict_T *const d = TV_LIST_ITEM_TV(li)->vval.v_dict; if (d == NULL) { continue; } - char *const filename = tv_dict_get_string(d, "filename", true); - char *const module = tv_dict_get_string(d, "module", true); - int bufnum = (int)tv_dict_get_number(d, "bufnr"); - long lnum = (long)tv_dict_get_number(d, "lnum"); - int col = (int)tv_dict_get_number(d, "col"); - char_u vcol = (char_u)tv_dict_get_number(d, "vcol"); - int nr = (int)tv_dict_get_number(d, "nr"); - const char *type_str = tv_dict_get_string(d, "type", false); - const char_u type = (char_u)(uint8_t)(type_str == NULL ? NUL : *type_str); - char *const pattern = tv_dict_get_string(d, "pattern", true); - char *text = tv_dict_get_string(d, "text", true); - if (text == NULL) { - text = xcalloc(1, 1); - } - bool valid = true; - if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL)) { - valid = false; - } - - /* Mark entries with non-existing buffer number as not valid. Give the - * error message only once. */ - if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) { - if (!did_bufnr_emsg) { - did_bufnr_emsg = TRUE; - EMSGN(_("E92: Buffer %" PRId64 " not found"), bufnum); - } - valid = false; - bufnum = 0; - } - - // If the 'valid' field is present it overrules the detected value. - if (tv_dict_find(d, "valid", -1) != NULL) { - valid = (int)tv_dict_get_number(d, "valid"); - } - - int status = qf_add_entry(qi, - qf_idx, - NULL, // dir - (char_u *)filename, - (char_u *)module, - bufnum, - (char_u *)text, - lnum, - col, - vcol, // vis_col - (char_u *)pattern, // search pattern - nr, - type, - valid); - - xfree(filename); - xfree(module); - xfree(pattern); - xfree(text); - - if (status == FAIL) { - retval = FAIL; + retval = qf_add_entry_from_dict(qi, qf_idx, d, li == tv_list_first(list)); + if (retval == FAIL) { break; } }); From 3d3c783fead3dc328ede3ccd0fb7b3befcad191a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 3 Oct 2019 05:40:25 +0200 Subject: [PATCH 0117/1293] ci: Travis: simplify 32bit build (#11093) - `CMAKE_SYSTEM_LIBRARY_PATH` should not be used, and is a semicolon-separated list anyway [1] 1: https://cmake.org/cmake/help/latest/variable/CMAKE_SYSTEM_LIBRARY_PATH.html --- .travis.yml | 6 ++---- ci/common/build.sh | 6 ------ 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 325b5e7b56..2882ba8935 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,10 +24,6 @@ env: -DDEPS_PREFIX=$DEPS_BUILD_DIR/usr -DMIN_LOG_LEVEL=3" - DEPS_CMAKE_FLAGS="-DUSE_BUNDLED_GPERF=OFF" - # Additional CMake flags for 32-bit builds. - - CMAKE_FLAGS_32BIT="-DCMAKE_SYSTEM_LIBRARY_PATH=/lib32:/usr/lib32:/usr/local/lib32 - -DCMAKE_IGNORE_PATH=/lib:/usr/lib:/usr/local/lib - -DCMAKE_TOOLCHAIN_FILE=$TRAVIS_BUILD_DIR/cmake/i386-linux-gnu.toolchain.cmake" # Environment variables for Clang sanitizers. - ASAN_OPTIONS="detect_leaks=1:check_initialization_order=1:log_path=$LOG_DIR/asan" - TSAN_OPTIONS="log_path=$LOG_DIR/tsan" @@ -136,6 +132,8 @@ jobs: compiler: gcc env: - BUILD_32BIT=ON + - CMAKE_FLAGS="$CMAKE_FLAGS -m32 -DCMAKE_TOOLCHAIN_FILE=$TRAVIS_BUILD_DIR/cmake/i386-linux-gnu.toolchain.cmake" + - DEPS_CMAKE_FLAGS="$DEPS_CMAKE_FLAGS -m32 -DCMAKE_TOOLCHAIN_FILE=$TRAVIS_BUILD_DIR/cmake/i386-linux-gnu.toolchain.cmake" # Minimum required CMake. - CMAKE_URL=https://cmake.org/files/v2.8/cmake-2.8.12-Linux-i386.sh - *common-job-env diff --git a/ci/common/build.sh b/ci/common/build.sh index 8e9b2f8ebb..02e1110a15 100644 --- a/ci/common/build.sh +++ b/ci/common/build.sh @@ -18,9 +18,6 @@ build_make() { } build_deps() { - if test "${BUILD_32BIT}" = ON ; then - DEPS_CMAKE_FLAGS="${DEPS_CMAKE_FLAGS} ${CMAKE_FLAGS_32BIT}" - fi if test "${FUNCTIONALTEST}" = "functionaltest-lua" \ || test "${CLANG_SANITIZER}" = "ASAN_UBSAN" ; then DEPS_CMAKE_FLAGS="${DEPS_CMAKE_FLAGS} -DUSE_BUNDLED_LUA=ON" @@ -53,9 +50,6 @@ prepare_build() { if test -n "${CLANG_SANITIZER}" ; then CMAKE_FLAGS="${CMAKE_FLAGS} -DCLANG_${CLANG_SANITIZER}=ON" fi - if test "${BUILD_32BIT}" = ON ; then - CMAKE_FLAGS="${CMAKE_FLAGS} ${CMAKE_FLAGS_32BIT}" - fi mkdir -p "${BUILD_DIR}" cd "${BUILD_DIR}" From b069e9b20f9e1f24fde34bee7d6e5d95f47ef10d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 3 Oct 2019 07:41:57 +0200 Subject: [PATCH 0118/1293] tests: unit: NVIM_TEST_TRACE_LEVEL: default to 0 #11144 Traces are not useful normally (unless debugging/fixing tests), but only add overhead. Disable them by default. --- test/README.md | 11 ++++++----- test/unit/helpers.lua | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/test/README.md b/test/README.md index 64892b5576..2b19740434 100644 --- a/test/README.md +++ b/test/README.md @@ -315,11 +315,12 @@ Number; !must be defined to function properly): - `NVIM_TEST_RUN_TESTTEST` (U) (1): allows running `test/unit/testtest_spec.lua` used to check how testing infrastructure works. -- `NVIM_TEST_TRACE_LEVEL` (U) (N): specifies unit tests tracing level: `0` - disables tracing (the fastest, but you get no data if tests crash and there - was no core dump generated), `1` or empty/undefined leaves only C function - cals and returns in the trace (faster then recording everything), `2` records - all function calls, returns and lua source lines exuecuted. +- `NVIM_TEST_TRACE_LEVEL` (U) (N): specifies unit tests tracing level: + - `0` disables tracing (the fastest, but you get no data if tests crash and + there no core dump was generated), + - `1` leaves only C function calls and returns in the trace (faster than + recording everything), + - `2` records all function calls, returns and executed Lua source lines. - `NVIM_TEST_TRACE_ON_ERROR` (U) (1): makes unit tests yield trace on error in addition to regular error message. diff --git a/test/unit/helpers.lua b/test/unit/helpers.lua index 24dbc65bd0..bacdc54416 100644 --- a/test/unit/helpers.lua +++ b/test/unit/helpers.lua @@ -545,7 +545,7 @@ local tracehelp = dedent([[ local function child_sethook(wr) local trace_level = os.getenv('NVIM_TEST_TRACE_LEVEL') if not trace_level or trace_level == '' then - trace_level = 1 + trace_level = 0 else trace_level = tonumber(trace_level) end @@ -708,7 +708,7 @@ local function check_child_err(rd) local eres = sc.read(rd, 2) if eres ~= '$\n' then if #trace == 0 then - err = '\nTest crashed, no trace available\n' + err = '\nTest crashed, no trace available (check NVIM_TEST_TRACE_LEVEL)\n' else err = '\nTest crashed, trace:\n' .. tracehelp for i = 1, #trace do From f96d1e6bc416fe0d1d11321234637695ff0e514e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 3 Oct 2019 08:04:24 +0200 Subject: [PATCH 0119/1293] tui: fix handling of bg response after suspend (#11145) `tui_terminal_after_startup` gets called right after resuming from suspending (via `Ctrl-z`) already (not delayed as with the startup itself), and would set `waiting_for_bg_response` to false then directly. This results in the terminal response not being processed then anymore, and leaking into Neovim itself. This changes it to try 5 times always, which means that it typically would stop after a few characters of input from the user typically, e.g. with tmux, which does not send a reply. While it might be better to have something based on the time (e.g. only wait for max 1s), this appears to be easier to do. Fixes regression in 8a4ae3d. --- src/nvim/tui/input.c | 10 +++++++--- src/nvim/tui/input.h | 2 +- src/nvim/tui/tui.c | 7 +------ test/unit/tui_spec.lua | 21 +++++++++------------ 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c index 876f00e03e..844bc0db40 100644 --- a/src/nvim/tui/input.c +++ b/src/nvim/tui/input.c @@ -27,7 +27,7 @@ void tinput_init(TermInput *input, Loop *loop) input->loop = loop; input->paste = 0; input->in_fd = 0; - input->waiting_for_bg_response = false; + input->waiting_for_bg_response = 0; input->key_buffer = rbuffer_new(KEY_BUFFER_SIZE); uv_mutex_init(&input->key_buffer_mutex); uv_cond_init(&input->key_buffer_cond); @@ -444,7 +444,7 @@ static void set_bg_deferred(void **argv) // [1] https://en.wikipedia.org/wiki/Luma_%28video%29 static bool handle_background_color(TermInput *input) { - if (!input->waiting_for_bg_response) { + if (input->waiting_for_bg_response <= 0) { return false; } size_t count = 0; @@ -465,9 +465,13 @@ static bool handle_background_color(TermInput *input) header_size = 10; num_components = 4; } else { + input->waiting_for_bg_response--; + if (input->waiting_for_bg_response == 0) { + DLOG("did not get a response for terminal background query"); + } return false; } - input->waiting_for_bg_response = false; + input->waiting_for_bg_response = 0; rbuffer_consumed(input->read_stream.buffer, header_size); RBUFFER_EACH(input->read_stream.buffer, c, i) { count = i + 1; diff --git a/src/nvim/tui/input.h b/src/nvim/tui/input.h index 49ae32f00e..77bd6fa132 100644 --- a/src/nvim/tui/input.h +++ b/src/nvim/tui/input.h @@ -12,7 +12,7 @@ typedef struct term_input { // Phases: -1=all 0=disabled 1=first-chunk 2=continue 3=last-chunk int8_t paste; bool waiting; - bool waiting_for_bg_response; + int8_t waiting_for_bg_response; TermKey *tk; #if TERMKEY_VERSION_MAJOR > 0 || TERMKEY_VERSION_MINOR > 18 TermKey_Terminfo_Getstr_Hook *tk_ti_hook_fn; ///< libtermkey terminfo hook diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index 956d4eb9da..150862bb18 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -296,7 +296,7 @@ static void terminfo_start(UI *ui) unibi_out(ui, unibi_keypad_xmit); unibi_out(ui, unibi_clear_screen); // Ask the terminal to send us the background color. - data->input.waiting_for_bg_response = true; + data->input.waiting_for_bg_response = 5; unibi_out_ext(ui, data->unibi_ext.get_bg); // Enable bracketed paste unibi_out_ext(ui, data->unibi_ext.enable_bracketed_paste); @@ -366,11 +366,6 @@ static void tui_terminal_after_startup(UI *ui) // 2.3 bug(?) which caused slow drawing during startup. #7649 unibi_out_ext(ui, data->unibi_ext.enable_focus_reporting); flush_buf(ui); - - if (data->input.waiting_for_bg_response) { - DLOG("did not get a response for terminal background query"); - data->input.waiting_for_bg_response = false; - } } static void tui_terminal_stop(UI *ui) diff --git a/test/unit/tui_spec.lua b/test/unit/tui_spec.lua index 47dbd87b71..e6b5c889d7 100644 --- a/test/unit/tui_spec.lua +++ b/test/unit/tui_spec.lua @@ -16,7 +16,7 @@ itp('handle_background_color', function() local events = globals.main_loop.thread_events -- Short-circuit when not waiting for response. - term_input.waiting_for_bg_response = false + term_input.waiting_for_bg_response = 0 eq(false, handle_background_color(term_input)) local capacity = 100 @@ -27,9 +27,9 @@ itp('handle_background_color', function() local term_response = '\027]11;'..colorspace..':'..color..'\007' rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response) - term_input.waiting_for_bg_response = true + term_input.waiting_for_bg_response = 1 eq(true, handle_background_color(term_input)) - eq(false, term_input.waiting_for_bg_response) + eq(0, term_input.waiting_for_bg_response) eq(1, multiqueue.multiqueue_size(events)) local event = multiqueue.multiqueue_get(events) @@ -101,10 +101,9 @@ itp('handle_background_color', function() local term_response = '\027]11;rgba:f/f/f/f' -- missing '\007 rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response) - term_input.waiting_for_bg_response = true - eq(true, term_input.waiting_for_bg_response) + term_input.waiting_for_bg_response = 1 eq(false, handle_background_color(term_input)) - eq(false, term_input.waiting_for_bg_response) + eq(0, term_input.waiting_for_bg_response) eq(0, multiqueue.multiqueue_size(events)) eq(0, rbuf.size) @@ -114,10 +113,9 @@ itp('handle_background_color', function() term_response = '123\027]11;rgba:f/f/f/f\007456' rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response) - term_input.waiting_for_bg_response = true - eq(true, term_input.waiting_for_bg_response) + term_input.waiting_for_bg_response = 3 eq(false, handle_background_color(term_input)) - eq(true, term_input.waiting_for_bg_response) + eq(2, term_input.waiting_for_bg_response) eq(0, multiqueue.multiqueue_size(events)) eq(#term_response, rbuf.size) @@ -128,10 +126,9 @@ itp('handle_background_color', function() term_response = '\027]11;rgba:f/f/f/f\007456' rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response) - term_input.waiting_for_bg_response = true - eq(true, term_input.waiting_for_bg_response) + term_input.waiting_for_bg_response = 1 eq(true, handle_background_color(term_input)) - eq(false, term_input.waiting_for_bg_response) + eq(0, term_input.waiting_for_bg_response) eq(1, multiqueue.multiqueue_size(events)) eq(3, rbuf.size) From 8d68a37c5a4d3258ccee1b5d6cdeda8f40d024c5 Mon Sep 17 00:00:00 2001 From: Zach Wegner Date: Thu, 3 Oct 2019 01:06:05 -0500 Subject: [PATCH 0120/1293] refactor: wrap common plines() usage in plines_win_full() #11141 --- src/nvim/misc1.c | 42 +++++++++++++++++++++++++------------- src/nvim/move.c | 53 ++++++++++++++++-------------------------------- 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c index ab3520dd73..c1de7ab9a4 100644 --- a/src/nvim/misc1.c +++ b/src/nvim/misc1.c @@ -484,25 +484,39 @@ int plines_win_col(win_T *wp, linenr_T lnum, long column) return lines; } +/// Get the number of screen lines lnum takes up. This takes care of +/// both folds and topfill, and limits to the current window height. +/// +/// @param[in] wp window line is in +/// @param[in] lnum line number +/// @param[out] nextp if not NULL, the line after a fold +/// @param[out] foldedp if not NULL, whether lnum is on a fold +/// @param[in] cache whether to use the window's cache for folds +/// +/// @return the total number of screen lines +int plines_win_full(win_T *wp, linenr_T lnum, linenr_T *const nextp, + bool *const foldedp, const bool cache) +{ + bool folded = hasFoldingWin(wp, lnum, NULL, nextp, cache, NULL); + if (foldedp) { + *foldedp = folded; + } + if (folded) { + return 1; + } else if (lnum == wp->w_topline) { + return plines_win_nofill(wp, lnum, true) + wp->w_topfill; + } + return plines_win(wp, lnum, true); +} + int plines_m_win(win_T *wp, linenr_T first, linenr_T last) { int count = 0; while (first <= last) { - // Check if there are any really folded lines, but also included lines - // that are maybe folded. - linenr_T x = foldedCount(wp, first, NULL); - if (x > 0) { - ++count; /* count 1 for "+-- folded" line */ - first += x; - } else { - if (first == wp->w_topline) { - count += plines_win_nofill(wp, first, true) + wp->w_topfill; - } else { - count += plines_win(wp, first, true); - } - first++; - } + linenr_T next = first; + count += plines_win_full(wp, first, &next, NULL, false); + first = next + 1; } return count; } diff --git a/src/nvim/move.c b/src/nvim/move.c index e6fee9999f..efbc548620 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -65,17 +65,10 @@ static void comp_botline(win_T *wp) done = 0; } - for (; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum) { - int n; + for (; lnum <= wp->w_buffer->b_ml.ml_line_count; lnum++) { linenr_T last = lnum; - bool folded = hasFoldingWin(wp, lnum, NULL, &last, true, NULL); - if (folded) { - n = 1; - } else if (lnum == wp->w_topline) { - n = plines_win_nofill(wp, lnum, true) + wp->w_topfill; - } else { - n = plines_win(wp, lnum, true); - } + bool folded; + int n = plines_win_full(wp, lnum, &last, &folded, true); if (lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum) { wp->w_cline_row = done; wp->w_cline_height = n; @@ -571,18 +564,14 @@ static void curs_rows(win_T *wp) break; wp->w_cline_row += wp->w_lines[i].wl_size; } else { - long fold_count = foldedCount(wp, lnum, NULL); - if (fold_count) { - lnum += fold_count; - if (lnum > wp->w_cursor.lnum) - break; - ++wp->w_cline_row; - } else if (lnum == wp->w_topline) { - wp->w_cline_row += plines_win_nofill(wp, lnum++, true) - + wp->w_topfill; - } else { - wp->w_cline_row += plines_win(wp, lnum++, true); + linenr_T last = lnum; + bool folded; + int n = plines_win_full(wp, lnum, &last, &folded, false); + lnum = last + 1; + if (folded && lnum > wp->w_cursor.lnum) { + break; } + wp->w_cline_row += n; } } @@ -593,18 +582,13 @@ static void curs_rows(win_T *wp) || (i < wp->w_lines_valid && (!wp->w_lines[i].wl_valid || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum))) { - if (wp->w_cursor.lnum == wp->w_topline) - wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum, - true) + wp->w_topfill; - else - wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, true); - wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum, - NULL, NULL, true, NULL); + wp->w_cline_height = plines_win_full(wp, wp->w_cursor.lnum, NULL, + &wp->w_cline_folded, true); } else if (i > wp->w_lines_valid) { /* a line that is too long to fit on the last screen line */ wp->w_cline_height = 0; - wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum, - NULL, NULL, true, NULL); + wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum, NULL, + NULL, true, NULL); } else { wp->w_cline_height = wp->w_lines[i].wl_size; wp->w_cline_folded = wp->w_lines[i].wl_folded; @@ -646,12 +630,9 @@ static void validate_cheight(void) { check_cursor_moved(curwin); if (!(curwin->w_valid & VALID_CHEIGHT)) { - if (curwin->w_cursor.lnum == curwin->w_topline) - curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum) - + curwin->w_topfill; - else - curwin->w_cline_height = plines(curwin->w_cursor.lnum); - curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL); + curwin->w_cline_height = plines_win_full(curwin, curwin->w_cursor.lnum, + NULL, &curwin->w_cline_folded, + true); curwin->w_valid |= VALID_CHEIGHT; } } From c3ae5e13753e1b27324f167bdc7fab94a86ca294 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 3 Oct 2019 09:32:14 +0200 Subject: [PATCH 0121/1293] test/old: align with Vim #11096 --- src/nvim/testdir/test_normal.vim | 18 +++++++-------- src/nvim/testdir/test_window_cmd.vim | 33 ++++++++++++++-------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index b3e43640bb..8bc4228359 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -2488,6 +2488,15 @@ func Test_normal_large_count() bwipe! endfunc +func Test_delete_until_paragraph() + new + normal grádv} + call assert_equal('á', getline(1)) + normal grád} + call assert_equal('', getline(1)) + bwipe! +endfunc + " Test for the gr (virtual replace) command " Test for the bug fixed by 7.4.387 func Test_gr_command() @@ -2553,15 +2562,6 @@ func Test_nv_hat_count() %bwipeout! endfunc -func Test_delete_until_paragraph() - new - normal grádv} - call assert_equal('á', getline(1)) - normal grád} - call assert_equal('', getline(1)) - bwipe! -endfunc - func Test_message_when_using_ctrl_c() " Make sure no buffers are changed. %bwipe! diff --git a/src/nvim/testdir/test_window_cmd.vim b/src/nvim/testdir/test_window_cmd.vim index 43c1f06c44..aaa291f87d 100644 --- a/src/nvim/testdir/test_window_cmd.vim +++ b/src/nvim/testdir/test_window_cmd.vim @@ -69,18 +69,6 @@ function Test_window_cmd_wincmd_gf() augroup! test_window_cmd_wincmd_gf endfunc -func Test_next_split_all() - " This was causing an illegal memory access. - n x - norm axxx - split - split - s/x - s/x - all - bwipe! -endfunc - func Test_window_quit() e Xa split Xb @@ -502,6 +490,17 @@ func Test_window_newtab() %bw! endfunc +func Test_next_split_all() + " This was causing an illegal memory access. + n x + norm axxx + split + split + s/x + s/x + all + bwipe! +endfunc " Tests for adjusting window and contents func GetScreenStr(row) @@ -541,6 +540,11 @@ func Test_window_contents() call test_garbagecollect_now() endfunc +func Test_window_colon_command() + " This was reading invalid memory. + exe "norm! v\:\echo v:version" +endfunc + func Test_access_freed_mem() " This was accessing freed memory au * 0 vs xxx @@ -837,9 +841,4 @@ func Test_winnr() only | tabonly endfunc -func Test_window_colon_command() - " This was reading invalid memory. - exe "norm! v\:\echo v:version" -endfunc - " vim: shiftwidth=2 sts=2 expandtab From 382391bb2de4698feb05e4c8e6c8286ccc4eaa8c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 4 Oct 2019 08:16:30 +0200 Subject: [PATCH 0122/1293] health: provider: skip checks with `g:loaded_X_provider = 0` (#11147) The Python provider was special (via [1]), and would continue to do checks with `0` being set explicitly even. This was fixed in #11044 (45447e3b6), ref: #11040. This extends it to use the same method with all providers. 1: https://github.com/neovim/neovim/pull/8047 --- runtime/autoload/health/provider.vim | 33 +++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/runtime/autoload/health/provider.vim b/runtime/autoload/health/provider.vim index 61858193c3..c750a954fa 100644 --- a/runtime/autoload/health/provider.vim +++ b/runtime/autoload/health/provider.vim @@ -265,6 +265,22 @@ function! s:check_bin(bin) abort return 1 endfunction +" Check "loaded" var for given a:provider. +" Returns 1 if the caller should return (skip checks). +function! s:disabled_via_loaded_var(provider) abort + let loaded_var = 'g:loaded_'.a:provider.'_provider' + if exists(loaded_var) && !exists('*provider#'.a:provider.'#Call') + let v = eval(loaded_var) + if 0 is v + call health#report_info('Disabled ('.loaded_var.'='.v.').') + return 1 + else + call health#report_info('Disabled ('.loaded_var.'='.v.'). This might be due to some previous error.') + endif + endif + return 0 +endfunction + function! s:check_python(version) abort call health#report_start('Python ' . a:version . ' provider (optional)') @@ -272,15 +288,10 @@ function! s:check_python(version) abort let python_exe = '' let venv = exists('$VIRTUAL_ENV') ? resolve($VIRTUAL_ENV) : '' let host_prog_var = pyname.'_host_prog' - let loaded_var = 'g:loaded_'.pyname.'_provider' let python_multiple = [] - if exists(loaded_var) && !exists('*provider#'.pyname.'#Call') - let v = eval(loaded_var) - call health#report_info('Disabled ('.loaded_var.'='.v.').'.(0 is v ? '' : ' This might be due to some previous error.')) - if 0 is v - return - endif + if s:disabled_via_loaded_var(pyname) + return endif let [pyenv, pyenv_root] = s:check_for_pyenv() @@ -488,9 +499,7 @@ endfunction function! s:check_ruby() abort call health#report_start('Ruby provider (optional)') - let loaded_var = 'g:loaded_ruby_provider' - if exists(loaded_var) && !exists('*provider#ruby#Call') - call health#report_info('Disabled. '.loaded_var.'='.eval(loaded_var)) + if s:disabled_via_loaded_var('ruby') return endif @@ -544,9 +553,7 @@ endfunction function! s:check_node() abort call health#report_start('Node.js provider (optional)') - let loaded_var = 'g:loaded_node_provider' - if exists(loaded_var) && !exists('*provider#node#Call') - call health#report_info('Disabled. '.loaded_var.'='.eval(loaded_var)) + if s:disabled_via_loaded_var('node') return endif From cd73a0342a457c035b84e4406428ac30b30bf754 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 1 Oct 2019 00:06:53 +0200 Subject: [PATCH 0123/1293] tests: tui_spec: improve/merge OptionSet/deferred Closes https://github.com/neovim/neovim/pull/11129. --- test/functional/terminal/tui_spec.lua | 73 ++++++++------------------- 1 file changed, 20 insertions(+), 53 deletions(-) diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index ada073c4e6..978267e040 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -1345,59 +1345,26 @@ describe("TUI", function() end) -describe('TUI background color', function() - local screen +it('TUI bg color triggers OptionSet event on terminal-response', function() + -- Only single integration test. + -- See test/unit/tui_spec.lua for unit tests. + clear() + local screen = thelpers.screen_setup(0, '["'..nvim_prog + ..'", "-u", "NONE", "-i", "NONE", "--cmd", "set noswapfile", ' + ..'"-c", "autocmd OptionSet background echo \\"did OptionSet, yay!\\""]') - before_each(function() - clear() - screen = thelpers.screen_setup(0, '["'..nvim_prog - ..'", "-u", "NONE", "-i", "NONE", "--cmd", "set noswapfile"]') - end) + screen:expect([[ + {1: } | + {4:~ }| + {4:~ }| + {4:~ }| + {5:[No Name] 0,0-1 All}| + | + {3:-- TERMINAL --} | + ]]) + feed_data('\027]11;rgb:ffff/ffff/ffff\007') + screen:expect{any='did OptionSet, yay!'} - it("triggers OptionSet event on terminal-response", function() - feed_data('\027:autocmd OptionSet background echo "did OptionSet, yay!"\n') - - -- Wait for the child Nvim to register the OptionSet handler. - feed_data('\027:autocmd OptionSet\n') - screen:expect({any='--- Autocommands ---'}) - - feed_data('\012') -- CTRL-L: clear the screen - screen:expect([[ - {1: } | - {4:~ }| - {4:~ }| - {4:~ }| - {5:[No Name] 0,0-1 All}| - | - {3:-- TERMINAL --} | - ]]) - feed_data('\027]11;rgb:ffff/ffff/ffff\007') - screen:expect{any='did OptionSet, yay!'} - end) - - it("handles deferred background color", function() - local function wait_for_bg(bg) - -- Retry until the terminal response is handled. - retry(100, nil, function() - feed_data(':echo &background\n') - screen:expect({ - timeout=40, - grid=string.format([[ - {1: } | - {4:~ }| - {4:~ }| - {4:~ }| - {5:[No Name] 0,0-1 All}| - %-5s | - {3:-- TERMINAL --} | - ]], bg) - }) - end) - end - - -- Only single integration test. - -- See test/unit/tui_spec.lua for unit tests. - feed_data('\027]11;rgb:ffff/ffff/ffff\007') - wait_for_bg('light') - end) + feed_data(':echo "new_bg=".&background\n') + screen:expect{any='new_bg=light'} end) From a341eb608706e5e8ac691a7e8f4a9d314bafee20 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 26 Sep 2019 09:15:21 +0200 Subject: [PATCH 0124/1293] win_line: update `w_last_cursorline` always Vim patch 8.1.0856 (54d9ea6) caused a performance regression in Neovim, when `set conceallevel=1 nocursorline` was used, since then due to refactoring in 23c71d5 `w_last_cursorline` would never get updated anymore. Adds/uses `redrawdebug+=nodelta` for testing this. Fixes https://github.com/neovim/neovim/issues/11100. Closes https://github.com/neovim/neovim/pull/11101. --- runtime/doc/options.txt | 7 ++-- src/nvim/option.c | 4 ++ src/nvim/option_defs.h | 9 ++++- src/nvim/screen.c | 8 ++-- test/functional/ui/syntax_conceal_spec.lua | 47 ++++++++++++++++++++++ 5 files changed, 67 insertions(+), 8 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 6b9f0380f6..8518d989d6 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -4588,9 +4588,11 @@ A jump table for the options with a short description can be found at |Q_op|. larger scroll when drawing the message area (with 'display' msgsep flag active). invalid Enable stricter checking (abort) of inconsistencies - of the internal screen state. This is mosly + of the internal screen state. This is mostly useful when running nvim inside a debugger (and the test suite). + nodelta Send all internally redrawn cells to the UI, even if + they are unchanged from the already displayed state. *'redrawtime'* *'rdt'* 'redrawtime' 'rdt' number (default 2000) @@ -6916,7 +6918,6 @@ A jump table for the options with a short description can be found at |Q_op|. global The number of milliseconds to wait for each character sent to the screen. When positive, characters are sent to the UI one by one. - When negative, all redrawn characters cause a delay, even if the - character already was displayed by the UI. For debugging purposes. + See 'redrawdebug' for more options. For debugging purposes. vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/src/nvim/option.c b/src/nvim/option.c index 3ccc67eb14..22f7b85133 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4306,6 +4306,10 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, if (value < 0) { errmsg = e_positive; } + } else if (pp == &p_wd) { + if (value < 0) { + errmsg = e_positive; + } } // Don't change the value and return early if validation failed. diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index 108a3dde7c..67cb53ce02 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -518,11 +518,18 @@ EXTERN long p_pyx; // 'pyxversion' EXTERN char_u *p_rdb; // 'redrawdebug' EXTERN unsigned rdb_flags; # ifdef IN_OPTION_C -static char *(p_rdb_values[]) = { "compositor", "nothrottle", "invalid", NULL }; +static char *(p_rdb_values[]) = { + "compositor", + "nothrottle", + "invalid", + "nodelta", + NULL +}; # endif # define RDB_COMPOSITOR 0x001 # define RDB_NOTHROTTLE 0x002 # define RDB_INVALID 0x004 +# define RDB_NODELTA 0x008 EXTERN long p_rdt; // 'redrawtime' EXTERN int p_remap; // 'remap' diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 187c89b28c..488341c6a7 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -577,7 +577,7 @@ void conceal_check_cursor_line(void) /// Whether cursorline is drawn in a special way /// /// If true, both old and new cursorline will need -/// need to be redrawn when moving cursor within windows. +/// to be redrawn when moving cursor within windows. /// TODO(bfredl): VIsual_active shouldn't be needed, but is used to fix a glitch /// caused by scrolling. bool win_cursorline_standout(const win_T *wp) @@ -2406,10 +2406,10 @@ win_line ( filler_todo = filler_lines; // Cursor line highlighting for 'cursorline' in the current window. - if (wp->w_p_cul && lnum == wp->w_cursor.lnum) { + if (lnum == wp->w_cursor.lnum) { // Do not show the cursor line when Visual mode is active, because it's // not clear what is selected then. - if (!(wp == curwin && VIsual_active)) { + if (wp->w_p_cul && !(wp == curwin && VIsual_active)) { int cul_attr = win_hl_attr(wp, HLF_CUL); HlAttrs ae = syn_attr2entry(cul_attr); @@ -4354,7 +4354,7 @@ static int grid_char_needs_redraw(ScreenGrid *grid, int off_from, int off_to, || (line_off2cells(linebuf_char, off_from, off_from + cols) > 1 && schar_cmp(linebuf_char[off_from + 1], grid->chars[off_to + 1]))) - || p_wd < 0)); + || rdb_flags & RDB_NODELTA)); } /// Move one buffered line to the window grid, but only the characters that diff --git a/test/functional/ui/syntax_conceal_spec.lua b/test/functional/ui/syntax_conceal_spec.lua index 7079b43414..566d183f11 100644 --- a/test/functional/ui/syntax_conceal_spec.lua +++ b/test/functional/ui/syntax_conceal_spec.lua @@ -1,6 +1,7 @@ local helpers = require('test.functional.helpers')(after_each) local Screen = require('test.functional.ui.screen') local clear, feed, command = helpers.clear, helpers.feed, helpers.command +local eq = helpers.eq local insert = helpers.insert describe('Screen', function() @@ -870,4 +871,50 @@ describe('Screen', function() ]]} end) end) + + it('redraws not too much with conceallevel=1', function() + command('set conceallevel=1') + command('set redrawdebug+=nodelta') + + insert([[ + aaa + bbb + ccc + ]]) + screen:expect{grid=[[ + aaa | + bbb | + ccc | + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]} + + -- XXX: hack to get notifications, and check only a single line is + -- updated. Could use next_msg() also. + local orig_handle_grid_line = screen._handle_grid_line + local grid_lines = {} + function screen._handle_grid_line(self, grid, row, col, items) + table.insert(grid_lines, {row, col, items}) + orig_handle_grid_line(self, grid, row, col, items) + end + feed('k') + screen:expect{grid=[[ + aaa | + bbb | + ^ccc | + | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]} + eq(grid_lines, {{2, 0, {{'c', 0, 3}}}}) + end) end) From 77a551b6571f9a455efffeb7f43bf8235b2cbe49 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 4 Oct 2019 16:09:42 +0200 Subject: [PATCH 0125/1293] ci: coverage for Lua (no Windows, using luacov) (#11127) --- .luacov | 19 +++++++++++++++++++ .travis.yml | 2 ++ ci/before_script.sh | 5 +++++ ci/common/submit_coverage.sh | 8 ++++++++ 4 files changed, 34 insertions(+) create mode 100644 .luacov diff --git a/.luacov b/.luacov new file mode 100644 index 0000000000..422783b858 --- /dev/null +++ b/.luacov @@ -0,0 +1,19 @@ +-- Configuration file for LuaCov + +local source = require("lfs").currentdir() + +local function pesc(s) + assert(type(s) == 'string', s) + return s:gsub('[%(%)%.%%%+%-%*%?%[%]%^%$]', '%%%1') +end + +return { + include = { + -- Absolute paths (starting with source dir, not hidden (i.e. .deps)). + pesc(source) .. "[/\\][^.].+", + -- Relative (non-hidden) paths. + '^[^/\\.]', + }, +} + +-- vim: ft=lua tw=80 sw=2 et diff --git a/.travis.yml b/.travis.yml index 2882ba8935..d8a1d2ddf0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -95,6 +95,8 @@ jobs: - GCOV=gcov-9 - CMAKE_FLAGS="$CMAKE_FLAGS -DUSE_GCOV=ON" - GCOV_ERROR_FILE="/tmp/libgcov-errors.log" + - USE_LUACOV=1 + - BUSTED_ARGS="--coverage" - *common-job-env addons: apt: diff --git a/ci/before_script.sh b/ci/before_script.sh index 605ecdbf66..6a07c6747e 100755 --- a/ci/before_script.sh +++ b/ci/before_script.sh @@ -35,5 +35,10 @@ fi # Compile dependencies. build_deps +# Install luacov for Lua coverage. +if [[ "$USE_LUACOV" == 1 ]]; then + "${DEPS_BUILD_DIR}/usr/bin/luarocks" install luacov +fi + rm -rf "${LOG_DIR}" mkdir -p "${LOG_DIR}" diff --git a/ci/common/submit_coverage.sh b/ci/common/submit_coverage.sh index 4e92975d22..c3e6be7f38 100755 --- a/ci/common/submit_coverage.sh +++ b/ci/common/submit_coverage.sh @@ -43,3 +43,11 @@ fi # Cleanup always, especially collected data. find . \( -name '*.gcov' -o -name '*.gcda' \) -ls -delete | wc -l rm -f coverage.xml + +# Upload Lua coverage (generated manually on AppVeyor/Windows). +if [ "$USE_LUACOV" = 1 ] && [ "$1" != "oldtest" ]; then + if ! "$codecov_sh" -f luacov.report.out -X gcov -X fix -Z -F "lua,${codecov_flags}"; then + echo "codecov upload failed." + fi + rm luacov.stats.out +fi From b4ea09cc064034f43808662c40cc1fff14284432 Mon Sep 17 00:00:00 2001 From: erw7 Date: Sat, 5 Oct 2019 00:18:24 +0900 Subject: [PATCH 0126/1293] Fix potential deadlock #11151 ELOG may call os_getenv and os_setenv internally. In that case, a deadlock occurs. --- src/nvim/os/env.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index 13853016d1..ae61e54993 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -55,6 +55,7 @@ const char *os_getenv(const char *name) return NULL; } uv_mutex_lock(&mutex); + int r = 0; if (pmap_has(cstr_t)(envmap, name) && !!(e = (char *)pmap_get(cstr_t)(envmap, name))) { if (e[0] != '\0') { @@ -67,7 +68,7 @@ const char *os_getenv(const char *name) pmap_del2(envmap, name); } e = xmalloc(size); - int r = uv_os_getenv(name, e, &size); + r = uv_os_getenv(name, e, &size); if (r == UV_ENOBUFS) { e = xrealloc(e, size); r = uv_os_getenv(name, e, &size); @@ -75,14 +76,15 @@ const char *os_getenv(const char *name) if (r != 0 || size == 0 || e[0] == '\0') { xfree(e); e = NULL; - if (r != 0 && r != UV_ENOENT && r != UV_UNKNOWN) { - ELOG("uv_os_getenv(%s) failed: %d %s", name, r, uv_err_name(r)); - } goto end; } pmap_put(cstr_t)(envmap, xstrdup(name), e); end: + // Must do this before ELOG, log.c may call os_setenv. uv_mutex_unlock(&mutex); + if (r != 0 && r != UV_ENOENT && r != UV_UNKNOWN) { + ELOG("uv_os_getenv(%s) failed: %d %s", name, r, uv_err_name(r)); + } return (e == NULL || size == 0 || e[0] == '\0') ? NULL : e; } @@ -146,10 +148,11 @@ int os_setenv(const char *name, const char *value, int overwrite) // Destroy the old map item. Do this AFTER uv_os_setenv(), because `value` // could be a previous os_getenv() result. pmap_del2(envmap, name); + // Must do this before ELOG, log.c may call os_setenv. + uv_mutex_unlock(&mutex); if (r != 0) { ELOG("uv_os_setenv(%s) failed: %d %s", name, r, uv_err_name(r)); } - uv_mutex_unlock(&mutex); return r == 0 ? 0 : -1; } @@ -163,10 +166,11 @@ int os_unsetenv(const char *name) uv_mutex_lock(&mutex); pmap_del2(envmap, name); int r = uv_os_unsetenv(name); + // Must do this before ELOG, log.c may call os_setenv. + uv_mutex_unlock(&mutex); if (r != 0) { ELOG("uv_os_unsetenv(%s) failed: %d %s", name, r, uv_err_name(r)); } - uv_mutex_unlock(&mutex); return r == 0 ? 0 : -1; } From 402afb08959c353a50e040dd0bdcc7cd7aa73041 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 4 Oct 2019 22:10:16 +0200 Subject: [PATCH 0127/1293] Makefile: use `$TMPDIR` below `src/nvim/testdir` (#11153) This makes it ignored/cleaned automatically. It was made absolute in 8821579ba, but to the root back then. --- src/nvim/testdir/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nvim/testdir/Makefile b/src/nvim/testdir/Makefile index d1a449c7cc..08353509af 100644 --- a/src/nvim/testdir/Makefile +++ b/src/nvim/testdir/Makefile @@ -11,7 +11,7 @@ ROOT := ../../.. export SHELL := sh export NVIM_PRG := $(NVIM_PRG) -export TMPDIR := $(abspath ../../../Xtest-tmpdir) +export TMPDIR := $(abspath Xtest-tmpdir) SCRIPTS_DEFAULT = \ test42.out \ From 5581ffac740d4a75809c6395da4ab757b8d7e6c8 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 5 Oct 2019 11:01:37 -0400 Subject: [PATCH 0128/1293] vim-patch:8.1.2113: ":help expr-!~?" only works after searching Problem: ":help expr-!~?" only works after searching. Solution: Escape "~" after "expr-". (closes vim/vim#5015) https://github.com/vim/vim/commit/9ca250855b55f4d3292b010525c827dc6992cb61 --- src/nvim/ex_cmds.c | 14 +++++++++++--- src/nvim/testdir/test_help.vim | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 16487ce447..d7ae522f32 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -4739,11 +4739,19 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, if (STRNICMP(arg, "expr-", 5) == 0) { // When the string starting with "expr-" and containing '?' and matches - // the table, it is taken literally. Otherwise '?' is recognized as a - // wildcard. + // the table, it is taken literally (but ~ is escaped). Otherwise '?' + // is recognized as a wildcard. for (i = (int)ARRAY_SIZE(expr_table); --i >= 0; ) { if (STRCMP(arg + 5, expr_table[i]) == 0) { - STRCPY(d, arg); + for (int si = 0, di = 0; ; si++) { + if (arg[si] == '~') { + d[di++] = '\\'; + } + d[di++] = arg[si]; + if (arg[si] == NUL) { + break; + } + } break; } } diff --git a/src/nvim/testdir/test_help.vim b/src/nvim/testdir/test_help.vim index ed3181564c..01fb9917e9 100644 --- a/src/nvim/testdir/test_help.vim +++ b/src/nvim/testdir/test_help.vim @@ -21,6 +21,12 @@ func Test_help_errors() bwipe! endfunc +func Test_help_expr() + help expr-!~? + call assert_equal('eval.txt', expand('%:t')) + close +endfunc + func Test_help_keyword() new set keywordprg=:help From 1396cc9abb0dfcdbd9572706235aba59f7c3318a Mon Sep 17 00:00:00 2001 From: Marvim the Paranoid Android Date: Sun, 6 Oct 2019 05:13:47 +0200 Subject: [PATCH 0129/1293] version.c: update [ci skip] #10981 vim-patch:8.0.0934: change to struts.h missing in patch vim-patch:8.0.1176: job_start() does not handle quote and backslash correctly vim-patch:8.0.1492: memory leak in balloon_split() vim-patch:8.0.1582: in the MS-Windows console mouse movement is not used vim-patch:8.0.1619: Win32 GUI: crash when winpty is not installed vim-patch:8.0.1624: options for term_dumpdiff() and term_dumpload() not implemented vim-patch:8.0.1665: when running a terminal from the GUI 'term' is not useful vim-patch:8.0.1666: % argument in ch_log() causes trouble vim-patch:8.0.1685: can't set ANSI colors of a terminal window vim-patch:8.0.1711: term_setsize() is not implemented yet vim-patch:8.0.1722: cannot specify a minimal size for a terminal window vim-patch:8.0.1725: terminal debugger doesn't handle command arguments vim-patch:8.0.1742: cannot get a list of all the jobs vim-patch:8.0.1798: MS-Windows: file considered read-only too often vim-patch:8.0.1835: print document name does not support multi-byte vim-patch:8.1.0080: can't see the breakpoint number in the terminal debugger vim-patch:8.1.0156: MS-Windows compiler warning vim-patch:8.1.0226: too many #ifdefs vim-patch:8.1.0722: cannot build without the virtualedit feature vim-patch:8.1.0745: compiler warnings for signed/unsigned string vim-patch:8.1.0752: one more compiler warning for signed/unsigned string vim-patch:8.1.2025: MS-Windows: Including shlguid.h causes problems for msys2 vim-patch:8.1.2027: MS-Windows: problem with ambiwidth characters vim-patch:8.1.2033: cannot build with tiny features vim-patch:8.1.2049: cannot build tiny version vim-patch:8.1.2061: MS-Windows GUI: ":sh" crashes when trying to use a terminal vim-patch:8.1.2075: get many log messages when waiting for a typed character vim-patch:8.1.2078: build error with +textprop but without +terminal vim-patch:8.1.2084: Amiga: cannot get the user name vim-patch:8.1.2086: missing a few changes for the renamed files vim-patch:8.1.2088: renamed libvterm mouse.c file not in distributed file list vim-patch:8.1.2090: not clear why channel log file ends vim-patch:8.1.2101: write_session_file() often defined but not used vim-patch:8.1.2102: can't build with GTK and FEAT_GUI_GNOME vim-patch:8.1.2112: build number for ConPTY is outdated The following `if_pyth` patch seems to be N/A. In `~/.local/`, python 2 and 3 have their own subfolders in `~/.local/include/` and `~/.local/lib/`. `PYTHONUSERBASE` is enough to make the user modules work (on my machine) for the legacy tests. vim-patch:8.0.1451: difficult to set the python home directories properly The following patch requires `set compatible` and unsupported `cpoptions`: vim-patch:8.1.1331: test 29 is old style --- src/nvim/version.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/nvim/version.c b/src/nvim/version.c index b0619d6273..b6122f6463 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -106,7 +106,7 @@ static const int included_patches[] = { 1815, 1814, 1813, - // 1812, + 1812, 1811, 1810, 1809, @@ -142,13 +142,13 @@ static const int included_patches[] = { 1779, 1778, 1777, - // 1776, + 1776, 1775, // 1774, 1773, - // 1772, - // 1771, - // 1770, + 1772, + 1771, + 1770, // 1769, 1768, // 1767, @@ -164,7 +164,7 @@ static const int included_patches[] = { 1757, 1756, 1755, - // 1754, + 1754, 1753, 1752, 1751, @@ -185,7 +185,7 @@ static const int included_patches[] = { 1736, 1735, 1734, - // 1733, + 1733, // 1732, 1731, 1730, @@ -297,7 +297,7 @@ static const int included_patches[] = { // 1624, 1623, 1622, - // 1621, + 1621, 1620, // 1619, 1618, @@ -379,7 +379,7 @@ static const int included_patches[] = { 1542, 1541, // 1540, - // 1539, + 1539, // 1538, 1537, 1536, @@ -389,13 +389,13 @@ static const int included_patches[] = { 1532, // 1531, 1530, - // 1529, + 1529, 1528, 1527, 1526, // 1525, 1524, - // 1523, + 1523, // 1522, 1521, // 1520, @@ -470,7 +470,7 @@ static const int included_patches[] = { // 1451, 1450, // 1449, - // 1448, + 1448, 1447, 1446, 1445, @@ -552,7 +552,7 @@ static const int included_patches[] = { 1369, 1368, // 1367, - // 1366, + 1366, 1365, 1364, 1363, @@ -784,7 +784,7 @@ static const int included_patches[] = { 1137, 1136, 1135, - // 1134, + 1134, 1133, 1132, 1131, @@ -809,7 +809,7 @@ static const int included_patches[] = { 1112, 1111, 1110, - // 1109, + 1109, 1108, 1107, 1106, @@ -880,7 +880,7 @@ static const int included_patches[] = { 1041, 1040, 1039, - // 1038, + 1038, 1037, 1036, 1035, @@ -948,7 +948,7 @@ static const int included_patches[] = { 973, 972, 971, - // 970, + 970, 969, 968, 967, @@ -977,11 +977,11 @@ static const int included_patches[] = { 944, 943, 942, - // 941, + 941, 940, 939, 938, - // 937, + 937, 936, 935, // 934, @@ -1004,7 +1004,7 @@ static const int included_patches[] = { 917, 916, 915, - // 914, + 914, 913, 912, 911, @@ -1093,7 +1093,7 @@ static const int included_patches[] = { 828, 827, 826, - // 825, + 825, 824, 823, 822, From fe074611cd5b3319a3f639f68289df6a718e64eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jurica=20Bradari=C4=87?= Date: Sun, 6 Oct 2019 05:35:48 +0200 Subject: [PATCH 0130/1293] vim-patch:8.1.1371: cannot recover from a swap file #11081 Problem: Cannot recover from a swap file. Solution: Do not expand environment variables in the swap file name. Do not check the extension when we already know a file is a swap file. (Ken Takata, closes 4415, closes vim/vim#4369) https://github.com/vim/vim/commit/99499b1c05f85f83876b828eea3f6e14f0f407b4 --- src/nvim/buffer.c | 5 +- src/nvim/ex_cmds.c | 4 +- src/nvim/ex_cmds2.c | 4 +- src/nvim/ex_docmd.c | 11 +++-- src/nvim/main.c | 16 ++++--- src/nvim/memline.c | 14 +++--- src/nvim/path.c | 11 +++-- src/nvim/path.h | 11 +++-- src/nvim/search.c | 3 +- src/nvim/spell.c | 10 ++-- src/nvim/spellfile.c | 5 +- src/nvim/tag.c | 3 +- src/nvim/testdir/test_swap.vim | 84 ++++++++++++++++++++++++++++++++++ test/unit/path_spec.lua | 4 +- 14 files changed, 143 insertions(+), 42 deletions(-) diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index e5b80693a4..b81ffd09e1 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -929,7 +929,7 @@ void handle_swap_exists(bufref_T *old_curbuf) // User selected Recover at ATTENTION prompt. msg_scroll = true; - ml_recover(); + ml_recover(false); MSG_PUTS("\n"); // don't overwrite the last message cmdline_row = msg_row; do_modelines(0); @@ -4629,7 +4629,8 @@ do_arg_all( if (i < alist->al_ga.ga_len && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum || path_full_compare(alist_name(&AARGLIST(alist)[i]), - buf->b_ffname, true) & kEqualFiles)) { + buf->b_ffname, + true, true) & kEqualFiles)) { int weight = 1; if (old_curtab == curtab) { diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 16487ce447..a3a08a5884 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -5023,7 +5023,7 @@ void fix_help_buffer(void) copy_option_part(&p, NameBuff, MAXPATHL, ","); char_u *const rt = (char_u *)vim_getenv("VIMRUNTIME"); if (rt != NULL - && path_full_compare(rt, NameBuff, false) != kEqualFiles) { + && path_full_compare(rt, NameBuff, false, true) != kEqualFiles) { int fcount; char_u **fnames; char_u *s; @@ -5233,7 +5233,7 @@ static void helptags_one(char_u *const dir, const char_u *const ext, ga_init(&ga, (int)sizeof(char_u *), 100); if (add_help_tags || path_full_compare((char_u *)"$VIMRUNTIME/doc", - dir, false) == kEqualFiles) { + dir, false, true) == kEqualFiles) { s = xmalloc(18 + STRLEN(tagfname)); sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); GA_APPEND(char_u *, &ga, s); diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 813d1a9b0b..87eae2dd4f 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -1743,7 +1743,7 @@ static bool editing_arg_idx(win_T *win) && (win->w_buffer->b_ffname == NULL || !(path_full_compare( alist_name(&WARGLIST(win)[win->w_arg_idx]), - win->w_buffer->b_ffname, true) & kEqualFiles)))); + win->w_buffer->b_ffname, true, true) & kEqualFiles)))); } /// Check if window "win" is editing the w_arg_idx file in its argument list. @@ -1761,7 +1761,7 @@ void check_arg_idx(win_T *win) && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum || (win->w_buffer->b_ffname != NULL && (path_full_compare(alist_name(&GARGLIST[GARGCOUNT - 1]), - win->w_buffer->b_ffname, true) + win->w_buffer->b_ffname, true, true) & kEqualFiles)))) { arg_had_last = true; } diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 34b4c10d3e..a6042b0e8c 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -6708,17 +6708,18 @@ static void ex_preserve(exarg_T *eap) /// ":recover". static void ex_recover(exarg_T *eap) { - /* Set recoverymode right away to avoid the ATTENTION prompt. */ - recoverymode = TRUE; + // Set recoverymode right away to avoid the ATTENTION prompt. + recoverymode = true; if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0) | CCGD_MULTWIN | (eap->forceit ? CCGD_FORCEIT : 0) | CCGD_EXCMD) && (*eap->arg == NUL - || setfname(curbuf, eap->arg, NULL, TRUE) == OK)) - ml_recover(); - recoverymode = FALSE; + || setfname(curbuf, eap->arg, NULL, true) == OK)) { + ml_recover(true); + } + recoverymode = false; } /* diff --git a/src/nvim/main.c b/src/nvim/main.c index be1f08bb46..ba15dcedad 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1460,12 +1460,13 @@ static void create_windows(mparm_T *parmp) } else parmp->window_count = 1; - if (recoverymode) { /* do recover */ - msg_scroll = TRUE; /* scroll message up */ - ml_recover(); - if (curbuf->b_ml.ml_mfp == NULL) /* failed */ + if (recoverymode) { // do recover + msg_scroll = true; // scroll message up + ml_recover(true); + if (curbuf->b_ml.ml_mfp == NULL) { // failed getout(1); - do_modelines(0); /* do modelines */ + } + do_modelines(0); // do modelines } else { // Open a buffer for windows that don't have one yet. // Commands in the vimrc might have loaded a file or split the window. @@ -1778,7 +1779,8 @@ static bool do_user_initialization(void) if (do_source(user_vimrc, true, DOSO_VIMRC) != FAIL) { do_exrc = p_exrc; if (do_exrc) { - do_exrc = (path_full_compare((char_u *)VIMRC_FILE, user_vimrc, false) + do_exrc = (path_full_compare((char_u *)VIMRC_FILE, user_vimrc, + false, true) != kEqualFiles); } xfree(user_vimrc); @@ -1805,7 +1807,7 @@ static bool do_user_initialization(void) do_exrc = p_exrc; if (do_exrc) { do_exrc = (path_full_compare((char_u *)VIMRC_FILE, (char_u *)vimrc, - false) != kEqualFiles); + false, true) != kEqualFiles); } xfree(vimrc); xfree(config_dirs); diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 15dd2767a2..f1d6ee064c 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -738,10 +738,10 @@ static void add_b0_fenc(ZERO_BL *b0p, buf_T *buf) } -/* - * Try to recover curbuf from the .swp file. - */ -void ml_recover(void) +/// Try to recover curbuf from the .swp file. +/// @param checkext If true, check the extension and detect whether it is a +/// swap file. +void ml_recover(bool checkext) { buf_T *buf = NULL; memfile_T *mfp = NULL; @@ -785,7 +785,7 @@ void ml_recover(void) if (fname == NULL) /* When there is no file name */ fname = (char_u *)""; len = (int)STRLEN(fname); - if (len >= 4 + if (checkext && len >= 4 && STRNICMP(fname + len - 4, ".s", 2) == 0 && vim_strchr((char_u *)"abcdefghijklmnopqrstuvw", TOLOWER_ASC(fname[len - 2])) != NULL @@ -1375,7 +1375,9 @@ recover_names ( if (curbuf->b_ml.ml_mfp != NULL && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL) { for (int i = 0; i < num_files; i++) { - if (path_full_compare(p, files[i], true) & kEqualFiles) { + // Do not expand wildcards, on Windows would try to expand + // "%tmp%" in "%tmp%file" + if (path_full_compare(p, files[i], true, false) & kEqualFiles) { // Remove the name from files[i]. Move further entries // down. When the array becomes empty free it here, since // FreeWild() won't be called below. diff --git a/src/nvim/path.c b/src/nvim/path.c index 1c787e3a1d..62d5d69d1a 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -51,9 +51,10 @@ /// expanded. /// @param s2 Second file name. /// @param checkname When both files don't exist, only compare their names. +/// @param expandenv Whether to expand environment variables in file names. /// @return Enum of type FileComparison. @see FileComparison. FileComparison path_full_compare(char_u *const s1, char_u *const s2, - const bool checkname) + const bool checkname, const bool expandenv) { assert(s1 && s2); char_u exp1[MAXPATHL]; @@ -61,7 +62,11 @@ FileComparison path_full_compare(char_u *const s1, char_u *const s2, char_u full2[MAXPATHL]; FileID file_id_1, file_id_2; - expand_env(s1, exp1, MAXPATHL); + if (expandenv) { + expand_env(s1, exp1, MAXPATHL); + } else { + xstrlcpy((char *)exp1, (const char *)s1, MAXPATHL - 1); + } bool id_ok_1 = os_fileid((char *)exp1, &file_id_1); bool id_ok_2 = os_fileid((char *)s2, &file_id_2); if (!id_ok_1 && !id_ok_2) { @@ -1203,7 +1208,7 @@ int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file, } } else { // First expand environment variables, "~/" and "~user/". - if (has_env_var(p) || *p == '~') { + if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~') { p = expand_env_save_opt(p, true); if (p == NULL) p = pat[i]; diff --git a/src/nvim/path.h b/src/nvim/path.h index 4e466d1b71..15abd19646 100644 --- a/src/nvim/path.h +++ b/src/nvim/path.h @@ -20,11 +20,12 @@ #define EW_KEEPDOLLAR 0x800 /* do not escape $, $var is expanded */ /* Note: mostly EW_NOTFOUND and EW_SILENT are mutually exclusive: EW_NOTFOUND * is used when executing commands and EW_SILENT for interactive expanding. */ -#define EW_ALLLINKS 0x1000 // also links not pointing to existing file -#define EW_SHELLCMD 0x2000 // called from expand_shellcmd(), don't check - // if executable is in $PATH -#define EW_DODOT 0x4000 // also files starting with a dot -#define EW_EMPTYOK 0x8000 // no matches is not an error +#define EW_ALLLINKS 0x1000 // also links not pointing to existing file +#define EW_SHELLCMD 0x2000 // called from expand_shellcmd(), don't check + // if executable is in $PATH +#define EW_DODOT 0x4000 // also files starting with a dot +#define EW_EMPTYOK 0x8000 // no matches is not an error +#define EW_NOTENV 0x10000 // do not expand environment variables /// Return value for the comparison of two files. Also @see path_full_compare. typedef enum file_comparison { diff --git a/src/nvim/search.c b/src/nvim/search.c index 7d1c19d68c..85c0d7eb48 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -4458,7 +4458,8 @@ find_pattern_in_path( if (i == max_path_depth) { break; } - if (path_full_compare(new_fname, files[i].name, true) & kEqualFiles) { + if (path_full_compare(new_fname, files[i].name, + true, true) & kEqualFiles) { if (type != CHECK_PATH && action == ACTION_SHOW_ALL && files[i].matched) { msg_putchar('\n'); // cursor below last one */ diff --git a/src/nvim/spell.c b/src/nvim/spell.c index 724a0332bc..ab40355a8a 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -2031,7 +2031,8 @@ char_u *did_set_spelllang(win_T *wp) // Check if we loaded this language before. for (slang = first_lang; slang != NULL; slang = slang->sl_next) { - if (path_full_compare(lang, slang->sl_fname, false) == kEqualFiles) { + if (path_full_compare(lang, slang->sl_fname, false, true) + == kEqualFiles) { break; } } @@ -2076,7 +2077,7 @@ char_u *did_set_spelllang(win_T *wp) // Loop over the languages, there can be several files for "lang". for (slang = first_lang; slang != NULL; slang = slang->sl_next) { if (filename - ? path_full_compare(lang, slang->sl_fname, false) == kEqualFiles + ? path_full_compare(lang, slang->sl_fname, false, true) == kEqualFiles : STRICMP(lang, slang->sl_name) == 0) { region_mask = REGION_ALL; if (!filename && region != NULL) { @@ -2129,7 +2130,7 @@ char_u *did_set_spelllang(win_T *wp) for (c = 0; c < ga.ga_len; ++c) { p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname; if (p != NULL - && path_full_compare(spf_name, p, false) == kEqualFiles) { + && path_full_compare(spf_name, p, false, true) == kEqualFiles) { break; } } @@ -2139,7 +2140,8 @@ char_u *did_set_spelllang(win_T *wp) // Check if it was loaded already. for (slang = first_lang; slang != NULL; slang = slang->sl_next) { - if (path_full_compare(spf_name, slang->sl_fname, false) == kEqualFiles) { + if (path_full_compare(spf_name, slang->sl_fname, false, true) + == kEqualFiles) { break; } } diff --git a/src/nvim/spellfile.c b/src/nvim/spellfile.c index 01daafa09e..eeec5be120 100644 --- a/src/nvim/spellfile.c +++ b/src/nvim/spellfile.c @@ -1787,7 +1787,7 @@ spell_reload_one ( bool didit = false; for (slang = first_lang; slang != NULL; slang = slang->sl_next) { - if (path_full_compare(fname, slang->sl_fname, false) == kEqualFiles) { + if (path_full_compare(fname, slang->sl_fname, false, true) == kEqualFiles) { slang_clear(slang); if (spell_load_file(fname, NULL, slang, false) == NULL) // reloading failed, clear the language @@ -4719,7 +4719,8 @@ static void spell_make_sugfile(spellinfo_T *spin, char_u *wfname) // of the code for the soundfolding stuff. // It might have been done already by spell_reload_one(). for (slang = first_lang; slang != NULL; slang = slang->sl_next) { - if (path_full_compare(wfname, slang->sl_fname, false) == kEqualFiles) { + if (path_full_compare(wfname, slang->sl_fname, false, true) + == kEqualFiles) { break; } } diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 91f3da1793..6fe3efbaae 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -2666,7 +2666,8 @@ static int test_for_current(char_u *fname, char_u *fname_end, char_u *tag_fname, *fname_end = NUL; } fullname = expand_tag_fname(fname, tag_fname, true); - retval = (path_full_compare(fullname, buf_ffname, true) & kEqualFiles); + retval = (path_full_compare(fullname, buf_ffname, true, true) + & kEqualFiles); xfree(fullname); *fname_end = c; } diff --git a/src/nvim/testdir/test_swap.vim b/src/nvim/testdir/test_swap.vim index 11eb324488..e072e9ed7f 100644 --- a/src/nvim/testdir/test_swap.vim +++ b/src/nvim/testdir/test_swap.vim @@ -221,3 +221,87 @@ func Test_swapfile_delete() augroup END augroup! test_swapfile_delete endfunc + +func Test_swap_recover() + autocmd! SwapExists + augroup test_swap_recover + autocmd! + autocmd SwapExists * let v:swapchoice = 'r' + augroup END + + + call mkdir('Xswap') + let $Xswap = 'foo' " Check for issue #4369. + set dir=Xswap// + " Create a valid swapfile by editing a file. + split Xswap/text + call setline(1, ['one', 'two', 'three']) + write " file is written, not modified + " read the swapfile as a Blob + let swapfile_name = swapname('%') + let swapfile_bytes = readfile(swapfile_name, 'B') + + " Close the file and recreate the swap file. + quit + call writefile(swapfile_bytes, swapfile_name) + " Edit the file again. This triggers recovery. + try + split Xswap/text + catch + " E308 should be caught, not E305. + call assert_exception('E308:') " Original file may have been changed + endtry + " The file should be recovered. + call assert_equal(['one', 'two', 'three'], getline(1, 3)) + quit! + + call delete('Xswap/text') + call delete(swapfile_name) + call delete('Xswap', 'd') + unlet $Xswap + set dir& + augroup test_swap_recover + autocmd! + augroup END + augroup! test_swap_recover +endfunc + +func Test_swap_recover_ext() + autocmd! SwapExists + augroup test_swap_recover_ext + autocmd! + autocmd SwapExists * let v:swapchoice = 'r' + augroup END + + + " Create a valid swapfile by editing a file with a special extension. + split Xtest.scr + call setline(1, ['one', 'two', 'three']) + write " file is written, not modified + write " write again to make sure the swapfile is created + " read the swapfile as a Blob + let swapfile_name = swapname('%') + let swapfile_bytes = readfile(swapfile_name, 'B') + + " Close and delete the file and recreate the swap file. + quit + call delete('Xtest.scr') + call writefile(swapfile_bytes, swapfile_name) + " Edit the file again. This triggers recovery. + try + split Xtest.scr + catch + " E308 should be caught, not E306. + call assert_exception('E308:') " Original file may have been changed + endtry + " The file should be recovered. + call assert_equal(['one', 'two', 'three'], getline(1, 3)) + quit! + + call delete('Xtest.scr') + call delete(swapfile_name) + augroup test_swap_recover_ext + autocmd! + augroup END + augroup! test_swap_recover_ext +endfunc diff --git a/test/unit/path_spec.lua b/test/unit/path_spec.lua index da52af1bf9..356c4997fa 100644 --- a/test/unit/path_spec.lua +++ b/test/unit/path_spec.lua @@ -66,10 +66,10 @@ describe('path.c', function() end) describe('path_full_compare', function() - local function path_full_compare(s1, s2, cn) + local function path_full_compare(s1, s2, cn, ee) s1 = to_cstr(s1) s2 = to_cstr(s2) - return cimp.path_full_compare(s1, s2, cn or 0) + return cimp.path_full_compare(s1, s2, cn or 0, ee or 1) end local f1 = 'f1.o' From 55007180a39e762dad7e80b7cd57fe4630e2e20a Mon Sep 17 00:00:00 2001 From: Vikram Pal Date: Sun, 6 Oct 2019 14:51:06 +0530 Subject: [PATCH 0131/1293] doc: Fix TEST_FILTER example #11158 --- test/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/README.md b/test/README.md index 2b19740434..2cee7da009 100644 --- a/test/README.md +++ b/test/README.md @@ -126,7 +126,7 @@ end) To run only test with filter name: - TEST_TAG='foo.*api' make functionaltest + TEST_FILTER='foo.*api' make functionaltest ### Filter by file From b007e5d8820c613606bdc3afcb49d7eecc14ea0b Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 5 Oct 2019 22:59:31 -0400 Subject: [PATCH 0132/1293] vim-patch:8.1.0059: displayed digraph for "ga" wrong with 'encoding' "cp1251" Problem: Displayed digraph for "ga" wrong with 'encoding' "cp1251". Solution: Convert from 'encoding' to "utf-8" if needed. (closes vim/vim#3015) https://github.com/vim/vim/commit/bc5020aa4d7ef4aea88395eff858f74fc881eab9 --- src/nvim/digraph.c | 3 ++- src/nvim/testdir/test_digraph.vim | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/nvim/digraph.c b/src/nvim/digraph.c index f6ec350488..427b5537f3 100644 --- a/src/nvim/digraph.c +++ b/src/nvim/digraph.c @@ -1450,8 +1450,9 @@ int do_digraph(int c) /// Find a digraph for "val". If found return the string to display it. /// If not found return NULL. -char_u *get_digraph_for_char(int val) +char_u *get_digraph_for_char(int val_arg) { + const int val = val_arg; digr_T *dp; static char_u r[3]; diff --git a/src/nvim/testdir/test_digraph.vim b/src/nvim/testdir/test_digraph.vim index 62a5da33df..5da05e85b5 100644 --- a/src/nvim/testdir/test_digraph.vim +++ b/src/nvim/testdir/test_digraph.vim @@ -465,4 +465,17 @@ func Test_show_digraph() bwipe! endfunc +func Test_show_digraph_cp1251() + throw 'skipped: Nvim supports ''utf8'' encoding only' + if !has('multi_byte') + return + endif + new + set encoding=cp1251 + call Put_Dig("='") + call assert_equal("\n<\xfa> <|z> 250, Hex fa, Oct 372, Digr ='", execute('ascii')) + set encoding=utf-8 + bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab From d1abd6513e95a41e41ad570038310087e97f3bb1 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 6 Oct 2019 08:03:47 -0400 Subject: [PATCH 0133/1293] vim-patch:8.1.0586: :digraph output is not easy to read Problem: :digraph output is not easy to read. Solution: Add highlighting for :digraphs. (Marcin Szamotulski, closes vim/vim#3572) Also add section headers for :digraphs!. https://github.com/vim/vim/commit/eae8ae1b2b4e532b125077d9838b70d966891be3 --- runtime/doc/digraph.txt | 4 +- src/nvim/digraph.c | 232 +++++++++++++++++++++++++++------------- src/nvim/ex_cmds.lua | 2 +- src/nvim/ex_docmd.c | 7 +- src/nvim/message.c | 10 +- 5 files changed, 170 insertions(+), 85 deletions(-) diff --git a/runtime/doc/digraph.txt b/runtime/doc/digraph.txt index b106e625f2..7f807b5eee 100644 --- a/runtime/doc/digraph.txt +++ b/runtime/doc/digraph.txt @@ -20,7 +20,9 @@ An alternative is using the 'keymap' option. 1. Defining digraphs *digraphs-define* *:dig* *:digraphs* -:dig[raphs] show currently defined digraphs. +:dig[raphs][!] Show currently defined digraphs. + With [!] headers are used to make it a bit easier to + find a specific character. *E104* *E39* :dig[raphs] {char1}{char2} {number} ... Add digraph {char1}{char2} to the list. {number} is diff --git a/src/nvim/digraph.c b/src/nvim/digraph.c index 427b5537f3..5a07137831 100644 --- a/src/nvim/digraph.c +++ b/src/nvim/digraph.c @@ -128,101 +128,154 @@ static digr_T digraphdefault[] = { 'P', 'M', 0x9e }, { 'A', 'C', 0x9f }, { 'N', 'S', 0xa0 }, +#define DG_START_LATIN 0xa1 { '!', 'I', 0xa1 }, + { '~', '!', 0xa1 }, // ¡ Vim 5.x compatible { 'C', 't', 0xa2 }, + { 'c', '|', 0xa2 }, // ¢ Vim 5.x compatible { 'P', 'd', 0xa3 }, + { '$', '$', 0xa3 }, // £ Vim 5.x compatible { 'C', 'u', 0xa4 }, + { 'o', 'x', 0xa4 }, // ¤ Vim 5.x compatible { 'Y', 'e', 0xa5 }, + { 'Y', '-', 0xa5 }, // ¥ Vim 5.x compatible { 'B', 'B', 0xa6 }, + { '|', '|', 0xa6 }, // ¦ Vim 5.x compatible { 'S', 'E', 0xa7 }, { '\'', ':', 0xa8 }, { 'C', 'o', 0xa9 }, + { 'c', 'O', 0xa9 }, // © Vim 5.x compatible { '-', 'a', 0xaa }, { '<', '<', 0xab }, { 'N', 'O', 0xac }, + { '-', ',', 0xac }, // ¬ Vim 5.x compatible { '-', '-', 0xad }, { 'R', 'g', 0xae }, { '\'', 'm', 0xaf }, + { '-', '=', 0xaf }, // ¯ Vim 5.x compatible { 'D', 'G', 0xb0 }, + { '~', 'o', 0xb0 }, // ° Vim 5.x compatible { '+', '-', 0xb1 }, { '2', 'S', 0xb2 }, + { '2', '2', 0xb2 }, // ² Vim 5.x compatible { '3', 'S', 0xb3 }, + { '3', '3', 0xb3 }, // ³ Vim 5.x compatible { '\'', '\'', 0xb4 }, { 'M', 'y', 0xb5 }, { 'P', 'I', 0xb6 }, + { 'p', 'p', 0xb6 }, // ¶ Vim 5.x compatible { '.', 'M', 0xb7 }, + { '~', '.', 0xb7 }, // · Vim 5.x compatible { '\'', ',', 0xb8 }, { '1', 'S', 0xb9 }, + { '1', '1', 0xb9 }, // ¹ Vim 5.x compatible { '-', 'o', 0xba }, { '>', '>', 0xbb }, { '1', '4', 0xbc }, { '1', '2', 0xbd }, { '3', '4', 0xbe }, { '?', 'I', 0xbf }, + { '~', '?', 0xbf }, // ¿ Vim 5.x compatible { 'A', '!', 0xc0 }, + { 'A', '`', 0xc0 }, // À Vim 5.x compatible { 'A', '\'', 0xc1 }, { 'A', '>', 0xc2 }, + { 'A', '^', 0xc2 }, // Â Vim 5.x compatible { 'A', '?', 0xc3 }, + { 'A', '~', 0xc3 }, // Ã Vim 5.x compatible { 'A', ':', 0xc4 }, + { 'A', '"', 0xc4 }, // Ä Vim 5.x compatible { 'A', 'A', 0xc5 }, + { 'A', '@', 0xc5 }, // Å Vim 5.x compatible { 'A', 'E', 0xc6 }, { 'C', ',', 0xc7 }, { 'E', '!', 0xc8 }, + { 'E', '`', 0xc8 }, // È Vim 5.x compatible { 'E', '\'', 0xc9 }, { 'E', '>', 0xca }, + { 'E', '^', 0xca }, // Ê Vim 5.x compatible { 'E', ':', 0xcb }, + { 'E', '"', 0xcb }, // Ë Vim 5.x compatible { 'I', '!', 0xcc }, + { 'I', '`', 0xcc }, // Ì Vim 5.x compatible { 'I', '\'', 0xcd }, { 'I', '>', 0xce }, + { 'I', '^', 0xce }, // Î Vim 5.x compatible { 'I', ':', 0xcf }, + { 'I', '"', 0xcf }, // Ï Vim 5.x compatible { 'D', '-', 0xd0 }, { 'N', '?', 0xd1 }, + { 'N', '~', 0xd1 }, // Ñ Vim 5.x compatible { 'O', '!', 0xd2 }, + { 'O', '`', 0xd2 }, // Ò Vim 5.x compatible { 'O', '\'', 0xd3 }, { 'O', '>', 0xd4 }, + { 'O', '^', 0xd4 }, // Ô Vim 5.x compatible { 'O', '?', 0xd5 }, + { 'O', '~', 0xd5 }, // Õ Vim 5.x compatible { 'O', ':', 0xd6 }, { '*', 'X', 0xd7 }, + { '/', '\\', 0xd7 }, // × Vim 5.x compatible { 'O', '/', 0xd8 }, { 'U', '!', 0xd9 }, + { 'U', '`', 0xd9 }, // Ù Vim 5.x compatible { 'U', '\'', 0xda }, { 'U', '>', 0xdb }, + { 'U', '^', 0xdb }, // Û Vim 5.x compatible { 'U', ':', 0xdc }, { 'Y', '\'', 0xdd }, { 'T', 'H', 0xde }, + { 'I', 'p', 0xde }, // Þ Vim 5.x compatible { 's', 's', 0xdf }, { 'a', '!', 0xe0 }, + { 'a', '`', 0xe0 }, // à Vim 5.x compatible { 'a', '\'', 0xe1 }, { 'a', '>', 0xe2 }, + { 'a', '^', 0xe2 }, // â Vim 5.x compatible { 'a', '?', 0xe3 }, + { 'a', '~', 0xe3 }, // ã Vim 5.x compatible { 'a', ':', 0xe4 }, + { 'a', '"', 0xe4 }, // ä Vim 5.x compatible { 'a', 'a', 0xe5 }, + { 'a', '@', 0xe5 }, // å Vim 5.x compatible { 'a', 'e', 0xe6 }, { 'c', ',', 0xe7 }, { 'e', '!', 0xe8 }, + { 'e', '`', 0xe8 }, // è Vim 5.x compatible { 'e', '\'', 0xe9 }, { 'e', '>', 0xea }, + { 'e', '^', 0xea }, // ê Vim 5.x compatible { 'e', ':', 0xeb }, + { 'e', '"', 0xeb }, // ë Vim 5.x compatible { 'i', '!', 0xec }, + { 'i', '`', 0xec }, // ì Vim 5.x compatible { 'i', '\'', 0xed }, { 'i', '>', 0xee }, + { 'i', '^', 0xee }, // î Vim 5.x compatible { 'i', ':', 0xef }, { 'd', '-', 0xf0 }, { 'n', '?', 0xf1 }, + { 'n', '~', 0xf1 }, // ñ Vim 5.x compatible { 'o', '!', 0xf2 }, + { 'o', '`', 0xf2 }, // ò Vim 5.x compatible { 'o', '\'', 0xf3 }, { 'o', '>', 0xf4 }, + { 'o', '^', 0xf4 }, // ô Vim 5.x compatible { 'o', '?', 0xf5 }, + { 'o', '~', 0xf5 }, // õ Vim 5.x compatible { 'o', ':', 0xf6 }, { '-', ':', 0xf7 }, { 'o', '/', 0xf8 }, { 'u', '!', 0xf9 }, + { 'u', '`', 0xf9 }, // ù Vim 5.x compatible { 'u', '\'', 0xfa }, { 'u', '>', 0xfb }, + { 'u', '^', 0xfb }, // û Vim 5.x compatible { 'u', ':', 0xfc }, { 'y', '\'', 0xfd }, { 't', 'h', 0xfe }, { 'y', ':', 0xff }, + { 'y', '"', 0xff }, // x XX Vim 5.x compatible { 'A', '-', 0x0100 }, { 'a', '-', 0x0101 }, @@ -397,6 +450,7 @@ static digr_T digraphdefault[] = { '\'', '0', 0x02da }, { '\'', ';', 0x02db }, { '\'', '"', 0x02dd }, +#define DG_START_GREEK 0x0386 { 'A', '%', 0x0386 }, { 'E', '%', 0x0388 }, { 'Y', '%', 0x0389 }, @@ -478,6 +532,7 @@ static digr_T digraphdefault[] = { 'p', '3', 0x03e1 }, { '\'', '%', 0x03f4 }, { 'j', '3', 0x03f5 }, +#define DG_START_CYRILLIC 0x0401 { 'I', 'O', 0x0401 }, { 'D', '%', 0x0402 }, { 'G', '%', 0x0403 }, @@ -582,6 +637,7 @@ static digr_T digraphdefault[] = { 'c', '3', 0x0481 }, { 'G', '3', 0x0490 }, { 'g', '3', 0x0491 }, +#define DG_START_HEBREW 0x05d0 { 'A', '+', 0x05d0 }, { 'B', '+', 0x05d1 }, { 'G', '+', 0x05d2 }, @@ -609,6 +665,7 @@ static digr_T digraphdefault[] = { 'R', '+', 0x05e8 }, { 'S', 'h', 0x05e9 }, { 'T', '+', 0x05ea }, +#define DG_START_ARABIC 0x060c { ',', '+', 0x060c }, { ';', '+', 0x061b }, { '?', '+', 0x061f }, @@ -671,6 +728,7 @@ static digr_T digraphdefault[] = { '7', 'a', 0x06f7 }, { '8', 'a', 0x06f8 }, { '9', 'a', 0x06f9 }, +#define DG_START_LATIN_EXTENDED 0x1e02 { 'B', '.', 0x1e02 }, { 'b', '.', 0x1e03 }, { 'B', '_', 0x1e06 }, @@ -722,7 +780,9 @@ static digr_T digraphdefault[] = { 'V', '?', 0x1e7c }, { 'v', '?', 0x1e7d }, { 'W', '!', 0x1e80 }, + { 'W', '`', 0x1e80 }, // extra alternative, easier to remember { 'w', '!', 0x1e81 }, + { 'w', '`', 0x1e81 }, // extra alternative, easier to remember { 'W', '\'', 0x1e82 }, { 'w', '\'', 0x1e83 }, { 'W', ':', 0x1e84 }, @@ -756,11 +816,14 @@ static digr_T digraphdefault[] = { 'U', '2', 0x1ee6 }, { 'u', '2', 0x1ee7 }, { 'Y', '!', 0x1ef2 }, + { 'Y', '`', 0x1ef2 }, // extra alternative, easier to remember { 'y', '!', 0x1ef3 }, + { 'y', '`', 0x1ef3 }, // extra alternative, easier to remember { 'Y', '2', 0x1ef6 }, { 'y', '2', 0x1ef7 }, { 'Y', '?', 0x1ef8 }, { 'y', '?', 0x1ef9 }, +#define DG_START_GREEK_EXTENDED 0x1f00 { ';', '\'', 0x1f00 }, { ',', '\'', 0x1f01 }, { ';', '!', 0x1f02 }, @@ -769,6 +832,7 @@ static digr_T digraphdefault[] = { '?', ',', 0x1f05 }, { '!', ':', 0x1f06 }, { '?', ':', 0x1f07 }, +#define DG_START_PUNCTUATION 0x2002 { '1', 'N', 0x2002 }, { '1', 'M', 0x2003 }, { '3', 'M', 0x2004 }, @@ -807,6 +871,7 @@ static digr_T digraphdefault[] = { ':', 'X', 0x203b }, { '\'', '-', 0x203e }, { '/', 'f', 0x2044 }, +#define DG_START_SUB_SUPER 0x2070 { '0', 'S', 0x2070 }, { '4', 'S', 0x2074 }, { '5', 'S', 0x2075 }, @@ -835,13 +900,15 @@ static digr_T digraphdefault[] = { '=', 's', 0x208c }, { '(', 's', 0x208d }, { ')', 's', 0x208e }, +#define DG_START_CURRENCY 0x20a4 { 'L', 'i', 0x20a4 }, { 'P', 't', 0x20a7 }, { 'W', '=', 0x20a9 }, - { '=', 'e', 0x20ac }, // euro - { 'E', 'u', 0x20ac }, // euro - { '=', 'R', 0x20bd }, // rouble - { '=', 'P', 0x20bd }, // rouble + { '=', 'e', 0x20ac }, // euro + { 'E', 'u', 0x20ac }, // euro + { '=', 'R', 0x20bd }, // rouble + { '=', 'P', 0x20bd }, // rouble +#define DG_START_OTHER1 0x2103 { 'o', 'C', 0x2103 }, { 'c', 'o', 0x2105 }, { 'o', 'F', 0x2109 }, @@ -864,6 +931,7 @@ static digr_T digraphdefault[] = { '3', '8', 0x215c }, { '5', '8', 0x215d }, { '7', '8', 0x215e }, +#define DG_START_ROMAN 0x2160 { '1', 'R', 0x2160 }, { '2', 'R', 0x2161 }, { '3', 'R', 0x2162 }, @@ -888,6 +956,7 @@ static digr_T digraphdefault[] = { 'a', 'r', 0x2179 }, { 'b', 'r', 0x217a }, { 'c', 'r', 0x217b }, +#define DG_START_ARROWS 0x2190 { '<', '-', 0x2190 }, { '-', '!', 0x2191 }, { '-', '>', 0x2192 }, @@ -897,6 +966,7 @@ static digr_T digraphdefault[] = { '<', '=', 0x21d0 }, { '=', '>', 0x21d2 }, { '=', '=', 0x21d4 }, +#define DG_START_MATH 0x2200 { 'F', 'A', 0x2200 }, { 'd', 'P', 0x2202 }, { 'T', 'E', 0x2203 }, @@ -954,6 +1024,7 @@ static digr_T digraphdefault[] = { '.', 'P', 0x22c5 }, { ':', '3', 0x22ee }, { '.', '3', 0x22ef }, +#define DG_START_TECHNICAL 0x2302 { 'E', 'h', 0x2302 }, { '<', '7', 0x2308 }, { '>', '7', 0x2309 }, @@ -966,6 +1037,7 @@ static digr_T digraphdefault[] = { 'I', 'l', 0x2321 }, { '<', '/', 0x2329 }, { '/', '>', 0x232a }, +#define DG_START_OTHER2 0x2423 { 'V', 's', 0x2423 }, { '1', 'h', 0x2440 }, { '3', 'h', 0x2441 }, @@ -984,6 +1056,7 @@ static digr_T digraphdefault[] = { '7', '.', 0x248e }, { '8', '.', 0x248f }, { '9', '.', 0x2490 }, +#define DG_START_DRAWING 0x2500 { 'h', 'h', 0x2500 }, { 'H', 'H', 0x2501 }, { 'v', 'v', 0x2502 }, @@ -1034,6 +1107,7 @@ static digr_T digraphdefault[] = { 'V', 'H', 0x254b }, { 'F', 'D', 0x2571 }, { 'B', 'D', 0x2572 }, +#define DG_START_BLOCK 0x2580 { 'T', 'B', 0x2580 }, { 'L', 'B', 0x2584 }, { 'F', 'B', 0x2588 }, @@ -1042,6 +1116,7 @@ static digr_T digraphdefault[] = { '.', 'S', 0x2591 }, { ':', 'S', 0x2592 }, { '?', 'S', 0x2593 }, +#define DG_START_SHAPES 0x25a0 { 'f', 'S', 0x25a0 }, { 'O', 'S', 0x25a1 }, { 'R', 'O', 0x25a2 }, @@ -1075,6 +1150,7 @@ static digr_T digraphdefault[] = { 'I', 'c', 0x25d9 }, { 'F', 'd', 0x25e2 }, { 'B', 'd', 0x25e3 }, +#define DG_START_SYMBOLS 0x2605 { '*', '2', 0x2605 }, { '*', '1', 0x2606 }, { '<', 'H', 0x261c }, @@ -1094,9 +1170,11 @@ static digr_T digraphdefault[] = { 'M', 'b', 0x266d }, { 'M', 'x', 0x266e }, { 'M', 'X', 0x266f }, +#define DG_START_DINGBATS 0x2713 { 'O', 'K', 0x2713 }, { 'X', 'X', 0x2717 }, { '-', 'X', 0x2720 }, +#define DG_START_CJK_SYMBOLS 0x3000 { 'I', 'S', 0x3000 }, { ',', '_', 0x3001 }, { '.', '_', 0x3002 }, @@ -1120,6 +1198,7 @@ static digr_T digraphdefault[] = { '(', 'I', 0x3016 }, { ')', 'I', 0x3017 }, { '-', '?', 0x301c }, +#define DG_START_HIRAGANA 0x3041 { 'A', '5', 0x3041 }, { 'a', '5', 0x3042 }, { 'I', '5', 0x3043 }, @@ -1208,6 +1287,7 @@ static digr_T digraphdefault[] = { '0', '5', 0x309c }, { '*', '5', 0x309d }, { '+', '5', 0x309e }, +#define DG_START_KATAKANA 0x30a1 { 'a', '6', 0x30a1 }, { 'A', '6', 0x30a2 }, { 'i', '6', 0x30a3 }, @@ -1302,6 +1382,7 @@ static digr_T digraphdefault[] = { '-', '6', 0x30fc }, { '*', '6', 0x30fd }, { '+', '6', 0x30fe }, +#define DG_START_BOPOMOFO 0x3105 { 'b', '4', 0x3105 }, { 'p', '4', 0x3106 }, { 'm', '4', 0x3107 }, @@ -1341,6 +1422,7 @@ static digr_T digraphdefault[] = { 'v', '4', 0x312a }, { 'n', 'G', 0x312b }, { 'g', 'n', 0x312c }, +#define DG_START_OTHER3 0x3220 { '1', 'c', 0x3220 }, { '2', 'c', 0x3221 }, { '3', 'c', 0x3222 }, @@ -1359,66 +1441,6 @@ static digr_T digraphdefault[] = { 'f', 't', 0xfb05 }, { 's', 't', 0xfb06 }, - // extra alternatives, easier to remember - { 'W', '`', 0x1e80 }, - { 'w', '`', 0x1e81 }, - { 'Y', '`', 0x1ef2 }, - { 'y', '`', 0x1ef3 }, - - // Vim 5.x compatible digraphs that don't conflict with the above - { '~', '!', 161 }, // ¡ - { 'c', '|', 162 }, // ¢ - { '$', '$', 163 }, // £ - { 'o', 'x', 164 }, // ¤ - currency symbol in ISO 8859-1 - { 'Y', '-', 165 }, // ¥ - { '|', '|', 166 }, // ¦ - { 'c', 'O', 169 }, // © - { '-', ',', 172 }, // ¬ - { '-', '=', 175 }, // ¯ - { '~', 'o', 176 }, // ° - { '2', '2', 178 }, // ² - { '3', '3', 179 }, // ³ - { 'p', 'p', 182 }, // ¶ - { '~', '.', 183 }, // · - { '1', '1', 185 }, // ¹ - { '~', '?', 191 }, // ¿ - { 'A', '`', 192 }, // À - { 'A', '^', 194 }, // Â - { 'A', '~', 195 }, // Ã - { 'A', '"', 196 }, // Ä - { 'A', '@', 197 }, // Å - { 'E', '`', 200 }, // È - { 'E', '^', 202 }, // Ê - { 'E', '"', 203 }, // Ë - { 'I', '`', 204 }, // Ì - { 'I', '^', 206 }, // Î - { 'I', '"', 207 }, // Ï - { 'N', '~', 209 }, // Ñ - { 'O', '`', 210 }, // Ò - { 'O', '^', 212 }, // Ô - { 'O', '~', 213 }, // Õ - { '/', '\\', 215 }, // × - multiplication symbol in ISO 8859-1 - { 'U', '`', 217 }, // Ù - { 'U', '^', 219 }, // Û - { 'I', 'p', 222 }, // Þ - { 'a', '`', 224 }, // à - { 'a', '^', 226 }, // â - { 'a', '~', 227 }, // ã - { 'a', '"', 228 }, // ä - { 'a', '@', 229 }, // å - { 'e', '`', 232 }, // è - { 'e', '^', 234 }, // ê - { 'e', '"', 235 }, // ë - { 'i', '`', 236 }, // ì - { 'i', '^', 238 }, // î - { 'n', '~', 241 }, // ñ - { 'o', '`', 242 }, // ò - { 'o', '^', 244 }, // ô - { 'o', '~', 245 }, // õ - { 'u', '`', 249 }, // ù - { 'u', '^', 251 }, // û - { 'y', '"', 255 }, // x XX - { NUL, NUL, NUL } }; @@ -1436,7 +1458,7 @@ int do_digraph(int c) backspaced = -1; } else if (p_dg) { if (backspaced >= 0) { - c = getdigraph(backspaced, c, FALSE); + c = getdigraph(backspaced, c, false); } backspaced = -1; @@ -1509,7 +1531,7 @@ int get_digraph(int cmdline) if (cc != ESC) { // ESC cancels CTRL-K - return getdigraph(c, cc, TRUE); + return getdigraph(c, cc, true); } } return NUL; @@ -1521,9 +1543,9 @@ int get_digraph(int cmdline) /// @param char2 /// @param meta_char /// -/// @return If no match, return "char2". If "meta_char" is TRUE and "char1" +/// @return If no match, return "char2". If "meta_char" is true and "char1" // is a space, return "char2" | 0x80. -static int getexactdigraph(int char1, int char2, int meta_char) +static int getexactdigraph(int char1, int char2, bool meta_char) { int retval = 0; @@ -1573,7 +1595,7 @@ static int getexactdigraph(int char1, int char2, int meta_char) /// @param meta_char /// /// @return The digraph. -int getdigraph(int char1, int char2, int meta_char) +int getdigraph(int char1, int char2, bool meta_char) { int retval; @@ -1643,9 +1665,20 @@ void putdigraph(char_u *str) } } -void listdigraphs(void) +static void digraph_header(const char *msg) + FUNC_ATTR_NONNULL_ALL +{ + if (msg_col > 0) { + msg_putchar('\n'); + } + msg_outtrans_attr((const char_u *)msg, HL_ATTR(HLF_CM)); + msg_putchar('\n'); +} + +void listdigraphs(bool use_headers) { digr_T *dp; + result_T previous = 0; msg_putchar('\n'); @@ -1657,25 +1690,63 @@ void listdigraphs(void) // May need to convert the result to 'encoding'. tmp.char1 = dp->char1; tmp.char2 = dp->char2; - tmp.result = getexactdigraph(tmp.char1, tmp.char2, FALSE); + tmp.result = getexactdigraph(tmp.char1, tmp.char2, false); if ((tmp.result != 0) && (tmp.result != tmp.char2)) { - printdigraph(&tmp); + printdigraph(&tmp, use_headers ? &previous : NULL); } dp++; fast_breakcheck(); } dp = (digr_T *)user_digraphs.ga_data; - for (int i = 0; i < user_digraphs.ga_len && !got_int; ++i) { - printdigraph(dp); + for (int i = 0; i < user_digraphs.ga_len && !got_int; i++) { + if (previous >= 0 && use_headers) { + digraph_header(_("Custom")); + } + previous = -1; + printdigraph(dp, NULL); fast_breakcheck(); dp++; } } -static void printdigraph(digr_T *dp) +struct dg_header_entry { + int dg_start; + const char *dg_header; +} header_table[] = { + { DG_START_LATIN, N_("Latin supplement") }, + { DG_START_GREEK, N_("Greek and Coptic") }, + { DG_START_CYRILLIC, N_("Cyrillic") }, + { DG_START_HEBREW, N_("Hebrew") }, + { DG_START_ARABIC, N_("Arabic") }, + { DG_START_LATIN_EXTENDED, N_("Latin extended") }, + { DG_START_GREEK_EXTENDED, N_("Greek extended") }, + { DG_START_PUNCTUATION, N_("Punctuation") }, + { DG_START_SUB_SUPER, N_("Super- and subscripts") }, + { DG_START_CURRENCY, N_("Currency") }, + { DG_START_OTHER1, N_("Other") }, + { DG_START_ROMAN, N_("Roman numbers") }, + { DG_START_ARROWS, N_("Arrows") }, + { DG_START_MATH, N_("Mathematical operators") }, + { DG_START_TECHNICAL, N_("Technical") }, + { DG_START_OTHER2, N_("Other") }, + { DG_START_DRAWING, N_("Box drawing") }, + { DG_START_BLOCK, N_("Block elements") }, + { DG_START_SHAPES, N_("Geometric shapes") }, + { DG_START_SYMBOLS, N_("Symbols") }, + { DG_START_DINGBATS, N_("Dingbats") }, + { DG_START_CJK_SYMBOLS, N_("CJK symbols and punctuation") }, + { DG_START_HIRAGANA, N_("Hiragana") }, + { DG_START_KATAKANA, N_("Katakana") }, + { DG_START_BOPOMOFO, N_("Bopomofo") }, + { DG_START_OTHER3, N_("Other") }, + { 0xfffffff, NULL }, +}; + +static void printdigraph(const digr_T *dp, result_T *previous) + FUNC_ATTR_NONNULL_ARG(1) { char_u buf[30]; char_u *p; @@ -1685,6 +1756,17 @@ static void printdigraph(digr_T *dp) list_width = 13; if (dp->result != 0) { + if (previous != NULL) { + for (int i = 0; header_table[i].dg_header != NULL; i++) { + if (*previous < header_table[i].dg_start + && dp->result >= header_table[i].dg_start + && dp->result < header_table[i + 1].dg_start) { + digraph_header(_(header_table[i].dg_header)); + break; + } + } + *previous = dp->result; + } if (msg_col > Columns - list_width) { msg_putchar('\n'); } diff --git a/src/nvim/ex_cmds.lua b/src/nvim/ex_cmds.lua index a709acd4ef..6317ec77ff 100644 --- a/src/nvim/ex_cmds.lua +++ b/src/nvim/ex_cmds.lua @@ -758,7 +758,7 @@ return { }, { command='digraphs', - flags=bit.bor(EXTRA, TRLBAR, CMDWIN), + flags=bit.bor(BANG, EXTRA, TRLBAR, CMDWIN), addr_type=ADDR_LINES, func='ex_digraphs', }, diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 34b4c10d3e..29b63c3f99 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -10010,10 +10010,11 @@ static void ex_setfiletype(exarg_T *eap) static void ex_digraphs(exarg_T *eap) { - if (*eap->arg != NUL) + if (*eap->arg != NUL) { putdigraph(eap->arg); - else - listdigraphs(); + } else { + listdigraphs(eap->forceit); + } } static void ex_set(exarg_T *eap) diff --git a/src/nvim/message.c b/src/nvim/message.c index 30e906cd5f..b518664f32 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -238,7 +238,7 @@ void msg_multiline_attr(const char *s, int attr, bool check_int) if (next_spec != NULL) { // Printing all char that are before the char found by strpbrk - msg_outtrans_len_attr((char_u *)s, next_spec - s, attr); + msg_outtrans_len_attr((const char_u *)s, next_spec - s, attr); if (*next_spec != TAB) { msg_clr_eos(); @@ -1384,12 +1384,12 @@ int msg_outtrans(char_u *str) return msg_outtrans_attr(str, 0); } -int msg_outtrans_attr(char_u *str, int attr) +int msg_outtrans_attr(const char_u *str, int attr) { return msg_outtrans_len_attr(str, (int)STRLEN(str), attr); } -int msg_outtrans_len(char_u *str, int len) +int msg_outtrans_len(const char_u *str, int len) { return msg_outtrans_len_attr(str, len, 0); } @@ -1402,7 +1402,7 @@ char_u *msg_outtrans_one(char_u *p, int attr) { int l; - if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) { + if ((l = utfc_ptr2len(p)) > 1) { msg_outtrans_len_attr(p, l, attr); return p + l; } @@ -1410,7 +1410,7 @@ char_u *msg_outtrans_one(char_u *p, int attr) return p + 1; } -int msg_outtrans_len_attr(char_u *msgstr, int len, int attr) +int msg_outtrans_len_attr(const char_u *msgstr, int len, int attr) { int retval = 0; const char *str = (const char *)msgstr; From e452988960b11e971d839c27dcd2f42e68f3aa4d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 6 Oct 2019 22:26:54 +0200 Subject: [PATCH 0134/1293] tests/functional: keep $TMPDIR in env (#11163) --- test/functional/helpers.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index bd36bac062..c195983e93 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -437,6 +437,7 @@ function module.new_argv(...) 'NVIM_LOG_FILE', 'NVIM_RPLUGIN_MANIFEST', 'GCOV_ERROR_FILE', + 'TMPDIR', }) do if not env_tbl[k] then env_tbl[k] = os.getenv(k) From 2b08dd8f062c3e0dc1c01c1b695a7dd282b8ff21 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 6 Oct 2019 22:47:40 +0200 Subject: [PATCH 0135/1293] tests: retry: "wait() evaluates the condition on given interval" (#11155) Ref: https://github.com/neovim/neovim/issues/11137 --- test/functional/eval/wait_spec.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/functional/eval/wait_spec.lua b/test/functional/eval/wait_spec.lua index ad7d1574cb..ee95e02a7f 100644 --- a/test/functional/eval/wait_spec.lua +++ b/test/functional/eval/wait_spec.lua @@ -58,8 +58,11 @@ describe('wait()', function() endfunction ]]) - nvim('set_var', 'counter', 0) - eq(-1, call('wait', 20, 'Count() >= 5', 99999)) + -- XXX: flaky (#11137) + helpers.retry(nil, nil, function() + nvim('set_var', 'counter', 0) + eq(-1, call('wait', 20, 'Count() >= 5', 99999)) + end) nvim('set_var', 'counter', 0) eq(0, call('wait', 10000, 'Count() >= 5', 5)) From 7a3602378fafc426dcefc3b94f75467d4df12d24 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 4 Oct 2019 16:23:48 +0200 Subject: [PATCH 0136/1293] ci: upgrade tree-sitter from 0.15.2 to 0.15.9 tree-sitter-c is still at 0.15.2 though. --- ci/build.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/build.ps1 b/ci/build.ps1 index 4e1a69376b..0a533e9d07 100644 --- a/ci/build.ps1 +++ b/ci/build.ps1 @@ -104,10 +104,10 @@ $client = new-object System.Net.WebClient cd c:\treesitter if ($bits -eq 32) { - $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.5/tree-sitter-windows-x86.gz","c:\treesitter\tree-sitter-cli.gz") + $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.9/tree-sitter-windows-x86.gz","c:\treesitter\tree-sitter-cli.gz") } elseif ($bits -eq 64) { - $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.5/tree-sitter-windows-x64.gz","c:\treesitter\tree-sitter-cli.gz") + $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.9/tree-sitter-windows-x64.gz","c:\treesitter\tree-sitter-cli.gz") } python -c "import gzip, shutil; f1,f2 = gzip.open('tree-sitter-cli.gz', 'rb'), open('tree-sitter.exe', 'wb'); shutil.copyfileobj(f1, f2); f2.close()" From e9b420dba5119536558f659d598546176d77070e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 4 Oct 2019 16:26:26 +0200 Subject: [PATCH 0137/1293] lint --- ci/build.ps1 | 6 +++--- ci/install.sh | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ci/build.ps1 b/ci/build.ps1 index 0a533e9d07..07da6c4e19 100644 --- a/ci/build.ps1 +++ b/ci/build.ps1 @@ -104,12 +104,12 @@ $client = new-object System.Net.WebClient cd c:\treesitter if ($bits -eq 32) { - $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.9/tree-sitter-windows-x86.gz","c:\treesitter\tree-sitter-cli.gz") + $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.9/tree-sitter-windows-x86.gz", "c:\treesitter\tree-sitter-cli.gz") } elseif ($bits -eq 64) { - $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.9/tree-sitter-windows-x64.gz","c:\treesitter\tree-sitter-cli.gz") + $client.DownloadFile("https://github.com/tree-sitter/tree-sitter/releases/download/0.15.9/tree-sitter-windows-x64.gz", "c:\treesitter\tree-sitter-cli.gz") } -python -c "import gzip, shutil; f1,f2 = gzip.open('tree-sitter-cli.gz', 'rb'), open('tree-sitter.exe', 'wb'); shutil.copyfileobj(f1, f2); f2.close()" +python -c "import gzip, shutil; f1,f2 = gzip.open('tree-sitter-cli.gz', 'rb'), open('tree-sitter.exe', 'wb'); shutil.copyfileobj(f1, f2); f2.close()" $client.DownloadFile("https://codeload.github.com/tree-sitter/tree-sitter-c/zip/v0.15.2","c:\treesitter\tree_sitter_c.zip") Expand-Archive c:\treesitter\tree_sitter_c.zip -DestinationPath c:\treesitter\ diff --git a/ci/install.sh b/ci/install.sh index b96cf3c073..21175f63e0 100755 --- a/ci/install.sh +++ b/ci/install.sh @@ -34,7 +34,7 @@ curl "https://codeload.github.com/tree-sitter/tree-sitter-c/tar.gz/v0.15.2" -o t tar xf tree_sitter_c.tar.gz cd tree-sitter-c-0.15.2 export TREE_SITTER_DIR=$HOME/tree-sitter-build/ -mkdir -p $TREE_SITTER_DIR/bin +mkdir -p "$TREE_SITTER_DIR/bin" if [[ "$BUILD_32BIT" != "ON" ]]; then # builds c parser in $HOME/tree-sitter-build/bin/c.(so|dylib) @@ -42,5 +42,5 @@ if [[ "$BUILD_32BIT" != "ON" ]]; then else # no tree-sitter binary for 32bit linux, so fake it (no tree-sitter unit tests) cd src/ - gcc -m32 -o $TREE_SITTER_DIR/bin/c.so -shared parser.c -I. + gcc -m32 -o "$TREE_SITTER_DIR/bin/c.so" -shared parser.c -I. fi From c8fe2a8d23978b1faf9f03af476dddbccf0c76f7 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 6 Oct 2019 23:35:52 +0200 Subject: [PATCH 0138/1293] test/old: add test_fnamemodify.vim (#11168) Moved to a new-style test in vim/vim@610cc1b9b (v7.4.1652). Ref: https://github.com/neovim/neovim/pull/11165#issuecomment-538785588 --- src/nvim/testdir/test_fnamemodify.vim | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/nvim/testdir/test_fnamemodify.vim diff --git a/src/nvim/testdir/test_fnamemodify.vim b/src/nvim/testdir/test_fnamemodify.vim new file mode 100644 index 0000000000..63f273677d --- /dev/null +++ b/src/nvim/testdir/test_fnamemodify.vim @@ -0,0 +1,47 @@ +" Test filename modifiers. + +func Test_fnamemodify() + let save_home = $HOME + let save_shell = &shell + let $HOME = fnamemodify('.', ':p:h:h') + set shell=sh + + call assert_equal('/', fnamemodify('.', ':p')[-1:]) + call assert_equal('r', fnamemodify('.', ':p:h')[-1:]) + call assert_equal('t', fnamemodify('test.out', ':p')[-1:]) + call assert_equal('test.out', fnamemodify('test.out', ':.')) + call assert_equal('a', fnamemodify('../testdir/a', ':.')) + call assert_equal('~/testdir/test.out', fnamemodify('test.out', ':~')) + call assert_equal('~/testdir/a', fnamemodify('../testdir/a', ':~')) + call assert_equal('a', fnamemodify('../testdir/a', ':t')) + call assert_equal('', fnamemodify('.', ':p:t')) + call assert_equal('test.out', fnamemodify('test.out', ':p:t')) + call assert_equal('out', fnamemodify('test.out', ':p:e')) + call assert_equal('out', fnamemodify('test.out', ':p:t:e')) + call assert_equal('abc.fb2.tar', fnamemodify('abc.fb2.tar.gz', ':r')) + call assert_equal('abc.fb2', fnamemodify('abc.fb2.tar.gz', ':r:r')) + call assert_equal('abc', fnamemodify('abc.fb2.tar.gz', ':r:r:r')) + call assert_equal('testdir/abc.fb2', substitute(fnamemodify('abc.fb2.tar.gz', ':p:r:r'), '.*\(testdir/.*\)', '\1', '')) + call assert_equal('gz', fnamemodify('abc.fb2.tar.gz', ':e')) + call assert_equal('tar.gz', fnamemodify('abc.fb2.tar.gz', ':e:e')) + call assert_equal('fb2.tar.gz', fnamemodify('abc.fb2.tar.gz', ':e:e:e')) + call assert_equal('fb2.tar.gz', fnamemodify('abc.fb2.tar.gz', ':e:e:e:e')) + call assert_equal('tar', fnamemodify('abc.fb2.tar.gz', ':e:e:r')) + + call assert_equal('''abc def''', fnamemodify('abc def', ':S')) + call assert_equal('''abc" "def''', fnamemodify('abc" "def', ':S')) + call assert_equal('''abc"%"def''', fnamemodify('abc"%"def', ':S')) + call assert_equal('''abc''\'''' ''\''''def''', fnamemodify('abc'' ''def', ':S')) + call assert_equal('''abc''\''''%''\''''def''', fnamemodify('abc''%''def', ':S')) + sp test_alot.vim + call assert_equal(expand('%:r:S'), shellescape(expand('%:r'))) + call assert_equal('test_alot,''test_alot'',test_alot.vim', join([expand('%:r'), expand('%:r:S'), expand('%')], ',')) + quit + + call assert_equal("'abc\ndef'", fnamemodify("abc\ndef", ':S')) + set shell=tcsh + call assert_equal("'abc\\\ndef'", fnamemodify("abc\ndef", ':S')) + + let $HOME = save_home + let &shell = save_shell +endfunc From 8f20c50caa7fa008f5e6257ef0fc43620e3baeb1 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 7 Oct 2019 00:44:54 +0200 Subject: [PATCH 0139/1293] ci: submit_coverage: run luacov actually (#11169) Apparently this got lost with #11127 / 77a551b65. --- ci/common/submit_coverage.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ci/common/submit_coverage.sh b/ci/common/submit_coverage.sh index c3e6be7f38..9c7887de0b 100755 --- a/ci/common/submit_coverage.sh +++ b/ci/common/submit_coverage.sh @@ -46,6 +46,9 @@ rm -f coverage.xml # Upload Lua coverage (generated manually on AppVeyor/Windows). if [ "$USE_LUACOV" = 1 ] && [ "$1" != "oldtest" ]; then + if [ -x "${DEPS_BUILD_DIR}/usr/bin/luacov" ]; then + "${DEPS_BUILD_DIR}/usr/bin/luacov" + fi if ! "$codecov_sh" -f luacov.report.out -X gcov -X fix -Z -F "lua,${codecov_flags}"; then echo "codecov upload failed." fi From 09232958ff9c7d4737701160549c8d64a0f92856 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 6 Oct 2019 20:52:30 -0400 Subject: [PATCH 0140/1293] vim-patch:8.1.2120: some MB_ macros are more complicated than necessary Problem: Some MB_ macros are more complicated than necessary. (Dominique Pelle) Solution: Simplify the macros. Expand inline. https://github.com/vim/vim/commit/1614a14901558ca091329315d14a7d5e1b53aa47 --- src/nvim/diff.c | 2 +- src/nvim/eval.c | 6 +++--- src/nvim/ex_getln.c | 2 +- src/nvim/file_search.c | 4 ++-- src/nvim/getchar.c | 12 ++++++------ src/nvim/macros.h | 2 -- src/nvim/ops.c | 2 +- src/nvim/path.c | 24 +++++++++++++----------- src/nvim/regexp_nfa.c | 2 +- src/nvim/screen.c | 2 +- src/nvim/search.c | 18 +++++++++--------- src/nvim/spell.c | 30 +++++++++++++++--------------- 12 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/nvim/diff.c b/src/nvim/diff.c index db3ef7ac47..34cac5a098 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -724,7 +724,7 @@ static int diff_write_buffer(buf_T *buf, diffin_T *din) c = PTR2CHAR(s); c = enc_utf8 ? utf_fold(c) : TOLOWER_LOC(c); - orig_len = MB_PTR2LEN(s); + orig_len = utfc_ptr2len(s); if (utf_char2bytes(c, cbuf) != orig_len) { // TODO(Bram): handle byte length difference memmove(ptr + len, s, orig_len); diff --git a/src/nvim/eval.c b/src/nvim/eval.c index e3e5bb9a90..925b64d42c 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -15502,7 +15502,7 @@ static void f_setcharsearch(typval_T *argvars, typval_T *rettv, FunPtr fptr) } else set_last_csearch(PTR2CHAR(csearch), - csearch, MB_PTR2LEN(csearch)); + csearch, utfc_ptr2len(csearch)); } di = tv_dict_find(d, S_LEN("forward")); @@ -24051,8 +24051,8 @@ char_u *do_string_sub(char_u *str, char_u *pat, char_u *sub, /* Skip empty match except for first match. */ if (regmatch.startp[0] == regmatch.endp[0]) { if (zero_width == regmatch.startp[0]) { - /* avoid getting stuck on a match with an empty string */ - int i = MB_PTR2LEN(tail); + // avoid getting stuck on a match with an empty string + int i = utfc_ptr2len(tail); memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); ga.ga_len += i; tail += i; diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 3f7bc45c2e..d0af8a0fdf 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2320,7 +2320,7 @@ redraw: } while (++vcol % 8); p++; } else { - len = MB_PTR2LEN(p); + len = utfc_ptr2len(p); msg_outtrans_len(p, len); vcol += ptr2cells(p); p += len; diff --git a/src/nvim/file_search.c b/src/nvim/file_search.c index ad6a481bc5..47272df2f0 100644 --- a/src/nvim/file_search.c +++ b/src/nvim/file_search.c @@ -1102,8 +1102,8 @@ static bool ff_wc_equal(char_u *s1, char_u *s2) prev2 = prev1; prev1 = c1; - i += MB_PTR2LEN(s1 + i); - j += MB_PTR2LEN(s2 + j); + i += utfc_ptr2len(s1 + i); + j += utfc_ptr2len(s2 + j); } return s1[i] == s2[j]; } diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index af642a8e11..1f82df3241 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -1833,13 +1833,13 @@ static int vgetorpeek(int advance) char_u *p1 = mp->m_keys; char_u *p2 = (char_u *)mb_unescape((const char **)&p1); - if (has_mbyte && p2 != NULL && MB_BYTE2LEN(c1) > MB_PTR2LEN(p2)) + if (p2 != NULL && MB_BYTE2LEN(c1) > utfc_ptr2len(p2)) { mlen = 0; - /* - * Check an entry whether it matches. - * - Full match: mlen == keylen - * - Partly match: mlen == typebuf.tb_len - */ + } + + // Check an entry whether it matches. + // - Full match: mlen == keylen + // - Partly match: mlen == typebuf.tb_len keylen = mp->m_keylen; if (mlen == keylen || (mlen == typebuf.tb_len diff --git a/src/nvim/macros.h b/src/nvim/macros.h index d11507fa6a..f6c8c0a4a0 100644 --- a/src/nvim/macros.h +++ b/src/nvim/macros.h @@ -110,8 +110,6 @@ // MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers. // PTR2CHAR(): get character from pointer. -// Get the length of the character p points to, including composing chars. -# define MB_PTR2LEN(p) mb_ptr2len(p) // Advance multi-byte pointer, skip over composing chars. # define MB_PTR_ADV(p) (p += mb_ptr2len((char_u *)p)) // Advance multi-byte pointer, do not skip over composing chars. diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 4f0c1b5cb9..6f515151d6 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -4641,7 +4641,7 @@ int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1) if (visual) { while (ptr[col] != NUL && length > 0 && !ascii_isdigit(ptr[col]) && !(doalp && ASCII_ISALPHA(ptr[col]))) { - int mb_len = MB_PTR2LEN(ptr + col); + int mb_len = utfc_ptr2len(ptr + col); col += mb_len; length -= mb_len; diff --git a/src/nvim/path.c b/src/nvim/path.c index 62d5d69d1a..a53870acb8 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -338,9 +338,9 @@ int path_fnamencmp(const char *const fname1, const char *const fname2, && (p_fic ? (c1 != c2 && CH_FOLD(c1) != CH_FOLD(c2)) : c1 != c2)) { break; } - len -= (size_t)MB_PTR2LEN((const char_u *)p1); - p1 += MB_PTR2LEN((const char_u *)p1); - p2 += MB_PTR2LEN((const char_u *)p2); + len -= (size_t)utfc_ptr2len((const char_u *)p1); + p1 += utfc_ptr2len((const char_u *)p1); + p2 += utfc_ptr2len((const char_u *)p2); } return c1 - c2; #else @@ -1910,16 +1910,16 @@ int pathcmp(const char *p, const char *q, int maxlen) : c1 - c2; // no match } - i += MB_PTR2LEN((char_u *)p + i); - j += MB_PTR2LEN((char_u *)q + j); + i += utfc_ptr2len((char_u *)p + i); + j += utfc_ptr2len((char_u *)q + j); } if (s == NULL) { // "i" or "j" ran into "maxlen" return 0; } c1 = PTR2CHAR((char_u *)s + i); - c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i)); - /* ignore a trailing slash, but not "//" or ":/" */ + c2 = PTR2CHAR((char_u *)s + i + utfc_ptr2len((char_u *)s + i)); + // ignore a trailing slash, but not "//" or ":/" if (c2 == NUL && i > 0 && !after_pathsep((char *)s, (char *)s + i) @@ -1928,10 +1928,12 @@ int pathcmp(const char *p, const char *q, int maxlen) #else && c1 == '/' #endif - ) - return 0; /* match with trailing slash */ - if (s == q) - return -1; /* no match */ + ) { + return 0; // match with trailing slash + } + if (s == q) { + return -1; // no match + } return 1; } diff --git a/src/nvim/regexp_nfa.c b/src/nvim/regexp_nfa.c index 63640e723e..f33c61d39f 100644 --- a/src/nvim/regexp_nfa.c +++ b/src/nvim/regexp_nfa.c @@ -4943,7 +4943,7 @@ static int skip_to_start(int c, colnr_T *colp) */ static long find_match_text(colnr_T startcol, int regstart, char_u *match_text) { -#define PTR2LEN(x) enc_utf8 ? utf_ptr2len(x) : MB_PTR2LEN(x) +#define PTR2LEN(x) utf_ptr2len(x) colnr_T col = startcol; int regstart_len = PTR2LEN(regline + startcol); diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 488341c6a7..1ce0b5217e 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -2994,7 +2994,7 @@ win_line ( if (shl->startcol != MAXCOL && v >= (long)shl->startcol && v < (long)shl->endcol) { - int tmp_col = v + MB_PTR2LEN(ptr); + int tmp_col = v + utfc_ptr2len(ptr); if (shl->endcol < tmp_col) { shl->endcol = tmp_col; diff --git a/src/nvim/search.c b/src/nvim/search.c index 85c0d7eb48..1f382d31c5 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -681,7 +681,7 @@ int searchit( } if (matchcol == matchpos.col && ptr[matchcol] != NUL) { - matchcol += MB_PTR2LEN(ptr + matchcol); + matchcol += utfc_ptr2len(ptr + matchcol); } if (matchcol == 0 && (options & SEARCH_START)) { @@ -1755,7 +1755,7 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel) find_mps_values(&initc, &findc, &backwards, FALSE); if (findc) break; - pos.col += MB_PTR2LEN(linep + pos.col); + pos.col += utfc_ptr2len(linep + pos.col); } if (!findc) { /* no brace in the line, maybe use " #if" then */ @@ -2234,14 +2234,14 @@ showmatch( for (p = curbuf->b_p_mps; *p != NUL; ++p) { if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri)) break; - p += MB_PTR2LEN(p) + 1; - if (PTR2CHAR(p) == c - && !(curwin->w_p_rl ^ p_ri) - ) + p += utfc_ptr2len(p) + 1; + if (PTR2CHAR(p) == c && !(curwin->w_p_rl ^ p_ri)) { break; - p += MB_PTR2LEN(p); - if (*p == NUL) + } + p += utfc_ptr2len(p); + if (*p == NUL) { return; + } } if ((lpos = findmatch(NULL, NUL)) == NULL) { // no match, so beep @@ -4845,7 +4845,7 @@ exit_matched: && action == ACTION_EXPAND && !(compl_cont_status & CONT_SOL) && *startp != NUL - && *(p = startp + MB_PTR2LEN(startp)) != NUL) + && *(p = startp + utfc_ptr2len(startp)) != NUL) goto search_line; } line_breakcheck(); diff --git a/src/nvim/spell.c b/src/nvim/spell.c index ab40355a8a..503657c427 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -2577,7 +2577,7 @@ static bool spell_iswordp(char_u *p, win_T *wp) int c; if (has_mbyte) { - l = MB_PTR2LEN(p); + l = utfc_ptr2len(p); s = p; if (l == 1) { // be quick for ASCII @@ -4118,7 +4118,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so && goodword_ends) { int l; - l = MB_PTR2LEN(fword + sp->ts_fidx); + l = utfc_ptr2len(fword + sp->ts_fidx); if (fword_ends) { // Copy the skipped character to preword. memmove(preword + sp->ts_prewordlen, @@ -4264,7 +4264,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // Correct ts_fidx for the byte length of the // character (we didn't check that before). sp->ts_fidx = sp->ts_fcharstart - + MB_PTR2LEN(fword + sp->ts_fcharstart); + + utfc_ptr2len(fword + sp->ts_fcharstart); // For changing a composing character adjust // the score from SCORE_SUBST to @@ -4363,7 +4363,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // a bit illogical for soundfold tree but it does give better // results. c = utf_ptr2char(fword + sp->ts_fidx); - stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx); + stack[depth].ts_fidx += utfc_ptr2len(fword + sp->ts_fidx); if (utf_iscomposing(c)) { stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP; } else if (c == utf_ptr2char(fword + stack[depth].ts_fidx)) { @@ -4533,9 +4533,9 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so case STATE_UNSWAP: // Undo the STATE_SWAP swap: "21" -> "12". p = fword + sp->ts_fidx; - n = MB_PTR2LEN(p); + n = utfc_ptr2len(p); c = utf_ptr2char(p + n); - memmove(p + MB_PTR2LEN(p + n), p, n); + memmove(p + utfc_ptr2len(p + n), p, n); utf_char2bytes(c, p); FALLTHROUGH; @@ -4589,11 +4589,11 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so case STATE_UNSWAP3: // Undo STATE_SWAP3: "321" -> "123" p = fword + sp->ts_fidx; - n = MB_PTR2LEN(p); + n = utfc_ptr2len(p); c2 = utf_ptr2char(p + n); - fl = MB_PTR2LEN(p + n); + fl = utfc_ptr2len(p + n); c = utf_ptr2char(p + n + fl); - tl = MB_PTR2LEN(p + n + fl); + tl = utfc_ptr2len(p + n + fl); memmove(p + fl + tl, p, n); utf_char2bytes(c, p); utf_char2bytes(c2, p + tl); @@ -4637,10 +4637,10 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so case STATE_UNROT3L: // Undo ROT3L: "231" -> "123" p = fword + sp->ts_fidx; - n = MB_PTR2LEN(p); - n += MB_PTR2LEN(p + n); + n = utfc_ptr2len(p); + n += utfc_ptr2len(p + n); c = utf_ptr2char(p + n); - tl = MB_PTR2LEN(p + n); + tl = utfc_ptr2len(p + n); memmove(p + tl, p, n); utf_char2bytes(c, p); @@ -4675,9 +4675,9 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // Undo ROT3R: "312" -> "123" p = fword + sp->ts_fidx; c = utf_ptr2char(p); - tl = MB_PTR2LEN(p); - n = MB_PTR2LEN(p + tl); - n += MB_PTR2LEN(p + tl + n); + tl = utfc_ptr2len(p); + n = utfc_ptr2len(p + tl); + n += utfc_ptr2len(p + tl + n); memmove(p, p + tl, n); utf_char2bytes(c, p + n); From 97cdfdcde24c6a804f879b6464512008db4b5cef Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 6 Oct 2019 21:22:02 -0400 Subject: [PATCH 0141/1293] Remove dead code --- src/nvim/diff.c | 2 +- src/nvim/spell.c | 46 ++++++++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 34cac5a098..31552929dc 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -723,7 +723,7 @@ static int diff_write_buffer(buf_T *buf, diffin_T *din) char_u cbuf[MB_MAXBYTES + 1]; c = PTR2CHAR(s); - c = enc_utf8 ? utf_fold(c) : TOLOWER_LOC(c); + c = utf_fold(c); orig_len = utfc_ptr2len(s); if (utf_char2bytes(c, cbuf) != orig_len) { // TODO(Bram): handle byte length difference diff --git a/src/nvim/spell.c b/src/nvim/spell.c index 503657c427..a3c1746383 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -2570,36 +2570,33 @@ void init_spell_chartab(void) /// Thus this only works properly when past the first character of the word. /// /// @param wp Buffer used. -static bool spell_iswordp(char_u *p, win_T *wp) +static bool spell_iswordp(const char_u *p, const win_T *wp) + FUNC_ATTR_NONNULL_ALL { - char_u *s; - int l; int c; - if (has_mbyte) { - l = utfc_ptr2len(p); - s = p; - if (l == 1) { - // be quick for ASCII - if (wp->w_s->b_spell_ismw[*p]) - s = p + 1; // skip a mid-word character - } else { - c = utf_ptr2char(p); - if (c < 256 ? wp->w_s->b_spell_ismw[c] - : (wp->w_s->b_spell_ismw_mb != NULL - && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL)) { - s = p + l; - } + const int l = utfc_ptr2len(p); + const char_u *s = p; + if (l == 1) { + // be quick for ASCII + if (wp->w_s->b_spell_ismw[*p]) { + s = p + 1; // skip a mid-word character } - - c = utf_ptr2char(s); - if (c > 255) { - return spell_mb_isword_class(mb_get_class(s), wp); + } else { + c = utf_ptr2char(p); + if (c < 256 + ? wp->w_s->b_spell_ismw[c] + : (wp->w_s->b_spell_ismw_mb != NULL + && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL)) { + s = p + l; } - return spelltab.st_isw[c]; } - return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]]; + c = utf_ptr2char(s); + if (c > 255) { + return spell_mb_isword_class(mb_get_class(s), wp); + } + return spelltab.st_isw[c]; } // Returns true if "p" points to a word character. @@ -2617,7 +2614,8 @@ bool spell_iswordp_nmw(const char_u *p, win_T *wp) // Only for characters above 255. // Unicode subscript and superscript are not considered word characters. // See also utf_class() in mbyte.c. -static bool spell_mb_isword_class(int cl, win_T *wp) +static bool spell_mb_isword_class(int cl, const win_T *wp) + FUNC_ATTR_PURE FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { if (wp->w_s->b_cjk) // East Asian characters are not considered word characters. From 6ea49d8c76388010ab066f53544074347c4dce01 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 7 Oct 2019 13:01:14 +0200 Subject: [PATCH 0142/1293] ci: use cluacov for better performance (#11152) --- ci/before_script.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/before_script.sh b/ci/before_script.sh index 6a07c6747e..a0e87adb9e 100755 --- a/ci/before_script.sh +++ b/ci/before_script.sh @@ -35,9 +35,9 @@ fi # Compile dependencies. build_deps -# Install luacov for Lua coverage. +# Install cluacov for Lua coverage. if [[ "$USE_LUACOV" == 1 ]]; then - "${DEPS_BUILD_DIR}/usr/bin/luarocks" install luacov + "${DEPS_BUILD_DIR}/usr/bin/luarocks" install cluacov fi rm -rf "${LOG_DIR}" From b1ada8ec2159fbc69b58cc40eb62a4e76edd8d45 Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Sun, 22 Sep 2019 21:47:24 +0200 Subject: [PATCH 0143/1293] vim-patch:8.1.1354: getting a list of text lines is clumsy Problem: Getting a list of text lines is clumsy. Solution: Add the =<< assignment. (Yegappan Lakshmanan, closes vim/vim#4386) https://github.com/vim/vim/commit/f5842c5a533346c4ff41ff666e465c85f1de35d5 --- runtime/doc/eval.txt | 38 +++++++++++++++ src/nvim/eval.c | 92 +++++++++++++++++++++++++++++++++++ src/nvim/testdir/test_let.vim | 55 +++++++++++++++++++++ 3 files changed, 185 insertions(+) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 5e6bfd0dbc..607e88b7c8 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -9779,6 +9779,44 @@ This does NOT work: > Like above, but append/add/subtract the value for each |List| item. + *:let=<<* *:let-heredoc* *E990* *E991* +:let {var-name} =<< [trim] {marker} +text... +text... +{marker} + Set internal variable {var-name} to a List containing + the lines of text bounded by the string {marker}. + {marker} must not contain white space. + The last line should end only with the {marker} string + without any other character. Watch out for white + space after {marker}! + If {marker} is not supplied, then "." is used as the + default marker. + + Any white space characters in the lines of text are + preserved. If "trim" is specified before {marker}, + then all the leading indentation exactly matching the + leading indentation before `let` is stripped from the + input lines and the line containing {marker}. Note + that the difference between space and tab matters + here. + + If {var-name} didn't exist yet, it is created. + Cannot be followed by another command, but can be + followed by a comment. + + Examples: > + let var1 =<< END + Sample text 1 + Sample text 2 + Sample text 3 + END + + let data =<< trim DATA + 1 2 3 4 + 5 6 7 8 + DATA +< *E121* :let {var-name} .. List the value of variable {var-name}. Multiple variable names may be given. Special names recognized diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 925b64d42c..9b12ca1e12 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1504,6 +1504,87 @@ void ex_const(exarg_T *eap) ex_let_const(eap, true); } +// Get a list of lines from a HERE document. The here document is a list of +// lines surrounded by a marker. +// cmd << {marker} +// {line1} +// {line2} +// .... +// {marker} +// +// The {marker} is a string. If the optional 'trim' word is supplied before the +// marker, then the leading indentation before the lines (matching the +// indentation in the 'cmd' line) is stripped. +// Returns a List with {lines} or NULL. +static list_T * +heredoc_get(exarg_T *eap, char_u *cmd) +{ + char_u *marker; + char_u *p; + int indent_len = 0; + + if (eap->getline == NULL) { + EMSG(_("E991: cannot use =<< here")); + return NULL; + } + + // Check for the optional 'trim' word before the marker + cmd = skipwhite(cmd); + if (STRNCMP(cmd, "trim", 4) == 0 + && (cmd[4] == NUL || ascii_iswhite(cmd[4]))) { + cmd = skipwhite(cmd + 4); + + // Trim the indentation from all the lines in the here document + // The amount of indentation trimmed is the same as the indentation of + // the :let command line. + p = *eap->cmdlinep; + while (ascii_iswhite(*p)) { + p++; + indent_len++; + } + } + + // The marker is the next word. Default marker is "." + if (*cmd != NUL && *cmd != '"') { + marker = skipwhite(cmd); + p = skiptowhite(marker); + if (*skipwhite(p) != NUL && *skipwhite(p) != '"') { + EMSG(_(e_trailing)); + return NULL; + } + *p = NUL; + } else { + marker = (char_u *)"."; + } + + list_T *l = tv_list_alloc(0); + for (;;) { + int i = 0; + + char_u *theline = eap->getline(NUL, eap->cookie, 0); + if (theline != NULL && indent_len > 0) { + // trim the indent matching the first line + if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0) { + i = indent_len; + } + } + + if (theline == NULL) { + EMSG2(_("E990: Missing end marker '%s'"), marker); + break; + } + if (STRCMP(marker, theline + i) == 0) { + xfree(theline); + break; + } + + tv_list_append_string(l, (char *)(theline + i), -1); + xfree(theline); + } + + return l; +} + // ":let" list all variable values // ":let var1 var2" list variable values // ":let var = expr" assignment command. @@ -1560,6 +1641,17 @@ static void ex_let_const(exarg_T *eap, const bool is_const) list_vim_vars(&first); } eap->nextcmd = check_nextcmd(arg); + } else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<') { + // HERE document + list_T *l = heredoc_get(eap, expr + 3); + if (l != NULL) { + tv_list_set_ret(&rettv, l); + op[0] = '='; + op[1] = NUL; + (void)ex_let_vars(eap->arg, &rettv, false, semicolon, var_count, + is_const, op); + tv_clear(&rettv); + } } else { op[0] = '='; op[1] = NUL; diff --git a/src/nvim/testdir/test_let.vim b/src/nvim/testdir/test_let.vim index 8a6f1bc320..9887fb531e 100644 --- a/src/nvim/testdir/test_let.vim +++ b/src/nvim/testdir/test_let.vim @@ -140,3 +140,58 @@ func Test_let_varg_fail() call assert_fails('call s:set_varg7(1)', 'E742:') call s:set_varg8([0]) endfunction + + +" Test for the setting a variable using the heredoc syntax +func Test_let_heredoc() + let var1 =<< END +Some sample text + Text with indent + !@#$%^&*()-+_={}|[]\~`:";'<>?,./ +END + + call assert_equal(["Some sample text", "\tText with indent", " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1) + + let var2 =<< +Editor +. + call assert_equal(['Editor'], var2) + + let var3 =< Date: Mon, 23 Sep 2019 19:46:45 +0200 Subject: [PATCH 0144/1293] vim-patch:8.1.1356: some text in heredoc assignment ends the text Problem: Some text in heredoc assignment ends the text. (Ozaki Kiichi) Solution: Recognize "let v =<<" and skip until the end. https://github.com/vim/vim/commit/8471e57026714c5a0faf89288ceef5231fb88d4f --- src/nvim/eval.c | 34 +++++++++++++-- src/nvim/testdir/test_let.vim | 79 ++++++++++++++++++++++++++++------- 2 files changed, 95 insertions(+), 18 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 9b12ca1e12..6d706939a1 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -21269,6 +21269,7 @@ void ex_function(exarg_T *eap) int indent; int nesting; char_u *skip_until = NULL; + char_u *trimmed = NULL; dictitem_T *v; funcdict_T fudi; static int func_nr = 0; /* number for nameless function */ @@ -21572,10 +21573,14 @@ void ex_function(exarg_T *eap) sourcing_lnum_off = 0; if (skip_until != NULL) { - /* between ":append" and "." and between ":python < Date: Mon, 23 Sep 2019 22:12:02 +0200 Subject: [PATCH 0145/1293] vim-patch:8.1.1362: code and data in tests can be hard to read Problem: Code and data in tests can be hard to read. Solution: Use the new heredoc style. (Yegappan Lakshmanan, closes vim/vim#4400) https://github.com/vim/vim/commit/c79745a82faeb5a6058e915ca49a4c69fa60ea01 --- src/nvim/testdir/test_autocmd.vim | 102 +++--- src/nvim/testdir/test_cindent.vim | 60 ++-- src/nvim/testdir/test_exit.vim | 66 ++-- src/nvim/testdir/test_fold.vim | 23 +- src/nvim/testdir/test_goto.vim | 406 ++++++++++++----------- src/nvim/testdir/test_mksession_utf8.vim | 57 ++-- src/nvim/testdir/test_normal.vim | 181 +++++++--- src/nvim/testdir/test_profile.vim | 266 +++++++-------- src/nvim/testdir/test_quickfix.vim | 252 +++++++------- src/nvim/testdir/test_startup.vim | 150 +++++---- 10 files changed, 859 insertions(+), 704 deletions(-) diff --git a/src/nvim/testdir/test_autocmd.vim b/src/nvim/testdir/test_autocmd.vim index 4dd48beef4..275b053bc9 100644 --- a/src/nvim/testdir/test_autocmd.vim +++ b/src/nvim/testdir/test_autocmd.vim @@ -425,18 +425,20 @@ func Test_autocmd_bufwipe_in_SessLoadPost() set noswapfile mksession! - let content = ['set nocp noswapfile', - \ 'let v:swapchoice="e"', - \ 'augroup test_autocmd_sessionload', - \ 'autocmd!', - \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"', - \ 'augroup END', - \ '', - \ 'func WriteErrors()', - \ ' call writefile([execute("messages")], "Xerrors")', - \ 'endfunc', - \ 'au VimLeave * call WriteErrors()', - \ ] + let content =<< trim [CODE] + set nocp noswapfile + let v:swapchoice="e" + augroup test_autocmd_sessionload + autocmd! + autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!" + augroup END + + func WriteErrors() + call writefile([execute("messages")], "Xerrors") + endfunc + au VimLeave * call WriteErrors() + [CODE] + call writefile(content, 'Xvimrc') call system(v:progpath. ' --headless -i NONE -u Xvimrc --noplugins -S Session.vim -c cq') let errors = join(readfile('Xerrors')) @@ -454,27 +456,29 @@ func Test_autocmd_bufwipe_in_SessLoadPost2() set noswapfile mksession! - let content = ['set nocp noswapfile', - \ 'function! DeleteInactiveBufs()', - \ ' tabfirst', - \ ' let tabblist = []', - \ ' for i in range(1, tabpagenr(''$''))', - \ ' call extend(tabblist, tabpagebuflist(i))', - \ ' endfor', - \ ' for b in range(1, bufnr(''$''))', - \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')', - \ ' exec ''bwipeout '' . b', - \ ' endif', - \ ' endfor', - \ ' echomsg "SessionLoadPost DONE"', - \ 'endfunction', - \ 'au SessionLoadPost * call DeleteInactiveBufs()', - \ '', - \ 'func WriteErrors()', - \ ' call writefile([execute("messages")], "Xerrors")', - \ 'endfunc', - \ 'au VimLeave * call WriteErrors()', - \ ] + let content =<< trim [CODE] + set nocp noswapfile + function! DeleteInactiveBufs() + tabfirst + let tabblist = [] + for i in range(1, tabpagenr(''$'')) + call extend(tabblist, tabpagebuflist(i)) + endfor + for b in range(1, bufnr(''$'')) + if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'') + exec ''bwipeout '' . b + endif + endfor + echomsg "SessionLoadPost DONE" + endfunction + au SessionLoadPost * call DeleteInactiveBufs() + + func WriteErrors() + call writefile([execute("messages")], "Xerrors") + endfunc + au VimLeave * call WriteErrors() + [CODE] + call writefile(content, 'Xvimrc') call system(v:progpath. ' --headless -i NONE -u Xvimrc --noplugins -S Session.vim -c cq') let errors = join(readfile('Xerrors')) @@ -936,21 +940,23 @@ func Test_bufunload_all() call writefile(['Test file Xxx1'], 'Xxx1')" call writefile(['Test file Xxx2'], 'Xxx2')" - let content = [ - \ "func UnloadAllBufs()", - \ " let i = 1", - \ " while i <= bufnr('$')", - \ " if i != bufnr('%') && bufloaded(i)", - \ " exe i . 'bunload'", - \ " endif", - \ " let i += 1", - \ " endwhile", - \ "endfunc", - \ "au BufUnload * call UnloadAllBufs()", - \ "au VimLeave * call writefile(['Test Finished'], 'Xout')", - \ "edit Xxx1", - \ "split Xxx2", - \ "q"] + let content =<< trim [CODE] + func UnloadAllBufs() + let i = 1 + while i <= bufnr('$') + if i != bufnr('%') && bufloaded(i) + exe i . 'bunload' + endif + let i += 1 + endwhile + endfunc + au BufUnload * call UnloadAllBufs() + au VimLeave * call writefile(['Test Finished'], 'Xout') + edit Xxx1 + split Xxx2 + q + [CODE] + call writefile(content, 'Xtest') call delete('Xout') diff --git a/src/nvim/testdir/test_cindent.vim b/src/nvim/testdir/test_cindent.vim index 7c2c5e341c..f979e354ba 100644 --- a/src/nvim/testdir/test_cindent.vim +++ b/src/nvim/testdir/test_cindent.vim @@ -18,25 +18,25 @@ endfunc func Test_cino_extern_c() " Test for cino-E - let without_ind = [ - \ '#ifdef __cplusplus', - \ 'extern "C" {', - \ '#endif', - \ 'int func_a(void);', - \ '#ifdef __cplusplus', - \ '}', - \ '#endif' - \ ] + let without_ind =<< trim [CODE] + #ifdef __cplusplus + extern "C" { + #endif + int func_a(void); + #ifdef __cplusplus + } + #endif + [CODE] - let with_ind = [ - \ '#ifdef __cplusplus', - \ 'extern "C" {', - \ '#endif', - \ "\tint func_a(void);", - \ '#ifdef __cplusplus', - \ '}', - \ '#endif' - \ ] + let with_ind =<< trim [CODE] + #ifdef __cplusplus + extern "C" { + #endif + int func_a(void); + #ifdef __cplusplus + } + #endif + [CODE] new setlocal cindent cinoptions=E0 call setline(1, without_ind) @@ -89,16 +89,32 @@ func Test_cindent_expr() return v:lnum == 1 ? shiftwidth() : 0 endfunc setl expandtab sw=8 indentkeys+=; indentexpr=MyIndentFunction() - call setline(1, ['var_a = something()', 'b = something()']) + let testinput =<< trim [CODE] + var_a = something() + b = something() + [CODE] + call setline(1, testinput) call cursor(1, 1) call feedkeys("^\j$A;\", 'tnix') - call assert_equal([' var_a = something();', 'b = something();'], getline(1, '$')) + let expected =<< trim [CODE] + var_a = something(); + b = something(); + [CODE] + call assert_equal(expected, getline(1, '$')) %d - call setline(1, [' var_a = something()', ' b = something()']) + let testinput =<< trim [CODE] + var_a = something() + b = something() + [CODE] + call setline(1, testinput) call cursor(1, 1) call feedkeys("^\j$A;\", 'tnix') - call assert_equal([' var_a = something();', ' b = something()'], getline(1, '$')) + let expected =<< trim [CODE] + var_a = something(); + b = something() + [CODE] + call assert_equal(expected, getline(1, '$')) bw! endfunc diff --git a/src/nvim/testdir/test_exit.vim b/src/nvim/testdir/test_exit.vim index 8f02fd29e3..3797626abf 100644 --- a/src/nvim/testdir/test_exit.vim +++ b/src/nvim/testdir/test_exit.vim @@ -3,52 +3,56 @@ source shared.vim func Test_exiting() - let after = [ - \ 'au QuitPre * call writefile(["QuitPre"], "Xtestout")', - \ 'au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")', - \ 'quit', - \ ] + let after =<< trim [CODE] + au QuitPre * call writefile(["QuitPre"], "Xtestout") + au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") + quit + [CODE] + if RunVim([], after, '') call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout')) endif call delete('Xtestout') - let after = [ - \ 'au QuitPre * call writefile(["QuitPre"], "Xtestout")', - \ 'au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")', - \ 'help', - \ 'wincmd w', - \ 'quit', - \ ] + let after =<< trim [CODE] + au QuitPre * call writefile(["QuitPre"], "Xtestout") + au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") + help + wincmd w + quit + [CODE] + if RunVim([], after, '') call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout')) endif call delete('Xtestout') - let after = [ - \ 'au QuitPre * call writefile(["QuitPre"], "Xtestout")', - \ 'au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")', - \ 'split', - \ 'new', - \ 'qall', - \ ] + let after =<< trim [CODE] + au QuitPre * call writefile(["QuitPre"], "Xtestout") + au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") + split + new + qall + [CODE] + if RunVim([], after, '') call assert_equal(['QuitPre', 'ExitPre'], readfile('Xtestout')) endif call delete('Xtestout') - let after = [ - \ 'au QuitPre * call writefile(["QuitPre"], "Xtestout", "a")', - \ 'au ExitPre * call writefile(["ExitPre"], "Xtestout", "a")', - \ 'augroup nasty', - \ ' au ExitPre * split', - \ 'augroup END', - \ 'quit', - \ 'augroup nasty', - \ ' au! ExitPre', - \ 'augroup END', - \ 'quit', - \ ] + let after =<< trim [CODE] + au QuitPre * call writefile(["QuitPre"], "Xtestout", "a") + au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") + augroup nasty + au ExitPre * split + augroup END + quit + augroup nasty + au! ExitPre + augroup END + quit + [CODE] + if RunVim([], after, '') call assert_equal(['QuitPre', 'ExitPre', 'QuitPre', 'ExitPre'], \ readfile('Xtestout')) diff --git a/src/nvim/testdir/test_fold.vim b/src/nvim/testdir/test_fold.vim index 03723b3cb5..324f3f8cf2 100644 --- a/src/nvim/testdir/test_fold.vim +++ b/src/nvim/testdir/test_fold.vim @@ -520,17 +520,18 @@ func Test_fold_create_marker_in_C() set fdm=marker fdl=9 set filetype=c - let content = [ - \ '/*', - \ ' * comment', - \ ' * ', - \ ' *', - \ ' */', - \ 'int f(int* p) {', - \ ' *p = 3;', - \ ' return 0;', - \ '}' - \] + let content =<< trim [CODE] + /* + * comment + * + * + */ + int f(int* p) { + *p = 3; + return 0; + } + [CODE] + for c in range(len(content) - 1) bw! call append(0, content) diff --git a/src/nvim/testdir/test_goto.vim b/src/nvim/testdir/test_goto.vim index c0235b1707..f04a5a7e3d 100644 --- a/src/nvim/testdir/test_goto.vim +++ b/src/nvim/testdir/test_goto.vim @@ -15,262 +15,283 @@ func XTest_goto_decl(cmd, lines, line, col) endfunc func Test_gD() - let lines = [ - \ 'int x;', - \ '', - \ 'int func(void)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int x; + + int func(void) + { + return x; + } + [CODE] + call XTest_goto_decl('gD', lines, 1, 5) endfunc func Test_gD_too() - let lines = [ - \ 'Filename x;', - \ '', - \ 'int Filename', - \ 'int func() {', - \ ' Filename x;', - \ ' return x;', - \ ] + let lines =<< trim [CODE] + Filename x; + + int Filename + int func() { + Filename x; + return x; + [CODE] + call XTest_goto_decl('gD', lines, 1, 10) endfunc func Test_gD_comment() - let lines = [ - \ '/* int x; */', - \ 'int x;', - \ '', - \ 'int func(void)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + /* int x; */ + int x; + + int func(void) + { + return x; + } + [CODE] + call XTest_goto_decl('gD', lines, 2, 5) endfunc func Test_gD_inline_comment() - let lines = [ - \ 'int y /* , x */;', - \ 'int x;', - \ '', - \ 'int func(void)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int y /* , x */; + int x; + + int func(void) + { + return x; + } + [CODE] + call XTest_goto_decl('gD', lines, 2, 5) endfunc func Test_gD_string() - let lines = [ - \ 'char *s[] = "x";', - \ 'int x = 1;', - \ '', - \ 'int func(void)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + char *s[] = "x"; + int x = 1; + + int func(void) + { + return x; + } + [CODE] + call XTest_goto_decl('gD', lines, 2, 5) endfunc func Test_gD_string_same_line() - let lines = [ - \ 'char *s[] = "x", int x = 1;', - \ '', - \ 'int func(void)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + char *s[] = "x", int x = 1; + + int func(void) + { + return x; + } + [CODE] + call XTest_goto_decl('gD', lines, 1, 22) endfunc func Test_gD_char() - let lines = [ - \ "char c = 'x';", - \ 'int x = 1;', - \ '', - \ 'int func(void)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + char c = 'x'; + int x = 1; + + int func(void) + { + return x; + } + [CODE] + call XTest_goto_decl('gD', lines, 2, 5) endfunc func Test_gd() - let lines = [ - \ 'int x;', - \ '', - \ 'int func(int x)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int x; + + int func(int x) + { + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 3, 14) endfunc func Test_gd_not_local() - let lines = [ - \ 'int func1(void)', - \ '{', - \ ' return x;', - \ '}', - \ '', - \ 'int func2(int x)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func1(void) + { + return x; + } + + int func2(int x) + { + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 3, 10) endfunc func Test_gd_kr_style() - let lines = [ - \ 'int func(x)', - \ ' int x;', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func(x) + int x; + { + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 2, 7) endfunc func Test_gd_missing_braces() - let lines = [ - \ 'def func1(a)', - \ ' a + 1', - \ 'end', - \ '', - \ 'a = 1', - \ '', - \ 'def func2()', - \ ' return a', - \ 'end', - \ ] + let lines =<< trim [CODE] + def func1(a) + a + 1 + end + + a = 1 + + def func2() + return a + end + [CODE] + call XTest_goto_decl('gd', lines, 1, 11) endfunc func Test_gd_comment() - let lines = [ - \ 'int func(void)', - \ '{', - \ ' /* int x; */', - \ ' int x;', - \ ' return x;', - \ '}', - \] + let lines =<< trim [CODE] + int func(void) + { + /* int x; */ + int x; + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 4, 7) endfunc func Test_gd_comment_in_string() - let lines = [ - \ 'int func(void)', - \ '{', - \ ' char *s ="//"; int x;', - \ ' int x;', - \ ' return x;', - \ '}', - \] + let lines =<< trim [CODE] + int func(void) + { + char *s ="//"; int x; + int x; + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 3, 22) endfunc func Test_gd_string_in_comment() set comments= - let lines = [ - \ 'int func(void)', - \ '{', - \ ' /* " */ int x;', - \ ' int x;', - \ ' return x;', - \ '}', - \] + let lines =<< trim [CODE] + int func(void) + { + /* " */ int x; + int x; + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 3, 15) set comments& endfunc func Test_gd_inline_comment() - let lines = [ - \ 'int func(/* x is an int */ int x)', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func(/* x is an int */ int x) + { + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 1, 32) endfunc func Test_gd_inline_comment_only() - let lines = [ - \ 'int func(void) /* one lonely x */', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func(void) /* one lonely x */ + { + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 3, 10) endfunc func Test_gd_inline_comment_body() - let lines = [ - \ 'int func(void)', - \ '{', - \ ' int y /* , x */;', - \ '', - \ ' for (/* int x = 0 */; y < 2; y++);', - \ '', - \ ' int x = 0;', - \ '', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func(void) + { + int y /* , x */; + + for (/* int x = 0 */; y < 2; y++); + + int x = 0; + + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 7, 7) endfunc func Test_gd_trailing_multiline_comment() - let lines = [ - \ 'int func(int x) /* x is an int */', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func(int x) /* x is an int */ + { + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 1, 14) endfunc func Test_gd_trailing_comment() - let lines = [ - \ 'int func(int x) // x is an int', - \ '{', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func(int x) // x is an int + { + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 1, 14) endfunc func Test_gd_string() - let lines = [ - \ 'int func(void)', - \ '{', - \ ' char *s = "x";', - \ ' int x = 1;', - \ '', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func(void) + { + char *s = "x"; + int x = 1; + + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 4, 7) endfunc func Test_gd_string_only() - let lines = [ - \ 'int func(void)', - \ '{', - \ ' char *s = "x";', - \ '', - \ ' return x;', - \ '}', - \ ] + let lines =<< trim [CODE] + int func(void) + { + char *s = "x"; + + return x; + } + [CODE] + call XTest_goto_decl('gd', lines, 5, 10) endfunc @@ -289,24 +310,25 @@ func Test_cursorline_keep_col() endfunc func Test_gd_local_block() - let lines = [ - \ ' int main()', - \ '{', - \ ' char *a = "NOT NULL";', - \ ' if(a)', - \ ' {', - \ ' char *b = a;', - \ ' printf("%s\n", b);', - \ ' }', - \ ' else', - \ ' {', - \ ' char *b = "NULL";', - \ ' return b;', - \ ' }', - \ '', - \ ' return 0;', - \ '}', - \ ] + let lines =<< trim [CODE] + int main() + { + char *a = "NOT NULL"; + if(a) + { + char *b = a; + printf("%s\n", b); + } + else + { + char *b = "NULL"; + return b; + } + + return 0; + } + [CODE] + call XTest_goto_decl('1gd', lines, 11, 11) endfunc diff --git a/src/nvim/testdir/test_mksession_utf8.vim b/src/nvim/testdir/test_mksession_utf8.vim index 67af3a9ca2..36f07512a8 100644 --- a/src/nvim/testdir/test_mksession_utf8.vim +++ b/src/nvim/testdir/test_mksession_utf8.vim @@ -65,34 +65,35 @@ func Test_mksession_utf8() call wincol() mksession! test_mks.out let li = filter(readfile('test_mks.out'), 'v:val =~# "\\(^ *normal! 0\\|^ *exe ''normal!\\)"') - let expected = [ - \ 'normal! 016|', - \ 'normal! 016|', - \ 'normal! 016|', - \ 'normal! 08|', - \ 'normal! 08|', - \ 'normal! 016|', - \ 'normal! 016|', - \ 'normal! 016|', - \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'", - \ " normal! 016|", - \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'", - \ " normal! 016|", - \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'", - \ " normal! 016|", - \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'", - \ " normal! 08|", - \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'", - \ " normal! 08|", - \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'", - \ " normal! 016|", - \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'", - \ " normal! 016|", - \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'", - \ " normal! 016|", - \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'", - \ " normal! 016|" - \ ] + let expected =<< trim [DATA] + normal! 016| + normal! 016| + normal! 016| + normal! 08| + normal! 08| + normal! 016| + normal! 016| + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 8 . '|' + normal! 08| + exe 'normal! ' . s:c . '|zs' . 8 . '|' + normal! 08| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + [DATA] + call assert_equal(expected, li) tabclose! diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index 8bc4228359..b967f84626 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -1563,52 +1563,94 @@ endfunc fun! Test_normal29_brace() " basic test for { and } movements - let text= ['A paragraph begins after each empty line, and also at each of a set of', - \ 'paragraph macros, specified by the pairs of characters in the ''paragraphs''', - \ 'option. The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to', - \ 'the macros ".IP", ".LP", etc. (These are nroff macros, so the dot must be in', - \ 'the first column). A section boundary is also a paragraph boundary.', - \ 'Note that a blank line (only containing white space) is NOT a paragraph', - \ 'boundary.', - \ '', - \ '', - \ 'Also note that this does not include a ''{'' or ''}'' in the first column. When', - \ 'the ''{'' flag is in ''cpoptions'' then ''{'' in the first column is used as a', - \ 'paragraph boundary |posix|.', - \ '{', - \ 'This is no paragraph', - \ 'unless the ''{'' is set', - \ 'in ''cpoptions''', - \ '}', - \ '.IP', - \ 'The nroff macros IP separates a paragraph', - \ 'That means, it must be a ''.''', - \ 'followed by IP', - \ '.LPIt does not matter, if afterwards some', - \ 'more characters follow.', - \ '.SHAlso section boundaries from the nroff', - \ 'macros terminate a paragraph. That means', - \ 'a character like this:', - \ '.NH', - \ 'End of text here'] + let text =<< trim [DATA] + A paragraph begins after each empty line, and also at each of a set of + paragraph macros, specified by the pairs of characters in the 'paragraphs' + option. The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to + the macros ".IP", ".LP", etc. (These are nroff macros, so the dot must be in + the first column). A section boundary is also a paragraph boundary. + Note that a blank line (only containing white space) is NOT a paragraph + boundary. + + + Also note that this does not include a '{' or '}' in the first column. When + the '{' flag is in 'cpoptions' then '{' in the first column is used as a + paragraph boundary |posix|. + { + This is no paragraph + unless the '{' is set + in 'cpoptions' + } + .IP + The nroff macros IP separates a paragraph + That means, it must be a '.' + followed by IP + .LPIt does not matter, if afterwards some + more characters follow. + .SHAlso section boundaries from the nroff + macros terminate a paragraph. That means + a character like this: + .NH + End of text here + [DATA] + new call append(0, text) 1 norm! 0d2} - call assert_equal(['.IP', - \ 'The nroff macros IP separates a paragraph', 'That means, it must be a ''.''', 'followed by IP', - \ '.LPIt does not matter, if afterwards some', 'more characters follow.', '.SHAlso section boundaries from the nroff', - \ 'macros terminate a paragraph. That means', 'a character like this:', '.NH', 'End of text here', ''], getline(1,'$')) + + let expected =<< trim [DATA] + .IP + The nroff macros IP separates a paragraph + That means, it must be a '.' + followed by IP + .LPIt does not matter, if afterwards some + more characters follow. + .SHAlso section boundaries from the nroff + macros terminate a paragraph. That means + a character like this: + .NH + End of text here + + [DATA] + call assert_equal(expected, getline(1, '$')) + norm! 0d} - call assert_equal(['.LPIt does not matter, if afterwards some', 'more characters follow.', - \ '.SHAlso section boundaries from the nroff', 'macros terminate a paragraph. That means', - \ 'a character like this:', '.NH', 'End of text here', ''], getline(1, '$')) + + let expected =<< trim [DATA] + .LPIt does not matter, if afterwards some + more characters follow. + .SHAlso section boundaries from the nroff + macros terminate a paragraph. That means + a character like this: + .NH + End of text here + + [DATA] + call assert_equal(expected, getline(1, '$')) + $ norm! d{ - call assert_equal(['.LPIt does not matter, if afterwards some', 'more characters follow.', - \ '.SHAlso section boundaries from the nroff', 'macros terminate a paragraph. That means', 'a character like this:', ''], getline(1, '$')) + + let expected =<< trim [DATA] + .LPIt does not matter, if afterwards some + more characters follow. + .SHAlso section boundaries from the nroff + macros terminate a paragraph. That means + a character like this: + + [DATA] + call assert_equal(expected, getline(1, '$')) + norm! d{ - call assert_equal(['.LPIt does not matter, if afterwards some', 'more characters follow.', ''], getline(1,'$')) + + let expected =<< trim [DATA] + .LPIt does not matter, if afterwards some + more characters follow. + + [DATA] + call assert_equal(expected, getline(1, '$')) + " Test with { in cpooptions %d call append(0, text) @@ -1616,21 +1658,62 @@ fun! Test_normal29_brace() " set cpo+={ " 1 " norm! 0d2} - " call assert_equal(['{', 'This is no paragraph', 'unless the ''{'' is set', 'in ''cpoptions''', '}', - " \ '.IP', 'The nroff macros IP separates a paragraph', 'That means, it must be a ''.''', - " \ 'followed by IP', '.LPIt does not matter, if afterwards some', 'more characters follow.', - " \ '.SHAlso section boundaries from the nroff', 'macros terminate a paragraph. That means', - " \ 'a character like this:', '.NH', 'End of text here', ''], getline(1,'$')) + " let expected =<< trim [DATA] + " { + " This is no paragraph + " unless the '{' is set + " in 'cpoptions' + " } + " .IP + " The nroff macros IP separates a paragraph + " That means, it must be a '.' + " followed by IP + " .LPIt does not matter, if afterwards some + " more characters follow. + " .SHAlso section boundaries from the nroff + " macros terminate a paragraph. That means + " a character like this: + " .NH + " End of text here + " + " [DATA] + " call assert_equal(expected, getline(1, '$')) + " " $ " norm! d} - " call assert_equal(['{', 'This is no paragraph', 'unless the ''{'' is set', 'in ''cpoptions''', '}', - " \ '.IP', 'The nroff macros IP separates a paragraph', 'That means, it must be a ''.''', - " \ 'followed by IP', '.LPIt does not matter, if afterwards some', 'more characters follow.', - " \ '.SHAlso section boundaries from the nroff', 'macros terminate a paragraph. That means', - " \ 'a character like this:', '.NH', 'End of text here', ''], getline(1,'$')) + " let expected =<< trim [DATA] + " { + " This is no paragraph + " unless the '{' is set + " in 'cpoptions' + " } + " .IP + " The nroff macros IP separates a paragraph + " That means, it must be a '.' + " followed by IP + " .LPIt does not matter, if afterwards some + " more characters follow. + " .SHAlso section boundaries from the nroff + " macros terminate a paragraph. That means + " a character like this: + " .NH + " End of text here + " + " [DATA] + " call assert_equal(expected, getline(1, '$')) + " " norm! gg} " norm! d5} - " call assert_equal(['{', 'This is no paragraph', 'unless the ''{'' is set', 'in ''cpoptions''', '}', ''], getline(1,'$')) + " + " let expected =<< trim [DATA] + " { + " This is no paragraph + " unless the '{' is set + " in 'cpoptions' + " } + + " [DATA] + " call assert_equal(expected, getline(1, '$')) " clean up set cpo-={ diff --git a/src/nvim/testdir/test_profile.vim b/src/nvim/testdir/test_profile.vim index b677ac3704..4ab20a9c77 100644 --- a/src/nvim/testdir/test_profile.vim +++ b/src/nvim/testdir/test_profile.vim @@ -6,34 +6,34 @@ endif source screendump.vim func Test_profile_func() - let lines = [ - \ 'profile start Xprofile_func.log', - \ 'profile func Foo*"', - \ "func! Foo1()", - \ "endfunc", - \ "func! Foo2()", - \ " let l:count = 100", - \ " while l:count > 0", - \ " let l:count = l:count - 1", - \ " endwhile", - \ "endfunc", - \ "func! Foo3()", - \ "endfunc", - \ "func! Bar()", - \ "endfunc", - \ "call Foo1()", - \ "call Foo1()", - \ "profile pause", - \ "call Foo1()", - \ "profile continue", - \ "call Foo2()", - \ "call Foo3()", - \ "call Bar()", - \ "if !v:profiling", - \ " delfunc Foo2", - \ "endif", - \ "delfunc Foo3", - \ ] + let lines =<< trim [CODE] + profile start Xprofile_func.log + profile func Foo* + func! Foo1() + endfunc + func! Foo2() + let l:count = 100 + while l:count > 0 + let l:count = l:count - 1 + endwhile + endfunc + func! Foo3() + endfunc + func! Bar() + endfunc + call Foo1() + call Foo1() + profile pause + call Foo1() + profile continue + call Foo2() + call Foo3() + call Bar() + if !v:profiling + delfunc Foo2 + endif + delfunc Foo3 + [CODE] call writefile(lines, 'Xprofile_func.vim') call system(v:progpath @@ -88,38 +88,38 @@ func Test_profile_func() endfunc func Test_profile_func_with_ifelse() - let lines = [ - \ "func! Foo1()", - \ " if 1", - \ " let x = 0", - \ " elseif 1", - \ " let x = 1", - \ " else", - \ " let x = 2", - \ " endif", - \ "endfunc", - \ "func! Foo2()", - \ " if 0", - \ " let x = 0", - \ " elseif 1", - \ " let x = 1", - \ " else", - \ " let x = 2", - \ " endif", - \ "endfunc", - \ "func! Foo3()", - \ " if 0", - \ " let x = 0", - \ " elseif 0", - \ " let x = 1", - \ " else", - \ " let x = 2", - \ " endif", - \ "endfunc", - \ "call Foo1()", - \ "call Foo2()", - \ "call Foo3()", - \ ] + let lines =<< trim [CODE] + func! Foo1() + if 1 + let x = 0 + elseif 1 + let x = 1 + else + let x = 2 + endif + endfunc + func! Foo2() + if 0 + let x = 0 + elseif 1 + let x = 1 + else + let x = 2 + endif + endfunc + func! Foo3() + if 0 + let x = 0 + elseif 0 + let x = 1 + else + let x = 2 + endif + endfunc + call Foo1() + call Foo2() + call Foo3() + [CODE] call writefile(lines, 'Xprofile_func.vim') call system(v:progpath @@ -198,41 +198,41 @@ func Test_profile_func_with_ifelse() endfunc func Test_profile_func_with_trycatch() - let lines = [ - \ "func! Foo1()", - \ " try", - \ " let x = 0", - \ " catch", - \ " let x = 1", - \ " finally", - \ " let x = 2", - \ " endtry", - \ "endfunc", - \ "func! Foo2()", - \ " try", - \ " throw 0", - \ " catch", - \ " let x = 1", - \ " finally", - \ " let x = 2", - \ " endtry", - \ "endfunc", - \ "func! Foo3()", - \ " try", - \ " throw 0", - \ " catch", - \ " throw 1", - \ " finally", - \ " let x = 2", - \ " endtry", - \ "endfunc", - \ "call Foo1()", - \ "call Foo2()", - \ "try", - \ " call Foo3()", - \ "catch", - \ "endtry", - \ ] + let lines =<< trim [CODE] + func! Foo1() + try + let x = 0 + catch + let x = 1 + finally + let x = 2 + endtry + endfunc + func! Foo2() + try + throw 0 + catch + let x = 1 + finally + let x = 2 + endtry + endfunc + func! Foo3() + try + throw 0 + catch + throw 1 + finally + let x = 2 + endtry + endfunc + call Foo1() + call Foo2() + try + call Foo3() + catch + endtry + [CODE] call writefile(lines, 'Xprofile_func.vim') call system(v:progpath @@ -311,15 +311,15 @@ func Test_profile_func_with_trycatch() endfunc func Test_profile_file() - let lines = [ - \ 'func! Foo()', - \ 'endfunc', - \ 'for i in range(10)', - \ ' " a comment', - \ ' call Foo()', - \ 'endfor', - \ 'call Foo()', - \ ] + let lines =<< trim [CODE] + func! Foo() + endfunc + for i in range(10) + " a comment + call Foo() + endfor + call Foo() + [CODE] call writefile(lines, 'Xprofile_file.vim') call system(v:progpath @@ -450,26 +450,27 @@ func Test_profile_truncate_mbyte() endfunc func Test_profdel_func() - let lines = [ - \ 'profile start Xprofile_file.log', - \ 'func! Foo1()', - \ 'endfunc', - \ 'func! Foo2()', - \ 'endfunc', - \ 'func! Foo3()', - \ 'endfunc', - \ '', - \ 'profile func Foo1', - \ 'profile func Foo2', - \ 'call Foo1()', - \ 'call Foo2()', - \ '', - \ 'profile func Foo3', - \ 'profdel func Foo2', - \ 'profdel func Foo3', - \ 'call Foo1()', - \ 'call Foo2()', - \ 'call Foo3()' ] + let lines =<< trim [CODE] + profile start Xprofile_file.log + func! Foo1() + endfunc + func! Foo2() + endfunc + func! Foo3() + endfunc + + profile func Foo1 + profile func Foo2 + call Foo1() + call Foo2() + + profile func Foo3 + profdel func Foo2 + profdel func Foo3 + call Foo1() + call Foo2() + call Foo3() + [CODE] call writefile(lines, 'Xprofile_file.vim') call system(v:progpath . ' -es --clean -c "so Xprofile_file.vim" -c q') call assert_equal(0, v:shell_error) @@ -496,14 +497,15 @@ endfunc func Test_profdel_star() " Foo() is invoked once before and once after 'profdel *'. " So profiling should report it only once. - let lines = [ - \ 'profile start Xprofile_file.log', - \ 'func! Foo()', - \ 'endfunc', - \ 'profile func Foo', - \ 'call Foo()', - \ 'profdel *', - \ 'call Foo()' ] + let lines =<< trim [CODE] + profile start Xprofile_file.log + func! Foo() + endfunc + profile func Foo + call Foo() + profdel * + call Foo() + [CODE] call writefile(lines, 'Xprofile_file.vim') call system(v:progpath . ' -es --clean -c "so Xprofile_file.vim" -c q') call assert_equal(0, v:shell_error) diff --git a/src/nvim/testdir/test_quickfix.vim b/src/nvim/testdir/test_quickfix.vim index 597be0aa89..b9a22aff51 100644 --- a/src/nvim/testdir/test_quickfix.vim +++ b/src/nvim/testdir/test_quickfix.vim @@ -775,68 +775,68 @@ func Test_efm1() return endif - let l = [ - \ '"Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.', - \ '"Xtestfile", line 6 col 19; this is an error', - \ 'gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c', - \ 'Xtestfile:9: parse error before `asd''', - \ 'make: *** [vim] Error 1', - \ 'in file "Xtestfile" linenr 10: there is an error', - \ '', - \ '2 returned', - \ '"Xtestfile", line 11 col 1; this is an error', - \ '"Xtestfile", line 12 col 2; this is another error', - \ '"Xtestfile", line 14:10; this is an error in column 10', - \ '=Xtestfile=, line 15:10; this is another error, but in vcol 10 this time', - \ '"Xtestfile", linenr 16: yet another problem', - \ 'Error in "Xtestfile" at line 17:', - \ 'x should be a dot', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17', - \ ' ^', - \ 'Error in "Xtestfile" at line 18:', - \ 'x should be a dot', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18', - \ '.............^', - \ 'Error in "Xtestfile" at line 19:', - \ 'x should be a dot', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19', - \ '--------------^', - \ 'Error in "Xtestfile" at line 20:', - \ 'x should be a dot', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20', - \ ' ^', - \ '', - \ 'Does anyone know what is the problem and how to correction it?', - \ '"Xtestfile", line 21 col 9: What is the title of the quickfix window?', - \ '"Xtestfile", line 22 col 9: What is the title of the quickfix window?' - \ ] + let l =<< trim [DATA] + "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set. + "Xtestfile", line 6 col 19; this is an error + gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c + Xtestfile:9: parse error before `asd' + make: *** [vim] Error 1 + in file "Xtestfile" linenr 10: there is an error + + 2 returned + "Xtestfile", line 11 col 1; this is an error + "Xtestfile", line 12 col 2; this is another error + "Xtestfile", line 14:10; this is an error in column 10 + =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time + "Xtestfile", linenr 16: yet another problem + Error in "Xtestfile" at line 17: + x should be a dot + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17 + ^ + Error in "Xtestfile" at line 18: + x should be a dot + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18 + .............^ + Error in "Xtestfile" at line 19: + x should be a dot + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19 + --------------^ + Error in "Xtestfile" at line 20: + x should be a dot + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20 + ^ + + Does anyone know what is the problem and how to correction it? + "Xtestfile", line 21 col 9: What is the title of the quickfix window? + "Xtestfile", line 22 col 9: What is the title of the quickfix window? + [DATA] call writefile(l, 'Xerrorfile1') call writefile(l[:-2], 'Xerrorfile2') - let m = [ - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21', - \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22' - \ ] + let m =<< trim [DATA] + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22 + [DATA] call writefile(m, 'Xtestfile') let save_efm = &efm @@ -895,20 +895,21 @@ func s:dir_stack_tests(cchar) let save_efm=&efm set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f' - let lines = ["Entering dir 'dir1/a'", - \ 'habits2.txt:1:Nine Healthy Habits', - \ "Entering dir 'b'", - \ 'habits3.txt:2:0 Hours of television', - \ 'habits2.txt:7:5 Small meals', - \ "Entering dir 'dir1/c'", - \ 'habits4.txt:3:1 Hour of exercise', - \ "Leaving dir 'dir1/c'", - \ "Leaving dir 'dir1/a'", - \ 'habits1.txt:4:2 Liters of water', - \ "Entering dir 'dir2'", - \ 'habits5.txt:5:3 Cups of hot green tea', - \ "Leaving dir 'dir2'" - \] + let lines =<< trim [DATA] + Entering dir 'dir1/a' + habits2.txt:1:Nine Healthy Habits + Entering dir 'b' + habits3.txt:2:0 Hours of television + habits2.txt:7:5 Small meals + Entering dir 'dir1/c' + habits4.txt:3:1 Hour of exercise + Leaving dir 'dir1/c' + Leaving dir 'dir1/a' + habits1.txt:4:2 Liters of water + Entering dir 'dir2' + habits5.txt:5:3 Cups of hot green tea + Leaving dir 'dir2 + [DATA] Xexpr "" for l in lines @@ -942,18 +943,20 @@ func Test_efm_dirstack() call mkdir('dir1/c') call mkdir('dir2') - let lines = ["Nine Healthy Habits", - \ "0 Hours of television", - \ "1 Hour of exercise", - \ "2 Liters of water", - \ "3 Cups of hot green tea", - \ "4 Short mental breaks", - \ "5 Small meals", - \ "6 AM wake up time", - \ "7 Minutes of laughter", - \ "8 Hours of sleep (at least)", - \ "9 PM end of the day and off to bed" - \ ] + let lines =<< trim [DATA] + Nine Healthy Habits, + 0 Hours of television, + 1 Hour of exercise, + 2 Liters of water, + 3 Cups of hot green tea, + 4 Short mental breaks, + 5 Small meals, + 6 AM wake up time, + 7 Minutes of laughter, + 8 Hours of sleep (at least), + 9 PM end of the day and off to bed + [DATA] + call writefile(lines, 'habits1.txt') call writefile(lines, 'dir1/a/habits2.txt') call writefile(lines, 'dir1/a/b/habits3.txt') @@ -1049,21 +1052,22 @@ func Test_efm2() call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l) " Test for %P, %Q and %t format specifiers - let lines=["[Xtestfile1]", - \ "(1,17) error: ';' missing", - \ "(21,2) warning: variable 'z' not defined", - \ "(67,3) error: end of file found before string ended", - \ "--", - \ "", - \ "[Xtestfile2]", - \ "--", - \ "", - \ "[Xtestfile3]", - \ "NEW compiler v1.1", - \ "(2,2) warning: variable 'x' not defined", - \ "(67,3) warning: 's' already defined", - \ "--" - \] + let lines =<< trim [DATA] + [Xtestfile1] + (1,17) error: ';' missing + (21,2) warning: variable 'z' not defined + (67,3) error: end of file found before string ended + -- + + [Xtestfile2] + -- + + [Xtestfile3] + NEW compiler v1.1 + (2,2) warning: variable 'x' not defined + (67,3) warning: 's' already defined + - + [DATA] set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r " To exercise the push/pop file functionality in quickfix, the test files " need to be created. @@ -1085,11 +1089,13 @@ func Test_efm2() call delete('Xtestfile3') " Tests for %E, %C and %Z format specifiers - let lines = ["Error 275", - \ "line 42", - \ "column 3", - \ "' ' expected after '--'" - \] + let lines =<< trim [DATA] + Error 275 + line 42 + column 3 + ' ' expected after '--' + [DATA] + set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m cgetexpr lines let l = getqflist() @@ -1100,9 +1106,11 @@ func Test_efm2() call assert_equal("\n' ' expected after '--'", l[0].text) " Test for %> - let lines = ["Error in line 147 of foo.c:", - \"unknown variable 'i'" - \] + let lines =<< trim [DATA] + Error in line 147 of foo.c: + unknown variable 'i' + [DATA] + set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m cgetexpr lines let l = getqflist() @@ -1111,21 +1119,21 @@ func Test_efm2() call assert_equal("\nunknown variable 'i'", l[0].text) " Test for %A, %C and other formats - let lines = [ - \"==============================================================", - \"FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)", - \"--------------------------------------------------------------", - \"Traceback (most recent call last):", - \' File "unittests/dbfacadeTest.py", line 89, in testFoo', - \" self.assertEquals(34, dtid)", - \' File "/usr/lib/python2.2/unittest.py", line 286, in', - \" failUnlessEqual", - \" raise self.failureException, \\", - \"AssertionError: 34 != 33", - \"", - \"--------------------------------------------------------------", - \"Ran 27 tests in 0.063s" - \] + let lines =<< trim [DATA] + ============================================================== + FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest) + -------------------------------------------------------------- + Traceback (most recent call last): + File "unittests/dbfacadeTest.py", line 89, in testFoo + self.assertEquals(34, dtid) + File "/usr/lib/python2.2/unittest.py", line 286, in + failUnlessEqual + raise self.failureException, \\ + AssertionError: 34 != 33 + + -------------------------------------------------------------- + Ran 27 tests in 0.063s + [DATA] set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m cgetexpr lines let l = getqflist() diff --git a/src/nvim/testdir/test_startup.vim b/src/nvim/testdir/test_startup.vim index 1e70f28a00..a38625cca8 100644 --- a/src/nvim/testdir/test_startup.vim +++ b/src/nvim/testdir/test_startup.vim @@ -20,25 +20,27 @@ func Test_after_comes_later() if !has('packages') return endif - let before = [ - \ 'set nocp viminfo+=nviminfo', - \ 'set guioptions+=M', - \ 'let $HOME = "/does/not/exist"', - \ 'set loadplugins', - \ 'set rtp=Xhere,Xafter,Xanother', - \ 'set packpath=Xhere,Xafter', - \ 'set nomore', - \ 'let g:sequence = ""', - \ ] - let after = [ - \ 'redir! > Xtestout', - \ 'scriptnames', - \ 'redir END', - \ 'redir! > Xsequence', - \ 'echo g:sequence', - \ 'redir END', - \ 'quit', - \ ] + let before =<< trim [CODE] + set nocp viminfo+=nviminfo + set guioptions+=M + let $HOME = "/does/not/exist" + set loadplugins + set rtp=Xhere,Xafter,Xanother + set packpath=Xhere,Xafter + set nomore + let g:sequence = "" + [CODE] + + let after =<< trim [CODE] + redir! > Xtestout + scriptnames + redir END + redir! > Xsequence + echo g:sequence + redir END + quit + [CODE] + call mkdir('Xhere/plugin', 'p') call writefile(['let g:sequence .= "here "'], 'Xhere/plugin/here.vim') call mkdir('Xanother/plugin', 'p') @@ -77,15 +79,16 @@ func Test_pack_in_rtp_when_plugins_run() if !has('packages') return endif - let before = [ - \ 'set nocp viminfo+=nviminfo', - \ 'set guioptions+=M', - \ 'let $HOME = "/does/not/exist"', - \ 'set loadplugins', - \ 'set rtp=Xhere', - \ 'set packpath=Xhere', - \ 'set nomore', - \ ] + let before =<< trim [CODE] + set nocp viminfo+=nviminfo + set guioptions+=M + let $HOME = "/does/not/exist" + set loadplugins + set rtp=Xhere + set packpath=Xhere + set nomore + [CODE] + let after = [ \ 'quit', \ ] @@ -133,11 +136,12 @@ endfunc func Test_compatible_args() throw "skipped: Nvim is always 'nocompatible'" - let after = [ - \ 'call writefile([string(&compatible)], "Xtestout")', - \ 'set viminfo+=nviminfo', - \ 'quit', - \ ] + let after =<< trim [CODE] + call writefile([string(&compatible)], "Xtestout") + set viminfo+=nviminfo + quit + [CODE] + if RunVim([], after, '-C') let lines = readfile('Xtestout') call assert_equal('1', lines[0]) @@ -154,14 +158,15 @@ endfunc " Test the -o[N] and -O[N] arguments to open N windows split " horizontally or vertically. func Test_o_arg() - let after = [ - \ 'call writefile([winnr("$"), - \ winheight(1), winheight(2), &lines, - \ winwidth(1), winwidth(2), &columns, - \ bufname(winbufnr(1)), bufname(winbufnr(2))], - \ "Xtestout")', - \ 'qall', - \ ] + let after =<< trim [CODE] + call writefile([winnr("$"), + \ winheight(1), winheight(2), &lines, + \ winwidth(1), winwidth(2), &columns, + \ bufname(winbufnr(1)), bufname(winbufnr(2))], + \ "Xtestout") + qall + [CODE] + if RunVim([], after, '-o2') " Open 2 windows split horizontally. Expect: " - 2 windows @@ -230,10 +235,11 @@ endfunc " Test the -p[N] argument to open N tabpages. func Test_p_arg() - let after = [ - \ 'call writefile(split(execute("tabs"), "\n"), "Xtestout")', - \ 'qall', - \ ] + let after =<< trim [CODE] + call writefile(split(execute("tabs"), "\n"), "Xtestout") + qall + [CODE] + if RunVim([], after, '-p2') let lines = readfile('Xtestout') call assert_equal(4, len(lines)) @@ -290,10 +296,11 @@ endfunc " -M resets 'modifiable' and 'write' " -R sets 'readonly' func Test_m_M_R() - let after = [ - \ 'call writefile([&write, &modifiable, &readonly, &updatecount], "Xtestout")', - \ 'qall', - \ ] + let after =<< trim [CODE] + call writefile([&write, &modifiable, &readonly, &updatecount], "Xtestout") + qall + [CODE] + if RunVim([], after, '') let lines = readfile('Xtestout') call assert_equal(['1', '1', '0', '200'], lines) @@ -316,10 +323,11 @@ endfunc " Test the -A, -F and -H arguments (Arabic, Farsi and Hebrew modes). func Test_A_F_H_arg() - let after = [ - \ 'call writefile([&rightleft, &arabic, 0, &hkmap], "Xtestout")', - \ 'qall', - \ ] + let after =<< trim [CODE] + call writefile([&rightleft, &arabic, 0, &hkmap], "Xtestout") + qall + [CODE] + " Use silent Ex mode to avoid the hit-Enter prompt for the warning that " 'encoding' is not utf-8. if has('arabic') && &encoding == 'utf-8' && RunVim([], after, '-e -s -A') @@ -423,10 +431,11 @@ func Test_invalid_args() endfunc func Test_file_args() - let after = [ - \ 'call writefile(argv(), "Xtestout")', - \ 'qall', - \ ] + let after =<< trim [CODE] + call writefile(argv(), "Xtestout") + qall + [CODE] + if RunVim([], after, '') let lines = readfile('Xtestout') call assert_equal(0, len(lines)) @@ -487,10 +496,11 @@ func Test_startuptime() endfunc func Test_read_stdin() - let after = [ - \ 'write Xtestout', - \ 'quit!', - \ ] + let after =<< trim [CODE] + write Xtestout + quit! + [CODE] + if RunVimPiped([], after, '-', 'echo something | ') let lines = readfile('Xtestout') " MS-Windows adds a space after the word @@ -540,20 +550,22 @@ endfunc func Test_zzz_startinsert() " Test :startinsert call writefile(['123456'], 'Xtestout') - let after = [ - \ ':startinsert', - \ 'call feedkeys("foobar\:wq\","t")' - \ ] + let after =<< trim [CODE] + :startinsert + call feedkeys("foobar\:wq\","t") + [CODE] + if RunVim([], after, 'Xtestout') let lines = readfile('Xtestout') call assert_equal(['foobar123456'], lines) endif " Test :startinsert! call writefile(['123456'], 'Xtestout') - let after = [ - \ ':startinsert!', - \ 'call feedkeys("foobar\:wq\","t")' - \ ] + let after =<< trim [CODE] + :startinsert! + call feedkeys("foobar\:wq\","t") + [CODE] + if RunVim([], after, 'Xtestout') let lines = readfile('Xtestout') call assert_equal(['123456foobar'], lines) From 0586a4b512b2495d32f20c46946d35a0d403bd52 Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Mon, 23 Sep 2019 21:19:26 +0200 Subject: [PATCH 0146/1293] vim-patch:8.1.1588: in :let-heredoc line continuation is recognized Problem: In :let-heredoc line continuation is recognized. Solution: Do not consume line continuation. (Ozaki Kiichi, closes vim/vim#4580) https://github.com/vim/vim/commit/e96a2498f9a2d3e93ac07431f6d4afd77f30afdf --- src/nvim/digraph.c | 2 +- src/nvim/eval.c | 13 +++++++---- src/nvim/ex_cmds.c | 4 ++-- src/nvim/ex_cmds2.c | 6 ++--- src/nvim/ex_cmds_defs.h | 2 +- src/nvim/ex_docmd.c | 52 ++++++++++++++++++++++------------------- src/nvim/ex_getln.c | 23 ++++++++++-------- src/nvim/fileio.c | 10 ++++---- src/nvim/getchar.c | 2 +- src/nvim/normal.c | 2 +- src/nvim/ops.c | 10 ++++---- 11 files changed, 68 insertions(+), 58 deletions(-) diff --git a/src/nvim/digraph.c b/src/nvim/digraph.c index 5a07137831..65d95ff158 100644 --- a/src/nvim/digraph.c +++ b/src/nvim/digraph.c @@ -1887,7 +1887,7 @@ void ex_loadkeymap(exarg_T *eap) // Get each line of the sourced file, break at the end. for (;;) { - line = eap->getline(0, eap->cookie, 0); + line = eap->getline(0, eap->cookie, 0, true); if (line == NULL) { break; diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 6d706939a1..c18d687cc1 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1561,7 +1561,7 @@ heredoc_get(exarg_T *eap, char_u *cmd) for (;;) { int i = 0; - char_u *theline = eap->getline(NUL, eap->cookie, 0); + char_u *theline = eap->getline(NUL, eap->cookie, 0, false); if (theline != NULL && indent_len > 0) { // trim the indent matching the first line if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0) { @@ -8734,7 +8734,7 @@ typedef struct { const listitem_T *li; } GetListLineCookie; -static char_u *get_list_line(int c, void *cookie, int indent) +static char_u *get_list_line(int c, void *cookie, int indent, bool do_concat) { GetListLineCookie *const p = (GetListLineCookie *)cookie; @@ -21279,6 +21279,7 @@ void ex_function(exarg_T *eap) hashitem_T *hi; int sourcing_lnum_off; bool show_block = false; + bool do_concat = true; /* * ":function" without argument: list functions. @@ -21548,9 +21549,9 @@ void ex_function(exarg_T *eap) } else { xfree(line_to_free); if (eap->getline == NULL) { - theline = getcmdline(':', 0L, indent); + theline = getcmdline(':', 0L, indent, do_concat); } else { - theline = eap->getline(':', eap->cookie, indent); + theline = eap->getline(':', eap->cookie, indent, do_concat); } line_to_free = theline; } @@ -21580,6 +21581,7 @@ void ex_function(exarg_T *eap) if (STRCMP(p, skip_until) == 0) { XFREE_CLEAR(skip_until); XFREE_CLEAR(trimmed); + do_concat = true; } } } else { @@ -21699,6 +21701,7 @@ void ex_function(exarg_T *eap) } else { skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p)); } + do_concat = false; } } @@ -23522,7 +23525,7 @@ char_u *get_return_cmd(void *rettv) * Called by do_cmdline() to get the next line. * Returns allocated string, or NULL for end of function. */ -char_u *get_func_line(int c, void *cookie, int indent) +char_u *get_func_line(int c, void *cookie, int indent, bool do_concat) { funccall_T *fcp = (funccall_T *)cookie; ufunc_T *fp = fcp->func; diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index b2d7ded6be..00de7a3d66 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -2767,7 +2767,7 @@ void ex_append(exarg_T *eap) State = CMDLINE; theline = eap->getline( eap->cstack->cs_looplevel > 0 ? -1 : - NUL, eap->cookie, indent); + NUL, eap->cookie, indent, true); State = save_State; } lines_left = Rows - 1; @@ -3630,7 +3630,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, for (; i <= (long)ec; ++i) msg_putchar('^'); - resp = getexmodeline('?', NULL, 0); + resp = getexmodeline('?', NULL, 0, true); if (resp != NULL) { typed = *resp; xfree(resp); diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 87eae2dd4f..272c81e29b 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -3218,7 +3218,7 @@ int do_source(char_u *fname, int check_other, int is_vimrc) cookie.conv.vc_type = CONV_NONE; // no conversion // Read the first line so we can check for a UTF-8 BOM. - firstline = getsourceline(0, (void *)&cookie, 0); + firstline = getsourceline(0, (void *)&cookie, 0, true); if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef && firstline[1] == 0xbb && firstline[2] == 0xbf) { // Found BOM; setup conversion, skip over BOM and recode the line. @@ -3381,7 +3381,7 @@ void free_scriptnames(void) /// /// @return pointer to the line in allocated memory, or NULL for end-of-file or /// some error. -char_u *getsourceline(int c, void *cookie, int indent) +char_u *getsourceline(int c, void *cookie, int indent, bool do_concat) { struct source_cookie *sp = (struct source_cookie *)cookie; char_u *line; @@ -3412,7 +3412,7 @@ char_u *getsourceline(int c, void *cookie, int indent) // Only concatenate lines starting with a \ when 'cpoptions' doesn't // contain the 'C' flag. - if (line != NULL && (vim_strchr(p_cpo, CPO_CONCAT) == NULL)) { + if (line != NULL && do_concat && (vim_strchr(p_cpo, CPO_CONCAT) == NULL)) { // compensate for the one line read-ahead sourcing_lnum--; diff --git a/src/nvim/ex_cmds_defs.h b/src/nvim/ex_cmds_defs.h index 42ba1060e9..3a9fd01dd9 100644 --- a/src/nvim/ex_cmds_defs.h +++ b/src/nvim/ex_cmds_defs.h @@ -88,7 +88,7 @@ typedef struct exarg exarg_T; typedef void (*ex_func_T)(exarg_T *eap); -typedef char_u *(*LineGetter)(int, void *, int); +typedef char_u *(*LineGetter)(int, void *, int, bool); /// Structure for command definition. typedef struct cmdname { diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 8d67ab5348..0da2cd67d6 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -117,11 +117,11 @@ typedef struct { * reads more lines that may come from the while/for loop. */ struct loop_cookie { - garray_T *lines_gap; /* growarray with line info */ - int current_line; /* last read line from growarray */ - int repeating; /* TRUE when looping a second time */ - /* When "repeating" is FALSE use "getline" and "cookie" to get lines */ - char_u *(*getline)(int, void *, int); + garray_T *lines_gap; // growarray with line info + int current_line; // last read line from growarray + int repeating; // TRUE when looping a second time + // When "repeating" is FALSE use "getline" and "cookie" to get lines + char_u *(*getline)(int, void *, int, bool); void *cookie; }; @@ -313,8 +313,8 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, struct msglist **saved_msg_list = NULL; struct msglist *private_msg_list; - /* "fgetline" and "cookie" passed to do_one_cmd() */ - char_u *(*cmd_getline)(int, void *, int); + // "fgetline" and "cookie" passed to do_one_cmd() + char_u *(*cmd_getline)(int, void *, int, bool); void *cmd_cookie; struct loop_cookie cmd_loop_cookie; void *real_cookie; @@ -507,17 +507,20 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, * Need to set msg_didout for the first line after an ":if", * otherwise the ":if" will be overwritten. */ - if (count == 1 && getline_equal(fgetline, cookie, getexline)) - msg_didout = TRUE; - if (fgetline == NULL || (next_cmdline = fgetline(':', cookie, - cstack.cs_idx < - 0 ? 0 : (cstack.cs_idx + 1) * 2 - )) == NULL) { - /* Don't call wait_return for aborted command line. The NULL - * returned for the end of a sourced file or executed function - * doesn't do this. */ - if (KeyTyped && !(flags & DOCMD_REPEAT)) - need_wait_return = FALSE; + if (count == 1 && getline_equal(fgetline, cookie, getexline)) { + msg_didout = true; + } + if (fgetline == NULL + || (next_cmdline = fgetline(':', cookie, + cstack.cs_idx < + 0 ? 0 : (cstack.cs_idx + 1) * 2, + true)) == NULL) { + // Don't call wait_return for aborted command line. The NULL + // returned for the end of a sourced file or executed function + // doesn't do this. + if (KeyTyped && !(flags & DOCMD_REPEAT)) { + need_wait_return = false; + } retval = FAIL; break; } @@ -951,7 +954,7 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline, /* * Obtain a line when inside a ":while" or ":for" loop. */ -static char_u *get_loop_line(int c, void *cookie, int indent) +static char_u *get_loop_line(int c, void *cookie, int indent, bool do_concat) { struct loop_cookie *cp = (struct loop_cookie *)cookie; wcmd_T *wp; @@ -961,11 +964,12 @@ static char_u *get_loop_line(int c, void *cookie, int indent) if (cp->repeating) return NULL; /* trying to read past ":endwhile"/":endfor" */ - /* First time inside the ":while"/":for": get line normally. */ - if (cp->getline == NULL) - line = getcmdline(c, 0L, indent); - else - line = cp->getline(c, cp->cookie, indent); + // First time inside the ":while"/":for": get line normally. + if (cp->getline == NULL) { + line = getcmdline(c, 0L, indent, do_concat); + } else { + line = cp->getline(c, cp->cookie, indent, do_concat); + } if (line != NULL) { store_loop_line(cp->lines_gap, line); ++cp->current_line; diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index d0af8a0fdf..5235b9e648 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -1991,7 +1991,8 @@ char_u * getcmdline ( int firstc, long count, // only used for incremental search - int indent // indent for inside conditionals + int indent, // indent for inside conditionals + bool do_concat // unused ) { // Be prepared for situations where cmdline can be invoked recursively. @@ -2167,17 +2168,18 @@ static void correct_screencol(int idx, int cells, int *col) * Get an Ex command line for the ":" command. */ char_u * -getexline ( - int c, /* normally ':', NUL for ":append" */ +getexline( + int c, // normally ':', NUL for ":append" void *cookie, - int indent /* indent for inside conditionals */ + int indent, // indent for inside conditionals + bool do_concat ) { /* When executing a register, remove ':' that's in front of each line. */ if (exec_from_reg && vpeekc() == ':') (void)vgetc(); - return getcmdline(c, 1L, indent); + return getcmdline(c, 1L, indent, do_concat); } /* @@ -2187,11 +2189,12 @@ getexline ( * Returns a string in allocated memory or NULL. */ char_u * -getexmodeline ( - int promptc, /* normally ':', NUL for ":append" and '?' for - :s prompt */ +getexmodeline( + int promptc, // normally ':', NUL for ":append" and '?' + // for :s prompt void *cookie, - int indent /* indent for inside conditionals */ + int indent, // indent for inside conditionals + bool do_concat ) { garray_T line_ga; @@ -6308,7 +6311,7 @@ char *script_get(exarg_T *const eap, size_t *const lenp) for (;;) { char *const theline = (char *)eap->getline( eap->cstack->cs_looplevel > 0 ? -1 : - NUL, eap->cookie, 0); + NUL, eap->cookie, 0, true); if (theline == NULL || strcmp(end_pattern, theline) == 0) { xfree(theline); diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 0394639a16..58e6b2ae92 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -7100,12 +7100,10 @@ auto_next_pat( } } -/* - * Get next autocommand command. - * Called by do_cmdline() to get the next line for ":if". - * Returns allocated string, or NULL for end of autocommands. - */ -char_u *getnextac(int c, void *cookie, int indent) +/// Get next autocommand command. +/// Called by do_cmdline() to get the next line for ":if". +/// @return allocated string, or NULL for end of autocommands. +char_u *getnextac(int c, void *cookie, int indent, bool do_concat) { AutoPatCmd *acp = (AutoPatCmd *)cookie; char_u *retval; diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 1f82df3241..399f0671b4 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -4419,7 +4419,7 @@ mapblock_T *get_maphash(int index, buf_T *buf) } /// Get command argument for key -char_u * getcmdkeycmd(int promptc, void *cookie, int indent) +char_u * getcmdkeycmd(int promptc, void *cookie, int indent, bool do_concat) { garray_T line_ga; int c1 = -1, c2; diff --git a/src/nvim/normal.c b/src/nvim/normal.c index e32b738c7e..e0dc9d4f23 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -5343,7 +5343,7 @@ static void nv_search(cmdarg_T *cap) // When using 'incsearch' the cursor may be moved to set a different search // start position. - cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0); + cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, true); if (cap->searchbuf == NULL) { clearop(oap); diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 6f515151d6..0d27365d2b 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -671,13 +671,15 @@ int get_expr_register(void) { char_u *new_line; - new_line = getcmdline('=', 0L, 0); - if (new_line == NULL) + new_line = getcmdline('=', 0L, 0, true); + if (new_line == NULL) { return NUL; - if (*new_line == NUL) /* use previous line */ + } + if (*new_line == NUL) { // use previous line xfree(new_line); - else + } else { set_expr_line(new_line); + } return '='; } From 7faa6c41c89f1c5d48f92a436ed690bc7ce6ea85 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 7 Oct 2019 17:42:40 +0200 Subject: [PATCH 0147/1293] cmake: only set LUA_PRG with successful check (#11172) This is relevant for when using `USE_BUNDLED_LUAJIT=ON` with `USE_BUNDLED_LUAROCKS=OFF`, and then building without the necessary modules being installed/activated there yet: it would check the other (system) "lua" interpreters also, and in case all failed keep the `LUA_PRG` in the cache for the last failed entry - making it not re-check the previous ones on the next build (after you might have activated your custom LuaRocks installation). Only setting LUA_PRG if the check was successful handles the case better where it is configured already - we should not try to re-configure it then. --- CMakeLists.txt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b6ba45820..98a32a116b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -487,18 +487,19 @@ include(LuaHelpers) set(LUA_DEPENDENCIES lpeg mpack bit) if(NOT LUA_PRG) foreach(CURRENT_LUA_PRG luajit lua5.1 lua5.2 lua) - # If LUA_PRG is set find_program() will not search - unset(LUA_PRG CACHE) + unset(_CHECK_LUA_PRG CACHE) unset(LUA_PRG_WORKS) - find_program(LUA_PRG ${CURRENT_LUA_PRG}) + find_program(_CHECK_LUA_PRG ${CURRENT_LUA_PRG}) - if(LUA_PRG) - check_lua_deps(${LUA_PRG} "${LUA_DEPENDENCIES}" LUA_PRG_WORKS) + if(_CHECK_LUA_PRG) + check_lua_deps(${_CHECK_LUA_PRG} "${LUA_DEPENDENCIES}" LUA_PRG_WORKS) if(LUA_PRG_WORKS) + set(LUA_PRG "${_CHECK_LUA_PRG}" CACHE FILEPATH "Path to a program.") break() endif() endif() endforeach() + unset(_CHECK_LUA_PRG CACHE) else() check_lua_deps(${LUA_PRG} "${LUA_DEPENDENCIES}" LUA_PRG_WORKS) endif() From 7e4b744b6429561b93489e4d639b6374bb260afc Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 8 Oct 2019 17:36:58 +0200 Subject: [PATCH 0148/1293] ci: OpenBSD: enable functionaltest (#11178) --- .builds/openbsd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.builds/openbsd.yml b/.builds/openbsd.yml index a8f6992e3f..6796802051 100644 --- a/.builds/openbsd.yml +++ b/.builds/openbsd.yml @@ -35,7 +35,7 @@ tasks: export LC_CTYPE=en_US.UTF-8 # functional tests cd neovim/build - # cmake --build . --config Debug --target functionaltest + cmake --build . --config Debug --target functionaltest # oldtests cd .. gmake oldtest From db9f68f98d9cebf372c83a9c512499e8388c884a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 9 Oct 2019 03:07:42 +0200 Subject: [PATCH 0149/1293] ci: AppVeyor: coverage for Lua (Windows) (#10426) --- ci/build.ps1 | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/ci/build.ps1 b/ci/build.ps1 index 07da6c4e19..6d91b97aed 100644 --- a/ci/build.ps1 +++ b/ci/build.ps1 @@ -46,6 +46,10 @@ if ($compiler -eq 'MINGW') { $nvimCmakeVars['USE_GCOV'] = 'ON' $uploadToCodecov = $true $env:GCOV = "C:\msys64\mingw$bits\bin\gcov" + + # Setup/build Lua coverage. + $env:USE_LUACOV = 1 + $env:BUSTED_ARGS = "--coverage" } # These are native MinGW builds, but they use the toolchain inside # MSYS2, this allows using all the dependencies and tools available @@ -135,6 +139,10 @@ cmake --build . --config $cmakeBuildType -- $cmakeGeneratorArgs ; exitIfFailed # Ensure that the "win32" feature is set. .\bin\nvim -u NONE --headless -c 'exe !has(\"win32\").\"cq\"' ; exitIfFailed +if ($env:USE_LUACOV -eq 1) { + & $env:DEPS_PREFIX\luarocks\luarocks.bat install cluacov +} + # Functional tests # The $LastExitCode from MSBuild can't be trusted $failed = $false @@ -143,19 +151,18 @@ Set-PSDebug -Off cmake --build . --config $cmakeBuildType --target functionaltest -- $cmakeGeneratorArgs 2>&1 | foreach { $failed = $failed -or $_ -match 'functional tests failed with error'; $_ } -if ($failed) { - if ($uploadToCodecov) { - bash -l /c/projects/neovim/ci/common/submit_coverage.sh functionaltest + +if ($uploadToCodecov) { + if ($env:USE_LUACOV -eq 1) { + & $env:DEPS_PREFIX\bin\luacov.bat } + bash -l /c/projects/neovim/ci/common/submit_coverage.sh functionaltest +} +if ($failed) { exit $LastExitCode } Set-PSDebug -Strict -Trace 1 - -if ($uploadToCodecov) { - bash -l /c/projects/neovim/ci/common/submit_coverage.sh functionaltest -} - # Old tests # Add MSYS to path, required for e.g. `find` used in test scripts. # But would break functionaltests, where its `more` would be used then. From f2ad93168b15dd808fff545cc8bec510cc020e0a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 9 Oct 2019 19:44:44 +0200 Subject: [PATCH 0150/1293] third-party: upgrade libvterm to 0.1.2 (#11177) --- third-party/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index 2dbb444aa5..dbb113aa0f 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -162,8 +162,8 @@ set(UNIBILIUM_SHA256 29815283c654277ef77a3adcc8840db79ddbb20a0f0b0c8f648bd8cd49a set(LIBTERMKEY_URL http://www.leonerd.org.uk/code/libtermkey/libtermkey-0.21.1.tar.gz) set(LIBTERMKEY_SHA256 cecbf737f35d18f433c8d7864f63c0f878af41f8bd0255a3ebb16010dc044d5f) -set(LIBVTERM_URL https://github.com/neovim/libvterm/archive/4a5fa43e0dbc0db4fe67d40d788d60852864df9e.tar.gz) -set(LIBVTERM_SHA256 49b3cf2dcb988b887671b1011cfeac98ff81bb5c23fb4ac34b91a59524992935) +set(LIBVTERM_URL https://github.com/neovim/libvterm/archive/7c72294d84ce20da4c27362dbd7fa4b08cfc91da.tar.gz) +set(LIBVTERM_SHA256 f30c4d43e0c6db3e0912daf7188d98fbf6ee88f97589d72f6f304e5db48826a8) set(LUV_VERSION 1.30.1-1) set(LUV_URL https://github.com/luvit/luv/archive/${LUV_VERSION}.tar.gz) From 51f2826f617532aaf5d682dfc3229f3723427ce6 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Thu, 10 Oct 2019 04:16:02 -0400 Subject: [PATCH 0151/1293] doc: update shellquote for powershell #11122 shellquote is not treated like shellxquote for non-quote values. --- runtime/doc/options.txt | 2 +- test/functional/helpers.lua | 15 +++++++++++++-- test/functional/ui/output_spec.lua | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 8518d989d6..971c4ffbd5 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -5169,7 +5169,7 @@ A jump table for the options with a short description can be found at |Q_op|. unescaping, so to keep yourself sane use |:let-&| like shown above. *shell-powershell* To use powershell (on Windows): > - set shell=powershell shellquote=( shellpipe=\| shellxquote= + set shell=powershell shellquote= shellpipe=\| shellxquote= set shellcmdflag=-NoLogo\ -NoProfile\ -ExecutionPolicy\ RemoteSigned\ -Command set shellredir=\|\ Out-File\ -Encoding\ UTF8 diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index c195983e93..20371b8ab0 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -501,9 +501,20 @@ function module.source(code) end function module.set_shell_powershell() + local shell = iswin() and 'powershell' or 'pwsh' + if not module.eval('executable("'..shell..'")') then + error(shell..' is not executable') + end + local aliases = iswin() and {'cat', 'sleep'} or {} + local cmd = '' + for _, alias in ipairs(aliases) do + cmd = cmd .. 'Remove-Item -Force alias:' .. alias .. ';' + end module.source([[ - set shell=powershell shellquote=( shellpipe=\| shellredir=> shellxquote= - let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command Remove-Item -Force alias:sleep; Remove-Item -Force alias:cat;' + let &shell = ']]..shell..[[' + set shellquote= shellpipe=\| shellxquote= + let &shellredir = '| Out-File -Encoding UTF8' + let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ]]..cmd..[[' ]]) end diff --git a/test/functional/ui/output_spec.lua b/test/functional/ui/output_spec.lua index c028f44b44..c5d3e536ad 100644 --- a/test/functional/ui/output_spec.lua +++ b/test/functional/ui/output_spec.lua @@ -10,6 +10,7 @@ local iswin = helpers.iswin local clear = helpers.clear local command = helpers.command local nvim_dir = helpers.nvim_dir +local set_shell_powershell = helpers.set_shell_powershell describe("shell command :!", function() local screen @@ -230,4 +231,19 @@ describe("shell command :!", function() ]]) end) end) + if iswin() or eval('executable("pwsh")') == 1 then + it('powershell supports literal strings', function() + set_shell_powershell() + local screen = Screen.new(30, 4) + screen:attach() + feed_command([[!'echo $a']]) + screen:expect{any='\necho %$a', timeout=10000} + feed_command([[!$a = 1; echo '$a']]) + screen:expect{any='\n%$a', timeout=10000} + feed_command([[!"echo $a"]]) + screen:expect{any='\necho', timeout=10000} + feed_command([[!$a = 1; echo "$a"]]) + screen:expect{any='\n1', timeout=10000} + end) + end end) From 6768c43e2129af85923ed0b4ed9f6f47be42b4fb Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 10 Oct 2019 10:38:15 +0200 Subject: [PATCH 0152/1293] update_version_stamp: redirect stderr on first try, --first-parent #11186 Avoid noise during builds: > fatal: No annotated tags can describe '417449f468c4ba186954f6295b3338fb55ee7b4a'. > However, there were unannotated tags: try --tags. This might be useful in general, but is expected to not happen - and falling back is OK then. The fallback command would still display errors then. It also uses `--first-parent`, which is important for when a release branch gets merged back. --- scripts/update_version_stamp.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/update_version_stamp.lua b/scripts/update_version_stamp.lua index 509e1f6fad..394c4f7694 100755 --- a/scripts/update_version_stamp.lua +++ b/scripts/update_version_stamp.lua @@ -20,9 +20,9 @@ end local versiondeffile = arg[1] local prefix = arg[2] -local described = io.popen('git describe --dirty'):read('*l') +local described = io.popen('git describe --first-parent --dirty 2>/dev/null'):read('*l') if not described then - described = io.popen('git describe --tags --always --dirty'):read('*l') + described = io.popen('git describe --first-parent --tags --always --dirty'):read('*l') end if not described then io.open(versiondeffile, 'w'):write('\n') From b772b86d2ba256a2c03ab701d00b322cf52560e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Thu, 10 Oct 2019 22:08:27 +0200 Subject: [PATCH 0153/1293] Remove "highbright bold" conversion. Fixes #11190 When using TUI host terminal should take care of this (regardless if 'termguicolors' is active or not). For GUI the behavior doesn't make sense (GUI should display bold attr as bold always). --- src/nvim/terminal.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 8fcc8bf0a5..7609006906 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -220,8 +220,6 @@ Terminal *terminal_open(TerminalOptions opts) rv->sb_size = (size_t)curbuf->b_p_scbk; rv->sb_buffer = xmalloc(sizeof(ScrollbackLine *) * rv->sb_size); - vterm_state_set_bold_highbright(state, true); - // Configure the color palette. Try to get the color from: // // - b:terminal_color_{NUM} From a7fc2f3f64f05ffd2a97c8ccf2e5c74d905ac808 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 11 Oct 2019 00:30:20 -0400 Subject: [PATCH 0154/1293] test: "!:&" works with powershell #11201 Removed 'echo' alias because it does not behave like POSIX echo. --- test/functional/eval/system_spec.lua | 8 ++++---- test/functional/helpers.lua | 12 ++++-------- test/functional/ui/output_spec.lua | 16 ++++++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/test/functional/eval/system_spec.lua b/test/functional/eval/system_spec.lua index 0a478fd05c..85d57006b5 100644 --- a/test/functional/eval/system_spec.lua +++ b/test/functional/eval/system_spec.lua @@ -84,7 +84,7 @@ describe('system()', function() it('does NOT run in shell', function() if iswin() then - eq("%PATH%\n", eval("system(['powershell', '-NoProfile', '-NoLogo', '-ExecutionPolicy', 'RemoteSigned', '-Command', 'echo', '%PATH%'])")) + eq("%PATH%\n", eval("system(['powershell', '-NoProfile', '-NoLogo', '-ExecutionPolicy', 'RemoteSigned', '-Command', 'Write-Output', '%PATH%'])")) else eq("* $PATH %PATH%\n", eval("system(['echo', '*', '$PATH', '%PATH%'])")) end @@ -133,7 +133,7 @@ describe('system()', function() eval([[system('"ping" "-n" "1" "127.0.0.1"')]]) eq(0, eval('v:shell_error')) eq('"a b"\n', eval([[system('cmd /s/c "cmd /s/c "cmd /s/c "echo "a b""""')]])) - eq('"a b"\n', eval([[system('powershell -NoProfile -NoLogo -ExecutionPolicy RemoteSigned -Command echo ''\^"a b\^"''')]])) + eq('"a b"\n', eval([[system('powershell -NoProfile -NoLogo -ExecutionPolicy RemoteSigned -Command Write-Output ''\^"a b\^"''')]])) end it('with shell=cmd.exe', function() @@ -169,9 +169,9 @@ describe('system()', function() it('works with powershell', function() helpers.set_shell_powershell() - eq('a\nb\n', eval([[system('echo a b')]])) + eq('a\nb\n', eval([[system('Write-Output a b')]])) eq('C:\\\n', eval([[system('cd c:\; (Get-Location).Path')]])) - eq('a b\n', eval([[system('echo "a b"')]])) + eq('a b\n', eval([[system('Write-Output "a b"')]])) end) end diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index 20371b8ab0..2473fc0d3b 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -502,14 +502,10 @@ end function module.set_shell_powershell() local shell = iswin() and 'powershell' or 'pwsh' - if not module.eval('executable("'..shell..'")') then - error(shell..' is not executable') - end - local aliases = iswin() and {'cat', 'sleep'} or {} - local cmd = '' - for _, alias in ipairs(aliases) do - cmd = cmd .. 'Remove-Item -Force alias:' .. alias .. ';' - end + assert(module.eval('executable("'..shell..'")')) + local cmd = 'Remove-Item -Force '..table.concat(iswin() + and {'alias:cat', 'alias:echo', 'alias:sleep'} + or {'alias:echo'}, ',')..';' module.source([[ let &shell = ']]..shell..[[' set shellquote= shellpipe=\| shellxquote= diff --git a/test/functional/ui/output_spec.lua b/test/functional/ui/output_spec.lua index c5d3e536ad..20413cb784 100644 --- a/test/functional/ui/output_spec.lua +++ b/test/functional/ui/output_spec.lua @@ -236,14 +236,18 @@ describe("shell command :!", function() set_shell_powershell() local screen = Screen.new(30, 4) screen:attach() - feed_command([[!'echo $a']]) - screen:expect{any='\necho %$a', timeout=10000} - feed_command([[!$a = 1; echo '$a']]) + feed_command([[!'Write-Output $a']]) + screen:expect{any='\nWrite%-Output %$a', timeout=10000} + feed_command([[!$a = 1; Write-Output '$a']]) screen:expect{any='\n%$a', timeout=10000} - feed_command([[!"echo $a"]]) - screen:expect{any='\necho', timeout=10000} - feed_command([[!$a = 1; echo "$a"]]) + feed_command([[!"Write-Output $a"]]) + screen:expect{any='\nWrite%-Output', timeout=10000} + feed_command([[!$a = 1; Write-Output "$a"]]) screen:expect{any='\n1', timeout=10000} + feed_command(iswin() + and [[!& 'C:\\Windows\\system32\\cmd.exe' /c 'echo $a']] + or [[!& '/bin/sh' -c 'echo ''$a''']]) + screen:expect{any='\n%$a', timeout=10000} end) end end) From 5f60861f5a7c7c588e1d638f734897bc5dc291cc Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 26 Sep 2019 23:04:59 +0100 Subject: [PATCH 0155/1293] fnamemodify: fix handling of :r after :e #11165 - Test fnamemodify() - Test handling of `expand("%:e:e:r")`. - Fix :e:e:r on filenames with insufficiently many extensions During `fnamemodify()`, ensuring that we don't go before the filename's tail is insufficient in cases where we've already handled a ":e" modifier, for example: ``` "path/to/this.file.ext" :e:e:r:r ^ ^-------- *fnamep +------------- tail ``` This means for a ":r", we'll go before `*fnamep`, and outside the bounds of the filename. This is both incorrect and causes neovim to exit with an allocation error. We exit because we attempt to calculate `s - *fnamep` (line 23948). Since `s` is before `*fnamep`, we caluclate a negative length, which ends up being interpreted as an amount to allocate, causing neovim to exit with ENOMEM (`memory.c:xmalloc`). We must instead ensure we don't go before `*fnamep` nor `tail`. The check for `tail` is still relevant, for example: ``` "path/to/this.file.ext" :r:r:r ^ ^------------- tail +--------------------- *fnamep ``` Here we don't want to go before `tail`. close #11165 --- src/nvim/eval.c | 47 +++++++-- test/functional/eval/fnamemodify_spec.lua | 119 +++++++++++++++++++++- 2 files changed, 154 insertions(+), 12 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 7d641f1295..a899dbcd3a 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -24051,22 +24051,47 @@ repeat: * - for second :e: before the current fname * - otherwise: The last '.' */ - if (src[*usedlen + 1] == 'e' && *fnamep > tail) + const bool is_second_e = *fnamep > tail; + if (src[*usedlen + 1] == 'e' && is_second_e) { s = *fnamep - 2; - else + } else { s = *fnamep + *fnamelen - 1; - for (; s > tail; --s) - if (s[0] == '.') + } + + for (; s > tail; s--) { + if (s[0] == '.') { break; - if (src[*usedlen + 1] == 'e') { /* :e */ - if (s > tail) { - *fnamelen += (size_t)(*fnamep - (s + 1)); - *fnamep = s + 1; - } else if (*fnamep <= tail) + } + } + if (src[*usedlen + 1] == 'e') { + if (s > tail || (0 && is_second_e && s == tail)) { + // we stopped at a '.' (so anchor to &'.' + 1) + char_u *newstart = s + 1; + size_t distance_stepped_back = *fnamep - newstart; + *fnamelen += distance_stepped_back; + *fnamep = newstart; + } else if (*fnamep <= tail) { *fnamelen = 0; - } else { /* :r */ - if (s > tail) /* remove one extension */ + } + } else { + // :r - Remove one extension + // + // Ensure that `s` doesn't go before `*fnamep`, + // since then we're taking too many roots: + // + // "path/to/this.file.ext" :e:e:r:r + // ^ ^-------- *fnamep + // +------------- tail + // + // Also ensure `s` doesn't go before `tail`, + // since then we're taking too many roots again: + // + // "path/to/this.file.ext" :r:r:r + // ^ ^------------- tail + // +--------------------- *fnamep + if (s > MAX(tail, *fnamep)) { *fnamelen = (size_t)(s - *fnamep); + } } *usedlen += 2; } diff --git a/test/functional/eval/fnamemodify_spec.lua b/test/functional/eval/fnamemodify_spec.lua index fe6b50a544..d54a6db417 100644 --- a/test/functional/eval/fnamemodify_spec.lua +++ b/test/functional/eval/fnamemodify_spec.lua @@ -3,8 +3,14 @@ local clear = helpers.clear local eq = helpers.eq local iswin = helpers.iswin local fnamemodify = helpers.funcs.fnamemodify +local getcwd = helpers.funcs.getcwd local command = helpers.command local write_file = helpers.write_file +local alter_slashes = helpers.alter_slashes + +local function eq_slashconvert(expected, got) + eq(alter_slashes(expected), alter_slashes(got)) +end describe('fnamemodify()', function() setup(function() @@ -17,7 +23,7 @@ describe('fnamemodify()', function() os.remove('Xtest-fnamemodify.txt') end) - it('works', function() + it('handles the root path', function() local root = helpers.pathroot() eq(root, fnamemodify([[/]], ':p:h')) eq(root, fnamemodify([[/]], ':p')) @@ -36,4 +42,115 @@ describe('fnamemodify()', function() it(':8 works', function() eq('Xtest-fnamemodify.txt', fnamemodify([[Xtest-fnamemodify.txt]], ':8')) end) + + it('handles examples from ":help filename-modifiers"', function() + local filename = "src/version.c" + local cwd = getcwd() + + eq_slashconvert(cwd .. '/src/version.c', fnamemodify(filename, ':p')) + + eq_slashconvert('src/version.c', fnamemodify(filename, ':p:.')) + eq_slashconvert(cwd .. '/src', fnamemodify(filename, ':p:h')) + eq_slashconvert(cwd .. '', fnamemodify(filename, ':p:h:h')) + eq('version.c', fnamemodify(filename, ':p:t')) + eq_slashconvert(cwd .. '/src/version', fnamemodify(filename, ':p:r')) + + eq_slashconvert(cwd .. '/src/main.c', fnamemodify(filename, ':s?version?main?:p')) + + local converted_cwd = cwd:gsub('/', '\\') + eq(converted_cwd .. '\\src\\version.c', fnamemodify(filename, ':p:gs?/?\\\\?')) + + eq('src', fnamemodify(filename, ':h')) + eq('version.c', fnamemodify(filename, ':t')) + eq_slashconvert('src/version', fnamemodify(filename, ':r')) + eq('version', fnamemodify(filename, ':t:r')) + eq('c', fnamemodify(filename, ':e')) + + eq_slashconvert('src/main.c', fnamemodify(filename, ':s?version?main?')) + end) + + it('handles advanced examples from ":help filename-modifiers"', function() + local filename = "src/version.c.gz" + + eq('gz', fnamemodify(filename, ':e')) + eq('c.gz', fnamemodify(filename, ':e:e')) + eq('c.gz', fnamemodify(filename, ':e:e:e')) + + eq('c', fnamemodify(filename, ':e:e:r')) + + eq_slashconvert('src/version.c', fnamemodify(filename, ':r')) + eq('c', fnamemodify(filename, ':r:e')) + + eq_slashconvert('src/version', fnamemodify(filename, ':r:r')) + eq_slashconvert('src/version', fnamemodify(filename, ':r:r:r')) + end) + + it('handles :h', function() + eq('.', fnamemodify('hello.txt', ':h')) + + eq_slashconvert('path/to', fnamemodify('path/to/hello.txt', ':h')) + end) + + it('handles :t', function() + eq('hello.txt', fnamemodify('hello.txt', ':t')) + eq_slashconvert('hello.txt', fnamemodify('path/to/hello.txt', ':t')) + end) + + it('handles :r', function() + eq('hello', fnamemodify('hello.txt', ':r')) + eq_slashconvert('path/to/hello', fnamemodify('path/to/hello.txt', ':r')) + end) + + it('handles :e', function() + eq('txt', fnamemodify('hello.txt', ':e')) + eq_slashconvert('txt', fnamemodify('path/to/hello.txt', ':e')) + end) + + it('handles regex replacements', function() + eq('content-there-here.txt', fnamemodify('content-here-here.txt', ':s/here/there/')) + eq('content-there-there.txt', fnamemodify('content-here-here.txt', ':gs/here/there/')) + end) + + it('handles shell escape', function() + local expected + + if iswin() then + -- we expand with double-quotes on Windows + expected = [["hello there! quote ' newline]] .. '\n' .. [["]] + else + expected = [['hello there! quote '\'' newline]] .. '\n' .. [[']] + end + + eq(expected, fnamemodify("hello there! quote ' newline\n", ':S')) + end) + + it('can combine :e and :r', function() + -- simple, single extension filename + eq('c', fnamemodify('a.c', ':e')) + eq('c', fnamemodify('a.c', ':e:e')) + eq('c', fnamemodify('a.c', ':e:e:r')) + eq('c', fnamemodify('a.c', ':e:e:r:r')) + + -- multi extension filename + eq('rb', fnamemodify('a.spec.rb', ':e:r')) + eq('rb', fnamemodify('a.spec.rb', ':e:r:r')) + + eq('spec', fnamemodify('a.spec.rb', ':e:e:r')) + eq('spec', fnamemodify('a.spec.rb', ':e:e:r:r')) + + eq('spec', fnamemodify('a.b.spec.rb', ':e:e:r')) + eq('b.spec', fnamemodify('a.b.spec.rb', ':e:e:e:r')) + eq('b', fnamemodify('a.b.spec.rb', ':e:e:e:r:r')) + + eq('spec', fnamemodify('a.b.spec.rb', ':r:e')) + eq('b', fnamemodify('a.b.spec.rb', ':r:r:e')) + + -- extraneous :e expansions + eq('c', fnamemodify('a.b.c.d.e', ':r:r:e')) + eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e')) + + -- :e never includes the whole filename, so "a.b":e:e:e --> "b" + eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e')) + eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e:e')) + end) end) From 401398bc4b07301e8f7ad6c288dbc799624b1a2d Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 10 Oct 2019 22:13:23 -0700 Subject: [PATCH 0156/1293] vim-patch:8.1.2125: fnamemodify() fails when repeating :e Problem: Fnamemodify() fails when repeating :e. Solution: Do not go before the tail. (Rob Pilling, closes vim/vim#5024) https://github.com/vim/vim/commit/b189295b72030f00c45c30d3daecf85d457221f8 --- src/nvim/testdir/test_fnamemodify.vim | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/nvim/testdir/test_fnamemodify.vim b/src/nvim/testdir/test_fnamemodify.vim index 63f273677d..116d23ba88 100644 --- a/src/nvim/testdir/test_fnamemodify.vim +++ b/src/nvim/testdir/test_fnamemodify.vim @@ -45,3 +45,31 @@ func Test_fnamemodify() let $HOME = save_home let &shell = save_shell endfunc + +func Test_fnamemodify_er() + call assert_equal("with", fnamemodify("path/to/file.with.extensions", ':e:e:r:r')) + + call assert_equal('c', fnamemodify('a.c', ':e')) + call assert_equal('c', fnamemodify('a.c', ':e:e')) + call assert_equal('c', fnamemodify('a.c', ':e:e:r')) + call assert_equal('c', fnamemodify('a.c', ':e:e:r:r')) + + call assert_equal('rb', fnamemodify('a.spec.rb', ':e:r')) + call assert_equal('rb', fnamemodify('a.spec.rb', ':e:r')) + call assert_equal('spec.rb', fnamemodify('a.spec.rb', ':e:e')) + call assert_equal('spec', fnamemodify('a.spec.rb', ':e:e:r')) + call assert_equal('spec', fnamemodify('a.spec.rb', ':e:e:r:r')) + call assert_equal('spec', fnamemodify('a.b.spec.rb', ':e:e:r')) + call assert_equal('b.spec', fnamemodify('a.b.spec.rb', ':e:e:e:r')) + call assert_equal('b', fnamemodify('a.b.spec.rb', ':e:e:e:r:r')) + + call assert_equal('spec', fnamemodify('a.b.spec.rb', ':r:e')) + call assert_equal('b', fnamemodify('a.b.spec.rb', ':r:r:e')) + + call assert_equal('c', fnamemodify('a.b.c.d.e', ':r:r:e')) + call assert_equal('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e')) + + " :e never includes the whole filename, so "a.b":e:e:e --> "b" + call assert_equal('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e')) + call assert_equal('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e:e')) +endfunc From 9af0fe529d2d91640e4d3388ab9f28159553f14c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 11 Oct 2019 19:17:11 +0200 Subject: [PATCH 0157/1293] recovery mode (-r/-L): use headless_mode (#11187) Fixes https://github.com/neovim/neovim/issues/11181. --- src/nvim/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/main.c b/src/nvim/main.c index ba15dcedad..e0a1e60fc0 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -957,6 +957,7 @@ static void command_line_scan(mparm_T *parmp) case 'r': // "-r" recovery mode case 'L': { // "-L" recovery mode recoverymode = 1; + headless_mode = true; break; } case 's': { From dd49a130ff0cd7a51cec3a7bae1ecda3708f8eb2 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Fri, 11 Oct 2019 18:32:15 +0100 Subject: [PATCH 0158/1293] vim-patch:8.1.1099: the do_tag() function is too long Problem: The do_tag() function is too long. Solution: Factor parts out to separate functions. Move simplify_filename() to a file where it fits better. (Andy Massimino, closes vim/vim#4195) https://github.com/vim/vim/commit/b4a6020ac6a0638167013f1e45ff440ddc8a1671 --- src/nvim/tag.c | 800 ++++++++++++++++++++++++++----------------------- 1 file changed, 427 insertions(+), 373 deletions(-) diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 6fe3efbaae..880c467d30 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -131,13 +131,13 @@ static taggy_T ptag_entry = { NULL, { { 0, 0, 0 }, 0, 0, NULL }, 0, 0 }; * * for cscope, returns TRUE if we jumped to tag or aborted, FALSE otherwise */ -int -do_tag ( - char_u *tag, /* tag (pattern) to jump to */ +int +do_tag( + char_u *tag, // tag (pattern) to jump to int type, int count, - int forceit, /* :ta with ! */ - int verbose /* print "tag not found" message */ + int forceit, // :ta with ! + int verbose // print "tag not found" message ) { taggy_T *tagstack = curwin->w_tagstack; @@ -148,28 +148,19 @@ do_tag ( int oldtagstackidx = tagstackidx; int prevtagstackidx = tagstackidx; int prev_num_matches; - int new_tag = FALSE; - int other_name; - int i, j, k; - int idx; + int new_tag = false; + int i; int ic; - char_u *p; - char_u *name; - int no_regexp = FALSE; + int no_regexp = false; int error_cur_match = 0; - char_u *command_end; - int save_pos = FALSE; + int save_pos = false; fmark_T saved_fmark; - int taglen; - int jumped_to_tag = FALSE; - tagptrs_T tagp, tagp2; + int jumped_to_tag = false; int new_num_matches; char_u **new_matches; - int attr; int use_tagstack; - int skip_msg = FALSE; - char_u *buf_ffname = curbuf->b_ffname; /* name to use for - priority computation */ + int skip_msg = false; + char_u *buf_ffname = curbuf->b_ffname; // name for priority computation /* remember the matches for the last used tag */ static int num_matches = 0; @@ -183,13 +174,13 @@ do_tag ( FreeWild(num_matches, matches); cs_free_tags(); num_matches = 0; - return FALSE; + return false; } #endif if (type == DT_HELP) { type = DT_TAG; - no_regexp = TRUE; + no_regexp = true; } prev_num_matches = num_matches; @@ -209,10 +200,11 @@ do_tag ( ptag_entry.tagname = vim_strsave(tag); } } else { - if (g_do_tagpreview != 0) - use_tagstack = FALSE; - else - use_tagstack = TRUE; + if (g_do_tagpreview != 0) { + use_tagstack = false; + } else { + use_tagstack = true; + } /* new pattern, add to the tag stack */ if (*tag != NUL @@ -253,15 +245,15 @@ do_tag ( curwin->w_tagstacklen = tagstacklen; - save_pos = TRUE; /* save the cursor position below */ + save_pos = true; // save the cursor position below } - new_tag = TRUE; + new_tag = true; } else { if ( - g_do_tagpreview != 0 ? ptag_entry.tagname == NULL : - tagstacklen == 0) { - /* empty stack */ + g_do_tagpreview != 0 ? ptag_entry.tagname == NULL : + tagstacklen == 0) { + // empty stack EMSG(_(e_tagstack)); goto end_do_tag; } @@ -271,7 +263,7 @@ do_tag ( if ((tagstackidx -= count) < 0) { EMSG(_(bottommsg)); if (tagstackidx + count == 0) { - /* We did [num]^T from the bottom of the stack */ + // We did [num]^T from the bottom of the stack tagstackidx = 0; goto end_do_tag; } @@ -279,7 +271,7 @@ do_tag ( * way to the bottom now. */ tagstackidx = 0; - } else if (tagstackidx >= tagstacklen) { /* count == 0? */ + } else if (tagstackidx >= tagstacklen) { // count == 0? EMSG(_(topmsg)); goto end_do_tag; } @@ -293,8 +285,8 @@ do_tag ( * file was changed) keep original position in tag stack. */ if (buflist_getfile(saved_fmark.fnum, saved_fmark.mark.lnum, - GETF_SETMARK, forceit) == FAIL) { - tagstackidx = oldtagstackidx; /* back to old posn */ + GETF_SETMARK, forceit) == FAIL) { + tagstackidx = oldtagstackidx; // back to old posn goto end_do_tag; } /* A BufReadPost autocommand may jump to the '" mark, but @@ -305,12 +297,12 @@ do_tag ( curwin->w_cursor.lnum = saved_fmark.mark.lnum; } curwin->w_cursor.col = saved_fmark.mark.col; - curwin->w_set_curswant = TRUE; + curwin->w_set_curswant = true; check_cursor(); if ((fdo_flags & FDO_TAG) && old_KeyTyped) foldOpenCursor(); - /* remove the old list of matches */ + // remove the old list of matches FreeWild(num_matches, matches); cs_free_tags(); num_matches = 0; @@ -325,8 +317,8 @@ do_tag ( cur_match = ptag_entry.cur_match; cur_fnum = ptag_entry.cur_fnum; } else { - /* ":tag" (no argument): go to newer pattern */ - save_pos = TRUE; /* save the cursor position below */ + // ":tag" (no argument): go to newer pattern + save_pos = true; // save the cursor position below if ((tagstackidx += count - 1) >= tagstacklen) { /* * Beyond the last one, just give an error message and @@ -335,8 +327,8 @@ do_tag ( */ tagstackidx = tagstacklen - 1; EMSG(_(topmsg)); - save_pos = FALSE; - } else if (tagstackidx < 0) { /* must have been count == 0 */ + save_pos = false; + } else if (tagstackidx < 0) { // must have been count == 0 EMSG(_(bottommsg)); tagstackidx = 0; goto end_do_tag; @@ -344,9 +336,9 @@ do_tag ( cur_match = tagstack[tagstackidx].cur_match; cur_fnum = tagstack[tagstackidx].cur_fnum; } - new_tag = TRUE; - } else { /* go to other matching tag */ - /* Save index for when selection is cancelled. */ + new_tag = true; + } else { // go to other matching tag + // Save index for when selection is cancelled. prevtagstackidx = tagstackidx; if (g_do_tagpreview != 0) { @@ -371,7 +363,7 @@ do_tag ( cur_match = MAXCOL - 1; else if (cur_match < 0) { EMSG(_("E425: Cannot go before first matching tag")); - skip_msg = TRUE; + skip_msg = true; cur_match = 0; cur_fnum = curbuf->b_fnum; } @@ -418,15 +410,17 @@ do_tag ( * Repeat searching for tags, when a file has not been found. */ for (;; ) { - /* - * When desired match not found yet, try to find it (and others). - */ - if (use_tagstack) + int other_name; + char_u *name; + + // When desired match not found yet, try to find it (and others). + if (use_tagstack) { name = tagstack[tagstackidx].tagname; - else if (g_do_tagpreview != 0) + } else if (g_do_tagpreview != 0) { name = ptag_entry.tagname; - else + } else { name = tag; + } other_name = (tagmatchname == NULL || STRCMP(tagmatchname, name) != 0); if (new_tag || (cur_match >= num_matches && max_num_matches != MAXCOL) @@ -446,7 +440,7 @@ do_tag ( max_num_matches = cur_match + 1; } - /* when the argument starts with '/', use it as a regexp */ + // when the argument starts with '/', use it as a regexp if (!no_regexp && *name == '/') { flags = TAG_REGEXP; ++name; @@ -467,17 +461,21 @@ do_tag ( * to the start. Avoids that the order changes when using * ":tnext" and jumping to another file. */ if (!new_tag && !other_name) { - /* Find the position of each old match in the new list. Need - * to use parse_match() to find the tag line. */ - idx = 0; - for (j = 0; j < num_matches; ++j) { + int j, k; + int idx = 0; + tagptrs_T tagp, tagp2; + + // Find the position of each old match in the new list. Need + // to use parse_match() to find the tag line. + for (j = 0; j < num_matches; j++) { parse_match(matches[j], &tagp); for (i = idx; i < new_num_matches; ++i) { parse_match(new_matches[i], &tagp2); if (STRCMP(tagp.tagname, tagp2.tagname) == 0) { - p = new_matches[i]; - for (k = i; k > idx; --k) + char_u *p = new_matches[i]; + for (k = i; k > idx; k--) { new_matches[k] = new_matches[k - 1]; + } new_matches[idx++] = p; break; } @@ -504,304 +502,27 @@ do_tag ( // jump to count'th matching tag. cur_match = count > 0 ? count - 1 : 0; } else if (type == DT_SELECT || (type == DT_JUMP && num_matches > 1)) { - // List all the matching tags. - // Assume that the first match indicates how long the tags can - // be, and align the file names to that. - parse_match(matches[0], &tagp); - taglen = (int)(tagp.tagname_end - tagp.tagname + 2); - if (taglen < 18) - taglen = 18; - if (taglen > Columns - 25) - taglen = MAXCOL; - if (msg_col == 0) - msg_didout = FALSE; /* overwrite previous message */ - msg_start(); - MSG_PUTS_ATTR(_(" # pri kind tag"), HL_ATTR(HLF_T)); - msg_clr_eos(); - taglen_advance(taglen); - MSG_PUTS_ATTR(_("file\n"), HL_ATTR(HLF_T)); - - for (i = 0; i < num_matches && !got_int; i++) { - parse_match(matches[i], &tagp); - if (!new_tag && ((g_do_tagpreview != 0 && i == ptag_entry.cur_match) - || (use_tagstack - && i == tagstack[tagstackidx].cur_match))) { - *IObuff = '>'; - } else { - *IObuff = ' '; - } - vim_snprintf((char *)IObuff + 1, IOSIZE - 1, "%2d %s ", i + 1, - mt_names[matches[i][0] & MT_MASK]); - msg_puts((const char *)IObuff); - if (tagp.tagkind != NULL) { - msg_outtrans_len(tagp.tagkind, - (int)(tagp.tagkind_end - tagp.tagkind)); - } - msg_advance(13); - msg_outtrans_len_attr(tagp.tagname, - (int)(tagp.tagname_end - tagp.tagname), - HL_ATTR(HLF_T)); - msg_putchar(' '); - taglen_advance(taglen); - - /* Find out the actual file name. If it is long, truncate - * it and put "..." in the middle */ - p = tag_full_fname(&tagp); - msg_puts_long_attr(p, HL_ATTR(HLF_D)); - xfree(p); - - if (msg_col > 0) - msg_putchar('\n'); - if (got_int) - break; - msg_advance(15); - - /* print any extra fields */ - command_end = tagp.command_end; - if (command_end != NULL) { - p = command_end + 3; - while (*p && *p != '\r' && *p != '\n') { - while (*p == TAB) - ++p; - - /* skip "file:" without a value (static tag) */ - if (STRNCMP(p, "file:", 5) == 0 - && ascii_isspace(p[5])) { - p += 5; - continue; - } - /* skip "kind:" and "" */ - if (p == tagp.tagkind - || (p + 5 == tagp.tagkind - && STRNCMP(p, "kind:", 5) == 0)) { - p = tagp.tagkind_end; - continue; - } - // print all other extra fields - attr = HL_ATTR(HLF_CM); - while (*p && *p != '\r' && *p != '\n') { - if (msg_col + ptr2cells(p) >= Columns) { - msg_putchar('\n'); - if (got_int) - break; - msg_advance(15); - } - p = msg_outtrans_one(p, attr); - if (*p == TAB) { - msg_puts_attr(" ", attr); - break; - } - if (*p == ':') - attr = 0; - } - } - if (msg_col > 15) { - msg_putchar('\n'); - if (got_int) - break; - msg_advance(15); - } - } else { - for (p = tagp.command; - *p && *p != '\r' && *p != '\n'; ++p) - ; - command_end = p; - } - - /* - * Put the info (in several lines) at column 15. - * Don't display "/^" and "?^". - */ - p = tagp.command; - if (*p == '/' || *p == '?') { - ++p; - if (*p == '^') - ++p; - } - /* Remove leading whitespace from pattern */ - while (p != command_end && ascii_isspace(*p)) - ++p; - - while (p != command_end) { - if (msg_col + (*p == TAB ? 1 : ptr2cells(p)) > Columns) - msg_putchar('\n'); - if (got_int) - break; - msg_advance(15); - - // Skip backslash used for escaping a command char or a backslash. - if (*p == '\\' && (*(p + 1) == *tagp.command - || *(p + 1) == '\\')) { - ++p; - } - - if (*p == TAB) { - msg_putchar(' '); - ++p; - } else - p = msg_outtrans_one(p, 0); - - /* don't display the "$/;\"" and "$?;\"" */ - if (p == command_end - 2 && *p == '$' - && *(p + 1) == *tagp.command) - break; - /* don't display matching '/' or '?' */ - if (p == command_end - 1 && *p == *tagp.command - && (*p == '/' || *p == '?')) - break; - } - if (msg_col) - msg_putchar('\n'); - os_breakcheck(); - } - if (got_int) { - got_int = false; // only stop the listing - } + print_tag_list(new_tag, use_tagstack, num_matches, matches); ask_for_selection = true; } else if (type == DT_LTAG) { - list_T *list; - char_u tag_name[128 + 1]; - char_u *fname; - char_u *cmd; - - /* - * Add the matching tags to the location list for the current - * window. - */ - - fname = xmalloc(MAXPATHL + 1); - cmd = xmalloc(CMDBUFFSIZE + 1); - list = tv_list_alloc(num_matches); - - for (i = 0; i < num_matches; ++i) { - int len, cmd_len; - long lnum; - dict_T *dict; - - parse_match(matches[i], &tagp); - - /* Save the tag name */ - len = (int)(tagp.tagname_end - tagp.tagname); - if (len > 128) - len = 128; - STRLCPY(tag_name, tagp.tagname, len + 1); - - /* Save the tag file name */ - p = tag_full_fname(&tagp); - STRLCPY(fname, p, MAXPATHL + 1); - xfree(p); - - /* - * Get the line number or the search pattern used to locate - * the tag. - */ - lnum = 0; - if (isdigit(*tagp.command)) - /* Line number is used to locate the tag */ - lnum = atol((char *)tagp.command); - else { - char_u *cmd_start, *cmd_end; - - /* Search pattern is used to locate the tag */ - - /* Locate the end of the command */ - cmd_start = tagp.command; - cmd_end = tagp.command_end; - if (cmd_end == NULL) { - for (p = tagp.command; - *p && *p != '\r' && *p != '\n'; ++p) - ; - cmd_end = p; - } - - /* - * Now, cmd_end points to the character after the - * command. Adjust it to point to the last - * character of the command. - */ - cmd_end--; - - /* - * Skip the '/' and '?' characters at the - * beginning and end of the search pattern. - */ - if (*cmd_start == '/' || *cmd_start == '?') - cmd_start++; - - if (*cmd_end == '/' || *cmd_end == '?') - cmd_end--; - - len = 0; - cmd[0] = NUL; - - /* - * If "^" is present in the tag search pattern, then - * copy it first. - */ - if (*cmd_start == '^') { - STRCPY(cmd, "^"); - cmd_start++; - len++; - } - - /* - * Precede the tag pattern with \V to make it very - * nomagic. - */ - STRCAT(cmd, "\\V"); - len += 2; - - cmd_len = (int)(cmd_end - cmd_start + 1); - if (cmd_len > (CMDBUFFSIZE - 5)) - cmd_len = CMDBUFFSIZE - 5; - STRNCAT(cmd, cmd_start, cmd_len); - len += cmd_len; - - if (cmd[len - 1] == '$') { - /* - * Replace '$' at the end of the search pattern - * with '\$' - */ - cmd[len - 1] = '\\'; - cmd[len] = '$'; - len++; - } - - cmd[len] = NUL; - } - - dict = tv_dict_alloc(); - tv_list_append_dict(list, dict); - - tv_dict_add_str(dict, S_LEN("text"), (const char *)tag_name); - tv_dict_add_str(dict, S_LEN("filename"), (const char *)fname); - tv_dict_add_nr(dict, S_LEN("lnum"), lnum); - if (lnum == 0) { - tv_dict_add_str(dict, S_LEN("pattern"), (const char *)cmd); - } + if (add_llist_tags(tag, num_matches, matches) == FAIL) { + goto end_do_tag; } - vim_snprintf((char *)IObuff, IOSIZE, "ltag %s", tag); - set_errorlist(curwin, list, ' ', IObuff, NULL); - - tv_list_free(list); - xfree(fname); - xfree(cmd); - - cur_match = 0; /* Jump to the first tag */ + cur_match = 0; // Jump to the first tag } if (ask_for_selection) { // Ask to select a tag from the list. i = prompt_for_number(NULL); if (i <= 0 || i > num_matches || got_int) { - /* no valid choice: don't change anything */ + // no valid choice: don't change anything if (use_tagstack) { tagstack[tagstackidx].fmark = saved_fmark; tagstackidx = prevtagstackidx; } cs_free_tags(); - jumped_to_tag = TRUE; + jumped_to_tag = true; break; } cur_match = i - 1; @@ -817,7 +538,7 @@ do_tag ( EMSG(_("E427: There is only one matching tag")); else EMSG(_("E428: Cannot go beyond last matching tag")); - skip_msg = TRUE; + skip_msg = true; } cur_match = num_matches - 1; } @@ -843,13 +564,14 @@ do_tag ( && type != DT_CSCOPE && (num_matches > 1 || ic) && !skip_msg) { - /* Give an indication of the number of matching tags */ - sprintf((char *)IObuff, _("tag %d of %d%s"), - cur_match + 1, - num_matches, - max_num_matches != MAXCOL ? _(" or more") : ""); - if (ic) + // Give an indication of the number of matching tags + snprintf((char *)IObuff, sizeof(IObuff), _("tag %d of %d%s"), + cur_match + 1, + num_matches, + max_num_matches != MAXCOL ? _(" or more") : ""); + if (ic) { STRCAT(IObuff, _(" Using tag with different case!")); + } if ((num_matches > prev_num_matches || new_tag) && num_matches > 1) { if (ic) { @@ -867,7 +589,7 @@ do_tag ( } } - /* Let the SwapExists event know what tag we are jumping to. */ + // Let the SwapExists event know what tag we are jumping to. vim_snprintf((char *)IObuff, IOSIZE, ":ta %s\r", name); set_vim_var_string(VV_SWAPCOMMAND, (char *) IObuff, -1); @@ -879,7 +601,7 @@ do_tag ( set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); if (i == NOTAGFILE) { - /* File not found: try again with another matching tag */ + // File not found: try again with another matching tag if ((type == DT_PREV && cur_match > 0) || ((type == DT_TAG || type == DT_NEXT || type == DT_FIRST) @@ -902,22 +624,354 @@ do_tag ( * tagstackidx is still valid. */ if (use_tagstack && tagstackidx > curwin->w_tagstacklen) tagstackidx = curwin->w_tagstackidx; - jumped_to_tag = TRUE; + jumped_to_tag = true; } } break; } end_do_tag: - /* Only store the new index when using the tagstack and it's valid. */ - if (use_tagstack && tagstackidx <= curwin->w_tagstacklen) + // Only store the new index when using the tagstack and it's valid. + if (use_tagstack && tagstackidx <= curwin->w_tagstacklen) { curwin->w_tagstackidx = tagstackidx; + } postponed_split = 0; // don't split next time g_do_tagpreview = 0; // don't do tag preview next time return jumped_to_tag; } +// +// List all the matching tags. +// +static void +print_tag_list( + int new_tag, + int use_tagstack, + int num_matches, + char_u **matches) +{ + taggy_T *tagstack = curwin->w_tagstack; + int tagstackidx = curwin->w_tagstackidx; + int i; + char_u *p; + char_u *command_end; + tagptrs_T tagp; + int taglen; + int attr; + + // Assume that the first match indicates how long the tags can + // be, and align the file names to that. + parse_match(matches[0], &tagp); + taglen = (int)(tagp.tagname_end - tagp.tagname + 2); + if (taglen < 18) { + taglen = 18; + } + if (taglen > Columns - 25) { + taglen = MAXCOL; + } + if (msg_col == 0) { + msg_didout = false; // overwrite previous message + } + msg_start(); + msg_puts_attr(_(" # pri kind tag"), HL_ATTR(HLF_T)); + msg_clr_eos(); + taglen_advance(taglen); + msg_puts_attr(_("file\n"), HL_ATTR(HLF_T)); + + for (i = 0; i < num_matches && !got_int; i++) { + parse_match(matches[i], &tagp); + if (!new_tag && ( + (g_do_tagpreview != 0 + && i == ptag_entry.cur_match) + || (use_tagstack + && i == tagstack[tagstackidx].cur_match))) { + *IObuff = '>'; + } else { + *IObuff = ' '; + } + vim_snprintf((char *)IObuff + 1, IOSIZE - 1, + "%2d %s ", i + 1, + mt_names[matches[i][0] & MT_MASK]); + msg_puts((char *)IObuff); + if (tagp.tagkind != NULL) { + msg_outtrans_len(tagp.tagkind, + (int)(tagp.tagkind_end - tagp.tagkind)); + } + msg_advance(13); + msg_outtrans_len_attr(tagp.tagname, + (int)(tagp.tagname_end - tagp.tagname), + HL_ATTR(HLF_T)); + msg_putchar(' '); + taglen_advance(taglen); + + // Find out the actual file name. If it is long, truncate + // it and put "..." in the middle + p = tag_full_fname(&tagp); + if (p != NULL) { + msg_outtrans_attr(p, HL_ATTR(HLF_D)); + XFREE_CLEAR(p); + } + if (msg_col > 0) { + msg_putchar('\n'); + } + if (got_int) { + break; + } + msg_advance(15); + + // print any extra fields + command_end = tagp.command_end; + if (command_end != NULL) { + p = command_end + 3; + while (*p && *p != '\r' && *p != '\n') { + while (*p == TAB) { + p++; + } + + // skip "file:" without a value (static tag) + if (STRNCMP(p, "file:", 5) == 0 && ascii_isspace(p[5])) { + p += 5; + continue; + } + // skip "kind:" and "" + if (p == tagp.tagkind + || (p + 5 == tagp.tagkind + && STRNCMP(p, "kind:", 5) == 0)) { + p = tagp.tagkind_end; + continue; + } + // print all other extra fields + attr = HL_ATTR(HLF_CM); + while (*p && *p != '\r' && *p != '\n') { + if (msg_col + ptr2cells(p) >= Columns) { + msg_putchar('\n'); + if (got_int) { + break; + } + msg_advance(15); + } + p = msg_outtrans_one(p, attr); + if (*p == TAB) { + msg_puts_attr(" ", attr); + break; + } + if (*p == ':') { + attr = 0; + } + } + } + if (msg_col > 15) { + msg_putchar('\n'); + if (got_int) { + break; + } + msg_advance(15); + } + } else { + for (p = tagp.command; + *p && *p != '\r' && *p != '\n'; + p++) { + } + command_end = p; + } + + // Put the info (in several lines) at column 15. + // Don't display "/^" and "?^". + p = tagp.command; + if (*p == '/' || *p == '?') { + p++; + if (*p == '^') { + p++; + } + } + // Remove leading whitespace from pattern + while (p != command_end && ascii_isspace(*p)) { + p++; + } + + while (p != command_end) { + if (msg_col + (*p == TAB ? 1 : ptr2cells(p)) > Columns) { + msg_putchar('\n'); + } + if (got_int) { + break; + } + msg_advance(15); + + // skip backslash used for escaping a command char or + // a backslash + if (*p == '\\' && (*(p + 1) == *tagp.command + || *(p + 1) == '\\')) { + p++; + } + + if (*p == TAB) { + msg_putchar(' '); + p++; + } else { + p = msg_outtrans_one(p, 0); + } + + // don't display the "$/;\"" and "$?;\"" + if (p == command_end - 2 && *p == '$' + && *(p + 1) == *tagp.command) { + break; + } + // don't display matching '/' or '?' + if (p == command_end - 1 && *p == *tagp.command + && (*p == '/' || *p == '?')) { + break; + } + } + if (msg_col) { + msg_putchar('\n'); + } + os_breakcheck(); + } + if (got_int) { + got_int = false; // only stop the listing + } +} + +// +// Add the matching tags to the location list for the current +// window. +// +static int +add_llist_tags( + char_u *tag, + int num_matches, + char_u **matches) +{ + list_T *list; + char_u tag_name[128 + 1]; + char_u *fname; + char_u *cmd; + int i; + char_u *p; + tagptrs_T tagp; + + fname = xmalloc(MAXPATHL + 1); + cmd = xmalloc(CMDBUFFSIZE + 1); + list = tv_list_alloc(0); + + for (i = 0; i < num_matches; i++) { + int len, cmd_len; + long lnum; + dict_T *dict; + + parse_match(matches[i], &tagp); + + // Save the tag name + len = (int)(tagp.tagname_end - tagp.tagname); + if (len > 128) { + len = 128; + } + xstrlcpy((char *)tag_name, (const char *)tagp.tagname, len); + tag_name[len] = NUL; + + // Save the tag file name + p = tag_full_fname(&tagp); + if (p == NULL) { + continue; + } + xstrlcpy((char *)fname, (const char *)p, MAXPATHL); + XFREE_CLEAR(p); + + // Get the line number or the search pattern used to locate + // the tag. + lnum = 0; + if (isdigit(*tagp.command)) { + // Line number is used to locate the tag + lnum = atol((char *)tagp.command); + } else { + char_u *cmd_start, *cmd_end; + + // Search pattern is used to locate the tag + + // Locate the end of the command + cmd_start = tagp.command; + cmd_end = tagp.command_end; + if (cmd_end == NULL) { + for (p = tagp.command; + *p && *p != '\r' && *p != '\n'; p++) { + } + cmd_end = p; + } + + // Now, cmd_end points to the character after the + // command. Adjust it to point to the last + // character of the command. + cmd_end--; + + // Skip the '/' and '?' characters at the + // beginning and end of the search pattern. + if (*cmd_start == '/' || *cmd_start == '?') { + cmd_start++; + } + + if (*cmd_end == '/' || *cmd_end == '?') { + cmd_end--; + } + + len = 0; + cmd[0] = NUL; + + // If "^" is present in the tag search pattern, then + // copy it first. + if (*cmd_start == '^') { + STRCPY(cmd, "^"); + cmd_start++; + len++; + } + + // Precede the tag pattern with \V to make it very + // nomagic. + STRCAT(cmd, "\\V"); + len += 2; + + cmd_len = (int)(cmd_end - cmd_start + 1); + if (cmd_len > (CMDBUFFSIZE - 5)) { + cmd_len = CMDBUFFSIZE - 5; + } + xstrlcat((char *)cmd, (char *)cmd_start, cmd_len); + len += cmd_len; + + if (cmd[len - 1] == '$') { + // Replace '$' at the end of the search pattern + // with '\$' + cmd[len - 1] = '\\'; + cmd[len] = '$'; + len++; + } + + cmd[len] = NUL; + } + + if ((dict = tv_dict_alloc()) == NULL) { + continue; + } + tv_list_append_dict(list, dict); + + tv_dict_add_str(dict, S_LEN("text"), (const char *)tag_name); + tv_dict_add_str(dict, S_LEN("filename"), (const char *)fname); + tv_dict_add_nr(dict, S_LEN("lnum"), lnum); + if (lnum == 0) { + tv_dict_add_str(dict, S_LEN("pattern"), (const char *)cmd); + } + } + + vim_snprintf((char *)IObuff, IOSIZE, "ltag %s", tag); + set_errorlist(curwin, list, ' ', IObuff, NULL); + + tv_list_free(list); + XFREE_CLEAR(fname); + XFREE_CLEAR(cmd); + + return OK; +} + /* * Free cached tags. */ @@ -1055,11 +1109,11 @@ static void prepare_pats(pat_T *pats, int has_re) * TAG_KEEP_LANG keep language * TAG_CSCOPE use cscope results for tags */ -int -find_tags ( - char_u *pat, /* pattern to search for */ - int *num_matches, /* return: number of matches found */ - char_u ***matchesp, /* return: array of matches found */ +int +find_tags( + char_u *pat, // pattern to search for + int *num_matches, // return: number of matches found + char_u ***matchesp, // return: array of matches found int flags, int mincount, /* MAXCOL: find all matches other: minimal number of matches */ @@ -1999,11 +2053,11 @@ void free_tag_stuff(void) * * Return FAIL if no more tag file names, OK otherwise. */ -int -get_tagfname ( - tagname_T *tnp, /* holds status info */ - int first, /* TRUE when first file name is wanted */ - char_u *buf /* pointer to buffer of MAXPATHL chars */ +int +get_tagfname( + tagname_T *tnp, // holds status info + int first, // TRUE when first file name is wanted + char_u *buf // pointer to buffer of MAXPATHL chars ) { char_u *fname = NULL; @@ -2128,9 +2182,9 @@ void tagname_free(tagname_T *tnp) * * Return FAIL if there is a format error in this line, OK otherwise. */ -static int -parse_tag_line ( - char_u *lbuf, /* line to be parsed */ +static int +parse_tag_line( + char_u *lbuf, // line to be parsed tagptrs_T *tagp ) { @@ -2211,10 +2265,10 @@ static size_t matching_line_len(const char_u *const lbuf) * * Return OK or FAIL. */ -static int -parse_match ( - char_u *lbuf, /* input: matching line */ - tagptrs_T *tagp /* output: pointers into the line */ +static int +parse_match( + char_u *lbuf, // input: matching line + tagptrs_T *tagp // output: pointers into the line ) { int retval; @@ -2768,8 +2822,8 @@ expand_tags ( * Add a tag field to the dictionary "dict". * Return OK or FAIL. */ -static int -add_tag_field ( +static int +add_tag_field( dict_T *dict, const char *field_name, const char_u *start, // start of the value From 6c012b0624935b93e92a0b12d86d49ef695210ba Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Sat, 12 Oct 2019 09:48:48 +0200 Subject: [PATCH 0159/1293] vim-patch:8.1.1585: :let-heredoc does not trim enough Problem: :let-heredoc does not trim enough. Solution: Trim indent from the contents based on the indent of the first line. Use let-heredoc in more tests. https://github.com/vim/vim/commit/e7eb92708ec2092a2fc11e78703b5dcf83844412 --- runtime/doc/eval.txt | 25 +- src/nvim/eval.c | 52 ++-- src/nvim/testdir/test_cindent.vim | 56 ++--- src/nvim/testdir/test_debugger.vim | 56 +++-- src/nvim/testdir/test_goto.vim | 288 +++++++++++------------ src/nvim/testdir/test_let.vim | 10 +- src/nvim/testdir/test_mksession_utf8.vim | 36 +-- src/nvim/testdir/test_normal.vim | 176 +++++++------- src/nvim/testdir/test_popup.vim | 11 +- src/nvim/testdir/test_profile.vim | 12 +- src/nvim/testdir/test_quickfix.vim | 152 ++++++------ 11 files changed, 461 insertions(+), 413 deletions(-) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 607e88b7c8..8cdaef007c 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -9793,13 +9793,24 @@ text... If {marker} is not supplied, then "." is used as the default marker. - Any white space characters in the lines of text are - preserved. If "trim" is specified before {marker}, - then all the leading indentation exactly matching the - leading indentation before `let` is stripped from the - input lines and the line containing {marker}. Note - that the difference between space and tab matters - here. + Without "trim" any white space characters in the lines + of text are preserved. If "trim" is specified before + {marker}, then indentation is stripped so you can do: > + let text =<< trim END + if ok + echo 'done' + endif + END +< Results in: ["if ok", " echo 'done'", "endif"] + The marker must line up with "let" and the indentation + of the first line is removed from all the text lines. + Specifically: all the leading indentation exactly + matching the leading indentation of the first + non-empty text line is stripped from the input lines. + All leading indentation exactly matching the leading + indentation before `let` is stripped from the line + containing {marker}. Note that the difference between + space and tab matters here. If {var-name} didn't exist yet, it is created. Cannot be followed by another command, but can be diff --git a/src/nvim/eval.c b/src/nvim/eval.c index a899dbcd3a..bedfe2d6d0 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1521,7 +1521,9 @@ heredoc_get(exarg_T *eap, char_u *cmd) { char_u *marker; char_u *p; - int indent_len = 0; + int marker_indent_len = 0; + int text_indent_len = 0; + char_u *text_indent = NULL; if (eap->getline == NULL) { EMSG(_("E991: cannot use =<< here")); @@ -1534,14 +1536,16 @@ heredoc_get(exarg_T *eap, char_u *cmd) && (cmd[4] == NUL || ascii_iswhite(cmd[4]))) { cmd = skipwhite(cmd + 4); - // Trim the indentation from all the lines in the here document + // Trim the indentation from all the lines in the here document. // The amount of indentation trimmed is the same as the indentation of - // the :let command line. + // the first line after the :let command line. To find the end marker + // the indent of the :let command line is trimmed. p = *eap->cmdlinep; while (ascii_iswhite(*p)) { p++; - indent_len++; + marker_indent_len++; } + text_indent_len = -1; } // The marker is the next word. Default marker is "." @@ -1559,28 +1563,48 @@ heredoc_get(exarg_T *eap, char_u *cmd) list_T *l = tv_list_alloc(0); for (;;) { - int i = 0; + int mi = 0; + int ti = 0; char_u *theline = eap->getline(NUL, eap->cookie, 0, false); - if (theline != NULL && indent_len > 0) { - // trim the indent matching the first line - if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0) { - i = indent_len; - } - } - if (theline == NULL) { EMSG2(_("E990: Missing end marker '%s'"), marker); break; } - if (STRCMP(marker, theline + i) == 0) { + + // with "trim": skip the indent matching the :let line to find the + // marker + if (marker_indent_len > 0 + && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0) { + mi = marker_indent_len; + } + if (STRCMP(marker, theline + mi) == 0) { xfree(theline); break; } + if (text_indent_len == -1 && *theline != NUL) { + // set the text indent from the first line. + p = theline; + text_indent_len = 0; + while (ascii_iswhite(*p)) { + p++; + text_indent_len++; + } + text_indent = vim_strnsave(theline, text_indent_len); + } + // with "trim": skip the indent matching the first line + if (text_indent != NULL) { + for (ti = 0; ti < text_indent_len; ti++) { + if (theline[ti] != text_indent[ti]) { + break; + } + } + } - tv_list_append_string(l, (char *)(theline + i), -1); + tv_list_append_string(l, (char *)(theline + ti), -1); xfree(theline); } + xfree(text_indent); return l; } diff --git a/src/nvim/testdir/test_cindent.vim b/src/nvim/testdir/test_cindent.vim index f979e354ba..d9795d9335 100644 --- a/src/nvim/testdir/test_cindent.vim +++ b/src/nvim/testdir/test_cindent.vim @@ -19,23 +19,23 @@ func Test_cino_extern_c() " Test for cino-E let without_ind =<< trim [CODE] - #ifdef __cplusplus - extern "C" { - #endif - int func_a(void); - #ifdef __cplusplus - } - #endif + #ifdef __cplusplus + extern "C" { + #endif + int func_a(void); + #ifdef __cplusplus + } + #endif [CODE] let with_ind =<< trim [CODE] - #ifdef __cplusplus - extern "C" { - #endif - int func_a(void); - #ifdef __cplusplus - } - #endif + #ifdef __cplusplus + extern "C" { + #endif + int func_a(void); + #ifdef __cplusplus + } + #endif [CODE] new setlocal cindent cinoptions=E0 @@ -90,30 +90,30 @@ func Test_cindent_expr() endfunc setl expandtab sw=8 indentkeys+=; indentexpr=MyIndentFunction() let testinput =<< trim [CODE] - var_a = something() - b = something() + var_a = something() + b = something() [CODE] call setline(1, testinput) call cursor(1, 1) call feedkeys("^\j$A;\", 'tnix') - let expected =<< trim [CODE] - var_a = something(); - b = something(); - [CODE] + let expected =<< [CODE] + var_a = something(); +b = something(); +[CODE] call assert_equal(expected, getline(1, '$')) %d - let testinput =<< trim [CODE] - var_a = something() - b = something() - [CODE] + let testinput =<< [CODE] + var_a = something() + b = something() +[CODE] call setline(1, testinput) call cursor(1, 1) call feedkeys("^\j$A;\", 'tnix') - let expected =<< trim [CODE] - var_a = something(); - b = something() - [CODE] + let expected =<< [CODE] + var_a = something(); + b = something() +[CODE] call assert_equal(expected, getline(1, '$')) bw! endfunc diff --git a/src/nvim/testdir/test_debugger.vim b/src/nvim/testdir/test_debugger.vim index 3ef460b4fe..130bcf8910 100644 --- a/src/nvim/testdir/test_debugger.vim +++ b/src/nvim/testdir/test_debugger.vim @@ -26,27 +26,29 @@ func Test_Debugger() endif " Create a Vim script with some functions - call writefile([ - \ 'func Foo()', - \ ' let var1 = 1', - \ ' let var2 = Bar(var1) + 9', - \ ' return var2', - \ 'endfunc', - \ 'func Bar(var)', - \ ' let var1 = 2 + a:var', - \ ' let var2 = Bazz(var1) + 4', - \ ' return var2', - \ 'endfunc', - \ 'func Bazz(var)', - \ ' try', - \ ' let var1 = 3 + a:var', - \ ' let var3 = "another var"', - \ ' let var3 = "value2"', - \ ' catch', - \ ' let var4 = "exception"', - \ ' endtry', - \ ' return var1', - \ 'endfunc'], 'Xtest.vim') + let lines =<< trim END + func Foo() + let var1 = 1 + let var2 = Bar(var1) + 9 + return var2 + endfunc + func Bar(var) + let var1 = 2 + a:var + let var2 = Bazz(var1) + 4 + return var2 + endfunc + func Bazz(var) + try + let var1 = 3 + a:var + let var3 = "another var" + let var3 = "value2" + catch + let var4 = "exception" + endtry + return var1 + endfunc + END + call writefile(lines, 'Xtest.vim') " Start Vim in a terminal let buf = RunVimInTerminal('-S Xtest.vim', {}) @@ -294,11 +296,13 @@ func Test_Debugger() " Tests for :breakadd file and :breakadd here " Breakpoints should be set before sourcing the file - call writefile([ - \ 'let var1 = 10', - \ 'let var2 = 20', - \ 'let var3 = 30', - \ 'let var4 = 40'], 'Xtest.vim') + let lines =<< trim END + let var1 = 10 + let var2 = 20 + let var3 = 30 + let var4 = 40 + END + call writefile(lines, 'Xtest.vim') " Start Vim in a terminal let buf = RunVimInTerminal('Xtest.vim', {}) diff --git a/src/nvim/testdir/test_goto.vim b/src/nvim/testdir/test_goto.vim index f04a5a7e3d..19513b315a 100644 --- a/src/nvim/testdir/test_goto.vim +++ b/src/nvim/testdir/test_goto.vim @@ -16,12 +16,12 @@ endfunc func Test_gD() let lines =<< trim [CODE] - int x; - - int func(void) - { - return x; - } + int x; + + int func(void) + { + return x; + } [CODE] call XTest_goto_decl('gD', lines, 1, 5) @@ -29,12 +29,12 @@ endfunc func Test_gD_too() let lines =<< trim [CODE] - Filename x; - - int Filename - int func() { Filename x; - return x; + + int Filename + int func() { + Filename x; + return x; [CODE] call XTest_goto_decl('gD', lines, 1, 10) @@ -42,13 +42,13 @@ endfunc func Test_gD_comment() let lines =<< trim [CODE] - /* int x; */ - int x; - - int func(void) - { - return x; - } + /* int x; */ + int x; + + int func(void) + { + return x; + } [CODE] call XTest_goto_decl('gD', lines, 2, 5) @@ -56,13 +56,13 @@ endfunc func Test_gD_inline_comment() let lines =<< trim [CODE] - int y /* , x */; - int x; - - int func(void) - { - return x; - } + int y /* , x */; + int x; + + int func(void) + { + return x; + } [CODE] call XTest_goto_decl('gD', lines, 2, 5) @@ -70,13 +70,13 @@ endfunc func Test_gD_string() let lines =<< trim [CODE] - char *s[] = "x"; - int x = 1; - - int func(void) - { - return x; - } + char *s[] = "x"; + int x = 1; + + int func(void) + { + return x; + } [CODE] call XTest_goto_decl('gD', lines, 2, 5) @@ -84,12 +84,12 @@ endfunc func Test_gD_string_same_line() let lines =<< trim [CODE] - char *s[] = "x", int x = 1; - - int func(void) - { - return x; - } + char *s[] = "x", int x = 1; + + int func(void) + { + return x; + } [CODE] call XTest_goto_decl('gD', lines, 1, 22) @@ -97,13 +97,13 @@ endfunc func Test_gD_char() let lines =<< trim [CODE] - char c = 'x'; - int x = 1; - - int func(void) - { - return x; - } + char c = 'x'; + int x = 1; + + int func(void) + { + return x; + } [CODE] call XTest_goto_decl('gD', lines, 2, 5) @@ -111,12 +111,12 @@ endfunc func Test_gd() let lines =<< trim [CODE] - int x; - - int func(int x) - { - return x; - } + int x; + + int func(int x) + { + return x; + } [CODE] call XTest_goto_decl('gd', lines, 3, 14) @@ -124,15 +124,15 @@ endfunc func Test_gd_not_local() let lines =<< trim [CODE] - int func1(void) - { - return x; - } - - int func2(int x) - { - return x; - } + int func1(void) + { + return x; + } + + int func2(int x) + { + return x; + } [CODE] call XTest_goto_decl('gd', lines, 3, 10) @@ -140,11 +140,11 @@ endfunc func Test_gd_kr_style() let lines =<< trim [CODE] - int func(x) - int x; - { - return x; - } + int func(x) + int x; + { + return x; + } [CODE] call XTest_goto_decl('gd', lines, 2, 7) @@ -152,15 +152,15 @@ endfunc func Test_gd_missing_braces() let lines =<< trim [CODE] - def func1(a) - a + 1 - end - - a = 1 - - def func2() - return a - end + def func1(a) + a + 1 + end + + a = 1 + + def func2() + return a + end [CODE] call XTest_goto_decl('gd', lines, 1, 11) @@ -168,12 +168,12 @@ endfunc func Test_gd_comment() let lines =<< trim [CODE] - int func(void) - { - /* int x; */ - int x; - return x; - } + int func(void) + { + /* int x; */ + int x; + return x; + } [CODE] call XTest_goto_decl('gd', lines, 4, 7) @@ -181,12 +181,12 @@ endfunc func Test_gd_comment_in_string() let lines =<< trim [CODE] - int func(void) - { - char *s ="//"; int x; - int x; - return x; - } + int func(void) + { + char *s ="//"; int x; + int x; + return x; + } [CODE] call XTest_goto_decl('gd', lines, 3, 22) @@ -195,12 +195,12 @@ endfunc func Test_gd_string_in_comment() set comments= let lines =<< trim [CODE] - int func(void) - { - /* " */ int x; - int x; - return x; - } + int func(void) + { + /* " */ int x; + int x; + return x; + } [CODE] call XTest_goto_decl('gd', lines, 3, 15) @@ -209,10 +209,10 @@ endfunc func Test_gd_inline_comment() let lines =<< trim [CODE] - int func(/* x is an int */ int x) - { - return x; - } + int func(/* x is an int */ int x) + { + return x; + } [CODE] call XTest_goto_decl('gd', lines, 1, 32) @@ -220,10 +220,10 @@ endfunc func Test_gd_inline_comment_only() let lines =<< trim [CODE] - int func(void) /* one lonely x */ - { - return x; - } + int func(void) /* one lonely x */ + { + return x; + } [CODE] call XTest_goto_decl('gd', lines, 3, 10) @@ -231,16 +231,16 @@ endfunc func Test_gd_inline_comment_body() let lines =<< trim [CODE] - int func(void) - { - int y /* , x */; - - for (/* int x = 0 */; y < 2; y++); - - int x = 0; - - return x; - } + int func(void) + { + int y /* , x */; + + for (/* int x = 0 */; y < 2; y++); + + int x = 0; + + return x; + } [CODE] call XTest_goto_decl('gd', lines, 7, 7) @@ -248,10 +248,10 @@ endfunc func Test_gd_trailing_multiline_comment() let lines =<< trim [CODE] - int func(int x) /* x is an int */ - { - return x; - } + int func(int x) /* x is an int */ + { + return x; + } [CODE] call XTest_goto_decl('gd', lines, 1, 14) @@ -259,10 +259,10 @@ endfunc func Test_gd_trailing_comment() let lines =<< trim [CODE] - int func(int x) // x is an int - { - return x; - } + int func(int x) // x is an int + { + return x; + } [CODE] call XTest_goto_decl('gd', lines, 1, 14) @@ -270,13 +270,13 @@ endfunc func Test_gd_string() let lines =<< trim [CODE] - int func(void) - { - char *s = "x"; - int x = 1; - - return x; - } + int func(void) + { + char *s = "x"; + int x = 1; + + return x; + } [CODE] call XTest_goto_decl('gd', lines, 4, 7) @@ -284,12 +284,12 @@ endfunc func Test_gd_string_only() let lines =<< trim [CODE] - int func(void) - { - char *s = "x"; - - return x; - } + int func(void) + { + char *s = "x"; + + return x; + } [CODE] call XTest_goto_decl('gd', lines, 5, 10) @@ -312,21 +312,21 @@ endfunc func Test_gd_local_block() let lines =<< trim [CODE] int main() - { - char *a = "NOT NULL"; - if(a) { - char *b = a; - printf("%s\n", b); + char *a = "NOT NULL"; + if(a) + { + char *b = a; + printf("%s\n", b); + } + else + { + char *b = "NULL"; + return b; + } + + return 0; } - else - { - char *b = "NULL"; - return b; - } - - return 0; - } [CODE] call XTest_goto_decl('1gd', lines, 11, 11) diff --git a/src/nvim/testdir/test_let.vim b/src/nvim/testdir/test_let.vim index 43f35e2b9d..d5100b5a82 100644 --- a/src/nvim/testdir/test_let.vim +++ b/src/nvim/testdir/test_let.vim @@ -199,10 +199,18 @@ END END call assert_equal(['Line1', ' Line2', "\tLine3", ' END'], var1) + let var1 =<< trim !!! + Line1 + line2 + Line3 + !!! + !!! + call assert_equal(['Line1', ' line2', "\tLine3", '!!!',], var1) + let var1 =<< trim Line1 . - call assert_equal([' Line1'], var1) + call assert_equal(['Line1'], var1) " ignore "endfunc" let var1 =<< END diff --git a/src/nvim/testdir/test_mksession_utf8.vim b/src/nvim/testdir/test_mksession_utf8.vim index 36f07512a8..722fd28beb 100644 --- a/src/nvim/testdir/test_mksession_utf8.vim +++ b/src/nvim/testdir/test_mksession_utf8.vim @@ -66,32 +66,32 @@ func Test_mksession_utf8() mksession! test_mks.out let li = filter(readfile('test_mks.out'), 'v:val =~# "\\(^ *normal! 0\\|^ *exe ''normal!\\)"') let expected =<< trim [DATA] - normal! 016| - normal! 016| - normal! 016| - normal! 08| - normal! 08| - normal! 016| - normal! 016| - normal! 016| - exe 'normal! ' . s:c . '|zs' . 16 . '|' normal! 016| - exe 'normal! ' . s:c . '|zs' . 16 . '|' normal! 016| - exe 'normal! ' . s:c . '|zs' . 16 . '|' normal! 016| - exe 'normal! ' . s:c . '|zs' . 8 . '|' normal! 08| - exe 'normal! ' . s:c . '|zs' . 8 . '|' normal! 08| - exe 'normal! ' . s:c . '|zs' . 16 . '|' normal! 016| - exe 'normal! ' . s:c . '|zs' . 16 . '|' normal! 016| - exe 'normal! ' . s:c . '|zs' . 16 . '|' - normal! 016| - exe 'normal! ' . s:c . '|zs' . 16 . '|' normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 8 . '|' + normal! 08| + exe 'normal! ' . s:c . '|zs' . 8 . '|' + normal! 08| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| + exe 'normal! ' . s:c . '|zs' . 16 . '|' + normal! 016| [DATA] call assert_equal(expected, li) diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index b967f84626..08b0db234e 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -1564,34 +1564,34 @@ endfunc fun! Test_normal29_brace() " basic test for { and } movements let text =<< trim [DATA] - A paragraph begins after each empty line, and also at each of a set of - paragraph macros, specified by the pairs of characters in the 'paragraphs' - option. The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to - the macros ".IP", ".LP", etc. (These are nroff macros, so the dot must be in - the first column). A section boundary is also a paragraph boundary. - Note that a blank line (only containing white space) is NOT a paragraph - boundary. + A paragraph begins after each empty line, and also at each of a set of + paragraph macros, specified by the pairs of characters in the 'paragraphs' + option. The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to + the macros ".IP", ".LP", etc. (These are nroff macros, so the dot must be in + the first column). A section boundary is also a paragraph boundary. + Note that a blank line (only containing white space) is NOT a paragraph + boundary. - Also note that this does not include a '{' or '}' in the first column. When - the '{' flag is in 'cpoptions' then '{' in the first column is used as a - paragraph boundary |posix|. - { - This is no paragraph - unless the '{' is set - in 'cpoptions' - } - .IP - The nroff macros IP separates a paragraph - That means, it must be a '.' - followed by IP - .LPIt does not matter, if afterwards some - more characters follow. - .SHAlso section boundaries from the nroff - macros terminate a paragraph. That means - a character like this: - .NH - End of text here + Also note that this does not include a '{' or '}' in the first column. When + the '{' flag is in 'cpoptions' then '{' in the first column is used as a + paragraph boundary |posix|. + { + This is no paragraph + unless the '{' is set + in 'cpoptions' + } + .IP + The nroff macros IP separates a paragraph + That means, it must be a '.' + followed by IP + .LPIt does not matter, if afterwards some + more characters follow. + .SHAlso section boundaries from the nroff + macros terminate a paragraph. That means + a character like this: + .NH + End of text here [DATA] new @@ -1600,17 +1600,17 @@ fun! Test_normal29_brace() norm! 0d2} let expected =<< trim [DATA] - .IP - The nroff macros IP separates a paragraph - That means, it must be a '.' - followed by IP - .LPIt does not matter, if afterwards some - more characters follow. - .SHAlso section boundaries from the nroff - macros terminate a paragraph. That means - a character like this: - .NH - End of text here + .IP + The nroff macros IP separates a paragraph + That means, it must be a '.' + followed by IP + .LPIt does not matter, if afterwards some + more characters follow. + .SHAlso section boundaries from the nroff + macros terminate a paragraph. That means + a character like this: + .NH + End of text here [DATA] call assert_equal(expected, getline(1, '$')) @@ -1618,13 +1618,13 @@ fun! Test_normal29_brace() norm! 0d} let expected =<< trim [DATA] - .LPIt does not matter, if afterwards some - more characters follow. - .SHAlso section boundaries from the nroff - macros terminate a paragraph. That means - a character like this: - .NH - End of text here + .LPIt does not matter, if afterwards some + more characters follow. + .SHAlso section boundaries from the nroff + macros terminate a paragraph. That means + a character like this: + .NH + End of text here [DATA] call assert_equal(expected, getline(1, '$')) @@ -1633,11 +1633,11 @@ fun! Test_normal29_brace() norm! d{ let expected =<< trim [DATA] - .LPIt does not matter, if afterwards some - more characters follow. - .SHAlso section boundaries from the nroff - macros terminate a paragraph. That means - a character like this: + .LPIt does not matter, if afterwards some + more characters follow. + .SHAlso section boundaries from the nroff + macros terminate a paragraph. That means + a character like this: [DATA] call assert_equal(expected, getline(1, '$')) @@ -1645,8 +1645,8 @@ fun! Test_normal29_brace() norm! d{ let expected =<< trim [DATA] - .LPIt does not matter, if afterwards some - more characters follow. + .LPIt does not matter, if afterwards some + more characters follow. [DATA] call assert_equal(expected, getline(1, '$')) @@ -1659,22 +1659,22 @@ fun! Test_normal29_brace() " 1 " norm! 0d2} " let expected =<< trim [DATA] - " { - " This is no paragraph - " unless the '{' is set - " in 'cpoptions' - " } - " .IP - " The nroff macros IP separates a paragraph - " That means, it must be a '.' - " followed by IP - " .LPIt does not matter, if afterwards some - " more characters follow. - " .SHAlso section boundaries from the nroff - " macros terminate a paragraph. That means - " a character like this: - " .NH - " End of text here + " { + " This is no paragraph + " unless the '{' is set + " in 'cpoptions' + " } + " .IP + " The nroff macros IP separates a paragraph + " That means, it must be a '.' + " followed by IP + " .LPIt does not matter, if afterwards some + " more characters follow. + " .SHAlso section boundaries from the nroff + " macros terminate a paragraph. That means + " a character like this: + " .NH + " End of text here " " [DATA] " call assert_equal(expected, getline(1, '$')) @@ -1682,22 +1682,22 @@ fun! Test_normal29_brace() " $ " norm! d} " let expected =<< trim [DATA] - " { - " This is no paragraph - " unless the '{' is set - " in 'cpoptions' - " } - " .IP - " The nroff macros IP separates a paragraph - " That means, it must be a '.' - " followed by IP - " .LPIt does not matter, if afterwards some - " more characters follow. - " .SHAlso section boundaries from the nroff - " macros terminate a paragraph. That means - " a character like this: - " .NH - " End of text here + " { + " This is no paragraph + " unless the '{' is set + " in 'cpoptions' + " } + " .IP + " The nroff macros IP separates a paragraph + " That means, it must be a '.' + " followed by IP + " .LPIt does not matter, if afterwards some + " more characters follow. + " .SHAlso section boundaries from the nroff + " macros terminate a paragraph. That means + " a character like this: + " .NH + " End of text here " " [DATA] " call assert_equal(expected, getline(1, '$')) @@ -1706,11 +1706,11 @@ fun! Test_normal29_brace() " norm! d5} " " let expected =<< trim [DATA] - " { - " This is no paragraph - " unless the '{' is set - " in 'cpoptions' - " } + " { + " This is no paragraph + " unless the '{' is set + " in 'cpoptions' + " } " [DATA] " call assert_equal(expected, getline(1, '$')) diff --git a/src/nvim/testdir/test_popup.vim b/src/nvim/testdir/test_popup.vim index c63269e5d2..8083672808 100644 --- a/src/nvim/testdir/test_popup.vim +++ b/src/nvim/testdir/test_popup.vim @@ -737,11 +737,12 @@ func Test_popup_position() if !CanRunVimInTerminal() return endif - call writefile([ - \ '123456789_123456789_123456789_a', - \ '123456789_123456789_123456789_b', - \ ' 123', - \ ], 'Xtest') + let lines =<< trim END + 123456789_123456789_123456789_a + 123456789_123456789_123456789_b + 123 + END + call writefile(lines, 'Xtest') let buf = RunVimInTerminal('Xtest', {}) call term_sendkeys(buf, ":vsplit\") diff --git a/src/nvim/testdir/test_profile.vim b/src/nvim/testdir/test_profile.vim index 4ab20a9c77..f3eb88abf0 100644 --- a/src/nvim/testdir/test_profile.vim +++ b/src/nvim/testdir/test_profile.vim @@ -312,13 +312,13 @@ endfunc func Test_profile_file() let lines =<< trim [CODE] - func! Foo() - endfunc - for i in range(10) - " a comment + func! Foo() + endfunc + for i in range(10) + " a comment + call Foo() + endfor call Foo() - endfor - call Foo() [CODE] call writefile(lines, 'Xprofile_file.vim') diff --git a/src/nvim/testdir/test_quickfix.vim b/src/nvim/testdir/test_quickfix.vim index b9a22aff51..fc514fc9e6 100644 --- a/src/nvim/testdir/test_quickfix.vim +++ b/src/nvim/testdir/test_quickfix.vim @@ -776,67 +776,67 @@ func Test_efm1() endif let l =<< trim [DATA] - "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set. - "Xtestfile", line 6 col 19; this is an error - gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c - Xtestfile:9: parse error before `asd' - make: *** [vim] Error 1 - in file "Xtestfile" linenr 10: there is an error - - 2 returned - "Xtestfile", line 11 col 1; this is an error - "Xtestfile", line 12 col 2; this is another error - "Xtestfile", line 14:10; this is an error in column 10 - =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time - "Xtestfile", linenr 16: yet another problem - Error in "Xtestfile" at line 17: - x should be a dot - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17 - ^ - Error in "Xtestfile" at line 18: - x should be a dot - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18 - .............^ - Error in "Xtestfile" at line 19: - x should be a dot - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19 - --------------^ - Error in "Xtestfile" at line 20: - x should be a dot - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20 - ^ - - Does anyone know what is the problem and how to correction it? - "Xtestfile", line 21 col 9: What is the title of the quickfix window? - "Xtestfile", line 22 col 9: What is the title of the quickfix window? + "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set. + "Xtestfile", line 6 col 19; this is an error + gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c + Xtestfile:9: parse error before `asd' + make: *** [vim] Error 1 + in file "Xtestfile" linenr 10: there is an error + + 2 returned + "Xtestfile", line 11 col 1; this is an error + "Xtestfile", line 12 col 2; this is another error + "Xtestfile", line 14:10; this is an error in column 10 + =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time + "Xtestfile", linenr 16: yet another problem + Error in "Xtestfile" at line 17: + x should be a dot + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17 + ^ + Error in "Xtestfile" at line 18: + x should be a dot + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18 + .............^ + Error in "Xtestfile" at line 19: + x should be a dot + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19 + --------------^ + Error in "Xtestfile" at line 20: + x should be a dot + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20 + ^ + + Does anyone know what is the problem and how to correction it? + "Xtestfile", line 21 col 9: What is the title of the quickfix window? + "Xtestfile", line 22 col 9: What is the title of the quickfix window? [DATA] call writefile(l, 'Xerrorfile1') call writefile(l[:-2], 'Xerrorfile2') - let m =<< trim [DATA] - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22 - [DATA] + let m =<< [DATA] + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21 + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22 +[DATA] call writefile(m, 'Xtestfile') let save_efm = &efm @@ -1053,20 +1053,20 @@ func Test_efm2() " Test for %P, %Q and %t format specifiers let lines =<< trim [DATA] - [Xtestfile1] - (1,17) error: ';' missing - (21,2) warning: variable 'z' not defined - (67,3) error: end of file found before string ended - -- - - [Xtestfile2] - -- - - [Xtestfile3] - NEW compiler v1.1 - (2,2) warning: variable 'x' not defined - (67,3) warning: 's' already defined - - + [Xtestfile1] + (1,17) error: ';' missing + (21,2) warning: variable 'z' not defined + (67,3) error: end of file found before string ended + -- + + [Xtestfile2] + -- + + [Xtestfile3] + NEW compiler v1.1 + (2,2) warning: variable 'x' not defined + (67,3) warning: 's' already defined + -- [DATA] set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r " To exercise the push/pop file functionality in quickfix, the test files @@ -1090,10 +1090,10 @@ func Test_efm2() " Tests for %E, %C and %Z format specifiers let lines =<< trim [DATA] - Error 275 - line 42 - column 3 - ' ' expected after '--' + Error 275 + line 42 + column 3 + ' ' expected after '--' [DATA] set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m @@ -1107,8 +1107,8 @@ func Test_efm2() " Test for %> let lines =<< trim [DATA] - Error in line 147 of foo.c: - unknown variable 'i' + Error in line 147 of foo.c: + unknown variable 'i' [DATA] set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m From 14c611ed7788fe68f5752ed29ab295a2f0e7c21e Mon Sep 17 00:00:00 2001 From: erw7 Date: Sun, 13 Oct 2019 17:34:08 +0900 Subject: [PATCH 0160/1293] vim-patch 8.1.0084: user name completion does not work on MS-Windows Problem: User name completion does not work on MS-Windows. Solution: Use NetUserEnum() to get user names. (Yasuhiro Matsumoto) https://github.com/vim/vim/commit/828c3d70833a0689cc07581f2a67d06430675da5 --- src/nvim/CMakeLists.txt | 1 + src/nvim/os/users.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index a64944ab0d..b00ac866b7 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -419,6 +419,7 @@ if(Iconv_LIBRARIES) endif() if(WIN32) + list(APPEND NVIM_LINK_LIBRARIES netapi32) list(APPEND NVIM_LINK_LIBRARIES ${WINPTY_LIBRARIES}) endif() diff --git a/src/nvim/os/users.c b/src/nvim/os/users.c index c6463c2c92..b24232b680 100644 --- a/src/nvim/os/users.c +++ b/src/nvim/os/users.c @@ -13,6 +13,9 @@ #ifdef HAVE_PWD_H # include #endif +#ifdef WIN32 +# include +#endif // Initialize users garray and fill it with os usernames. // Return Ok for success, FAIL for failure. @@ -34,6 +37,26 @@ int os_get_usernames(garray_T *users) } } endpwent(); +# elif defined(WIN32) + { + DWORD nusers = 0, ntotal = 0, i; + PUSER_INFO_0 uinfo; + + if (NetUserEnum(NULL, 0, 0, (LPBYTE *)&uinfo, MAX_PREFERRED_LENGTH, + &nusers, &ntotal, NULL) == NERR_Success) { + for (i = 0; i < nusers; i++) { + char *user; + int conversion_result = utf16_to_utf8(uinfo[i].usri0_name, -1, &user); + if (conversion_result != 0) { + EMSG2("utf16_to_utf8 failed: %d", conversion_result); + break; + } + GA_APPEND(char *, users, user); + } + + NetApiBufferFree(uinfo); + } + } # endif return OK; From b89e970cfb472af021e56438a5147dd698e66376 Mon Sep 17 00:00:00 2001 From: erw7 Date: Sun, 13 Oct 2019 17:48:01 +0900 Subject: [PATCH 0161/1293] vim-patch 8.1.0085: no test for completing user name and language Problem: No test for completing user name and language. Solution: Add tests. (Dominique Pelle, closes #2978) https://github.com/vim/vim/commit/5f8f2d378a4f6d7db12806f3e35ec6f7fc6bd1f3 --- src/nvim/testdir/test_cmdline.vim | 45 ++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/nvim/testdir/test_cmdline.vim b/src/nvim/testdir/test_cmdline.vim index e6aafd964b..0a3e6ae625 100644 --- a/src/nvim/testdir/test_cmdline.vim +++ b/src/nvim/testdir/test_cmdline.vim @@ -1,6 +1,5 @@ " Tests for editing the command line. - func Test_complete_tab() call writefile(['testfile'], 'Xtestfile') call feedkeys(":e Xtestf\t\r", "tx") @@ -477,6 +476,50 @@ func Test_cmdline_complete_user_cmd() delcommand Foo endfunc +func Test_cmdline_complete_user_names() + if has('unix') && executable('whoami') + let whoami = systemlist('whoami')[0] + let first_letter = whoami[0] + if len(first_letter) > 0 + " Trying completion of :e ~x where x is the first letter of + " the user name should complete to at least the user name. + call feedkeys(':e ~' . first_letter . "\\\"\", 'tx') + call assert_match('^"e \~.*\<' . whoami . '\>', @:) + endif + endif + if has('win32') + " Just in case: check that the system has an Administrator account. + let names = system('net user') + if names =~ 'Administrator' + " Trying completion of :e ~A should complete to Administrator. + call feedkeys(':e ~A' . "\\\"\", 'tx') + call assert_match('^"e \~Administrator', @:) + endif + endif +endfunc + +funct Test_cmdline_complete_languages() + let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '') + + call feedkeys(":language \\\"\", 'tx') + call assert_match('^"language .*\.*\.*\', @:) + + if has('unix') + " TODO: these tests don't work on Windows. lang appears to be 'C' + " but C does not appear in the completion. Why? + call assert_match('^"language .*\<' . lang . '\>', @:) + + call feedkeys(":language messages \\\"\", 'tx') + call assert_match('^"language .*\<' . lang . '\>', @:) + + call feedkeys(":language ctype \\\"\", 'tx') + call assert_match('^"language .*\<' . lang . '\>', @:) + + call feedkeys(":language time \\\"\", 'tx') + call assert_match('^"language .*\<' . lang . '\>', @:) + endif +endfunc + func Test_cmdline_write_alternatefile() new call setline('.', ['one', 'two']) From fcc24d0df3b1a6bde82c0e5b90f1392639f3fa5b Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Sat, 12 Oct 2019 22:49:21 +0200 Subject: [PATCH 0162/1293] vim-patch:8.1.1625: script line numbers are not exactly right Problem: Script line numbers are not exactly right. Solution: Handle heredoc and continuation lines better. (Ozaki Kiichi, closes vim/vim#4611, closes vim/vim#4511) https://github.com/vim/vim/commit/bc2cfe4672d370330b8698d4d025697a9a6ec569 --- src/nvim/eval.c | 26 ++++++----- src/nvim/ex_cmds2.c | 21 ++++++--- src/nvim/testdir/test_vimscript.vim | 70 +++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 15 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index bedfe2d6d0..d314e3a732 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -21301,7 +21301,8 @@ void ex_function(exarg_T *eap) hashtab_T *ht; int todo; hashitem_T *hi; - int sourcing_lnum_off; + linenr_T sourcing_lnum_off; + linenr_T sourcing_lnum_top; bool show_block = false; bool do_concat = true; @@ -21550,15 +21551,17 @@ void ex_function(exarg_T *eap) cmdline_row = msg_row; } + // Save the starting line number. + sourcing_lnum_top = sourcing_lnum; + indent = 2; nesting = 0; for (;; ) { if (KeyTyped) { - msg_scroll = TRUE; - saved_wait_return = FALSE; + msg_scroll = true; + saved_wait_return = false; } - need_wait_return = FALSE; - sourcing_lnum_off = sourcing_lnum; + need_wait_return = false; if (line_arg != NULL) { /* Use eap->arg, split up in parts by line breaks. */ @@ -21591,11 +21594,13 @@ void ex_function(exarg_T *eap) ui_ext_cmdline_block_append((size_t)indent, (const char *)theline); } - /* Detect line continuation: sourcing_lnum increased more than one. */ - if (sourcing_lnum > sourcing_lnum_off + 1) - sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1; - else + // Detect line continuation: sourcing_lnum increased more than one. + sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie); + if (sourcing_lnum < sourcing_lnum_off) { + sourcing_lnum_off -= sourcing_lnum; + } else { sourcing_lnum_off = 0; + } if (skip_until != NULL) { // Between ":append" and "." and between ":python <uf_flags = flags; fp->uf_calls = 0; fp->uf_script_ctx = current_sctx; - fp->uf_script_ctx.sc_lnum += sourcing_lnum - newlines.ga_len - 1; + fp->uf_script_ctx.sc_lnum += sourcing_lnum_top; + goto ret_free; erret: diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 272c81e29b..84291b3637 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -97,10 +97,11 @@ typedef struct sn_prl_S { struct source_cookie { FILE *fp; ///< opened file for sourcing char_u *nextline; ///< if not NULL: line that was read ahead + linenr_T sourcing_lnum; ///< line number of the source file int finished; ///< ":finish" used #if defined(USE_CRNL) int fileformat; ///< EOL_UNKNOWN, EOL_UNIX or EOL_DOS - bool error; ///< true if LF found after CR-LF + bool error; ///< true if LF found after CR-LF #endif linenr_T breakpoint; ///< next line with breakpoint or zero char_u *fname; ///< name of sourced file @@ -3124,6 +3125,7 @@ int do_source(char_u *fname, int check_other, int is_vimrc) #endif cookie.nextline = NULL; + cookie.sourcing_lnum = 0; cookie.finished = false; // Check if this script has a breakpoint. @@ -3375,6 +3377,13 @@ void free_scriptnames(void) } # endif +linenr_T get_sourced_lnum(LineGetter fgetline, void *cookie) +{ + return fgetline == getsourceline + ? ((struct source_cookie *)cookie)->sourcing_lnum + : sourcing_lnum; +} + /// Get one full line from a sourced file. /// Called by do_cmdline() when it's called from do_source(). @@ -3395,6 +3404,8 @@ char_u *getsourceline(int c, void *cookie, int indent, bool do_concat) if (do_profiling == PROF_YES) { script_line_end(); } + // Set the current sourcing line number. + sourcing_lnum = sp->sourcing_lnum + 1; // Get current line. If there is a read-ahead line, use it, otherwise get // one now. if (sp->finished) { @@ -3404,7 +3415,7 @@ char_u *getsourceline(int c, void *cookie, int indent, bool do_concat) } else { line = sp->nextline; sp->nextline = NULL; - sourcing_lnum++; + sp->sourcing_lnum++; } if (line != NULL && do_profiling == PROF_YES) { script_line_start(); @@ -3414,7 +3425,7 @@ char_u *getsourceline(int c, void *cookie, int indent, bool do_concat) // contain the 'C' flag. if (line != NULL && do_concat && (vim_strchr(p_cpo, CPO_CONCAT) == NULL)) { // compensate for the one line read-ahead - sourcing_lnum--; + sp->sourcing_lnum--; // Get the next line and concatenate it when it starts with a // backslash. We always need to read the next line, keep it in @@ -3492,7 +3503,7 @@ static char_u *get_one_sourceline(struct source_cookie *sp) ga_init(&ga, 1, 250); // Loop until there is a finished line (or end-of-file). - sourcing_lnum++; + sp->sourcing_lnum++; for (;; ) { // make room to read at least 120 (more) characters ga_grow(&ga, 120); @@ -3559,7 +3570,7 @@ retry: // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--) {} if ((len & 1) != (c & 1)) { // escaped NL, read more - sourcing_lnum++; + sp->sourcing_lnum++; continue; } diff --git a/src/nvim/testdir/test_vimscript.vim b/src/nvim/testdir/test_vimscript.vim index f39e53d6dd..3fcba4134e 100644 --- a/src/nvim/testdir/test_vimscript.vim +++ b/src/nvim/testdir/test_vimscript.vim @@ -1409,6 +1409,76 @@ func Test_compound_assignment_operators() let @/ = '' endfunc +func Test_function_defined_line() + if has('gui_running') + " Can't catch the output of gvim. + return + endif + + let lines =<< trim [CODE] + " F1 + func F1() + " F2 + func F2() + " + " + " + return + endfunc + " F3 + execute "func F3()\n\n\n\nreturn\nendfunc" + " F4 + execute "func F4()\n + \\n + \\n + \\n + \return\n + \endfunc" + endfunc + " F5 + execute "func F5()\n\n\n\nreturn\nendfunc" + " F6 + execute "func F6()\n + \\n + \\n + \\n + \return\n + \endfunc" + call F1() + verbose func F1 + verbose func F2 + verbose func F3 + verbose func F4 + verbose func F5 + verbose func F6 + qall! + [CODE] + + call writefile(lines, 'Xtest.vim') + let res = system(v:progpath .. ' --clean -es -X -S Xtest.vim') + call assert_equal(0, v:shell_error) + + let m = matchstr(res, 'function F1()[^[:print:]]*[[:print:]]*') + call assert_match(' line 2$', m) + + let m = matchstr(res, 'function F2()[^[:print:]]*[[:print:]]*') + call assert_match(' line 4$', m) + + let m = matchstr(res, 'function F3()[^[:print:]]*[[:print:]]*') + call assert_match(' line 11$', m) + + let m = matchstr(res, 'function F4()[^[:print:]]*[[:print:]]*') + call assert_match(' line 13$', m) + + let m = matchstr(res, 'function F5()[^[:print:]]*[[:print:]]*') + call assert_match(' line 21$', m) + + let m = matchstr(res, 'function F6()[^[:print:]]*[[:print:]]*') + call assert_match(' line 23$', m) + + call delete('Xtest.vim') +endfunc + "------------------------------------------------------------------------------- " Modelines {{{1 " vim: ts=8 sw=4 tw=80 fdm=marker From 3b894b1cb18a9d4e399ab5b55004767f63a384c3 Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Sat, 12 Oct 2019 23:47:00 +0200 Subject: [PATCH 0163/1293] vim-patch:8.1.1723: heredoc assignment has no room for new features Problem: Heredoc assignment has no room for new features. (FUJIWARA Takuya) Solution: Require the marker does not start with a lower case character. (closes vim/vim#4705) https://github.com/vim/vim/commit/24582007294b0db3be9669d3b583ea45fc4f19b8 --- runtime/doc/eval.txt | 7 +++---- src/nvim/eval.c | 9 +++++++-- src/nvim/testdir/test_let.vim | 34 ++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 8cdaef007c..77b6ee24a4 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -9779,19 +9779,18 @@ This does NOT work: > Like above, but append/add/subtract the value for each |List| item. - *:let=<<* *:let-heredoc* *E990* *E991* + *:let=<<* *:let-heredoc* + *E990* *E991* *E172* *E221* :let {var-name} =<< [trim] {marker} text... text... {marker} Set internal variable {var-name} to a List containing the lines of text bounded by the string {marker}. - {marker} must not contain white space. + {marker} cannot start with a lower case character. The last line should end only with the {marker} string without any other character. Watch out for white space after {marker}! - If {marker} is not supplied, then "." is used as the - default marker. Without "trim" any white space characters in the lines of text are preserved. If "trim" is specified before diff --git a/src/nvim/eval.c b/src/nvim/eval.c index d314e3a732..7db9386937 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1548,7 +1548,7 @@ heredoc_get(exarg_T *eap, char_u *cmd) text_indent_len = -1; } - // The marker is the next word. Default marker is "." + // The marker is the next word. if (*cmd != NUL && *cmd != '"') { marker = skipwhite(cmd); p = skiptowhite(marker); @@ -1557,8 +1557,13 @@ heredoc_get(exarg_T *eap, char_u *cmd) return NULL; } *p = NUL; + if (islower(*marker)) { + EMSG(_("E221: Marker cannot start with lower case letter")); + return NULL; + } } else { - marker = (char_u *)"."; + EMSG(_("E172: Missing marker")); + return NULL; } list_T *l = tv_list_alloc(0); diff --git a/src/nvim/testdir/test_let.vim b/src/nvim/testdir/test_let.vim index d5100b5a82..66067d3fc0 100644 --- a/src/nvim/testdir/test_let.vim +++ b/src/nvim/testdir/test_let.vim @@ -153,14 +153,28 @@ func Test_let_heredoc_fails() call assert_fails('source XheredocFail', 'E126:') call delete('XheredocFail') - let text =<< trim END + let text =<< trim CodeEnd func MissingEnd() let v =<< END endfunc - END + CodeEnd call writefile(text, 'XheredocWrong') call assert_fails('source XheredocWrong', 'E126:') call delete('XheredocWrong') + + let text =<< trim TEXTend + let v =<< " comment + TEXTend + call writefile(text, 'XheredocNoMarker') + call assert_fails('source XheredocNoMarker', 'E172:') + call delete('XheredocNoMarker') + + let text =<< trim TEXTend + let v =<< text + TEXTend + call writefile(text, 'XheredocBadMarker') + call assert_fails('source XheredocBadMarker', 'E221:') + call delete('XheredocBadMarker') endfunc " Test for the setting a variable using the heredoc syntax @@ -173,9 +187,9 @@ END call assert_equal(["Some sample text", "\tText with indent", " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1) - let var2 =<< + let var2 =<< XXX Editor -. +XXX call assert_equal(['Editor'], var2) let var3 =< Date: Sun, 13 Oct 2019 00:04:41 +0200 Subject: [PATCH 0164/1293] vim-patch:8.1.1729: heredoc with trim not properly handled in function Problem: Heredoc with trim not properly handled in function. Solution: Allow for missing indent. (FUJIWARA Takuya, closes vim/vim#4713) https://github.com/vim/vim/commit/ecaa75b4cea329a3902b8565e028b32279b8322b --- src/nvim/eval.c | 39 ++++++++++++++++++++++------------- src/nvim/testdir/test_let.vim | 9 ++++++++ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 7db9386937..fd37e1001a 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -21297,8 +21297,6 @@ void ex_function(exarg_T *eap) bool overwrite = false; int indent; int nesting; - char_u *skip_until = NULL; - char_u *trimmed = NULL; dictitem_T *v; funcdict_T fudi; static int func_nr = 0; /* number for nameless function */ @@ -21308,6 +21306,9 @@ void ex_function(exarg_T *eap) hashitem_T *hi; linenr_T sourcing_lnum_off; linenr_T sourcing_lnum_top; + bool is_heredoc = false; + char_u *skip_until = NULL; + char_u *heredoc_trimmed = NULL; bool show_block = false; bool do_concat = true; @@ -21608,14 +21609,27 @@ void ex_function(exarg_T *eap) } if (skip_until != NULL) { - // Between ":append" and "." and between ":python < Date: Fri, 11 Oct 2019 19:27:15 +0200 Subject: [PATCH 0165/1293] tests/ui: make screen.lua use "linegrid" representation internally PR #8221 took a short-cut when implementing the tests: screen.lua would translate the linegrid highlight ids back into the old per-cell attribute description. Apart from cleaning up technical debt, this enables to check both rgb and cterm colors in the same expect(), which previously was needlessly restricted to ext_hlstate tests only. --- test/functional/terminal/helpers.lua | 2 +- test/functional/ui/hlstate_spec.lua | 2 +- test/functional/ui/screen.lua | 110 +++++++++++++++------------ 3 files changed, 63 insertions(+), 51 deletions(-) diff --git a/test/functional/terminal/helpers.lua b/test/functional/terminal/helpers.lua index f6cab6bd1e..d909888613 100644 --- a/test/functional/terminal/helpers.lua +++ b/test/functional/terminal/helpers.lua @@ -52,7 +52,7 @@ local function screen_setup(extra_rows, command, cols, opts) [3] = {bold = true}, [4] = {foreground = 12}, [5] = {bold = true, reverse = true}, - [6] = {background = 11}, + -- 6 was a duplicate item [7] = {foreground = 130}, [8] = {foreground = 15, background = 1}, -- error message [9] = {foreground = 4}, diff --git a/test/functional/ui/hlstate_spec.lua b/test/functional/ui/hlstate_spec.lua index d1c115587e..1e18df835a 100644 --- a/test/functional/ui/hlstate_spec.lua +++ b/test/functional/ui/hlstate_spec.lua @@ -259,7 +259,7 @@ describe('ext_hlstate detailed highlights', function() it("can use independent cterm and rgb colors", function() -- tell test module to save all attributes (doesn't change nvim options) - screen:set_hlstate_cterm(true) + screen:set_rgb_cterm(true) screen:set_default_attr_ids({ [1] = {{bold = true, foreground = Screen.colors.Blue1}, {foreground = 12}, {{hi_name = "NonText", ui_name = "EndOfBuffer", kind = "ui"}}}, diff --git a/test/functional/ui/screen.lua b/test/functional/ui/screen.lua index 06a2ac3ca2..68c3d54922 100644 --- a/test/functional/ui/screen.lua +++ b/test/functional/ui/screen.lua @@ -172,9 +172,9 @@ function Screen.new(width, height) _default_attr_ignore = nil, _mouse_enabled = true, _attrs = {}, - _hl_info = {}, + _hl_info = {[0]={}}, _attr_table = {[0]={{},{}}}, - _clear_attrs = {}, + _clear_attrs = nil, _new_attrs = false, _width = width, _height = height, @@ -206,8 +206,8 @@ function Screen:set_default_attr_ignore(attr_ignore) self._default_attr_ignore = attr_ignore end -function Screen:set_hlstate_cterm(val) - self._hlstate_cterm = val +function Screen:set_rgb_cterm(val) + self._rgb_cterm = val end function Screen:attach(options, session) @@ -223,7 +223,7 @@ function Screen:attach(options, session) self._session = session self._options = options - self._clear_attrs = (options.ext_linegrid and {{},{}}) or {} + self._clear_attrs = (not options.ext_linegrid) and {} or nil self:_handle_resize(self._width, self._height) self.uimeths.attach(self._width, self._height, options) if self._options.rgb == nil then @@ -363,8 +363,8 @@ function Screen:expect(expected, attr_ids, attr_ignore, ...) ids = attr_ids or self._default_attr_ids, ignore = attr_ignore or self._default_attr_ignore, } - if self._options.ext_hlstate then - attr_state.id_to_index = self:hlstate_check_attrs(attr_state.ids or {}) + if self._options.ext_linegrid then + attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids or {}) end self._new_attrs = false self:_wait(function() @@ -375,8 +375,8 @@ function Screen:expect(expected, attr_ids, attr_ignore, ...) end end - if self._options.ext_hlstate and self._new_attrs then - attr_state.id_to_index = self:hlstate_check_attrs(attr_state.ids or {}) + if self._options.ext_linegrid and self._new_attrs then + attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids or {}) end local actual_rows = self:render(not expected.any, attr_state) @@ -898,19 +898,16 @@ function Screen:_handle_grid_line(grid, row, col, items) assert(self._options.ext_linegrid) local line = self._grids[grid].rows[row+1] local colpos = col+1 - local hl = self._clear_attrs local hl_id = 0 for _,item in ipairs(items) do local text, hl_id_cell, count = unpack(item) if hl_id_cell ~= nil then hl_id = hl_id_cell - hl = self._attr_table[hl_id] end for _ = 1, (count or 1) do local cell = line[colpos] cell.text = text cell.hl_id = hl_id - cell.attrs = hl colpos = colpos+1 end end @@ -1070,6 +1067,7 @@ function Screen:_clear_row_section(grid, rownum, startcol, stopcol, invalid) for i = startcol, stopcol do row[i].text = (invalid and '�' or ' ') row[i].attrs = self._clear_attrs + row[i].hl_id = 0 end end @@ -1100,11 +1098,7 @@ function Screen:_row_repr(gridnr, rownr, attr_state, cursor) end if not did_window then - local attrs = row[i].attrs - if self._options.ext_linegrid then - attrs = attrs[(self._options.rgb and 1) or 2] - end - local attr_id = self:_get_attr_id(attr_state, attrs, row[i].hl_id) + local attr_id = self:_get_attr_id(attr_state, row[i].attrs, row[i].hl_id) if current_attr_id and attr_id ~= current_attr_id then -- close current attribute bracket table.insert(rv, '}') @@ -1261,8 +1255,8 @@ function Screen:get_snapshot(attrs, ignore) attr_state.ids[i] = a end end - if self._options.ext_hlstate then - attr_state.id_to_index = self:hlstate_check_attrs(attr_state.ids) + if self._options.ext_linegrid then + attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids) end local lines = self:render(true, attr_state, true) @@ -1299,8 +1293,8 @@ function Screen:print_snapshot(attrs, ignore) local attrstrs = {} for i, a in pairs(attr_state.ids) do local dict - if self._options.ext_hlstate then - dict = self:_pprint_hlstate(a) + if self._options.ext_linegrid then + dict = self:_pprint_hlitem(a) else dict = "{"..self:_pprint_attrs(a).."}" end @@ -1328,37 +1322,41 @@ function Screen:_insert_hl_id(attr_state, hl_id) return attr_state.id_to_index[hl_id] end local raw_info = self._hl_info[hl_id] - local info = {} - if #raw_info > 1 then - for i, item in ipairs(raw_info) do - info[i] = self:_insert_hl_id(attr_state, item.id) - end - else - info[1] = {} - for k, v in pairs(raw_info[1]) do - if k ~= "id" then - info[1][k] = v + local info = nil + if self._options.ext_hlstate then + info = {} + if #raw_info > 1 then + for i, item in ipairs(raw_info) do + info[i] = self:_insert_hl_id(attr_state, item.id) + end + else + info[1] = {} + for k, v in pairs(raw_info[1]) do + if k ~= "id" then + info[1][k] = v + end end end end local entry = self._attr_table[hl_id] local attrval - if self._hlstate_cterm then + if self._rgb_cterm then attrval = {entry[1], entry[2], info} -- unpack() doesn't work - else + elseif self._options.ext_hlstate then attrval = {entry[1], info} + else + attrval = self._options.rgb and entry[1] or entry[2] end - table.insert(attr_state.ids, attrval) attr_state.id_to_index[hl_id] = #attr_state.ids return #attr_state.ids end -function Screen:hlstate_check_attrs(attrs) +function Screen:linegrid_check_attrs(attrs) local id_to_index = {} - for i = 1,#self._attr_table do + for i, def_attr in pairs(self._attr_table) do local iinfo = self._hl_info[i] local matchinfo = {} if #iinfo > 1 then @@ -1370,13 +1368,16 @@ function Screen:hlstate_check_attrs(attrs) end for k,v in pairs(attrs) do local attr, info, attr_rgb, attr_cterm - if self._hlstate_cterm then + if self._rgb_cterm then attr_rgb, attr_cterm, info = unpack(v) attr = {attr_rgb, attr_cterm} - else + elseif self._options.ext_hlstate then attr, info = unpack(v) + else + attr = v + info = {} end - if self:_equal_attr_def(attr, self._attr_table[i]) then + if self:_equal_attr_def(attr, def_attr) then if #info == #matchinfo then local match = false if #info == 1 then @@ -1397,24 +1398,31 @@ function Screen:hlstate_check_attrs(attrs) end end end + if self:_equal_attr_def(self._rgb_cterm and {{}, {}} or {}, def_attr) and #self._hl_info[i] == 0 then + id_to_index[i] = "" + end end return id_to_index end -function Screen:_pprint_hlstate(item) +function Screen:_pprint_hlitem(item) -- print(inspect(item)) - local attrdict = "{"..self:_pprint_attrs(item[1]).."}, " + local multi = self._rgb_cterm or self._options.ext_hlstate + local attrdict = "{"..self:_pprint_attrs(multi and item[1] or item).."}" local attrdict2, hlinfo - if self._hlstate_cterm then - attrdict2 = "{"..self:_pprint_attrs(item[2]).."}, " + local descdict = "" + if self._rgb_cterm then + attrdict2 = ", {"..self:_pprint_attrs(item[2]).."}" hlinfo = item[3] else attrdict2 = "" hlinfo = item[2] end - local descdict = "{"..self:_pprint_hlinfo(hlinfo).."}" - return "{"..attrdict..attrdict2..descdict.."}" + if self._options.ext_hlstate then + descdict = ", {"..self:_pprint_hlinfo(hlinfo).."}" + end + return (multi and "{" or "")..attrdict..attrdict2..descdict..(multi and "}" or "") end function Screen:_pprint_hlinfo(states) @@ -1464,9 +1472,11 @@ function Screen:_get_attr_id(attr_state, attrs, hl_id) return end - if self._options.ext_hlstate then + if self._options.ext_linegrid then local id = attr_state.id_to_index[hl_id] - if id ~= nil or hl_id == 0 then + if id == "" then -- sentinel for empty it + return nil + elseif id ~= nil then return id end if attr_state.mutable then @@ -1497,10 +1507,12 @@ function Screen:_get_attr_id(attr_state, attrs, hl_id) end function Screen:_equal_attr_def(a, b) - if self._hlstate_cterm then + if self._rgb_cterm then return self:_equal_attrs(a[1],b[1]) and self:_equal_attrs(a[2],b[2]) - else + elseif self._options.rgb then return self:_equal_attrs(a,b[1]) + else + return self:_equal_attrs(a,b[2]) end end From a330129a280a34963e42d498f7cb1f53e6723e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 12 Oct 2019 13:29:51 +0200 Subject: [PATCH 0166/1293] tests/ui: cleanup illegitimate usages of "attr_ignore" "attr_ignore" is an anti-pattern, with snapshot_util() just include all the highlights already. --- .../legacy/063_match_and_matchadd_spec.lua | 20 +++++--- test/functional/legacy/search_spec.lua | 5 +- test/functional/plugin/health_spec.lua | 20 ++++---- test/functional/plugin/man_spec.lua | 50 +++++++++---------- test/functional/provider/clipboard_spec.lua | 48 +++++++++++------- test/functional/terminal/buffer_spec.lua | 6 +-- test/functional/ui/mouse_spec.lua | 14 +++--- test/functional/ui/screen.lua | 10 ++-- 8 files changed, 90 insertions(+), 83 deletions(-) diff --git a/test/functional/legacy/063_match_and_matchadd_spec.lua b/test/functional/legacy/063_match_and_matchadd_spec.lua index 518d79861b..a4f5d6ac5b 100644 --- a/test/functional/legacy/063_match_and_matchadd_spec.lua +++ b/test/functional/legacy/063_match_and_matchadd_spec.lua @@ -14,6 +14,10 @@ describe('063: Test for ":match", "matchadd()" and related functions', function( it('is working', function() local screen = Screen.new(40, 5) screen:attach() + screen:set_default_attr_ids({ + [0] = {bold = true, foreground = Screen.colors.Blue}, + [1] = {background = Screen.colors.Red}, + }) -- Check that "matcharg()" returns the correct group and pattern if a match -- is defined. @@ -126,22 +130,22 @@ describe('063: Test for ":match", "matchadd()" and related functions', function( command("call matchaddpos('MyGroup1', [[1, 5], [1, 8, 3]], 10, 3)") screen:expect([[ abcd{1:e}fg{1:hij}klmnop^q | - ~ | - ~ | - ~ | + {0:~ }| + {0:~ }| + {0:~ }| | - ]], {[1] = {background = Screen.colors.Red}}, {{bold = true, foreground = Screen.colors.Blue}}) + ]]) command("call clearmatches()") command("call setline(1, 'abcdΣabcdef')") command("call matchaddpos('MyGroup1', [[1, 4, 2], [1, 9, 2]])") screen:expect([[ abc{1:dΣ}ab{1:cd}e^f | - ~ | - ~ | - ~ | + {0:~ }| + {0:~ }| + {0:~ }| | - ]],{[1] = {background = Screen.colors.Red}}, {{bold = true, foreground = Screen.colors.Blue}}) + ]]) end) end) diff --git a/test/functional/legacy/search_spec.lua b/test/functional/legacy/search_spec.lua index 3ed06a22e7..bed4dc2a09 100644 --- a/test/functional/legacy/search_spec.lua +++ b/test/functional/legacy/search_spec.lua @@ -408,10 +408,7 @@ describe('search cmdline', function() screen = Screen.new(20, 6) screen:attach() screen:set_default_attr_ids({ - inc = {reverse = true} - }) - screen:set_default_attr_ignore({ - {bold=true, reverse=true}, {bold=true, foreground=Screen.colors.Blue1} + inc = {reverse = true}, }) tenlines() diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index 3525e235de..a78ed07876 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -116,8 +116,6 @@ describe('health.vim', function() screen:set_default_attr_ids({ Ok = { foreground = Screen.colors.Grey3, background = 6291200 }, Error = { foreground = Screen.colors.Grey100, background = Screen.colors.Red }, - }) - screen:set_default_attr_ignore({ Heading = { bold=true, foreground=Screen.colors.Magenta }, Heading2 = { foreground = Screen.colors.SlateBlue }, Bar = { foreground=Screen.colors.Purple }, @@ -126,18 +124,18 @@ describe('health.vim', function() command("checkhealth foo success1") command("1tabclose") command("set laststatus=0") - screen:expect([[ + screen:expect{grid=[[ ^ | - health#foo#check | - ========================================================================| - - {Error:ERROR:} No healthcheck found for "foo" plugin. | + {Heading:health#foo#check} | + {Bar:========================================================================}| + {Bullet: -} {Error:ERROR:} No healthcheck found for "foo" plugin. | | - health#success1#check | - ========================================================================| - ## report 1 | - - {Ok:OK:} everything is fine | + {Heading:health#success1#check} | + {Bar:========================================================================}| + {Heading2:##}{Heading: report 1} | + {Bullet: -} {Ok:OK:} everything is fine | | - ]]) + ]]} end) it("gracefully handles invalid healthcheck", function() diff --git a/test/functional/plugin/man_spec.lua b/test/functional/plugin/man_spec.lua index d95995797e..e06e4b874e 100644 --- a/test/functional/plugin/man_spec.lua +++ b/test/functional/plugin/man_spec.lua @@ -19,10 +19,8 @@ describe(':Man', function() u = { underline = true }, bi = { bold = true, italic = true }, biu = { bold = true, italic = true, underline = true }, - }) - screen:set_default_attr_ignore({ - { foreground = Screen.colors.Blue }, -- control chars - { bold = true, foreground = Screen.colors.Blue } -- empty line '~'s + c = { foreground = Screen.colors.Blue }, -- control chars + eob = { bold = true, foreground = Screen.colors.Blue } -- empty line '~'s }) screen:attach() end) @@ -36,21 +34,21 @@ describe(':Man', function() ithis iiss aa test with _o_v_e_r_s_t_r_u_c_k text]]) - screen:expect([[ - this i^His^Hs a^Ha test | - with _^Ho_^Hv_^He_^Hr_^Hs_^Ht_^Hr_^Hu_^Hc_^Hk tex^t | - ~ | - ~ | - | - ]]) + screen:expect{grid=[[ + this i{c:^H}is{c:^H}s a{c:^H}a test | + with _{c:^H}o_{c:^H}v_{c:^H}e_{c:^H}r_{c:^H}s_{c:^H}t_{c:^H}r_{c:^H}u_{c:^H}c_{c:^H}k tex^t | + {eob:~ }| + {eob:~ }| + | + ]]} eval('man#init_pager()') screen:expect([[ ^this {b:is} {b:a} test | with {u:overstruck} text | - ~ | - ~ | + {eob:~ }| + {eob:~ }| | ]]) end) @@ -60,21 +58,21 @@ describe(':Man', function() ithis [1mis [3ma [4mtest[0m [4mwith[24m [4mescaped[24m [4mtext[24m]]) - screen:expect([=[ - this ^[[1mis ^[[3ma ^[[4mtest^[[0m | - ^[[4mwith^[[24m ^[[4mescaped^[[24m ^[[4mtext^[[24^m | - ~ | - ~ | - | - ]=]) + screen:expect{grid=[=[ + this {c:^[}[1mis {c:^[}[3ma {c:^[}[4mtest{c:^[}[0m | + {c:^[}[4mwith{c:^[}[24m {c:^[}[4mescaped{c:^[}[24m {c:^[}[4mtext{c:^[}[24^m | + {eob:~ }| + {eob:~ }| + | + ]=]} eval('man#init_pager()') screen:expect([[ ^this {b:is }{bi:a }{biu:test} | {u:with} {u:escaped} {u:text} | - ~ | - ~ | + {eob:~ }| + {eob:~ }| | ]]) end) @@ -88,8 +86,8 @@ describe(':Man', function() screen:expect([[ ^this {b:is} {b:あ} test | with {u:överstrũck} te{i:xt¶} | - ~ | - ~ | + {eob:~ }| + {eob:~ }| | ]]) end) @@ -105,7 +103,7 @@ describe(':Man', function() {b:^_begins} | {b:mid_dle} | {u:mid_dle} | - ~ | + {eob:~ }| | ]]) end) @@ -121,7 +119,7 @@ describe(':Man', function() ^· {b:·} | {b:·} | {b:·} double | - ~ | + {eob:~ }| | ]]) end) diff --git a/test/functional/provider/clipboard_spec.lua b/test/functional/provider/clipboard_spec.lua index b2d75db745..da9dd09129 100644 --- a/test/functional/provider/clipboard_spec.lua +++ b/test/functional/provider/clipboard_spec.lua @@ -88,6 +88,11 @@ describe('clipboard', function() before_each(function() clear() screen = Screen.new(72, 4) + screen:set_default_attr_ids({ + [0] = {bold = true, foreground = Screen.colors.Blue}, + [1] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red}, + [2] = {bold = true, foreground = Screen.colors.SeaGreen4}, + }) screen:attach() command("set display-=msgsep") end) @@ -103,22 +108,22 @@ describe('clipboard', function() feed_command('redir @+> | :silent echo system("cat CONTRIBUTING.md") | redir END') screen:expect([[ ^ | - ~ | - ~ | + {0:~ }| + {0:~ }| clipboard: No provider. Try ":checkhealth" or ":h clipboard". | - ]], nil, {{bold = true, foreground = Screen.colors.Blue}}) + ]]) end) it('`:redir @+>|bogus_cmd|redir END` + invalid g:clipboard must not recurse #7184', function() command("let g:clipboard = 'bogus'") feed_command('redir @+> | bogus_cmd | redir END') - screen:expect([[ - ~ | + screen:expect{grid=[[ + {0:~ }| clipboard: No provider. Try ":checkhealth" or ":h clipboard". | - E492: Not an editor command: bogus_cmd | redir END | - Press ENTER or type command to continue^ | - ]], nil, {{bold = true, foreground = Screen.colors.Blue}}) + {1:E492: Not an editor command: bogus_cmd | redir END} | + {2:Press ENTER or type command to continue}^ | + ]]} end) it('invalid g:clipboard shows hint if :redir is not active', function() @@ -131,10 +136,10 @@ describe('clipboard', function() feed_command('let @+="foo"') screen:expect([[ ^ | - ~ | - ~ | + {0:~ }| + {0:~ }| clipboard: No provider. Try ":checkhealth" or ":h clipboard". | - ]], nil, {{bold = true, foreground = Screen.colors.Blue}}) + ]]) end) it('valid g:clipboard', function() @@ -266,13 +271,17 @@ describe('clipboard (with fake clipboard.vim)', function() function() local screen = Screen.new(72, 4) screen:attach() + screen:set_default_attr_ids({ + [0] = {bold = true, foreground = Screen.colors.Blue}, + [1] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red}, + }) feed_command('redir @+> | bogus_cmd | redir END') screen:expect([[ ^ | - ~ | - ~ | - E492: Not an editor command: bogus_cmd | redir END | - ]], nil, {{bold = true, foreground = Screen.colors.Blue}}) + {0:~ }| + {0:~ }| + {1:E492: Not an editor command: bogus_cmd | redir END} | + ]]) end) it('has independent "* and unnamed registers by default', function() @@ -637,6 +646,9 @@ describe('clipboard (with fake clipboard.vim)', function() feed_command('set mouse=a') local screen = Screen.new(30, 5) + screen:set_default_attr_ids({ + [0] = {bold = true, foreground = Screen.colors.Blue}, + }) screen:attach() insert([[ the source @@ -646,10 +658,10 @@ describe('clipboard (with fake clipboard.vim)', function() screen:expect([[ the ^source | a target | - ~ | - ~ | + {0:~ }| + {0:~ }| | - ]], nil, {{bold = true, foreground = Screen.colors.Blue}}) + ]]) feed('<0,1>') expect([[ diff --git a/test/functional/terminal/buffer_spec.lua b/test/functional/terminal/buffer_spec.lua index 1763574bf9..7560b0e872 100644 --- a/test/functional/terminal/buffer_spec.lua +++ b/test/functional/terminal/buffer_spec.lua @@ -208,18 +208,18 @@ describe(':terminal buffer', function() feed_command('terminal') feed('') feed_command('confirm bdelete') - screen:expect{any='Close "term://', attr_ignore=true} + screen:expect{any='Close "term://'} end) it('with &confirm', function() feed_command('terminal') feed('') feed_command('bdelete') - screen:expect{any='E89', attr_ignore=true} + screen:expect{any='E89'} feed('') eq('terminal', eval('&buftype')) feed_command('set confirm | bdelete') - screen:expect{any='Close "term://', attr_ignore=true} + screen:expect{any='Close "term://'} feed('y') neq('terminal', eval('&buftype')) end) diff --git a/test/functional/ui/mouse_spec.lua b/test/functional/ui/mouse_spec.lua index 440bae58e0..fedef7da8a 100644 --- a/test/functional/ui/mouse_spec.lua +++ b/test/functional/ui/mouse_spec.lua @@ -26,6 +26,8 @@ describe('ui/mouse/input', function() }, [4] = {reverse = true}, [5] = {bold = true, reverse = true}, + [6] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red}, + [7] = {bold = true, foreground = Screen.colors.SeaGreen4}, }) command("set display-=msgsep") feed('itestingmousesupport and selection') @@ -620,12 +622,12 @@ describe('ui/mouse/input', function() meths.set_option('tags', './non-existent-tags-file') feed('<0,0>') screen:expect([[ - E433: No tags file | - E426: tag not found: test| - ing | - Press ENTER or type comma| - nd to continue^ | - ]],nil,true) + {6:E433: No tags file} | + {6:E426: tag not found: test}| + {6:ing} | + {7:Press ENTER or type comma}| + {7:nd to continue}^ | + ]]) feed('') end) diff --git a/test/functional/ui/screen.lua b/test/functional/ui/screen.lua index 68c3d54922..b15e2980de 100644 --- a/test/functional/ui/screen.lua +++ b/test/functional/ui/screen.lua @@ -66,7 +66,6 @@ -- [1] = {reverse = true, bold = true}, -- [2] = {reverse = true} -- }) --- screen:set_default_attr_ignore( {{}, {bold=true, foreground=NonText}} ) -- -- To help write screen tests, see Screen:snapshot_util(). -- To debug screen tests, see Screen:redraw_debug(). @@ -169,7 +168,6 @@ function Screen.new(width, height) ruler = {}, hl_groups = {}, _default_attr_ids = nil, - _default_attr_ignore = nil, _mouse_enabled = true, _attrs = {}, _hl_info = {[0]={}}, @@ -202,10 +200,6 @@ function Screen:get_default_attr_ids() return deepcopy(self._default_attr_ids) end -function Screen:set_default_attr_ignore(attr_ignore) - self._default_attr_ignore = attr_ignore -end - function Screen:set_rgb_cterm(val) self._rgb_cterm = val end @@ -361,7 +355,7 @@ function Screen:expect(expected, attr_ids, attr_ignore, ...) end local attr_state = { ids = attr_ids or self._default_attr_ids, - ignore = attr_ignore or self._default_attr_ignore, + ignore = attr_ignore } if self._options.ext_linegrid then attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids or {}) @@ -1478,6 +1472,8 @@ function Screen:_get_attr_id(attr_state, attrs, hl_id) return nil elseif id ~= nil then return id + elseif attr_state.ignore == true then + return nil end if attr_state.mutable then id = self:_insert_hl_id(attr_state, hl_id) From 4987311fb5b8f4a11d26995f71f5f402a9e2ace4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sun, 13 Oct 2019 20:18:22 +0200 Subject: [PATCH 0167/1293] tests/ui: remove unnecessary screen:detach() It is perfectly fine and expected to detach from the screen just by the UI disconnecting from nvim or exiting nvim. Just keep detach() in screen_basic_spec, to get some coverage of the detach method itself. This avoids hang on failure in many situations (though one could argue that detach() should be "fast", or at least "as fast as resize", which works in press-return already). Never use detach() just to change the size of the screen, try_resize() method exists for that specifically. --- test/functional/api/menu_spec.lua | 4 --- test/functional/core/job_spec.lua | 3 -- test/functional/eval/api_functions_spec.lua | 1 - test/functional/eval/system_spec.lua | 4 --- test/functional/ex_cmds/drop_spec.lua | 4 --- test/functional/ex_cmds/highlight_spec.lua | 4 --- test/functional/legacy/045_folding_spec.lua | 3 -- test/functional/legacy/search_spec.lua | 31 +++++--------------- test/functional/options/chars_spec.lua | 8 ----- test/functional/plugin/man_spec.lua | 4 --- test/functional/terminal/mouse_spec.lua | 4 --- test/functional/terminal/scrollback_spec.lua | 8 ----- test/functional/terminal/tui_spec.lua | 4 --- test/functional/ui/bufhl_spec.lua | 4 --- test/functional/ui/cmdline_spec.lua | 8 ----- test/functional/ui/cursor_spec.lua | 4 --- test/functional/ui/fold_spec.lua | 4 --- test/functional/ui/highlight_spec.lua | 31 +++++--------------- test/functional/ui/inccommand_spec.lua | 15 ---------- test/functional/ui/mouse_spec.lua | 4 --- test/functional/ui/multibyte_spec.lua | 8 ----- test/functional/ui/multigrid_spec.lua | 4 --- test/functional/ui/options_spec.lua | 4 --- test/functional/ui/output_spec.lua | 1 - test/functional/ui/sign_spec.lua | 4 --- test/functional/ui/spell_spec.lua | 4 --- test/functional/ui/syntax_conceal_spec.lua | 4 --- test/functional/ui/tabline_spec.lua | 4 --- 28 files changed, 14 insertions(+), 171 deletions(-) diff --git a/test/functional/api/menu_spec.lua b/test/functional/api/menu_spec.lua index 2cfa0e3e47..34a92477f3 100644 --- a/test/functional/api/menu_spec.lua +++ b/test/functional/api/menu_spec.lua @@ -15,10 +15,6 @@ describe("update_menu notification", function() screen:attach() end) - after_each(function() - screen:detach() - end) - local function expect_sent(expected) screen:expect{condition=function() if screen.update_menu ~= expected then diff --git a/test/functional/core/job_spec.lua b/test/functional/core/job_spec.lua index 9c37e55f42..d4ce690867 100644 --- a/test/functional/core/job_spec.lua +++ b/test/functional/core/job_spec.lua @@ -963,9 +963,6 @@ describe("pty process teardown", function() | ]]) end) - after_each(function() - screen:detach() - end) it("does not prevent/delay exit. #4798 #4900", function() if helpers.pending_win32(pending) then return end diff --git a/test/functional/eval/api_functions_spec.lua b/test/functional/eval/api_functions_spec.lua index 4fbd08f102..f527aff33f 100644 --- a/test/functional/eval/api_functions_spec.lua +++ b/test/functional/eval/api_functions_spec.lua @@ -144,7 +144,6 @@ describe('eval-API', function() {5:~ }| | ]]) - screen:detach() end) it('cannot be called from sandbox', function() diff --git a/test/functional/eval/system_spec.lua b/test/functional/eval/system_spec.lua index 85d57006b5..1e4d760dbc 100644 --- a/test/functional/eval/system_spec.lua +++ b/test/functional/eval/system_spec.lua @@ -121,10 +121,6 @@ describe('system()', function() screen:attach() end) - after_each(function() - screen:detach() - end) - if iswin() then local function test_more() eq('root = true', eval([[get(split(system('"more" ".editorconfig"'), "\n"), 0, '')]])) diff --git a/test/functional/ex_cmds/drop_spec.lua b/test/functional/ex_cmds/drop_spec.lua index d6da0d8e88..762ea3d166 100644 --- a/test/functional/ex_cmds/drop_spec.lua +++ b/test/functional/ex_cmds/drop_spec.lua @@ -19,10 +19,6 @@ describe(":drop", function() command("set laststatus=2 shortmess-=F") end) - after_each(function() - screen:detach() - end) - it("works like :e when called with only one window open", function() feed_command("drop tmp1.vim") screen:expect([[ diff --git a/test/functional/ex_cmds/highlight_spec.lua b/test/functional/ex_cmds/highlight_spec.lua index 25968b8204..1cd6759a53 100644 --- a/test/functional/ex_cmds/highlight_spec.lua +++ b/test/functional/ex_cmds/highlight_spec.lua @@ -13,10 +13,6 @@ describe(':highlight', function() screen:attach() end) - after_each(function() - screen:detach() - end) - it('invalid color name', function() eq('Vim(highlight):E421: Color name or number not recognized: ctermfg=#181818', exc_exec("highlight normal ctermfg=#181818")) diff --git a/test/functional/legacy/045_folding_spec.lua b/test/functional/legacy/045_folding_spec.lua index 6ca1176aea..1e5239ceac 100644 --- a/test/functional/legacy/045_folding_spec.lua +++ b/test/functional/legacy/045_folding_spec.lua @@ -14,9 +14,6 @@ describe('folding', function() screen = Screen.new(20, 8) screen:attach() end) - after_each(function() - screen:detach() - end) it('creation, opening, moving (to the end) and closing', function() insert([[ diff --git a/test/functional/legacy/search_spec.lua b/test/functional/legacy/search_spec.lua index bed4dc2a09..a207b176d3 100644 --- a/test/functional/legacy/search_spec.lua +++ b/test/functional/legacy/search_spec.lua @@ -17,7 +17,10 @@ describe('search cmdline', function() screen = Screen.new(20, 3) screen:attach() screen:set_default_attr_ids({ - inc = {reverse = true} + inc = {reverse = true}, + err = { foreground = Screen.colors.Grey100, background = Screen.colors.Red }, + more = { bold = true, foreground = Screen.colors.SeaGreen4 }, + tilde = { bold = true, foreground = Screen.colors.Blue1 }, }) end) @@ -404,12 +407,7 @@ describe('search cmdline', function() end) it('keeps the view after deleting a char from the search', function() - screen:detach() - screen = Screen.new(20, 6) - screen:attach() - screen:set_default_attr_ids({ - inc = {reverse = true}, - }) + screen:try_resize(20, 6) tenlines() feed('/foo') @@ -445,14 +443,7 @@ describe('search cmdline', function() end) it('restores original view after failed search', function() - screen:detach() - screen = Screen.new(40, 3) - screen:attach() - screen:set_default_attr_ids({ - inc = {reverse = true}, - err = { foreground = Screen.colors.Grey100, background = Screen.colors.Red }, - more = { bold = true, foreground = Screen.colors.SeaGreen4 }, - }) + screen:try_resize(40, 3) tenlines() feed('0') feed('/foo') @@ -481,15 +472,7 @@ describe('search cmdline', function() it("CTRL-G with 'incsearch' and ? goes in the right direction", function() -- oldtest: Test_search_cmdline4(). - screen:detach() - screen = Screen.new(40, 4) - screen:attach() - screen:set_default_attr_ids({ - inc = {reverse = true}, - err = { foreground = Screen.colors.Grey100, background = Screen.colors.Red }, - more = { bold = true, foreground = Screen.colors.SeaGreen4 }, - tilde = { bold = true, foreground = Screen.colors.Blue1 }, - }) + screen:try_resize(40, 4) command('enew!') funcs.setline(1, {' 1 the first', ' 2 the second', ' 3 the third'}) command('set laststatus=0 shortmess+=s') diff --git a/test/functional/options/chars_spec.lua b/test/functional/options/chars_spec.lua index 1330c29e61..3453e79429 100644 --- a/test/functional/options/chars_spec.lua +++ b/test/functional/options/chars_spec.lua @@ -16,10 +16,6 @@ describe("'fillchars'", function() screen:attach() end) - after_each(function() - screen:detach() - end) - local function shouldfail(val,errval) errval = errval or val eq('Vim(set):E474: Invalid argument: fillchars='..errval, @@ -100,10 +96,6 @@ describe("'listchars'", function() screen:attach() end) - after_each(function() - screen:detach() - end) - it('is local to window', function() feed('i') command('set laststatus=0') diff --git a/test/functional/plugin/man_spec.lua b/test/functional/plugin/man_spec.lua index e06e4b874e..e5b2e7dc1f 100644 --- a/test/functional/plugin/man_spec.lua +++ b/test/functional/plugin/man_spec.lua @@ -25,10 +25,6 @@ describe(':Man', function() screen:attach() end) - after_each(function() - screen:detach() - end) - it('clears backspaces from text and adds highlights', function() rawfeed([[ ithis iiss aa test diff --git a/test/functional/terminal/mouse_spec.lua b/test/functional/terminal/mouse_spec.lua index 64f437f206..ee3db7ae97 100644 --- a/test/functional/terminal/mouse_spec.lua +++ b/test/functional/terminal/mouse_spec.lua @@ -31,10 +31,6 @@ describe(':terminal mouse', function() ]]) end) - after_each(function() - screen:detach() - end) - describe('when the terminal has focus', function() it('will exit focus on mouse-scroll', function() eq('t', eval('mode()')) diff --git a/test/functional/terminal/scrollback_spec.lua b/test/functional/terminal/scrollback_spec.lua index ff6a74fe89..060f065bfc 100644 --- a/test/functional/terminal/scrollback_spec.lua +++ b/test/functional/terminal/scrollback_spec.lua @@ -21,10 +21,6 @@ describe(':terminal scrollback', function() screen = thelpers.screen_setup(nil, nil, 30) end) - after_each(function() - screen:detach() - end) - describe('when the limit is exceeded', function() before_each(function() local lines = {} @@ -406,8 +402,6 @@ describe("'scrollback' option", function() feed_data(nvim_dir..'/shell-test REP 31 line'..(iswin() and '\r' or '\n')) screen:expect{any='30: line '} retry(nil, nil, function() expect_lines(7) end) - - screen:detach() end) it('deletes lines (only) if necessary', function() @@ -464,8 +458,6 @@ describe("'scrollback' option", function() -- Verify off-screen state eq((iswin() and '36: line' or '35: line'), eval("getline(line('w0') - 1)")) eq((iswin() and '27: line' or '26: line'), eval("getline(line('w0') - 10)")) - - screen:detach() end) it('defaults to 10000 in :terminal buffers', function() diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index 978267e040..326a578bb7 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -45,10 +45,6 @@ describe('TUI', function() child_session = helpers.connect(child_server) end) - after_each(function() - screen:detach() - end) - -- Wait for mode in the child Nvim (avoid "typeahead race" #10826). local function wait_for_mode(mode) retry(nil, nil, function() diff --git a/test/functional/ui/bufhl_spec.lua b/test/functional/ui/bufhl_spec.lua index d8ca947645..5df909f79c 100644 --- a/test/functional/ui/bufhl_spec.lua +++ b/test/functional/ui/bufhl_spec.lua @@ -37,10 +37,6 @@ describe('Buffer highlighting', function() }) end) - after_each(function() - screen:detach() - end) - local add_highlight = curbufmeths.add_highlight local clear_namespace = curbufmeths.clear_namespace diff --git a/test/functional/ui/cmdline_spec.lua b/test/functional/ui/cmdline_spec.lua index f9769c706f..fe1b2c13d1 100644 --- a/test/functional/ui/cmdline_spec.lua +++ b/test/functional/ui/cmdline_spec.lua @@ -25,10 +25,6 @@ local function test_cmdline(linegrid) screen = new_screen({rgb=true, ext_cmdline=true, ext_linegrid=linegrid}) end) - after_each(function() - screen:detach() - end) - it('works', function() feed(':') screen:expect{grid=[[ @@ -804,10 +800,6 @@ describe('cmdline redraw', function() screen = new_screen({rgb=true}) end) - after_each(function() - screen:detach() - end) - it('with timer', function() feed(':012345678901234567890123456789') screen:expect{grid=[[ diff --git a/test/functional/ui/cursor_spec.lua b/test/functional/ui/cursor_spec.lua index 67aba919b0..8ad4182f41 100644 --- a/test/functional/ui/cursor_spec.lua +++ b/test/functional/ui/cursor_spec.lua @@ -13,10 +13,6 @@ describe('ui/cursor', function() screen:attach() end) - after_each(function() - screen:detach() - end) - it("'guicursor' is published as a UI event", function() local expected_mode_info = { [1] = { diff --git a/test/functional/ui/fold_spec.lua b/test/functional/ui/fold_spec.lua index c5ef718883..eb81aba131 100644 --- a/test/functional/ui/fold_spec.lua +++ b/test/functional/ui/fold_spec.lua @@ -24,10 +24,6 @@ describe("folded lines", function() }) end) - after_each(function() - screen:detach() - end) - it("work with more than one signcolumn", function() command("set signcolumn=yes:9") feed("i") diff --git a/test/functional/ui/highlight_spec.lua b/test/functional/ui/highlight_spec.lua index 1b25570997..d7791a3107 100644 --- a/test/functional/ui/highlight_spec.lua +++ b/test/functional/ui/highlight_spec.lua @@ -35,7 +35,6 @@ describe('highlight: `:syntax manual`', function() end) after_each(function() - screen:detach() os.remove('Xtest-functional-ui-highlight.tmp.vim') end) @@ -97,10 +96,6 @@ describe('highlight defaults', function() command("set display-=msgsep") end) - after_each(function() - screen:detach() - end) - it('window status bar', function() screen:set_default_attr_ids({ [0] = {bold=true, foreground=Screen.colors.Blue}, @@ -346,17 +341,10 @@ describe('highlight defaults', function() end) describe('highlight', function() - local screen - - before_each(function() - clear() - screen = Screen.new(25,10) - screen:attach() - end) + before_each(clear) it('visual', function() - screen:detach() - screen = Screen.new(20,4) + local screen = Screen.new(20,4) screen:attach() screen:set_default_attr_ids({ [1] = {background = Screen.colors.LightGrey}, @@ -389,8 +377,7 @@ describe('highlight', function() end) it('cterm=standout gui=standout', function() - screen:detach() - screen = Screen.new(20,5) + local screen = Screen.new(20,5) screen:attach() screen:set_default_attr_ids({ [1] = {bold = true, foreground = Screen.colors.Blue1}, @@ -413,8 +400,7 @@ describe('highlight', function() end) it('strikethrough', function() - screen:detach() - screen = Screen.new(25,6) + local screen = Screen.new(25,6) screen:attach() feed_command('syntax on') feed_command('syn keyword TmpKeyword foo') @@ -439,8 +425,7 @@ describe('highlight', function() end) it('nocombine', function() - screen:detach() - screen = Screen.new(25,6) + local screen = Screen.new(25,6) screen:set_default_attr_ids{ [1] = {foreground = Screen.colors.SlateBlue, underline = true}, [2] = {bold = true, foreground = Screen.colors.Blue1}, @@ -487,6 +472,8 @@ describe('highlight', function() end) it('guisp (special/undercurl)', function() + local screen = Screen.new(25,10) + screen:attach() feed_command('syntax on') feed_command('syn keyword TmpKeyword neovim') feed_command('syn keyword TmpKeyword1 special') @@ -542,10 +529,6 @@ describe("'listchars' highlight", function() screen:attach() end) - after_each(function() - screen:detach() - end) - it("'cursorline' and 'cursorcolumn'", function() screen:set_default_attr_ids({ [0] = {bold=true, foreground=Screen.colors.Blue}, diff --git a/test/functional/ui/inccommand_spec.lua b/test/functional/ui/inccommand_spec.lua index e9a7c8c2df..d60cd08fb0 100644 --- a/test/functional/ui/inccommand_spec.lua +++ b/test/functional/ui/inccommand_spec.lua @@ -556,7 +556,6 @@ describe(":substitute, 'inccommand' preserves undo", function() ]]) end end - screen:detach() end) it('with undolevels=2', function() @@ -647,7 +646,6 @@ describe(":substitute, 'inccommand' preserves undo", function() Already ...t change | ]]) end - screen:detach() end end) @@ -713,7 +711,6 @@ describe(":substitute, 'inccommand' preserves undo", function() Already ...t change | ]]) end - screen:detach() end) end) @@ -726,10 +723,6 @@ describe(":substitute, inccommand=split", function() common_setup(screen, "split", default_text .. default_text) end) - after_each(function() - screen:detach() - end) - it("preserves 'modified' buffer flag", function() feed_command("set nomodified") feed(":%s/tw") @@ -1241,10 +1234,6 @@ describe("inccommand=nosplit", function() common_setup(screen, "nosplit", default_text .. default_text) end) - after_each(function() - if screen then screen:detach() end - end) - it("works with :smagic, :snomagic", function() feed_command("set hlsearch") insert("Line *.3.* here") @@ -1719,10 +1708,6 @@ describe("'inccommand' split windows", function() common_setup(screen, "split", default_text) end - after_each(function() - screen:detach() - end) - it('work after more splits', function() refresh() diff --git a/test/functional/ui/mouse_spec.lua b/test/functional/ui/mouse_spec.lua index fedef7da8a..7840ba9167 100644 --- a/test/functional/ui/mouse_spec.lua +++ b/test/functional/ui/mouse_spec.lua @@ -40,10 +40,6 @@ describe('ui/mouse/input', function() ]]) end) - after_each(function() - screen:detach() - end) - it('single left click moves cursor', function() feed('<2,1>') screen:expect([[ diff --git a/test/functional/ui/multibyte_spec.lua b/test/functional/ui/multibyte_spec.lua index 3e63353ad2..8122cb08a3 100644 --- a/test/functional/ui/multibyte_spec.lua +++ b/test/functional/ui/multibyte_spec.lua @@ -21,10 +21,6 @@ describe("multibyte rendering", function() }) end) - after_each(function() - screen:detach() - end) - it("works with composed char at start of line", function() insert([[ ̊ @@ -131,10 +127,6 @@ describe('multibyte rendering: statusline', function() command('set laststatus=2') end) - after_each(function() - screen:detach() - end) - it('last char shows (multibyte)', function() command('set statusline=你好') screen:expect([[ diff --git a/test/functional/ui/multigrid_spec.lua b/test/functional/ui/multigrid_spec.lua index 30a5b63d89..01ffe80be3 100644 --- a/test/functional/ui/multigrid_spec.lua +++ b/test/functional/ui/multigrid_spec.lua @@ -37,10 +37,6 @@ describe('ext_multigrid', function() }) end) - after_each(function() - screen:detach() - end) - it('default initial screen', function() screen:expect{grid=[[ ## grid 1 diff --git a/test/functional/ui/options_spec.lua b/test/functional/ui/options_spec.lua index 93192934c7..ea71f5eae9 100644 --- a/test/functional/ui/options_spec.lua +++ b/test/functional/ui/options_spec.lua @@ -40,10 +40,6 @@ describe('ui receives option updates', function() return defaults end - after_each(function() - screen:detach() - end) - it("for defaults", function() local expected = reset() screen:expect(function() diff --git a/test/functional/ui/output_spec.lua b/test/functional/ui/output_spec.lua index 20413cb784..a201dfe898 100644 --- a/test/functional/ui/output_spec.lua +++ b/test/functional/ui/output_spec.lua @@ -31,7 +31,6 @@ describe("shell command :!", function() after_each(function() child_session.feed_data("\3") -- Ctrl-C - screen:detach() end) it("displays output without LF/EOF. #4646 #4569 #3772", function() diff --git a/test/functional/ui/sign_spec.lua b/test/functional/ui/sign_spec.lua index 68e675b8e5..0ed62b21b2 100644 --- a/test/functional/ui/sign_spec.lua +++ b/test/functional/ui/sign_spec.lua @@ -26,10 +26,6 @@ describe('Signs', function() } ) end) - after_each(function() - screen:detach() - end) - describe(':sign place', function() it('allows signs with combining characters', function() feed('iab') diff --git a/test/functional/ui/spell_spec.lua b/test/functional/ui/spell_spec.lua index 913f1b9bed..243b737583 100644 --- a/test/functional/ui/spell_spec.lua +++ b/test/functional/ui/spell_spec.lua @@ -20,10 +20,6 @@ describe("'spell'", function() }) end) - after_each(function() - screen:detach() - end) - it('joins long lines #7937', function() feed_command('set spell') insert([[ diff --git a/test/functional/ui/syntax_conceal_spec.lua b/test/functional/ui/syntax_conceal_spec.lua index 566d183f11..d1af0e955c 100644 --- a/test/functional/ui/syntax_conceal_spec.lua +++ b/test/functional/ui/syntax_conceal_spec.lua @@ -22,10 +22,6 @@ describe('Screen', function() } ) end) - after_each(function() - screen:detach() - end) - describe("match and conceal", function() before_each(function() diff --git a/test/functional/ui/tabline_spec.lua b/test/functional/ui/tabline_spec.lua index dcab9f7ef4..0ee7e03fac 100644 --- a/test/functional/ui/tabline_spec.lua +++ b/test/functional/ui/tabline_spec.lua @@ -17,10 +17,6 @@ describe('ui/ext_tabline', function() end) end) - after_each(function() - screen:detach() - end) - it('publishes UI events', function() command("tabedit another-tab") From cc0d7252304f10ed6cdc0bc58789100093f7d021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sun, 13 Oct 2019 09:19:57 +0200 Subject: [PATCH 0168/1293] tests/ui: completely delete "attr_ignore" feature All existing usages are ad-hoc/random/lazyness. Generating attribute specifications is not hard since four years, just do it always. --- test/functional/ui/screen.lua | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/test/functional/ui/screen.lua b/test/functional/ui/screen.lua index b15e2980de..8bc1e14e13 100644 --- a/test/functional/ui/screen.lua +++ b/test/functional/ui/screen.lua @@ -259,7 +259,7 @@ local ext_keys = { -- Asserts that the screen state eventually matches an expected state. -- -- Can be called with positional args: --- screen:expect(grid, [attr_ids, attr_ignore]) +-- screen:expect(grid, [attr_ids]) -- screen:expect(condition) -- or keyword args (supports more options): -- screen:expect{grid=[[...]], cmdline={...}, condition=function() ... end} @@ -276,8 +276,6 @@ local ext_keys = { -- attributes in the final state are an error. -- Use screen:set_default_attr_ids() to define attributes for many -- expect() calls. --- attr_ignore: Ignored text attributes, or `true` to ignore all. By default --- nothing is ignored. -- condition: Function asserting some arbitrary condition. Return value is -- ignored, throw an error (use eq() or similar) to signal failure. -- any: Lua pattern string expected to match a screen line. NB: the @@ -312,13 +310,13 @@ local ext_keys = { -- cmdline_block: Expected ext_cmdline block (for function definitions) -- wildmenu_items: Expected items for ext_wildmenu -- wildmenu_pos: Expected position for ext_wildmenu -function Screen:expect(expected, attr_ids, attr_ignore, ...) +function Screen:expect(expected, attr_ids, ...) local grid, condition = nil, nil local expected_rows = {} assert(next({...}) == nil, "invalid args to expect()") if type(expected) == "table" then - assert(not (attr_ids ~= nil or attr_ignore ~= nil)) - local is_key = {grid=true, attr_ids=true, attr_ignore=true, condition=true, + assert(not (attr_ids ~= nil)) + local is_key = {grid=true, attr_ids=true, condition=true, any=true, mode=true, unchanged=true, intermediate=true, reset=true, timeout=true, request_cb=true, hl_groups=true} for _, v in ipairs(ext_keys) do @@ -331,14 +329,13 @@ function Screen:expect(expected, attr_ids, attr_ignore, ...) end grid = expected.grid attr_ids = expected.attr_ids - attr_ignore = expected.attr_ignore condition = expected.condition assert(not (expected.any ~= nil and grid ~= nil)) elseif type(expected) == "string" then grid = expected expected = {} elseif type(expected) == "function" then - assert(not (attr_ids ~= nil or attr_ignore ~= nil)) + assert(not (attr_ids ~= nil)) condition = expected expected = {} else @@ -355,7 +352,6 @@ function Screen:expect(expected, attr_ids, attr_ignore, ...) end local attr_state = { ids = attr_ids or self._default_attr_ids, - ignore = attr_ignore } if self._options.ext_linegrid then attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids or {}) @@ -1472,8 +1468,6 @@ function Screen:_get_attr_id(attr_state, attrs, hl_id) return nil elseif id ~= nil then return id - elseif attr_state.ignore == true then - return nil end if attr_state.mutable then id = self:_insert_hl_id(attr_state, hl_id) @@ -1482,9 +1476,7 @@ function Screen:_get_attr_id(attr_state, attrs, hl_id) end return "UNEXPECTED "..self:_pprint_attrs(self._attr_table[hl_id][1]) else - if self:_equal_attrs(attrs, {}) or - attr_state.ignore == true or - self:_attr_index(attr_state.ignore, attrs) ~= nil then + if self:_equal_attrs(attrs, {}) then -- ignore this attrs return nil end From 5cf6beb22178a844bc1b6e75e744eb04508b865c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 13 Oct 2019 23:09:09 +0200 Subject: [PATCH 0169/1293] scripts/vim-patch.sh -l: display commit subjects Closes https://github.com/neovim/neovim/pull/11182. --- scripts/vim-patch.sh | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/scripts/vim-patch.sh b/scripts/vim-patch.sh index 2cc32f0dd0..4efe509008 100755 --- a/scripts/vim-patch.sh +++ b/scripts/vim-patch.sh @@ -366,7 +366,7 @@ submit_pr() { # Gets all Vim commits since the "start" commit. list_vim_commits() { ( - cd "${VIM_SOURCE_DIR}" && git log --reverse --format='%H' v8.0.0000..HEAD "$@" + cd "${VIM_SOURCE_DIR}" && git log --reverse v8.0.0000..HEAD "$@" ) } # Prints all (sorted) "vim-patch:xxx" tokens found in the Nvim git log. @@ -389,6 +389,7 @@ list_vimpatch_numbers() { } # Prints a newline-delimited list of Vim commits, for use by scripts. +# "$1": use extended format? # "$@" is passed to list_vim_commits, as extra arguments to git-log. list_missing_vimpatches() { local token vim_commit vim_tag patch_number @@ -396,6 +397,13 @@ list_missing_vimpatches() { declare -A vim_commit_tags declare -a git_log_args + local extended_format=$1; shift + if [[ "$extended_format" == 1 ]]; then + git_log_args=("--format=%H %s") + else + git_log_args=("--format=%H") + fi + # Massage arguments for git-log. declare -A git_log_replacements=( [^\(.*/\)?src/nvim/\(.*\)]="\${BASH_REMATCH[1]}src/\${BASH_REMATCH[2]}" @@ -431,13 +439,27 @@ list_missing_vimpatches() { # Get missing Vim commits set +u # Avoid "unbound variable" with bash < 4.4 below. - for vim_commit in $(list_vim_commits "${git_log_args[@]}"); do + local vim_commit info + while IFS=' ' read -r line; do # Check for vim-patch: (usually runtime updates). - token="vim-patch:${vim_commit:0:7}" + token="vim-patch:${line:0:7}" if [[ "${tokens[$token]-}" ]]; then continue fi + # Get commit hash, and optional info from line. This is used in + # extended mode, and when using e.g. '--format' manually. + vim_commit=${line%% *} + if [[ "$vim_commit" == "$line" ]]; then + info= + else + info=${line#* } + if [[ -n $info ]]; then + # Remove any "patch 8.0.0902: " prefixes, and prefix with ": ". + info=": ${info#patch*: }" + fi + fi + vim_tag="${vim_commit_tags[$vim_commit]-}" if [[ -n "$vim_tag" ]]; then # Check for vim-patch: (not commit hash). @@ -445,11 +467,11 @@ list_missing_vimpatches() { if [[ "${tokens[$patch_number]-}" ]]; then continue fi - echo "$vim_tag" + printf '%s%s\n' "$vim_tag" "$info" else - echo "$vim_commit" + printf '%s%s\n' "$vim_commit" "$info" fi - done + done < <(list_vim_commits "${git_log_args[@]}") set -u } @@ -464,7 +486,7 @@ show_vimpatches() { runtime_commits[$commit]=1 done - list_missing_vimpatches "$@" | while read -r vim_commit; do + list_missing_vimpatches 1 "$@" | while read -r vim_commit; do if [[ "${runtime_commits[$vim_commit]-}" ]]; then printf ' • %s (+runtime)\n' "${vim_commit}" else @@ -596,7 +618,7 @@ while getopts "hlLMVp:P:g:r:s" opt; do ;; L) shift # remove opt - list_missing_vimpatches "$@" + list_missing_vimpatches 0 "$@" exit 0 ;; M) From 8add4cb8fd39b0f6462f0dc064c7534d66326faf Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 14 Oct 2019 14:44:18 +0900 Subject: [PATCH 0170/1293] vim-patch 8.1.0361: remote user not used for completion Problem: Remote user not used for completion. (Stucki) Solution: Use $USER too. (Dominique Pelle, closes #3407) https://github.com/vim/vim/commit/6b0b83f768cf536b34ce4d3f2de6bf62324229aa --- src/nvim/os/users.c | 62 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/src/nvim/os/users.c b/src/nvim/os/users.c index b24232b680..9374693550 100644 --- a/src/nvim/os/users.c +++ b/src/nvim/os/users.c @@ -17,6 +17,22 @@ # include #endif +// Add a user name to the list of users in garray_T *users. +// Do nothing if user name is NULL or empty. +static void add_user(garray_T *users, char *user, bool need_copy) +{ + char *user_copy = (user != NULL && need_copy) + ? xstrdup(user) : user; + + if (user_copy == NULL || *user_copy == NUL) { + if (need_copy) { + xfree(user); + } + return; + } + GA_APPEND(char *, users, user_copy); +} + // Initialize users garray and fill it with os usernames. // Return Ok for success, FAIL for failure. int os_get_usernames(garray_T *users) @@ -27,16 +43,15 @@ int os_get_usernames(garray_T *users) ga_init(users, sizeof(char *), 20); # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) - struct passwd *pw; + { + struct passwd *pw; - setpwent(); - while ((pw = getpwent()) != NULL) { - // pw->pw_name shouldn't be NULL but just in case... - if (pw->pw_name != NULL) { - GA_APPEND(char *, users, xstrdup(pw->pw_name)); + setpwent(); + while ((pw = getpwent()) != NULL) { + add_user(users, pw->pw_name, true); } + endpwent(); } - endpwent(); # elif defined(WIN32) { DWORD nusers = 0, ntotal = 0, i; @@ -51,13 +66,44 @@ int os_get_usernames(garray_T *users) EMSG2("utf16_to_utf8 failed: %d", conversion_result); break; } - GA_APPEND(char *, users, user); + add_user(users, user, false); } NetApiBufferFree(uinfo); } } # endif +# if defined(HAVE_GETPWNAM) + { + const char *user_env = os_getenv("USER"); + + // The $USER environment variable may be a valid remote user name (NIS, + // LDAP) not already listed by getpwent(), as getpwent() only lists + // local user names. If $USER is not already listed, check whether it + // is a valid remote user name using getpwnam() and if it is, add it to + // the list of user names. + + if (user_env != NULL && *user_env != NUL) { + int i; + + for (i = 0; i < users->ga_len; i++) { + char *local_user = ((char **)users->ga_data)[i]; + + if (STRCMP(local_user, user_env) == 0) { + break; + } + } + + if (i == users->ga_len) { + struct passwd *pw = getpwnam(user_env); // NOLINT + + if (pw != NULL) { + add_user(users, pw->pw_name, true); + } + } + } + } +# endif return OK; } From d0efc1c9062441c9addc846429794ad4a06cc130 Mon Sep 17 00:00:00 2001 From: dm1try Date: Mon, 14 Oct 2019 14:58:41 +0300 Subject: [PATCH 0171/1293] mac: fix "tags file not sorted" bug on Catalina (#11222) I/O in Catalina is currently known to be broken. This commit works around a pesky bug and also makes the code more consistent by removing the mix of C file and standard I/O. Fixes https://github.com/neovim/neovim/issues/11196 --- src/nvim/tag.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 6fe3efbaae..1f70a10f28 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -1508,11 +1508,11 @@ line_read_in: * compute the first offset. */ if (state == TS_BINARY) { - // Get the tag file size. - if ((filesize = vim_lseek(fileno(fp), (off_T)0L, SEEK_END)) <= 0) { + if (vim_fseek(fp, 0, SEEK_END) != 0) { state = TS_LINEAR; } else { - vim_lseek(fileno(fp), (off_T)0L, SEEK_SET); + filesize = vim_ftell(fp); + vim_fseek(fp, 0, SEEK_SET); /* Calculate the first read offset in the file. Start * the search in the middle of the file. */ From 2e14dffbb49666bc79c2aa4fd9cea23d7f2f058c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 14 Oct 2019 15:16:58 +0200 Subject: [PATCH 0172/1293] deps: pass LDFLAGS+=-static (#11138) This is required when `build_old_libs=no` is used in `libtool`. Fixes https://github.com/neovim/neovim/issues/11198 --- third-party/cmake/BuildLibtermkey.cmake | 1 + third-party/cmake/BuildLibvterm.cmake | 1 + third-party/cmake/BuildUnibilium.cmake | 1 + 3 files changed, 3 insertions(+) diff --git a/third-party/cmake/BuildLibtermkey.cmake b/third-party/cmake/BuildLibtermkey.cmake index b2332ed65a..10e98fbab3 100644 --- a/third-party/cmake/BuildLibtermkey.cmake +++ b/third-party/cmake/BuildLibtermkey.cmake @@ -48,6 +48,7 @@ ExternalProject_Add(libtermkey PREFIX=${DEPS_INSTALL_DIR} PKG_CONFIG_PATH=${DEPS_LIB_DIR}/pkgconfig CFLAGS=-fPIC + LDFLAGS+=-static ${DEFAULT_MAKE_CFLAGS} install) endif() diff --git a/third-party/cmake/BuildLibvterm.cmake b/third-party/cmake/BuildLibvterm.cmake index e4649986af..61c1c90fa6 100644 --- a/third-party/cmake/BuildLibvterm.cmake +++ b/third-party/cmake/BuildLibvterm.cmake @@ -58,6 +58,7 @@ else() set(LIBVTERM_INSTALL_COMMAND ${MAKE_PRG} CC=${DEPS_C_COMPILER} PREFIX=${DEPS_INSTALL_DIR} CFLAGS=-fPIC + LDFLAGS+=-static ${DEFAULT_MAKE_CFLAGS} install) endif() diff --git a/third-party/cmake/BuildUnibilium.cmake b/third-party/cmake/BuildUnibilium.cmake index e9deeb4987..74c1cbddb0 100644 --- a/third-party/cmake/BuildUnibilium.cmake +++ b/third-party/cmake/BuildUnibilium.cmake @@ -40,6 +40,7 @@ else() BUILD_COMMAND ${MAKE_PRG} CC=${DEPS_C_COMPILER} PREFIX=${DEPS_INSTALL_DIR} CFLAGS=-fPIC + LDFLAGS+=-static INSTALL_COMMAND ${MAKE_PRG} PREFIX=${DEPS_INSTALL_DIR} install) endif() From 932edf4f338c4d31f94fdaaa125d3ff8c2e6fe08 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 15 Oct 2019 20:50:51 +0200 Subject: [PATCH 0173/1293] tests: tui_spec: fix waiting for terminal to be ready (#11232) The screen would have '-- TERMINAL --' already initially. Related to flakiness of "TUI FocusGained/FocusLost in terminal-mode". --- test/functional/terminal/tui_spec.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index 326a578bb7..4f5cfa930a 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -924,7 +924,15 @@ describe('TUI FocusGained/FocusLost', function() feed_data(':terminal\n') -- Wait for terminal to be ready. - screen:expect{any='-- TERMINAL --'} + screen:expect{grid=[[ + {1:r}eady $ | + [Process exited 0] | + | + | + | + :terminal | + {3:-- TERMINAL --} | + ]]} feed_data('\027[I') screen:expect{grid=[[ From c5c06665ed302e774dc1a01bed949815b4b7c8c3 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 16 Oct 2019 15:44:38 +0200 Subject: [PATCH 0174/1293] scripts/vim-patch.sh: lazily update Vim source (#11207) This gets done only initially, for `-l`, and when a commit cannot be found. Also provide more compact instructions with `-l` / `show_vimpatches`. --- scripts/vim-patch.sh | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/scripts/vim-patch.sh b/scripts/vim-patch.sh index 4efe509008..2a04805606 100755 --- a/scripts/vim-patch.sh +++ b/scripts/vim-patch.sh @@ -89,7 +89,7 @@ get_vim_sources() { echo "Cloning Vim into: ${VIM_SOURCE_DIR}" git clone https://github.com/vim/vim.git "${VIM_SOURCE_DIR}" cd "${VIM_SOURCE_DIR}" - else + elif [[ "${1-}" == update ]]; then cd "${VIM_SOURCE_DIR}" if ! [ -d ".git" ] \ && ! [ "$(git rev-parse --show-toplevel)" = "${VIM_SOURCE_DIR}" ]; then @@ -103,6 +103,8 @@ get_vim_sources() { else msg_err "Could not update Vim sources; ignoring error." fi + else + cd "${VIM_SOURCE_DIR}" fi } @@ -124,7 +126,7 @@ find_git_remote() { } # Assign variables for a given Vim tag, patch version, or commit. -# Might exit in case it cannot be found. +# Might exit in case it cannot be found, after updating Vim sources. assign_commit_details() { local vim_commit_ref if [[ ${1} =~ v?[0-9]\.[0-9]\.[0-9]{3,4} ]]; then @@ -146,9 +148,14 @@ assign_commit_details() { local munge_commit_line=false fi - vim_commit=$(git -C "${VIM_SOURCE_DIR}" log -1 --format="%H" "${vim_commit_ref}" --) || { - >&2 msg_err "Couldn't find Vim revision '${vim_commit_ref}'." - exit 3 + local get_vim_commit_cmd="git -C ${VIM_SOURCE_DIR} log -1 --format=%H ${vim_commit_ref} --" + vim_commit=$($get_vim_commit_cmd 2>&1) || { + # Update Vim sources. + get_vim_sources update + vim_commit=$($get_vim_commit_cmd 2>&1) || { + >&2 msg_err "Couldn't find Vim revision '${vim_commit_ref}': git error: ${vim_commit}." + exit 3 + } } vim_commit_url="https://github.com/vim/vim/commit/${vim_commit}" @@ -478,8 +485,8 @@ list_missing_vimpatches() { # Prints a human-formatted list of Vim commits, with instructional messages. # Passes "$@" onto list_missing_vimpatches (args for git-log). show_vimpatches() { - get_vim_sources - printf "\nVim patches missing from Neovim:\n" + get_vim_sources update + printf "Vim patches missing from Neovim:\n" local -A runtime_commits for commit in $(git -C "${VIM_SOURCE_DIR}" log --format="%H %D" -- runtime | sed 's/,\? tag: / /g'); do @@ -494,17 +501,12 @@ show_vimpatches() { fi done - printf "Instructions: - - To port one of the above patches to Neovim, execute - this script with the patch revision as argument and - follow the instructions. - - Examples: '%s -p 7.4.487' - '%s -p 1e8ebf870720e7b671f98f22d653009826304c4f' + printf "\nInstructions: + To port one of the above patches to Neovim, execute this script with the patch revision as argument and follow the instructions, e.g. + '%s -p v8.0.1234', or '%s -P v8.0.1234' NOTE: Please port the _oldest_ patch if you possibly can. - Out-of-order patches increase the possibility of bugs. + You can use '%s -l path/to/file' to see what patches are missing for a file. " "${BASENAME}" "${BASENAME}" } @@ -646,7 +648,7 @@ while getopts "hlLMVp:P:g:r:s" opt; do exit 0 ;; V) - get_vim_sources + get_vim_sources update exit 0 ;; *) From 913d01bb03616f3bb7468490573a1579d62debbe Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 17 Oct 2019 09:44:10 +0200 Subject: [PATCH 0175/1293] vim-patch:8.1.2096: too many #ifdefs #11229 Problem: Too many #ifdefs. Solution: Graduate FEAT_COMMENTS. https://github.com/vim/vim/commit/8c96af9c05bfcac2d5ae081e098d4863db561511 Fixes https://github.com/vim/vim/issues/4972. --- src/nvim/edit.c | 43 ++++++++++++++++++++++--------------------- src/nvim/misc1.c | 2 -- src/nvim/ops.c | 40 ++++++++++++++++++++-------------------- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 16c4882975..49cf090962 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -5479,10 +5479,10 @@ insertchar ( if (c == NUL) /* only formatting was wanted */ return; - /* Check whether this character should end a comment. */ + // Check whether this character should end a comment. if (did_ai && c == end_comment_pending) { char_u *line; - char_u lead_end[COM_MAX_LEN]; /* end-comment string */ + char_u lead_end[COM_MAX_LEN]; // end-comment string int middle_len, end_len; int i; @@ -5490,39 +5490,40 @@ insertchar ( * Need to remove existing (middle) comment leader and insert end * comment leader. First, check what comment leader we can find. */ - i = get_leader_len(line = get_cursor_line_ptr(), &p, FALSE, TRUE); - if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) { /* Just checking */ - /* Skip middle-comment string */ - while (*p && p[-1] != ':') /* find end of middle flags */ - ++p; + i = get_leader_len(line = get_cursor_line_ptr(), &p, false, true); + if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) { // Just checking + // Skip middle-comment string + while (*p && p[-1] != ':') { // find end of middle flags + p++; + } middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); - /* Don't count trailing white space for middle_len */ - while (middle_len > 0 && ascii_iswhite(lead_end[middle_len - 1])) - --middle_len; + // Don't count trailing white space for middle_len + while (middle_len > 0 && ascii_iswhite(lead_end[middle_len - 1])) { + middle_len--; + } - /* Find the end-comment string */ - while (*p && p[-1] != ':') /* find end of end flags */ - ++p; + // Find the end-comment string + while (*p && p[-1] != ':') { // find end of end flags + p++; + } end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); - /* Skip white space before the cursor */ + // Skip white space before the cursor i = curwin->w_cursor.col; while (--i >= 0 && ascii_iswhite(line[i])) ; i++; - /* Skip to before the middle leader */ + // Skip to before the middle leader i -= middle_len; - /* Check some expected things before we go on */ + // Check some expected things before we go on if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) { - /* Backspace over all the stuff we want to replace */ + // Backspace over all the stuff we want to replace backspace_until_column(i); - /* - * Insert the end-comment string, except for the last - * character, which will get inserted as normal later. - */ + // Insert the end-comment string, except for the last + // character, which will get inserted as normal later. ins_bytes_len(lead_end, end_len - 1); } } diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c index c1de7ab9a4..1db8a1fa11 100644 --- a/src/nvim/misc1.c +++ b/src/nvim/misc1.c @@ -792,8 +792,6 @@ int prompt_for_number(int *mouse_used) cmdline_row = msg_row - 1; } need_wait_return = false; - msg_didany = false; - msg_didout = false; } else { cmdline_row = save_cmdline_row; } diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 0d27365d2b..030782cbcc 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -270,20 +270,21 @@ void shift_line( int left, int round, int amount, - int call_changed_bytes /* call changed_bytes() */ + int call_changed_bytes // call changed_bytes() ) { int count; int i, j; int p_sw = get_sw_value(curbuf); - count = get_indent(); /* get current indent */ + count = get_indent(); // get current indent - if (round) { /* round off indent */ - i = count / p_sw; /* number of p_sw rounded down */ - j = count % p_sw; /* extra spaces */ - if (j && left) /* first remove extra spaces */ - --amount; + if (round) { // round off indent + i = count / p_sw; // number of p_sw rounded down + j = count % p_sw; // extra spaces + if (j && left) { // first remove extra spaces + amount--; + } if (left) { i -= amount; if (i < 0) @@ -291,7 +292,7 @@ void shift_line( } else i += amount; count = i * p_sw; - } else { /* original vi indent */ + } else { // original vi indent if (left) { count -= p_sw * amount; if (count < 0) @@ -300,11 +301,12 @@ void shift_line( count += p_sw * amount; } - /* Set new indent */ - if (State & VREPLACE_FLAG) - change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes); - else + // Set new indent + if (State & VREPLACE_FLAG) { + change_indent(INDENT_SET, count, false, NUL, call_changed_bytes); + } else { (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0); + } } /* @@ -4281,15 +4283,13 @@ int paragraph_start(linenr_T lnum) return TRUE; /* after empty line */ do_comments = has_format_option(FO_Q_COMS); - if (fmt_check_par(lnum - 1 - , &leader_len, &leader_flags, do_comments - )) - return TRUE; /* after non-paragraph line */ + if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments)) { + return true; // after non-paragraph line + } - if (fmt_check_par(lnum - , &next_leader_len, &next_leader_flags, do_comments - )) - return TRUE; /* "lnum" is not a paragraph line */ + if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments)) { + return true; // "lnum" is not a paragraph line + } if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) return TRUE; /* missing trailing space in previous line. */ From 0785f8e8b11b2fa290cfbc0604d570f49b954ba6 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Thu, 17 Oct 2019 04:24:23 -0400 Subject: [PATCH 0176/1293] vim-patch:8.1.2140: "gk" and "gj" do not work correctly in number column #11208 Problem: "gk" and "gj" do not work correctly in number column. Solution: Allow for a negative "curswant". (Zach Wegner, closes vim/vim#4969) https://github.com/vim/vim/commit/ceba3dd5187788e09f65bd41b07b40f6f9aab953 --- src/nvim/cursor.c | 11 +++++++---- src/nvim/normal.c | 19 ++++++++++++++----- src/nvim/testdir/test_normal.vim | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index f2b3cfe690..036ae32589 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -93,11 +93,12 @@ int coladvance(colnr_T wcol) static int coladvance2( pos_T *pos, - bool addspaces, /* change the text to achieve our goal? */ - bool finetune, /* change char offset for the exact column */ - colnr_T wcol /* column to move to */ + bool addspaces, // change the text to achieve our goal? + bool finetune, // change char offset for the exact column + colnr_T wcol_arg // column to move to (can be negative) ) { + colnr_T wcol = wcol_arg; int idx; char_u *ptr; char_u *line; @@ -165,6 +166,7 @@ static int coladvance2( if (virtual_active() && addspaces + && wcol >= 0 && ((col != wcol && col != wcol + 1) || csize > 1)) { /* 'virtualedit' is set: The difference between wcol and col is * filled with spaces. */ @@ -244,8 +246,9 @@ static int coladvance2( mark_mb_adjustpos(curbuf, pos); } - if (col < wcol) + if (wcol < 0 || col < wcol) { return FAIL; + } return OK; } diff --git a/src/nvim/normal.c b/src/nvim/normal.c index e0dc9d4f23..d051ba33b8 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -3925,15 +3925,17 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1; else n = width1; - if (curwin->w_curswant > (colnr_T)n + 1) - curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1) - * width2; + if (curwin->w_curswant >= n) { + curwin->w_curswant = n - 1; + } } while (dist--) { if (dir == BACKWARD) { - if (curwin->w_curswant > width2) { - // move back within line + if (curwin->w_curswant >= width1) { + // Move back within the line. This can give a negative value + // for w_curswant if width1 < width2 (with cpoptions+=n), + // which will get clipped to column 0. curwin->w_curswant -= width2; } else { // to previous line @@ -3973,6 +3975,13 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) } curwin->w_cursor.lnum++; curwin->w_curswant %= width2; + // Check if the cursor has moved below the number display + // when width1 < width2 (with cpoptions+=n). Subtract width2 + // to get a negative value for w_curswant, which will get + // clipped to column 0. + if (curwin->w_curswant >= width1) { + curwin->w_curswant -= width2; + } linelen = linetabsize(get_cursor_line_ptr()); } } diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index b967f84626..aeae6423d0 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -2813,4 +2813,29 @@ func Test_normal_gk() call assert_equal(95, virtcol('.')) bw! bw! + + " needs 80 column new window + new + vert 80new + set number + set numberwidth=10 + set cpoptions+=n + put =[repeat('0',90), repeat('1',90)] + norm! 075l + call assert_equal(76, col('.')) + norm! gk + call assert_equal(1, col('.')) + norm! gk + call assert_equal(76, col('.')) + norm! gk + call assert_equal(1, col('.')) + norm! gj + call assert_equal(76, col('.')) + norm! gj + call assert_equal(1, col('.')) + norm! gj + call assert_equal(76, col('.')) + bw! + bw! + set cpoptions& number& numberwidth& endfunc From 4bbad5481773ac2a273a41a9fe8035ca57e03cd8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 18 Oct 2019 04:46:30 +0200 Subject: [PATCH 0177/1293] tests: fix non-controversial misuse of `pending` (#11247) Ref: https://github.com/neovim/neovim/pull/11184 --- test/functional/api/server_requests_spec.lua | 9 +++------ test/functional/core/job_spec.lua | 3 +-- test/functional/ex_cmds/write_spec.lua | 7 +++---- test/functional/legacy/delete_spec.lua | 2 +- test/functional/terminal/tui_spec.lua | 2 +- test/functional/ui/cmdline_spec.lua | 3 +-- test/functional/ui/embed_spec.lua | 3 +-- test/functional/ui/output_spec.lua | 6 ++---- 8 files changed, 13 insertions(+), 22 deletions(-) diff --git a/test/functional/api/server_requests_spec.lua b/test/functional/api/server_requests_spec.lua index 5a9ef7dd40..61184d2c59 100644 --- a/test/functional/api/server_requests_spec.lua +++ b/test/functional/api/server_requests_spec.lua @@ -317,8 +317,7 @@ describe('server -> client', function() set_session(server) local status, address = pcall(funcs.serverstart, "127.0.0.1:") if not status then - pending('no ipv4 stack', function() end) - return + pending('no ipv4 stack') end eq('127.0.0.1:', string.sub(address,1,10)) connect_test(server, 'tcp', address) @@ -329,8 +328,7 @@ describe('server -> client', function() set_session(server) local status, address = pcall(funcs.serverstart, '::1:') if not status then - pending('no ipv6 stack', function() end) - return + pending('no ipv6 stack') end eq('::1:', string.sub(address,1,4)) connect_test(server, 'tcp', address) @@ -349,8 +347,7 @@ describe('server -> client', function() it('does not deadlock', function() if not helpers.isCI('travis') and helpers.is_os('mac') then -- It does, in fact, deadlock on QuickBuild. #6851 - pending("deadlocks on QuickBuild", function() end) - return + pending("deadlocks on QuickBuild") end local address = funcs.serverlist()[1] local first = string.sub(address,1,1) diff --git a/test/functional/core/job_spec.lua b/test/functional/core/job_spec.lua index d4ce690867..d285008a33 100644 --- a/test/functional/core/job_spec.lua +++ b/test/functional/core/job_spec.lua @@ -202,8 +202,7 @@ describe('jobs', function() if helpers.isCI('travis') and os.getenv('CC') == 'gcc-4.9' and helpers.is_os('mac') then -- XXX: Hangs Travis macOS since e9061117a5b8f195c3f26a5cb94e18ddd7752d86. - pending("[Hangs on Travis macOS. #5002]", function() end) - return + pending("[Hangs on Travis macOS. #5002]") end nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") diff --git a/test/functional/ex_cmds/write_spec.lua b/test/functional/ex_cmds/write_spec.lua index 3f54ff6f41..4f526ddfca 100644 --- a/test/functional/ex_cmds/write_spec.lua +++ b/test/functional/ex_cmds/write_spec.lua @@ -41,7 +41,7 @@ describe(':write', function() command("silent !ln -s test_bkc_file.txt test_bkc_link.txt") end if eval('v:shell_error') ~= 0 then - pending('Cannot create symlink', function()end) + pending('Cannot create symlink') end source([[ edit test_bkc_link.txt @@ -61,7 +61,7 @@ describe(':write', function() command("silent !ln -s test_bkc_file.txt test_bkc_link.txt") end if eval('v:shell_error') ~= 0 then - pending('Cannot create symlink', function()end) + pending('Cannot create symlink') end source([[ edit test_bkc_link.txt @@ -75,8 +75,7 @@ describe(':write', function() it("appends FIFO file", function() -- mkfifo creates read-only .lnk files on Windows if iswin() or eval("executable('mkfifo')") == 0 then - pending('missing "mkfifo" command', function()end) - return + pending('missing "mkfifo" command') end local text = "some fifo text from write_spec" diff --git a/test/functional/legacy/delete_spec.lua b/test/functional/legacy/delete_spec.lua index 9ea3269828..f2ced8942d 100644 --- a/test/functional/legacy/delete_spec.lua +++ b/test/functional/legacy/delete_spec.lua @@ -56,7 +56,7 @@ describe('Test for delete()', function() endif ]]) if eval('v:shell_error') ~= 0 then - pending('Cannot create symlink', function()end) + pending('Cannot create symlink') end -- Delete the link, not the file eq(0, eval("delete('Xlink')")) diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index 4f5cfa930a..bc83660c19 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -1287,7 +1287,7 @@ describe("TUI 'term' option", function() elseif is_macos then local status, _ = pcall(assert_term, "xterm", "xterm") if not status then - pending("macOS: unibilium could not find terminfo", function() end) + pending("macOS: unibilium could not find terminfo") end else assert_term("xterm", "xterm") diff --git a/test/functional/ui/cmdline_spec.lua b/test/functional/ui/cmdline_spec.lua index fe1b2c13d1..c2354103c2 100644 --- a/test/functional/ui/cmdline_spec.lua +++ b/test/functional/ui/cmdline_spec.lua @@ -821,8 +821,7 @@ describe('cmdline redraw', function() it('with ', function() if 'openbsd' == helpers.uname() then - pending('FIXME #10804', function() end) - return + pending('FIXME #10804') end command('cmap a call sin(0)') -- no-op feed(':012345678901234567890123456789') diff --git a/test/functional/ui/embed_spec.lua b/test/functional/ui/embed_spec.lua index f3cd223f53..8218c8e12d 100644 --- a/test/functional/ui/embed_spec.lua +++ b/test/functional/ui/embed_spec.lua @@ -50,8 +50,7 @@ local function test_embed(ext_linegrid) it("doesn't erase output when setting color scheme", function() if 'openbsd' == helpers.uname() then - pending('FIXME #10804', function() end) - return + pending('FIXME #10804') end startup('--cmd', 'echoerr "foo"', '--cmd', 'color default', '--cmd', 'echoerr "bar"') screen:expect([[ diff --git a/test/functional/ui/output_spec.lua b/test/functional/ui/output_spec.lua index a201dfe898..9b1e803649 100644 --- a/test/functional/ui/output_spec.lua +++ b/test/functional/ui/output_spec.lua @@ -51,8 +51,7 @@ describe("shell command :!", function() it("throttles shell-command output greater than ~10KB", function() if 'openbsd' == helpers.uname() then - pending('FIXME #10804', function() end) - return + pending('FIXME #10804') end child_session.feed_data(":!"..nvim_dir.."/shell-test REP 30001 foo\n") @@ -96,8 +95,7 @@ describe("shell command :!", function() it('handles control codes', function() if iswin() then - pending('missing printf', function() end) - return + pending('missing printf') end local screen = Screen.new(50, 4) screen:attach() From 84aa86afb7c58f6a769ec9215fa69dec19619f5f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 18 Oct 2019 16:32:56 +0200 Subject: [PATCH 0178/1293] build: do not build test fixtures by default (#11230) - tty-test is also used on Windows - FUNCTIONALTEST_PREREQS: add printenv-test --- CMakeLists.txt | 5 +---- test/functional/fixtures/CMakeLists.txt | 10 +++++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 98a32a116b..08447d5ff9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -562,10 +562,7 @@ if(BUSTED_PRG) endif() set(UNITTEST_PREREQS nvim-test unittest-headers) - set(FUNCTIONALTEST_PREREQS nvim printargs-test shell-test streams-test ${GENERATED_HELP_TAGS}) - if(NOT WIN32) - list(APPEND FUNCTIONALTEST_PREREQS tty-test) - endif() + set(FUNCTIONALTEST_PREREQS nvim printenv-test printargs-test shell-test streams-test tty-test ${GENERATED_HELP_TAGS}) set(BENCHMARK_PREREQS nvim tty-test) # Useful for automated build systems, if they want to manually run the tests. diff --git a/test/functional/fixtures/CMakeLists.txt b/test/functional/fixtures/CMakeLists.txt index dbcb157956..270540de2e 100644 --- a/test/functional/fixtures/CMakeLists.txt +++ b/test/functional/fixtures/CMakeLists.txt @@ -1,12 +1,12 @@ -add_executable(tty-test tty-test.c) +add_executable(tty-test EXCLUDE_FROM_ALL tty-test.c) target_link_libraries(tty-test ${LIBUV_LIBRARIES}) -add_executable(shell-test shell-test.c) -add_executable(printargs-test printargs-test.c) -add_executable(printenv-test printenv-test.c) +add_executable(shell-test EXCLUDE_FROM_ALL shell-test.c) +add_executable(printargs-test EXCLUDE_FROM_ALL printargs-test.c) +add_executable(printenv-test EXCLUDE_FROM_ALL printenv-test.c) if(WIN32) set_target_properties(printenv-test PROPERTIES LINK_FLAGS -municode) endif() -add_executable(streams-test streams-test.c) +add_executable(streams-test EXCLUDE_FROM_ALL streams-test.c) target_link_libraries(streams-test ${LIBUV_LIBRARIES}) From 175ca82ca7baec7ce449d9e72f77a8f12d2e50dc Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 18 Oct 2019 20:41:24 +0200 Subject: [PATCH 0179/1293] tests: let_spec: enable "multibyte env var to child process" (#11233) --- test/functional/eval/let_spec.lua | 4 ---- test/functional/fixtures/printenv-test.c | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/test/functional/eval/let_spec.lua b/test/functional/eval/let_spec.lua index 63e18f943f..f8fcdfd41f 100644 --- a/test/functional/eval/let_spec.lua +++ b/test/functional/eval/let_spec.lua @@ -59,10 +59,6 @@ describe(':let', function() end) it("multibyte env var to child process #8398 #9267", function() - if (not helpers.iswin()) and helpers.isCI() then - -- Fails on non-Windows CI. Buffering/timing issue? - pending('fails on unix CI', function() end) - end local cmd_get_child_env = "let g:env_from_child = system(['"..nvim_dir.."/printenv-test', 'NVIM_TEST'])" command("let $NVIM_TEST = 'AìaB'") command(cmd_get_child_env) diff --git a/test/functional/fixtures/printenv-test.c b/test/functional/fixtures/printenv-test.c index 5ac076f653..0e68129543 100644 --- a/test/functional/fixtures/printenv-test.c +++ b/test/functional/fixtures/printenv-test.c @@ -44,7 +44,7 @@ int main(int argc, char **argv) utf8_len, NULL, NULL); - fprintf(stderr, "%s", utf8_value); + fprintf(stdout, "%s", utf8_value); free(utf8_value); #else char *value = getenv(argv[1]); @@ -52,8 +52,8 @@ int main(int argc, char **argv) fprintf(stderr, "env var not found: %s", argv[1]); return 1; } - // Print to stderr to avoid buffering. - fprintf(stderr, "%s", value); + fprintf(stdout, "%s", value); #endif + fflush(stdout); return 0; } From 0e0d4a7b4c0a38c83282013b732c83d82b1844e9 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 15 Oct 2019 20:35:47 -0400 Subject: [PATCH 0180/1293] vim-patch:8.1.2152: problems navigating tags file on MacOS Catalina Problem: Problems navigating tags file on MacOS Catalina. Solution: Use fseek instead of lseek. (John Lamb, fixes vim/vim#5061) https://github.com/vim/vim/commit/27fc8cab227e30f649f52e74efd58ad56d21e9bb --- src/nvim/tag.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 1f70a10f28..c8c9677a98 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -1094,7 +1094,6 @@ find_tags ( int low_char; // first char at low_offset int high_char; // first char at high_offset } search_info; - off_T filesize; int tagcmp; off_T offset; int round; @@ -1503,19 +1502,21 @@ line_read_in: state = TS_LINEAR; } - /* - * When starting a binary search, get the size of the file and - * compute the first offset. - */ + // When starting a binary search, get the size of the file and + // compute the first offset. if (state == TS_BINARY) { if (vim_fseek(fp, 0, SEEK_END) != 0) { + // can't seek, don't use binary search state = TS_LINEAR; } else { - filesize = vim_ftell(fp); + // Get the tag file size. + // Don't use lseek(), it doesn't work + // properly on MacOS Catalina. + const off_T filesize = vim_ftell(fp); vim_fseek(fp, 0, SEEK_SET); - /* Calculate the first read offset in the file. Start - * the search in the middle of the file. */ + // Calculate the first read offset in the file. Start + // the search in the middle of the file. search_info.low_offset = 0; search_info.low_char = 0; search_info.high_offset = filesize; From c54a7e586bc39ff5798b32641aefd320389a0303 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 16 Oct 2019 19:16:04 -0400 Subject: [PATCH 0181/1293] vim-patch:8.1.2161: mapping test fails Problem: Mapping test fails. Solution: Run the test separately. https://github.com/vim/vim/commit/4bd88d568a81d37df69dc3cf8cdd8d9dbb4011b7 --- src/nvim/testdir/test_alot.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/src/nvim/testdir/test_alot.vim b/src/nvim/testdir/test_alot.vim index 6bf2e8329c..2c52452f6b 100644 --- a/src/nvim/testdir/test_alot.vim +++ b/src/nvim/testdir/test_alot.vim @@ -27,7 +27,6 @@ source test_jumps.vim source test_fileformat.vim source test_filetype.vim source test_lambda.vim -source test_mapping.vim source test_menu.vim source test_messages.vim source test_modeline.vim From 7ba26ef3c01305334ccd84a78ef04d6f54e6b486 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 16 Oct 2019 19:11:03 -0400 Subject: [PATCH 0182/1293] vim-patch:8.1.2162: popup resize test is flaky Problem: Popup resize test is flaky. (Christian Brabandt) Solution: Add the function to the list of flaky tests. https://github.com/vim/vim/commit/4e03933726e3698d962bf7dacdd27f306a4c5086 --- src/nvim/testdir/runtest.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/testdir/runtest.vim b/src/nvim/testdir/runtest.vim index 8f5f3f82e7..49ec308db6 100644 --- a/src/nvim/testdir/runtest.vim +++ b/src/nvim/testdir/runtest.vim @@ -289,6 +289,7 @@ let s:flaky_tests = [ \ 'Test_oneshot()', \ 'Test_out_cb()', \ 'Test_paused()', + \ 'Test_popup_and_window_resize()', \ 'Test_quoteplus()', \ 'Test_quotestar()', \ 'Test_reltime()', From 6c6abd11f75052d8bbc1a3ff279aab61ed0bca58 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Mon, 14 Oct 2019 17:55:07 -0400 Subject: [PATCH 0183/1293] vim-patch:8.1.2151: state test is a bit flaky Problem: State test is a bit flaky. Solution: Add to the list of flaky tests. https://github.com/vim/vim/commit/3c8cd4a1dcbc34d8818a2a38b1d1e4755da9edc2 --- src/nvim/testdir/runtest.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nvim/testdir/runtest.vim b/src/nvim/testdir/runtest.vim index 49ec308db6..5c2e570adf 100644 --- a/src/nvim/testdir/runtest.vim +++ b/src/nvim/testdir/runtest.vim @@ -295,6 +295,7 @@ let s:flaky_tests = [ \ 'Test_reltime()', \ 'Test_repeat_many()', \ 'Test_repeat_three()', + \ 'Test_state()', \ 'Test_stop_all_in_callback()', \ 'Test_terminal_composing_unicode()', \ 'Test_terminal_redir_file()', From 1e4a9f9993a26a1495d1a3bdfd80fe079127ba83 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 18 Oct 2019 23:20:15 -0400 Subject: [PATCH 0184/1293] vim-patch:8.1.2175: meson files are not recognized Problem: Meson files are not recognized. Solution: Add the meson filetype. (Liam Beguin , Nirbheek Chauhan, closes vim/vim#5056) Also recognize hollywood. https://github.com/vim/vim/commit/c3bf7b56f2703e2d6f36dfb05fd32b5b43ce3c3f --- runtime/filetype.vim | 3 +++ src/nvim/testdir/test_filetype.vim | 2 ++ 2 files changed, 5 insertions(+) diff --git a/runtime/filetype.vim b/runtime/filetype.vim index 6abf9da55f..bbf9a91e2d 100644 --- a/runtime/filetype.vim +++ b/runtime/filetype.vim @@ -975,6 +975,9 @@ au BufNewFile,BufRead hg-editor-*.txt setf hgcommit " Mercurial config (looks like generic config file) au BufNewFile,BufRead *.hgrc,*hgrc setf cfg +" Meson Build system config +au BufNewFile,BufRead meson.build,meson_options.txt setf meson + " Messages (logs mostly) au BufNewFile,BufRead */log/{auth,cron,daemon,debug,kern,lpr,mail,messages,news/news,syslog,user}{,.log,.err,.info,.warn,.crit,.notice}{,.[0-9]*,-[0-9]*} setf messages diff --git a/src/nvim/testdir/test_filetype.vim b/src/nvim/testdir/test_filetype.vim index 7512d599b8..8e76046b13 100644 --- a/src/nvim/testdir/test_filetype.vim +++ b/src/nvim/testdir/test_filetype.vim @@ -201,6 +201,7 @@ let s:filename_checks = { \ 'hex': ['file.hex', 'file.h32'], \ 'hgcommit': ['hg-editor-file.txt'], \ 'hog': ['file.hog', 'snort.conf', 'vision.conf'], + \ 'hollywood': ['file.hws'], \ 'hostconf': ['/etc/host.conf'], \ 'hostsaccess': ['/etc/hosts.allow', '/etc/hosts.deny'], \ 'template': ['file.tmpl'], @@ -273,6 +274,7 @@ let s:filename_checks = { \ 'mason': ['file.mason', 'file.mhtml', 'file.comp'], \ 'master': ['file.mas', 'file.master'], \ 'mel': ['file.mel'], + \ 'meson': ['meson.build', 'meson_options.txt'], \ 'messages': ['/log/auth', '/log/cron', '/log/daemon', '/log/debug', '/log/kern', '/log/lpr', '/log/mail', '/log/messages', '/log/news/news', '/log/syslog', '/log/user', \ '/log/auth.log', '/log/cron.log', '/log/daemon.log', '/log/debug.log', '/log/kern.log', '/log/lpr.log', '/log/mail.log', '/log/messages.log', '/log/news/news.log', '/log/syslog.log', '/log/user.log', \ '/log/auth.err', '/log/cron.err', '/log/daemon.err', '/log/debug.err', '/log/kern.err', '/log/lpr.err', '/log/mail.err', '/log/messages.err', '/log/news/news.err', '/log/syslog.err', '/log/user.err', From 437fe261ab93e5b366fdcd095ccac7be1235b0eb Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 19 Oct 2019 11:55:23 -0400 Subject: [PATCH 0185/1293] vim-patch:8.1.2177: Dart files are not recognized Problem: Dart files are not recognized. Solution: Add a filetype rule. (Eugene Ciurana, closes vim/vim#5087) https://github.com/vim/vim/commit/afbdb905c37675851e79d21239f502cd8e4ced9e --- runtime/filetype.vim | 3 +++ src/nvim/testdir/test_filetype.vim | 1 + 2 files changed, 4 insertions(+) diff --git a/runtime/filetype.vim b/runtime/filetype.vim index bbf9a91e2d..8ce45b6a50 100644 --- a/runtime/filetype.vim +++ b/runtime/filetype.vim @@ -421,6 +421,9 @@ au BufNewFile,BufRead *.csp,*.fdr setf csp au BufNewFile,BufRead *.pld setf cupl au BufNewFile,BufRead *.si setf cuplsim +" Dart +au BufRead,BufNewfile *.dart,*.drt setf dart + " Debian Control au BufNewFile,BufRead */debian/control setf debcontrol au BufNewFile,BufRead control diff --git a/src/nvim/testdir/test_filetype.vim b/src/nvim/testdir/test_filetype.vim index 8e76046b13..4053746c82 100644 --- a/src/nvim/testdir/test_filetype.vim +++ b/src/nvim/testdir/test_filetype.vim @@ -122,6 +122,7 @@ let s:filename_checks = { \ 'cvs': ['cvs123'], \ 'cvsrc': ['.cvsrc'], \ 'cynpp': ['file.cyn'], + \ 'dart': ['file.dart', 'file.drt'], \ 'datascript': ['file.ds'], \ 'dcd': ['file.dcd'], \ 'debcontrol': ['/debian/control'], From d27fc0825732d575109ce7d149164e86d7b2cb98 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 19 Oct 2019 11:57:34 -0400 Subject: [PATCH 0186/1293] vim-patch:8.1.2178: accessing uninitialized memory in test Problem: Accessing uninitialized memory in test. Solution: Check if there was a match before using the match position. (Dominique Pelle, closes vim/vim#5088) https://github.com/vim/vim/commit/15ee567809a9808693163dd7c357ef0c172ecc9e --- src/nvim/search.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index 1f382d31c5..fb31e76986 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -4184,7 +4184,7 @@ static int is_one_char(char_u *pattern, bool move, pos_T *cur, nmatched = vim_regexec_multi(®match, curwin, curbuf, pos.lnum, regmatch.startpos[0].col, NULL, NULL); - if (!nmatched) { + if (nmatched != 0) { break; } } while (direction == FORWARD @@ -4196,7 +4196,10 @@ static int is_one_char(char_u *pattern, bool move, pos_T *cur, && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum && regmatch.startpos[0].col == regmatch.endpos[0].col); // one char width - if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col) { + if (!result + && nmatched != 0 + && inc(&pos) >= 0 + && pos.col == regmatch.endpos[0].col) { result = true; } } From 68b0873c458f4a0b5ca8483958994d338060202a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 19 Oct 2019 20:31:50 +0200 Subject: [PATCH 0187/1293] vim-patch:8.1.2182: test42 seen as binary by git diff #11256 Problem: Test42 seen as binary by git diff. Solution: Add .gitattributes file. Make explicit that 'cpo' does not contain 'S'. (Daniel Hahler, closes vim/vim#5072) https://github.com/vim/vim/commit/5b39d7adb0b9f02afe242f607d4c96250f06965d --- .gitattributes | 1 + src/nvim/testdir/test42.in | Bin 2438 -> 2373 bytes 2 files changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 15a5c58091..cb5934a2a1 100755 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ *.h linguist-language=C +src/nvim/testdir/test42.in diff diff --git a/src/nvim/testdir/test42.in b/src/nvim/testdir/test42.in index baa6e67d269af71f439100f002242b1eadc2a2cd..d9057e72fb91e86fc84ac5a7804a4c9f0ec56be9 100644 GIT binary patch delta 33 ocmZn@J}NZ9SxDQeIJHC}xgfuwBr`v+Slc#uqEqu`Nyc0b0L0b{SpWb4 delta 94 zcmX>q)FwQ^d16>ofVOS0LT0f-QEFjnW>IR2LQ-mSVrg-zLUKXALRn%?X(~uIB{MB8 twJ0^OL?NvxKUbkFGgl)xHB~{`(7@c-KvR#)syMX-q(j>_c;l5q4gfNDAY=dl From 3de4dc539ae938c5fdeddbdf25722fd1f6d9c77c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 19 Oct 2019 23:11:31 +0200 Subject: [PATCH 0188/1293] vim-patch:8.1.2180: Error E303 is not useful when 'directory' is empty (#11257) Problem: Error E303 is not useful when 'directory' is empty. Solution: Skip the error message. (Daniel Hahler, vim/vim#5067) https://github.com/vim/vim/commit/00e192becd50a38cb21a1bc3f86fcc7a21f8ee88 --- runtime/doc/message.txt | 3 ++- runtime/doc/options.txt | 2 +- src/nvim/memline.c | 6 +++--- src/nvim/testdir/test_recover.vim | 6 ++++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/runtime/doc/message.txt b/runtime/doc/message.txt index e8c76adad4..965b062728 100644 --- a/runtime/doc/message.txt +++ b/runtime/doc/message.txt @@ -556,7 +556,8 @@ allowed for the command that was used. Vim was not able to create a swap file. You can still edit the file, but if Vim unexpectedly exits the changes will be lost. And Vim may consume a lot of memory when editing a big file. You may want to change the 'directory' option -to avoid this error. See |swap-file|. +to avoid this error. This error is not given when 'directory' is empty. See +|swap-file|. *E140* > Use ! to write partial buffer diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 971c4ffbd5..ee4625a9e0 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -1978,7 +1978,7 @@ A jump table for the options with a short description can be found at |Q_op|. possible. If it is not possible in any directory, but last directory listed in the option does not exist, it is created. - Empty means that no swap file will be used (recovery is - impossible!). + impossible!) and no |E303| error will be given. - A directory "." means to put the swap file in the same directory as the edited file. On Unix, a dot is prepended to the file name, so it doesn't show in a directory listing. On MS-Windows the "hidden" diff --git a/src/nvim/memline.c b/src/nvim/memline.c index f1d6ee064c..b85c23e50f 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -523,9 +523,9 @@ void ml_open_file(buf_T *buf) } } - if (mfp->mf_fname == NULL) { /* Failed! */ - need_wait_return = TRUE; /* call wait_return later */ - ++no_wait_return; + if (*p_dir != NUL && mfp->mf_fname == NULL) { + need_wait_return = true; // call wait_return later + no_wait_return++; (void)EMSG2(_( "E303: Unable to open swap file for \"%s\", recovery impossible"), buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname); diff --git a/src/nvim/testdir/test_recover.vim b/src/nvim/testdir/test_recover.vim index 09c8d1cda6..fc073cacd2 100644 --- a/src/nvim/testdir/test_recover.vim +++ b/src/nvim/testdir/test_recover.vim @@ -14,6 +14,12 @@ func Test_recover_root_dir() set dir=/notexist/ endif call assert_fails('split Xtest', 'E303:') + + " No error with empty 'directory' setting. + set directory= + split XtestOK + close! + set dir& endfunc From d89ec55c45e73544c614a3436ae16b9ea17b5535 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 19 Oct 2019 23:15:07 +0200 Subject: [PATCH 0189/1293] test/functional: retry/Screen: failure instead of error #11173 - Running out of retries, or unexpected screen state should make the test FAIL, not ERROR. - Uses levels to report the location of the caller. - Improve message with retry-failure (formatting). Before: [ RUN ] test: 103.53 ms ERR test/functional/helpers.lua:388: retry() attempts: 1 test/functional/ui/screen.lua:587: Row 1 did not match. Expected: |*X^ | |{0:~ }| |{0:~ }| | | Actual: |*^ | |{0:~ }| |{0:~ }| | | To print the expect() call that would assert the current screen state, use screen:snapshot_util(). In case of non-deterministic failures, use screen:redraw_debug() to show all intermediate screen states. stack traceback: test/functional/helpers.lua:388: in function 'retry' test/functional/test_spec.lua:24: in function After: [ RUN ] test: 105.22 ms FAIL test/functional/test_spec.lua:24: stopping after 1 retry() attempts. test/functional/test_spec.lua:25: Row 1 did not match. Expected: |*X^ | |{0:~ }| |{0:~ }| | | Actual: |*^ | |{0:~ }| |{0:~ }| | | To print the expect() call that would assert the current screen state, use screen:snapshot_util(). In case of non-deterministic failures, use screen:redraw_debug() to show all intermediate screen states. stack traceback: test/functional/helpers.lua:389: in function 'retry' test/functional/test_spec.lua:24: in function --- test/functional/helpers.lua | 3 ++- test/functional/ui/screen.lua | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index 2473fc0d3b..a6ff10c0a4 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -1,4 +1,5 @@ require('coxpcall') +local busted = require('busted') local luv = require('luv') local lfs = require('lfs') local mpack = require('mpack') @@ -385,7 +386,7 @@ function module.retry(max, max_ms, fn) end luv.update_time() -- Update cached value of luv.now() (libuv: uv_now()). if (max and tries >= max) or (luv.now() - start_time > timeout) then - error("\nretry() attempts: "..tostring(tries).."\n"..tostring(result)) + busted.fail(string.format("retry() attempts: %d\n%s", tries, tostring(result)), 2) end tries = tries + 1 luv.sleep(20) -- Avoid hot loop... diff --git a/test/functional/ui/screen.lua b/test/functional/ui/screen.lua index 8bc1e14e13..b57e13fea1 100644 --- a/test/functional/ui/screen.lua +++ b/test/functional/ui/screen.lua @@ -71,6 +71,7 @@ -- To debug screen tests, see Screen:redraw_debug(). local helpers = require('test.functional.helpers')(nil) +local busted = require('busted') local deepcopy = helpers.deepcopy local shallowcopy = helpers.shallowcopy local concat_tables = helpers.concat_tables @@ -574,7 +575,7 @@ asynchronous (feed(), nvim_input()) and synchronous API calls. if err then - assert(false, err) + busted.fail(err, 3) elseif did_warn then local tb = debug.traceback() local index = string.find(tb, '\n%s*%[C]') From 93fe30593b47fe98a31c6bb67f4d6effb8b725fe Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 19 Oct 2019 23:45:27 +0200 Subject: [PATCH 0190/1293] ex_echo: fix check for got_int #11225 It needs to return to not output any remaining parts. Followup to https://github.com/neovim/neovim/pull/10926 Ref: https://github.com/neovim/neovim/issues/10923 --- src/nvim/message.c | 5 ++++- test/functional/ui/messages_spec.lua | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/nvim/message.c b/src/nvim/message.c index b518664f32..03cbe8ec18 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -233,7 +233,10 @@ void msg_multiline_attr(const char *s, int attr, bool check_int) { const char *next_spec = s; - while (next_spec != NULL && (!check_int || !got_int)) { + while (next_spec != NULL) { + if (check_int && got_int) { + return; + } next_spec = strpbrk(s, "\t\n\r"); if (next_spec != NULL) { diff --git a/test/functional/ui/messages_spec.lua b/test/functional/ui/messages_spec.lua index 377d49c036..d16559bab2 100644 --- a/test/functional/ui/messages_spec.lua +++ b/test/functional/ui/messages_spec.lua @@ -1125,7 +1125,7 @@ aliquip ex ea commodo consequat.]]) it('can be quit', function() screen:try_resize(25,5) - feed(':echon join(map(range(0, &lines*2), "v:val"), "\\n")') + feed(':echon join(map(range(0, &lines*10), "v:val"), "\\n")') screen:expect{grid=[[ 0 | 1 | From 019c8d13dd7056725c0715dc15e451118b767b7d Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 19 Oct 2019 18:04:08 -0700 Subject: [PATCH 0191/1293] build/doc/CI: remove/update quickbuild references #11258 --- CMakeLists.txt | 4 ++-- CONTRIBUTING.md | 25 ++++++++++++-------- runtime/doc/if_lua.txt | 4 ++-- runtime/doc/intro.txt | 10 ++++---- test/functional/api/server_requests_spec.lua | 4 ---- test/functional/helpers.lua | 4 +--- test/helpers.lua | 6 ++--- test/unit/mbyte_spec.lua | 5 ---- 8 files changed, 27 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 08447d5ff9..790fc9fb41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -317,10 +317,10 @@ if(HAS_DIAG_COLOR_FLAG) endif() endif() -option(TRAVIS_CI_BUILD "Travis/QuickBuild CI, extra flags will be set" OFF) +option(TRAVIS_CI_BUILD "Travis/sourcehut CI, extra flags will be set" OFF) if(TRAVIS_CI_BUILD) - message(STATUS "Travis/QuickBuild CI build enabled") + message(STATUS "Travis/sourcehut CI build enabled") add_compile_options(-Werror) if(DEFINED ENV{BUILD_32BIT}) # Get some test coverage for unsigned char diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 607f1aae83..04b4d23a2e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -85,7 +85,7 @@ the VCS/git logs more valuable. ### Automated builds (CI) -Each pull request must pass the automated builds on [Travis CI], [QuickBuild] +Each pull request must pass the automated builds on [Travis CI], [sourcehut] and [AppVeyor]. - CI builds are compiled with [`-Werror`][gcc-warnings], so compiler warnings @@ -100,14 +100,19 @@ and [AppVeyor]. - The [lint](#lint) build checks modified lines _and their immediate neighbors_, to encourage incrementally updating the legacy style to meet our [style](#style). (See [#3174][3174] for background.) -- [How to investigate QuickBuild failures](https://github.com/neovim/neovim/pull/4718#issuecomment-217631350) - - QuickBuild uses this invocation: - ``` - mkdir -p build/${params.get("buildType")} \ - && cd build/${params.get("buildType")} \ - && cmake -G "Unix Makefiles" -DBUSTED_OUTPUT_TYPE=TAP -DCMAKE_BUILD_TYPE=${params.get("buildType")} - -DTRAVIS_CI_BUILD=ON ../.. && ${node.getAttribute("make", "make")} - VERBOSE=1 nvim unittest-prereqs functionaltest-prereqs +- CI for freebsd and openbsd runs on [sourcehut]. + - To get a backtrace on freebsd (after connecting via ssh): + ```sh + sudo pkg install tmux # If you want tmux. + lldb build/bin/nvim -c nvim.core + + # To get a full backtrace: + # 1. Rebuild with debug info. + rm -rf nvim.core build + gmake CMAKE_BUILD_TYPE=RelWithDebInfo CMAKE_EXTRA_FLAGS="-DTRAVIS_CI_BUILD=ON -DMIN_LOG_LEVEL=3" nvim + # 2. Run the failing test to generate a new core file. + TEST_FILE=test/functional/foo.lua gmake functionaltest + lldb build/bin/nvim -c nvim.core ``` ### Clang scan-build @@ -223,7 +228,7 @@ as context, use the `-W` argument as well. [review-checklist]: https://github.com/neovim/neovim/wiki/Code-review-checklist [3174]: https://github.com/neovim/neovim/issues/3174 [Travis CI]: https://travis-ci.org/neovim/neovim -[QuickBuild]: http://neovim-qb.szakmeister.net/dashboard +[sourcehut]: https://builds.sr.ht/~jmk [AppVeyor]: https://ci.appveyor.com/project/neovim/neovim [Merge a Vim patch]: https://github.com/neovim/neovim/wiki/Merging-patches-from-upstream-Vim [Clang report]: https://neovim.io/doc/reports/clang/ diff --git a/runtime/doc/if_lua.txt b/runtime/doc/if_lua.txt index 0ba35aeae6..8528085f47 100644 --- a/runtime/doc/if_lua.txt +++ b/runtime/doc/if_lua.txt @@ -361,7 +361,7 @@ Note that underscore-prefixed functions (e.g. "_os_proc_children") are internal/private and must not be used by plugins. ------------------------------------------------------------------------------ -VIM.API *lua-api* +VIM.API *lua-api* *vim.api* `vim.api` exposes the full Nvim |API| as a table of Lua functions. @@ -371,7 +371,7 @@ Example: to use the "nvim_get_current_line()" API function, call print(tostring(vim.api.nvim_get_current_line())) ------------------------------------------------------------------------------ -VIM.LOOP *lua-loop* +VIM.LOOP *lua-loop* *vim.loop* `vim.loop` exposes all features of the Nvim event-loop. This is a low-level API that provides functionality for networking, filesystem, and process diff --git a/runtime/doc/intro.txt b/runtime/doc/intro.txt index 887ef764bd..3292489eda 100644 --- a/runtime/doc/intro.txt +++ b/runtime/doc/intro.txt @@ -378,11 +378,11 @@ notation meaning equivalent decimal value(s) ~ keypad = *keypad-equal* keypad Enter *keypad-enter* - keypad 0 to 9 *keypad-0* *keypad-9* - shift-key *shift* * control-key *control* *ctrl* * alt-key or meta-key *META* *ALT* * same as * command-key or "super" key * shift-key *shift* * control-key *control* *ctrl* * alt-key or meta-key *META* *ALT* * same as * command-key or "super" key * client', function() describe('connecting to its own pipe address', function() it('does not deadlock', function() - if not helpers.isCI('travis') and helpers.is_os('mac') then - -- It does, in fact, deadlock on QuickBuild. #6851 - pending("deadlocks on QuickBuild") - end local address = funcs.serverlist()[1] local first = string.sub(address,1,1) ok(first == '/' or first == '\\') diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index a6ff10c0a4..1108fbb2ba 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -29,10 +29,8 @@ local module = { } local start_dir = lfs.currentdir() --- XXX: NVIM_PROG takes precedence, QuickBuild sets it. module.nvim_prog = ( - os.getenv('NVIM_PROG') - or os.getenv('NVIM_PRG') + os.getenv('NVIM_PRG') or global_helpers.test_build_dir .. '/bin/nvim' ) -- Default settings for the test session. diff --git a/test/helpers.lua b/test/helpers.lua index 30e43a9ea4..4c526d217f 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -715,13 +715,11 @@ end function module.isCI(name) local any = (name == nil) - assert(any or name == 'appveyor' or name == 'quickbuild' or name == 'travis' - or name == 'sourcehut') + assert(any or name == 'appveyor' or name == 'travis' or name == 'sourcehut') local av = ((any or name == 'appveyor') and nil ~= os.getenv('APPVEYOR')) local tr = ((any or name == 'travis') and nil ~= os.getenv('TRAVIS')) - local qb = ((any or name == 'quickbuild') and nil ~= lfs.attributes('/usr/home/quickbuild')) local sh = ((any or name == 'sourcehut') and nil ~= os.getenv('SOURCEHUT')) - return tr or av or qb or sh + return tr or av or sh end diff --git a/test/unit/mbyte_spec.lua b/test/unit/mbyte_spec.lua index d27f52923a..fdb1bceab0 100644 --- a/test/unit/mbyte_spec.lua +++ b/test/unit/mbyte_spec.lua @@ -8,11 +8,6 @@ local mbyte = helpers.cimport("./src/nvim/mbyte.h") local charset = helpers.cimport('./src/nvim/charset.h') describe('mbyte', function() - if helpers.isCI('quickbuild') then - pending("crashes on quickbuild", function() end) - return - end - -- Array for composing characters local intp = ffi.typeof('int[?]') local function to_intp() From 6fd6f4683d19d5ca18f48c1d1f0d87113e80368e Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 21 Oct 2019 07:47:08 +0900 Subject: [PATCH 0192/1293] TUI/thread: guard env map from potential race with unibilium #11259 unibi_from_term calls getenv internally, so exclusive control is required. --- src/nvim/os/env.c | 10 ++++++++++ src/nvim/tui/tui.c | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index ae61e54993..eb86cb8ac7 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -44,6 +44,16 @@ void env_init(void) uv_mutex_init(&mutex); } +void os_env_var_lock(void) +{ + uv_mutex_lock(&mutex); +} + +void os_env_var_unlock(void) +{ + uv_mutex_unlock(&mutex); +} + /// Like getenv(), but returns NULL if the variable is empty. /// @see os_env_exists const char *os_getenv(const char *name) diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index 150862bb18..945b093f32 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -234,7 +234,9 @@ static void terminfo_start(UI *ui) // Set up unibilium/terminfo. char *termname = NULL; if (term) { + os_env_var_lock(); data->ut = unibi_from_term(term); + os_env_var_unlock(); if (data->ut) { termname = xstrdup(term); } From 2e4465e0585053bddaf8e6e60dc50bf1be0ba54e Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Mon, 21 Oct 2019 02:17:25 +0200 Subject: [PATCH 0193/1293] vim-patch:8.1.2168: heredoc not skipped in if-block #11265 Problem: Heredoc assignment not skipped in if block. Solution: Check if "skip" is set. https://github.com/vim/vim/commit/b1ba9abcb385b0a5355788a7eefef78ec68d2f65 Fixes https://github.com/neovim/neovim/issues/11264 --- src/nvim/eval.c | 10 ++++++---- src/nvim/testdir/test_let.vim | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index fd37e1001a..71ffb26cc2 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1675,10 +1675,12 @@ static void ex_let_const(exarg_T *eap, const bool is_const) list_T *l = heredoc_get(eap, expr + 3); if (l != NULL) { tv_list_set_ret(&rettv, l); - op[0] = '='; - op[1] = NUL; - (void)ex_let_vars(eap->arg, &rettv, false, semicolon, var_count, - is_const, op); + if (!eap->skip) { + op[0] = '='; + op[1] = NUL; + (void)ex_let_vars(eap->arg, &rettv, false, semicolon, var_count, + is_const, op); + } tv_clear(&rettv); } } else { diff --git a/src/nvim/testdir/test_let.vim b/src/nvim/testdir/test_let.vim index b5af871ab2..1fce3d6937 100644 --- a/src/nvim/testdir/test_let.vim +++ b/src/nvim/testdir/test_let.vim @@ -276,4 +276,12 @@ E app END call assert_equal(['something', 'app'], var1) + + let check = [] + if 0 + let check =<< trim END + from heredoc + END + endif + call assert_equal([], check) endfunc From 02393a0c74d3aaeece939bafa4f658763acca965 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Mon, 21 Oct 2019 14:53:00 +0900 Subject: [PATCH 0194/1293] ci/install.sh: pin treesitter to v0.15.9 #11266 When "tree-sitter test" is executed, query test is also executed, but "tree-sitter-c" does not have query test yet, so cli version that does not include query test execution To use. ref https://github.com/tree-sitter/tree-sitter/commit/e14e285a1087264a8c74a7c62fcaecc49db9d904 --- ci/install.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ci/install.sh b/ci/install.sh index 21175f63e0..24a4bd7450 100755 --- a/ci/install.sh +++ b/ci/install.sh @@ -26,7 +26,11 @@ npm install -g neovim npm link neovim echo "Install tree-sitter npm package" -npm install -g tree-sitter-cli + +# FIXME +# https://github.com/tree-sitter/tree-sitter/commit/e14e285a1087264a8c74a7c62fcaecc49db9d904 +# If queries added to tree-sitter-c, we can use latest tree-sitter-cli +npm install -g tree-sitter-cli@v0.15.9 npm link tree-sitter-cli echo "Install tree-sitter c parser" From f4cbe96488fce4de971be2e25b254320f0fa71b2 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 20 Oct 2019 05:59:29 -0400 Subject: [PATCH 0195/1293] vim-patch:8.1.2185: syntax test fails Problem: Syntax test fails. Solution: Add missing file patch. https://github.com/vim/vim/commit/bbfd1562aeaa5b40b6451effc399846b692d6992 --- src/nvim/testdir/test_syntax.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nvim/testdir/test_syntax.vim b/src/nvim/testdir/test_syntax.vim index b9310e2168..c2025b36e0 100644 --- a/src/nvim/testdir/test_syntax.vim +++ b/src/nvim/testdir/test_syntax.vim @@ -519,6 +519,7 @@ func Test_syntax_c() \ ' for (int i = 0; i < count; ++i) {', \ ' break;', \ ' }', + \ " Note: asdf", \ '}', \ ], 'Xtest.c') @@ -526,7 +527,8 @@ func Test_syntax_c() " response to t_RB corrects it to "light". let $COLORFGBG = '15;0' - let buf = RunVimInTerminal('Xtest.c', {}) + let buf = RunVimInTerminal('Xtest.c', #{rows: 22}) + call term_sendkeys(buf, ":syn keyword Search Note\r") call VerifyScreenDump(buf, 'Test_syntax_c_01', {}) call StopVimInTerminal(buf) From 60415a5d3a0fec6fc42a900aba15943b3d1730cd Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 20 Oct 2019 06:04:07 -0400 Subject: [PATCH 0196/1293] vim-patch:8.1.2188: build error for missing define Problem: Build error for missing define. Solution: Add missing change. https://github.com/vim/vim/commit/2b78ab5d0c91c229715ae140a34978506343bde3 These "WILD_" macros are used in earlier vim patches. --- src/nvim/ex_getln.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/nvim/ex_getln.h b/src/nvim/ex_getln.h index 051564fbe1..99d5a7786d 100644 --- a/src/nvim/ex_getln.h +++ b/src/nvim/ex_getln.h @@ -27,6 +27,8 @@ #define WILD_ESCAPE 0x80 #define WILD_ICASE 0x100 #define WILD_ALLLINKS 0x200 +#define WILD_IGNORE_COMPLETESLASH 0x400 +#define WILD_NOERROR 0x800 // sets EW_NOERROR /// Present history tables typedef enum { From 13a6878d187612721baecede181e7dfdc3699a59 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 20 Oct 2019 16:39:48 -0400 Subject: [PATCH 0197/1293] vim-patch:8.1.2190: syntax test fails on Mac Problem: Syntax test fails on Mac. Solution: Limit the window size to 20 rows. https://github.com/vim/vim/commit/83e9a1ce75818a78c5ddf8dcfb820634ca6fabff --- src/nvim/testdir/test_syntax.vim | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/nvim/testdir/test_syntax.vim b/src/nvim/testdir/test_syntax.vim index c2025b36e0..6cada1503f 100644 --- a/src/nvim/testdir/test_syntax.vim +++ b/src/nvim/testdir/test_syntax.vim @@ -502,9 +502,7 @@ func Test_syntax_c() endif call writefile([ \ '/* comment line at the top */', - \ ' int', - \ 'main(int argc, char **argv)// another comment', - \ '{', + \ 'int main(int argc, char **argv) { // another comment', \ '#if 0', \ ' int not_used;', \ '#else', @@ -527,7 +525,7 @@ func Test_syntax_c() " response to t_RB corrects it to "light". let $COLORFGBG = '15;0' - let buf = RunVimInTerminal('Xtest.c', #{rows: 22}) + let buf = RunVimInTerminal('Xtest.c', {}) call term_sendkeys(buf, ":syn keyword Search Note\r") call VerifyScreenDump(buf, 'Test_syntax_c_01', {}) call StopVimInTerminal(buf) From c067efa696698d455d9a1488c26e0fb5d8cb5bf5 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 20 Oct 2019 20:12:08 -0400 Subject: [PATCH 0198/1293] vim-patch:8.1.2197: ExitPre autocommand may cause accessing freed memory Problem: ExitPre autocommand may cause accessing freed memory. Solution: Check the window pointer is still valid. (closes vim/vim#5093) https://github.com/vim/vim/commit/34ba06b6e6f94bb46062e6c85dbfdcbb0d255ada --- src/nvim/ex_docmd.c | 8 +++++--- src/nvim/testdir/test_exit.vim | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 0da2cd67d6..ae3fb4fbfb 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -6070,9 +6070,11 @@ static bool before_quit_autocmds(win_T *wp, bool quit_all, int forceit) if (quit_all || (check_more(false, forceit) == OK && only_one_window())) { apply_autocmds(EVENT_EXITPRE, NULL, NULL, false, curbuf); - // Refuse to quit when locked or when the buffer in the last window is - // being closed (can only happen in autocommands). - if (curbuf_locked() + // Refuse to quit when locked or when the window was closed or the + // buffer in the last window is being closed (can only happen in + // autocommands). + if (!win_valid(wp) + || curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0)) { return true; } diff --git a/src/nvim/testdir/test_exit.vim b/src/nvim/testdir/test_exit.vim index 3797626abf..99a401d4a4 100644 --- a/src/nvim/testdir/test_exit.vim +++ b/src/nvim/testdir/test_exit.vim @@ -40,6 +40,7 @@ func Test_exiting() endif call delete('Xtestout') + " ExitPre autocommand splits the window, so that it's no longer the last one. let after =<< trim [CODE] au QuitPre * call writefile(["QuitPre"], "Xtestout", "a") au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") @@ -58,4 +59,25 @@ func Test_exiting() \ readfile('Xtestout')) endif call delete('Xtestout') + + " ExitPre autocommand splits and closes the window, so that there is still + " one window but it's a different one. + let after =<< trim [CODE] + au QuitPre * call writefile(["QuitPre"], "Xtestout", "a") + au ExitPre * call writefile(["ExitPre"], "Xtestout", "a") + augroup nasty + au ExitPre * split | only + augroup END + quit + augroup nasty + au! ExitPre + augroup END + quit + [CODE] + + if RunVim([], after, '') + call assert_equal(['QuitPre', 'ExitPre', 'QuitPre', 'ExitPre'], + \ readfile('Xtestout')) + endif + call delete('Xtestout') endfunc From e284b7233fb459a7a6d4ce0f98371b34b3639d2b Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 22 Oct 2019 19:55:55 +0100 Subject: [PATCH 0199/1293] Perform HASHTAB_ITER bookkeeping before user-code The `HASHTAB_ITER` logic keeps track of how many entries in the hash table are left to visit, decrementing this on each iteration of the loop. This was previously decremented at the end of the loop body: ```c size_t hi##todo_ = hi##ht_->ht_used; for (hashitem_T *hi = hi##ht_->ht_array; hi##todo_; hi++) { if (!HASHITEM_EMPTY(hi)) { { } hi##todo_--; // <--- important decrement here } } ``` This meant that if the body of the loop (substituted in via macro expansion) contained a `continue` statement, we'd skip decrementing our counter, meaning we'd iterate too many times over the hash table, usually leading to an out of bounds read beyond the hash table's memory, or uninitialised/null pointers from unused hash table slots. Decrementing `hi##todo` before the arbitrary loop body protects us from this, and has no adverse side-effects since only the macro code can (or should) use this variable. Before this commit, no code within `HASHTAB_ITER()` contained a `continue`, meaning this bug was left dormant and the fix has a very minimal chance of introducing any bugs. --- src/nvim/hashtab.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nvim/hashtab.h b/src/nvim/hashtab.h index a70a8bea63..19633d455f 100644 --- a/src/nvim/hashtab.h +++ b/src/nvim/hashtab.h @@ -81,10 +81,10 @@ typedef struct hashtable_S { size_t hi##todo_ = hi##ht_->ht_used; \ for (hashitem_T *hi = hi##ht_->ht_array; hi##todo_; hi++) { \ if (!HASHITEM_EMPTY(hi)) { \ + hi##todo_--; \ { \ code \ } \ - hi##todo_--; \ } \ } \ } while (0) From 194f7bfacea934177d524197127242947bd28471 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 10 Oct 2019 22:06:45 +0100 Subject: [PATCH 0200/1293] vim-patch:8.1.1228: not possible to process tags with a function Problem: Not possible to process tags with a function. Solution: Add tagfunc() (Christian Brabandt, Andy Massimino, closes vim/vim#4010) https://github.com/vim/vim/commit/45e18cbdc40afd8144d20dcc07ad2d981636f4c9 --- runtime/doc/options.txt | 8 + runtime/doc/tagsrch.txt | 66 +++++ runtime/optwin.vim | 5 + src/nvim/buffer.c | 1 + src/nvim/buffer_defs.h | 10 +- src/nvim/edit.c | 2 + src/nvim/ex_cmds.c | 2 +- src/nvim/globals.h | 6 +- src/nvim/normal.c | 2 + src/nvim/option.c | 5 + src/nvim/option_defs.h | 1 + src/nvim/options.lua | 8 + src/nvim/tag.c | 386 ++++++++++++++++++++++++++---- src/nvim/tag.h | 27 ++- src/nvim/testdir/test_alot.vim | 1 + src/nvim/testdir/test_tagfunc.vim | 84 +++++++ src/nvim/window.c | 16 +- 17 files changed, 561 insertions(+), 69 deletions(-) create mode 100644 src/nvim/testdir/test_tagfunc.vim diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 971c4ffbd5..65d2a7652c 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -6159,6 +6159,14 @@ A jump table for the options with a short description can be found at |Q_op|. match Match case smart Ignore case unless an upper case letter is used + *'tagfunc'* *'tfu'* + 'tagfunc' 'tfu' string (default: empty) + local to buffer + This option specifies a function to be used to perform tag searches. + The function gets the tag pattern and should return a List of matching + tags. See |tag-function| for an explanation of how to write the + function and an example. + *'taglength'* *'tl'* 'taglength' 'tl' number (default 0) global diff --git a/runtime/doc/tagsrch.txt b/runtime/doc/tagsrch.txt index bb3134feb6..b011db3dd3 100644 --- a/runtime/doc/tagsrch.txt +++ b/runtime/doc/tagsrch.txt @@ -838,4 +838,70 @@ Common arguments for the commands above: < For a ":djump", ":dsplit", ":dlist" and ":dsearch" command the pattern is used as a literal string, not as a search pattern. +============================================================================== +7. Using 'tagfunc' *tag-function* + +It is possible to provide Vim with a function which will generate a list of +tags used for commands like |:tag|, |:tselect| and Normal mode tag commands +like |CTRL-]|. + +The function used for generating the taglist is specified by setting the +'tagfunc' option. The function will be called with three arguments: + a:pattern The tag identifier used during the tag search. + a:flags List of flags to control the function behavior. + a:info Dict containing the following entries: + buf_ffname Full filename which can be used for priority. + user_data Custom data String, if stored in the tag + stack previously by tagfunc. + +Currently two flags may be passed to the tag function: + 'c' The function was invoked by a normal command being processed + (mnemonic: the tag function may use the context around the + cursor to perform a better job of generating the tag list.) + 'i' In Insert mode, the user was completing a tag (with + |i_CTRL-X_CTRL-]|). + +Note that when 'tagfunc' is set, the priority of the tags described in +|tag-priority| does not apply. Instead, the priority is exactly as the +ordering of the elements in the list returned by the function. + *E987* +The function should return a List of Dict entries. Each Dict must at least +include the following entries and each value must be a string: + name Name of the tag. + filename Name of the file where the tag is defined. It is + either relative to the current directory or a full path. + cmd Ex command used to locate the tag in the file. This + can be either an Ex search pattern or a line number. +Note that the format is similar to that of |taglist()|, which makes it possible +to use its output to generate the result. +The following fields are optional: + kind Type of the tag. + user_data String of custom data stored in the tag stack which + can be used to disambiguate tags between operations. + +If the function returns |v:null| instead of a List, a standard tag lookup will +be performed instead. + +It is not allowed to change the tagstack from inside 'tagfunc'. *E986* + +The following is a hypothetical example of a function used for 'tagfunc'. It +uses the output of |taglist()| to generate the result: a list of tags in the +inverse order of file names. +> + function! TagFunc(pattern, flags, info) + function! CompareFilenames(item1, item2) + let f1 = a:item1['filename'] + let f2 = a:item2['filename'] + return f1 >=# f2 ? + \ -1 : f1 <=# f2 ? 1 : 0 + endfunction + + let result = taglist(a:pattern) + call sort(result, "CompareFilenames") + + return result + endfunc + set tagfunc=TagFunc +< + vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/runtime/optwin.vim b/runtime/optwin.vim index 51b2bbb583..6b3328a5d4 100644 --- a/runtime/optwin.vim +++ b/runtime/optwin.vim @@ -300,6 +300,11 @@ call append("$", "tagstack\ta :tag command will use the tagstack") call BinOptionG("tgst", &tgst) call append("$", "showfulltag\twhen completing tags in Insert mode show more info") call BinOptionG("sft", &sft) +if has("eval") + call append("$", "tagfunc\ta function to be used to perform tag searches") + call append("$", "\t(local to buffer)") + call OptionL("tfu") +endif if has("cscope") call append("$", "cscopeprg\tcommand for executing cscope") call OptionG("csprg", &csprg) diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index b81ffd09e1..1d5aa8ba9b 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -1947,6 +1947,7 @@ void free_buf_options(buf_T *buf, int free_p_ff) clear_string_option(&buf->b_p_path); clear_string_option(&buf->b_p_tags); clear_string_option(&buf->b_p_tc); + clear_string_option(&buf->b_p_tfu); clear_string_option(&buf->b_p_dict); clear_string_option(&buf->b_p_tsr); clear_string_option(&buf->b_p_qe); diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index 16c7804be0..ca740dea21 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -119,10 +119,11 @@ typedef uint16_t disptick_T; // display tick type * The taggy struct is used to store the information about a :tag command. */ typedef struct taggy { - char_u *tagname; /* tag name */ - fmark_T fmark; /* cursor position BEFORE ":tag" */ - int cur_match; /* match number */ - int cur_fnum; /* buffer number used for cur_match */ + char_u *tagname; // tag name + fmark_T fmark; // cursor position BEFORE ":tag" + int cur_match; // match number + int cur_fnum; // buffer number used for cur_match + char_u *user_data; // used with tagfunc } taggy_T; typedef struct buffblock buffblock_T; @@ -647,6 +648,7 @@ struct file_buffer { char_u *b_p_cpt; ///< 'complete' char_u *b_p_cfu; ///< 'completefunc' char_u *b_p_ofu; ///< 'omnifunc' + char_u *b_p_tfu; ///< 'tagfunc' int b_p_eol; ///< 'endofline' int b_p_fixeol; ///< 'fixendofline' int b_p_et; ///< 'expandtab' diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 16c4882975..a4b4e0d980 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -4047,12 +4047,14 @@ static int ins_compl_get_exp(pos_T *ini) // Find up to TAG_MANY matches. Avoids that an enormous number // of matches is found when compl_pattern is empty + g_tag_at_cursor = true; if (find_tags(compl_pattern, &num_matches, &matches, TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP | (l_ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0), TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) { ins_compl_add_matches(num_matches, matches, p_ic); } + g_tag_at_cursor = false; p_ic = save_p_ic; break; diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 00de7a3d66..2e8bd79c81 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -4905,7 +4905,7 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, *matches = NULL; *num_matches = 0; - int flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE; + int flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE | TAG_NO_TAGFUNC; if (keep_lang) { flags |= TAG_KEEP_LANG; } diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 5237c621f9..c3d1a4d40b 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -787,7 +787,11 @@ EXTERN int postponed_split_flags INIT(= 0); /* args for win_split() */ EXTERN int postponed_split_tab INIT(= 0); /* cmdmod.tab */ EXTERN int g_do_tagpreview INIT(= 0); /* for tag preview commands: height of preview window */ -EXTERN int replace_offset INIT(= 0); /* offset for replace_push() */ +EXTERN int g_tag_at_cursor INIT(= false); // whether the tag command comes + // from the command line (0) or was + // invoked as a normal command (1) + +EXTERN int replace_offset INIT(= 0); // offset for replace_push() EXTERN char_u *escape_chars INIT(= (char_u *)" \t\\\"|"); /* need backslash in cmd line */ diff --git a/src/nvim/normal.c b/src/nvim/normal.c index e0dc9d4f23..28183ffa1d 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -4929,7 +4929,9 @@ static void nv_ident(cmdarg_T *cap) add_to_history(HIST_SEARCH, (char_u *)buf, true, NUL); (void)normal_search(cap, cmdchar == '*' ? '/' : '?', (char_u *)buf, 0); } else { + g_tag_at_cursor = true; do_cmdline_cmd(buf); + g_tag_at_cursor = false; } xfree(buf); diff --git a/src/nvim/option.c b/src/nvim/option.c index 22f7b85133..20351d3908 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -133,6 +133,7 @@ static char_u *p_cms; static char_u *p_cpt; static char_u *p_cfu; static char_u *p_ofu; +static char_u *p_tfu; static int p_eol; static int p_fixeol; static int p_et; @@ -2273,6 +2274,7 @@ void check_buf_options(buf_T *buf) check_string_option(&buf->b_p_ep); check_string_option(&buf->b_p_path); check_string_option(&buf->b_p_tags); + check_string_option(&buf->b_p_tfu); check_string_option(&buf->b_p_tc); check_string_option(&buf->b_p_dict); check_string_option(&buf->b_p_tsr); @@ -5590,6 +5592,7 @@ static char_u *get_varp_scope(vimoption_T *p, int opt_flags) case PV_INC: return (char_u *)&(curbuf->b_p_inc); case PV_DICT: return (char_u *)&(curbuf->b_p_dict); case PV_TSR: return (char_u *)&(curbuf->b_p_tsr); + case PV_TFU: return (char_u *)&(curbuf->b_p_tfu); case PV_STL: return (char_u *)&(curwin->w_p_stl); case PV_UL: return (char_u *)&(curbuf->b_p_ul); case PV_LW: return (char_u *)&(curbuf->b_p_lw); @@ -5742,6 +5745,7 @@ static char_u *get_varp(vimoption_T *p) case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf); case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl); case PV_SW: return (char_u *)&(curbuf->b_p_sw); + case PV_TFU: return (char_u *)&(curbuf->b_p_tfu); case PV_TS: return (char_u *)&(curbuf->b_p_ts); case PV_TW: return (char_u *)&(curbuf->b_p_tw); case PV_UDF: return (char_u *)&(curbuf->b_p_udf); @@ -6004,6 +6008,7 @@ void buf_copy_options(buf_T *buf, int flags) buf->b_p_cpt = vim_strsave(p_cpt); buf->b_p_cfu = vim_strsave(p_cfu); buf->b_p_ofu = vim_strsave(p_ofu); + buf->b_p_tfu = vim_strsave(p_tfu); buf->b_p_sts = p_sts; buf->b_p_sts_nopaste = p_sts_nopaste; buf->b_p_com = vim_strsave(p_com); diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index 67cb53ce02..e5a3c0bd95 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -780,6 +780,7 @@ enum { , BV_SUA , BV_SW , BV_SWF + , BV_TFU , BV_TAGS , BV_TC , BV_TS diff --git a/src/nvim/options.lua b/src/nvim/options.lua index 52e788944b..e96b3f8e02 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -2388,6 +2388,14 @@ return { varname='p_syn', defaults={if_true={vi=""}} }, + { + full_name='tagfunc', abbreviation='tfu', + type='string', scope={'buffer'}, + vim=true, + vi_def=true, + varname='p_tfu', + defaults={if_true={vi=""}} + }, { full_name='tabline', abbreviation='tal', type='string', scope={'global'}, diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 880c467d30..872fc0f279 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -51,17 +51,20 @@ * Structure to hold pointers to various items in a tag line. */ typedef struct tag_pointers { - /* filled in by parse_tag_line(): */ - char_u *tagname; /* start of tag name (skip "file:") */ - char_u *tagname_end; /* char after tag name */ - char_u *fname; /* first char of file name */ - char_u *fname_end; /* char after file name */ - char_u *command; /* first char of command */ - /* filled in by parse_match(): */ - char_u *command_end; /* first char after command */ - char_u *tag_fname; /* file name of the tags file */ - char_u *tagkind; /* "kind:" value */ - char_u *tagkind_end; /* end of tagkind */ + // filled in by parse_tag_line(): + char_u *tagname; // start of tag name (skip "file:") + char_u *tagname_end; // char after tag name + char_u *fname; // first char of file name + char_u *fname_end; // char after file name + char_u *command; // first char of command + // filled in by parse_match(): + char_u *command_end; // first char after command + char_u *tag_fname; // file name of the tags file. This is used + // when 'tr' is set. + char_u *tagkind; // "kind:" value + char_u *tagkind_end; // end of tagkind + char_u *user_data; // user_data string + char_u *user_data_end; // end of user_data } tagptrs_T; /* @@ -102,6 +105,10 @@ static char_u *nofile_fname = NULL; /* fname for NOTAGFILE error */ static char_u *bottommsg = (char_u *)N_("E555: at bottom of tag stack"); static char_u *topmsg = (char_u *)N_("E556: at top of tag stack"); +static char_u *recurmsg + = (char_u *)N_("E986: cannot modify the tag stack within tagfunc"); +static char_u *tfu_inv_ret_msg + = (char_u *)N_("E987: invalid return value from tagfunc"); static char_u *tagmatchname = NULL; /* name of last used tag */ @@ -109,7 +116,12 @@ static char_u *tagmatchname = NULL; /* name of last used tag */ * Tag for preview window is remembered separately, to avoid messing up the * normal tagstack. */ -static taggy_T ptag_entry = { NULL, { { 0, 0, 0 }, 0, 0, NULL }, 0, 0 }; +static taggy_T ptag_entry = { NULL, { { 0, 0, 0 }, 0, 0, NULL }, 0, 0, NULL }; + +static int tfu_in_use = false; // disallow recursive call of tagfunc + +// Used instead of NUL to separate tag fields in the growarrays. +#define TAG_SEP 0x02 /* * Jump to tag; handling of tag commands and tag stack @@ -161,6 +173,7 @@ do_tag( int use_tagstack; int skip_msg = false; char_u *buf_ffname = curbuf->b_ffname; // name for priority computation + int use_tfu = 1; /* remember the matches for the last used tag */ static int num_matches = 0; @@ -168,6 +181,11 @@ do_tag( static char_u **matches = NULL; static int flags; + if (tfu_in_use) { + EMSG(_(recurmsg)); + return false; + } + #ifdef EXITFREE if (type == DT_FREE) { /* remove the list of matches */ @@ -181,6 +199,7 @@ do_tag( if (type == DT_HELP) { type = DT_TAG; no_regexp = true; + use_tfu = 0; } prev_num_matches = num_matches; @@ -196,7 +215,7 @@ do_tag( use_tagstack = false; new_tag = true; if (g_do_tagpreview != 0) { - xfree(ptag_entry.tagname); + tagstack_clear_entry(&ptag_entry); ptag_entry.tagname = vim_strsave(tag); } } else { @@ -220,7 +239,7 @@ do_tag( cur_match = ptag_entry.cur_match; cur_fnum = ptag_entry.cur_fnum; } else { - xfree(ptag_entry.tagname); + tagstack_clear_entry(&ptag_entry); ptag_entry.tagname = vim_strsave(tag); } } else { @@ -228,16 +247,18 @@ do_tag( * If the last used entry is not at the top, delete all tag * stack entries above it. */ - while (tagstackidx < tagstacklen) - xfree(tagstack[--tagstacklen].tagname); + while (tagstackidx < tagstacklen) { + tagstack_clear_entry(&tagstack[--tagstacklen]); + } /* if the tagstack is full: remove oldest entry */ if (++tagstacklen > TAGSTACKSIZE) { tagstacklen = TAGSTACKSIZE; - xfree(tagstack[0].tagname); - for (i = 1; i < tagstacklen; ++i) + tagstack_clear_entry(&tagstack[0]); + for (i = 1; i < tagstacklen; i++) { tagstack[i - 1] = tagstack[i]; - --tagstackidx; + } + tagstackidx--; } // put the tag name in the tag stack @@ -447,15 +468,22 @@ do_tag( } else flags = TAG_NOIC; - if (type == DT_CSCOPE) + if (type == DT_CSCOPE) { flags = TAG_CSCOPE; - if (verbose) + } + if (verbose) { flags |= TAG_VERBOSE; + } + if (!use_tfu) { + flags |= TAG_NO_TAGFUNC; + } + if (find_tags(name, &new_num_matches, &new_matches, flags, - max_num_matches, buf_ffname) == OK - && new_num_matches < max_num_matches) - max_num_matches = MAXCOL; /* If less than max_num_matches - found: all matches found. */ + max_num_matches, buf_ffname) == OK + && new_num_matches < max_num_matches) { + max_num_matches = MAXCOL; // If less than max_num_matches + // found: all matches found. + } /* If there already were some matches for the same name, move them * to the start. Avoids that the order changes when using @@ -543,9 +571,20 @@ do_tag( cur_match = num_matches - 1; } if (use_tagstack) { + tagptrs_T tagp2; + tagstack[tagstackidx].cur_match = cur_match; tagstack[tagstackidx].cur_fnum = cur_fnum; - ++tagstackidx; + + // store user-provided data originating from tagfunc + if (use_tfu && parse_match(matches[cur_match], &tagp2) == OK + && tagp2.user_data) { + XFREE_CLEAR(tagstack[tagstackidx].user_data); + tagstack[tagstackidx].user_data = vim_strnsave( + tagp2.user_data, tagp2.user_data_end - tagp2.user_data); + } + + tagstackidx++; } else if (g_do_tagpreview != 0) { ptag_entry.cur_match = cur_match; ptag_entry.cur_fnum = cur_fnum; @@ -1083,6 +1122,220 @@ static void prepare_pats(pat_T *pats, int has_re) pats->regmatch.regprog = NULL; } +// +// Call the user-defined function to generate a list of tags used by +// find_tags(). +// +// Return OK if at least 1 tag has been successfully found, +// NOTDONE if the function returns v:null, and FAIL otherwise. +// +static int find_tagfunc_tags( + char_u *pat, // pattern supplied to the user-defined function + garray_T *ga, // the tags will be placed here + int *match_count, // here the number of tags found will be placed + int flags, // flags from find_tags (TAG_*) + char_u *buf_ffname) // name of buffer for priority +{ + pos_T save_pos; + list_T *taglist; + int ntags = 0; + int result = FAIL; + typval_T args[4]; + typval_T rettv; + char_u flagString[3]; + dict_T *d; + taggy_T *tag = &curwin->w_tagstack[curwin->w_tagstackidx]; + + if (*curbuf->b_p_tfu == NUL) { + return FAIL; + } + + args[0].v_type = VAR_STRING; + args[0].vval.v_string = pat; + args[1].v_type = VAR_STRING; + args[1].vval.v_string = flagString; + + // create 'info' dict argument + d = tv_dict_alloc(); + if (tag->user_data != NULL) { + tv_dict_add_str(d, S_LEN("user_data"), (const char *)tag->user_data); + } + if (buf_ffname != NULL) { + tv_dict_add_str(d, S_LEN("buf_ffname"), (const char *)buf_ffname); + } + + d->dv_refcount++; + args[2].v_type = VAR_DICT; + args[2].vval.v_dict = d; + + args[3].v_type = VAR_UNKNOWN; + + vim_snprintf((char *)flagString, sizeof(flagString), + "%s%s", + g_tag_at_cursor ? "c": "", + flags & TAG_INS_COMP ? "i": ""); + + save_pos = curwin->w_cursor; + result = call_vim_function(curbuf->b_p_tfu, 3, args, &rettv); + curwin->w_cursor = save_pos; // restore the cursor position + d->dv_refcount--; + + if (result == FAIL) { + return FAIL; + } + if (rettv.v_type == VAR_SPECIAL && rettv.vval.v_number == VV_NULL) { + tv_clear(&rettv); + return NOTDONE; + } + if (rettv.v_type != VAR_LIST || !rettv.vval.v_list) { + tv_clear(&rettv); + EMSG(_(tfu_inv_ret_msg)); + return FAIL; + } + taglist = rettv.vval.v_list; + + TV_LIST_ITER_CONST(taglist, li, { + char_u *mfp; + char_u *res_name; + char_u *res_fname; + char_u *res_cmd; + char_u *res_kind; + int len; + int has_extra = 0; + int name_only = flags & TAG_NAMES; + + if (TV_LIST_ITEM_TV(li)->v_type != VAR_DICT) { + EMSG(_(tfu_inv_ret_msg)); + break; + } + + len = 2; + res_name = NULL; + res_fname = NULL; + res_cmd = NULL; + res_kind = NULL; + + TV_DICT_ITER(TV_LIST_ITEM_TV(li)->vval.v_dict, di, { + const char_u *dict_key = di->di_key; + typval_T *tv = &di->di_tv; + + if (tv->v_type != VAR_STRING || tv->vval.v_string == NULL) { + continue; + } + + len += STRLEN(tv->vval.v_string) + 1; // Space for "\tVALUE" + if (!STRCMP(dict_key, "name")) { + res_name = tv->vval.v_string; + continue; + } + if (!STRCMP(dict_key, "filename")) { + res_fname = tv->vval.v_string; + continue; + } + if (!STRCMP(dict_key, "cmd")) { + res_cmd = tv->vval.v_string; + continue; + } + has_extra = 1; + if (!STRCMP(dict_key, "kind")) { + res_kind = tv->vval.v_string; + continue; + } + // Other elements will be stored as "\tKEY:VALUE" + // Allocate space for the key and the colon + len += STRLEN(dict_key) + 1; + }); + + if (has_extra) { + len += 2; // need space for ;" + } + + if (!res_name || !res_fname || !res_cmd) { + EMSG(_(tfu_inv_ret_msg)); + break; + } + + if (name_only) { + mfp = vim_strsave(res_name); + } else { + mfp = (char_u *)xmalloc((int)sizeof(char_u) + len + 1); + } + + if (mfp == NULL) { + continue; + } + + if (!name_only) { + char_u *p = mfp; + + *p++ = MT_GL_OTH + 1; // mtt + *p++ = TAG_SEP; // no tag file name + + STRCPY(p, res_name); + p += STRLEN(p); + + *p++ = TAB; + STRCPY(p, res_fname); + p += STRLEN(p); + + *p++ = TAB; + STRCPY(p, res_cmd); + p += STRLEN(p); + + if (has_extra) { + STRCPY(p, ";\""); + p += STRLEN(p); + + if (res_kind) { + *p++ = TAB; + STRCPY(p, res_kind); + p += STRLEN(p); + } + + TV_DICT_ITER(TV_LIST_ITEM_TV(li)->vval.v_dict, di, { + const char_u *dict_key = di->di_key; + typval_T *tv = &di->di_tv; + if (tv->v_type != VAR_STRING || tv->vval.v_string == NULL) { + continue; + } + + if (!STRCMP(dict_key, "name")) { + continue; + } + if (!STRCMP(dict_key, "filename")) { + continue; + } + if (!STRCMP(dict_key, "cmd")) { + continue; + } + if (!STRCMP(dict_key, "kind")) { + continue; + } + + *p++ = TAB; + STRCPY(p, dict_key); + p += STRLEN(p); + STRCPY(p, ":"); + p += STRLEN(p); + STRCPY(p, tv->vval.v_string); + p += STRLEN(p); + }); + } + } + + // Add all matches because tagfunc should do filtering. + ga_grow(ga, 1); + ((char_u **)(ga->ga_data))[ga->ga_len++] = mfp; + ntags++; + result = OK; + }); + + tv_clear(&rettv); + + *match_count = ntags; + return result; +} + /* * find_tags() - search for tags in tags files * @@ -1108,6 +1361,7 @@ static void prepare_pats(pat_T *pats, int has_re) * TAG_NOIC don't always ignore case * TAG_KEEP_LANG keep language * TAG_CSCOPE use cscope results for tags + * TAG_NO_TAGFUNC do not call the 'tagfunc' function */ int find_tags( @@ -1198,6 +1452,7 @@ find_tags( int get_it_again = FALSE; int use_cscope = (flags & TAG_CSCOPE); int verbose = (flags & TAG_VERBOSE); + int use_tfu = ((flags & TAG_NO_TAGFUNC) == 0); int save_p_ic = p_ic; // Change the value of 'ignorecase' according to 'tagcase' for the @@ -1275,6 +1530,16 @@ find_tags( // uninitialised. memset(&search_info, 0, 1); // -V512 + if (*curbuf->b_p_tfu != NUL && use_tfu && !tfu_in_use) { + tfu_in_use = true; + retval = find_tagfunc_tags(pat, &ga_match[0], &match_count, + flags, buf_ffname); + tfu_in_use = false; + if (retval != NOTDONE) { + goto findtag_end; + } + } + /* * When finding a specified number of matches, first try with matching * case, so binary search can be used, and try ignore-case matches in a @@ -1856,7 +2121,6 @@ parse_line: } } } else { -#define TAG_SEP 0x02 size_t tag_fname_len = STRLEN(tag_fname); // Save the tag in a buffer. // Use 0x02 to separate fields (Can't use NUL, because the @@ -2040,9 +2304,7 @@ void free_tag_stuff(void) do_tag(NULL, DT_FREE, 0, 0, 0); tag_freematch(); - if (ptag_entry.tagname) { - XFREE_CLEAR(ptag_entry.tagname); - } + tagstack_clear_entry(&ptag_entry); } #endif @@ -2283,6 +2545,7 @@ parse_match( tagp); tagp->tagkind = NULL; + tagp->user_data = NULL; tagp->command_end = NULL; if (retval == OK) { @@ -2300,13 +2563,17 @@ parse_match( while (ASCII_ISALPHA(*p)) { if (STRNCMP(p, "kind:", 5) == 0) { tagp->tagkind = p + 5; + } else if (STRNCMP(p, "user_data:", 10) == 0) { + tagp->user_data = p + 10; + } + if (tagp->tagkind != NULL && tagp->user_data != NULL) { break; } + pc = vim_strchr(p, ':'); pt = vim_strchr(p, '\t'); if (pc == NULL || (pt != NULL && pc > pt)) { tagp->tagkind = p; - break; } if (pt == NULL) break; @@ -2320,6 +2587,12 @@ parse_match( ; tagp->tagkind_end = p; } + if (tagp->user_data != NULL) { + for (p = tagp->user_data; + *p && *p != '\t' && *p != '\r' && *p != '\n'; p++) { + } + tagp->user_data_end = p; + } } return retval; } @@ -2770,6 +3043,15 @@ static int find_extra(char_u **pp) return FAIL; } +// +// Free a single entry in a tag stack +// +static void tagstack_clear_entry(taggy_T *item) +{ + XFREE_CLEAR(item->tagname); + XFREE_CLEAR(item->user_data); +} + int expand_tags ( int tagnames, /* expand tag names */ @@ -2789,14 +3071,16 @@ expand_tags ( tagnmflag = TAG_NAMES; else tagnmflag = 0; - if (pat[0] == '/') + if (pat[0] == '/') { ret = find_tags(pat + 1, num_file, file, - TAG_REGEXP | tagnmflag | TAG_VERBOSE, - TAG_MANY, curbuf->b_ffname); - else + TAG_REGEXP | tagnmflag | TAG_VERBOSE | TAG_NO_TAGFUNC, + TAG_MANY, curbuf->b_ffname); + } else { ret = find_tags(pat, num_file, file, - TAG_REGEXP | tagnmflag | TAG_VERBOSE | TAG_NOIC, - TAG_MANY, curbuf->b_ffname); + TAG_REGEXP | tagnmflag | TAG_VERBOSE + | TAG_NO_TAGFUNC | TAG_NOIC, + TAG_MANY, curbuf->b_ffname); + } if (ret == OK && !tagnames) { /* Reorganize the tags for display and matching as strings of: * "\0\0\0" @@ -2958,6 +3242,9 @@ static void get_tag_details(taggy_T *tag, dict_T *retdict) tv_dict_add_str(retdict, S_LEN("tagname"), (const char *)tag->tagname); tv_dict_add_nr(retdict, S_LEN("matchnr"), tag->cur_match + 1); tv_dict_add_nr(retdict, S_LEN("bufnr"), tag->cur_fnum); + if (tag->user_data) { + tv_dict_add_str(retdict, S_LEN("user_data"), (const char *)tag->user_data); + } pos = tv_list_alloc(4); tv_dict_add_list(retdict, S_LEN("from"), pos); @@ -2996,7 +3283,7 @@ static void tagstack_clear(win_T *wp) { // Free the current tag stack for (int i = 0; i < wp->w_tagstacklen; i++) { - xfree(wp->w_tagstack[i].tagname); + tagstack_clear_entry(&wp->w_tagstack[i]); } wp->w_tagstacklen = 0; wp->w_tagstackidx = 0; @@ -3007,7 +3294,7 @@ static void tagstack_clear(win_T *wp) static void tagstack_shift(win_T *wp) { taggy_T *tagstack = wp->w_tagstack; - xfree(tagstack[0].tagname); + tagstack_clear_entry(&tagstack[0]); for (int i = 1; i < wp->w_tagstacklen; i++) { tagstack[i - 1] = tagstack[i]; } @@ -3021,7 +3308,8 @@ static void tagstack_push_item( int cur_fnum, int cur_match, pos_T mark, - int fnum) + int fnum, + char_u *user_data) { taggy_T *tagstack = wp->w_tagstack; int idx = wp->w_tagstacklen; // top of the stack @@ -3041,6 +3329,7 @@ static void tagstack_push_item( } tagstack[idx].fmark.mark = mark; tagstack[idx].fmark.fnum = fnum; + tagstack[idx].user_data = user_data; } // Add a list of items to the tag stack in the specified window @@ -3076,10 +3365,13 @@ static void tagstack_push_items(win_T *wp, list_T *l) if (mark.col > 0) { mark.col--; } - tagstack_push_item(wp, tagname, - (int)tv_dict_get_number(itemdict, "bufnr"), - (int)tv_dict_get_number(itemdict, "matchnr") - 1, - mark, fnum); + tagstack_push_item( + wp, + tagname, + (int)tv_dict_get_number(itemdict, "bufnr"), + (int)tv_dict_get_number(itemdict, "matchnr") - 1, + mark, fnum, + (char_u *)tv_dict_get_string(itemdict, "user_data", true)); } } @@ -3103,6 +3395,12 @@ int set_tagstack(win_T *wp, dict_T *d, int action) dictitem_T *di; list_T *l; + // not allowed to alter the tag stack entries from inside tagfunc + if (tfu_in_use) { + EMSG(_(recurmsg)); + return FAIL; + } + if ((di = tv_dict_find(d, "items", -1)) != NULL) { if (di->di_tv.v_type != VAR_LIST) { return FAIL; diff --git a/src/nvim/tag.h b/src/nvim/tag.h index a8fddd05da..9f671043b3 100644 --- a/src/nvim/tag.h +++ b/src/nvim/tag.h @@ -20,20 +20,21 @@ #define DT_LTAG 11 /* tag using location list */ #define DT_FREE 99 /* free cached matches */ -/* - * flags for find_tags(). - */ -#define TAG_HELP 1 /* only search for help tags */ -#define TAG_NAMES 2 /* only return name of tag */ -#define TAG_REGEXP 4 /* use tag pattern as regexp */ -#define TAG_NOIC 8 /* don't always ignore case */ -#define TAG_CSCOPE 16 /* cscope tag */ -#define TAG_VERBOSE 32 /* message verbosity */ -#define TAG_INS_COMP 64 /* Currently doing insert completion */ -#define TAG_KEEP_LANG 128 /* keep current language */ +// +// flags for find_tags(). +// +#define TAG_HELP 1 // only search for help tags +#define TAG_NAMES 2 // only return name of tag +#define TAG_REGEXP 4 // use tag pattern as regexp +#define TAG_NOIC 8 // don't always ignore case +#define TAG_CSCOPE 16 // cscope tag +#define TAG_VERBOSE 32 // message verbosity +#define TAG_INS_COMP 64 // Currently doing insert completion +#define TAG_KEEP_LANG 128 // keep current language +#define TAG_NO_TAGFUNC 256 // do not use 'tagfunc' -#define TAG_MANY 300 /* When finding many tags (for completion), - find up to this many tags */ +#define TAG_MANY 300 // When finding many tags (for completion), + // find up to this many tags /* * Structure used for get_tagfname(). diff --git a/src/nvim/testdir/test_alot.vim b/src/nvim/testdir/test_alot.vim index 6bf2e8329c..a871924d32 100644 --- a/src/nvim/testdir/test_alot.vim +++ b/src/nvim/testdir/test_alot.vim @@ -45,6 +45,7 @@ source test_syn_attr.vim source test_tabline.vim source test_tabpage.vim source test_tagcase.vim +source test_tagfunc.vim source test_tagjump.vim source test_taglist.vim source test_true_false.vim diff --git a/src/nvim/testdir/test_tagfunc.vim b/src/nvim/testdir/test_tagfunc.vim new file mode 100644 index 0000000000..242aa3a235 --- /dev/null +++ b/src/nvim/testdir/test_tagfunc.vim @@ -0,0 +1,84 @@ +" Test 'tagfunc' + +func TagFunc(pat, flag, info) + let g:tagfunc_args = [a:pat, a:flag, a:info] + let tags = [] + for num in range(1,10) + let tags += [{ + \ 'cmd': '2', 'name': 'nothing'.num, 'kind': 'm', + \ 'filename': 'Xfile1', 'user_data': 'somedata'.num, + \}] + endfor + return tags +endfunc + +func Test_tagfunc() + set tagfunc=TagFunc + new Xfile1 + call setline(1, ['empty', 'one()', 'empty']) + write + + call assert_equal({'cmd': '2', 'static': 0, + \ 'name': 'nothing2', 'user_data': 'somedata2', + \ 'kind': 'm', 'filename': 'Xfile1'}, taglist('.')[1]) + + call settagstack(win_getid(), {'items': []}) + + tag arbitrary + call assert_equal('arbitrary', g:tagfunc_args[0]) + call assert_equal('', g:tagfunc_args[1]) + call assert_equal('somedata1', gettagstack().items[0].user_data) + 5tag arbitrary + call assert_equal('arbitrary', g:tagfunc_args[0]) + call assert_equal('', g:tagfunc_args[1]) + call assert_equal('somedata5', gettagstack().items[1].user_data) + pop + tag + call assert_equal('arbitrary', g:tagfunc_args[0]) + call assert_equal('', g:tagfunc_args[1]) + call assert_equal('somedata5', gettagstack().items[1].user_data) + + let g:tagfunc_args=[] + execute "normal! \" + call assert_equal('one', g:tagfunc_args[0]) + call assert_equal('c', g:tagfunc_args[1]) + + set cpt=t + let g:tagfunc_args=[] + execute "normal! i\\" + call assert_equal('ci', g:tagfunc_args[1]) + call assert_equal('nothing1', getline('.')[0:7]) + + func BadTagFunc1(...) + return 0 + endfunc + func BadTagFunc2(...) + return [1] + endfunc + func BadTagFunc3(...) + return [{'name': 'foo'}] + endfunc + + for &tagfunc in ['BadTagFunc1', 'BadTagFunc2', 'BadTagFunc3'] + try + tag nothing + call assert_false(1, 'tag command should have failed') + catch + call assert_exception('E987:') + endtry + exe 'delf' &tagfunc + endfor + + func NullTagFunc(...) + return v:null + endfunc + set tags= tfu=NullTagFunc + call assert_fails('tag nothing', 'E426') + delf NullTagFunc + + bwipe! + set tags& tfu& cpt& + call delete('Xfile1') +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/window.c b/src/nvim/window.c index 4d8eaa9dcc..976f1d8ff0 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -70,8 +70,8 @@ static char *m_onlyone = N_("Already only one window"); /* * all CTRL-W window commands are handled here, called from normal_cmd(). */ -void -do_window ( +void +do_window( int nchar, long Prenum, int xchar /* extra char from ":wincmd gx" or NUL */ @@ -1537,10 +1537,14 @@ static void win_init(win_T *newp, win_T *oldp, int flags) /* copy tagstack and folds */ for (i = 0; i < oldp->w_tagstacklen; i++) { - newp->w_tagstack[i] = oldp->w_tagstack[i]; - if (newp->w_tagstack[i].tagname != NULL) - newp->w_tagstack[i].tagname = - vim_strsave(newp->w_tagstack[i].tagname); + taggy_T *tag = &newp->w_tagstack[i]; + *tag = oldp->w_tagstack[i]; + if (tag->tagname != NULL) { + tag->tagname = vim_strsave(tag->tagname); + } + if (tag->user_data != NULL) { + tag->user_data = vim_strsave(tag->user_data); + } } newp->w_tagstackidx = oldp->w_tagstackidx; newp->w_tagstacklen = oldp->w_tagstacklen; From 3b6b528ea98ca7bf8cd5ae1cf103203e3ca67814 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 10 Oct 2019 23:40:57 +0100 Subject: [PATCH 0201/1293] vim-patch:8.1.1962: leaking memory when using tagfunc() Problem: Leaking memory when using tagfunc(). Solution: Free the user_data. (Dominique Pelle, closes vim/vim#4886) https://github.com/vim/vim/commit/55008aad50601cae079037fda8fb434cde70c0f4 --- src/nvim/window.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nvim/window.c b/src/nvim/window.c index 976f1d8ff0..ce5be8e904 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -4642,8 +4642,10 @@ win_free ( xfree(wp->w_lines); - for (i = 0; i < wp->w_tagstacklen; ++i) + for (i = 0; i < wp->w_tagstacklen; i++) { xfree(wp->w_tagstack[i].tagname); + xfree(wp->w_tagstack[i].user_data); + } xfree(wp->w_localdir); From c2fc4255f92157b65a2f0d5b299027195541d6b8 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Wed, 23 Oct 2019 10:50:42 +0900 Subject: [PATCH 0202/1293] runtime: Use module pattern with vim/shared.lua It's a bit cumbersome for us to add an export target every time we define a new function. It's also cumbersome to care about the order of definition when creating a new function by referring to other functions in the module. --- runtime/lua/vim/shared.lua | 75 +++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 220b6c6c7c..7727fdbab0 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -4,34 +4,37 @@ -- test-suite. If, in the future, Nvim itself is used to run the test-suite -- instead of "vanilla Lua", these functions could move to src/nvim/lua/vim.lua +local vim = {} --- Returns a deep copy of the given object. Non-table objects are copied as --- in a typical Lua assignment, whereas table objects are copied recursively. --- --@param orig Table to copy --@returns New table of copied keys and (nested) values. -local function deepcopy(orig) - error(orig) -end -local function _id(v) - return v -end -local deepcopy_funcs = { - table = function(orig) - local copy = {} - for k, v in pairs(orig) do - copy[deepcopy(k)] = deepcopy(v) - end - return copy - end, - number = _id, - string = _id, - ['nil'] = _id, - boolean = _id, -} -deepcopy = function(orig) - return deepcopy_funcs[type(orig)](orig) -end +function vim.deepcopy(orig) end -- luacheck: no unused +vim.deepcopy = (function() + local function _id(v) + return v + end + + local deepcopy_funcs = { + table = function(orig) + local copy = {} + for k, v in pairs(orig) do + copy[vim.deepcopy(k)] = vim.deepcopy(v) + end + return copy + end, + number = _id, + string = _id, + ['nil'] = _id, + boolean = _id, + } + + return function(orig) + return deepcopy_funcs[type(orig)](orig) + end +end)() --- Splits a string at each instance of a separator. --- @@ -43,7 +46,7 @@ end --@param sep Separator string or pattern --@param plain If `true` use `sep` literally (passed to String.find) --@returns Iterator over the split components -local function gsplit(s, sep, plain) +function vim.gsplit(s, sep, plain) assert(type(s) == "string", string.format("Expected string, got %s", type(s))) assert(type(sep) == "string", string.format("Expected string, got %s", type(sep))) assert(type(plain) == "boolean" or type(plain) == "nil", string.format("Expected boolean or nil, got %s", type(plain))) @@ -92,8 +95,8 @@ end --@param sep Separator string or pattern --@param plain If `true` use `sep` literally (passed to String.find) --@returns List-like table of the split components. -local function split(s,sep,plain) - local t={} for c in gsplit(s, sep, plain) do table.insert(t,c) end +function vim.split(s,sep,plain) + local t={} for c in vim.gsplit(s, sep, plain) do table.insert(t,c) end return t end @@ -102,7 +105,7 @@ end --@param t Table to check --@param value Value to compare --@returns true if `t` contains `value` -local function tbl_contains(t, value) +function vim.tbl_contains(t, value) assert(type(t) == 'table', string.format("Expected table, got %s", type(t))) for _,v in ipairs(t) do @@ -122,7 +125,7 @@ end --- - "keep": use value from the leftmost map --- - "force": use value from the rightmost map --@param ... Two or more map-like tables. -local function tbl_extend(behavior, ...) +function vim.tbl_extend(behavior, ...) if (behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force') then error('invalid "behavior": '..tostring(behavior)) end @@ -149,7 +152,7 @@ end --- --@param t List-like table --@returns Flattened copy of the given list-like table. -local function tbl_flatten(t) +function vim.tbl_flatten(t) -- From https://github.com/premake/premake-core/blob/master/src/base/table.lua local result = {} local function _tbl_flatten(_t) @@ -172,7 +175,7 @@ end --@see https://www.lua.org/pil/20.2.html --@param s String to trim --@returns String with whitespace removed from its beginning and end -local function trim(s) +function vim.trim(s) assert(type(s) == 'string', string.format("Expected string, got %s", type(s))) return s:match('^%s*(.*%S)') or '' end @@ -182,19 +185,9 @@ end --@see https://github.com/rxi/lume --@param s String to escape --@returns %-escaped pattern string -local function pesc(s) +function vim.pesc(s) assert(type(s) == 'string', string.format("Expected string, got %s", type(s))) return s:gsub('[%(%)%.%%%+%-%*%?%[%]%^%$]', '%%%1') end -local module = { - deepcopy = deepcopy, - gsplit = gsplit, - pesc = pesc, - split = split, - tbl_contains = tbl_contains, - tbl_extend = tbl_extend, - tbl_flatten = tbl_flatten, - trim = trim, -} -return module +return vim From 6dceaf33613cc4d1ff361053e65ce801ce2678cf Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 12 Oct 2019 18:25:27 -0400 Subject: [PATCH 0203/1293] vim-patch:8.1.0014: qf_init_ext() is too long Problem: qf_init_ext() is too long. Solution: Split it into multiple functions. (Yegappan Lakshmanan, closes vim/vim#2939) https://github.com/vim/vim/commit/6053f2d29a979ffed1fe01b0a2f28e23750530e9 --- src/nvim/memline.c | 1 + src/nvim/quickfix.c | 269 ++++++++++++++++++++++++++------------------ 2 files changed, 163 insertions(+), 107 deletions(-) diff --git a/src/nvim/memline.c b/src/nvim/memline.c index b85c23e50f..05cc62bb33 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -1929,6 +1929,7 @@ int ml_append_buf( colnr_T len, // length of new line, including NUL, or 0 bool newfile // flag, see above ) + FUNC_ATTR_NONNULL_ARG(1) { if (buf->b_ml.ml_mfp == NULL) return FAIL; diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index c5e8d4b490..c220516270 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -229,8 +229,9 @@ static char *e_loc_list_changed = N_("E926: Current location list was changed"); /// @params enc If non-NULL, encoding used to parse errors /// /// @returns -1 for error, number of errors for success. -int qf_init(win_T *wp, char_u *efile, char_u *errorformat, int newlist, - char_u *qf_title, char_u *enc) +int qf_init(win_T *wp, const char_u *restrict efile, + char_u *restrict errorformat, int newlist, + const char_u *restrict qf_title, char_u *restrict enc) { qf_info_T *qi = &ql_info; @@ -880,6 +881,79 @@ restofline: return QF_OK; } +// Allocate the fields used for parsing lines and populating a quickfix list. +static void qf_alloc_fields(qffields_T *pfields) + FUNC_ATTR_NONNULL_ALL +{ + pfields->namebuf = xmalloc(CMDBUFFSIZE + 1); + pfields->module = xmalloc(CMDBUFFSIZE + 1); + pfields->errmsglen = CMDBUFFSIZE + 1; + pfields->errmsg = xmalloc(pfields->errmsglen); + pfields->pattern = xmalloc(CMDBUFFSIZE + 1); +} + +// Free the fields used for parsing lines and populating a quickfix list. +static void qf_free_fields(qffields_T *pfields) + FUNC_ATTR_NONNULL_ALL +{ + xfree(pfields->namebuf); + xfree(pfields->module); + xfree(pfields->errmsg); + xfree(pfields->pattern); +} + +// Setup the state information used for parsing lines and populating a +// quickfix list. +static int qf_setup_state( + qfstate_T *pstate, + char_u *restrict enc, + const char_u *restrict efile, + typval_T *tv, + buf_T *buf, + linenr_T lnumfirst, + linenr_T lnumlast) + FUNC_ATTR_NONNULL_ARG(1) +{ + pstate->vc.vc_type = CONV_NONE; + if (enc != NULL && *enc != NUL) { + convert_setup(&pstate->vc, enc, p_enc); + } + + if (efile != NULL + && (pstate->fd = os_fopen((const char *)efile, "r")) == NULL) { + EMSG2(_(e_openerrf), efile); + return FAIL; + } + + if (tv != NULL) { + if (tv->v_type == VAR_STRING) { + pstate->p_str = tv->vval.v_string; + } else if (tv->v_type == VAR_LIST) { + pstate->p_li = tv_list_first(tv->vval.v_list); + } + pstate->tv = tv; + } + pstate->buf = buf; + pstate->buflnum = lnumfirst; + pstate->lnumlast = lnumlast; + + return OK; +} + +// Cleanup the state information used for parsing lines and populating a +// quickfix list. +static void qf_cleanup_state(qfstate_T *pstate) + FUNC_ATTR_NONNULL_ALL +{ + if (pstate->fd != NULL) { + fclose(pstate->fd); + } + xfree(pstate->growbuf); + if (pstate->vc.vc_type != CONV_NONE) { + convert_setup(&pstate->vc, NULL, NULL); + } +} + // Read the errorfile "efile" into memory, line by line, building the error // list. // Alternative: when "efile" is NULL read errors from buffer "buf". @@ -892,19 +966,20 @@ static int qf_init_ext( qf_info_T *qi, int qf_idx, - char_u *efile, + const char_u *restrict efile, buf_T *buf, typval_T *tv, - char_u *errorformat, - int newlist, // TRUE: start a new error list + char_u *restrict errorformat, + bool newlist, // true: start a new error list linenr_T lnumfirst, // first line number to use linenr_T lnumlast, // last line number to use - char_u *qf_title, - char_u *enc + const char_u *restrict qf_title, + char_u *restrict enc ) + FUNC_ATTR_NONNULL_ARG(1) { - qfstate_T state; - qffields_T fields; + qfstate_T state = { 0 }; + qffields_T fields = { 0 }; qfline_T *old_last = NULL; bool adding = false; static efm_T *fmt_first = NULL; @@ -916,21 +991,9 @@ qf_init_ext( // Do not used the cached buffer, it may have been wiped out. XFREE_CLEAR(qf_last_bufname); - memset(&state, 0, sizeof(state)); - memset(&fields, 0, sizeof(fields)); - state.vc.vc_type = CONV_NONE; - if (enc != NULL && *enc != NUL) { - convert_setup(&state.vc, enc, p_enc); - } - - fields.namebuf = xmalloc(CMDBUFFSIZE + 1); - fields.module = xmalloc(CMDBUFFSIZE + 1); - fields.errmsglen = CMDBUFFSIZE + 1; - fields.errmsg = xmalloc(fields.errmsglen); - fields.pattern = xmalloc(CMDBUFFSIZE + 1); - - if (efile != NULL && (state.fd = os_fopen((char *)efile, "r")) == NULL) { - EMSG2(_(e_openerrf), efile); + qf_alloc_fields(&fields); + if (qf_setup_state(&state, enc, efile, tv, buf, + lnumfirst, lnumlast) == FAIL) { goto qf_init_end; } @@ -979,19 +1042,6 @@ qf_init_ext( */ got_int = FALSE; - if (tv != NULL) { - if (tv->v_type == VAR_STRING) { - state.p_str = tv->vval.v_string; - } else if (tv->v_type == VAR_LIST) { - state.p_list = tv->vval.v_list; - state.p_li = tv_list_first(tv->vval.v_list); - } - state.tv = tv; - } - state.buf = buf; - state.buflnum = lnumfirst; - state.lnumlast = lnumlast; - /* * Read the lines in the error file one by one. * Try to recognize one of the error formats in each line. @@ -1059,22 +1109,11 @@ error2: } } qf_init_end: - if (state.fd != NULL) { - fclose(state.fd); - } - xfree(fields.namebuf); - xfree(fields.module); - xfree(fields.errmsg); - xfree(fields.pattern); - xfree(state.growbuf); - if (qf_idx == qi->qf_curlist) { qf_update_buffer(qi, old_last); } - - if (state.vc.vc_type != CONV_NONE) { - convert_setup(&state.vc, NULL, NULL); - } + qf_cleanup_state(&state); + qf_free_fields(&fields); return retval; } @@ -1111,7 +1150,7 @@ static char_u * qf_cmdtitle(char_u *cmd) // Prepare for adding a new quickfix list. If the current list is in the // middle of the stack, then all the following lists are freed and then // the new list is added. -static void qf_new_list(qf_info_T *qi, char_u *qf_title) +static void qf_new_list(qf_info_T *qi, const char_u *qf_title) { int i; @@ -2760,10 +2799,12 @@ next_entry: * Remove newlines and leading whitespace from an error message. * Put the result in "buf[bufsize]". */ -static void qf_fmt_text(char_u *text, char_u *buf, int bufsize) +static void qf_fmt_text(const char_u *restrict text, char_u *restrict buf, + int bufsize) + FUNC_ATTR_NONNULL_ALL { int i; - char_u *p = text; + const char_u *p = text; for (i = 0; *p != NUL && i < bufsize - 1; ++i) { if (*p == '\n') { @@ -3415,17 +3456,82 @@ static void qf_set_title_var(qf_info_T *qi) } } +// Add an error line to the quickfix buffer. +static int qf_buf_add_line(buf_T *buf, linenr_T lnum, const qfline_T *qfp, + char_u *dirname) + FUNC_ATTR_NONNULL_ALL +{ + int len; + buf_T *errbuf; + + if (qfp->qf_module != NULL) { + STRCPY(IObuff, qfp->qf_module); + len = (int)STRLEN(IObuff); + } else if (qfp->qf_fnum != 0 + && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL + && errbuf->b_fname != NULL) { + if (qfp->qf_type == 1) { // :helpgrep + STRLCPY(IObuff, path_tail(errbuf->b_fname), sizeof(IObuff)); + } else { + // shorten the file name if not done already + if (errbuf->b_sfname == NULL + || path_is_absolute(errbuf->b_sfname)) { + if (*dirname == NUL) { + os_dirname(dirname, MAXPATHL); + } + shorten_buf_fname(errbuf, dirname, false); + } + STRLCPY(IObuff, errbuf->b_fname, sizeof(IObuff)); + } + len = (int)STRLEN(IObuff); + } else { + len = 0; + } + IObuff[len++] = '|'; + + if (qfp->qf_lnum > 0) { + snprintf((char *)IObuff + len, sizeof(IObuff), "%" PRId64, + (int64_t)qfp->qf_lnum); + len += (int)STRLEN(IObuff + len); + + if (qfp->qf_col > 0) { + snprintf((char *)IObuff + len, sizeof(IObuff), " col %d", qfp->qf_col); + len += (int)STRLEN(IObuff + len); + } + + snprintf((char *)IObuff + len, sizeof(IObuff), "%s", + (char *)qf_types(qfp->qf_type, qfp->qf_nr)); + len += (int)STRLEN(IObuff + len); + } else if (qfp->qf_pattern != NULL) { + qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len); + len += (int)STRLEN(IObuff + len); + } + IObuff[len++] = '|'; + IObuff[len++] = ' '; + + // Remove newlines and leading whitespace from the text. + // For an unrecognized line keep the indent, the compiler may + // mark a word with ^^^^. + qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text, + IObuff + len, IOSIZE - len); + + if (ml_append_buf(buf, lnum, IObuff, + (colnr_T)STRLEN(IObuff) + 1, false) == FAIL) { + return FAIL; + } + return OK; +} + // Fill current buffer with quickfix errors, replacing any previous contents. // curbuf must be the quickfix buffer! // If "old_last" is not NULL append the items after this one. // When "old_last" is NULL then "buf" must equal "curbuf"! Because ml_delete() // is used and autocommands will be triggered. static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last) + FUNC_ATTR_NONNULL_ARG(1, 2) { linenr_T lnum; qfline_T *qfp; - buf_T *errbuf; - int len; const bool old_KeyTyped = KeyTyped; if (old_last == NULL) { @@ -3455,58 +3561,7 @@ static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last) lnum = buf->b_ml.ml_line_count; } while (lnum < qi->qf_lists[qi->qf_curlist].qf_count) { - if (qfp->qf_module != NULL) { - STRCPY(IObuff, qfp->qf_module); - len = (int)STRLEN(IObuff); - } else if (qfp->qf_fnum != 0 - && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL - && errbuf->b_fname != NULL) { - if (qfp->qf_type == 1) { // :helpgrep - STRLCPY(IObuff, path_tail(errbuf->b_fname), sizeof(IObuff)); - } else { - // shorten the file name if not done already - if (errbuf->b_sfname == NULL - || path_is_absolute(errbuf->b_sfname)) { - if (*dirname == NUL) { - os_dirname(dirname, MAXPATHL); - } - shorten_buf_fname(errbuf, dirname, false); - } - STRLCPY(IObuff, errbuf->b_fname, sizeof(IObuff)); - } - len = (int)STRLEN(IObuff); - } else { - len = 0; - } - IObuff[len++] = '|'; - - if (qfp->qf_lnum > 0) { - sprintf((char *)IObuff + len, "%" PRId64, (int64_t)qfp->qf_lnum); - len += (int)STRLEN(IObuff + len); - - if (qfp->qf_col > 0) { - sprintf((char *)IObuff + len, " col %d", qfp->qf_col); - len += (int)STRLEN(IObuff + len); - } - - sprintf((char *)IObuff + len, "%s", - (char *)qf_types(qfp->qf_type, qfp->qf_nr)); - len += (int)STRLEN(IObuff + len); - } else if (qfp->qf_pattern != NULL) { - qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len); - len += (int)STRLEN(IObuff + len); - } - IObuff[len++] = '|'; - IObuff[len++] = ' '; - - /* Remove newlines and leading whitespace from the text. - * For an unrecognized line keep the indent, the compiler may - * mark a word with ^^^^. */ - qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text, - IObuff + len, IOSIZE - len); - - if (ml_append_buf(buf, lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, false) - == FAIL) { + if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL) { break; } lnum++; From 5e02bd071ed054b52ca7e53536d4b5cd594737eb Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 13 Oct 2019 19:19:32 -0400 Subject: [PATCH 0204/1293] vim-patch:8.1.0288: quickfix code uses cmdidx too often Problem: Quickfix code uses cmdidx too often. Solution: Add is_loclist_cmd(). (Yegappan Lakshmanan) https://github.com/vim/vim/commit/396659592fe039decc8c088694912067fe32a681 --- src/nvim/ex_docmd.c | 11 ++++ src/nvim/quickfix.c | 122 +++++++++++++++++++++++--------------------- 2 files changed, 76 insertions(+), 57 deletions(-) diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index ae3fb4fbfb..16751b3a53 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -10139,6 +10139,17 @@ static void ex_folddo(exarg_T *eap) ml_clearmarked(); // clear rest of the marks } +// Returns true if the supplied Ex cmdidx is for a location list command +// instead of a quickfix command. +bool is_loclist_cmd(int cmdidx) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + if (cmdidx < 0 || cmdidx > CMD_SIZE) { + return false; + } + return cmdnames[cmdidx].cmd_name[0] == 'l'; +} + bool get_pressedreturn(void) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index c220516270..4483ada3e4 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -2647,7 +2647,7 @@ void qf_list(exarg_T *eap) // recognised errors qf_info_T *qi = &ql_info; - if (eap->cmdidx == CMD_llist) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); if (qi == NULL) { EMSG(_(e_loclist)); @@ -2856,7 +2856,7 @@ void qf_age(exarg_T *eap) qf_info_T *qi = &ql_info; int count; - if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); if (qi == NULL) { EMSG(_(e_loclist)); @@ -2895,7 +2895,7 @@ void qf_history(exarg_T *eap) qf_info_T *qi = &ql_info; int i; - if (eap->cmdidx == CMD_lhistory) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); } if (qi == NULL || (qi->qf_listcount == 0 @@ -3097,7 +3097,7 @@ void ex_cwindow(exarg_T *eap) qf_info_T *qi = &ql_info; win_T *win; - if (eap->cmdidx == CMD_lwindow) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); if (qi == NULL) return; @@ -3129,7 +3129,7 @@ void ex_cclose(exarg_T *eap) win_T *win = NULL; qf_info_T *qi = &ql_info; - if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); if (qi == NULL) return; @@ -3155,7 +3155,7 @@ void ex_copen(exarg_T *eap) buf_T *qf_buf; win_T *oldwin = curwin; - if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); if (qi == NULL) { EMSG(_(e_loclist)); @@ -3284,7 +3284,7 @@ void ex_cbottom(exarg_T *eap) { qf_info_T *qi = &ql_info; - if (eap->cmdidx == CMD_lbottom) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); if (qi == NULL) { EMSG(_(e_loclist)); @@ -3700,9 +3700,9 @@ void ex_make(exarg_T *eap) } } - if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep - || eap->cmdidx == CMD_lgrepadd) + if (is_loclist_cmd(eap->cmdidx)) { wp = curwin; + } autowrite_all(); fname = get_mef_name(); @@ -3817,7 +3817,7 @@ size_t qf_get_size(exarg_T *eap) FUNC_ATTR_NONNULL_ALL { qf_info_T *qi = &ql_info; - if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) { + if (is_loclist_cmd(eap->cmdidx)) { // Location list. qi = GET_LOC_LIST(curwin); if (qi == NULL) { @@ -3857,7 +3857,7 @@ size_t qf_get_cur_idx(exarg_T *eap) { qf_info_T *qi = &ql_info; - if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) { + if (is_loclist_cmd(eap->cmdidx)) { // Location list. qi = GET_LOC_LIST(curwin); if (qi == NULL) { @@ -3877,7 +3877,7 @@ int qf_get_cur_valid_idx(exarg_T *eap) { qf_info_T *qi = &ql_info; - if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) { + if (is_loclist_cmd(eap->cmdidx)) { // Location list. qi = GET_LOC_LIST(curwin); if (qi == NULL) { @@ -3970,12 +3970,7 @@ void ex_cc(exarg_T *eap) { qf_info_T *qi = &ql_info; - if (eap->cmdidx == CMD_ll - || eap->cmdidx == CMD_lrewind - || eap->cmdidx == CMD_lfirst - || eap->cmdidx == CMD_llast - || eap->cmdidx == CMD_ldo - || eap->cmdidx == CMD_lfdo) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); if (qi == NULL) { EMSG(_(e_loclist)); @@ -3986,13 +3981,22 @@ void ex_cc(exarg_T *eap) int errornr; if (eap->addr_count > 0) { errornr = (int)eap->line2; - } else if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll) { - errornr = 0; - } else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind - || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst) { - errornr = 1; } else { - errornr = 32767; + switch (eap->cmdidx) { + case CMD_cc: + case CMD_ll: + errornr = 0; + break; + case CMD_crewind: + case CMD_lrewind: + case CMD_cfirst: + case CMD_lfirst: + errornr = 1; + break; + default: + errornr = 32767; + break; + } } // For cdo and ldo commands, jump to the nth valid error. @@ -4024,14 +4028,7 @@ void ex_cnext(exarg_T *eap) { qf_info_T *qi = &ql_info; - if (eap->cmdidx == CMD_lnext - || eap->cmdidx == CMD_lNext - || eap->cmdidx == CMD_lprevious - || eap->cmdidx == CMD_lnfile - || eap->cmdidx == CMD_lNfile - || eap->cmdidx == CMD_lpfile - || eap->cmdidx == CMD_ldo - || eap->cmdidx == CMD_lfdo) { + if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); if (qi == NULL) { EMSG(_(e_loclist)); @@ -4048,17 +4045,37 @@ void ex_cnext(exarg_T *eap) errornr = 1; } - qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext - || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo) - ? FORWARD - : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile - || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) - ? FORWARD_FILE - : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile - || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile) - ? BACKWARD_FILE - : BACKWARD, - errornr, eap->forceit); + // Depending on the command jump to either next or previous entry/file. + Direction dir; + switch (eap->cmdidx) { + case CMD_cprevious: + case CMD_lprevious: + case CMD_cNext: + case CMD_lNext: + dir = BACKWARD; + break; + case CMD_cnfile: + case CMD_lnfile: + case CMD_cfdo: + case CMD_lfdo: + dir = FORWARD_FILE; + break; + case CMD_cpfile: + case CMD_lpfile: + case CMD_cNfile: + case CMD_lNfile: + dir = BACKWARD_FILE; + break; + case CMD_cnext: + case CMD_lnext: + case CMD_cdo: + case CMD_ldo: + default: + dir = FORWARD; + break; + } + + qf_jump(qi, dir, errornr, eap->forceit); } /* @@ -4087,9 +4104,7 @@ void ex_cfile(exarg_T *eap) char_u *enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc; - if (eap->cmdidx == CMD_lfile - || eap->cmdidx == CMD_lgetfile - || eap->cmdidx == CMD_laddfile) { + if (is_loclist_cmd(eap->cmdidx)) { wp = curwin; } @@ -4346,10 +4361,7 @@ void ex_vimgrep(exarg_T *eap) } } - if (eap->cmdidx == CMD_lgrep - || eap->cmdidx == CMD_lvimgrep - || eap->cmdidx == CMD_lgrepadd - || eap->cmdidx == CMD_lvimgrepadd) { + if (is_loclist_cmd(eap->cmdidx)) { qi = ll_get_or_alloc_list(curwin); wp = curwin; } @@ -5575,9 +5587,7 @@ void ex_cbuffer(exarg_T *eap) } // Must come after autocommands. - if (eap->cmdidx == CMD_lbuffer - || eap->cmdidx == CMD_lgetbuffer - || eap->cmdidx == CMD_laddbuffer) { + if (is_loclist_cmd(eap->cmdidx)) { qi = ll_get_or_alloc_list(curwin); wp = curwin; } @@ -5677,9 +5687,7 @@ void ex_cexpr(exarg_T *eap) } } - if (eap->cmdidx == CMD_lexpr - || eap->cmdidx == CMD_lgetexpr - || eap->cmdidx == CMD_laddexpr) { + if (is_loclist_cmd(eap->cmdidx)) { qi = ll_get_or_alloc_list(curwin); wp = curwin; } @@ -5878,7 +5886,7 @@ void ex_helpgrep(exarg_T *eap) char_u *const save_cpo = p_cpo; p_cpo = empty_option; - if (eap->cmdidx == CMD_lhelpgrep) { + if (is_loclist_cmd(eap->cmdidx)) { qi = hgr_get_ll(&new_qi); } From f3d6d8750b70d56f92d6ece87031ed205abdfcca Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 13 Oct 2019 19:49:17 -0400 Subject: [PATCH 0205/1293] vim-patch:8.1.0345: cannot get the window id associated with the location list Problem: Cannot get the window id associated with the location list. Solution: Add the "filewinid" argument to getloclist(). (Yegappan Lakshmanan, closes vim/vim#3202) https://github.com/vim/vim/commit/c9cc9c78f21caba7ecb5c90403df5e19a57aa96a --- runtime/doc/eval.txt | 4 +++ src/nvim/quickfix.c | 40 ++++++++++++++++++++++++-- src/nvim/testdir/test_quickfix.vim | 46 ++++++++++++++++++++++++++++-- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 77b6ee24a4..d21e441888 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -4553,6 +4553,10 @@ getloclist({nr},[, {what}]) *getloclist()* If the optional {what} dictionary argument is supplied, then returns the items listed in {what} as a dictionary. Refer to |getqflist()| for the supported items in {what}. + If {what} contains 'filewinid', then returns the id of the + window used to display files from the location list. This + field is applicable only when called from a location list + window. getmatches() *getmatches()* Returns a |List| with all matches previously defined for the diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 4483ada3e4..13e8a73af4 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -4809,7 +4809,8 @@ enum { QF_GETLIST_IDX = 0x40, QF_GETLIST_SIZE = 0x80, QF_GETLIST_TICK = 0x100, - QF_GETLIST_ALL = 0x1FF + QF_GETLIST_FILEWINID = 0x200, + QF_GETLIST_ALL = 0x3FF, }; /// Parse text from 'di' and return the quickfix list items. @@ -4865,12 +4866,17 @@ static int qf_winid(qf_info_T *qi) } /// Convert the keys in 'what' to quickfix list property flags. -static int qf_getprop_keys2flags(dict_T *what) +static int qf_getprop_keys2flags(const dict_T *what, bool loclist) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { int flags = QF_GETLIST_NONE; if (tv_dict_find(what, S_LEN("all")) != NULL) { flags |= QF_GETLIST_ALL; + if (!loclist) { + // File window ID is applicable only to location list windows + flags &= ~QF_GETLIST_FILEWINID; + } } if (tv_dict_find(what, S_LEN("title")) != NULL) { flags |= QF_GETLIST_TITLE; @@ -4899,6 +4905,9 @@ static int qf_getprop_keys2flags(dict_T *what) if (tv_dict_find(what, S_LEN("changedtick")) != NULL) { flags |= QF_GETLIST_TICK; } + if (loclist && tv_dict_find(what, S_LEN("filewinid")) != NULL) { + flags |= QF_GETLIST_FILEWINID; + } return flags; } @@ -4984,6 +4993,9 @@ static int qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict) if ((status == OK) && (flags & QF_GETLIST_TICK)) { status = tv_dict_add_nr(retdict, S_LEN("changedtick"), 0); } + if ((status == OK) && (qi != &ql_info) && (flags & QF_GETLIST_FILEWINID)) { + status = tv_dict_add_nr(retdict, S_LEN("filewinid"), 0); + } return status; } @@ -4995,6 +5007,25 @@ static int qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict) (const char *)qi->qf_lists[qf_idx].qf_title); } +// Returns the identifier of the window used to display files from a location +// list. If there is no associated window, then returns 0. Useful only when +// called from a location list window. +static int qf_getprop_filewinid(const win_T *wp, const qf_info_T *qi, + dict_T *retdict) + FUNC_ATTR_NONNULL_ARG(3) +{ + handle_T winid = 0; + + if (wp != NULL && IS_LL_WINDOW(wp)) { + win_T *ll_wp = qf_find_win_with_loclist(qi); + if (ll_wp != NULL) { + winid = ll_wp->handle; + } + } + + return tv_dict_add_nr(retdict, S_LEN("filewinid"), winid); +} + /// Return the quickfix list items/entries as 'items' in retdict static int qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict) { @@ -5053,7 +5084,7 @@ int qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict) qi = GET_LOC_LIST(wp); } - int flags = qf_getprop_keys2flags(what); + const int flags = qf_getprop_keys2flags(what, wp != NULL); if (qi != NULL && qi->qf_listcount != 0) { qf_idx = qf_getprop_qfidx(qi, what); @@ -5093,6 +5124,9 @@ int qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict) status = tv_dict_add_nr(retdict, S_LEN("changedtick"), qi->qf_lists[qf_idx].qf_changedtick); } + if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID)) { + status = qf_getprop_filewinid(wp, qi, retdict); + } return status; } diff --git a/src/nvim/testdir/test_quickfix.vim b/src/nvim/testdir/test_quickfix.vim index fc514fc9e6..4af986ffba 100644 --- a/src/nvim/testdir/test_quickfix.vim +++ b/src/nvim/testdir/test_quickfix.vim @@ -1963,6 +1963,18 @@ func Xproperty_tests(cchar) call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]}) call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title) + " Test for getting id of window associated with a location list window + if a:cchar == 'l' + only + call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid) + let wid = win_getid() + Xopen + call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid) + wincmd w + call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid) + only + endif + " The following used to crash Vim with address sanitizer call g:Xsetlist([], 'f') call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]}) @@ -3075,7 +3087,17 @@ func Xgetlist_empty_tests(cchar) call assert_equal('', g:Xgetlist({'title' : 0}).title) call assert_equal(0, g:Xgetlist({'winid' : 0}).winid) call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick) - call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0, 'changedtick': 0}, g:Xgetlist({'all' : 0})) + if a:cchar == 'c' + call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, + \ 'items' : [], 'nr' : 0, 'size' : 0, + \ 'title' : '', 'winid' : 0, 'changedtick': 0}, + \ g:Xgetlist({'all' : 0})) + else + call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, + \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', + \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0}, + \ g:Xgetlist({'all' : 0})) + endif " Quickfix window with empty stack silent! Xopen @@ -3108,7 +3130,16 @@ func Xgetlist_empty_tests(cchar) call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title) call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid) call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick) - call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0, 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0})) + if a:cchar == 'c' + call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], + \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0, + \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0})) + else + call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], + \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0, + \ 'changedtick' : 0, 'filewinid' : 0}, + \ g:Xgetlist({'id' : qfid, 'all' : 0})) + endif " Non-existing quickfix list number call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context) @@ -3120,7 +3151,16 @@ func Xgetlist_empty_tests(cchar) call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title) call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid) call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick) - call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0, 'changedtick' : 0}, g:Xgetlist({'nr' : 5, 'all' : 0})) + if a:cchar == 'c' + call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], + \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0, + \ 'changedtick' : 0}, g:Xgetlist({'nr' : 5, 'all' : 0})) + else + call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], + \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0, + \ 'changedtick' : 0, 'filewinid' : 0}, + \ g:Xgetlist({'nr' : 5, 'all' : 0})) + endif endfunc func Test_getqflist() From 8257d49ff6bd825e8f4ffca189cd6c23bd0c66be Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Mon, 14 Oct 2019 01:30:53 -0400 Subject: [PATCH 0206/1293] vim-patch:8.1.0410: the ex_copen() function is too long Problem: The ex_copen() function is too long. Solution: Refactor to split off two functions. (Yegappan Lakshmanan) https://github.com/vim/vim/commit/476c0db00205590974395df717519407a7717270 --- src/nvim/quickfix.c | 192 +++++++++++++++++++++++++------------------- src/nvim/window.c | 2 +- 2 files changed, 109 insertions(+), 85 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 13e8a73af4..3f2688809d 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -3142,6 +3142,98 @@ void ex_cclose(exarg_T *eap) } } +// Goto a quickfix or location list window (if present). +// Returns OK if the window is found, FAIL otherwise. +static int qf_goto_cwindow(const qf_info_T *qi, bool resize, int sz, + bool vertsplit) +{ + win_T *const win = qf_find_win(qi); + if (win == NULL) { + return FAIL; + } + + win_goto(win); + if (resize) { + if (vertsplit) { + if (sz != win->w_width) { + win_setwidth(sz); + } + } else if (sz != win->w_height) { + win_setheight(sz); + } + } + + return OK; +} + +// Open a new quickfix or location list window, load the quickfix buffer and +// set the appropriate options for the window. +// Returns FAIL if the window could not be opened. +static int qf_open_new_cwindow(const qf_info_T *qi, int height) +{ + win_T *oldwin = curwin; + const tabpage_T *const prevtab = curtab; + int flags = 0; + + const buf_T *const qf_buf = qf_find_buf(qi); + + // The current window becomes the previous window afterwards. + win_T *const win = curwin; + + if (IS_QF_STACK(qi) && cmdmod.split == 0) { + // Create the new quickfix window at the very bottom, except when + // :belowright or :aboveleft is used. + win_goto(lastwin); + } + // Default is to open the window below the current window + if (cmdmod.split == 0) { + flags = WSP_BELOW; + } + flags |= WSP_NEWLOC; + if (win_split(height, flags) == FAIL) { + return FAIL; // not enough room for window + } + RESET_BINDING(curwin); + + if (IS_LL_STACK(qi)) { + // For the location list window, create a reference to the + // location list from the window 'win'. + curwin->w_llist_ref = win->w_llist; + win->w_llist->qf_refcount++; + } + + if (oldwin != curwin) { + oldwin = NULL; // don't store info when in another window + } + if (qf_buf != NULL) { + // Use the existing quickfix buffer + (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE, + ECMD_HIDE + ECMD_OLDBUF, oldwin); + } else { + // Create a new quickfix buffer + (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin); + + // switch off 'swapfile' + set_option_value("swf", 0L, NULL, OPT_LOCAL); + set_option_value("bt", 0L, "quickfix", OPT_LOCAL); + set_option_value("bh", 0L, "wipe", OPT_LOCAL); + RESET_BINDING(curwin); + curwin->w_p_diff = false; + set_option_value("fdm", 0L, "manual", OPT_LOCAL); + } + + // Only set the height when still in the same tab page and there is no + // window to the side. + if (curtab == prevtab && curwin->w_width == Columns) { + win_setheight(height); + } + curwin->w_p_wfh = true; // set 'winfixheight' + if (win_valid(win)) { + prevwin = win; + } + return OK; +} + /* * ":copen": open a window that shows the list of errors. * ":lopen": open a window that shows the location list. @@ -3150,10 +3242,7 @@ void ex_copen(exarg_T *eap) { qf_info_T *qi = &ql_info; int height; - win_T *win; - tabpage_T *prevtab = curtab; - buf_T *qf_buf; - win_T *oldwin = curwin; + int status = FAIL; if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); @@ -3171,83 +3260,15 @@ void ex_copen(exarg_T *eap) } reset_VIsual_and_resel(); // stop Visual mode - /* - * Find existing quickfix window, or open a new one. - */ - win = qf_find_win(qi); - - if (win != NULL && cmdmod.tab == 0) { - win_goto(win); - if (eap->addr_count != 0) { - if (cmdmod.split & WSP_VERT) { - if (height != win->w_width) { - win_setwidth(height); - } - } else { - if (height != win->w_height) { - win_setheight(height); - } - } + // Find an existing quickfix window, or open a new one. + if (cmdmod.tab == 0) { + status = qf_goto_cwindow(qi, eap->addr_count != 0, height, + cmdmod.split & WSP_VERT); + } + if (status == FAIL) { + if (qf_open_new_cwindow(qi, height) == FAIL) { + return; } - } else { - int flags = 0; - - qf_buf = qf_find_buf(qi); - - /* The current window becomes the previous window afterwards. */ - win = curwin; - - if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow) - && cmdmod.split == 0) - // Create the new quickfix window at the very bottom, except when - // :belowright or :aboveleft is used. - win_goto(lastwin); - // Default is to open the window below the current window - if (cmdmod.split == 0) { - flags = WSP_BELOW; - } - flags |= WSP_NEWLOC; - if (win_split(height, flags) == FAIL) { - return; // not enough room for window - } - RESET_BINDING(curwin); - - if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow) { - /* - * For the location list window, create a reference to the - * location list from the window 'win'. - */ - curwin->w_llist_ref = win->w_llist; - win->w_llist->qf_refcount++; - } - - if (oldwin != curwin) - oldwin = NULL; /* don't store info when in another window */ - if (qf_buf != NULL) - /* Use the existing quickfix buffer */ - (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE, - ECMD_HIDE + ECMD_OLDBUF, oldwin); - else { - /* Create a new quickfix buffer */ - (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin); - // Switch off 'swapfile'. - set_option_value("swf", 0L, NULL, OPT_LOCAL); - set_option_value("bt", 0L, "quickfix", OPT_LOCAL); - set_option_value("bh", 0L, "wipe", OPT_LOCAL); - RESET_BINDING(curwin); - curwin->w_p_diff = false; - set_option_value("fdm", 0L, "manual", OPT_LOCAL); - } - - /* Only set the height when still in the same tab page and there is no - * window to the side. */ - if (curtab == prevtab - && curwin->w_width == Columns - ) - win_setheight(height); - curwin->w_p_wfh = TRUE; /* set 'winfixheight' */ - if (win_valid(win)) - prevwin = win; } qf_set_title_var(qi); @@ -3258,7 +3279,7 @@ void ex_copen(exarg_T *eap) curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index; curwin->w_cursor.col = 0; check_cursor(); - update_topline(); /* scroll to show the line */ + update_topline(); // scroll to show the line } // Move the cursor in the quickfix window to "lnum". @@ -3349,7 +3370,8 @@ qf_win_pos_update ( /// Checks whether the given window is displaying the specified /// quickfix/location list buffer. -static int is_qf_win(win_T *win, qf_info_T *qi) +static int is_qf_win(const win_T *win, const qf_info_T *qi) + FUNC_ATTR_NONNULL_ARG(2) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { // // A window displaying the quickfix buffer will have the w_llist_ref field @@ -3369,7 +3391,8 @@ static int is_qf_win(win_T *win, qf_info_T *qi) /// Find a window displaying the quickfix/location list 'qi' /// Only searches in the current tabpage. -static win_T *qf_find_win(qf_info_T *qi) +static win_T *qf_find_win(const qf_info_T *qi) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { FOR_ALL_WINDOWS_IN_TAB(win, curtab) { if (is_qf_win(win, qi)) { @@ -3384,7 +3407,8 @@ static win_T *qf_find_win(qf_info_T *qi) * Find a quickfix buffer. * Searches in windows opened in all the tabs. */ -static buf_T *qf_find_buf(qf_info_T *qi) +static buf_T *qf_find_buf(const qf_info_T *qi) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { FOR_ALL_TAB_WINDOWS(tp, win) { if (is_qf_win(win, qi)) { diff --git a/src/nvim/window.c b/src/nvim/window.c index ce5be8e904..d4a0db9c89 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -1574,7 +1574,7 @@ static void win_init_some(win_T *newp, win_T *oldp) /// Check if "win" is a pointer to an existing window in the current tabpage. /// /// @param win window to check -bool win_valid(win_T *win) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +bool win_valid(const win_T *win) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { if (win == NULL) { return false; From 279ff233799f23dc8a11882fe78df79f9dafdfa3 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Mon, 14 Oct 2019 10:00:14 -0400 Subject: [PATCH 0207/1293] vim-patch:8.1.0434: copy_loclist() is too long Problem: copy_loclist() is too long. Solution: Split in multiple functions. (Yegappan Lakshmanan) https://github.com/vim/vim/commit/09037503ea5f957ad23121bc61e15e4bb1765edf --- src/nvim/quickfix.c | 204 ++++++++++++++++++++++++-------------------- src/nvim/window.c | 5 +- 2 files changed, 114 insertions(+), 95 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 3f2688809d..9483670864 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -1622,116 +1622,134 @@ static qf_info_T *ll_get_or_alloc_list(win_T *wp) return wp->w_llist; } -/* - * Copy the location list from window "from" to window "to". - */ -void copy_loclist(win_T *from, win_T *to) +// Copy location list entries from 'from_qfl' to 'to_qfl'. +static int copy_loclist_entries(const qf_list_T *from_qfl, + qf_list_T *to_qfl, + qf_info_T *to_qi) + FUNC_ATTR_NONNULL_ALL { - qf_info_T *qi; - int idx; int i; + const qfline_T *from_qfp; - /* - * When copying from a location list window, copy the referenced - * location list. For other windows, copy the location list for - * that window. - */ - if (IS_LL_WINDOW(from)) + // copy all the location entries in this list + for (i = 0, from_qfp = from_qfl->qf_start; + i < from_qfl->qf_count && from_qfp != NULL; + i++, from_qfp = from_qfp->qf_next) { + if (qf_add_entry(to_qi, + to_qi->qf_curlist, + NULL, + NULL, + from_qfp->qf_module, + 0, + from_qfp->qf_text, + from_qfp->qf_lnum, + from_qfp->qf_col, + from_qfp->qf_viscol, + from_qfp->qf_pattern, + from_qfp->qf_nr, + 0, + from_qfp->qf_valid) == FAIL) { + return FAIL; + } + + // qf_add_entry() will not set the qf_num field, as the + // directory and file names are not supplied. So the qf_fnum + // field is copied here. + qfline_T *const prevp = to_qfl->qf_last; + prevp->qf_fnum = from_qfp->qf_fnum; // file number + prevp->qf_type = from_qfp->qf_type; // error type + if (from_qfl->qf_ptr == from_qfp) { + to_qfl->qf_ptr = prevp; // current location + } + } + + return OK; +} + +// Copy the specified location list 'from_qfl' to 'to_qfl'. +static int copy_loclist(const qf_list_T *from_qfl, + qf_list_T *to_qfl, + qf_info_T *to_qi) + FUNC_ATTR_NONNULL_ALL +{ + // Some of the fields are populated by qf_add_entry() + to_qfl->qf_nonevalid = from_qfl->qf_nonevalid; + to_qfl->qf_count = 0; + to_qfl->qf_index = 0; + to_qfl->qf_start = NULL; + to_qfl->qf_last = NULL; + to_qfl->qf_ptr = NULL; + if (from_qfl->qf_title != NULL) { + to_qfl->qf_title = vim_strsave(from_qfl->qf_title); + } else { + to_qfl->qf_title = NULL; + } + if (from_qfl->qf_ctx != NULL) { + to_qfl->qf_ctx = xcalloc(1, sizeof(*to_qfl->qf_ctx)); + if (to_qfl->qf_ctx != NULL) { + tv_copy(from_qfl->qf_ctx, to_qfl->qf_ctx); + } + } else { + to_qfl->qf_ctx = NULL; + } + if (from_qfl->qf_count) { + if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL) { + return FAIL; + } + } + + to_qfl->qf_index = from_qfl->qf_index; // current index in the list + + // Assign a new ID for the location list + to_qfl->qf_id = ++last_qf_id; + to_qfl->qf_changedtick = 0L; + + // When no valid entries are present in the list, qf_ptr points to + // the first item in the list + if (to_qfl->qf_nonevalid) { + to_qfl->qf_ptr = to_qfl->qf_start; + to_qfl->qf_index = 1; + } + + return OK; +} + +// Copy the location list stack 'from' window to 'to' window. +void copy_loclist_stack(win_T *from, win_T *to) + FUNC_ATTR_NONNULL_ALL +{ + qf_info_T *qi; + + // When copying from a location list window, copy the referenced + // location list. For other windows, copy the location list for + // that window. + if (IS_LL_WINDOW(from)) { qi = from->w_llist_ref; - else + } else { qi = from->w_llist; + } - if (qi == NULL) /* no location list to copy */ + if (qi == NULL) { // no location list to copy return; + } - /* allocate a new location list */ + // allocate a new location list to->w_llist = ll_new_list(); to->w_llist->qf_listcount = qi->qf_listcount; - /* Copy the location lists one at a time */ - for (idx = 0; idx < qi->qf_listcount; idx++) { - qf_list_T *from_qfl; - qf_list_T *to_qfl; - + // Copy the location lists one at a time + for (int idx = 0; idx < qi->qf_listcount; idx++) { to->w_llist->qf_curlist = idx; - from_qfl = &qi->qf_lists[idx]; - to_qfl = &to->w_llist->qf_lists[idx]; - - /* Some of the fields are populated by qf_add_entry() */ - to_qfl->qf_nonevalid = from_qfl->qf_nonevalid; - to_qfl->qf_count = 0; - to_qfl->qf_index = 0; - to_qfl->qf_start = NULL; - to_qfl->qf_last = NULL; - to_qfl->qf_ptr = NULL; - if (from_qfl->qf_title != NULL) - to_qfl->qf_title = vim_strsave(from_qfl->qf_title); - else - to_qfl->qf_title = NULL; - - if (from_qfl->qf_ctx != NULL) { - to_qfl->qf_ctx = xcalloc(1, sizeof(typval_T)); - tv_copy(from_qfl->qf_ctx, to_qfl->qf_ctx); - } else { - to_qfl->qf_ctx = NULL; - } - - if (from_qfl->qf_count) { - qfline_T *from_qfp; - qfline_T *prevp; - - // copy all the location entries in this list - for (i = 0, from_qfp = from_qfl->qf_start; - i < from_qfl->qf_count && from_qfp != NULL; - i++, from_qfp = from_qfp->qf_next) { - if (qf_add_entry(to->w_llist, - to->w_llist->qf_curlist, - NULL, - NULL, - from_qfp->qf_module, - 0, - from_qfp->qf_text, - from_qfp->qf_lnum, - from_qfp->qf_col, - from_qfp->qf_viscol, - from_qfp->qf_pattern, - from_qfp->qf_nr, - 0, - from_qfp->qf_valid) == FAIL) { - qf_free_all(to); - return; - } - /* - * qf_add_entry() will not set the qf_num field, as the - * directory and file names are not supplied. So the qf_fnum - * field is copied here. - */ - prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last; - prevp->qf_fnum = from_qfp->qf_fnum; // file number - prevp->qf_type = from_qfp->qf_type; // error type - if (from_qfl->qf_ptr == from_qfp) { - to_qfl->qf_ptr = prevp; // current location - } - } - } - - to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */ - - // Assign a new ID for the location list - to_qfl->qf_id = ++last_qf_id; - to_qfl->qf_changedtick = 0L; - - /* When no valid entries are present in the list, qf_ptr points to - * the first item in the list */ - if (to_qfl->qf_nonevalid) { - to_qfl->qf_ptr = to_qfl->qf_start; - to_qfl->qf_index = 1; + if (copy_loclist(&qi->qf_lists[idx], &to->w_llist->qf_lists[idx], + to->w_llist) == FAIL) { + qf_free_all(to); + return; } } - to->w_llist->qf_curlist = qi->qf_curlist; /* current list */ + to->w_llist->qf_curlist = qi->qf_curlist; // current list } // Get buffer number for file "directory/fname". diff --git a/src/nvim/window.c b/src/nvim/window.c index d4a0db9c89..0531ad1938 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -1530,8 +1530,9 @@ static void win_init(win_T *newp, win_T *oldp, int flags) /* Don't copy the location list. */ newp->w_llist = NULL; newp->w_llist_ref = NULL; - } else - copy_loclist(oldp, newp); + } else { + copy_loclist_stack(oldp, newp); + } newp->w_localdir = (oldp->w_localdir == NULL) ? NULL : vim_strsave(oldp->w_localdir); From aa8f059397313a931706f32a610f70e377043e13 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 18 Oct 2019 01:17:35 -0400 Subject: [PATCH 0208/1293] vim-patch:8.1.0438: the ex_make() function is too long Problem: The ex_make() function is too long. Solution: Split it into several functions. (Yegappan Lakshmanan) https://github.com/vim/vim/commit/b434ae2a1fcbbd43244c6130451de7f14346e224 --- src/nvim/quickfix.c | 85 +++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 9483670864..b5e0731935 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -3708,6 +3708,58 @@ int grep_internal(cmdidx_T cmdidx) *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0; } +// Return the make/grep autocmd name. +static char_u *make_get_auname(cmdidx_T cmdidx) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + switch (cmdidx) { + case CMD_make: + return (char_u *)"make"; + case CMD_lmake: + return (char_u *)"lmake"; + case CMD_grep: + return (char_u *)"grep"; + case CMD_lgrep: + return (char_u *)"lgrep"; + case CMD_grepadd: + return (char_u *)"grepadd"; + case CMD_lgrepadd: + return (char_u *)"lgrepadd"; + default: + return NULL; + } +} + +// Form the complete command line to invoke 'make'/'grep'. Quote the command +// using 'shellquote' and append 'shellpipe'. Echo the fully formed command. +static char *make_get_fullcmd(const char_u *makecmd, const char_u *fname) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET +{ + size_t len = STRLEN(p_shq) * 2 + STRLEN(makecmd) + 1; + if (*p_sp != NUL) { + len += STRLEN(p_sp) + STRLEN(fname) + 3; + } + char *const cmd = xmalloc(len); + snprintf(cmd, len, "%s%s%s", (char *)p_shq, (char *)makecmd, (char *)p_shq); + + // If 'shellpipe' empty: don't redirect to 'errorfile'. + if (*p_sp != NUL) { + append_redir(cmd, len, (char *)p_sp, (char *)fname); + } + + // Display the fully formed command. Output a newline if there's something + // else than the :make command that was typed (in which case the cursor is + // in column 0). + if (msg_col == 0) { + msg_didout = false; + } + msg_start(); + MSG_PUTS(":!"); + msg_outtrans((char_u *)cmd); // show what we are doing + + return cmd; +} + /* * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd" */ @@ -3717,24 +3769,15 @@ void ex_make(exarg_T *eap) win_T *wp = NULL; qf_info_T *qi = &ql_info; int res; - char_u *au_name = NULL; char_u *enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc; - /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */ + // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". if (grep_internal(eap->cmdidx)) { ex_vimgrep(eap); return; } - switch (eap->cmdidx) { - case CMD_make: au_name = (char_u *)"make"; break; - case CMD_lmake: au_name = (char_u *)"lmake"; break; - case CMD_grep: au_name = (char_u *)"grep"; break; - case CMD_lgrep: au_name = (char_u *)"lgrep"; break; - case CMD_grepadd: au_name = (char_u *)"grepadd"; break; - case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break; - default: break; - } + char_u *const au_name = make_get_auname(eap->cmdidx); if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, curbuf->b_fname, true, curbuf)) { if (aborting()) { @@ -3752,25 +3795,7 @@ void ex_make(exarg_T *eap) return; os_remove((char *)fname); // in case it's not unique - // If 'shellpipe' empty: don't redirect to 'errorfile'. - const size_t len = (STRLEN(p_shq) * 2 + STRLEN(eap->arg) + 1 - + (*p_sp == NUL - ? 0 - : STRLEN(p_sp) + STRLEN(fname) + 3)); - char *const cmd = xmalloc(len); - snprintf(cmd, len, "%s%s%s", (char *)p_shq, (char *)eap->arg, - (char *)p_shq); - if (*p_sp != NUL) { - append_redir(cmd, len, (char *) p_sp, (char *) fname); - } - // Output a newline if there's something else than the :make command that - // was typed (in which case the cursor is in column 0). - if (msg_col == 0) { - msg_didout = false; - } - msg_start(); - MSG_PUTS(":!"); - msg_outtrans((char_u *)cmd); // show what we are doing + char *const cmd = make_get_fullcmd(eap->arg, fname); do_shell((char_u *)cmd, 0); From 8daefa348e24a2fb06a7d13cec0f827c7c3e60c5 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 20 Oct 2019 21:21:56 -0400 Subject: [PATCH 0209/1293] vim-patch:8.1.0455: checking for empty quickfix stack is not consistent Problem: Checking for empty quickfix stack is not consistent. Solution: Use qf_stack_empty(). (Yegappan Lakshmanan) https://github.com/vim/vim/commit/019dfe6855e011c02427bb922aafeae0245372c9 --- src/nvim/quickfix.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index b5e0731935..ec32a88f79 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -784,6 +784,13 @@ static int qf_get_nextline(qfstate_T *state) return QF_OK; } +// Returns true if the specified quickfix/location stack is empty +static bool qf_stack_empty(const qf_info_T *qi) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + return qi == NULL || qi->qf_listcount <= 0; +} + // Returns true if the specified quickfix/location list is empty. static bool qf_list_empty(const qf_info_T *qi, int qf_idx) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT @@ -2537,8 +2544,7 @@ void qf_jump(qf_info_T *qi, int dir, int errornr, int forceit) if (qi == NULL) qi = &ql_info; - if (qi->qf_curlist >= qi->qf_listcount - || qi->qf_lists[qi->qf_curlist].qf_count == 0) { + if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist)) { EMSG(_(e_quickfix)); return; } @@ -2673,8 +2679,7 @@ void qf_list(exarg_T *eap) } } - if (qi->qf_curlist >= qi->qf_listcount - || qi->qf_lists[qi->qf_curlist].qf_count == 0) { + if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist)) { EMSG(_(e_quickfix)); return; } @@ -2916,8 +2921,7 @@ void qf_history(exarg_T *eap) if (is_loclist_cmd(eap->cmdidx)) { qi = GET_LOC_LIST(curwin); } - if (qi == NULL || (qi->qf_listcount == 0 - && qi->qf_lists[qi->qf_curlist].qf_count == 0)) { + if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist)) { MSG(_("No entries")); } else { for (i = 0; i < qi->qf_listcount; i++) { @@ -3129,13 +3133,15 @@ void ex_cwindow(exarg_T *eap) * close the window. If a quickfix window is not open, then open * it if we have errors; otherwise, leave it closed. */ - if (qi->qf_lists[qi->qf_curlist].qf_nonevalid - || qi->qf_lists[qi->qf_curlist].qf_count == 0 - || qi->qf_curlist >= qi->qf_listcount) { - if (win != NULL) + if (qf_stack_empty(qi) + || qi->qf_lists[qi->qf_curlist].qf_nonevalid + || qf_list_empty(qi, qi->qf_curlist)) { + if (win != NULL) { ex_cclose(eap); - } else if (win == NULL) + } + } else if (win == NULL) { ex_copen(eap); + } } /* @@ -3588,8 +3594,8 @@ static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last) } } - /* Check if there is anything to display */ - if (qi->qf_curlist < qi->qf_listcount) { + // Check if there is anything to display + if (!qf_stack_empty(qi)) { char_u dirname[MAXPATHL]; *dirname = NUL; @@ -4460,7 +4466,7 @@ void ex_vimgrep(exarg_T *eap) if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd) - || qi->qf_curlist == qi->qf_listcount) { + || qf_stack_empty(qi)) { // make place for a new list qf_new_list(qi, title); } @@ -5153,12 +5159,12 @@ int qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict) const int flags = qf_getprop_keys2flags(what, wp != NULL); - if (qi != NULL && qi->qf_listcount != 0) { + if (!qf_stack_empty(qi)) { qf_idx = qf_getprop_qfidx(qi, what); } // List is not present or is empty - if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX) { + if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX) { return qf_getprop_defaults(qi, flags, retdict); } @@ -5350,7 +5356,7 @@ static int qf_setprop_get_qfidx( // non-available list and add the new list at the end of the // stack. *newlist = true; - qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0; + qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1; } else if (qf_idx < 0 || qf_idx >= qi->qf_listcount) { return INVALID_QFIDX; } else if (action != ' ') { @@ -5358,7 +5364,7 @@ static int qf_setprop_get_qfidx( } } else if (di->di_tv.v_type == VAR_STRING && strequal((const char *)di->di_tv.vval.v_string, "$")) { - if (qi->qf_listcount > 0) { + if (!qf_stack_empty(qi)) { qf_idx = qi->qf_listcount - 1; } else if (*newlist) { qf_idx = 0; @@ -5477,7 +5483,7 @@ static int qf_set_properties(qf_info_T *qi, const dict_T *what, int action, { dictitem_T *di; int retval = FAIL; - bool newlist = action == ' ' || qi->qf_curlist == qi->qf_listcount; + bool newlist = action == ' ' || qf_stack_empty(qi); int qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist); if (qf_idx == INVALID_QFIDX) { // List not found return FAIL; From ed72d9597d61f4f32162b7810dc93469bcee1ce8 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 24 Oct 2019 20:25:58 +0100 Subject: [PATCH 0210/1293] man.vim: pull out s:get_paths() --- runtime/autoload/man.vim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/runtime/autoload/man.vim b/runtime/autoload/man.vim index 153f1afed8..ec48d96dd6 100644 --- a/runtime/autoload/man.vim +++ b/runtime/autoload/man.vim @@ -360,14 +360,18 @@ function! man#complete(arg_lead, cmd_line, cursor_pos) abort return s:complete(sect, sect, name) endfunction -function! s:complete(sect, psect, name) abort +function! s:get_paths(sect, name) abort try let mandirs = join(split(s:system(['man', s:find_arg]), ':\|\n'), ',') catch call s:error(v:exception) return endtry - let pages = globpath(mandirs,'man?/'.a:name.'*.'.a:sect.'*', 0, 1) + return globpath(mandirs,'man?/'.a:name.'*.'.a:sect.'*', 0, 1) +endfunction + +function! s:complete(sect, psect, name) abort + let pages = s:get_paths(a:sect, a:name) " We remove duplicates in case the same manpage in different languages was found. return uniq(sort(map(pages, 's:format_candidate(v:val, a:psect)'), 'i')) endfunction From 63f0ca326322376271c68f51cf8908daad524339 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 22 Oct 2019 23:40:16 +0100 Subject: [PATCH 0211/1293] man.vim: use 'tagfunc' instead of remapping man#pop_tag() is also no longer used --- runtime/autoload/man.vim | 23 +++++++++++++++-------- runtime/ftplugin/man.vim | 4 ++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/runtime/autoload/man.vim b/runtime/autoload/man.vim index ec48d96dd6..8825719ec7 100644 --- a/runtime/autoload/man.vim +++ b/runtime/autoload/man.vim @@ -264,14 +264,6 @@ function! s:push_tag() abort \ }] endfunction -function! man#pop_tag() abort - if !empty(s:tag_stack) - let tag = remove(s:tag_stack, -1) - execute 'silent' tag['buf'].'buffer' - call cursor(tag['lnum'], tag['col']) - endif -endfunction - " extracts the name and sect out of 'path/name.sect' function! s:extract_sect_and_name_path(path) abort let tail = fnamemodify(a:path, ':t') @@ -410,4 +402,19 @@ function! man#init_pager() abort endif endfunction +function! man#goto_tag(pattern, flags, info) abort + " currently no support for section completion + let sect = "" + + let candidates = s:get_paths(sect, a:pattern) + + return map(candidates, { + \ _, path -> { + \ 'name': s:extract_sect_and_name_path(path)[1], + \ 'filename': 'man://' . path, + \ 'cmd': '1' + \ } + \ }) +endfunction + call s:init() diff --git a/runtime/ftplugin/man.vim b/runtime/ftplugin/man.vim index 6c7f095f62..b3b23833ba 100644 --- a/runtime/ftplugin/man.vim +++ b/runtime/ftplugin/man.vim @@ -20,13 +20,13 @@ setlocal wrap breakindent linebreak setlocal nonumber norelativenumber setlocal foldcolumn=0 colorcolumn=0 nolist nofoldenable +setlocal tagfunc=man#goto_tag + if !exists('g:no_plugin_maps') && !exists('g:no_man_maps') nnoremap j gj nnoremap k gk nnoremap gO :call man#show_toc() - nnoremap :Man nnoremap K :Man - nnoremap :call man#pop_tag() if 1 == bufnr('%') || s:pager nnoremap q :lclose:q else From 2f0412e61d3c5113f9c121283a7e94a294706387 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 24 Oct 2019 20:48:18 +0100 Subject: [PATCH 0212/1293] man.vim: `:Man` preserves the tag stack --- runtime/autoload/man.vim | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/runtime/autoload/man.vim b/runtime/autoload/man.vim index 8825719ec7..9280474516 100644 --- a/runtime/autoload/man.vim +++ b/runtime/autoload/man.vim @@ -64,8 +64,9 @@ function! man#open_page(count, count1, mods, ...) abort return endtry - call s:push_tag() - let bufname = 'man://'.name.(empty(sect)?'':'('.sect.')') + let fullname = name.(empty(sect)?'':'('.sect.')') + call s:push_tag(fullname) + let bufname = 'man://'.fullname try set eventignore+=BufReadCmd @@ -254,14 +255,30 @@ function! s:verify_exists(sect, name) abort return s:extract_sect_and_name_path(path) + [path] endfunction -let s:tag_stack = [] +function! s:push_tag(name) abort + " emulate vim's tag pushing for cases where we don't use 'tagfunc' + if !&tagstack + return + endif -function! s:push_tag() abort - let s:tag_stack += [{ - \ 'buf': bufnr('%'), - \ 'lnum': line('.'), - \ 'col': col('.'), - \ }] + let winnr = winnr() + let stack = gettagstack(winnr) + + let curidx = stack.curidx + let items = stack.items + + let newstack = items[0 : curidx - 1] + let newstack += [{ + \ 'bufnr': bufnr('%'), + \ 'from': getpos('.'), + \ 'matchnr': 0, + \ 'tagname': a:name, + \ }] + + call settagstack(winnr, { + \ 'length': len(newstack), + \ 'items': newstack, + \ }) endfunction " extracts the name and sect out of 'path/name.sect' From 0173bdf98be6f867d1b316d4d2ac87f7a93d95e4 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 24 Oct 2019 21:15:08 +0100 Subject: [PATCH 0213/1293] man.vim: parse the section from the tag --- runtime/autoload/man.vim | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/runtime/autoload/man.vim b/runtime/autoload/man.vim index 9280474516..08c6fc1eca 100644 --- a/runtime/autoload/man.vim +++ b/runtime/autoload/man.vim @@ -420,10 +420,9 @@ function! man#init_pager() abort endfunction function! man#goto_tag(pattern, flags, info) abort - " currently no support for section completion - let sect = "" + let [sect, name] = man#extract_sect_and_name_ref(a:pattern) - let candidates = s:get_paths(sect, a:pattern) + let candidates = s:get_paths(sect, name) return map(candidates, { \ _, path -> { From ced2a38ad4dce6898c800dd68026c7b1823e484e Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Thu, 24 Oct 2019 07:26:22 -0400 Subject: [PATCH 0214/1293] tag: fix pvs/v547 error --- src/nvim/tag.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 0d42deed2b..20fd7caa51 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -988,9 +988,7 @@ add_llist_tags( cmd[len] = NUL; } - if ((dict = tv_dict_alloc()) == NULL) { - continue; - } + dict = tv_dict_alloc(); tv_list_append_dict(list, dict); tv_dict_add_str(dict, S_LEN("text"), (const char *)tag_name); From c065b0d75f3039030ca7de36397585c66542574b Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sun, 20 Oct 2019 22:36:30 -0400 Subject: [PATCH 0215/1293] CI: bump nodejs to v10.x (LTS) v8.x will be EOL at end of 2019. nvm on Travis has outdated LTS aliases. --- appveyor.yml | 2 +- ci/before_install.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index bb7bb1c4e9..7e2aef345b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -24,7 +24,7 @@ matrix: fast_finish: true install: [] before_build: -- ps: Install-Product node 8 +- ps: Install-Product node 10 build_script: - powershell ci\build.ps1 after_build: diff --git a/ci/before_install.sh b/ci/before_install.sh index 283605e113..7e2ff12d6d 100755 --- a/ci/before_install.sh +++ b/ci/before_install.sh @@ -47,8 +47,8 @@ if [[ "${TRAVIS_OS_NAME}" == osx ]] || [ ! -f ~/.nvm/nvm.sh ]; then fi source ~/.nvm/nvm.sh -nvm install --lts -nvm use --lts +nvm install 10 +nvm use 10 if [[ -n "$CMAKE_URL" ]]; then echo "Installing custom CMake: $CMAKE_URL" From 9b22b69454de45d37ae0bd5ebbefa8538d36197f Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Tue, 22 Oct 2019 20:28:56 -0400 Subject: [PATCH 0216/1293] CI/Appveyor: set powershell strict mode Set-PSDebug produces too much noise and has global scope. Strict mode is scoped to the script context and catches errors. --- ci/build.ps1 | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ci/build.ps1 b/ci/build.ps1 index 6d91b97aed..8f7fdd5f59 100644 --- a/ci/build.ps1 +++ b/ci/build.ps1 @@ -1,10 +1,10 @@ +Set-StrictMode -Version Latest $ErrorActionPreference = 'stop' -Set-PSDebug -Strict -Trace 1 $isPullRequest = ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT -ne $null) $env:CONFIGURATION -match '^(?\w+)_(?32|64)(?:-(?