Skip to content

Commit 8da719f

Browse files
committed
fix wront indent of VSCode
1 parent c700a5f commit 8da719f

File tree

5 files changed

+210
-14
lines changed

5 files changed

+210
-14
lines changed

script/core/diagnostics/codestyle-check.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local files = require 'files'
2-
local codeFormat = require 'code_format'
32
local converter = require 'proto.converter'
43
local log = require 'log'
54
local pformatting = require 'provider.formatting'
@@ -12,6 +11,11 @@ return function(uri, callback)
1211
return
1312
end
1413

14+
local suc, codeFormat = pcall(require, 'code_format')
15+
if not suc then
16+
return
17+
end
18+
1519
pformatting.updateConfig(uri)
1620

1721
local status, diagnosticInfos = codeFormat.diagnose_file(uri, text)

script/core/formatting.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
local codeFormat = require("code_format")
21
local files = require("files")
32
local log = require("log")
43

54
return function(uri, options)
5+
local suc, codeFormat = pcall(require, "code_format")
6+
if not suc then
7+
return
8+
end
69
local text = files.getOriginText(uri)
710
local state = files.getState(uri)
811
if not state then

script/core/rangeformatting.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
local codeFormat = require("code_format")
21
local files = require("files")
32
local log = require("log")
43
local converter = require("proto.converter")
54

65
return function(uri, range, options)
6+
local suc, codeFormat = pcall(require, "code_format")
7+
if not suc then
8+
return
9+
end
710
local text = files.getOriginText(uri)
811
local status, formattedText, startLine, endLine = codeFormat.range_format(
912
uri, text, range.start.line, range["end"].line, options)

script/core/type-formatting.lua

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
local files = require 'files'
22
local lookBackward = require 'core.look-backward'
3-
local guide = require "parser.guide"
4-
local codeFormat = require "code_format"
5-
local config = require "config"
3+
local guide = require 'parser.guide'
4+
local config = require 'config'
5+
local util = require 'utility'
6+
67

78
local function insertIndentation(uri, position, edits)
89
local text = files.getText(uri)
@@ -88,10 +89,105 @@ local function checkSplitOneLine(results, uri, position, ch)
8889
end
8990
end
9091

92+
local function getIndent(state, row)
93+
local offset = state.lines[row]
94+
local indent = state.lua:match('^[\t ]*', offset)
95+
return indent
96+
end
97+
98+
local function isInBlock(state, position)
99+
local block = guide.eachSourceContain(state.ast, position, function(source)
100+
if source.type == 'ifblock'
101+
or source.type == 'elseifblock' then
102+
if source.keyword[4] and source.keyword[4] <= position then
103+
return true
104+
end
105+
end
106+
if source.type == 'else' then
107+
if source.keyword[2] and source.keyword[2] <= position then
108+
return true
109+
end
110+
end
111+
if source.type == 'while' then
112+
if source.keyword[4] and source.keyword[4] <= position then
113+
return true
114+
end
115+
end
116+
if source.type == 'repeat' then
117+
if source.keyword[2] and source.keyword[2] <= position then
118+
return true
119+
end
120+
end
121+
if source.type == 'loop' then
122+
if source.keyword[4] and source.keyword[4] <= position then
123+
return true
124+
end
125+
end
126+
if source.type == 'in' then
127+
if source.keyword[6] and source.keyword[6] <= position then
128+
return true
129+
end
130+
end
131+
if source.type == 'do' then
132+
if source.keyword[2] and source.keyword[2] <= position then
133+
return true
134+
end
135+
end
136+
if source.type == 'function' then
137+
if source.args and source.args.finish <= position then
138+
return true
139+
end
140+
if not source.keyword[3] or source.keyword[3] >= position then
141+
return true
142+
end
143+
end
144+
end)
145+
return block ~= nil
146+
end
147+
148+
local function checkWrongIndentation(results, uri, position, ch)
149+
if ch ~= '\n' then
150+
return
151+
end
152+
local state = files.getState(uri)
153+
if not state then
154+
return
155+
end
156+
local row = guide.rowColOf(position)
157+
if row <= 0 then
158+
return
159+
end
160+
local myIndent = getIndent(state, row)
161+
local lastIndent = getIndent(state, row - 1)
162+
if #myIndent <= #lastIndent then
163+
return
164+
end
165+
if not util.stringStartWith(myIndent, lastIndent) then
166+
return
167+
end
168+
local lastOffset = lookBackward.findAnyOffset(state.lua, guide.positionToOffset(state, position) - 1)
169+
if not lastOffset then
170+
return
171+
end
172+
local lastPosition = guide.offsetToPosition(state, lastOffset)
173+
if isInBlock(state, lastPosition) then
174+
return
175+
end
176+
results[#results+1] = {
177+
start = position - #myIndent + #lastIndent,
178+
finish = position,
179+
text = '',
180+
}
181+
end
182+
91183
local function typeFormat(results, uri, position, ch, options)
92184
if ch ~= '\n' then
93185
return
94186
end
187+
local suc, codeFormat = pcall(require, "code_format")
188+
if not suc then
189+
return
190+
end
95191
local text = files.getOriginText(uri)
96192
local state = files.getState(uri)
97193
if not state then
@@ -120,9 +216,23 @@ return function (uri, position, ch, options)
120216
local results = {}
121217
-- split `function () $ end`
122218
checkSplitOneLine(results, uri, position, ch)
123-
if #results == 0 then
124-
typeFormat(results, uri, position, ch, options)
219+
if #results > 0 then
220+
return results
125221
end
126222

