Hi, so after a discussion started here, I decided I should try and play a bit with Lua ASTs and make an Atom autocomplete+ provider for Lua. I made this thread to share progress.
I’m not sure if this should be here or in The Defoldmine. Correct me if I’m wrong.
So, the repo is at dapetcu21/atom-autocomplete-lua
Get it for Atom here: https://atom.io/packages/autocomplete-lua
I also made a package specifically for Defold which parses the reference docs: https://atom.io/packages/defold-ide
The idea
I started with the idea of structural type definitions. Type definitions for primitives are straightforward. For tables I store the type definition of the fields they might contain and the type definition of their metatable (if known).
Then, as I would parse, I would evaluate the types of expressions and update the type definitions as I scan by assignments and function definitions.
I solved the problem of variable scopes by treating them like tables and assigning them type defs, since table type defs can inherit from each other with metatables.
This type system lets me able to do more advanced stuff like inferring the return type of a function:
local cat = { __index: {} }
function cat.new()
local inst = {}
setmetatable(inst, cat)
return inst
end
function cat.__index:meow()
print('meow')
end
local tom = cat.new()
tom: -- this would suggest meow()
What I have now
- Discovers symbols from assignments, initializers, declarations or just plain usage of tables
- Dot completion with
.
and:
- Roughly detects the types of the symbols
- Respects scope (even if you shadow a global var with a local one)
- Can “see through”
setmetatable()
and__index
- Infers the return type of functions
- Can be configured with a
.luacompleterc