summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2024-08-15 18:48:20 +0800
committer最萌小汐 <sumneko@hotmail.com>2024-08-15 18:48:20 +0800
commitbed118701e6fb12e27f67d744b1895952eb24e33 (patch)
treebe2ae678298eb09f31407dda5a19e48b68d18aab
parent2fe2ff371a8d2ee97a46c1b48eb421d9f8ee65ad (diff)
downloadlua-language-server-bed118701e6fb12e27f67d744b1895952eb24e33.zip
fix incorrect indent fixings
fix #2799
-rw-r--r--changelog.md1
-rw-r--r--script/core/fix-indent.lua64
-rw-r--r--script/parser/compile.lua19
-rw-r--r--script/parser/guide.lua7
4 files changed, 38 insertions, 53 deletions
diff --git a/changelog.md b/changelog.md
index c66283cd..6f872d20 100644
--- a/changelog.md
+++ b/changelog.md
@@ -5,6 +5,7 @@
* `NEW` Add matching checks between the shape of tables and classes, during type checking. [#2768](https://github.com/LuaLS/lua-language-server/pull/2768)
* `FIX` Error `attempt to index a nil value` when `Lua.hint.semicolon == 'All'` [#2788](https://github.com/LuaLS/lua-language-server/issues/2788)
* `FIX` Incorrect LuaCats parsing for `"'"`
+* `FIX` Incorrect indent fixings
## 3.10.3
`2024-8-8`
diff --git a/script/core/fix-indent.lua b/script/core/fix-indent.lua
index 59adfb7b..0542e4a8 100644
--- a/script/core/fix-indent.lua
+++ b/script/core/fix-indent.lua
@@ -60,59 +60,20 @@ local function getIndent(state, row)
return indent
end
-local function isInBlock(state, position)
- local block = guide.eachSourceContain(state.ast, position, function(source)
- if source.type == 'ifblock'
- or source.type == 'elseifblock' then
- if source.keyword[4] and source.keyword[4] <= position then
- return true
- end
- end
- if source.type == 'else' then
- if source.keyword[2] and source.keyword[2] <= position then
- return true
- end
- end
- if source.type == 'while' then
- if source.keyword[4] and source.keyword[4] <= position then
- return true
- end
- end
- if source.type == 'repeat' then
- if source.keyword[2] and source.keyword[2] <= position then
- return true
- end
- end
- if source.type == 'loop' then
- if source.keyword[4] and source.keyword[4] <= position then
- return true
- end
- end
- if source.type == 'in' then
- if source.keyword[6] and source.keyword[6] <= position then
- return true
- end
- end
- if source.type == 'do' then
- if source.keyword[2] and source.keyword[2] <= position then
- return true
- end
- end
- if source.type == 'function' then
- if source.args and source.args.finish <= position then
- return true
- end
- if not source.keyword[3] or source.keyword[3] >= position then
- return true
- end
+---@param state parser.state
+---@param pos integer
+---@return parser.object
+local function getBlock(state, pos)
+ local block
+ guide.eachSourceContain(state.ast, pos, function (src)
+ if not src.bstart then
+ return
end
- if source.type == 'table' then
- if source.start + 1 == position then
- return true
- end
+ if not block or block.bstart < src.bstart then
+ block = src
end
end)
- return block ~= nil
+ return block
end
local function fixWrongIndent(state, change)
@@ -135,7 +96,8 @@ local function fixWrongIndent(state, change)
if not util.stringStartWith(myIndent, lastIndent) then
return
end
- if isInBlock(state, lastPosition) then
+ local myBlock = getBlock(state, position)
+ if myBlock.bstart >= lastOffset then
return
end
diff --git a/script/parser/compile.lua b/script/parser/compile.lua
index d5a2e162..623a8c2c 100644
--- a/script/parser/compile.lua
+++ b/script/parser/compile.lua
@@ -1585,6 +1585,7 @@ local function parseTable()
start = getPosition(Tokens[Index], 'left'),
finish = getPosition(Tokens[Index], 'right'),
}
+ tbl.bstart = tbl.finish
Index = Index + 2
local index = 0
local tindex = 0
@@ -1593,6 +1594,7 @@ local function parseTable()
skipSpace(true)
local token = Tokens[Index + 1]
if token == '}' then
+ tbl.bfinish = getPosition(Tokens[Index], 'left')
Index = Index + 2
break
end
@@ -1712,6 +1714,8 @@ local function parseTable()
end
missSymbol '}'
+ skipSpace()
+ tbl.bfinish = getPosition(Tokens[Index], 'left')
break
::CONTINUE::
end
@@ -2379,6 +2383,7 @@ local function parseFunction(isLocal, isAction)
end
parseActions()
popChunk()
+ func.bfinish = getPosition(Tokens[Index], 'left')
if Tokens[Index + 1] == 'end' then
local endLeft = getPosition(Tokens[Index], 'left')
local endRight = getPosition(Tokens[Index] + 2, 'right')
@@ -2464,7 +2469,8 @@ local function parseLambda(isDoublePipe)
start = child.start,
finish = child.finish,
parent = lambda,
- [1] = child}
+ [1] = child
+ }
child.parent = rtn
lambda[1] = rtn
lambda.returns = {rtn}
@@ -2475,6 +2481,7 @@ local function parseLambda(isDoublePipe)
lambda.finish = lastRightPosition()
missExp()
end
+ lambda.bfinish = getPosition(Tokens[Index], 'left')
LocalCount = LastLocalCount
return lambda
end
@@ -3131,6 +3138,7 @@ local function parseDo()
pushChunk(obj)
parseActions()
popChunk()
+ obj.bfinish = getPosition(Tokens[Index], 'left')
if Tokens[Index + 1] == 'end' then
obj.finish = getPosition(Tokens[Index] + 2, 'right')
obj.keyword[3] = getPosition(Tokens[Index], 'left')
@@ -3350,6 +3358,7 @@ local function parseIfBlock(parent)
parseActions()
popChunk()
ifblock.finish = getPosition(Tokens[Index], 'left')
+ ifblock.bfinish = ifblock.finish
if ifblock.locals then
LocalCount = LocalCount - #ifblock.locals
end
@@ -3412,6 +3421,7 @@ local function parseElseIfBlock(parent)
parseActions()
popChunk()
elseifblock.finish = getPosition(Tokens[Index], 'left')
+ elseifblock.bfinish = elseifblock.finish
if elseifblock.locals then
LocalCount = LocalCount - #elseifblock.locals
end
@@ -3438,6 +3448,7 @@ local function parseElseBlock(parent)
parseActions()
popChunk()
elseblock.finish = getPosition(Tokens[Index], 'left')
+ elseblock.bfinish = elseblock.finish
if elseblock.locals then
LocalCount = LocalCount - #elseblock.locals
end
@@ -3680,8 +3691,8 @@ local function parseFor()
skipSpace()
parseActions()
popChunk()
-
skipSpace()
+ action.bfinish = getPosition(Tokens[Index], 'left')
if Tokens[Index + 1] == 'end' then
action.finish = getPosition(Tokens[Index] + 2, 'right')
action.keyword[#action.keyword+1] = getPosition(Tokens[Index], 'left')
@@ -3763,6 +3774,7 @@ local function parseWhile()
popChunk()
skipSpace()
+ action.bfinish = getPosition(Tokens[Index], 'left')
if Tokens[Index + 1] == 'end' then
action.finish = getPosition(Tokens[Index] + 2, 'right')
action.keyword[#action.keyword+1] = getPosition(Tokens[Index], 'left')
@@ -3797,6 +3809,7 @@ local function parseRepeat()
parseActions()
skipSpace()
+ action.bfinish = getPosition(Tokens[Index], 'left')
if Tokens[Index + 1] == 'until' then
action.finish = getPosition(Tokens[Index] + 4, 'right')
action.keyword[#action.keyword+1] = getPosition(Tokens[Index], 'left')
@@ -3989,6 +4002,7 @@ local function parseLua()
type = 'main',
start = 0,
finish = 0,
+ bstart = 0,
}
pushChunk(main)
createLocal{
@@ -4014,6 +4028,7 @@ local function parseLua()
end
popChunk()
main.finish = getPosition(#Lua, 'right')
+ main.bfinish = main.finish
return main
end
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 768d7dec..aeb22965 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -22,6 +22,7 @@ local type = type
---@field range integer
---@field effect integer
---@field bstart integer
+---@field bfinish integer
---@field attrs string[]
---@field specials parser.object[]
---@field labels parser.object[]
@@ -589,6 +590,9 @@ end
function m.getStartFinish(source)
local start = source.start
local finish = source.finish
+ if source.bfinish and source.bfinish > finish then
+ finish = source.bfinish
+ end
if not start then
local first = source[1]
if not first then
@@ -604,6 +608,9 @@ end
function m.getRange(source)
local start = source.vstart or source.start
local finish = source.range or source.finish
+ if source.bfinish and source.bfinish > finish then
+ finish = source.bfinish
+ end
if not start then
local first = source[1]
if not first then