127-
return results
223+
checkWrongIndentation(results, uri, position, ch)
224+
if #results > 0 then
225+
return results
226+
end
227+
228+
if TEST then
229+
return nil
230+
end
231+
232+
typeFormat(results, uri, position, ch, options)
233+
if #results > 0 then
234+
return results
235+
end
236+
237+
return nil
128238
end

test/type_formatting/init.lua

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,93 @@ TEST [[
117117
}
118118

119119
TEST [[
120-
if true then
120+
local x = 1
121+
<??>
122+
]]
123+
{
124+
ch = '\n',
125+
edits = nil,
126+
}
127+
128+
TEST [[
129+
local x = 'if 1 then'
130+
<??>
131+
]]
132+
{
133+
ch = '\n',
134+
edits = {
135+
{
136+
start = 10000,
137+
finish = 10004,
138+
text = '',
139+
}
140+
}
141+
}
142+
143+
TEST [[
144+
local x = 'do'
145+
<??>
146+
]]
147+
{
148+
ch = '\n',
149+
edits = {
150+
{
151+
start = 10000,
152+
finish = 10004,
153+
text = '',
154+
}
155+
}
156+
}
157+
158+
TEST [[
159+
local x = 'function'
121160
<??>
122-
end
123161
]]
124162
{
125163
ch = '\n',
126164
edits = {
127165
{
128-
start = 0,
129-
finish = 10000,
130-
text = 'if true then\n',
166+
start = 10000,
167+
finish = 10004,
168+
text = '',
131169
}
132170
}
133171
}
172+
173+
TEST [[
174+
do
175+
<??>
176+
]]
177+
{
178+
ch = '\n',
179+
edits = nil
180+
}
181+
182+
TEST [[
183+
do
184+
<??>
185+
end
186+
]]
187+
{
188+
ch = '\n',
189+
edits = nil
190+
}
191+
192+
TEST [[
193+
function ()
194+
<??>
195+
]]
196+
{
197+
ch = '\n',
198+
edits = nil
199+
}
200+
201+
TEST [[
202+
function ()
203+
<??>
204+
end
205+
]]
206+
{
207+
ch = '\n',
208+
edits = nil
209+
}

0 commit comments

Comments
 (0)