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

autoload #121

Closed
SingularisArt opened this issue Jan 28, 2022 · 5 comments
Closed

autoload #121

SingularisArt opened this issue Jan 28, 2022 · 5 comments

Comments

@SingularisArt
Copy link
Contributor

This isn't a bug. It's just a question.

I'm kind of new to lua and all of that.
Could you please explain to me why you put:

return wincent.autoload('wincent.vim')

Could you please be detailed in your responses? I do know basic Lua, but not advanced level like this neovim config.

@wincent
Copy link
Owner

wincent commented Jan 28, 2022

I don't know if I can provide a very good explanation for it, but the basic idea is described here:

-- Provides a lazy autoload mechanism similar to Vimscript's autoload mechanism.
--
-- Examples:
--
-- " Vimscript - looks for function named `wincent#foo#bar#baz()` in
-- " autoload/wincent/foo/bar.vim):
--
-- call wincent#foo#bar#baz()
--
-- -- Lua - lazy-loads these files in sequence before calling
-- -- `wincent.foo.bar.baz()`:
-- --
-- -- 1. lua/wincent/foo.lua (or lua/wincent/foo/init.lua)
-- -- 2. lua/wincent/foo/bar.lua (or lua/wincent/foo/bar/init.lua)
-- -- 3. lua/wincent/foo/bar/baz.lua (or lua/wincent/foo/bar/baz/init.lua)
--
-- local wincent = require'wincent'
-- wincent.foo.bar.baz()
--
-- -- Note that because `require'wincent'` appears at the top of the top-level
-- -- init.lua, the previous example can be written as:
--
-- wincent.foo.bar.baz()

The "normal/idiomatic" Lua way to do this kind of thing would be to not have autoloading at all. Any time you need anything you would write require'something', and if you need to use it multiple times, you write out the require multiple times — because modules are cached, these subsequent calls don't re-evaluate the module; they just return the already-loaded module.

The specific line you cite was added in f53783e, which says:

refactor(nvim): streamline global access

Yes, globals are evil, but it is 2 AM and I reserve the right to exercise poor judgment at this time of night.

Seriously though, I think the streamlining here makes for some real usability benefits (for example, see the improvement we get from doing v:lua.wincent.something.blah() as opposed to luaeval("require'wincent.something'.blah()"); the quoting becomes dramatically simpler and less error prone), so I think it's worth it.

As you can see in that commit, this allowed me to remove a bunch of require'wincent' calls and then just directly access "autoloaded" functions by chaining them off the wincent global (eg. wincent.vim.setlocal).

Overall I would describe all this as a "cute hack", but not necessarily a pattern I have seen anywhere else or would actively recommend. It might be a bit too magical to be a good idea. Does that answer your question, @SingularisArt?

@SingularisArt
Copy link
Contributor Author

@wincent

Yes. Thank you.

Also, I've learned a ton from your youtube videos and I was wondering if you could create a youtube video talking about your neovim setup and how you configure your things. Like, the structure of the config and describe what each file/folder is doing.

@SingularisArt
Copy link
Contributor Author

Quick question regarding the usage of this function. I have all of my plugin configuration located at lua/SingularisArt/plugins and I have a ton of files in there. How can I use this function to require all of the files in that directory?

My init.lua in the lua/SingularisArt/plugins directory is where I add my plugins using packer. I tried to add this at the end of my init.lua file:

return SingularisArt.autoload('SingularisArt.plugins')

You can view it here.
Thanks for your help and your time!

@wincent
Copy link
Owner

wincent commented Jan 29, 2022

What you've got in there right now looks about right to me — is it working?

@SingularisArt
Copy link
Contributor Author

Finally, I got it to work. I just had some problems with another file.

Thanks so much!

dmouraneto pushed a commit to dmouraneto/wincent that referenced this issue Feb 11, 2022
Motivation; get this fix:

    L3MON4D3/LuaSnip#142

But it turns out that there have been a _lot_ of changes since I last
updated; if anything breaks, I will follow up:

