diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-04-30 14:33:41 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-04-30 14:33:41 +0800 |
commit | 6b106652fb3b8497f777d18dbb70ffc33de0667d (patch) | |
tree | 3432b3ca81444a7738a854aa2b2421ffa535157b | |
parent | d5f2c21d65f1cabe577ff8e6e259e546f3c969d8 (diff) | |
download | lua-language-server-6b106652fb3b8497f777d18dbb70ffc33de0667d.zip |
highlight region
-rw-r--r-- | script/core/highlight.lua | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/script/core/highlight.lua b/script/core/highlight.lua index e79bb797..12ec114f 100644 --- a/script/core/highlight.lua +++ b/script/core/highlight.lua @@ -3,6 +3,7 @@ local files = require 'files' local vm = require 'vm' local define = require 'proto.define' local findSource = require 'core.find-source' +local util = require 'utility' local function eachRef(source, callback) local results = guide.requestReference(source) @@ -138,6 +139,88 @@ local function findKeyWord(ast, text, offset, callback) end) end +local function isRegion(str) + if str:sub(1, #'region') == 'region' + or str:sub(1, #'#region') == '#region' then + return true + end + return false +end + +local function isEndRegion(str) + if str:sub(1, #'endregion') == 'endregion' + or str:sub(1, #'#endregion') == '#endregion' then + return true + end + return false +end + +local function checkRegion(ast, text, offset, callback) + local count + local start, finish + local selected + for i, comment in ipairs(ast.comms) do + if comment.type == 'comment.short' then + if comment.start <= offset + and comment.finish >= offset then + local ltext = comment.text:lower() + ltext = util.trim(ltext, 'left') + if isRegion(ltext) then + start = comment.start - 2 + count = 1 + selected = i + elseif isEndRegion(ltext) then + finish = comment.finish + count = 1 + selected = i + else + return + end + break + end + end + end + if not selected then + return + end + if start then + for i = selected + 1, #ast.comms do + local comment = ast.comms[i] + if comment.type == 'comment.short' then + local ltext = comment.text:lower() + ltext = util.trim(ltext, 'left') + if isRegion(ltext) then + count = count + 1 + elseif isEndRegion(ltext) then + count = count - 1 + if count == 0 then + callback(start, comment.finish) + return + end + end + end + end + end + if finish then + for i = selected - 1, 1, -1 do + local comment = ast.comms[i] + if comment.type == 'comment.short' then + local ltext = comment.text:lower() + ltext = util.trim(ltext, 'left') + if isEndRegion(ltext) then + count = count + 1 + elseif isRegion(ltext) then + count = count - 1 + if count == 0 then + callback(comment.start - 2, finish) + return + end + end + end + end + end +end + local accept = { ['label'] = true, ['goto'] = true, @@ -255,6 +338,14 @@ return function (uri, offset) } end) + checkRegion(ast, text, offset, function (start, finish) + results[#results+1] = { + start = start, + finish = finish, + kind = define.DocumentHighlightKind.Text + } + end) + if #results == 0 then return nil end |