summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-07-20 19:52:49 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-07-20 19:52:49 +0800
commit59d6e659219b7e63ea8ae770bb59f6278678b5eb (patch)
tree6c3a1146942695a5b3c9b3fdfa7447aa0804624d
parentfde2dc4cdb275aeb550c7d0af96875d4c7c2affb (diff)
downloadlua-language-server-59d6e659219b7e63ea8ae770bb59f6278678b5eb.zip
支持全局符号
-rw-r--r--README.md2
-rw-r--r--script/method/init.lua1
-rw-r--r--script/method/initialize.lua3
-rw-r--r--script/method/workspace/symbol.lua84
-rw-r--r--script/service.lua4
5 files changed, 93 insertions, 1 deletions
diff --git a/README.md b/README.md
index 65688592..c41505b2 100644
--- a/README.md
+++ b/README.md
@@ -13,11 +13,13 @@
- [x] IntelliSense
- [x] Signature Help
- [x] Document Symbols
+- [x] Workspace Symbols
- [x] Syntax Check
- [x] Highlight
- [x] Code Action
- [x] EmmyLua Annotation
- [x] Multi Workspace
+- [x] Semantic Tokens
- [ ] Type Format
## Preview
diff --git a/script/method/init.lua b/script/method/init.lua
index cd9010bb..742590f6 100644
--- a/script/method/init.lua
+++ b/script/method/init.lua
@@ -30,5 +30,6 @@ init 'workspace/didChangeConfiguration'
init 'workspace/didChangeWatchedFiles'
init 'workspace/didChangeWorkspaceFolders'
init 'workspace/executeCommand'
+init 'workspace/symbol'
return method
diff --git a/script/method/initialize.lua b/script/method/initialize.lua
index 0f986dc1..5038a27f 100644
--- a/script/method/initialize.lua
+++ b/script/method/initialize.lua
@@ -38,6 +38,7 @@ return function (lsp, params)
documentHighlightProvider = true,
codeActionProvider = true,
foldingRangeProvider = true,
+ workspaceSymbolProvider = true,
signatureHelpProvider = {
triggerCharacters = { '(', ',' },
},
@@ -52,7 +53,7 @@ return function (lsp, params)
workspaceFolders = {
supported = true,
changeNotifications = true,
- }
+ },
},
documentOnTypeFormattingProvider = {
firstTriggerCharacter = '}',
diff --git a/script/method/workspace/symbol.lua b/script/method/workspace/symbol.lua
new file mode 100644
index 00000000..6ec2d025
--- /dev/null
+++ b/script/method/workspace/symbol.lua
@@ -0,0 +1,84 @@
+local matchKey = require 'core.matchKey'
+
+local SymbolKind = {
+ File = 1,
+ Module = 2,
+ Namespace = 3,
+ Package = 4,
+ Class = 5,
+ Method = 6,
+ Property = 7,
+ Field = 8,
+ Constructor = 9,
+ Enum = 10,
+ Interface = 11,
+ Function = 12,
+ Variable = 13,
+ Constant = 14,
+ String = 15,
+ Number = 16,
+ Boolean = 17,
+ Array = 18,
+ Object = 19,
+ Key = 20,
+ Null = 21,
+ EnumMember = 22,
+ Struct = 23,
+ Event = 24,
+ Operator = 25,
+ TypeParameter = 26,
+}
+
+local function convertRange(lines, range)
+ local start_row, start_col = lines:rowcol(range.start)
+ local finish_row, finish_col = lines:rowcol(range.finish)
+ local result = {
+ start = {
+ line = start_row - 1,
+ character = start_col - 1,
+ },
+ ['end'] = {
+ line = finish_row - 1,
+ -- 这里不用-1,因为前端期待的是匹配完成后的位置
+ character = finish_col,
+ },
+ }
+ return result
+end
+
+local function searchVM(lsp, results, query, uri)
+ local vm, lines = lsp:getVM(uri)
+ if not vm then
+ return
+ end
+ vm:eachSource(function (src)
+ if src.type == 'name' then
+ if matchKey(query, src[1]) then
+ results[#results+1] = {
+ name = src[1],
+ kind = SymbolKind.Variable,
+ location = {
+ uri = uri,
+ range = convertRange(lines, src),
+ }
+ }
+ end
+ end
+ end)
+end
+
+--- @param lsp LSP
+--- @param params table
+return function (lsp, params)
+ local query = params.query
+ if #query <= 0 then
+ return {}
+ end
+ local results = {}
+
+ for uri in lsp:eachFile() do
+ searchVM(lsp, results, query, uri)
+ end
+
+ return results
+end
diff --git a/script/service.lua b/script/service.lua
index ef9d3d96..d0e34891 100644
--- a/script/service.lua
+++ b/script/service.lua
@@ -285,6 +285,10 @@ function mt:eachOpened()
return self._files:eachOpened()
end
+function mt:eachFile()
+ return self._files:eachFile()
+end
+
---@param uri uri
---@param path path
---@param text string