diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-02-09 15:38:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-09 15:38:40 +0800 |
commit | 0f63b39275be9c5c3eb3bca27f5bd660cdc0ae26 (patch) | |
tree | 53b3a597a20deab6e2ff81a727f65e6aaf9d94df /script/core | |
parent | 6c4fe2e8360b8bee1a6e01d5578e5cfc8191c1ee (diff) | |
parent | 96bdc534fc2c0353e0e21eda7a8deec617f2db33 (diff) | |
download | lua-language-server-0f63b39275be9c5c3eb3bca27f5bd660cdc0ae26.zip |
Merge pull request #915 from CppCXY/format
code reformat
Diffstat (limited to 'script/core')
-rw-r--r-- | script/core/diagnostics/codestyle-check.lua | 34 | ||||
-rw-r--r-- | script/core/formatting.lua | 25 | ||||
-rw-r--r-- | script/core/rangeformatting.lua | 26 | ||||
-rw-r--r-- | script/core/type-formatting.lua | 3 |
4 files changed, 87 insertions, 1 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 |