diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-16 01:12:07 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-16 01:12:07 +0800 |
commit | 333a10fd656b48a063092127c610348d861c2f74 (patch) | |
tree | c9fea7eea15285ce05150b6a5707430d28f42d88 /server-beta/src/core/highlight.lua | |
parent | cc7584f46c50eafbcd6a78c0daea611b6cb98800 (diff) | |
download | lua-language-server-333a10fd656b48a063092127c610348d861c2f74.zip |
core.highlight
Diffstat (limited to 'server-beta/src/core/highlight.lua')
-rw-r--r-- | server-beta/src/core/highlight.lua | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/server-beta/src/core/highlight.lua b/server-beta/src/core/highlight.lua new file mode 100644 index 00000000..1516187e --- /dev/null +++ b/server-beta/src/core/highlight.lua @@ -0,0 +1,102 @@ +local guide = require 'parser.guide' +local files = require 'files' +local searcher = require 'searcher' + +local function ofLocal(source, callback) + callback(source) + if source.ref then + for _, ref in ipairs(source.ref) do + callback(ref) + end + end +end + +local function ofField(source, uri, callback) + callback(source) + local parent = source.parent + if not parent or not parent.node then + return + end + local myKey = guide.getKeyName(source) + searcher.eachField(parent.node, function (info) + if info.key ~= myKey then + return + end + local destUri = guide.getRoot(info.source).uri + if destUri ~= uri then + return + end + callback(info.source) + end) +end + +local function ofIndex(source, uri, callback) + local parent = source.parent + if parent.type == 'setindex' + or parent.type == 'getindex' + or parent.type == 'tableindex' then + ofField(source, uri, callback) + end +end + +local function ofLabel(source, callback) + searcher.eachRef(source, function (info) + callback(info.source) + end) +end + +local function find(source, uri, callback) + if source.type == 'local' then + ofLocal(source, callback) + elseif source.type == 'getlocal' + or source.type == 'setlocal' then + ofLocal(source.node, callback) + elseif source.type == 'field' + or source.type == 'method' then + ofField(source, uri, callback) + elseif source.type == 'string' + or source.type == 'boolean' + or source.type == 'number' then + ofIndex(source, uri, callback) + elseif source.type == 'goto' + or source.type == 'label' then + ofLabel(source, callback) + end +end + +return function (uri, offset) + local ast = files.getAst(uri) + if not ast then + return nil + end + local results = {} + local mark = {} + guide.eachSourceContain(ast.ast, offset, function (source) + find(source, uri, function (target) + if target.type == 'getfield' + or target.type == 'setfield' + or target.type == 'tablefield' then + target = target.field + elseif target.type == 'getmethod' + or target.type == 'setmethod' then + target = target.method + elseif target.type == 'getindex' + or target.type == 'setindex' + or target.type == 'tableindex' then + target = target.index + end + if mark[target] then + return + end + mark[target] = true + results[#results+1] = { + start = target.start, + finish = target.finish, + } + end) + end) + if #results == 0 then + return nil + end + return results +end |