Giter Site home page Giter Site logo

autoload about wincent HOT 5 CLOSED

SingularisArt avatar SingularisArt commented on June 16, 2024
autoload

from wincent.

Comments (5)

wincent avatar wincent commented on June 16, 2024

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?

from wincent.

SingularisArt avatar SingularisArt commented on June 16, 2024

@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.

from wincent.

SingularisArt avatar SingularisArt commented on June 16, 2024

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!

from wincent.

wincent avatar wincent commented on June 16, 2024

What you've got in there right now looks about right to me β€”Β is it working?

from wincent.

SingularisArt avatar SingularisArt commented on June 16, 2024

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

Thanks so much!

from wincent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.