diff options
author | 2019-11-08 23:50:57 -0500 | |
---|---|---|
committer | 2019-11-08 23:50:57 -0500 | |
commit | ee9e8700ae84aa246ec01422aef83b673b5b1cf7 (patch) | |
tree | c808da714d412d4c6d5972932e9ce384ff3dff55 /modules | |
parent | e7d1e26a6db30304f61a59702cbca2f92d673af0 (diff) | |
download | textadept-ee9e8700ae84aa246ec01422aef83b673b5b1cf7.tar.gz textadept-ee9e8700ae84aa246ec01422aef83b673b5b1cf7.zip |
API file lists and Lua tags lists can contain functions that return file paths.
This gives more control over when to include certain autocompletion and
documentation files like Textadept's API.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/lua/init.lua | 52 | ||||
-rw-r--r-- | modules/textadept/command_entry.lua | 9 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 7 |
3 files changed, 31 insertions, 37 deletions
diff --git a/modules/lua/init.lua b/modules/lua/init.lua index 9cbb06b5..f25b6b82 100644 --- a/modules/lua/init.lua +++ b/modules/lua/init.lua @@ -10,15 +10,33 @@ module('_M.lua')]] -- Autocompletion and documentation. +-- Returns a function that, when called from a Textadept Lua file or the Lua +-- command entry, returns the given Textadept tags or API file for use in +-- autocompletion and documentation. +-- @param filename Textadept tags or api file to return. +local function ta_api(filename) + return function() + if (buffer.filename or ''):find('^'.._HOME:gsub('%p', '%%%0')) or + (buffer.filename or ''):find('^'.._USERHOME:gsub('%p', '%%%0')) or + buffer == ui.command_entry then + return filename + end + end +end + --- --- List of "fake" ctags files to use for autocompletion. +-- List of "fake" ctags files (or functions that return such files) to use for +-- autocompletion. -- The kind 'm' is recognized as a module, 'f' as a function, 't' as a table and -- 'F' as a module or table field. -- The *modules/lua/tadoc.lua* script can generate *tags* and -- [*api*](#textadept.editing.api_files) files for Lua modules via LuaDoc. -- @class table -- @name tags -M.tags = {_HOME..'/modules/lua/tags', _USERHOME..'/modules/lua/tags'} +M.tags = { + _HOME..'/modules/lua/tags', _USERHOME..'/modules/lua/tags', + ta_api(_HOME..'/modules/lua/ta_tags') +} --- -- Map of expression patterns to their types. @@ -54,8 +72,10 @@ textadept.editing.autocompleters.lua = function() local name_patt = '^'..part local sep = string.char(buffer.auto_c_type_separator) for i = 1, #M.tags do - if lfs.attributes(M.tags[i]) then - for tag_line in io.lines(M.tags[i]) do + local file = M.tags[i] + if type(file) == 'function' then file = file() end + if file and lfs.attributes(file) then + for tag_line in io.lines(file) do local name = tag_line:match('^%S+') if name:find(name_patt) and not list[name] then local fields = tag_line:match(';"\t(.*)$') @@ -73,30 +93,10 @@ textadept.editing.autocompleters.lua = function() end textadept.editing.api_files.lua = { - _HOME..'/modules/lua/api', _USERHOME..'/modules/lua/api' + _HOME..'/modules/lua/api', _USERHOME..'/modules/lua/api', + ta_api(_HOME..'/modules/lua/ta_api') } --- For Lua buffers, enable or disable Textadept API autocompletion and --- documentation depending on `buffer.filename`. -local function update_textadept_tags_api() - if buffer:get_lexer() ~= 'lua' then return end - local tags, api = M.tags, textadept.editing.api_files.lua - if (buffer.filename or ''):find('^'.._HOME:gsub('%p', '%%%0')) or - (buffer.filename or ''):find('^'.._USERHOME:gsub('%p', '%%%0')) then - if not tags[_HOME] then - tags[#tags + 1] = _HOME..'/modules/lua/ta_tags' - api[#api + 1] = _HOME..'/modules/lua/ta_api' - tags[_HOME], api[_HOME] = #tags, #api - end - elseif tags[_HOME] then - table.remove(tags, tags[_HOME]) - table.remove(api, api[_HOME]) - tags[_HOME], api[_HOME] = nil, nil - end -end -events.connect(events.LEXER_LOADED, update_textadept_tags_api) -events.connect(events.VIEW_AFTER_SWITCH, update_textadept_tags_api) - -- Commands. --- diff --git a/modules/textadept/command_entry.lua b/modules/textadept/command_entry.lua index b506757a..d858f071 100644 --- a/modules/textadept/command_entry.lua +++ b/modules/textadept/command_entry.lua @@ -203,21 +203,12 @@ events.connect(events.INITIALIZED, function() for key, f in pairs(keys) do if f == textadept.editing.show_documentation then lua_mode_keys[key] = function() - -- Enable Textadept API documentation unless the Lua module already - -- enabled it. - local api_files = textadept.editing.api_files - if not api_files.lua[_HOME] then - api_files.lua[#api_files.lua + 1] = _HOME..'/modules/lua/ta_api' - end -- Temporarily change _G.buffer since ui.command_entry is the "active" -- buffer. local orig_buffer = _G.buffer _G.buffer = ui.command_entry textadept.editing.show_documentation() _G.buffer = orig_buffer - -- Disable Textadept API documentation unless the Lua module previously - -- enabled it. - if not api_files.lua[_HOME] then api_files.lua[#api_files.lua] = nil end end break end diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index 35016c71..fe54e1ed 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -113,6 +113,7 @@ M.autocompleters = {} --- -- Map of lexer names to API documentation file tables. +-- File tables contain API file paths or functions that return such paths. -- Each line in an API file consists of a symbol name (not a fully qualified -- symbol name), a space character, and that symbol's documentation. "\n" -- represents a newline character. @@ -697,8 +698,10 @@ function M.show_documentation(pos, case_insensitive) end) end for i = 1, #M.api_files[lang] do - if lfs.attributes(M.api_files[lang][i]) then - for line in io.lines(M.api_files[lang][i]) do + local file = M.api_files[lang][i] + if type(file) == 'function' then file = file() end + if file and lfs.attributes(file) then + for line in io.lines(file) do if line:find(symbol_patt) then api_docs[#api_docs + 1] = line:match(symbol_patt..'%s+(.+)$') end |