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(lua): for / function snippets overhaul #537

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

WieeRd
Copy link

@WieeRd WieeRd commented Dec 17, 2024

Rewrite and add more snippets for the for statements and functions to cover more use cases.

For statements

Before

-- `for`
for i=1,10 do ... end

-- `foreach`
for i, x in pairs(table) do ... end

After

-- `for`
for ... do ... end

-- `forn` (for-numeric)
for i = 1, 10 do ... end

-- `fori` (for-ipairs)
for i, x in ipairs(t) ... end

-- `forp` (for-pairs)
for k, v in pairs(t) ... end
  • for is now a generic for loop with zero assumption
  • Added variants for 3 typical for loops: numeric range, array table, dictionary table
  • Added spacing in numeric for: from i=1,10 to i = 1, 10
    • The latter seems predominant on most conventions and formatters.

Functions

Before

-- `fu` (function)
function name(args) ... end

-- `f=` (inline-function)
local name = function(args) ... end

After

-- `fu` (function)
function(args) ... end
--      ^ note: jump target with empty placeholder between `function` and `(`

-- `f=` (assign-function)
name = function(args) ... end

-- `lfu` (local-function)
local function name(args) ... end

-- `lf=` (local-assign-function)
local name = function(args) ... end

-- `f,` (member-function)
-- local tbl = {
       name = function(args) ... end,
--     existing_key_below = 42,
-- }
  • *fu / *f= to support both function name() and name = function() style.
  • fu: Removed space and name placeholder between function and (
    • This is in case the function is being used as an anonymous function (typically callbacks)
      vim.wait(300, function()
        print("300ms later")
      end)
    • The jump target itself still exists between function and (
  • Obvious l* local variants
  • member-function: Same as f= but with comma at the end,.
    If you assign a function to a key using f=, syntax highlights will be broken and the LSP complains until you finally insert the , after finishing writing the function.

Misc

Capitalized the descriptions of the snippets.

Not entirely sure about the styling / punctuation convention for the descriptions and names, though.
The CONTRIBUTING.md is missing, and I see no consistency in other language snippets I looked up for reference.
Some snippets are named foo-bar like here in lua.json, some are Foo bar, I don't know.

@WieeRd
Copy link
Author

WieeRd commented Dec 17, 2024

Maybe I should create a separate snippet for anonymous functions rather than emptying the name placeholder of fu, for consistency and convenience.

-- `fu` (function)
function name(args) ... end

-- `??` (anonymous-function)
function(args) ... end

What do you think? What should its prefix be?
I'm considering f) since f( would trigger auto pairs and turn into f().

@WieeRd
Copy link
Author

WieeRd commented Dec 17, 2024

Played around it, and it was a lot more convenient than having to tap twice using fu.

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

Successfully merging this pull request may close these issues.

1 participant