Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(:source, nvim_exec): anonymous script improvements #16071

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

seandewar
Copy link
Member

@seandewar seandewar commented Oct 18, 2021

Needs a big rebase; will get around to it

Ref: #16064

Summary of changes:

  • <SID> works always; no prior s: access required. Every anonymous Vim script has a unique SID.
  • Lazily allocate script items and variables better: (especially useful for nvim_exec, which may be used many times)
    • Script items are only allocated for named scripts.
    • Script variables hash tables are only allocated if s: is used in a script.
  • Fixed bug where s: defined in nested contexts such as commands defined in anonymous scripts may get lost.
  • Fixed various bugs from feat(lua): add :verbose support for lua config #15079.
  • Reuse the same anonymous script when re:sourceing the same buffer (like Vim, but it always allocates a named script item :source buffer=<buffer id>; we could emulate this if we want, but I imagine it's done as they don't support anonymous scripts).
  • Show correct line numbers in Vim script anonymous sources. (feat(:source, nvim_exec): show line number in :verbose output #16806)
  • Use nvim_exec over helpers.source in more tests. (possible after the bug fixes)

Things to explore in the future:

  • A version of nvim_exec that returns the SID of the anonymous script used. Can take an opts dict allowing it to run in the context of a given SID.
  • Maybe also an API function for getting the current SID as an integer (and maybe other script context info).

@seandewar seandewar added api libnvim, Nvim RPC API vimscript labels Oct 18, 2021
src/nvim/eval.c Outdated Show resolved Hide resolved
src/nvim/ex_cmds2.c Outdated Show resolved Hide resolved
@seandewar seandewar force-pushed the anon-sid2 branch 3 times, most recently from 17b5c68 to 1795930 Compare October 20, 2021 13:40
@seandewar

This comment was marked as outdated.

@seandewar seandewar force-pushed the anon-sid2 branch 2 times, most recently from f83ca29 to 2d1de09 Compare October 20, 2021 20:29
@seandewar seandewar marked this pull request as ready for review October 20, 2021 20:31
@seandewar

This comment was marked as outdated.

@seandewar

This comment was marked as resolved.

@seandewar

This comment was marked as resolved.

@seandewar seandewar force-pushed the anon-sid2 branch 2 times, most recently from 3c7c41e to 7360116 Compare March 22, 2022 20:15
@seandewar seandewar changed the title fix(:source, nvim_exec): anonymous script item fixes feat(:source, nvim_exec): anonymous script improvements Mar 22, 2022
@seandewar seandewar force-pushed the anon-sid2 branch 3 times, most recently from 832c28b to 201dc29 Compare March 23, 2022 12:19
justinmk added a commit that referenced this pull request Mar 27, 2022
helpers.source() was a hack to work around the lack of anonymous
:source. Its "create tempfile" behavior is not a required part of most
tests that use it.

Some tests still need the old "create tempfile" behavior either because
they test SID behavior, or because of missing nvim_exec features: #16071
@seandewar seandewar force-pushed the anon-sid2 branch 2 times, most recently from 2055059 to 43e2c1d Compare March 28, 2022 23:33
@seandewar seandewar force-pushed the anon-sid2 branch 3 times, most recently from 4426f48 to 94aeb56 Compare March 29, 2022 18:38
@seandewar seandewar marked this pull request as ready for review March 29, 2022 18:38
@gpanders gpanders removed their request for review March 29, 2022 19:11
@seandewar seandewar requested a review from justinmk March 29, 2022 19:29
@seandewar seandewar force-pushed the anon-sid2 branch 2 times, most recently from 659246e to 7201cf4 Compare March 31, 2022 17:17
dmitmel pushed a commit to dmitmel/neovim that referenced this pull request Apr 16, 2022
helpers.source() was a hack to work around the lack of anonymous
:source. Its "create tempfile" behavior is not a required part of most
tests that use it.

Some tests still need the old "create tempfile" behavior either because
they test SID behavior, or because of missing nvim_exec features: neovim#16071
seandewar and others added 7 commits May 13, 2022 01:05
This reverts the change made in neovim#16064
(neovim@72652cb),
as it's not needed.

The reason why this fails when helpers.exec is used directly, but not when using
helpers.source, is due to source calling write_file, which applies a second
dedent to remove the indent before the heredoc END marker (required to make
heredocs work). Though helpers.source is now deprecated, and uses exec under the
hood, it still calls dedent to mimic the old behaviour, so we can still use it.
Followup to da9b0ab neovim#15994.
`scriptitem_T` was lifted out of ex_cmds2.c but it's not needed, so keep it
private.

Also clean up some comments from neovim#15079 and rename a few functions.
Add a test for ensuring different anon execs in the same Lua file still have
unique SIDs with tracing enabled.
Always assign unique SID for anonymous sources, but defer allocation of script
items and vars until script var access.

Because all anonymous scripts always have SIDs now, <SID> and s: scope (also
defined in functions, autocmds, commands, etc.) will work.

Because a SID may not yet (or ever) map to an item, use a hash map for
script_items and ga_scripts (now script_vars).

Add file_sids; a sorted growarray of SIDs associated with a script file (used
for :scriptnames, profiling, do_source, etc.).

Replace SCRIPT macros with script_item and script_sv functions.
Remove SCRIPT_VARS as it isn't very useful now.

Use PRIdSCID to format scid_T in places that were missing it.

This also fixes an issue since neovim#15079 that resulted in duplicate file name
entries in :scriptnames output; test for this.

Adjust more tests to use anonymous :source.
Note that as prior s: or <SID> access is no longer needed for scripts to be
given a SID, <SNR>s in tests that already use anonymous :sources such as
echo_spec.lua need to be adjusted.

Also note that we can potentially support adding profiling support for anonymous
scripts now. :)
This removes the need for script_ensure_anon_item and applies to all scripts.

