From 5888272ccf7fc0b542edf88d4d8a38636351b42b Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Thu, 20 Jan 2022 01:48:41 +0800 Subject: =?UTF-8?q?=E6=8F=90=E4=BE=9B=E8=8C=83=E5=9B=B4=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rd/EmmyLuaCodeStyle | 2 +- script/core/rangeformatting.lua | 26 ++++++++++++++++++++++++++ script/provider/capability.lua | 3 ++- script/provider/provider.lua | 30 ++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 script/core/rangeformatting.lua diff --git a/3rd/EmmyLuaCodeStyle b/3rd/EmmyLuaCodeStyle index 2eb0e2db..0d062958 160000 --- a/3rd/EmmyLuaCodeStyle +++ b/3rd/EmmyLuaCodeStyle @@ -1 +1 @@ -Subproject commit 2eb0e2dbf860f6bd10e51da6bf96656805d887a2 +Subproject commit 0d062958639e679b5299583a6a09470982b7f6f9 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 _ = 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 -- cgit v1.2.3