Skip to content

Commit bf225c6

Browse files
authored
Merge pull request #1460 from CppCXY/master
支持typeformat
2 parents fd394ee + b287db9 commit bf225c6

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

script/core/type-formatting.lua

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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)
@@ -86,7 +87,29 @@ local function checkSplitOneLine(results, uri, position, ch)
8687
end
8788
end
8889

89-
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)
90113
local ast = files.getState(uri)
91114
if not ast then
92115
return nil
@@ -95,6 +118,9 @@ return function (uri, position, ch)
95118
local results = {}
96119
-- split `function () $ end`
97120
checkSplitOneLine(results, uri, position, ch)
121+
if #results == 0 then
122+
typeFormat(results, uri, position, ch, options)
123+
end
98124

99125
return results
100126
end

script/provider/provider.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ m.register 'textDocument/onTypeFormatting' {
11791179
end
11801180
local core = require 'core.type-formatting'
11811181
local pos = converter.unpackPosition(uri, params.position)
1182-
local edits = core(uri, pos, ch)
1182+
local edits = core(uri, pos, ch, params.options)
11831183
if not edits or #edits == 0 then
11841184
return nil
11851185
end

test/type_formatting/init.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local catch = require 'catch'
66
rawset(_G, 'TEST', true)
77

88
function TEST(script)
9-
return function (expect)
9+
return function(expect)
1010
local newScript, catched = catch(script, '?')
1111
files.setText('', newScript)
1212
local edits = core('', catched['?'][1][1], expect.ch)
@@ -123,5 +123,11 @@ end
123123
]]
124124
{
125125
ch = '\n',
126-
edits = {}
126+
edits = {
127+
{
128+
start = 0,
129+
finish = 10000,
130+
text = 'if true then\n',
131+
}
132+
}
127133
}

0 commit comments

Comments
 (0)