Skip to content

Instantly share code, notes, and snippets.

@shadmansaleh
Last active November 9, 2020 08:17
Show Gist options
  • Save shadmansaleh/7bd9eaf9697491e39758576c0a4731fa to your computer and use it in GitHub Desktop.
Save shadmansaleh/7bd9eaf9697491e39758576c0a4731fa to your computer and use it in GitHub Desktop.
local lua_complete = {}
lua_complete.data = {}
lua_complete.__loadData = function(parent, parent_name)
-- check whether parent_name was passed
if parent_name == nil then
parent_name = ""
-- add . to parent_name when it's not empty
elseif parent_name ~= "" then
parent_name = parent_name .. "."
end
-- loop through dictionary
for child_name,child in pairs(parent) do
table.insert(lua_complete.data, parent_name..child_name)
-- recursivly call getData
-- TODO check for recursive entries properly
if type(child) == "table" and
not string.match(parent_name, child_name) and
not string.match(parent_name, "lua_complete") and
child_name ~= "_G" then
lua_complete.__loadData(child, parent_name..child_name)
end
end
end
function lua_complete.__sortData()
-- sort the data before writing so the suggetions are more relavent
function dots(str)
-- used to compare to strings to check how many (.) it has
count = 0
pos = -1
while true do
pos = string.find(str, "\[.\]", pos+1)
if pos == nil then
break
end
count = count + 1
end
return count
end
function compare(a, b)
-- Campare function used for sorting
-- Compares based on 2 conditions how many (.) is has and alfabatical order
dota = dots(a)
dotb = dots(b)
if dota == dotb then
return a < b
else
return dota < dotb
end
end
-- Sort the data
table.sort(lua_complete.data, compare)
end
function lua_complete.__writeData()
-- Write the data to file
for _,val in ipairs(lua_complete.data) do
lua_complete.file:write(val..'\n')
end
end
function lua_complete.reload()
-- This function recreats the dictionary
-- Create cache dir if it doesn't exist
local cache_dir = vim.fn.stdpath("cache").."/lua_complete"
if vim.fn.isdirectory(cache_dir) == 0 then
vim.fn.mkdir(cache_dir)
end
-- run the loading prossess in a different thread (WIP)
-- co = coroutine.create(function()
lua_complete.file = io.open(cache_dir.."/lua_dictionary.txt","w")
lua_complete.__loadData(_G)
lua_complete.__sortData()
lua_complete.__writeData()
-- free resources
lua_complete.file:close()
lua_complete.data = nil
--end)
-- coroutine.resume(co)
end
function lua_complete.load()
-- Loads the dictionary on luafiles
local fname = vim.fn.stdpath("cache").."/lua_complete/lua_dictionary.txt"
-- Create dictionary if it doesn't exist
if vim.fn.filereadable(fname) == 0 then
lua_complete.reload()
end
-- vim commands
vim.cmd("augroup Lua_Complete")
vim.cmd("autocmd!")
vim.cmd("au Filetype lua setlocal complete+=k")
vim.cmd("au Filetype lua setlocal dictionary+="..fname)
vim.cmd("au Filetype lua setlocal iskeyword+=.")
vim.cmd("au Filetype lua setlocal iskeyword+=:")
vim.cmd("augroup END")
end
return lua_complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment