diff options
author | CppCXY <812125110@qq.com> | 2022-01-20 10:26:36 +0800 |
---|---|---|
committer | CppCXY <812125110@qq.com> | 2022-01-20 10:26:36 +0800 |
commit | e1faccfd253fe14b4f8ccc5ee9d4052b782ec5df (patch) | |
tree | 888aa207b19a477fd8b88685258d71dae689b121 /script | |
parent | 5888272ccf7fc0b542edf88d4d8a38636351b42b (diff) | |
download | lua-language-server-e1faccfd253fe14b4f8ccc5ee9d4052b782ec5df.zip |
提供范围格式化
Diffstat (limited to 'script')
-rw-r--r-- | script/core/type-formatting.lua | 80 | ||||
-rw-r--r-- | script/provider/capability.lua | 3 |
2 files changed, 53 insertions, 30 deletions
diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua index b946184b..faee4c4a 100644 --- a/script/core/type-formatting.lua +++ b/script/core/type-formatting.lua @@ -1,6 +1,9 @@ local files = require 'files' local lookBackward = require 'core.look-backward' local guide = require "parser.guide" +local code_format = require "code_format" +local log = require("server.script.log") +local converter = require("server.script.proto.converter") local function insertIndentation(uri, position, edits) local text = files.getText(uri) @@ -46,38 +49,61 @@ 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 - end - local bPosition = findBackward(uri, position, 'then', 'do', ')', '{') - if not bPosition then + + -- local fPosition, fSymbol = findForward(uri, position, 'end', '}') + -- if not fPosition then + -- return + -- end + -- local bPosition = findBackward(uri, position, 'then', 'do', ')', '{') + -- if not bPosition then + -- return + -- end + -- local edits = {} + -- edits[#edits+1] = { + -- start = bPosition, + -- finish = position, + -- text = '\n\t', + -- } + -- edits[#edits+1] = { + -- start = position, + -- finish = fPosition + 1, + -- text = '', + -- } + -- edits[#edits+1] = { + -- start = fPosition + 1, + -- finish = fPosition + 1, + -- text = '\n' .. fSymbol:sub(1, 1) + -- } + -- insertIndentation(uri, bPosition, edits) + -- for _, edit in ipairs(edits) do + -- results[#results+1] = edit + -- end + + local text = files.getText(uri) + local status, formattedText, startLine, endLine = code_format.range_format( + uri, text, position.line - 1, position.line) + + if not status then + if formattedText ~= nil then + log.error(formattedText) + end + return end - local edits = {} - edits[#edits+1] = { - start = bPosition, - finish = position, - text = '\n\t', - } - edits[#edits+1] = { - start = position, - finish = fPosition + 1, - text = '', - } - edits[#edits+1] = { - start = fPosition + 1, - finish = fPosition + 1, - text = '\n' .. fSymbol:sub(1, 1) - } - insertIndentation(uri, bPosition, edits) - for _, edit in ipairs(edits) do - results[#results+1] = edit - end + + table.insert(results, { + { + start = converter.unpackPosition(uri, { line = startLine, character = 0 }), + finish = converter.unpackPosition(uri, { line = endLine + 1, character = 0 }), + text = formattedText, + } + }) + + 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/provider/capability.lua b/script/provider/capability.lua index 31baf690..e13e11e0 100644 --- a/script/provider/capability.lua +++ b/script/provider/capability.lua @@ -114,9 +114,6 @@ function m.getIniter() }, documentFormattingProvider = true, documentRangeFormattingProvider = true - --documentOnTypeFormattingProvider = { - -- firstTriggerCharacter = '}', - --}, } --testFileEvents() |