* aspects/nvim/files/.config/nvim/pack/bundle/opt/LuaSnip 72323c1...9a8cf5f (261):
  > Merge pull request wincent#143 from leiserfg/multi-ft
  > Format with stylua
  > fix: account for compound filetypes (closes wincent#142).
  > Update README.md
  > Format with stylua
  > Fix parser, for real now.
  > Format with stylua
  > Fix slight regression with variables in lsp-parsed snippets.
  > Fixup(d2aa43c): BREAKING, parsing has become more strict.
  > Format with stylua
  > Provide option to set better `assemble`-function for nested snippets.
  > Format with stylua
  > Merge branch 'master' of github.com:L3MON4D3/LuaSnip
  > lsp: correctly parse brackets (closes wincent#141) and split iNode-text.
  > fix(parser): remove escape-chars everywhere.
  > Merge pull request wincent#140 from carlitux/patch-2
  > Set pos to nil for better docstring if queried manually.
  > Format with stylua
  > Improve get_docstring().
  > Typo.
  > Format with stylua
  > Account for selecting empty lines (only) (closes wincent#139).
  > Use `rawget` to get position.
  > Merge pull request wincent#138 from leiserfg/handle_emtpy_snippets
  > Parser: ${n:sometext} can also be repetitions of previous insertNodes.
  > Try expanding autosnippets if pum is visible, too.
  > Format with stylua
  > Use tokenizer to create helper-snippet for regex-triggers.
  > Implement tokenizer for patterns.
  > Format with stylua
  > pcall `get_text`, {""} on error. Don't error on `str_utfindex`.
  > Try to prevent jump_from from erroring due to deleted snippets.
  > Auto generate docs
  > Format with stylua
  > Merge branch 'insert_node_event_callbacks'
  > Add <Plug>-mapping for previous choice (Closes wincent#131).
  > Format with stylua
  > Provide setting for delete-check autocommands.
  > Recommend `InsertEnter` for `region_check_events`
  > Make bytecol_to_utfcol behave correctly (used to return three values).
  > Auto generate docs
  > Format with stylua
  > Small changes+improvements for the enter/leave events:
  > Auto generate docs
  > Rename AUTOCOMMANDS to EVENTS.
  > Auto generate docs
  > Format with stylua
  > No need to input_leave the generated snippet in dynamicNode.
  > Trigger autocommands on entering/leaving nodes.
  > Add function to check if the current snippet was deleted+<Plug>-mapping.
  > Merge pull request wincent#126 from leiserfg/generic-package-name
  > Auto generate docs
  > Format with stylua
  > Document indent_snippet_node and some usages for it.
  > Remove prefix_snippet_node, it's really just a specification of ISN.
  > Re-implement changing choice backwards.
  > Prevent region check for autotriggered snippets too.
  > Each choice gets a pointer to the next.
  > Rename choiceNode.inner to choiceNode.active_choice, more expressive.
  > Clear snippets' own marks on exit.
  > Rename vars that store choiceNodes to reflect that.
  > Format with stylua
  > Disable region check while jumping.
  > Make sure node isn't nil before indexing.
  > Check for specific node+also check following snippets.
  > Leave exit node when next snippet is expanded, only compare lines for region.
  > Mention setting in README.
  > Add config for specifying when to do region check, defaults to never.
  > Fix minor bugs with exit_out_of_region_snippet.
  > Fix small issues with exitNode.
  > Fix bug caused by new jump_from/into-behaviour.
  > Add function to leave active snippet if its' region is left.
  > Make no_move actually funcitional.
  > Add snippet.snippet, contains 'outer' (ie. the user-triggered) snippet.
  > Have jump_into/jump_leave return the new active node.
  > Merge pull request wincent#124 from leiserfg/clean_lazy
  > Auto generate docs
  > Fix lazy-loading.
  > Add more details for loading vscode-packaged snippets.
  > Format with stylua
  > Fix/improve update_dependents-behaviour.
  > Clean up a bit.
  > Reset active upon exit()ing, only pop active choice if this was active.
  > Simplify choiceNodes' init-process.
  > Merge pull request wincent#121 from L3MON4D3/static_text_for_snippets
  > Ignore and correctly format lines on which stylua fails.
  > Format with stylua
  > Fix regression: *move_on_mark_*-functions were removed, typo(?) in util.mark.
  > Format with stylua
  > Remove print's.
  > Changes nothing functionally, but clearer.
  > After parsing, the positions of insertNodes are not 'dense', fix it.
  > Inserting a missing `i(0)` is handled by snippet-constructor.
  > Auto generate docs
  > Format with stylua
  > Document expected layout of directories loaded via vscode-loader.
  > Format with stylua
  > Separate expanding tabs in text and indenting; make both more generic.
  > Fix expand_tab-implementation.
  > Always initialize static_text for insert and textNode.
  > Format with stylua
  > Fix unlink_current, correctly set active node.
  > Set Luasnip_active_choice correctly after exit()ing choiceNode.
  > Format with stylua
  > Merge pull request wincent#114 from leiserfg/extra-vars
  > Format with stylua
  > Differentiate between insert and exitNodes(`i(0)`).
  > Call insertNodes' input_leave function for i(0).
  > Auto generate docs
  > Format with stylua
  > Document new behaviour of i(0).
  > Format with stylua
  > Do insertNodes' input_enter for i(0).
  > Fix TM_LINE_INDEX/NUMBER, it should be a string.
  > Format with stylua
  > Explicitly open folds after moving, doesn't seem to happen automatically.
  > Auto generate docs
  > Format with stylua
  > Mention passive extending active and highlight groups in doc.
  > Fix overriding-behaviour.
  > Fix clear_invalid, don't have it override active with passive.
  > Use user-defined Luasnip*Node{Active,Passive} as highlight when defined.
  > Merge default into user-defined table.
  > Format with stylua
  > Replace link to matrix room with icon.
  > Format with stylua
  > Clarify arguments to util.put.
  > Use byte-position of mark to nvim_buf_set_text().
  > Expose function to get byte-position of mark.
  > Format with stylua
  > Default ext_opts-table to {}.
  > Auto generate docs
  > Format with stylua
  > Add example for ext_opts, fix jdoc-snippet.
  > Rename prio_increase to ext_prio_increase.
  > Add documentation for ext_opts.
  > Correctly pass ext_opts with higher prios for inner snippets.
  > Config: include priority+functions to modify.
  > Add appropriate exit()-functions to dynamic and choice.
  > Use copy of passed table.
  > Fix dynNode:set_rgrav, after changing textNode, grav is default-false.
  > Fill missing values in ext_opts.
  > Don't unnecessarily update extmark.
  > Change ext_opts on entering/leaving.
  > Correctly set type and opts for snippet.
  > Rename change_rgravs to update_rgravs.
  > DO change gravities for textNodes, prevents some bug+more uniform.
  > Enter node in both paths.
  > Delete marks on exit.
  > Use two functions to set gravs or other opts.
  > Set snippets' mark from outside also.
  > Format with stylua
  > Mention nvim-cmp and cmp_luasnip in README.
  > Format with stylua
  > Merge pull request wincent#106 from amenonsen/patch-3
  > Format with stylua
  > Merge pull request wincent#105 from amenonsen/patch2
  > Format with stylua
  > Yeeeeeaaaaah no, not really anymore
  > Format with stylua
  > Mention autosnippets in README.
  > Format with stylua
  > Merge branch 'autotrigger'
  > Format with stylua
  > Fix: Adding a i(0) if none was specified.
  > Don't set correctly and then overwrite with old values :/
  > As the mark-id isn't copied anymore, it can be set later in put_initial()
  > Implement clear-function, fix bug with raw_pos.
  > Print gravities in snippet:dump().
  > Store ext_opts in snippet, pass to children; Change marks for dyn/choice
  > Add ext_opts to config.
  > Use raw, not utf-adjusted position for initializing extmarks.
  > Don't set positions in opts.
  > Default-init marks with nil.
  > Fix set_mark_rgrav-function, use default-version everywhere.
  > Actually use new marks in snippets.
  > Most likely, there won't be any updates to pos, and if, then to both.
  > Marks are objects; Simplify extmark_rgrav-setting.
  > Add table for node-names, shift indices to match name.
  > Refactor types to use enum.
  > Auto generate docs
  > Mention mappings in DOC, refer to README.
  > Auto generate docs
  > Merge pull request wincent#104 from amenonsen/patch-1
  > Mention new config-option in README.
  > Auto generate docs
  > Document SELECT-behaviour and lsp-parsing.
  > Provide example mappings for lua and vimscript.
  > Link to lsp-snippet spec.
  > Format with stylua
  > Only remove indent for TM_SELECTED_TEXT.
  > Enable new vars in env.
  > Correctly populate selection.
  > Create all types of indent in store_selection().
  > Modify store_selection to get all lines at once.
  > Allow node.pos to be nil, mimic vscodes' behaviour for $TM_SELECTED_TEXT.
  > Should be an insertNode.
  > Add new snippetNodes prefixSnippetNode and indentSnippetNode
  > Refactor population of args for function/dynamicNodes.
  > Improve autowrapping for snippets, fix it for snippetNodes.
  > Merge pull request #102 from leiserfg/find_vimrc
  > Auto generate docs
  > Specify what the text looks like when executing condition-function.
  > Auto generate docs
  > Maybe also document condition-function.
  > Format with stylua
  > Change snippet:match(...) for regex-triggers, parentheses can mess up patterns.
  > Reuse mark-id if possible.
  > Refactor snippet:indent() slightly.
  > Clean up unneeded args.
  > Indent + populate snippetNodes' env in put_initial().
  > Auto generate docs
  > Format with stylua
  > Explicitly mention text-passing behaviour in docs.
  > BREAKING CHANGE: Pass dedented text to function/dynamicNodes, indent returned text for functionNodes.
  > Merge pull request wincent#94 from leiserfg/fix-of-by-one
  > Merge pull request wincent#93 from leiserfg/use-named-register
  > Format with stylua
  > Reset Visual after use.
  > Return {} instead of "" on empty visual.
  > Prepare for storing prio,hl_group,... in node.mark.
  > Format with stylua
  > Correctly check for empty table ({} != {} in lua);
  > Rewrite extmarks to work more consistently.
  > Update parsed nodes.
  > Fix node-gravities for exitNodes'.
  > Remove prints', fix enter_node.
  > Use only one node to mark beginning and end of node.
  > Merge pull request wincent#92 from leiserfg/select
  > If lambda returns nil, use default of "" to prevent errors.
  > Format with stylua
  > Add dynamic lambdas+examples.
  > Format with stylua
  > Add nonempty-node. Inserts text if arg-node is not empty.
  > Add some examples for `match()`.
  > Adjust default-value for `match()`
  > Merge pull request wincent#86 from leiserfg/match_with_lambda
  > BREAKING CHANGE: Move pseudo-nodes from ls.util.functions to ls.extras.
  > Quickly shuffle around signature of `match()`.
  > Merge pull request wincent#84 from leiserfg/match_with_lambda
  > Auto generate docs
  > Merge pull request wincent#83 from praveendhawan/patch-1
  > Auto generate docs
  > Format with stylua
  > BREAKING CHANGE: Default `wordTrig` to true, not false.
  > Add expand and expandable function+vim-versions (close wincent#79).
  > Luasnip_current_nodes may (somehow) be nil, check before indexing.
  > Format with stylua
  > Add partial function+example (closes wincent#72).
  > Auto generate docs
  > Replace table with list, md2vim doesn't like tables.
  > Auto generate docs
  > Fix table in Doc.
  > Merge pull request wincent#69 from leiserfg/md-doc
  > Alias set_config to setup.
  > Merge pull request wincent#29 from tjdevries/update_config_correctly
  > Initialize function+dynamicNodes when cildren of choiceNode.
  > Format with stylua
  > Add Examples for recently added nodes.
  > Repeat doesn't need lambdas.
  > Merge pull request wincent#67 from leiserfg/lambda-util
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants