Skip to content

Commit ac7ce0d

Browse files
committed
modify require after renaming files
1 parent 1ec33e6 commit ac7ce0d

File tree

10 files changed

+162
-83
lines changed

10 files changed

+162
-83
lines changed

changelog.md

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

33
## 3.6.4
4+
* `NEW` modify `require` after renaming files
45
* `FIX` circulation reference in process analysis
56
```lua
67
---@type number

locale/en-us/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ WINDOW_CONFIG_LUA_DEPRECATED =
551551
'`config.lua` is deprecated, please use `config.json` instead.'
552552
WINDOW_CONVERT_CONFIG_LUA =
553553
'Convert to `config.json`'
554+
WINDOW_MODIFY_REQUIRE_PATH =
555+
'Do you want to modify the require path?'
556+
WINDOW_MODIFY_REQUIRE_OK =
557+
'Modify'
554558

555559
CONFIG_LOAD_FAILED =
556560
'Unable to read the settings file: {}'

locale/pt-br/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ WINDOW_CONFIG_LUA_DEPRECATED = -- TODO: need translate!
551551
'`config.lua` is deprecated, please use `config.json` instead.'
552552
WINDOW_CONVERT_CONFIG_LUA = -- TODO: need translate!
553553
'Convert to `config.json`'
554+
WINDOW_MODIFY_REQUIRE_PATH = -- TODO: need translate!
555+
'Do you want to modify the require path?'
556+
WINDOW_MODIFY_REQUIRE_OK = -- TODO: need translate!
557+
'Modify'
554558

555559
CONFIG_LOAD_FAILED =
556560
'Não é possível ler o arquivo de configurações: {}'

locale/zh-cn/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ WINDOW_CONFIG_LUA_DEPRECATED =
551551
'`config.lua` 已废弃,请改用 `config.json` 。'
552552
WINDOW_CONVERT_CONFIG_LUA =
553553
'转换为 `config.json`'
554+
WINDOW_MODIFY_REQUIRE_PATH =
555+
'你想要修改 `require` 的路径吗?'
556+
WINDOW_MODIFY_REQUIRE_OK =
557+
'修改'
554558

555559
CONFIG_LOAD_FAILED =
556560
'无法读取设置文件:{}'

locale/zh-tw/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ WINDOW_CONFIG_LUA_DEPRECATED = -- TODO: need translate!
551551
'`config.lua` is deprecated, please use `config.json` instead.'
552552
WINDOW_CONVERT_CONFIG_LUA = -- TODO: need translate!
553553
'Convert to `config.json`'
554+
WINDOW_MODIFY_REQUIRE_PATH = -- TODO: need translate!
555+
'Do you want to modify the require path?'
556+
WINDOW_MODIFY_REQUIRE_OK = -- TODO: need translate!
557+
'Modify'
554558

555559
CONFIG_LOAD_FAILED =
556560
'無法讀取設定檔案:{}'

script/client.lua

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,41 @@ function m.editText(uri, edits)
391391
for i, edit in ipairs(edits) do
392392
textEdits[i] = converter.textEdit(converter.packRange(state, edit.start, edit.finish), edit.text)
393393
end
394-
proto.request('workspace/applyEdit', {
394+
local params = {
395395
edit = {
396396
changes = {
397397
[uri] = textEdits,
398398
}
399399
}
400-
})
400+
}
401+
proto.request('workspace/applyEdit', params)
402+
log.info('workspace/applyEdit', inspect(params))
403+
end
404+
405+
---@alias textMultiEditor {uri: uri, start: integer, finish: integer, text: string}
406+
407+
---@param editors textMultiEditor[]
408+
function m.editMultiText(editors)
409+
local files = require 'files'
410+
local changes = {}
411+
for _, editor in ipairs(editors) do
412+
local uri = editor.uri
413+
local state = files.getState(uri)
414+
if state then
415+
if not changes[uri] then
416+
changes[uri] = {}
417+
end
418+
local edit = converter.textEdit(converter.packRange(state, editor.start, editor.finish), editor.text)
419+
table.insert(changes[uri], edit)
420+
end
421+
end
422+
local params = {
423+
edit = {
424+
changes = changes,
425+
}
426+
}
427+
proto.request('workspace/applyEdit', params)
428+
log.info('workspace/applyEdit', inspect(params))
401429
end
402430

403431
---@param callback async fun(ev: string)

script/core/modifyRequirePath.lua

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
local files = require 'files'
2+
local await = require 'await'
3+
local guide = require 'parser.guide'
4+
local rpath = require 'workspace.require-path'
5+
local furi = require 'file-uri'
6+
local util = require 'utility'
7+
local client = require 'client'
8+
local lang = require 'language'
9+
10+
---@alias rename { oldUri: uri, newUri: uri }
11+
12+
---@param changes table[]
13+
---@param uri uri
14+
---@param renames rename[]
15+
local function checkConvert(changes, uri, renames)
16+
local state = files.getState(uri)
17+
if not state then
18+
return
19+
end
20+
21+
guide.eachSpecialOf(state.ast, 'require', function (source)
22+
local call = source.parent
23+
if call.type ~= 'call' then
24+
return
25+
end
26+
local nameObj = call.args and call.args[1]
27+
if not nameObj then
28+
return
29+
end
30+
local name = nameObj.type == 'string' and nameObj[1]
31+
if type(name) ~= 'string' then
32+
return
33+
end
34+
local uris = rpath.findUrisByRequireName(uri, name)
35+
local ruri = uris and uris[1]
36+
if not ruri then
37+
return
38+
end
39+
for _, rename in ipairs(renames) do
40+
if rename.oldUri == ruri then
41+
local visibles = rpath.getVisiblePath(uri, furi.decode(rename.newUri))
42+
if #visibles > 0 then
43+
local newName = visibles[1].name
44+
changes[#changes+1] = {
45+
uri = uri,
46+
start = nameObj.start,
47+
finish = nameObj.finish,
48+
text = util.viewString(newName, nameObj[2]),
49+
}
50+
return
51+
end
52+
end
53+
end
54+
end)
55+
end
56+
57+
---@async
58+
---@param renames rename[]
59+
return function (renames)
60+
if #renames == 0 then
61+
return
62+
end
63+
local changes = {}
64+
for uri in files.eachFile() do
65+
checkConvert(changes, uri, renames)
66+
await.delay()
67+
end
68+
if #changes == 0 then
69+
return
70+
end
71+
72+
local _, index = client.awaitRequestMessage('Info', lang.script.WINDOW_MODIFY_REQUIRE_PATH, {
73+
lang.script.WINDOW_MODIFY_REQUIRE_OK
74+
})
75+
76+
if index == 1 then
77+
client.editMultiText(changes)
78+
end
79+
end

script/parser/guide.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@ function m.eachChild(source, callback)
776776
end
777777

778778
--- 获取指定的 special
779+
---@param ast parser.object
780+
---@param name string
781+
---@param callback fun(src: parser.object)
779782
function m.eachSpecialOf(ast, name, callback)
780783
local root = m.getRoot(ast)
781784
local state = root.state

script/provider/capability.lua

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,6 @@ require 'provider.inlay-hint'
1010

1111
local m = {}
1212

13-
local function testFileEvents(initer)
14-
initer.fileOperations = {
15-
didCreate = {
16-
filters = {
17-
{
18-
pattern = {
19-
glob = '**',
20-
--matches = 'file',
21-
options = platform.OS == 'Windows',
22-
}
23-
}
24-
}
25-
},
26-
didDelete = {
27-
filters = {
28-
{
29-
pattern = {
30-
glob = '**',
31-
--matches = 'file',
32-
options = platform.OS == 'Windows',
33-
}
34-
}
35-
}
36-
},
37-
didRename = {
38-
filters = {
39-
{
40-
pattern = {
41-
glob = '**',
42-
--matches = 'file',
43-
options = platform.OS == 'Windows',
44-
}
45-
}
46-
}
47-
},
48-
}
49-
end
50-
5113
m.fillings = {}
5214

5315
local function mergeFillings(provider)
@@ -79,8 +41,6 @@ function m.getProvider()
7941
},
8042
}
8143

82-
--testFileEvents()
83-
8444
nonil.enable()
8545
if not client.info.capabilities.textDocument.completion.dynamicRegistration
8646
or not client.info.capabilities.workspace.configuration then

script/provider/provider.lua

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -180,59 +180,51 @@ m.register 'workspace/didChangeConfiguration' {
180180
end
181181
}
182182

183-
m.register 'workspace/didCreateFiles' {
184-
---@async
185-
function (params)
186-
log.debug('workspace/didCreateFiles', inspect(params))
187-
for _, file in ipairs(params.files) do
188-
if workspace.isValidLuaUri(file.uri) then
189-
files.setText(file.uri, util.loadFile(furi.decode(file.uri)), false)
190-
end
191-
end
192-
end
193-
}
194-
195-
m.register 'workspace/didDeleteFiles' {
196-
function (params)
197-
log.debug('workspace/didDeleteFiles', inspect(params))
198-
for _, file in ipairs(params.files) do
199-
files.remove(file.uri)
200-
local childs = files.getChildFiles(file.uri)
201-
for _, uri in ipairs(childs) do
202-
log.debug('workspace/didDeleteFiles#child', uri)
203-
files.remove(uri)
204-
end
205-
end
206-
end
207-
}
208-
209183
m.register 'workspace/didRenameFiles' {
184+
capability = {
185+
workspace = {
186+
fileOperations = {
187+
didRename = {
188+
filters = {
189+
{
190+
pattern = {
191+
glob = '**',
192+
},
193+
},
194+
},
195+
},
196+
},
197+
},
198+
},
210199
---@async
211200
function (params)
212201
log.debug('workspace/didRenameFiles', inspect(params))
202+
local renames = {}
213203
for _, file in ipairs(params.files) do
214-
local text = files.getOriginText(file.oldUri)
215-
if text then
216-
files.remove(file.oldUri)
217-
if workspace.isValidLuaUri(file.newUri) then
218-
files.setText(file.newUri, text, false)
219-
end
204+
local oldUri = furi.normalize(file.oldUri)
205+
local newUri = furi.normalize(file.newUri)
206+
if files.exists(oldUri)
207+
and workspace.isValidLuaUri(newUri) then
208+
renames[#renames+1] = {
209+
oldUri = oldUri,
210+
newUri = newUri,
211+
}
220212
end
221-
local childs = files.getChildFiles(file.oldUri)
213+
local childs = files.getChildFiles(oldUri)
222214
for _, uri in ipairs(childs) do
223-
local ctext = files.getOriginText(uri)
224-
if ctext then
215+
if files.exists(uri) then
225216
local ouri = uri
226-
local tail = ouri:sub(#file.oldUri)
217+
local tail = ouri:sub(#oldUri)
227218
local nuri = file.newUri .. tail
228-
log.debug('workspace/didRenameFiles#child', ouri, nuri)
229-
files.remove(uri)
230-
if workspace.isValidLuaUri(nuri) then
231-
files.setText(nuri, text, false)
232-
end
219+
renames[#renames+1] = {
220+
oldUri = ouri,
221+
newUri = nuri,
222+
}
233223
end
234224
end
235225
end
226+
local core = require 'core.modifyRequirePath'
227+
core(renames)
236228
end
237229
}
238230

0 commit comments

Comments
 (0)