Skip to content

Commit 789f06b

Browse files
authored
Merge pull request #2656 from carsakiller/2175-md-symbol-reference
add: resolve links to symbols in markdown descriptions
2 parents c082325 + ce40df7 commit 789f06b

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
5+
* `NEW` Reference workspace symbols in comments using `[some text](lua://symbolName)` syntax
56

67
## 3.9.0
78
`2024-5-11`

script/core/completion/completion.lua

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ local function tryWord(state, position, triggerCharacter, results)
15461546
local env = guide.getENV(state.ast, startPos)
15471547
if env then
15481548
checkGlobal(state, word, startPos, position, env, false, results)
1549-
checkModule(state, word, startPos, results)
1549+
checkModule(state, word, startPos, results)
15501550
end
15511551
end
15521552
end
@@ -2256,6 +2256,35 @@ local function tryluaDocOfFunction(doc, results, pad)
22562256
}
22572257
end
22582258

2259+
---Checks for a lua symbol reference in comment
2260+
---@async
2261+
local function trySymbolReference(state, position, results)
2262+
local doc = getLuaDoc(state, position)
2263+
if not doc then
2264+
return
2265+
end
2266+
2267+
local line = doc.originalComment.text ---@type string
2268+
local col = select(2, guide.rowColOf(position)) - 2 ---@type integer
2269+
2270+
-- 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
2271+
-- 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
2272+
local symbol = string.match(string.reverse(line), "%)?([%w%s-_.*]*)//:aul%(%]", #line - col)
2273+
2274+
if symbol then
2275+
-- flip it back the right way around
2276+
symbol = string.reverse(symbol)
2277+
2278+
for _, match in ipairs(wssymbol(symbol)) do
2279+
results[#results+1] = {
2280+
label = match.name,
2281+
kind = define.CompletionItemKind.Class,
2282+
insertText = match.name
2283+
}
2284+
end
2285+
end
2286+
end
2287+
22592288
---@async
22602289
local function tryLuaDoc(state, position, results)
22612290
local doc = getLuaDoc(state, position)
@@ -2327,6 +2356,7 @@ end
23272356
---@async
23282357
local function tryCompletions(state, position, triggerCharacter, results)
23292358
if getComment(state, position) then
2359+
trySymbolReference(state, position, results)
23302360
tryLuaDoc(state, position, results)
23312361
tryComment(state, position, results)
23322362
return

script/provider/markdown.lua

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1+
local wssymbol = require("core.workspace-symbol")
2+
local guide = require("parser.guide")
3+
14
---@class markdown
25
local mt = {}
36
mt.__index = mt
47
mt.__name = 'markdown'
58

69
mt._splitLine = false
710

11+
---@async
12+
---Converts `[mySymbol](lua://mySymbol)` into a link that points to the origin of `mySymbol`.
13+
---@param txt string
14+
local function processSymbolReferences(txt)
15+
local function replacer(linkText, symbol)
16+
local source ---@type table
17+
18+
for _, match in ipairs(wssymbol(symbol)) do
19+
if match.name == symbol then
20+
source = match.source
21+
break
22+
end
23+
end
24+
25+
if not source then
26+
log.warn(string.format("Failed to find source of %q symbol in markdown comment", symbol))
27+
return
28+
end
29+
30+
local row, _ = guide.rowColOf(source.start)
31+
local uri = string.format("%s#%i", guide.getUri(source), row + 1)
32+
33+
return string.format("[%s](%s)", linkText, uri)
34+
end
35+
36+
return string.gsub(txt, "%[([^]]*)%]%(lua://([^)]+)%)", replacer)
37+
end
38+
839
function mt:__tostring()
940
return self:string()
1041
end
@@ -104,7 +135,7 @@ function mt:string(nl)
104135
end
105136
end
106137
end
107-
lines[#lines+1] = obj.text
138+
lines[#lines + 1] = processSymbolReferences(obj.text)
108139
language = obj.language
109140
end
110141
end

0 commit comments

Comments
 (0)