summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--meta/template/math.lua16
-rw-r--r--script/config/loader.lua1
-rw-r--r--script/core/code-action.lua5
-rw-r--r--script/core/command/autoRequire.lua2
-rw-r--r--script/core/command/removeSpace.lua2
-rw-r--r--script/core/command/solve.lua2
-rw-r--r--script/core/completion/completion.lua3
-rw-r--r--script/core/diagnostics/ambiguity-1.lua4
-rw-r--r--script/core/diagnostics/newfield-call.lua8
-rw-r--r--script/core/diagnostics/newline-call.lua2
-rw-r--r--script/core/diagnostics/trailing-space.lua4
-rw-r--r--script/core/rename.lua3
-rw-r--r--script/core/signature.lua3
-rw-r--r--script/core/type-formatting.lua8
-rw-r--r--script/files.lua4
-rw-r--r--script/parser/guide.lua2
-rw-r--r--script/parser/luadoc.lua3
-rw-r--r--script/proto/converter.lua2
-rw-r--r--script/provider/diagnostic.lua7
-rw-r--r--script/service/service.lua2
-rw-r--r--test/type_inference/init.lua9
21 files changed, 67 insertions, 25 deletions
diff --git a/meta/template/math.lua b/meta/template/math.lua
index d9837424..c126aef7 100644
--- a/meta/template/math.lua
+++ b/meta/template/math.lua
@@ -137,16 +137,18 @@ function math.log(x, base) end
function math.log10(x) end
---#DES 'math.max'
----@param x number
----@param ... number
----@return number
+---@generic Number: number
+---@param x Number
+---@param ... Number
+---@return Number
---@nodiscard
function math.max(x, ...) end
---#DES 'math.min'
----@param x number
----@param ... number
----@return number
+---@generic Number: number
+---@param x Number
+---@param ... Number
+---@return Number
---@nodiscard
function math.min(x, ...) end
@@ -225,7 +227,7 @@ function math.tanh(x) end
---@version >5.3
---#DES 'math.tointeger'
----@param x number
+---@param x number|string
---@return integer?
---@nodiscard
function math.tointeger(x) end
diff --git a/script/config/loader.lua b/script/config/loader.lua
index 867c3b16..c95c0a35 100644
--- a/script/config/loader.lua
+++ b/script/config/loader.lua
@@ -39,6 +39,7 @@ function m.loadRCConfig(uri, filename)
return res
end
+---@return table?
function m.loadLocalConfig(uri, filename)
local scp = scope.getScope(uri)
local path = workspace.getAbsolutePath(uri, filename)
diff --git a/script/core/code-action.lua b/script/core/code-action.lua
index f9926b64..4eb21ff8 100644
--- a/script/core/code-action.lua
+++ b/script/core/code-action.lua
@@ -451,7 +451,7 @@ end
local function checkSwapParams(results, uri, start, finish)
local state = files.getState(uri)
local text = files.getText(uri)
- if not state then
+ if not state or not text then
return
end
local args = {}
@@ -619,6 +619,9 @@ end
local function checkJsonToLua(results, uri, start, finish)
local text = files.getText(uri)
local state = files.getState(uri)
+ if not state or not text then
+ return
+ end
local startOffset = guide.positionToOffset(state, start)
local finishOffset = guide.positionToOffset(state, finish)
local jsonStart = text:match('()[%{%[]', startOffset + 1)
diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua
index 923c6f11..020cacae 100644
--- a/script/core/command/autoRequire.lua
+++ b/script/core/command/autoRequire.lua
@@ -21,7 +21,7 @@ end
local function findInsertRow(uri)
local text = files.getText(uri)
local state = files.getState(uri)
- if not state then
+ if not state or not text then
return
end
local lines = state.lines
diff --git a/script/core/command/removeSpace.lua b/script/core/command/removeSpace.lua
index aa565f7f..69a01b53 100644
--- a/script/core/command/removeSpace.lua
+++ b/script/core/command/removeSpace.lua
@@ -17,7 +17,7 @@ return function (data)
local uri = data.uri
local text = files.getText(uri)
local state = files.getState(uri)
- if not state then
+ if not state or not text then
return
end
diff --git a/script/core/command/solve.lua b/script/core/command/solve.lua
index 8065aa9d..98ceaa58 100644
--- a/script/core/command/solve.lua
+++ b/script/core/command/solve.lua
@@ -32,7 +32,7 @@ return function (data)
local uri = data.uri
local text = files.getText(uri)
local state = files.getState(uri)
- if not state then
+ if not state or not text then
return
end
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua
index 4fc63962..2f0180d2 100644
--- a/script/core/completion/completion.lua
+++ b/script/core/completion/completion.lua
@@ -66,6 +66,9 @@ end
local function findNearestTableField(state, position)
local uri = state.uri
local text = files.getText(uri)
+ if not text then
+ return nil
+ end
local offset = guide.positionToOffset(state, position)
local soffset = lookBackward.findAnyOffset(text, offset)
if not soffset then
diff --git a/script/core/diagnostics/ambiguity-1.lua b/script/core/diagnostics/ambiguity-1.lua
index f03f4361..830b2f2f 100644
--- a/script/core/diagnostics/ambiguity-1.lua
+++ b/script/core/diagnostics/ambiguity-1.lua
@@ -27,10 +27,10 @@ local literalMap = {
return function (uri, callback)
local state = files.getState(uri)
- if not state then
+ local text = files.getText(uri)
+ if not state or not text then
return
end
- local text = files.getText(uri)
guide.eachSourceType(state.ast, 'binary', function (source)
if source.op.type ~= 'or' then
return
diff --git a/script/core/diagnostics/newfield-call.lua b/script/core/diagnostics/newfield-call.lua
index 669ed2bb..b81d8c0a 100644
--- a/script/core/diagnostics/newfield-call.lua
+++ b/script/core/diagnostics/newfield-call.lua
@@ -3,14 +3,14 @@ local guide = require 'parser.guide'
local lang = require 'language'
return function (uri, callback)
- local ast = files.getState(uri)
- if not ast then
+ local state = files.getState(uri)
+ local text = files.getText(uri)
+ if not state or not text then
return
end
- local text = files.getText(uri)
- guide.eachSourceType(ast.ast, 'table', function (source)
+ guide.eachSourceType(state.ast, 'table', function (source)
for i = 1, #source do
local field = source[i]
if field.type ~= 'tableexp' then
diff --git a/script/core/diagnostics/newline-call.lua b/script/core/diagnostics/newline-call.lua
index 3f2d5ca5..9c526980 100644
--- a/script/core/diagnostics/newline-call.lua
+++ b/script/core/diagnostics/newline-call.lua
@@ -5,7 +5,7 @@ local lang = require 'language'
return function (uri, callback)
local state = files.getState(uri)
local text = files.getText(uri)
- if not state then
+ if not state or not text then
return
end
diff --git a/script/core/diagnostics/trailing-space.lua b/script/core/diagnostics/trailing-space.lua
index cc51cf77..d23b0692 100644
--- a/script/core/diagnostics/trailing-space.lua
+++ b/script/core/diagnostics/trailing-space.lua
@@ -14,10 +14,10 @@ end
return function (uri, callback)
local state = files.getState(uri)
- if not state then
+ local text = files.getText(uri)
+ if not state or not text then
return
end
- local text = files.getText(uri)
local lines = state.lines
for i = 0, #lines do
local startOffset = lines[i]
diff --git a/script/core/rename.lua b/script/core/rename.lua
index c3325b7e..85cbab52 100644
--- a/script/core/rename.lua
+++ b/script/core/rename.lua
@@ -81,6 +81,9 @@ local function renameField(source, newname, callback)
local uri = guide.getUri(source)
local text = files.getText(uri)
local state = files.getState(uri)
+ if not state or not text then
+ return false
+ end
local func = parent.value
-- function mt:name () end --> mt['newname'] = function (self) end
local startOffset = guide.positionToOffset(state, parent.start) + 1
diff --git a/script/core/signature.lua b/script/core/signature.lua
index 025e70b7..f5de90d5 100644
--- a/script/core/signature.lua
+++ b/script/core/signature.lua
@@ -8,6 +8,9 @@ local lookback = require 'core.look-backward'
local function findNearCall(uri, ast, pos)
local text = files.getText(uri)
local state = files.getState(uri)
+ if not state or not text then
+ return nil
+ end
local nearCall
guide.eachSourceContain(ast.ast, pos, function (src)
if src.type == 'call'
diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua
index 7005af26..0c326b8b 100644
--- a/script/core/type-formatting.lua
+++ b/script/core/type-formatting.lua
@@ -6,7 +6,7 @@ local function insertIndentation(uri, position, edits)
local text = files.getText(uri)
local state = files.getState(uri)
local row = guide.rowColOf(position)
- if not state then
+ if not state or not text then
return
end
local offset = state.lines[row]
@@ -19,6 +19,9 @@ end
local function findForward(uri, position, ...)
local text = files.getText(uri)
local state = files.getState(uri)
+ if not state or not text then
+ return nil
+ end
local offset = guide.positionToOffset(state, position)
local firstOffset = text:match('^[ \t]*()', offset + 1)
if not firstOffset then
@@ -35,6 +38,9 @@ end
local function findBackward(uri, position, ...)
local text = files.getText(uri)
local state = files.getState(uri)
+ if not state or not text then
+ return nil
+ end
local offset = guide.positionToOffset(state, position)
local lastOffset = lookBackward.findAnyOffset(text, offset)
for _, symbol in ipairs { ... } do
diff --git a/script/files.lua b/script/files.lua
index 513fb52f..3bef6d33 100644
--- a/script/files.lua
+++ b/script/files.lua
@@ -177,7 +177,7 @@ end
--- 设置文件文本
---@param uri uri
----@param text string
+---@param text? string
---@param isTrust? boolean
---@param callback? function
function m.setText(uri, text, isTrust, callback)
@@ -333,7 +333,7 @@ end
--- 获取文件文本
---@param uri uri
----@return string text
+---@return string? text
function m.getText(uri)
local file = m.fileMap[uri]
if not file then
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 7a9cb875..969eb386 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -825,6 +825,8 @@ function m.positionToOffset(state, position)
return m.positionToOffsetByLines(state.lines, position)
end
+---@param lines integer[]
+---@param offset integer
function m.offsetToPositionByLines(lines, offset)
local left = 0
local right = #lines
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index 94be437f..847b7d37 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -159,6 +159,8 @@ local function peekToken()
return TokenTypes[Ci+1], TokenContents[Ci+1]
end
+---@return string tokenType
+---@return string tokenContent
local function nextToken()
Ci = Ci + 1
if not TokenTypes[Ci] then
@@ -181,6 +183,7 @@ local function getStart()
return TokenStarts[Ci] + Offset
end
+---@return integer
local function getFinish()
if Ci == 0 then
return Offset
diff --git a/script/proto/converter.lua b/script/proto/converter.lua
index 9c75f056..3f5ddebc 100644
--- a/script/proto/converter.lua
+++ b/script/proto/converter.lua
@@ -13,7 +13,7 @@ local function rawPackPosition(uri, pos)
if col > 0 then
local state = files.getState(uri)
local text = files.getText(uri)
- if text then
+ if state and text then
local lineOffset = state.lines[row]
if lineOffset then
local start = lineOffset
diff --git a/script/provider/diagnostic.lua b/script/provider/diagnostic.lua
index 23dad33d..4c94c952 100644
--- a/script/provider/diagnostic.lua
+++ b/script/provider/diagnostic.lua
@@ -32,6 +32,9 @@ end
local function buildSyntaxError(uri, err)
local text = files.getText(uri)
+ if not text then
+ return
+ end
local message = lang.script('PARSER_' .. err.type, err.info)
if err.version then
@@ -83,10 +86,14 @@ local function buildDiagnostic(uri, diag)
relatedInformation = {}
for _, rel in ipairs(diag.related) do
local rtext = files.getText(rel.uri)
+ if not rtext then
+ goto CONTINUE
+ end
relatedInformation[#relatedInformation+1] = {
message = rel.message or rtext:sub(rel.start, rel.finish),
location = converter.location(rel.uri, converter.packRange(rel.uri, rel.start, rel.finish))
}
+ ::CONTINUE::
end
end
diff --git a/script/service/service.lua b/script/service/service.lua
index 26790c63..07612d7b 100644
--- a/script/service/service.lua
+++ b/script/service/service.lua
@@ -235,7 +235,7 @@ end
function m.testVersion()
local stack = debug.setcstacklimit(200)
debug.setcstacklimit(stack + 1)
- if debug.setcstacklimit(stack) == stack + 1 then
+ if type(stack) == 'number' and debug.setcstacklimit(stack) == stack + 1 then
proto.notify('window/showMessage', {
type = 2,
message = 'It seems to be running in Lua 5.4.0 or Lua 5.4.1 . Please upgrade to Lua 5.4.2 or above. Otherwise, it may encounter weird "C stack overflow", resulting in failure to work properly',
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 254320e4..bca3a546 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -3007,3 +3007,12 @@ local x = true
local y = x--[[@as integer]] --is `integer` here
local z = <?x?>--[[@as string]] --is `true` here
]=]
+
+TEST 'integer' [[
+---@type integer
+local x
+
+if type(x) == 'number' then
+ print(<?x?>)
+end
+]]