diff options
-rw-r--r-- | server-beta/src/core/highlight.lua | 102 | ||||
-rw-r--r-- | server-beta/src/core/reference.lua | 1 | ||||
-rw-r--r-- | server-beta/test.lua | 2 | ||||
-rw-r--r-- | server-beta/test/highlight/init.lua | 57 |
4 files changed, 133 insertions, 29 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 diff --git a/server-beta/src/core/reference.lua b/server-beta/src/core/reference.lua index 1e845630..9ffd8fe0 100644 --- a/server-beta/src/core/reference.lua +++ b/server-beta/src/core/reference.lua @@ -1,5 +1,4 @@ local guide = require 'parser.guide' -local workspace = require 'workspace' local files = require 'files' local searcher = require 'searcher' diff --git a/server-beta/test.lua b/server-beta/test.lua index ffe9402d..17d1f2bf 100644 --- a/server-beta/test.lua +++ b/server-beta/test.lua @@ -39,7 +39,7 @@ local function main() test 'references' test 'definition' test 'diagnostics' - --test 'highlight' + test 'highlight' --test 'rename' --test 'type_inference' --test 'find_lib' diff --git a/server-beta/test/highlight/init.lua b/server-beta/test/highlight/init.lua index 6b8ab355..ebde9204 100644 --- a/server-beta/test/highlight/init.lua +++ b/server-beta/test/highlight/init.lua @@ -1,6 +1,5 @@ -local core = require 'core' -local parser = require 'parser' -local buildVM = require 'vm' +local core = require 'core.highlight' +local files = require 'files' local function catch_target(script) local list = {} @@ -10,7 +9,10 @@ local function catch_target(script) if not start then break end - list[#list+1] = { start + 2, finish - 2 } + list[#list+1] = { + start = start + 2, + finish = finish - 2, + } cur = finish + 1 end return list @@ -32,53 +34,54 @@ local function founded(targets, results) return true end -function TEST(newName) - return function (script) - local target = catch_target(script) - local start = script:find('<?', 1, true) - local finish = script:find('?>', 1, true) - local pos = (start + finish) // 2 + 1 - local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:parse(new_script, 'lua', 'Lua 5.3') - assert(ast) - local vm = buildVM(ast) - assert(vm) +function TEST(script) + files.removeAll() + local target = catch_target(script) + local start = script:find('<?', 1, true) + local finish = script:find('?>', 1, true) + local pos = (start + finish) // 2 + 1 + local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') + files.setText('', new_script) - local positions = core.highlight(vm, pos) - if positions then - assert(founded(target, positions)) - else - assert(#target == 0) - end + local positions = core('', pos) + if positions then + assert(founded(target, positions)) + else + assert(#target == 0) end end -TEST 'b' [[ +TEST [[ local <?a?> = 1 ]] -TEST 'b' [[ +TEST [[ local <?a?> = 1 <!a!> = 2 <!a!> = <!a!> ]] -TEST 'b' [[ +TEST [[ t.<?a?> = 1 a = t.<!a!> ]] -TEST 'b' [[ +TEST [[ t[<!'a'!>] = 1 a = t.<?a?> ]] -TEST 'b' [[ +TEST [[ +t[<?'a'?>] = 1 +a = t.<!a!> +]] + +TEST [[ :: <?a?> :: goto <!a!> ]] -TEST 'b' [[ +TEST [[ local function f(<!a!>) return <?a?> end |