summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
Diffstat (limited to 'script')
-rw-r--r--script/client.lua32
-rw-r--r--script/core/modifyRequirePath.lua79
-rw-r--r--script/parser/guide.lua3
-rw-r--r--script/provider/capability.lua40
-rw-r--r--script/provider/provider.lua74
5 files changed, 145 insertions, 83 deletions
diff --git a/script/client.lua b/script/client.lua
index 8ab3ac0e..14320edf 100644
--- a/script/client.lua
+++ b/script/client.lua
@@ -391,13 +391,41 @@ function m.editText(uri, edits)
for i, edit in ipairs(edits) do
textEdits[i] = converter.textEdit(converter.packRange(state, edit.start, edit.finish), edit.text)
end
- proto.request('workspace/applyEdit', {
+ local params = {
edit = {
changes = {
[uri] = textEdits,
}
}
- })
+ }
+ proto.request('workspace/applyEdit', params)
+ log.info('workspace/applyEdit', inspect(params))
+end
+
+---@alias textMultiEditor {uri: uri, start: integer, finish: integer, text: string}
+
+---@param editors textMultiEditor[]
+function m.editMultiText(editors)
+ local files = require 'files'
+ local changes = {}
+ for _, editor in ipairs(editors) do
+ local uri = editor.uri
+ local state = files.getState(uri)
+ if state then
+ if not changes[uri] then
+ changes[uri] = {}
+ end
+ local edit = converter.textEdit(converter.packRange(state, editor.start, editor.finish), editor.text)
+ table.insert(changes[uri], edit)
+ end
+ end
+ local params = {
+ edit = {
+ changes = changes,
+ }
+ }
+ proto.request('workspace/applyEdit', params)
+ log.info('workspace/applyEdit', inspect(params))
end
---@param callback async fun(ev: string)
diff --git a/script/core/modifyRequirePath.lua b/script/core/modifyRequirePath.lua
new file mode 100644
index 00000000..91c600ac
--- /dev/null
+++ b/script/core/modifyRequirePath.lua
@@ -0,0 +1,79 @@
+local files = require 'files'
+local await = require 'await'
+local guide = require 'parser.guide'
+local rpath = require 'workspace.require-path'
+local furi = require 'file-uri'
+local util = require 'utility'
+local client = require 'client'
+local lang = require 'language'
+
+---@alias rename { oldUri: uri, newUri: uri }
+
+---@param changes table[]
+---@param uri uri
+---@param renames rename[]
+local function checkConvert(changes, uri, renames)
+ local state = files.getState(uri)
+ if not state then
+ return
+ end
+
+ guide.eachSpecialOf(state.ast, 'require', function (source)
+ local call = source.parent
+ if call.type ~= 'call' then
+ return
+ end
+ local nameObj = call.args and call.args[1]
+ if not nameObj then
+ return
+ end
+ local name = nameObj.type == 'string' and nameObj[1]
+ if type(name) ~= 'string' then
+ return
+ end
+ local uris = rpath.findUrisByRequireName(uri, name)
+ local ruri = uris and uris[1]
+ if not ruri then
+ return
+ end
+ for _, rename in ipairs(renames) do
+ if rename.oldUri == ruri then
+ local visibles = rpath.getVisiblePath(uri, furi.decode(rename.newUri))
+ if #visibles > 0 then
+ local newName = visibles[1].name
+ changes[#changes+1] = {
+ uri = uri,
+ start = nameObj.start,
+ finish = nameObj.finish,
+ text = util.viewString(newName, nameObj[2]),
+ }
+ return
+ end
+ end
+ end
+ end)
+end
+
+---@async
+---@param renames rename[]
+return function (renames)
+ if #renames == 0 then
+ return
+ end
+ local changes = {}
+ for uri in files.eachFile() do
+ checkConvert(changes, uri, renames)
+ await.delay()
+ end
+ if #changes == 0 then
+ return
+ end
+
+ local _, index = client.awaitRequestMessage('Info', lang.script.WINDOW_MODIFY_REQUIRE_PATH, {
+ lang.script.WINDOW_MODIFY_REQUIRE_OK
+ })
+
+ if index == 1 then
+ client.editMultiText(changes)
+ end
+end
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 5dee571c..206820ef 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -776,6 +776,9 @@ function m.eachChild(source, callback)
end
--- 获取指定的 special
+---@param ast parser.object
+---@param name string
+---@param callback fun(src: parser.object)
function m.eachSpecialOf(ast, name, callback)
local root = m.getRoot(ast)
local state = root.state
diff --git a/script/provider/capability.lua b/script/provider/capability.lua
index 0f00cec1..873fbfd3 100644
--- a/script/provider/capability.lua
+++ b/script/provider/capability.lua
@@ -10,44 +10,6 @@ require 'provider.inlay-hint'
local m = {}
-local function testFileEvents(initer)
- initer.fileOperations = {
- didCreate = {
- filters = {
- {
- pattern = {
- glob = '**',
- --matches = 'file',
- options = platform.OS == 'Windows',
- }
- }
- }
- },
- didDelete = {
- filters = {
- {
- pattern = {
- glob = '**',
- --matches = 'file',
- options = platform.OS == 'Windows',
- }
- }
- }
- },
- didRename = {
- filters = {
- {
- pattern = {
- glob = '**',
- --matches = 'file',
- options = platform.OS == 'Windows',
- }
- }
- }
- },
- }
-end
-
m.fillings = {}
local function mergeFillings(provider)
@@ -79,8 +41,6 @@ function m.getProvider()
},
}
- --testFileEvents()
-
nonil.enable()
if not client.info.capabilities.textDocument.completion.dynamicRegistration
or not client.info.capabilities.workspace.configuration then
diff --git a/script/provider/provider.lua b/script/provider/provider.lua
index ce762d1d..62395df2 100644
--- a/script/provider/provider.lua
+++ b/script/provider/provider.lua
@@ -180,59 +180,51 @@ m.register 'workspace/didChangeConfiguration' {
end
}
-m.register 'workspace/didCreateFiles' {
- ---@async
- function (params)
- log.debug('workspace/didCreateFiles', inspect(params))
- for _, file in ipairs(params.files) do
- if workspace.isValidLuaUri(file.uri) then
- files.setText(file.uri, util.loadFile(furi.decode(file.uri)), false)
- end
- end
- end
-}
-
-m.register 'workspace/didDeleteFiles' {
- function (params)
- log.debug('workspace/didDeleteFiles', inspect(params))
- for _, file in ipairs(params.files) do
- files.remove(file.uri)
- local childs = files.getChildFiles(file.uri)
- for _, uri in ipairs(childs) do
- log.debug('workspace/didDeleteFiles#child', uri)
- files.remove(uri)
- end
- end
- end
-}
-
m.register 'workspace/didRenameFiles' {
+ capability = {
+ workspace = {
+ fileOperations = {
+ didRename = {
+ filters = {
+ {
+ pattern = {
+ glob = '**',
+ },
+ },
+ },
+ },
+ },
+ },
+ },
---@async
function (params)
log.debug('workspace/didRenameFiles', inspect(params))
+ local renames = {}
for _, file in ipairs(params.files) do
- local text = files.getOriginText(file.oldUri)
- if text then
- files.remove(file.oldUri)
- if workspace.isValidLuaUri(file.newUri) then
- files.setText(file.newUri, text, false)
- end
+ local oldUri = furi.normalize(file.oldUri)
+ local newUri = furi.normalize(file.newUri)
+ if files.exists(oldUri)
+ and workspace.isValidLuaUri(newUri) then
+ renames[#renames+1] = {
+ oldUri = oldUri,
+ newUri = newUri,
+ }
end
- local childs = files.getChildFiles(file.oldUri)
+ local childs = files.getChildFiles(oldUri)
for _, uri in ipairs(childs) do
- local ctext = files.getOriginText(uri)
- if ctext then
+ if files.exists(uri) then
local ouri = uri
- local tail = ouri:sub(#file.oldUri)
+ local tail = ouri:sub(#oldUri)
local nuri = file.newUri .. tail
- log.debug('workspace/didRenameFiles#child', ouri, nuri)
- files.remove(uri)
- if workspace.isValidLuaUri(nuri) then
- files.setText(nuri, text, false)
- end
+ renames[#renames+1] = {
+ oldUri = ouri,
+ newUri = nuri,
+ }
end
end
end
+ local core = require 'core.modifyRequirePath'
+ core(renames)
end
}