Skip to content

Commit e794ff1

Browse files
committed
close LuaLS#292 rename doc
1 parent 63910e6 commit e794ff1

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* `NEW` code action: swap params
77
* `CHG` unbind the relative path between binaries and scripts
88
* `CHG` `LuaDoc` also catchs `--` (no need `---`)
9+
* `CHG` rename doc
910

1011
## 1.5.0
1112
`2020-12-5`

script/core/rename.lua

+54-1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ local function ofLocal(source, newname, callback)
230230
renameLocal(ref, newname, callback)
231231
end
232232
end
233+
if source.parent.type == 'funcargs'
234+
and source.bindDocs then
235+
for _, doc in ipairs(source.bindDocs) do
236+
if doc.type == 'doc.param'
237+
and doc.param[1] == source[1] then
238+
callback(doc.param, doc.param.start, doc.param.finish, newname)
239+
end
240+
end
241+
end
233242
end
234243

235244
local function ofFieldThen(key, src, newname, callback)
@@ -298,6 +307,35 @@ local function ofLabel(source, newname, callback)
298307
end
299308
end
300309

310+
local function ofDocTypeName(source, newname, callback)
311+
for _, doc in ipairs(vm.getDocTypes(source[1])) do
312+
if doc.type == 'doc.class.name'
313+
or doc.type == 'doc.type.name'
314+
or doc.type == 'doc.alias.name' then
315+
callback(doc, doc.start, doc.finish, newname)
316+
end
317+
end
318+
end
319+
320+
local function ofDocParamName(source, newname, callback)
321+
callback(source, source.start, source.finish, newname)
322+
local doc = guide.getDocState(source)
323+
if doc.bindSources then
324+
for _, src in ipairs(doc.bindSources) do
325+
if src.type == 'local'
326+
and src.parent.type == 'funcargs'
327+
and src[1] == source[1] then
328+
renameLocal(src, newname, callback)
329+
if src.ref then
330+
for _, ref in ipairs(src.ref) do
331+
renameLocal(ref, newname, callback)
332+
end
333+
end
334+
end
335+
end
336+
end
337+
end
338+
301339
local function rename(source, newname, callback)
302340
if source.type == 'label'
303341
or source.type == 'goto' then
@@ -314,6 +352,12 @@ local function rename(source, newname, callback)
314352
elseif source.type == 'setglobal'
315353
or source.type == 'getglobal' then
316354
return ofGlobal(source, newname, callback)
355+
elseif source.type == 'doc.class.name'
356+
or source.type == 'doc.type.name'
357+
or source.type == 'doc.alias.name' then
358+
return ofDocTypeName(source, newname, callback)
359+
elseif source.type == 'doc.param.name' then
360+
return ofDocParamName(source, newname, callback)
317361
elseif source.type == 'string'
318362
or source.type == 'number'
319363
or source.type == 'boolean' then
@@ -340,7 +384,11 @@ local function prepareRename(source)
340384
or source.type == 'method'
341385
or source.type == 'tablefield'
342386
or source.type == 'setglobal'
343-
or source.type == 'getglobal' then
387+
or source.type == 'getglobal'
388+
or source.type == 'doc.class.name'
389+
or source.type == 'doc.type.name'
390+
or source.type == 'doc.alias.name'
391+
or source.type == 'doc.param.name' then
344392
return source, source[1]
345393
elseif source.type == 'string'
346394
or source.type == 'number'
@@ -373,6 +421,11 @@ local accept = {
373421
['string'] = true,
374422
['boolean'] = true,
375423
['number'] = true,
424+
425+
['doc.class.name'] = true,
426+
['doc.type.name'] = true,
427+
['doc.alias.name'] = true,
428+
['doc.param.name'] = true,
376429
}
377430

378431
local m = {}

test/rename/init.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,14 @@ function a:b() end
164164
a:b()
165165
]]
166166

167-
-- TODO
168-
do return end
169167
TEST ('class1', 'class2') [[
170-
---@class1
168+
---@class class1
171169
172170
---@type class1
173171
174172
---@param x class1
175173
]] [[
176-
---@class2
174+
---@class class2
177175
178176
---@type class2
179177
@@ -197,9 +195,11 @@ TEST ('alias1', 'alias2') [[
197195
TEST ('arg1', 'arg2') [[
198196
---@param arg1 number
199197
function f(arg1)
198+
print(arg1)
200199
end
201200
]] [[
202201
---@param arg2 number
203202
function f(arg2)
203+
print(arg2)
204204
end
205205
]]

0 commit comments

Comments
 (0)