diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-08-12 23:25:35 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-12 23:25:35 +0800 |
commit | bf225c646f1b8b125922928fe6aafe4881f2d576 (patch) | |
tree | bb8bf35dd729e3cd1b1c345ad84cd0fd28b1f3d9 /script | |
parent | fd394ee8ba4e3c583d3dbccfac58e7636563e318 (diff) | |
parent | b287db94a779a435438a407ea17c8418b7b35152 (diff) | |
download | lua-language-server-bf225c646f1b8b125922928fe6aafe4881f2d576.zip |
Merge pull request #1460 from CppCXY/master
支持typeformat
Diffstat (limited to 'script')
-rw-r--r-- | script/core/type-formatting.lua | 28 | ||||
-rw-r--r-- | script/provider/provider.lua | 2 |
2 files changed, 28 insertions, 2 deletions
diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua index 0c326b8b..c6316d80 100644 --- a/script/core/type-formatting.lua +++ b/script/core/type-formatting.lua @@ -1,6 +1,7 @@ local files = require 'files' local lookBackward = require 'core.look-backward' local guide = require "parser.guide" +local codeFormat = require "code_format" local function insertIndentation(uri, position, edits) local text = files.getText(uri) @@ -86,7 +87,29 @@ local function checkSplitOneLine(results, uri, position, ch) end end -return function (uri, position, ch) +local function typeFormat(results, uri, position, ch, options) + if ch ~= '\n' then + return + end + local text = files.getOriginText(uri) + local state = files.getState(uri) + if not state then + return + end + local converter = require("proto.converter") + local pos = converter.packPosition(uri, position) + local success, result = codeFormat.type_format(uri, text, pos.line, pos.character, options) + if success then + local range = result.range + results[#results+1] = { + text = result.newText, + start = converter.unpackPosition(uri, { line = range.start.line, character = range.start.character }), + finish = converter.unpackPosition(uri, { line = range["end"].line, character = range["end"].character }), + } + end +end + +return function (uri, position, ch, options) local ast = files.getState(uri) if not ast then return nil @@ -95,6 +118,9 @@ return function (uri, position, ch) local results = {} -- split `function () $ end` checkSplitOneLine(results, uri, position, ch) + if #results == 0 then + typeFormat(results, uri, position, ch, options) + end return results end diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 239a39d7..018db0c3 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -1179,7 +1179,7 @@ m.register 'textDocument/onTypeFormatting' { end local core = require 'core.type-formatting' local pos = converter.unpackPosition(uri, params.position) - local edits = core(uri, pos, ch) + local edits = core(uri, pos, ch, params.options) if not edits or #edits == 0 then return nil end |