aboutsummaryrefslogtreecommitdiff
path: root/plugins/selectionhighlight.lua
blob: 132e6aa38af9ae79b4ff727a4d721d7e8a00a765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- mod-version:1 -- lite-xl 1.16
local style = require "core.style"
local DocView = require "core.docview"
local config = require "core.config"

-- originally written by luveti

-- Set to true to also highlight empty spaces
config.highlight_spaces = false

local function draw_box(x, y, w, h, color)
  local r = renderer.draw_rect
  local s = math.ceil(SCALE)
  r(x, y, w, s, color)
  r(x, y + h - s, w, s, color)
  r(x, y + s, s, h - s * 2, color)
  r(x + w - s, y + s, s, h - s * 2, color)
end


local draw_line_body = DocView.draw_line_body

function DocView:draw_line_body(idx, x, y)
  local line1, col1, line2, col2 = self.doc:get_selection(true)
  local selection = self.doc:get_text(line1, col1, line2, col2)
  if
    line1 == line2 and col1 ~= col2
    and
    (
      config.highlight_spaces
      or
      not selection:match("^%s+$")
    )
  then
    local lh = self:get_line_height()
    local selected_text = self.doc.lines[line1]:sub(col1, col2 - 1)
    local current_line_text = self.doc.lines[idx]
    local last_col = 1
    while true do
      local start_col, end_col = current_line_text:find(
        selected_text, last_col, true
      )
      if start_col == nil then break end
      local x1 = x + self:get_col_x_offset(idx, start_col)
      local x2 = x + self:get_col_x_offset(idx, end_col + 1)
      local color = style.selectionhighlight or style.syntax.comment
      draw_box(x1, y, x2 - x1, lh, color)
      last_col = end_col + 1
    end
  end
  draw_line_body(self, idx, x, y)
end