From 36994ae9a8943755fe7b37b0d8377182a7c381e4 Mon Sep 17 00:00:00 2001 From: carsakiller Date: Wed, 8 May 2024 19:52:15 +0000 Subject: 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. --- script/provider/markdown.lua | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) 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 -- cgit v1.2.3 From 1f3a500b81ad1d55cf8564de9eca590e9f29e80b Mon Sep 17 00:00:00 2001 From: carsakiller Date: Thu, 23 May 2024 05:49:56 +0000 Subject: add: completion of lua symbol references --- script/core/completion/completion.lua | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index d047dd56..cf2ec745 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -2256,6 +2256,37 @@ local function tryluaDocOfFunction(doc, results, pad) } end +---Checks for a lua symbol reference in comment and returns an async callback if found +local function trySymbolReference(state, position, results) + local doc = getLuaDoc(state, position) + if not doc then + return + end + + local line = doc.originalComment.text ---@type string + local col = select(2, guide.rowColOf(position)) - 2 ---@type integer + + -- User will ask for completion at end of symbol name so we need to perform a reverse match to see if they are in a symbol reference + -- Matching in reverse allows the symbol to be of any length and we can still match all the way back to `](lua://` from right to left + local symbol = string.match(string.reverse(line), "%)?(.*)//:aul%(%]", #line - col) + + if symbol then + -- flip it back the right way around + symbol = string.reverse(symbol) + + ---@async + return function () + for _, match in ipairs(wssymbol(symbol)) do + results[#results+1] = { + label = match.name, + kind = define.CompletionItemKind.Class, + insertText = match.name + } + end + end + end +end + ---@async local function tryLuaDoc(state, position, results) local doc = getLuaDoc(state, position) @@ -2327,8 +2358,13 @@ end ---@async local function tryCompletions(state, position, triggerCharacter, results) if getComment(state, position) then + local callback = trySymbolReference(state, position, results) + if callback then + callback() + else + tryComment(state, position, results) + end tryLuaDoc(state, position, results) - tryComment(state, position, results) return end if postfix(state, position, results) then -- cgit v1.2.3 From b9c9f9423ff31bb9160acc5d75feaec3c0bf777c Mon Sep 17 00:00:00 2001 From: carsakiller Date: Thu, 23 May 2024 06:11:46 +0000 Subject: fix: greedy match --- script/core/completion/completion.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index cf2ec745..dc045474 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -2268,7 +2268,7 @@ local function trySymbolReference(state, position, results) -- User will ask for completion at end of symbol name so we need to perform a reverse match to see if they are in a symbol reference -- Matching in reverse allows the symbol to be of any length and we can still match all the way back to `](lua://` from right to left - local symbol = string.match(string.reverse(line), "%)?(.*)//:aul%(%]", #line - col) + local symbol = string.match(string.reverse(line), "%)?([%w%s-_.*]*)//:aul%(%]", #line - col) if symbol then -- flip it back the right way around -- cgit v1.2.3 From 50ff3a41836e2d3ab872007b679151cf51d6659e Mon Sep 17 00:00:00 2001 From: carsakiller Date: Sat, 25 May 2024 03:36:29 +0000 Subject: fix: remove callback --- script/core/completion/completion.lua | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index dc045474..e9257bea 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -1546,7 +1546,7 @@ local function tryWord(state, position, triggerCharacter, results) local env = guide.getENV(state.ast, startPos) if env then checkGlobal(state, word, startPos, position, env, false, results) - checkModule(state, word, startPos, results) + checkModule(state, word, startPos, results) end end end @@ -2256,7 +2256,8 @@ local function tryluaDocOfFunction(doc, results, pad) } end ----Checks for a lua symbol reference in comment and returns an async callback if found +---Checks for a lua symbol reference in comment +---@async local function trySymbolReference(state, position, results) local doc = getLuaDoc(state, position) if not doc then @@ -2274,15 +2275,12 @@ local function trySymbolReference(state, position, results) -- flip it back the right way around symbol = string.reverse(symbol) - ---@async - return function () - for _, match in ipairs(wssymbol(symbol)) do - results[#results+1] = { - label = match.name, - kind = define.CompletionItemKind.Class, - insertText = match.name - } - end + for _, match in ipairs(wssymbol(symbol)) do + results[#results+1] = { + label = match.name, + kind = define.CompletionItemKind.Class, + insertText = match.name + } end end end @@ -2358,12 +2356,8 @@ end ---@async local function tryCompletions(state, position, triggerCharacter, results) if getComment(state, position) then - local callback = trySymbolReference(state, position, results) - if callback then - callback() - else - tryComment(state, position, results) - end + trySymbolReference(state, position, results) + tryComment(state, position, results) tryLuaDoc(state, position, results) return end -- cgit v1.2.3 From 551cea65a306acdf8ec7ec841a3398b54fbade60 Mon Sep 17 00:00:00 2001 From: carsakiller Date: Sat, 25 May 2024 03:41:49 +0000 Subject: add: entry in changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index 62ca8974..315fa729 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## Unreleased +* `NEW` Reference workspace symbols in comments using `[some text](lua://symbolName)` syntax ## 3.9.0 `2024-5-11` -- cgit v1.2.3 From ce40df78023814708c583f93d28fa0505a353047 Mon Sep 17 00:00:00 2001 From: carsakiller Date: Sat, 25 May 2024 03:51:04 +0000 Subject: fix: restore completion order --- script/core/completion/completion.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index e9257bea..5c55c16b 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -2357,8 +2357,8 @@ end local function tryCompletions(state, position, triggerCharacter, results) if getComment(state, position) then trySymbolReference(state, position, results) - tryComment(state, position, results) tryLuaDoc(state, position, results) + tryComment(state, position, results) return end if postfix(state, position, results) then -- cgit v1.2.3