Skip to content

Commit 59b4390

Browse files
authored
Merge branch 'master' into patch-1
2 parents d0a712d + d87be6d commit 59b4390

File tree

3 files changed

+121
-9
lines changed

3 files changed

+121
-9
lines changed

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
* `FIX` Typos in annotation descriptions
1717
* `NEW` You can now click on "References" in CodeLen to display the reference list
1818
* `FIX` incorrect `CompletionItemKind` for postfix snippets [#2773](https://github.com/LuaLS/lua-language-server/pull/2773)
19+
* `NEW` You can now click on "References" in CodeLen to display the reference list(VSCode)
20+
* `NEW` Improved behavior for inserting new lines:
21+
+ When inside an annotation, an annotation tag will be added at the beginning of the line (VSCode).
22+
+ When between `function () end` or similar constructs, the format will be adjusted to a more reasonable one (VSCode) and leading/trailing spaces will be removed (generic).
23+
+ Attempts to semantically fix improper indentation (generic).
1924

2025
## 3.9.3
2126
`2024-6-11`

script/core/fix-indent.lua

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
local files = require 'files'
22
local guide = require 'parser.guide'
3-
local lookBackward = require 'core.look-backward'
43
local proto = require 'proto.proto'
4+
local lookBackward = require 'core.look-backward'
5+
local util = require 'utility'
56

67
---@param state parser.state
78
---@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
11-
return
9+
local function removeSpacesAfterEnter(state, change)
10+
if not change.text:match '^\r?\n[\t ]+\r?\n$' then
11+
return false
1212
end
1313
local lines = state.originLines or state.lines
1414
local text = state.originText or state.lua
1515
---@cast text -?
1616

17+
local edits = {}
1718
-- 清除前置空格
1819
local startPos = guide.positionOf(change.range.start.line, change.range.start.character)
1920
local startOffset = guide.positionToOffsetByLines(lines, startPos)
@@ -25,6 +26,7 @@ local function removeSpacesAfterEnter(state, change, edits)
2526
break
2627
end
2728
end
29+
2830
if leftOffset and leftOffset < startOffset then
2931
edits[#edits+1] = {
3032
start = leftOffset,
@@ -43,6 +45,108 @@ local function removeSpacesAfterEnter(state, change, edits)
4345
text = '',
4446
}
4547
end
48+
49+
if #edits == 0 then
50+
return nil
51+
end
52+
53+
return edits
54+
end
55+
56+
local function getIndent(state, row)
57+
local offset = state.lines[row]
58+
local indent = state.lua:match('^[\t ]*', offset)
59+
return indent
60+
end
61+
62+
local function isInBlock(state, position)
63+
local block = guide.eachSourceContain(state.ast, position, function(source)
64+
if source.type == 'ifblock'
65+
or source.type == 'elseifblock' then
66+
if source.keyword[4] and source.keyword[4] <= position then
67+
return true
68+
end
69+
end
70+
if source.type == 'else' then
71+
if source.keyword[2] and source.keyword[2] <= position then
72+
return true
73+
end
74+
end
75+
if source.type == 'while' then
76+
if source.keyword[4] and source.keyword[4] <= position then
77+
return true
78+
end
79+
end
80+
if source.type == 'repeat' then
81+
if source.keyword[2] and source.keyword[2] <= position then
82+
return true
83+
end
84+
end
85+
if source.type == 'loop' then
86+
if source.keyword[4] and source.keyword[4] <= position then
87+
return true
88+
end
89+
end
90+
if source.type == 'in' then
91+
if source.keyword[6] and source.keyword[6] <= position then
92+
return true
93+
end
94+
end
95+
if source.type == 'do' then
96+
if source.keyword[2] and source.keyword[2] <= position then
97+
return true
98+
end
99+
end
100+
if source.type == 'function' then
101+
if source.args and source.args.finish <= position then
102+
return true
103+
end
104+
if not source.keyword[3] or source.keyword[3] >= position then
105+
return true
106+
end
107+
end
108+
if source.type == 'table' then
109+
if source.start + 1 == position then
110+
return true
111+
end
112+
end
113+
end)
114+
return block ~= nil
115+
end
116+
117+
local function fixWrongIdent(state, change)
118+
if not change.text:match '^\r?\n[\t ]+$' then
119+
return false
120+
end
121+
local position = guide.positionOf(change.range.start.line, change.range.start.character)
122+
local row = guide.rowColOf(position)
123+
local myIndent = getIndent(state, row + 1)
124+
local lastIndent = getIndent(state, row)
125+
if #myIndent <= #lastIndent then
126+
return
127+
end
128+
if not util.stringStartWith(myIndent, lastIndent) then
129+
return
130+
end
131+
local lastOffset = lookBackward.findAnyOffset(state.lua, guide.positionToOffset(state, position))
132+
if not lastOffset then
133+
return
134+
end
135+
local lastPosition = guide.offsetToPosition(state, lastOffset)
136+
if isInBlock(state, lastPosition) then
137+
return
138+
end
139+
140+
local endOffset = guide.positionToOffset(state, position) + #change.text
141+
142+
local edits = {}
143+
edits[#edits+1] = {
144+
start = endOffset - #myIndent + #lastIndent,
145+
finish = endOffset,
146+
text = '',
147+
}
148+
149+
return edits
46150
end
47151

48152
---@param state parser.state
@@ -90,11 +194,12 @@ return function (uri, changes)
90194
return
91195
end
92196

93-
local edits = {}
94197
local firstChange = changes[1]
95198
if firstChange.range then
96-
removeSpacesAfterEnter(state, firstChange, edits)
199+
local edits = removeSpacesAfterEnter(state, firstChange)
200+
or fixWrongIdent(state, firstChange)
201+
if edits then
202+
applyEdits(state, edits)
203+
end
97204
end
98-
99-
applyEdits(state, edits)
100205
end

script/parser/guide.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ local blockTypes = {
105105
['main'] = true,
106106
}
107107

108+
m.blockTypes = blockTypes
109+
108110
local topBlockTypes = {
109111
['while'] = true,
110112
['function'] = true,

0 commit comments

Comments
 (0)