summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/src/matcher/compile.lua2
-rw-r--r--server/src/matcher/diagnostics.lua82
-rw-r--r--server/src/method/init.lua1
-rw-r--r--server/src/method/initialize.lua2
-rw-r--r--server/src/method/initialized.lua2
-rw-r--r--server/src/method/shutdown.lua2
-rw-r--r--server/src/method/textDocument/definition.lua1
-rw-r--r--server/src/method/textDocument/hover.lua1
-rw-r--r--server/src/method/textDocument/implementation.lua1
-rw-r--r--server/src/method/textDocument/publishDiagnostics.lua74
-rw-r--r--server/src/method/textDocument/references.lua1
-rw-r--r--server/src/method/textDocument/rename.lua1
-rw-r--r--server/src/service.lua12
13 files changed, 124 insertions, 58 deletions
diff --git a/server/src/matcher/compile.lua b/server/src/matcher/compile.lua
index 68fdf9b6..02db109c 100644
--- a/server/src/matcher/compile.lua
+++ b/server/src/matcher/compile.lua
@@ -227,7 +227,7 @@ function mt:searchReturn(action)
end
end
-function mt:setTable(var, tbl, mode)
+function mt:setTable(var, tbl)
if not var or not tbl then
return
end
diff --git a/server/src/matcher/diagnostics.lua b/server/src/matcher/diagnostics.lua
index 9cd4f6ee..ffdab80e 100644
--- a/server/src/matcher/diagnostics.lua
+++ b/server/src/matcher/diagnostics.lua
@@ -1,49 +1,45 @@
-local DiagnosticSeverity = {
- Error = 1,
- Warning = 2,
- Information = 3,
- Hint = 4,
-}
-
--[[
-/**
- * Represents a related message and source code location for a diagnostic. This should be
- * used to point to code locations that cause or related to a diagnostics, e.g when duplicating
- * a symbol in a scope.
- */
-export interface DiagnosticRelatedInformation {
- /**
- * The location of this related diagnostic information.
- */
- location: Location;
-
- /**
- * The message of this related diagnostic information.
- */
- message: string;
+data = {
+ start = 1,
+ finish = 1,
+ level = 'Error' or 'Warning' or 'Information' or 'Hint',
+ code = '',
+ message = '',
}
]]--
-return function (ast, results)
- local diagnostics = {}
-
- diagnostics[1] = {
- range = {
- start = {
- line = 0,
- character = 0,
- },
- ['end'] = {
- line = 0,
- character = 10,
- },
- },
- severity = DiagnosticSeverity.Warning,
- code = 'I am code',
- source = 'I am source',
- message = 'I am message',
- relatedInformation = nil,
- }
+local function searchUnusedLocals(results, callback)
+ for _, var in ipairs(results.vars) do
+ if var.type ~= 'local' then
+ goto NEXT_VAR
+ end
+ if var.key == 'self'
+ or var.key == '_'
+ or var.key == '_ENV'
+ then
+ goto NEXT_VAR
+ end
+ for _, info in ipairs(var) do
+ if info.type == 'get' then
+ goto NEXT_VAR
+ end
+ end
+ callback(var.source.start, var.source.finish, var.key)
+ ::NEXT_VAR::
+ end
+end
- return diagnostics
+return function (ast, results, lines)
+ local datas = {}
+ -- 搜索未使用的局部变量
+ searchUnusedLocals(results, function (start, finish, code)
+ datas[#datas+1] = {
+ start = start,
+ finish = finish,
+ level = 'Warning',
+ code = code,
+ message = 'Unused local', -- LOCALE
+ }
+ end)
+ return datas
end
diff --git a/server/src/method/init.lua b/server/src/method/init.lua
index c01152a7..80619cbe 100644
--- a/server/src/method/init.lua
+++ b/server/src/method/init.lua
@@ -14,6 +14,7 @@ init 'textDocument/didChange'
init 'textDocument/didClose'
init 'textDocument/hover'
init 'textDocument/implementation'
+init 'textDocument/publishDiagnostics'
init 'textDocument/rename'
init 'textDocument/references'
diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua
index 161959d4..113acfb8 100644
--- a/server/src/method/initialize.lua
+++ b/server/src/method/initialize.lua
@@ -1,4 +1,4 @@
-return function (lsp, data)
+return function (lsp)
lsp._inited = true
return {
capabilities = {
diff --git a/server/src/method/initialized.lua b/server/src/method/initialized.lua
index 0451dc50..716a86e7 100644
--- a/server/src/method/initialized.lua
+++ b/server/src/method/initialized.lua
@@ -1,3 +1,3 @@
-return function (lsp)
+return function ()
return true
end
diff --git a/server/src/method/shutdown.lua b/server/src/method/shutdown.lua
index 0451dc50..716a86e7 100644
--- a/server/src/method/shutdown.lua
+++ b/server/src/method/shutdown.lua
@@ -1,3 +1,3 @@
-return function (lsp)
+return function ()
return true
end
diff --git a/server/src/method/textDocument/definition.lua b/server/src/method/textDocument/definition.lua
index a0a25f46..4d3d861e 100644
--- a/server/src/method/textDocument/definition.lua
+++ b/server/src/method/textDocument/definition.lua
@@ -1,4 +1,3 @@
-local parser = require 'parser'
local matcher = require 'matcher'
return function (lsp, params)
diff --git a/server/src/method/textDocument/hover.lua b/server/src/method/textDocument/hover.lua
index 2fd7aaec..5cb581a3 100644
--- a/server/src/method/textDocument/hover.lua
+++ b/server/src/method/textDocument/hover.lua
@@ -1,4 +1,3 @@
-local parser = require 'parser'
local matcher = require 'matcher'
return function (lsp, params)
diff --git a/server/src/method/textDocument/implementation.lua b/server/src/method/textDocument/implementation.lua
index c0a37830..aef9f1a0 100644
--- a/server/src/method/textDocument/implementation.lua
+++ b/server/src/method/textDocument/implementation.lua
@@ -1,4 +1,3 @@
-local parser = require 'parser'
local matcher = require 'matcher'
return function (lsp, params)
diff --git a/server/src/method/textDocument/publishDiagnostics.lua b/server/src/method/textDocument/publishDiagnostics.lua
new file mode 100644
index 00000000..5f3d1d28
--- /dev/null
+++ b/server/src/method/textDocument/publishDiagnostics.lua
@@ -0,0 +1,74 @@
+local matcher = require 'matcher'
+
+local DiagnosticSeverity = {
+ Error = 1,
+ Warning = 2,
+ Information = 3,
+ Hint = 4,
+}
+
+--[[
+/**
+ * Represents a related message and source code location for a diagnostic. This should be
+ * used to point to code locations that cause or related to a diagnostics, e.g when duplicating
+ * a symbol in a scope.
+ */
+export interface DiagnosticRelatedInformation {
+ /**
+ * The location of this related diagnostic information.
+ */
+ location: Location;
+
+ /**
+ * The message of this related diagnostic information.
+ */
+ message: string;
+}
+]]--
+
+local function getRange(start, finish, lines)
+ local start_row, start_col = lines:rowcol(start, 'utf8')
+ local finish_row, finish_col = lines:rowcol(finish, 'utf8')
+ return {
+ start = {
+ line = start_row - 1,
+ character = start_col - 1,
+ },
+ ['end'] = {
+ line = finish_row - 1,
+ -- 这里不用-1,因为前端期待的是匹配完成后的位置
+ character = finish_col,
+ },
+ }
+end
+
+local function createInfo(data, lines)
+ local diagnostic = {
+ source = 'Lsp',
+ range = getRange(data.start, data.finish, lines),
+ severity = DiagnosticSeverity[data.level],
+ code = data.code,
+ message = data.message,
+ }
+ return diagnostic
+end
+
+return function (lsp, params)
+ local results = params.results
+ local ast = params.ast
+ local lines = params.lines
+
+ local datas = matcher.diagnostics(ast, results, lines)
+
+ if not datas then
+ -- 返回空表以清空之前的结果
+ return {}
+ end
+
+ local diagnostics = {}
+ for i, data in ipairs(datas) do
+ diagnostics[i] = createInfo(data, lines)
+ end
+
+ return diagnostics
+end
diff --git a/server/src/method/textDocument/references.lua b/server/src/method/textDocument/references.lua
index b816e732..4b17700e 100644
--- a/server/src/method/textDocument/references.lua
+++ b/server/src/method/textDocument/references.lua
@@ -1,4 +1,3 @@
-local parser = require 'parser'
local matcher = require 'matcher'
return function (lsp, params)
diff --git a/server/src/method/textDocument/rename.lua b/server/src/method/textDocument/rename.lua
index f8d46409..bfb5cbba 100644
--- a/server/src/method/textDocument/rename.lua
+++ b/server/src/method/textDocument/rename.lua
@@ -1,4 +1,3 @@
-local parser = require 'parser'
local matcher = require 'matcher'
return function (lsp, params)
diff --git a/server/src/service.lua b/server/src/service.lua
index f2007bda..ab33b8e2 100644
--- a/server/src/service.lua
+++ b/server/src/service.lua
@@ -1,6 +1,5 @@
local subprocess = require 'bee.subprocess'
local method = require 'method'
-local fs = require 'bee.filesystem'
local thread = require 'thread'
local json = require 'json'
local parser = require 'parser'
@@ -96,17 +95,17 @@ function mt:_doDiagnostic()
self._needDiagnostics[uri] = nil
end
for uri, data in pairs(copy) do
- local ast = data.ast
- local results = data.results
- local suc, res = xpcall(matcher.diagnostics, log.error, ast, results)
- if suc then
+ local method = 'textDocument/publishDiagnostics'
+ local res = self:_callMethod(method, data)
+ if res then
self:_send {
- method = 'textDocument/publishDiagnostics',
+ method = method,
params = {
uri = uri,
diagnostics = res,
},
}
+ log.debug('publishDiagnostics', uri)
end
end
local passed = os.clock() - clock
@@ -210,6 +209,7 @@ function mt:compileText(uri)
self._needDiagnostics[uri] = {
ast = ast,
results = obj.results,
+ lines = obj.lines,
}
return obj