Lua autocomplete for Atom


#1

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


Adding a function of using other code editors and offline mode
Auto complete not working (SOLVED)
Triggering hot reloading externally
I'm new to Lua, so I'm confused
#2

Very nice! This will be quite useful!


#3

Here’s some more progress. I’ll publish to APM soon.


#4

Wow! Very helpful! Thanks!


#5

Aaand, it’s up: https://atom.io/packages/autocomplete-lua

Remember, this is still work in progress. There is no support for defining globals yet, no standard library definitions and the type inference system is nowhere complete.


#6

It now also scans all the table members you’ve used and suggests them:

I still have some scoping issues to iron out.

…and by this point I should start writing tests… the dread…


#7

Ladies and gents, I give you Atom Defold IDE:

It automatically downloads the API refs and keeps itself up to date.


#8

Brilliant! Than you for the time you’ve put into this poject. Did you have any prior experience creating Atom plugins?


#9

Nope. First Atom plugins. But I have a lot of experience with JS.


#10

It’s great! Very useful plugin. Thank you!


#11

Nothing brilliant to say, I just want to thanks and encourage you to keep doing it :wink:


#12

Here’s some more progress:

It’s aware of setmetatable() and keeps track of function return types:


#13

Looking awesome so far! How far off is it to get autocomplete for requires? I’ve tested most IDEs with autocomplete for LUA but not completely happy with any so far. LDT and Lua Glider have been the best so far. Will be nice to test this when requires work!


#14

It’s not very far off. I’ve been slowly heading in that direction with return type detection and some refactoring, but I’m adding definitions for Lua’s standard library first. I have them parsed, just need to do some work on the plugin system before I add them in.


#15

Lua standard library completion and docs:


#16

I also implemented triggering hot reloading from within Atom:

It’s not as instant as from within the Editor, but sure beats all that Alt-Tabbing.


#17

I guess you should be able to also build and lunch using pyfold?


#18

Huh? I don’t get it. What does pyfold have to do with this? You meant Defold? Yes, the intended use case is for you to still Build & Launch, work on collections etc. using the Defold editor, but when it comes to code, since you’re already editing code in Atom, you should be hot reloading without alt tabbing. It’s not meant to replace other aspects of the Defold editor in any way, other than their code editor and hot reloading seems to be a big feature of their code editor that’s desirable to replicate.


#19

I believe Pyfold is not an public library as for now.


#20

To clarify:
pyfold is a python library developed internally at King (but not written nor maintained by the Defold team). The purpose is to simplify different tasks for commandline usage, mainly for CI. I bet it still uses bob though as I don’t think the pyfold authors replicated the entire content pipeline in python? :slight_smile: