summaryrefslogtreecommitdiff
path: root/server-beta/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-09-24 19:20:54 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-09-24 19:20:54 +0800
commitbaef351c6398b8723bdeb96dde1cf0ef2ff5de86 (patch)
treec35c3c28273d4bd96a708488af80ea26278de8e9 /server-beta/src
parent8b7e91c28278f492520d3787428d319c562d00ff (diff)
downloadlua-language-server-baef351c6398b8723bdeb96dde1cf0ef2ff5de86.zip
整理代码
Diffstat (limited to 'server-beta/src')
-rw-r--r--server-beta/src/brave/init.lua56
-rw-r--r--server-beta/src/brave/work.lua54
-rw-r--r--server-beta/src/files.lua2
-rw-r--r--server-beta/src/pub/init.lua21
-rw-r--r--server-beta/src/pub/report.lua21
-rw-r--r--server-beta/src/workspace/workspace.lua67
6 files changed, 116 insertions, 105 deletions
diff --git a/server-beta/src/brave/init.lua b/server-beta/src/brave/init.lua
index 0a2d2d25..24c2e412 100644
--- a/server-beta/src/brave/init.lua
+++ b/server-beta/src/brave/init.lua
@@ -1,58 +1,4 @@
local brave = require 'brave.brave'
-local jsonrpc = require 'jsonrpc'
-local parser = require 'parser'
-local fs = require 'bee.filesystem'
-local furi = require 'file-uri'
-local util = require 'utility'
-
-brave.on('loadProto', function ()
- while true do
- local proto = jsonrpc.decode(io.read, log.error)
- if proto then
- brave.push('proto', proto)
- end
- end
-end)
-
-brave.on('compile', function (text)
- local state, err = parser:compile(text, 'lua', 'Lua 5.4')
- if not state then
- log.error(err)
- return
- end
- local lines = parser:lines(text)
- return {
- root = state.root,
- errs = state.errs,
- lines = lines,
- }
-end)
-
-brave.on('listDirectory', function (uri)
- local path = fs.path(furi.decode(uri))
- local uris = {}
- local dirs = {}
- for child in path:list_directory() do
- local childUri = furi.encode(child:string())
- uris[#uris+1] = childUri
- if fs.is_directory(child) then
- dirs[childUri] = true
- end
- end
- return {
- uris = uris,
- dirs = dirs,
- }
-end)
-
-brave.on('loadFile', function (uri)
- local filename = furi.decode(uri)
- return util.loadFile(filename)
-end)
-
-brave.on('saveFile', function (params)
- local filename = furi.decode(params.uri)
- return util.saveFile(filename, params.text)
-end)
+require 'brave.work'
return brave
diff --git a/server-beta/src/brave/work.lua b/server-beta/src/brave/work.lua
new file mode 100644
index 00000000..edbb35f9
--- /dev/null
+++ b/server-beta/src/brave/work.lua
@@ -0,0 +1,54 @@
+local brave = require 'brave.brave'
+local jsonrpc = require 'jsonrpc'
+local parser = require 'parser'
+local fs = require 'bee.filesystem'
+local furi = require 'file-uri'
+local util = require 'utility'
+
+brave.on('loadProto', function ()
+ while true do
+ local proto = jsonrpc.decode(io.read, log.error)
+ if proto then
+ brave.push('proto', proto)
+ end
+ end
+end)
+
+brave.on('compile', function (text)
+ local state, err = parser:compile(text, 'lua', 'Lua 5.4')
+ if not state then
+ log.error(err)
+ return
+ end
+ local lines = parser:lines(text)
+ return {
+ root = state.root,
+ errs = state.errs,
+ lines = lines,
+ }
+end)
+
+brave.on('listDirectory', function (uri)
+ local path = fs.path(furi.decode(uri))
+ local uris = {}
+ for child in path:list_directory() do
+ local childUri = furi.encode(child:string())
+ uris[#uris+1] = childUri
+ end
+ return uris
+end)
+
+brave.on('isDirectory', function (uri)
+ local path = fs.path(furi.decode(uri))
+ return fs.is_directory(path)
+end)
+
+brave.on('loadFile', function (uri)
+ local filename = furi.decode(uri)
+ return util.loadFile(filename)
+end)
+
+brave.on('saveFile', function (params)
+ local filename = furi.decode(params.uri)
+ return util.saveFile(filename, params.text)
+end)
diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua
index 370b50eb..6b924aeb 100644
--- a/server-beta/src/files.lua
+++ b/server-beta/src/files.lua
@@ -141,7 +141,7 @@ end
--- 判断是否是Lua文件
function m.isLua(uri)
- local ext = uri:match '%.(.-)$'
+ local ext = uri:match '%.([^%.%/%\\]-)$'
if not ext then
return false
end
diff --git a/server-beta/src/pub/init.lua b/server-beta/src/pub/init.lua
index fbac27f5..61b43da7 100644
--- a/server-beta/src/pub/init.lua
+++ b/server-beta/src/pub/init.lua
@@ -1,23 +1,4 @@
local pub = require 'pub.pub'
-local task = require 'task'
-
-pub.on('log', function (params, brave)
- log.raw(brave.id, params.level, params.msg, params.src, params.line)
-end)
-
-pub.on('mem', function (count, brave)
- brave.memory = count
-end)
-
-pub.on('proto', function (params)
- local proto = require 'proto'
- task.create(function ()
- if params.method then
- proto.doMethod(params)
- else
- proto.doResponse(params)
- end
- end)
-end)
+require 'pub.report'
return pub
diff --git a/server-beta/src/pub/report.lua b/server-beta/src/pub/report.lua
new file mode 100644
index 00000000..d73a4e69
--- /dev/null
+++ b/server-beta/src/pub/report.lua
@@ -0,0 +1,21 @@
+local pub = require 'pub.pub'
+local task = require 'task'
+
+pub.on('log', function (params, brave)
+ log.raw(brave.id, params.level, params.msg, params.src, params.line)
+end)
+
+pub.on('mem', function (count, brave)
+ brave.memory = count
+end)
+
+pub.on('proto', function (params)
+ local proto = require 'proto'
+ task.create(function ()
+ if params.method then
+ proto.doMethod(params)
+ else
+ proto.doResponse(params)
+ end
+ end)
+end)
diff --git a/server-beta/src/workspace/workspace.lua b/server-beta/src/workspace/workspace.lua
index b17f2345..f320cef6 100644
--- a/server-beta/src/workspace/workspace.lua
+++ b/server-beta/src/workspace/workspace.lua
@@ -15,12 +15,16 @@ m.ignoreMatcher = nil
function m.init(name, uri)
m.name = name
m.uri = uri
+ m.path = furi.decode(uri)
end
--- 创建排除文件匹配器
-function m.buildIgnoreMatcher()
- local pattern = {}
+function m.getIgnoreMatcher()
+ if m.ignoreVersion == config.version then
+ return m.ignoreMatcher
+ end
+ local pattern = {}
-- config.workspace.ignoreDir
for path in pairs(config.config.workspace.ignoreDir) do
log.info('Ignore directory:', path)
@@ -35,7 +39,7 @@ function m.buildIgnoreMatcher()
end
-- config.workspace.ignoreSubmodules
if config.config.workspace.ignoreSubmodules then
- local buf = pub.task('loadFile', furi.encode((ROOT / '.gitmodules'):string()))
+ local buf = pub.task('loadFile', furi.encode(m.path .. '/.gitmodules'))
if buf then
for path in buf:gmatch('path = ([^\r\n]+)') do
log.info('Ignore by .gitmodules:', path)
@@ -45,7 +49,7 @@ function m.buildIgnoreMatcher()
end
-- config.workspace.useGitIgnore
if config.config.workspace.useGitIgnore then
- local buf = pub.task('loadFile', furi.encode((ROOT / '.gitignore'):string()))
+ local buf = pub.task('loadFile', furi.encode(m.path .. '/.gitignore'))
if buf then
for line in buf:gmatch '[^\r\n]+' do
log.info('Ignore by .gitignore:', line)
@@ -59,24 +63,21 @@ function m.buildIgnoreMatcher()
pattern[#pattern+1] = path
end
- local matcher = glob.gitignore(pattern)
+ m.ignoreMatcher = glob.gitignore(pattern)
if platform.OS == "Windows" then
- matcher:setOption 'ignoreCase'
+ m.ignoreMatcher:setOption 'ignoreCase'
end
- return matcher
+ m.ignoreVersion = config.version
+ return m.ignoreMatcher
end
--- 文件是否被忽略
function m.isIgnored(uri)
local path = furi.decode(uri)
- if m.ignoreVersion == config.version then
- return m.ignoreMatcher(path)
- end
- m.ignoreMatcher = m.buildIgnoreMatcher()
- m.ignoreVersion = config.version
- return m.ignoreMatcher(path)
+ local ignore = m.getIgnoreMatcher()
+ return ignore(path)
end
--- 预读工作区内所有文件(异步)
@@ -85,28 +86,36 @@ function m.preload()
return
end
log.info('Preload start.')
- local function scan(dir, callback)
- local result = pub.task('listDirectory', dir)
- if not result then
- return
+ local ignore = m.getIgnoreMatcher()
+
+ ignore:setInterface('type', function (path)
+ if pub.task('isDirectory', furi.encode(m.path .. '/' .. path)) then
+ return 'directory'
+ else
+ return 'file'
end
- for i = 1, #result.uris do
- local childUri = result.uris[i]
- if not m.isIgnored(childUri) then
- if result.dirs[childUri] then
- scan(childUri, callback)
- elseif files.isLua(childUri) then
- callback(childUri)
- end
- end
+ end)
+
+ ignore:setInterface('list', function (path)
+ local uris = pub.task('listDirectory', furi.encode(m.path .. '/' .. path))
+ local paths = {}
+ for i, uri in ipairs(uris) do
+ paths[i] = furi.decode(uri)
+ end
+ return paths
+ end)
+
+ ignore:scan(function (path)
+ local uri = furi.encode(m.path .. '/' .. path)
+ if not files.isLua(uri) then
+ return
end
- end
- scan(m.uri, function (uri)
pub.syncTask('loadFile', uri, function (text)
- log.debug('Preload file at: ' .. uri, #text)
+ log.info(('Preload file at: %s , size = %.3f KB'):format(uri, #text / 1000.0))
files.setText(uri, text)
end)
end)
+
log.info('Preload finish.')
end