Skip to content

Commit 40f191a

Browse files
committed
支持typeformat
1 parent e62d964 commit 40f191a

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

3rd/EmmyLuaCodeStyle

Submodule EmmyLuaCodeStyle updated 217 files

script/core/type-formatting.lua

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
local files = require 'files'
22
local lookBackward = require 'core.look-backward'
33
local guide = require "parser.guide"
4+
local codeFormat = require "code_format"
45

56
local function insertIndentation(uri, position, edits)
67
local text = files.getText(uri)
78
local state = files.getState(uri)
89
local row = guide.rowColOf(position)
10+
if not state or not text then
11+
return
12+
end
913
local offset = state.lines[row]
1014
local indent = text:match('^%s*', offset)
1115
for _, edit in ipairs(edits) do
@@ -16,6 +20,9 @@ end
1620
local function findForward(uri, position, ...)
1721
local text = files.getText(uri)
1822
local state = files.getState(uri)
23+
if not state or not text then
24+
return nil
25+
end
1926
local offset = guide.positionToOffset(state, position)
2027
local firstOffset = text:match('^[ \t]*()', offset + 1)
2128
if not firstOffset then
@@ -32,6 +39,9 @@ end
3239
local function findBackward(uri, position, ...)
3340
local text = files.getText(uri)
3441
local state = files.getState(uri)
42+
if not state or not text then
43+
return nil
44+
end
3545
local offset = guide.positionToOffset(state, position)
3646
local lastOffset = lookBackward.findAnyOffset(text, offset)
3747
for _, symbol in ipairs { ... } do
@@ -48,7 +58,7 @@ local function checkSplitOneLine(results, uri, position, ch)
4858
end
4959

5060
local fPosition, fSymbol = findForward(uri, position, 'end', '}')
51-
if not fPosition then
61+
if not fPosition or not fSymbol then
5262
return
5363
end
5464
local bPosition = findBackward(uri, position, 'then', 'do', ')', '{')
@@ -77,7 +87,29 @@ local function checkSplitOneLine(results, uri, position, ch)
7787
end
7888
end
7989

80-
return function (uri, position, ch)
90+
local function typeFormat(results, uri, position, ch, options)
91+
if ch ~= '\n' then
92+
return
93+
end
94+
local text = files.getOriginText(uri)
95+
local state = files.getState(uri)
96+
if not state then
97+
return
98+
end
99+
local converter = require("proto.converter")
100+
local pos = converter.packPosition(uri, position)
101+
local success, result = codeFormat.type_format(uri, text, pos.line, pos.character, options)
102+
if success then
103+
local range = result.range
104+
results[#results+1] = {
105+
text = result.newText,
106+
start = converter.unpackPosition(uri, { line = range.start.line, character = range.start.character }),
107+
finish = converter.unpackPosition(uri, { line = range["end"].line, character = range["end"].character }),
108+
}
109+
end
110+
end
111+
112+
return function (uri, position, ch, options)
81113
local ast = files.getState(uri)
82114
if not ast then
83115
return nil
@@ -86,6 +118,9 @@ return function (uri, position, ch)
86118
local results = {}
87119
-- split `function () $ end`
88120
checkSplitOneLine(results, uri, position, ch)
121+
if #results == 0 then
122+
typeFormat(results, uri, position, ch, options)
123+
end
89124

90125
return results
91126
end

script/provider/provider.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ m.register 'textDocument/onTypeFormatting' {
11071107
end
11081108
local core = require 'core.type-formatting'
11091109
local pos = converter.unpackPosition(uri, params.position)
1110-
local edits = core(uri, pos, ch)
1110+
local edits = core(uri, pos, ch, params.options)
11111111
if not edits or #edits == 0 then
11121112
return nil
11131113
end

0 commit comments

Comments
 (0)