Autocomplete and/or Codea being Class-aware

I think I want this, but I’m not 100% sure, so I thought I’d post and see if there’s a downside I don’t see.

I don’t use autocomplete, much - in fact, often, I’m writing code, and since I’m on the bottom line, it pops up and obscures what I’m working on, which is non-optimal. I tend to do 20 carriage-returns just so my new code doesn’t get blocked - but that’s not why I’m here.

I think it would be REALLY COOL - something I would use - if Autocomplete was Class-Savvy. In other words - when I am in a class, and I do "function " - I want it to type “HersheyCodea:” if I’m in the HersheyCodea class. Or if not autocomplete - some other easy way to spit out the class name.

And (probably a much taller order), when I do “font = HersheyCodea()” and I have a HersheyCodea class - and I later type “font:” - I want autocomplete to provide my class functions, as if they were built-ins. I can enumerate a class, which is really just a table internally, and pick out the functions - it should be doable, somehow, to get them dynamically into autocomplete.

I suspect this is NOT how it was coded. I want it anyway. :slight_smile: I don’t expect it soon, or maybe ever, but I wanted to say as I was plugging away tonight I realized it would be really handy.

This is something I’d like to add, each time I think about it I find new problems, though.

One naive way to add it is to keep track of all “= class()” assignments and add those symbols to the autocomplete dictionary. A more naive way is just to add the tab names (except for Main) into the autocomplete.

It’s hard to do

font = HersheyCodea()

Because you might do

font = getMyFont()

or

font = (function genFont() return HersheyCodea() end)()

And I’d have no idea what type the object is.

Hee hee - when I was suggesting it, the back of my mind was saying to me “You know how you would do autocomplete, and you know what a giant pain it would be to make it do what you’re asking for if they coded it the way you would have” :slight_smile:

I would think the editor would have to parse (but not execute) the lua code to do it right, so it could ID which tokens were variables assigned to Class names, and I’m not sure you could do it even then. Alas - I just don’t know enough about Lua (and iphone apps!) to say if it’s easy, hard, or near impossible, so I figured I’d put it out there and see if it was interesting. Never let it be said I won’t suggest a feature just because it would be insanely hard to implement :slight_smile:

What would work for me would be to just add the class names, so that typing f would bring Font up among the autocomplete options.

But what would be MUCH nicer for me would be handling properties and functions. When I’ve defined myFont as an instance of Font, typing myFont. Should get me a list of all Font properties, and typing myFont: should fill the bar with all Font functions.

It’s tricky because classes don’t actually exist in the Lua language. The class() function returns a function that does some metatable magic. And a class is just a variable that refers to the function returned from the function called class.

(I could, if I wanted, write my own function called class that does whatever I want – define a different dispatch model for example. Or write a new function with a different name that called class() and added some useful behaviour, and therefore defined classes but wasn’t called “class”.)

But I don’t think that’s actually a problem. I usually want Codea to autocomplete on my variables, or functions (again, Lua sort of doesn’t have named functions, but has named variables that refer to function values).

I would be happy if Codea offered any global identifier as an autocompletion, whether defined in my code or by Codea itself. This could be done purely by parsing the text and indexing global identifiers found in the parse tree.

Even more helpful would be if Codea could index local identifiers within a function definition and offer those as autocompletions only when the user is editing within that function.

I’d be comfortable with Codea saying “Classes don’t really exist in Lua, but in Codea we have a Class structure we’ve defined, and if you use it we’ll scan those for functions, and add them to autocomplete - if you define your own class types, you’re on your own”.

@Nat unfortunately Lua doesn’t generate a parse tree (or AST), and we don’t do that in Codea either. Codea simply evaluates each line on its own. I think we might be able to index global identifiers similar to how the function browser does it - line-by-line. I just have to be careful not to re-index too often or typing could become quite slow.

@Bortels I think it might come down to that, we’ll end up offering a solution that “works” for a majority of cases and common uses.