summaryrefslogtreecommitdiff
path: root/script/plugins/astHelper.lua
diff options
context:
space:
mode:
authorfesily <fesil@foxmail.com>2024-01-09 14:00:49 +0800
committerfesily <fesil@foxmail.com>2024-01-09 14:00:49 +0800
commit281ba5f8c4a8cb15e53acc5f3f0d4f43872ea941 (patch)
tree5f0ee208d1a1187e40db410851d3ff84abb8dc79 /script/plugins/astHelper.lua
parent7e895e9f9e9d81b6e78400976b5148a3d106dc74 (diff)
downloadlua-language-server-281ba5f8c4a8cb15e53acc5f3f0d4f43872ea941.zip
plugin: add OnTransformAst interface
Diffstat (limited to 'script/plugins/astHelper.lua')
-rw-r--r--script/plugins/astHelper.lua66
1 files changed, 66 insertions, 0 deletions
diff --git a/script/plugins/astHelper.lua b/script/plugins/astHelper.lua
new file mode 100644
index 00000000..6f303c79
--- /dev/null
+++ b/script/plugins/astHelper.lua
@@ -0,0 +1,66 @@
+local luadoc = require 'parser.luadoc'
+local ssub = require 'core.substring'
+local guide = require 'parser.guide'
+local _M = {}
+
+function _M.buildComment(t, value)
+ return {
+ type = 'comment.short',
+ start = 1,
+ finish = 1,
+ text = "-@" .. t .. " " .. value,
+ }
+end
+
+function _M.InsertDoc(ast, comm)
+ local comms = ast.state.comms or {}
+ comms[#comms+1] = comm
+ ast.state.comms = comms
+end
+
+--- give the local/global variable add doc.class
+---@param ast parser.object
+---@param source parser.object local/global variable
+---@param classname string
+function _M.addClassDoc(ast, source, classname)
+ if source.type ~= 'local' and not guide.isGlobal(source) then
+ return false
+ end
+ --TODO fileds
+ --TODO callers
+ local comment = _M.buildComment("class", classname)
+ comment.start = source.start - 1
+ comment.finish = comment.start
+
+ return luadoc.buildAndBindDoc(ast, source, comment)
+end
+
+---remove `ast` function node `index` arg, the variable will be the function local variable
+---@param source parser.object function node
+---@param index integer
+---@return parser.object?
+function _M.removeArg(source, index)
+ if source.type == 'function' then
+ local arg = table.remove(source.args, index)
+ if not arg then
+ return nil
+ end
+ arg.parent = arg.parent.parent
+ return arg
+ end
+ return nil
+end
+
+--- 把特定函数当成构造函数,`index` 参数是self
+---@param classname string
+---@param source parser.object function node
+---@param index integer
+function _M.addClassDocAtParam(ast, classname, source, index)
+ local arg = _M.removeArg(source, index)
+ if arg then
+ return _M.addClassDoc(ast, arg, classname)
+ end
+ return false
+end
+
+return _M