diff options
author | carsakiller <carsakiller@gmail.com> | 2024-05-08 19:52:15 +0000 |
---|---|---|
committer | carsakiller <carsakiller@gmail.com> | 2024-05-08 19:52:15 +0000 |
commit | 36994ae9a8943755fe7b37b0d8377182a7c381e4 (patch) | |
tree | 56a663229fc3cbcecd606a4f3ccfbf4dc870d78d /script | |
parent | a47b432b13f1d4bfa415185bb305b3672bc22d7d (diff) | |
download | lua-language-server-36994ae9a8943755fe7b37b0d8377182a7c381e4.zip |
add: resolve links to symbols in markdown descriptions
Links like [mySymbol](lua://mySymbol) in a comment will now be resolved to a URI pointing to the actual file where `mySymbol` can be found.
Diffstat (limited to 'script')
-rw-r--r-- | script/provider/markdown.lua | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/script/provider/markdown.lua b/script/provider/markdown.lua index fe1b26b2..b5478567 100644 --- a/script/provider/markdown.lua +++ b/script/provider/markdown.lua @@ -1,3 +1,6 @@ +local wssymbol = require("core.workspace-symbol") +local guide = require("parser.guide") + ---@class markdown local mt = {} mt.__index = mt @@ -5,6 +8,34 @@ mt.__name = 'markdown' mt._splitLine = false +---@async +---Converts `[mySymbol](lua://mySymbol)` into a link that points to the origin of `mySymbol`. +---@param txt string +local function processSymbolReferences(txt) + local function replacer(linkText, symbol) + local source ---@type table + + for _, match in ipairs(wssymbol(symbol)) do + if match.name == symbol then + source = match.source + break + end + end + + if not source then + log.warn(string.format("Failed to find source of %q symbol in markdown comment", symbol)) + return + end + + local row, _ = guide.rowColOf(source.start) + local uri = string.format("%s#%i", guide.getUri(source), row + 1) + + return string.format("[%s](%s)", linkText, uri) + end + + return string.gsub(txt, "%[([^]]*)%]%(lua://([^)]+)%)", replacer) +end + function mt:__tostring() return self:string() end @@ -104,7 +135,7 @@ function mt:string(nl) end end end - lines[#lines+1] = obj.text + lines[#lines + 1] = processSymbolReferences(obj.text) language = obj.language end end |