Skip to content

Commit 02dd5b6

Browse files
committed
improve ---@see
resolve #1344
1 parent 63510ae commit 02dd5b6

File tree

7 files changed

+114
-2
lines changed

7 files changed

+114
-2
lines changed

Diff for: changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
104104
}
105105
```
106106
* `CHG` find reference: respect `includeDeclaration` (although I don't know how to turn off this option in VSCode)
107+
* `CHG` [#1344] improve `---@see`
107108
* `FIX` [#1567]
108109
* `FIX` [#1593]
109110
* `FIX` [#1595]
@@ -118,6 +119,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
118119
[#1177]: https://github.com/sumneko/lua-language-server/issues/1177
119120
[#1202]: https://github.com/sumneko/lua-language-server/issues/1202
120121
[#1332]: https://github.com/sumneko/lua-language-server/issues/1332
122+
[#1344]: https://github.com/sumneko/lua-language-server/issues/1344
121123
[#1458]: https://github.com/sumneko/lua-language-server/issues/1458
122124
[#1557]: https://github.com/sumneko/lua-language-server/issues/1557
123125
[#1558]: https://github.com/sumneko/lua-language-server/issues/1558

Diff for: script/core/hover/description.lua

+51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local util = require 'utility'
77
local guide = require 'parser.guide'
88
local rpath = require 'workspace.require-path'
99
local furi = require 'file-uri'
10+
local wssymbol = require 'core.workspace-symbol'
1011

1112
local function collectRequire(mode, literal, uri)
1213
local result, searchers
@@ -125,6 +126,52 @@ local function getBindComment(source)
125126
return table.concat(lines, '\n')
126127
end
127128

129+
---@async
130+
local function packSee(see)
131+
local name = see.name[1]
132+
local buf = {}
133+
local target
134+
for _, symbol in ipairs(wssymbol(name)) do
135+
if symbol.name == name then
136+
target = symbol.source
137+
break
138+
end
139+
end
140+
if target then
141+
local row, col = guide.rowColOf(target.start)
142+
buf[#buf+1] = ('[%s](%s#%d#%d)'):format(name, guide.getUri(target), row + 1, col)
143+
else
144+
buf[#buf+1] = ('~%s~'):format(name)
145+
end
146+
if see.comment then
147+
buf[#buf+1] = ' '
148+
buf[#buf+1] = see.comment.text
149+
end
150+
return table.concat(buf)
151+
end
152+
153+
---@async
154+
local function lookUpDocSees(lines, docGroup)
155+
local sees = {}
156+
for _, doc in ipairs(docGroup) do
157+
if doc.type == 'doc.see' then
158+
sees[#sees+1] = doc
159+
end
160+
end
161+
if #sees == 0 then
162+
return
163+
end
164+
if #sees == 1 then
165+
lines[#lines+1] = ('See: %s'):format(packSee(sees[1]))
166+
return
167+
end
168+
lines[#lines+1] = 'See:'
169+
for _, see in ipairs(sees) do
170+
lines[#lines+1] = (' * %s'):format(packSee(see))
171+
end
172+
end
173+
174+
---@async
128175
local function lookUpDocComments(source)
129176
local docGroup = source.bindDocs
130177
if not docGroup then
@@ -158,6 +205,7 @@ local function lookUpDocComments(source)
158205
if source.comment then
159206
lines[#lines+1] = normalizeComment(source.comment.text, uri)
160207
end
208+
lookUpDocSees(lines, docGroup)
161209
if not lines or #lines == 0 then
162210
return nil
163211
end
@@ -352,6 +400,7 @@ local function getFunctionComment(source)
352400
return comment
353401
end
354402

403+
---@async
355404
local function tryDocComment(source)
356405
local md = markdown()
357406
if source.value and source.value.type == 'function' then
@@ -379,6 +428,7 @@ local function tryDocComment(source)
379428
return result
380429
end
381430

431+
---@async
382432
local function tryDocOverloadToComment(source)
383433
if source.type ~= 'doc.type.function' then
384434
return
@@ -457,6 +507,7 @@ local function tryDocEnum(source)
457507
return md:string()
458508
end
459509

510+
---@async
460511
return function (source)
461512
if source.type == 'string' then
462513
return asString(source)

Diff for: script/core/hover/init.lua

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local util = require 'utility'
66
local findSource = require 'core.find-source'
77
local markdown = require 'provider.markdown'
88
local guide = require 'parser.guide'
9+
local wssymbol = require 'core.workspace-symbol'
910

1011
---@async
1112
local function getHover(source)
@@ -14,6 +15,15 @@ local function getHover(source)
1415
local labelMark = {}
1516
local descMark = {}
1617

18+
if source.type == 'doc.see.name' then
19+
for _, symbol in ipairs(wssymbol(source[1])) do
20+
if symbol.name == source[1] then
21+
source = symbol.source
22+
break
23+
end
24+
end
25+
end
26+
1727
---@async
1828
local function addHover(def, checkLable, oop)
1929
if defMark[def] then
@@ -111,6 +121,7 @@ local accept = {
111121
['doc.enum.name'] = true,
112122
['function'] = true,
113123
['doc.module'] = true,
124+
['doc.see.name'] = true,
114125
}
115126

116127
---@async

Diff for: script/parser/guide.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ local childMap = {
160160
['doc.type.field'] = {'name', 'extends'},
161161
['doc.type.sign'] = {'node', '#signs'},
162162
['doc.overload'] = {'overload', 'comment'},
163-
['doc.see'] = {'name'},
163+
['doc.see'] = {'name', 'comment'},
164164
['doc.version'] = {'#versions'},
165165
['doc.diagnostic'] = {'#names'},
166166
['doc.as'] = {'as'},

Diff for: script/parser/luadoc.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,8 @@ local function bindDoc(source, binded)
17031703
or doc.type == 'doc.private'
17041704
or doc.type == 'doc.protected'
17051705
or doc.type == 'doc.public'
1706-
or doc.type == 'doc.package' then
1706+
or doc.type == 'doc.package'
1707+
or doc.type == 'doc.see' then
17071708
if source.type == 'function'
17081709
or isParam then
17091710
goto CONTINUE

Diff for: test/crossfile/hover.lua

+37
Original file line numberDiff line numberDiff line change
@@ -1644,3 +1644,40 @@ function f(x: number)
16441644
function f(x: number, y: number)
16451645
```]]
16461646
}
1647+
1648+
TEST { {path = 'a.lua', content = [[
1649+
---@class A
1650+
1651+
---@see A # comment1
1652+
local <?x?>
1653+
]]},
1654+
hover = [[
1655+
```lua
1656+
local x: unknown
1657+
```
1658+
1659+
---
1660+
1661+
See: [A](file:///a.lua:1:10) comment1]]
1662+
}
1663+
1664+
TEST { {path = 'a.lua', content = [[
1665+
---@class A
1666+
1667+
TTT = 1
1668+
1669+
---@see A # comment1
1670+
---@see TTT # comment2
1671+
local <?x?>
1672+
]]},
1673+
hover = [[
1674+
```lua
1675+
local x: unknown
1676+
```
1677+
1678+
---
1679+
1680+
See:
1681+
* [A](file:///a.lua:1:10) comment1
1682+
* [TTT](file:///a.lua:3:0) comment2]]
1683+
}

Diff for: test/hover/init.lua

+10
Original file line numberDiff line numberDiff line change
@@ -2393,3 +2393,13 @@ local x = obj[''].<?x?>()
23932393
[[
23942394
(field) A.x: fun():string
23952395
]]
2396+
2397+
TEST [[
2398+
---@class A
2399+
---@field x number
2400+
2401+
---@see <?A.x?>
2402+
]]
2403+
[[
2404+
(field) A.x: number
2405+
]]

0 commit comments

Comments
 (0)