diff options
author | CppCXY <812125110@qq.com> | 2022-01-20 01:48:41 +0800 |
---|---|---|
committer | CppCXY <812125110@qq.com> | 2022-01-20 01:48:41 +0800 |
commit | 5888272ccf7fc0b542edf88d4d8a38636351b42b (patch) | |
tree | 70458098581c4fe9964ef154f9922d1aa3aff536 | |
parent | 8e41ce1b39044742a6aaadfd16976654666d67ca (diff) | |
download | lua-language-server-5888272ccf7fc0b542edf88d4d8a38636351b42b.zip |
提供范围格式化
m--------- | 3rd/EmmyLuaCodeStyle | 0 | ||||
-rw-r--r-- | script/core/rangeformatting.lua | 26 | ||||
-rw-r--r-- | script/provider/capability.lua | 3 | ||||
-rw-r--r-- | script/provider/provider.lua | 30 |
4 files changed, 58 insertions, 1 deletions
diff --git a/3rd/EmmyLuaCodeStyle b/3rd/EmmyLuaCodeStyle -Subproject 2eb0e2dbf860f6bd10e51da6bf96656805d887a +Subproject 0d062958639e679b5299583a6a09470982b7f6f diff --git a/script/core/rangeformatting.lua b/script/core/rangeformatting.lua new file mode 100644 index 00000000..b66f0fbd --- /dev/null +++ b/script/core/rangeformatting.lua @@ -0,0 +1,26 @@ +local code_format = 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 = code_format.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/provider/capability.lua b/script/provider/capability.lua index 27950d7f..31baf690 100644 --- a/script/provider/capability.lua +++ b/script/provider/capability.lua @@ -112,7 +112,8 @@ function m.getIniter() range = true, full = false, }, - documentFormattingProvider = true + documentFormattingProvider = true, + documentRangeFormattingProvider = true --documentOnTypeFormattingProvider = { -- firstTriggerCharacter = '}', --}, diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 63450f59..a5e0a1ca 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -954,6 +954,36 @@ m.register 'textDocument/formatting' { 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 |