Skip to content
Adam Wagner edited this page Nov 29, 2020 · 15 revisions

Contents

  1. lua package manager
  2. Get lua code completion in neovim

install luarocks

luarocks is the package manager for the Lua programming language. It is the easiest way to install each of the lua development tools described below.

brew install luarocks

setup lua code-completion in (neo)vim

Language-server-protocol (LSP) enables content and library aware code completion in your editor.

In VSCode, enabling LSP-powered code completion is as easy as installing an extension. Read on to see how to get the same experience in vim or neovim.

Install lus-lsp with luarocks

See lua package manager if you haven't installed luarocks.

luarocks install lua-lsp

Add this section to your coc-settings.json:

"languageserver": {
        "lua": {
            "command": "YOUR-PATH-TO/lua-language-server/bin/macOS/lua-language-server",
            "args": ["-E", "YOUR-PATH-TO/lua-language-server/main.lua"],
            "filetypes": ["lua"],
            "rootPatterns": [".git/"],
            "settings": {
                "Lua": {
                    "workspace": {
                        "library": {
                            "YOUR-PATH-TO/hs-lsp-completion/build/stubs": true,
                            "YOUR-PATH-TO/neovim/runtime/lua": true,
                            "YOUR-PATH-TO/neovim/src/nvim/lua": true
                        },
                        "ignoreDir": [".cache"],
                        "maxPreload": 2000,
                        "preloadFileSize": 1000
                    },
                    "runtime": {
                        "version": "5.4.0"
                    },
                    "diagnostics": {
                        "enable": true,
                        "disable": ["lowercase-global", "unused-local"],
                        "globals": [
                            "hs",
                            "vim",
                            "it",
                            "describe",
                            "setup",
                            "teardown",
                            "before_each",
                            "after_each",
                            "pending",
                            "insulate"
                        ]
                    },
                    "completion": {
                        "keywordSnippet": "Disable"
                    }
                }
            }
        }

},
"diagnostic-languageserver.formatters": {
    "lua-format": {
        "command": "lua-format",
        "args": ["-c", "~/.config/luaformatter/config.yaml"]

    }
},
"diagnostic-languageserver.formatFiletypes": {
     "lua": "lua-format"
},

Setup code completion specifically for Hammerspoon modules

Follow the instructions here.

I've created a python script that uses build/docs.json to generate Hammerspoon API stubs using EmmyLua annotations These kinds of annotations are supported by the following lsp servers:

hammerspoon-annotations


Linting with luacheck

If you're not using VS Code, there is still hope. Here's my setup with neovim:

luarocks install luacheck

~/.luacheckrc config file:

-- vim:set ft=lua:
stds.hs = {
    globals = {
        hs = {other_fields = true},
        spoon = {other_fields = true},
        vim = {other_fields = true},
    },
}

exclude_files = {"/Users/adamwagner/.luacheckrc"}

std = 'max+hs'
allow_defined = true

globals = {"hs", "rawrequire"}

ignore = {
    "113",
    "211/_.*", -- unused local var, except when prefixed with  "_"
    "212/_.*", -- unused argument, except when prefixed with  "_"
    "213/_.*", -- unused var in loop, except when prefixed with "_"
    "212/self", -- ignore self
    "631",
    "614",
}

-- "423", -- Shadowing a loop variable
-- "211", -- Unused local variable
-- "212", -- Unused argument
-- "213", -- Unused loop variable

.luacheckrc inspiration:

Config lua-lsp and luacheck for coc.nvim:

Add this section to your coc-settings.json:

"diagnostic-languageserver.filetypes": {
    "lua": "luacheck"
},

Lua formatter

luarocks install luaformatter

Add this section to your coc-settings.json:

"diagnostic-languageserver.formatFiletypes": {
    "lua": "lua-format"
},

Debugging lua

note: I haven't set this up yet, so can't vouch for it.

renatomaia/loop-debugging

LOOP classes of debugging utilities for Lua.

debugger.lua

A simple, embedabble CLI debugger for Lua.


Lua libraries & utilities

Lua's stdlib is very thin, to say the least. Many common operations and patterns will require you to either find a library or write your own. The upside is a sort of charm that is earned by rolling your own low-level utils. Or, at minimum, researching available libraries and deciding which is best.


Caching & queing in lua

pyericz/LuaWorkQueue

A work queue implementation written in Lua.

darkwark/queue-lua

Queue implementation for Lua and PICO-8 Newer (2020)

hewenning/Lua-Container

🌏 Implement various containers, stack, queue, priority queue, heap, A* algorithms, etc. through Lua.

KurtLoeffler/Lua_CachePool

A lua library for creating pools of cached objects.


Functional utility belt

Yonaba/Moses is clearly the leading functional utility library for lua. I chose not to use Moses in stackline due to it's multi-file architecture. It's meant to be installed with luarocks, which increases installation complexity. Setting that aside, browsing the Moses sourcecode is generally instructive.


Next: Working with Hammerspoon →