Skip to content

Commit a72aa21

Browse files
committed
删除多余空格
1 parent fcf2952 commit a72aa21

File tree

2 files changed

+36
-297
lines changed

2 files changed

+36
-297
lines changed

script/core/fix-indent.lua

Lines changed: 34 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,44 @@ local lookBackward = require 'core.look-backward'
44
local proto = require 'proto.proto'
55

66
---@param state parser.state
7-
local function insertIndentation(state, position, edits)
8-
local text = state.originText or state.lua
9-
local lines = state.originLines or state.lines
10-
local row = guide.rowColOf(position)
11-
if not lines or not text then
7+
---@param change table
8+
---@param edits table[]
9+
local function removeSpacesAfterEnter(state, change, edits)
10+
if not change.text:match '^\r?\n[\t ]+\r?\n' then
1211
return
1312
end
14-
local offset = lines[row]
15-
local indent = text:match('^%s*', offset)
16-
for _, edit in ipairs(edits) do
17-
edit.text = edit.text:gsub('\n', '\n' .. indent)
18-
end
19-
end
20-
21-
---@param state parser.state
22-
local function findForward(state, position, ...)
2313
local lines = state.originLines or state.lines
24-
local offset = guide.positionToOffsetByLines(lines, position)
25-
local firstOffset = state.originText:match('^[ \t]*()', offset + 1)
26-
if not firstOffset then
27-
return nil
28-
end
29-
for _, symbol in ipairs { ... } do
30-
if state.originText:sub(firstOffset, firstOffset + #symbol - 1) == symbol then
31-
return guide.offsetToPositionByLines(lines, firstOffset - 1), symbol
32-
end
33-
end
34-
return nil
35-
end
14+
local text = state.originText or state.lua
15+
---@cast text -?
3616

37-
---@param state parser.state
38-
local function findBackward(state, position, ...)
39-
local text = state.originText or state.lua
40-
local lines = state.originLines or state.lines
41-
if not text or not lines then
42-
return nil
43-
end
44-
local offset = guide.positionToOffsetByLines(lines, position)
45-
local lastOffset = lookBackward.findAnyOffset(text, offset)
46-
if not lastOffset then
47-
return nil
48-
end
49-
for _, symbol in ipairs { ... } do
50-
if text:sub(lastOffset - #symbol + 1, lastOffset) == symbol then
51-
return guide.offsetToPositionByLines(lines, lastOffset)
17+
-- 清除前置空格
18+
local startPos = guide.positionOf(change.range.start.line, change.range.start.character)
19+
local startOffset = guide.positionToOffsetByLines(lines, startPos)
20+
local leftOffset
21+
for offset = startOffset, lines[change.range.start.line], -1 do
22+
leftOffset = offset
23+
local char = text:sub(offset, offset)
24+
if char ~= ' ' and char ~= '\t' then
25+
break
5226
end
5327
end
54-
return nil
55-
end
56-
57-
---@param state parser.state
58-
---@param change table
59-
---@param result any[]
60-
local function checkSplitOneLine(state, change, result)
61-
if change.text ~= '\r\n'
62-
and change.text ~= '\n' then
63-
return
28+
if leftOffset and leftOffset < startOffset then
29+
edits[#edits+1] = {
30+
start = leftOffset,
31+
finish = startOffset,
32+
text = '',
33+
}
6434
end
6535

66-
local lines = state.originLines or state.lines
67-
local position = lines[change.range.start.line + 1]
68-
69-
local fPosition, fSymbol = findForward(state, position, 'end', '}')
70-
if not fPosition or not fSymbol then
71-
return
72-
end
73-
local bPosition = findBackward(state, position, 'then', 'do', ')', '{')
74-
if not bPosition then
75-
return
76-
end
77-
local edits = {}
78-
edits[#edits+1] = {
79-
start = bPosition,
80-
finish = position,
81-
text = '\n\t',
82-
}
83-
edits[#edits+1] = {
84-
start = position,
85-
finish = fPosition + 1,
86-
text = '',
87-
}
88-
edits[#edits+1] = {
89-
start = fPosition + 1,
90-
finish = fPosition + 1,
91-
text = '\n' .. fSymbol:sub(1, 1)
92-
}
93-
insertIndentation(state, bPosition, edits)
94-
for _, edit in ipairs(edits) do
95-
result[#result+1] = edit
36+
-- 清除后置空格
37+
local endOffset = startOffset + #change.text
38+
local _, rightOffset = text:find('^[\t ]+', endOffset + 1)
39+
if rightOffset then
40+
edits[#edits+1] = {
41+
start = endOffset,
42+
finish = rightOffset,
43+
text = '',
44+
}
9645
end
9746
end
9847

@@ -129,41 +78,22 @@ local function applyEdits(state, edits)
12978
label = 'Fix Indent',
13079
edit = {
13180
changes = {
132-
[state.uri] = {
133-
{
134-
range = {
135-
start = {
136-
line = 1,
137-
character = 0,
138-
},
139-
['end'] = {
140-
line = 1,
141-
character = 0,
142-
}
143-
},
144-
newText = '\t',
145-
},
146-
}
81+
[state.uri] = results
14782
}
14883
},
14984
})
150-
proto.notify('$/command', {
151-
command = 'cursorMove',
152-
})
15385
end
15486

15587
return function (uri, changes)
156-
do return end
15788
local state = files.compileState(uri)
15889
if not state then
15990
return
16091
end
16192

16293
local edits = {}
163-
for _, change in ipairs(changes) do
164-
if change.range then
165-
checkSplitOneLine(state, change, edits)
166-
end
94+
local firstChange = changes[1]
95+
if firstChange.range then
96+
removeSpacesAfterEnter(state, firstChange, edits)
16797
end
16898

16999
applyEdits(state, edits)

script/core/type-formatting.lua

Lines changed: 2 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -4,187 +4,6 @@ local guide = require 'parser.guide'
44
local config = require 'config'
55
local util = require 'utility'
66

7-
8-
local function insertIndentation(uri, position, edits)
9-
local text = files.getText(uri)
10-
local state = files.getState(uri)
11-
local row = guide.rowColOf(position)
12-
if not state or not text then
13-
return
14-
end
15-
local offset = state.lines[row]
16-
local indent = text:match('^%s*', offset)
17-
for _, edit in ipairs(edits) do
18-
edit.text = edit.text:gsub('\n', '\n' .. indent)
19-
end
20-
end
21-
22-
local function findForward(uri, position, ...)
23-
local text = files.getText(uri)
24-
local state = files.getState(uri)
25-
if not state or not text then
26-
return nil
27-
end
28-
local offset = guide.positionToOffset(state, position)
29-
local firstOffset = text:match('^[ \t]*()', offset + 1)
30-
if not firstOffset then
31-
return nil
32-
end
33-
for _, symbol in ipairs { ... } do
34-
if text:sub(firstOffset, firstOffset + #symbol - 1) == symbol then
35-
return guide.offsetToPosition(state, firstOffset - 1), symbol
36-
end
37-
end
38-
return nil
39-
end
40-
41-
local function findBackward(uri, position, ...)
42-
local text = files.getText(uri)
43-
local state = files.getState(uri)
44-
if not state or not text then
45-
return nil
46-
end
47-
local offset = guide.positionToOffset(state, position)
48-
local lastOffset = lookBackward.findAnyOffset(text, offset)
49-
for _, symbol in ipairs { ... } do
50-
if text:sub(lastOffset - #symbol + 1, lastOffset) == symbol then
51-
return guide.offsetToPosition(state, lastOffset)
52-
end
53-
end
54-
return nil
55-
end
56-
57-
local function checkSplitOneLine(results, uri, position, ch)
58-
if ch ~= '\n' then
59-
return
60-
end
61-
62-
local fPosition, fSymbol = findForward(uri, position, 'end', '}')
63-
if not fPosition or not fSymbol then
64-
return
65-
end
66-
local bPosition = findBackward(uri, position, 'then', 'do', ')', '{')
67-
if not bPosition then
68-
return
69-
end
70-
local edits = {}
71-
edits[#edits+1] = {
72-
start = bPosition,
73-
finish = position,
74-
text = '\n\t',
75-
}
76-
edits[#edits+1] = {
77-
start = position,
78-
finish = fPosition + 1,
79-
text = '',
80-
}
81-
edits[#edits+1] = {
82-
start = fPosition + 1,
83-
finish = fPosition + 1,
84-
text = '\n' .. fSymbol:sub(1, 1)
85-
}
86-
insertIndentation(uri, bPosition, edits)
87-
for _, edit in ipairs(edits) do
88-
results[#results+1] = edit
89-
end
90-
end
91-
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-
if source.type == 'table' then
145-
if source.start + 1 == position then
146-
return true
147-
end
148-
end
149-
end)
150-
return block ~= nil
151-
end
152-
153-
local function checkWrongIndentation(results, uri, position, ch)
154-
if ch ~= '\n' then
155-
return
156-
end
157-
local state = files.getState(uri)
158-
if not state then
159-
return
160-
end
161-
local row = guide.rowColOf(position)
162-
if row <= 0 then
163-
return
164-
end
165-
local myIndent = getIndent(state, row)
166-
local lastIndent = getIndent(state, row - 1)
167-
if #myIndent <= #lastIndent then
168-
return
169-
end
170-
if not util.stringStartWith(myIndent, lastIndent) then
171-
return
172-
end
173-
local lastOffset = lookBackward.findAnyOffset(state.lua, guide.positionToOffset(state, position) - 1)
174-
if not lastOffset then
175-
return
176-
end
177-
local lastPosition = guide.offsetToPosition(state, lastOffset)
178-
if isInBlock(state, lastPosition) then
179-
return
180-
end
181-
results[#results+1] = {
182-
start = position - #myIndent + #lastIndent,
183-
finish = position,
184-
text = '',
185-
}
186-
end
187-
1887
local function typeFormat(results, uri, position, ch, options)
1898
if ch ~= '\n' then
1909
return
@@ -218,22 +37,12 @@ return function (uri, position, ch, options)
21837
return nil
21938
end
22039

221-
local results = {}
222-
-- split `function () $ end`
223-
checkSplitOneLine(results, uri, position, ch)
224-
if #results > 0 then
225-
return results
226-
end
227-
228-
checkWrongIndentation(results, uri, position, ch)
229-
if #results > 0 then
230-
return results
231-
end
232-
23340
if TEST then
23441
return nil
23542
end
23643

44+
local results = {}
45+
23746
typeFormat(results, uri, position, ch, options)
23847
if #results > 0 then
23948
return results

0 commit comments

Comments
 (0)