summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-02-09 15:38:40 +0800
committerGitHub <noreply@github.com>2022-02-09 15:38:40 +0800
commit0f63b39275be9c5c3eb3bca27f5bd660cdc0ae26 (patch)
tree53b3a597a20deab6e2ff81a727f65e6aaf9d94df /script
parent6c4fe2e8360b8bee1a6e01d5578e5cfc8191c1ee (diff)
parent96bdc534fc2c0353e0e21eda7a8deec617f2db33 (diff)
downloadlua-language-server-0f63b39275be9c5c3eb3bca27f5bd660cdc0ae26.zip
Merge pull request #915 from CppCXY/format
code reformat
Diffstat (limited to 'script')
-rw-r--r--script/core/diagnostics/codestyle-check.lua34
-rw-r--r--script/core/formatting.lua25
-rw-r--r--script/core/rangeformatting.lua26
-rw-r--r--script/core/type-formatting.lua3
-rw-r--r--script/proto/define.lua1
-rw-r--r--script/provider/capability.lua5
-rw-r--r--script/provider/provider.lua86
7 files changed, 176 insertions, 4 deletions
diff --git a/script/core/diagnostics/codestyle-check.lua b/script/core/diagnostics/codestyle-check.lua
new file mode 100644
index 00000000..16623eca
--- /dev/null
+++ b/script/core/diagnostics/codestyle-check.lua
@@ -0,0 +1,34 @@
+local files = require("files")
+local codeFormat = require "code_format"
+local converter = require("proto.converter")
+local log = require("log")
+local config = require("config")
+
+
+---@async
+return function(uri, callback)
+ local text = files.getText(uri)
+ if not text then
+ return
+ end
+
+ local status, diagnosticInfos = codeFormat.diagnose_file(uri, text)
+
+ if not status then
+ if diagnosticInfos ~= nil then
+ log.error(diagnosticInfos)
+ end
+
+ return
+ end
+
+ if diagnosticInfos then
+ for _, diagnosticInfo in pairs(diagnosticInfos) do
+ callback {
+ start = converter.unpackPosition(uri, diagnosticInfo.range.start),
+ finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]),
+ message = diagnosticInfo.message
+ }
+ end
+ end
+end
diff --git a/script/core/formatting.lua b/script/core/formatting.lua
new file mode 100644
index 00000000..6c57b8c2
--- /dev/null
+++ b/script/core/formatting.lua
@@ -0,0 +1,25 @@
+local codeFormat = 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 = codeFormat.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/core/rangeformatting.lua b/script/core/rangeformatting.lua
new file mode 100644
index 00000000..de9516c1
--- /dev/null
+++ b/script/core/rangeformatting.lua
@@ -0,0 +1,26 @@
+local codeFormat = require("code_format")
+local files = require("files")
+local log = require("log")
+local converter = require("proto.converter")
+
+return function(uri, range)
+ local text = files.getText(uri)
+ local status, formattedText, startLine, endLine = codeFormat.range_format(
+ uri, text, range.start.line, range["end"].line)
+
+ if not status then
+ if formattedText ~= nil then
+ log.error(formattedText)
+ end
+
+ return
+ end
+
+ return {
+ {
+ start = converter.unpackPosition(uri, { line = startLine, character = 0 }),
+ finish = converter.unpackPosition(uri, { line = endLine + 1, character = 0 }),
+ text = formattedText,
+ }
+ }
+end
diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua
index b946184b..cc305982 100644
--- a/script/core/type-formatting.lua
+++ b/script/core/type-formatting.lua
@@ -46,6 +46,7 @@ local function checkSplitOneLine(results, uri, position, ch)
if ch ~= '\n' then
return
end
+
local fPosition, fSymbol = findForward(uri, position, 'end', '}')
if not fPosition then
return
@@ -77,7 +78,7 @@ local function checkSplitOneLine(results, uri, position, ch)
end
return function (uri, position, ch)
- local ast = files.getState(uri)
+ local ast = files.getState(uri)
if not ast then
return nil
end
diff --git a/script/proto/define.lua b/script/proto/define.lua
index dbb6ba85..b4a0da0c 100644
--- a/script/proto/define.lua
+++ b/script/proto/define.lua
@@ -58,6 +58,7 @@ m.DiagnosticDefaultSeverity = {
['doc-field-no-class'] = 'Warning',
['duplicate-doc-field'] = 'Warning',
['unknown-diag-code'] = 'Waiting',
+ ['codestyle-check'] = "Warning",
}
---@alias DiagnosticDefaultNeededFileStatus
diff --git a/script/provider/capability.lua b/script/provider/capability.lua
index 8a1424ca..e13e11e0 100644
--- a/script/provider/capability.lua
+++ b/script/provider/capability.lua
@@ -112,9 +112,8 @@ function m.getIniter()
range = true,
full = false,
},
- --documentOnTypeFormattingProvider = {
- -- firstTriggerCharacter = '}',
- --},
+ documentFormattingProvider = true,
+ documentRangeFormattingProvider = true
}
--testFileEvents()
diff --git a/script/provider/provider.lua b/script/provider/provider.lua
index bd0dd826..778c8b1f 100644
--- a/script/provider/provider.lua
+++ b/script/provider/provider.lua
@@ -102,6 +102,19 @@ m.register 'initialize' {
workspace.create(params.rootUri)
end
+ if params.initializationOptions then
+ if params.initializationOptions.editorConfigFiles then
+ local codeFormat = require "code_format"
+ for _, config in pairs(params.initializationOptions.editorConfigFiles) do
+ local status, err = codeFormat.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 = {
@@ -916,6 +929,79 @@ 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 'textDocument/rangeFormatting' {
+ 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.rangeformatting'
+ local edits = core(uri, params.range)
+ 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 codeFormat = require "code_format"
+ local status, err = codeFormat.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