From 40f191a85ea21bb64c427f9dab4bc597e2a0ea1b Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Thu, 11 Aug 2022 19:36:29 +0800 Subject: =?UTF-8?q?=E6=94=AF=E6=8C=81typeformat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rd/EmmyLuaCodeStyle | 2 +- script/core/type-formatting.lua | 39 +++++++++++++++++++++++++++++++++++++-- script/provider/provider.lua | 2 +- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/3rd/EmmyLuaCodeStyle b/3rd/EmmyLuaCodeStyle index a92e5ca8..e89f5c46 160000 --- a/3rd/EmmyLuaCodeStyle +++ b/3rd/EmmyLuaCodeStyle @@ -1 +1 @@ -Subproject commit a92e5ca884c7bb4c31eb1ee1577201676c9c732d +Subproject commit e89f5c4608c411d602a0dcdf24159809e715859b diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua index cc305982..c6316d80 100644 --- a/script/core/type-formatting.lua +++ b/script/core/type-formatting.lua @@ -1,11 +1,15 @@ 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) local state = files.getState(uri) local row = guide.rowColOf(position) + if not state or not text then + return + end local offset = state.lines[row] local indent = text:match('^%s*', offset) for _, edit in ipairs(edits) do @@ -16,6 +20,9 @@ end local function findForward(uri, position, ...) local text = files.getText(uri) local state = files.getState(uri) + if not state or not text then + return nil + end local offset = guide.positionToOffset(state, position) local firstOffset = text:match('^[ \t]*()', offset + 1) if not firstOffset then @@ -32,6 +39,9 @@ end local function findBackward(uri, position, ...) local text = files.getText(uri) local state = files.getState(uri) + if not state or not text then + return nil + end local offset = guide.positionToOffset(state, position) local lastOffset = lookBackward.findAnyOffset(text, offset) for _, symbol in ipairs { ... } do @@ -48,7 +58,7 @@ local function checkSplitOneLine(results, uri, position, ch) end local fPosition, fSymbol = findForward(uri, position, 'end', '}') - if not fPosition then + if not fPosition or not fSymbol then return end local bPosition = findBackward(uri, position, 'then', 'do', ')', '{') @@ -77,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 @@ -86,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 08b6ca93..3d012757 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -1107,7 +1107,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 -- cgit v1.2.3