summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
authorCppCXY <812125110@qq.com>2022-01-19 16:10:51 +0800
committerCppCXY <812125110@qq.com>2022-01-19 16:10:51 +0800
commit53b4c03fb2e7aa4969b5a1fd712686bad9548131 (patch)
tree9025b760ab47191bcffee8abfe20aaf2c1d5af60 /script
parent2f984c1a2baf162f72240ca7b7f3f530459a71f4 (diff)
downloadlua-language-server-53b4c03fb2e7aa4969b5a1fd712686bad9548131.zip
提交代码格式化
Diffstat (limited to 'script')
-rw-r--r--script/core/formatting.lua25
-rw-r--r--script/provider/capability.lua1
-rw-r--r--script/provider/provider.lua56
3 files changed, 82 insertions, 0 deletions
diff --git a/script/core/formatting.lua b/script/core/formatting.lua
new file mode 100644
index 00000000..8f72028b
--- /dev/null
+++ b/script/core/formatting.lua
@@ -0,0 +1,25 @@
+local code_format = require "code_format"
+local files = require("files")
+local log = require("log")
+
+return function(uri)
+ local text = files.getText(uri)
+ local ast = files.getState(uri)
+ local status, formattedText = code_format.format(uri, text)
+
+ if not status then
+ if formattedText ~= nil then
+ log.error(formattedText)
+ end
+
+ return
+ end
+
+ return {
+ {
+ start = ast.ast.start,
+ finish = ast.ast.finish,
+ text = formattedText,
+ }
+ }
+end
diff --git a/script/provider/capability.lua b/script/provider/capability.lua
index 8a1424ca..27950d7f 100644
--- a/script/provider/capability.lua
+++ b/script/provider/capability.lua
@@ -112,6 +112,7 @@ function m.getIniter()
range = true,
full = false,
},
+ documentFormattingProvider = true
--documentOnTypeFormattingProvider = {
-- firstTriggerCharacter = '}',
--},
diff --git a/script/provider/provider.lua b/script/provider/provider.lua
index 4b22cb47..63450f59 100644
--- a/script/provider/provider.lua
+++ b/script/provider/provider.lua
@@ -101,6 +101,19 @@ m.register 'initialize' {
workspace.create(params.rootUri)
end
+ if params.initializationOptions then
+ if params.initializationOptions.editorConfigFiles then
+ local code_format = require "code_format"
+ for _, config in pairs(params.initializationOptions.editorConfigFiles) do
+ local status, err = code_format.update_config(1, config.workspace, config.path)
+
+ if not status and err ~= nil then
+ log.error(err)
+ end
+ end
+ end
+ end
+
return {
capabilities = cap.getIniter(),
serverInfo = {
@@ -911,6 +924,49 @@ m.register '$/status/click' {
end
}
+m.register 'textDocument/formatting' {
+ abortByFileUpdate = true,
+ ---@async
+ function(params)
+ local uri = files.getRealUri(params.textDocument.uri)
+ workspace.awaitReady(uri)
+ local _<close> = progress.create(workspace.getScope(uri), lang.script.WINDOW_PROCESSING_TYPE_FORMATTING, 0.5)
+
+ if not files.exists(uri) then
+ return nil
+ end
+
+ local core = require 'core.formatting'
+ local edits = core(uri)
+ if not edits or #edits == 0 then
+ return nil
+ end
+
+ local results = {}
+ for i, edit in ipairs(edits) do
+ results[i] = {
+ range = converter.packRange(uri, edit.start, edit.finish),
+ newText = edit.text,
+ }
+ end
+
+ return results
+ end
+}
+
+m.register 'config/editorconfig/update' {
+ abortByFileUpdate = true,
+ ---@async
+ function(params)
+ local code_format = require "code_format"
+ local status, err = code_format.update_config(params.type, params.source.workspace, params.source.path)
+
+ if not status and err ~= nil then
+ log.error(err)
+ end
+ end
+}
+
m.register 'textDocument/onTypeFormatting' {
abortByFileUpdate = true,
---@async