Also no longer alloc a scriptitem_T for anon sources; they're not needed for
anything (at least not right now).
Associate a unique SID for each anonymously :sourced Vim script buffer.
This means sourcing the same buffer will retain the same script-local variables.

Use anonymous :source for memory_usage_spec.lua's "function captures lvars"
test. Use a buffer :source, as the test requires the same SID to be used.
Remove "s:defined_func" logic; it isn't needed, and removing it matches Vim.
Unfortunately, even after our changes that allow for <SID> to be always usable
in anonymous Vim scripts, the changes in neovim#15079 break this for Lua scripts due
to the use of SID_LUA over a unique SID in places such as nvim_exec.

Instead, use the special SID offsets to identify scripts from Lua contexts in
which nlua_set_sctx can be used. Encoding it this way avoids the need to store
such information in an allocated script item or elsewhere.

However, this approach produces ugly script IDs, so it's only used when :verbose
logging for Lua is enabled. As a result, it's now even more strongly recommended
to start nvim as `nvim -V1` rather than to set verbose=1 in the middle of a
session, otherwise tracing for previously sourced Lua scripts before verbose
was set will not work.

Remove the previous LastSet sctx dancing logic when accessing "s:" scope. It's
no longer needed as anonymous Lua scripts now get a unique SID.

Add some tests that would previously fail due to bugs with the old verbose Lua
approach.

Also, remove find_sid (unused leftover from the autocmd API PR) and change
sid_T's type to a fixed-width type (int64_t).
Support showing line numbers for Vim script sourced anonymously via :source (no
args) and nvim_exec. For ranged buffer :sources, offset the reported number by
the line number of the range's start.

Don't offset the line number shown from anonymous scripts with the line number
of an enclosing script. For example, this could produce confusing and incorrect
numbers in situations where we have escaped NLs:

```vim
echo "hi"
call nvim_exec("\n\n\nset cul", 1)
verbose set cul?
```
Would print "line 6" (preferably "line 5"), but it's actually on line 2 of the
enclosing script...
As :verbose will only report the context of nvim_exec's anonymous script here,
just show the effective line number inside of it: "last set from anonymous
:source line 4".

Also, fix the order of nlua_set_sctx in set_option_sctx_idx; the current order
will erroneously offset the line number from the Lua script... Added a test.
@zeertzjq
Copy link
Member

@seandewar Do you plan to continue this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api libnvim, Nvim RPC API vimscript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants