-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Completion caching (simplified) #148
Changes from 13 commits
35fc573
be932f2
0591197
6b126e9
9dfbb12
1d9f69e
424b130
6aadffa
f2cd92d
fa1013e
54d1528
316d0c2
c038e78
eb29c18
0c59a28
c618207
9085997
a678055
b713a31
1d928e1
e307d61
863e9df
623642d
5697ea8
0045b51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
-- __emacs_lua_complete: | ||
-- This function finds and prints a limited number of matches on the | ||
-- lua globals tree, _G. See `lua-complete-string`. N.B.: Does not | ||
-- complete numeric keys (e.g. for arrays) or keys which begin with an | ||
-- underscore. | ||
-- Arguments: | ||
-- PARTS: a table array of dot-separated parts of the completion item | ||
-- (e.g. table.con would become {"table","con"}). All but the last | ||
-- element must be valid subkeys in the _G heirarchy. | ||
-- LIBS: an array of tables of the form | ||
-- {var: varname, lib: libfile} | ||
-- for e.g. local varname=require("libfile"). This library will be | ||
-- require'd and searched alongside global variables. | ||
-- LOCALS: an array of strings parsed from local aloc,bloc = ... to | ||
-- complete as well | ||
-- | ||
-- Example: | ||
-- __emacs_lua_complete({'table','ins'},{{var="xyz",lib="libfile"}}, | ||
-- {"frank","beans"},5000) | ||
-- | ||
-- Written 2019/05 J.D. Smith | ||
function __emacs_lua_complete(parts,libs,locals,limit) | ||
-- Setup a FIFO queue for breadth-first tree traversal | ||
local queue={head=0,tail=0} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coming from Python I feel like this line is missing whitespaces around equals signs (or binary operators). What are the common style guidelines in Lua world? I saw some packages defining whitespace requirements, and some just deferring to "whatever is the default in PIL". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lua is very lenient about whitespace in either direction, but I can certainly add it in for visual clarity. Added. BTW, I will alter history in this branch after the fact to avoid the clutter of small commits like these, so please don't rely on specific commit ID's! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, let's please add whitespace around binary operators and newlines where necessary, now that there's no necessity to keep the size to the minimum. And things like
could probably use a touch to clarify ( |
||
function queue:push(value) | ||
self.tail=self.tail+1 self[self.tail]=value | ||
end | ||
function queue:pop() | ||
self.head=self.head+1 return self[self.head] | ||
end | ||
|
||
-- Mirror the globals table | ||
local globals,cnt,limit={},0,limit or 2000 | ||
for k,v in pairs(_G) do globals[k] = v end | ||
|
||
-- Add libs and locals | ||
if libs then | ||
for _,v in ipairs(libs) do | ||
local good,lib=pcall(require,v.lib) | ||
globals[v.var]=good and lib or {} | ||
end | ||
end | ||
if locals then | ||
for _,v in ipairs(locals) do globals[v]={} end | ||
end | ||
|
||
-- Descend the tree to the limit of dotted parts | ||
local g,pre=globals,"" | ||
for i=1,#parts do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, what I'm saying is that you are basically doing
whereas
and you get to keep both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, fixed. |
||
if not g then break end -- should always exist | ||
if i<#parts then -- keep descending | ||
pre = (i > 1 and pre.."." or "")..parts[i] | ||
g = g[parts[i]] | ||
else -- final part; seed the queue with any matching tables | ||
for k,v in pairs(g) do | ||
if type(k)=="string" and k:sub(1,1) ~= "_" and k:find('^'..parts[i]) then | ||
local dpath=(i > 1 and pre.."." or "")..k | ||
print(dpath) | ||
immerrr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cnt=cnt+1 | ||
if type(v)=="table" then queue:push({pre=dpath,entries=v}) end | ||
end | ||
end | ||
end | ||
end | ||
|
||
-- Perform a limited breadth-first traversal of the queued tables | ||
while queue.head < queue.tail and cnt < limit do | ||
local q=queue:pop() | ||
for k,v in pairs(q.entries) do | ||
if type(k) == "string" and k:sub(1,1) ~= "_" then | ||
pre = q.pre.."."..k | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember you told me that when keys come from completions, they are verified that they don't contain whitespaces or dots? Here, if I'm not mistaken, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Yeah any valid key in Lua can be in (mapcar 'lua-completion-trim-input (split-string expr "\\.")) Wouldn't be terrible to update this to take the expression and remove comments, find brackets etc. Right now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the simplest solution is to just filter out any keys that are not alphanumeric-plus-underscore here. |
||
print(pre) | ||
cnt = cnt + 1 | ||
if cnt >= limit then break end | ||
if type(v) == "table" then queue:push({pre=pre,entries=v}) end | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice step to move this away, it is clearly way too complicated to include in
lua-mode.el
file verbatim. One extra thing to do here is to figure out the packaging part. For example, we could put this intoscripts/
directory, and update MELPA recipe and the Cask file to include(:files :defaults ("scripts", "*"))
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. It also saves constant recompilation every time you complete, and opens the door for more "behind-the-scenes" interaction with lua going forward.
The scripts sounds like a good plan. I created a
scripts
directory and referenced it. I'm not really familiar with Cask; I think you have push access to this branch, if you don't mind sending a commit over.