require call autocomplete

Corona Editor’s really come along nicely now, but one thing it lacks: require parameter autocomplete!

I try to organise my code into folders, but that can mean that sometimes (nearly always) I forget which folder I’ve stuck a file in. I’m forever hitting ⌘+P to find the file, and then the best solution I’ve found is to use SidebarEnhancements’ “File: Copy Path from Project Encoded” command to get the file’s path. However that still has /'s as separators instead of .'s and it’s be nicer if this all happened intelligently after typing the require command.

I think this could be added by detecting if the characters preceding the caret match require\s?(?" and then adding correctly encoded paths from the project to the autocomplete (filtering them as you type). That way you’d narrow down to the path you want as you type.

What do people think? I might have a go at it if that sounds about right once I’ve got a free moment. 

Let me know if you get this working and I’ll look at including it in Corona Editor (with appropriate acknowledgment of course). 

Cool, I’ve added it to my pull request. It’s actually pretty simple! No doubt it’ll need refining. I find Sublime’s API a little tricky to use, or at least to get at what I want. 

Here’s the change:

https://github.com/personalnadir/CoronaSDK-SublimeText/commit/730eafc739f7ae673dc46a45b0218bd958f2ff4a

It does require the user to have set up a Sublime project with the folders specified. (and I’ve just made some changes to fail gracefully in another commit).

After playing around with it a bit more I think it needs to be more general. At the moment it only inserts suggestions when at the beginning of the require statement (i.e. immediately after the first "). It should instead continue to add suggestions while inside the quotes. 

Also I realised it won’t work with single quotes (or [[)

There’s been a few further commits to it. Turns out there was a mistake in the original which caused the regex to match against the entire document up to the cursor. Once I removed the $ from the regex, that became apparent! 

It’s also worth saying that adding the following snippet along with the changes makes a the experience a joy:

\<snippet\> &nbsp; \<content\>\<![CDATA[local ${2:module}=require "${1:path}"]]\>\</content\> &nbsp; \<!-- Optional: Set a tabTrigger to define how to trigger the snippet --\> &nbsp; \<tabTrigger\>require\</tabTrigger\> &nbsp; \<!-- Optional: Set a scope to limit where the snippet will trigger --\> &nbsp; \<scope\>source.lua\</scope\> \</snippet\> &nbsp;

Basically it just creates a simple template

[lua]

local <module> = require “<path>”

[/lua] 

But orders it so that you complete the path first (with the new autocomplete) before the name of the local variable you’re storing the reference in. Doing it in that order means that the path name will be in the document’s list of completions when you come to name the variable. 

I’ve updated the code to no longer require a project path to be set. If one isn’t set (or if it’s relative) it now searches the open folders for a main.lua file and uses the directory of that as a root path to search for modules.

Let me know if you get this working and I’ll look at including it in Corona Editor (with appropriate acknowledgment of course). 

Cool, I’ve added it to my pull request. It’s actually pretty simple! No doubt it’ll need refining. I find Sublime’s API a little tricky to use, or at least to get at what I want. 

Here’s the change:

https://github.com/personalnadir/CoronaSDK-SublimeText/commit/730eafc739f7ae673dc46a45b0218bd958f2ff4a

It does require the user to have set up a Sublime project with the folders specified. (and I’ve just made some changes to fail gracefully in another commit).

After playing around with it a bit more I think it needs to be more general. At the moment it only inserts suggestions when at the beginning of the require statement (i.e. immediately after the first "). It should instead continue to add suggestions while inside the quotes. 

Also I realised it won’t work with single quotes (or [[)

There’s been a few further commits to it. Turns out there was a mistake in the original which caused the regex to match against the entire document up to the cursor. Once I removed the $ from the regex, that became apparent! 

It’s also worth saying that adding the following snippet along with the changes makes a the experience a joy:

\<snippet\> &nbsp; \<content\>\<![CDATA[local ${2:module}=require "${1:path}"]]\>\</content\> &nbsp; \<!-- Optional: Set a tabTrigger to define how to trigger the snippet --\> &nbsp; \<tabTrigger\>require\</tabTrigger\> &nbsp; \<!-- Optional: Set a scope to limit where the snippet will trigger --\> &nbsp; \<scope\>source.lua\</scope\> \</snippet\> &nbsp;

Basically it just creates a simple template

[lua]

local <module> = require “<path>”

[/lua] 

But orders it so that you complete the path first (with the new autocomplete) before the name of the local variable you’re storing the reference in. Doing it in that order means that the path name will be in the document’s list of completions when you come to name the variable. 

I’ve updated the code to no longer require a project path to be set. If one isn’t set (or if it’s relative) it now searches the open folders for a main.lua file and uses the directory of that as a root path to search for modules.