summaryrefslogtreecommitdiff
path: root/server-beta
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-04 17:28:42 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-04 17:28:42 +0800
commit137ede024f4ddeac7c602351371447cd6cf36091 (patch)
treebff1d78ec63125294bf9337a192902f706db731a /server-beta
parenteb1fffc9ddfaa804973b1e05c7001d84194ce5a6 (diff)
downloadlua-language-server-137ede024f4ddeac7c602351371447cd6cf36091.zip
更新诊断
Diffstat (limited to 'server-beta')
-rw-r--r--server-beta/src/core/diagnostics/ambiguity-1.lua3
-rw-r--r--server-beta/src/core/diagnostics/duplicate-index.lua3
-rw-r--r--server-beta/src/core/diagnostics/duplicate-method.lua3
-rw-r--r--server-beta/src/core/diagnostics/emmy-lua.lua3
-rw-r--r--server-beta/src/core/diagnostics/empty-block.lua3
-rw-r--r--server-beta/src/core/diagnostics/global-in-nil-env.lua3
-rw-r--r--server-beta/src/core/diagnostics/init.lua29
-rw-r--r--server-beta/src/core/diagnostics/lowercase-global.lua3
-rw-r--r--server-beta/src/core/diagnostics/newfield-call.lua3
-rw-r--r--server-beta/src/core/diagnostics/newline-call.lua3
-rw-r--r--server-beta/src/core/diagnostics/redefined-local.lua3
-rw-r--r--server-beta/src/core/diagnostics/redundant-parameter.lua3
-rw-r--r--server-beta/src/core/diagnostics/redundant-value.lua3
-rw-r--r--server-beta/src/core/diagnostics/set-const.lua3
-rw-r--r--server-beta/src/core/diagnostics/trailing-space.lua3
-rw-r--r--server-beta/src/core/diagnostics/undefined-env-child.lua3
-rw-r--r--server-beta/src/core/diagnostics/undefined-global.lua3
-rw-r--r--server-beta/src/core/diagnostics/unused-function.lua3
-rw-r--r--server-beta/src/core/diagnostics/unused-label.lua3
-rw-r--r--server-beta/src/core/diagnostics/unused-local.lua38
-rw-r--r--server-beta/src/core/diagnostics/unused-vararg.lua3
-rw-r--r--server-beta/src/language.lua7
-rw-r--r--server-beta/src/parser/guide.lua17
-rw-r--r--server-beta/src/proto/define.lua (renamed from server-beta/src/proto/interface.lua)39
-rw-r--r--server-beta/src/proto/provider.lua12
-rw-r--r--server-beta/test.lua6
-rw-r--r--server-beta/test/diagnostics/init.lua239
27 files changed, 309 insertions, 135 deletions
diff --git a/server-beta/src/core/diagnostics/ambiguity-1.lua b/server-beta/src/core/diagnostics/ambiguity-1.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/ambiguity-1.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/duplicate-index.lua b/server-beta/src/core/diagnostics/duplicate-index.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/duplicate-index.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/duplicate-method.lua b/server-beta/src/core/diagnostics/duplicate-method.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/duplicate-method.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/emmy-lua.lua b/server-beta/src/core/diagnostics/emmy-lua.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/emmy-lua.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/empty-block.lua b/server-beta/src/core/diagnostics/empty-block.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/empty-block.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/global-in-nil-env.lua b/server-beta/src/core/diagnostics/global-in-nil-env.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/global-in-nil-env.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/init.lua b/server-beta/src/core/diagnostics/init.lua
new file mode 100644
index 00000000..69815003
--- /dev/null
+++ b/server-beta/src/core/diagnostics/init.lua
@@ -0,0 +1,29 @@
+local files = require 'files'
+local define = require 'proto.define'
+local config = require 'config'
+
+local function check(uri, name, level, results)
+ if config.config.diagnostics.disable[name] then
+ return
+ end
+ level = config.config.diagnostics.severity[name] or level
+ require('core.diagnostics.' .. name)(uri, function (result)
+ result.level = level or result.level
+ result.code = name
+ results[#results+1] = result
+ end)
+end
+
+return function (uri)
+ local ast = files.getAst(uri)
+ if not ast then
+ return nil
+ end
+ local results = {}
+
+ for name, level in pairs(define.DiagnosticDefaultSeverity) do
+ check(uri, name, level, results)
+ end
+
+ return results
+end
diff --git a/server-beta/src/core/diagnostics/lowercase-global.lua b/server-beta/src/core/diagnostics/lowercase-global.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/lowercase-global.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/newfield-call.lua b/server-beta/src/core/diagnostics/newfield-call.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/newfield-call.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/newline-call.lua b/server-beta/src/core/diagnostics/newline-call.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/newline-call.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/redefined-local.lua b/server-beta/src/core/diagnostics/redefined-local.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/redefined-local.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/redundant-parameter.lua b/server-beta/src/core/diagnostics/redundant-parameter.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/redundant-parameter.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/redundant-value.lua b/server-beta/src/core/diagnostics/redundant-value.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/redundant-value.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/set-const.lua b/server-beta/src/core/diagnostics/set-const.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/set-const.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/trailing-space.lua b/server-beta/src/core/diagnostics/trailing-space.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/trailing-space.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/undefined-env-child.lua b/server-beta/src/core/diagnostics/undefined-env-child.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/undefined-env-child.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/undefined-global.lua b/server-beta/src/core/diagnostics/undefined-global.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/undefined-global.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/unused-function.lua b/server-beta/src/core/diagnostics/unused-function.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/unused-function.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/unused-label.lua b/server-beta/src/core/diagnostics/unused-label.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/unused-label.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/core/diagnostics/unused-local.lua b/server-beta/src/core/diagnostics/unused-local.lua
new file mode 100644
index 00000000..9b97ca88
--- /dev/null
+++ b/server-beta/src/core/diagnostics/unused-local.lua
@@ -0,0 +1,38 @@
+local files = require 'files'
+local guide = require 'parser.guide'
+local define = require 'proto.define'
+local lang = require 'language'
+
+local function hasGet(loc)
+ if not loc.ref then
+ return false
+ end
+ for _, ref in ipairs(loc.ref) do
+ if ref.type == 'get' then
+ return true
+ end
+ end
+ return false
+end
+
+return function (uri, callback)
+ local ast = files.getAst(uri)
+ if not ast then
+ return
+ end
+ guide.eachSourceType(ast.ast, 'local', function (source)
+ local name = source[1]
+ if name == '_'
+ or name == '_ENV' then
+ return
+ end
+ if not hasGet(source) then
+ callback {
+ start = source.start,
+ finish = source.finish,
+ tags = { define.DiagnosticTag.Unnecessary },
+ message = lang.script('DIAG_UNUSED_LOCAL', name),
+ }
+ end
+ end)
+end
diff --git a/server-beta/src/core/diagnostics/unused-vararg.lua b/server-beta/src/core/diagnostics/unused-vararg.lua
new file mode 100644
index 00000000..b3d19c21
--- /dev/null
+++ b/server-beta/src/core/diagnostics/unused-vararg.lua
@@ -0,0 +1,3 @@
+return function ()
+
+end
diff --git a/server-beta/src/language.lua b/server-beta/src/language.lua
index 3294c5b2..d1a4b4cf 100644
--- a/server-beta/src/language.lua
+++ b/server-beta/src/language.lua
@@ -1,5 +1,6 @@
-local fs = require 'bee.filesystem'
-local lni = require 'lni'
+local fs = require 'bee.filesystem'
+local lni = require 'lni'
+local util = require 'utility'
local function supportLanguage()
local list = {}
@@ -33,7 +34,7 @@ end
local function loadFileByLanguage(name, language)
local path = ROOT / 'locale' / language / (name .. '.lni')
- local buf = io.load(path)
+ local buf = util.loadFile(path:string())
if not buf then
return {}
end
diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua
index 0ca83489..773fb6d5 100644
--- a/server-beta/src/parser/guide.lua
+++ b/server-beta/src/parser/guide.lua
@@ -307,6 +307,23 @@ function m.eachSourceContain(ast, offset, callback)
end
end
+--- 遍历所有指定类型的source
+function m.eachSourceType(ast, type, callback)
+ local cache = ast.typeCache
+ if not cache then
+ cache = {}
+ ast.typeCache = cache
+ m.eachSource(ast, function (source)
+ if source.type == type then
+ cache[#cache+1] = source
+ end
+ end)
+ end
+ for i = 1, #cache do
+ callback(cache[i])
+ end
+end
+
--- 遍历所有的source
function m.eachSource(ast, callback)
local list = { ast }
diff --git a/server-beta/src/proto/interface.lua b/server-beta/src/proto/define.lua
index 6af0c996..9401e9e5 100644
--- a/server-beta/src/proto/interface.lua
+++ b/server-beta/src/proto/define.lua
@@ -2,7 +2,6 @@ local guide = require 'parser.guide'
local m = {}
-
--- 获取 position 对应的光标位置
---@param lines table
---@param text string
@@ -69,4 +68,42 @@ function m.locationLink(uri, range, selection, origin)
}
end
+--- 诊断等级
+m.DiagnosticSeverity = {
+ Error = 1,
+ Warning = 2,
+ Information = 3,
+ Hint = 4,
+}
+
+--- 诊断类型与默认等级
+m.DiagnosticDefaultSeverity = {
+ ['unused-local'] = 'Hint',
+ ['unused-function'] = 'Hint',
+ ['undefined-global'] = 'Warning',
+ ['global-in-nil-env'] = 'Warning',
+ ['unused-label'] = 'Hint',
+ ['unused-vararg'] = 'Hint',
+ ['trailing-space'] = 'Hint',
+ ['redefined-local'] = 'Hint',
+ ['newline-call'] = 'Information',
+ ['newfield-call'] = 'Warning',
+ ['redundant-parameter'] = 'Hint',
+ ['ambiguity-1'] = 'Warning',
+ ['lowercase-global'] = 'Information',
+ ['undefined-env-child'] = 'Information',
+ ['duplicate-index'] = 'Warning',
+ ['duplicate-method'] = 'Warning',
+ ['empty-block'] = 'Hint',
+ ['redundant-value'] = 'Hint',
+ ['emmy-lua'] = 'Warning',
+ ['set-const'] = 'Error',
+}
+
+--- 诊断报告标签
+m.DiagnosticTag = {
+ Unnecessary = 1,
+ Deprecated = 2,
+}
+
return m
diff --git a/server-beta/src/proto/provider.lua b/server-beta/src/proto/provider.lua
index 9f3b5697..1f6f6813 100644
--- a/server-beta/src/proto/provider.lua
+++ b/server-beta/src/proto/provider.lua
@@ -3,7 +3,7 @@ local cap = require 'proto.capability'
local task = require 'task'
local files = require 'files'
local proto = require 'proto.proto'
-local interface = require 'proto.interface'
+local define = require 'proto.define'
local workspace = require 'workspace'
local config = require 'config'
@@ -154,7 +154,7 @@ proto.on('textDocument/definition', function (params)
end
local lines = files.getLines(uri)
local text = files.getText(uri)
- local offset = interface.offset(lines, text, params.position)
+ local offset = define.offset(lines, text, params.position)
local result = core(uri, offset)
if not result then
return nil
@@ -164,10 +164,10 @@ proto.on('textDocument/definition', function (params)
local targetUri = info.uri
local targetLines = files.getLines(targetUri)
local targetText = files.getText(targetUri)
- response[i] = interface.locationLink(targetUri
- , interface.range(targetLines, targetText, info.target.start - 1, info.target.finish)
- , interface.range(targetLines, targetText, info.target.start - 1, info.target.finish)
- , interface.range(lines, text, info.source.start - 1, info.source.finish)
+ response[i] = define.locationLink(targetUri
+ , define.range(targetLines, targetText, info.target.start - 1, info.target.finish)
+ , define.range(targetLines, targetText, info.target.start - 1, info.target.finish)
+ , define.range(lines, text, info.source.start - 1, info.source.finish)
)
end
return response
diff --git a/server-beta/test.lua b/server-beta/test.lua
index 32f9dc57..3ebfca70 100644
--- a/server-beta/test.lua
+++ b/server-beta/test.lua
@@ -37,10 +37,10 @@ local function main()
end
test 'definition'
- --test 'rename'
- --test 'highlight'
+ test 'diagnostics'
--test 'references'
- --test 'diagnostics'
+ --test 'highlight'
+ --test 'rename'
--test 'type_inference'
--test 'find_lib'
--test 'hover'
diff --git a/server-beta/test/diagnostics/init.lua b/server-beta/test/diagnostics/init.lua
index bd922825..b0438eb2 100644
--- a/server-beta/test/diagnostics/init.lua
+++ b/server-beta/test/diagnostics/init.lua
@@ -1,8 +1,7 @@
-local core = require 'core'
-local buildVM = require 'vm'
-local parser = require 'parser'
-local service = require 'service'
-local config = require 'config'
+local core = require 'core.diagnostics'
+local files = require 'files'
+local config = require 'config'
+local util = require 'utility'
rawset(_G, 'TEST', true)
@@ -48,14 +47,10 @@ local function founded(targets, results)
end
function TEST(script, ...)
+ files.removeAll()
local new_script, target = catch_target(script, ...)
- local lsp = service()
- local ast = parser:parse(new_script, 'lua', 'Lua 5.3')
- assert(ast)
- local lines = parser:lines(new_script)
- local vm = buildVM(ast, lsp, 'test')
- assert(vm)
- local datas = core.diagnostics(vm, lines, 'test')
+ files.setText('', new_script)
+ local datas = core('')
local results = {}
for i, data in ipairs(datas) do
results[i] = { data.start, data.finish }
@@ -63,7 +58,7 @@ function TEST(script, ...)
if results[1] then
if not founded(target, results) then
- error(('%s\n%s'):format(table.dump(target), table.dump(results)))
+ error(('%s\n%s'):format(util.dump(target), util.dump(results)))
end
else
assert(#target == 0)
@@ -402,108 +397,108 @@ local <!t!> = {}
t[1] = 1
]]
-TEST [[
----@class <!Class!>
----@class <!Class!>
-]]
-
-TEST [[
----@class A : <!B!>
-]]
-
-TEST [[
----@class <!A : B!>
----@class <!B : C!>
----@class <!C : D!>
----@class <!D : A!>
-]]
-
-TEST [[
----@class A : B
----@class B : C
----@class C : D
----@class D
-]]
-
-TEST [[
----@type <!A!>
-]]
-
-TEST [[
----@class A
----@type A|<!B!>|<!C!>
-]]
-
-TEST [[
----@class AAA
----@alias B AAA
-
----@type B
-]]
-
-TEST [[
----@alias B <!AAA!>
-]]
-
-TEST [[
----@class <!A!>
----@class B
----@alias <!A B!>
-]]
-
-TEST [[
----@param x <!Class!>
-]]
-
-TEST [[
----@class Class
----@param <!y!> Class
-local function f(x)
- return x
-end
-f()
-]]
-
-TEST [[
----@class Class
----@param <!y!> Class
-function F(x)
- return x
-end
-F()
-]]
-
-TEST [[
----@class Class
----@param <!x!> Class
----@param y Class
----@param <!x!> Class
-local function f(x, y)
- return x, y
-end
-f()
-]]
-
-TEST [[
----@field <!x Class!>
----@class Class
-]]
-
-TEST [[
----@class Class
----@field <!x!> Class
----@field <!x!> Class
-]]
-
-TEST [[
----@class Class : any
-]]
-
-TEST [[
----@type fun(a: integer)
-local f
-f()
-]]
+--TEST [[
+-----@class <!Class!>
+-----@class <!Class!>
+--]]
+--
+--TEST [[
+-----@class A : <!B!>
+--]]
+--
+--TEST [[
+-----@class <!A : B!>
+-----@class <!B : C!>
+-----@class <!C : D!>
+-----@class <!D : A!>
+--]]
+--
+--TEST [[
+-----@class A : B
+-----@class B : C
+-----@class C : D
+-----@class D
+--]]
+--
+--TEST [[
+-----@type <!A!>
+--]]
+--
+--TEST [[
+-----@class A
+-----@type A|<!B!>|<!C!>
+--]]
+--
+--TEST [[
+-----@class AAA
+-----@alias B AAA
+--
+-----@type B
+--]]
+--
+--TEST [[
+-----@alias B <!AAA!>
+--]]
+--
+--TEST [[
+-----@class <!A!>
+-----@class B
+-----@alias <!A B!>
+--]]
+--
+--TEST [[
+-----@param x <!Class!>
+--]]
+--
+--TEST [[
+-----@class Class
+-----@param <!y!> Class
+--local function f(x)
+-- return x
+--end
+--f()
+--]]
+--
+--TEST [[
+-----@class Class
+-----@param <!y!> Class
+--function F(x)
+-- return x
+--end
+--F()
+--]]
+--
+--TEST [[
+-----@class Class
+-----@param <!x!> Class
+-----@param y Class
+-----@param <!x!> Class
+--local function f(x, y)
+-- return x, y
+--end
+--f()
+--]]
+--
+--TEST [[
+-----@field <!x Class!>
+-----@class Class
+--]]
+--
+--TEST [[
+-----@class Class
+-----@field <!x!> Class
+-----@field <!x!> Class
+--]]
+--
+--TEST [[
+-----@class Class : any
+--]]
+--
+--TEST [[
+-----@type fun(a: integer)
+--local f
+--f()
+--]]
TEST [[
local x <const> = 1
@@ -531,13 +526,13 @@ local x
x = x or -1
]]
---TEST [[
---local t = {}
---function t:<!a!>()
---end
---function t:<!a!>()
---end
---]]
+TEST [[
+local t = {}
+function t:<!a!>()
+end
+function t:<!a!>()
+end
+]]
TEST [[
local t = {}