2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local common = require "core.common"
|
|
|
|
local config = require "core.config"
|
|
|
|
local style = require "core.style"
|
2020-05-13 17:12:20 +02:00
|
|
|
local keymap = require "core.keymap"
|
2019-12-28 12:16:32 +01:00
|
|
|
local translate = require "core.doc.translate"
|
2022-10-16 01:58:51 +02:00
|
|
|
local ime = require "core.ime"
|
2019-12-28 12:16:32 +01:00
|
|
|
local View = require "core.view"
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@class core.docview : core.view
|
|
|
|
---@field super core.view
|
2019-12-28 12:16:32 +01:00
|
|
|
local DocView = View:extend()
|
|
|
|
|
2021-08-27 23:55:17 +02:00
|
|
|
DocView.context = "session"
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local function move_to_line_offset(dv, line, col, offset)
|
|
|
|
local xo = dv.last_x_offset
|
|
|
|
if xo.line ~= line or xo.col ~= col then
|
|
|
|
xo.offset = dv:get_col_x_offset(line, col)
|
|
|
|
end
|
|
|
|
xo.line = line + offset
|
|
|
|
xo.col = dv:get_x_offset_col(line + offset, xo.offset)
|
|
|
|
return xo.line, xo.col
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
DocView.translate = {
|
|
|
|
["previous_page"] = function(doc, line, col, dv)
|
|
|
|
local min, max = dv:get_visible_line_range()
|
|
|
|
return line - (max - min), 1
|
|
|
|
end,
|
|
|
|
|
|
|
|
["next_page"] = function(doc, line, col, dv)
|
2022-05-30 16:40:46 +02:00
|
|
|
if line == #doc.lines then
|
|
|
|
return #doc.lines, #doc.lines[line]
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
local min, max = dv:get_visible_line_range()
|
|
|
|
return line + (max - min), 1
|
|
|
|
end,
|
|
|
|
|
|
|
|
["previous_line"] = function(doc, line, col, dv)
|
|
|
|
if line == 1 then
|
|
|
|
return 1, 1
|
|
|
|
end
|
|
|
|
return move_to_line_offset(dv, line, col, -1)
|
|
|
|
end,
|
|
|
|
|
|
|
|
["next_line"] = function(doc, line, col, dv)
|
|
|
|
if line == #doc.lines then
|
|
|
|
return #doc.lines, math.huge
|
|
|
|
end
|
|
|
|
return move_to_line_offset(dv, line, col, 1)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:new(doc)
|
|
|
|
DocView.super.new(self)
|
|
|
|
self.cursor = "ibeam"
|
|
|
|
self.scrollable = true
|
|
|
|
self.doc = assert(doc)
|
|
|
|
self.font = "code_font"
|
|
|
|
self.last_x_offset = {}
|
2022-10-16 01:58:51 +02:00
|
|
|
self.ime_selection = { from = 0, size = 0 }
|
2022-12-21 01:11:13 +01:00
|
|
|
self.ime_status = false
|
2022-11-02 00:34:23 +01:00
|
|
|
self.hovering_gutter = false
|
2022-11-01 23:38:50 +01:00
|
|
|
self.v_scrollbar:set_forced_status(config.force_scrollbar_status)
|
|
|
|
self.h_scrollbar:set_forced_status(config.force_scrollbar_status)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:try_close(do_close)
|
|
|
|
if self.doc:is_dirty()
|
|
|
|
and #core.get_views_referencing_doc(self.doc) == 1 then
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Unsaved Changes; Confirm Close", {
|
|
|
|
submit = function(_, item)
|
|
|
|
if item.text:match("^[cC]") then
|
|
|
|
do_close()
|
|
|
|
elseif item.text:match("^[sS]") then
|
|
|
|
self.doc:save()
|
|
|
|
do_close()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
suggest = function(text)
|
|
|
|
local items = {}
|
|
|
|
if not text:find("^[^cC]") then table.insert(items, "Close Without Saving") end
|
|
|
|
if not text:find("^[^sS]") then table.insert(items, "Save And Close") end
|
|
|
|
return items
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
else
|
|
|
|
do_close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:get_name()
|
|
|
|
local post = self.doc:is_dirty() and "*" or ""
|
|
|
|
local name = self.doc:get_name()
|
|
|
|
return name:match("[^/%\\]*$") .. post
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-18 00:26:19 +01:00
|
|
|
function DocView:get_filename()
|
2021-02-28 18:54:32 +01:00
|
|
|
if self.doc.abs_filename then
|
|
|
|
local post = self.doc:is_dirty() and "*" or ""
|
2021-05-05 10:35:19 +02:00
|
|
|
return common.home_encode(self.doc.abs_filename) .. post
|
2021-02-28 18:54:32 +01:00
|
|
|
end
|
|
|
|
return self:get_name()
|
2021-02-18 00:26:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function DocView:get_scrollable_size()
|
2021-09-30 22:10:38 +02:00
|
|
|
if not config.scroll_past_end then
|
|
|
|
return self:get_line_height() * (#self.doc.lines) + style.padding.y * 2
|
|
|
|
end
|
2021-08-30 00:50:46 +02:00
|
|
|
return self:get_line_height() * (#self.doc.lines - 1) + self.size.y
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-10-16 02:12:15 +02:00
|
|
|
function DocView:get_h_scrollable_size()
|
|
|
|
return math.huge
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
function DocView:get_font()
|
|
|
|
return style[self.font]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:get_line_height()
|
|
|
|
return math.floor(self:get_font():get_height() * config.line_height)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:get_gutter_width()
|
2021-07-28 20:12:46 +02:00
|
|
|
local padding = style.padding.x * 2
|
|
|
|
return self:get_font():get_width(#self.doc.lines) + padding, padding
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-03-24 17:01:46 +01:00
|
|
|
function DocView:get_line_screen_position(line, col)
|
2019-12-28 12:16:32 +01:00
|
|
|
local x, y = self:get_content_offset()
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
local gw = self:get_gutter_width()
|
2022-03-24 17:01:46 +01:00
|
|
|
y = y + (line-1) * lh + style.padding.y
|
|
|
|
if col then
|
|
|
|
return x + gw + self:get_col_x_offset(line, col), y
|
|
|
|
else
|
|
|
|
return x + gw, y
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocView:get_line_text_y_offset()
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
local th = self:get_font():get_height()
|
|
|
|
return (lh - th) / 2
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:get_visible_line_range()
|
|
|
|
local x, y, x2, y2 = self:get_content_bounds()
|
|
|
|
local lh = self:get_line_height()
|
2023-02-06 19:03:29 +01:00
|
|
|
local minline = math.max(1, math.floor((y - style.padding.y) / lh) + 1)
|
|
|
|
local maxline = math.min(#self.doc.lines, math.floor((y2 - style.padding.y) / lh) + 1)
|
2019-12-28 12:16:32 +01:00
|
|
|
return minline, maxline
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:get_col_x_offset(line, col)
|
2021-05-26 19:16:56 +02:00
|
|
|
local default_font = self:get_font()
|
2023-02-09 18:26:32 +01:00
|
|
|
local _, indent_size = self.doc:get_indent_info()
|
|
|
|
default_font:set_tab_size(indent_size)
|
2021-05-26 19:16:56 +02:00
|
|
|
local column = 1
|
|
|
|
local xoffset = 0
|
|
|
|
for _, type, text in self.doc.highlighter:each_token(line) do
|
|
|
|
local font = style.syntax_fonts[type] or default_font
|
2023-02-09 18:26:32 +01:00
|
|
|
if font ~= default_font then font:set_tab_size(indent_size) end
|
2023-03-28 01:17:11 +02:00
|
|
|
local length = #text
|
|
|
|
if column + length <= col then
|
|
|
|
xoffset = xoffset + font:get_width(text)
|
|
|
|
column = column + length
|
|
|
|
if column >= col then
|
2021-09-11 04:22:30 +02:00
|
|
|
return xoffset
|
2021-05-26 19:16:56 +02:00
|
|
|
end
|
2023-03-28 01:17:11 +02:00
|
|
|
else
|
|
|
|
for char in common.utf8_chars(text) do
|
|
|
|
if column >= col then
|
|
|
|
return xoffset
|
|
|
|
end
|
|
|
|
xoffset = xoffset + font:get_width(char)
|
|
|
|
column = column + #char
|
|
|
|
end
|
2021-05-26 19:16:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-11 04:22:30 +02:00
|
|
|
return xoffset
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:get_x_offset_col(line, x)
|
2021-05-26 19:16:56 +02:00
|
|
|
local line_text = self.doc.lines[line]
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local xoffset, last_i, i = 0, 1, 1
|
2021-05-26 19:16:56 +02:00
|
|
|
local default_font = self:get_font()
|
2023-02-09 18:26:32 +01:00
|
|
|
local _, indent_size = self.doc:get_indent_info()
|
|
|
|
default_font:set_tab_size(indent_size)
|
2021-05-26 19:16:56 +02:00
|
|
|
for _, type, text in self.doc.highlighter:each_token(line) do
|
|
|
|
local font = style.syntax_fonts[type] or default_font
|
2023-02-09 18:26:32 +01:00
|
|
|
if font ~= default_font then font:set_tab_size(indent_size) end
|
2023-03-28 01:17:11 +02:00
|
|
|
local width = font:get_width(text)
|
|
|
|
-- Don't take the shortcut if the width matches x,
|
|
|
|
-- because we need last_i which should be calculated using utf-8.
|
|
|
|
if xoffset + width < x then
|
|
|
|
xoffset = xoffset + width
|
|
|
|
i = i + #text
|
|
|
|
else
|
|
|
|
for char in common.utf8_chars(text) do
|
|
|
|
local w = font:get_width(char)
|
|
|
|
if xoffset >= x then
|
|
|
|
return (xoffset - x > w / 2) and last_i or i
|
|
|
|
end
|
|
|
|
xoffset = xoffset + w
|
|
|
|
last_i = i
|
|
|
|
i = i + #char
|
2021-05-26 19:16:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-26 19:16:56 +02:00
|
|
|
return #line_text
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:resolve_screen_position(x, y)
|
|
|
|
local ox, oy = self:get_line_screen_position(1)
|
|
|
|
local line = math.floor((y - oy) / self:get_line_height()) + 1
|
|
|
|
line = common.clamp(line, 1, #self.doc.lines)
|
|
|
|
local col = self:get_x_offset_col(line, x - ox)
|
|
|
|
return line, col
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:scroll_to_line(line, ignore_if_visible, instant)
|
|
|
|
local min, max = self:get_visible_line_range()
|
|
|
|
if not (ignore_if_visible and line > min and line < max) then
|
2022-03-24 17:01:46 +01:00
|
|
|
local x, y = self:get_line_screen_position(line)
|
|
|
|
local ox, oy = self:get_content_offset()
|
|
|
|
self.scroll.to.y = math.max(0, y - oy - self.size.y / 2)
|
2019-12-28 12:16:32 +01:00
|
|
|
if instant then
|
|
|
|
self.scroll.y = self.scroll.to.y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2023-04-07 18:42:46 +02:00
|
|
|
function DocView:supports_text_input()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function DocView:scroll_to_make_visible(line, col)
|
2022-03-24 17:01:46 +01:00
|
|
|
local ox, oy = self:get_content_offset()
|
|
|
|
local _, ly = self:get_line_screen_position(line, col)
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
self.scroll.to.y = common.clamp(self.scroll.to.y, ly - oy - self.size.y + lh * 2, ly - oy - lh)
|
2019-12-28 12:16:32 +01:00
|
|
|
local gw = self:get_gutter_width()
|
|
|
|
local xoffset = self:get_col_x_offset(line, col)
|
2020-12-12 20:26:27 +01:00
|
|
|
local xmargin = 3 * self:get_font():get_width(' ')
|
|
|
|
local xsup = xoffset + gw + xmargin
|
|
|
|
local xinf = xoffset - xmargin
|
|
|
|
if xsup > self.scroll.x + self.size.x then
|
|
|
|
self.scroll.to.x = xsup - self.size.x
|
|
|
|
elseif xinf < self.scroll.x then
|
|
|
|
self.scroll.to.x = math.max(0, xinf)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocView:on_mouse_moved(x, y, ...)
|
|
|
|
DocView.super.on_mouse_moved(self, x, y, ...)
|
|
|
|
|
2022-11-02 00:34:23 +01:00
|
|
|
self.hovering_gutter = false
|
|
|
|
local gw = self:get_gutter_width()
|
|
|
|
|
2022-10-16 02:12:15 +02:00
|
|
|
if self:scrollbar_hovering() or self:scrollbar_dragging() then
|
2019-12-28 12:16:32 +01:00
|
|
|
self.cursor = "arrow"
|
2022-11-02 00:34:23 +01:00
|
|
|
elseif gw > 0 and x >= self.position.x and x <= (self.position.x + gw) then
|
|
|
|
self.cursor = "arrow"
|
|
|
|
self.hovering_gutter = true
|
2019-12-28 12:16:32 +01:00
|
|
|
else
|
|
|
|
self.cursor = "ibeam"
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.mouse_selecting then
|
2020-06-25 14:33:38 +02:00
|
|
|
local l1, c1 = self:resolve_screen_position(x, y)
|
2021-11-19 10:05:26 +01:00
|
|
|
local l2, c2, snap_type = table.unpack(self.mouse_selecting)
|
2021-06-05 21:22:56 +02:00
|
|
|
if keymap.modkeys["ctrl"] then
|
2021-06-06 03:18:05 +02:00
|
|
|
if l1 > l2 then l1, l2 = l2, l1 end
|
2021-06-10 18:29:55 +02:00
|
|
|
self.doc.selections = { }
|
2021-06-05 21:22:56 +02:00
|
|
|
for i = l1, l2 do
|
2021-06-06 03:18:05 +02:00
|
|
|
self.doc:set_selections(i - l1 + 1, i, math.min(c1, #self.doc.lines[i]), i, math.min(c2, #self.doc.lines[i]))
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
|
|
|
else
|
2021-11-19 10:05:26 +01:00
|
|
|
if snap_type then
|
|
|
|
l1, c1, l2, c2 = self:mouse_selection(self.doc, snap_type, l1, c1, l2, c2)
|
|
|
|
end
|
2021-10-06 01:20:06 +02:00
|
|
|
self.doc:set_selection(l1, c1, l2, c2)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-11-19 10:05:26 +01:00
|
|
|
function DocView:mouse_selection(doc, snap_type, line1, col1, line2, col2)
|
|
|
|
local swap = line2 < line1 or line2 == line1 and col2 <= col1
|
|
|
|
if swap then
|
|
|
|
line1, col1, line2, col2 = line2, col2, line1, col1
|
|
|
|
end
|
|
|
|
if snap_type == "word" then
|
|
|
|
line1, col1 = translate.start_of_word(doc, line1, col1)
|
|
|
|
line2, col2 = translate.end_of_word(doc, line2, col2)
|
|
|
|
elseif snap_type == "lines" then
|
|
|
|
col1, col2 = 1, math.huge
|
|
|
|
end
|
|
|
|
if swap then
|
|
|
|
return line2, col2, line1, col1
|
|
|
|
end
|
|
|
|
return line1, col1, line2, col2
|
|
|
|
end
|
|
|
|
|
2022-11-02 00:34:23 +01:00
|
|
|
|
|
|
|
function DocView:on_mouse_pressed(button, x, y, clicks)
|
|
|
|
if button ~= "left" or not self.hovering_gutter then
|
|
|
|
return DocView.super.on_mouse_pressed(self, button, x, y, clicks)
|
|
|
|
end
|
|
|
|
local line = self:resolve_screen_position(x, y)
|
|
|
|
if keymap.modkeys["shift"] then
|
|
|
|
local sline, scol, sline2, scol2 = self.doc:get_selection(true)
|
|
|
|
if line > sline then
|
|
|
|
self.doc:set_selection(sline, 1, line, #self.doc.lines[line])
|
|
|
|
else
|
|
|
|
self.doc:set_selection(line, 1, sline2, #self.doc.lines[sline2])
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if clicks == 1 then
|
|
|
|
self.doc:set_selection(line, 1, line, 1)
|
|
|
|
elseif clicks == 2 then
|
|
|
|
self.doc:set_selection(line, 1, line, #self.doc.lines[line])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-11-19 10:05:26 +01:00
|
|
|
|
2022-04-12 02:38:30 +02:00
|
|
|
function DocView:on_mouse_released(...)
|
|
|
|
DocView.super.on_mouse_released(self, ...)
|
2020-06-25 14:33:38 +02:00
|
|
|
self.mouse_selecting = nil
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:on_text_input(text)
|
|
|
|
self.doc:text_input(text)
|
|
|
|
end
|
|
|
|
|
2022-10-16 01:58:51 +02:00
|
|
|
function DocView:on_ime_text_editing(text, start, length)
|
|
|
|
self.doc:ime_text_editing(text, start, length)
|
2022-12-21 01:11:13 +01:00
|
|
|
self.ime_status = #text > 0
|
2022-10-16 01:58:51 +02:00
|
|
|
self.ime_selection.from = start
|
|
|
|
self.ime_selection.size = length
|
|
|
|
|
|
|
|
-- Set the composition bounding box that the system IME
|
|
|
|
-- will consider when drawing its interface
|
2022-12-21 01:11:13 +01:00
|
|
|
local line1, col1, line2, col2 = self.doc:get_selection(true)
|
|
|
|
local col = math.min(col1, col2)
|
|
|
|
self:update_ime_location()
|
|
|
|
self:scroll_to_make_visible(line1, col + start)
|
|
|
|
end
|
|
|
|
|
|
|
|
---Update the composition bounding box that the system IME
|
|
|
|
---will consider when drawing its interface
|
|
|
|
function DocView:update_ime_location()
|
|
|
|
if not self.ime_status then return end
|
|
|
|
|
2022-10-16 01:58:51 +02:00
|
|
|
local line1, col1, line2, col2 = self.doc:get_selection(true)
|
|
|
|
local x, y = self:get_line_screen_position(line1)
|
|
|
|
local h = self:get_line_height()
|
|
|
|
local col = math.min(col1, col2)
|
|
|
|
|
|
|
|
local x1, x2 = 0, 0
|
|
|
|
|
2022-12-21 01:11:13 +01:00
|
|
|
if self.ime_selection.size > 0 then
|
2022-10-16 01:58:51 +02:00
|
|
|
-- focus on a part of the text
|
2022-12-21 01:11:13 +01:00
|
|
|
local from = col + self.ime_selection.from
|
|
|
|
local to = from + self.ime_selection.size
|
2022-10-16 01:58:51 +02:00
|
|
|
x1 = self:get_col_x_offset(line1, from)
|
|
|
|
x2 = self:get_col_x_offset(line1, to)
|
|
|
|
else
|
|
|
|
-- focus the whole text
|
|
|
|
x1 = self:get_col_x_offset(line1, col1)
|
|
|
|
x2 = self:get_col_x_offset(line2, col2)
|
|
|
|
end
|
|
|
|
|
|
|
|
ime.set_location(x + x1, y, x2 - x1, h)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
function DocView:update()
|
|
|
|
-- scroll to make caret visible and reset blink timer if it moved
|
2022-02-11 06:04:58 +01:00
|
|
|
local line1, col1, line2, col2 = self.doc:get_selection()
|
|
|
|
if (line1 ~= self.last_line1 or col1 ~= self.last_col1 or
|
|
|
|
line2 ~= self.last_line2 or col2 ~= self.last_col2) and self.size.x > 0 then
|
2022-10-16 01:58:51 +02:00
|
|
|
if core.active_view == self and not ime.editing then
|
2022-02-11 06:04:58 +01:00
|
|
|
self:scroll_to_make_visible(line1, col1)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-04-17 23:57:04 +02:00
|
|
|
core.blink_reset()
|
2022-02-11 06:04:58 +01:00
|
|
|
self.last_line1, self.last_col1 = line1, col1
|
|
|
|
self.last_line2, self.last_col2 = line2, col2
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- update blink timer
|
|
|
|
if self == core.active_view and not self.mouse_selecting then
|
2021-04-17 23:57:04 +02:00
|
|
|
local T, t0 = config.blink_period, core.blink_start
|
|
|
|
local ta, tb = core.blink_timer, system.get_time()
|
|
|
|
if ((tb - t0) % T < T / 2) ~= ((ta - t0) % T < T / 2) then
|
2019-12-28 12:16:32 +01:00
|
|
|
core.redraw = true
|
|
|
|
end
|
2021-04-17 23:57:04 +02:00
|
|
|
core.blink_timer = tb
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-12-21 01:11:13 +01:00
|
|
|
self:update_ime_location()
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
DocView.super.update(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function DocView:draw_line_highlight(x, y)
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
renderer.draw_rect(x, y, self.size.x, lh, style.line_highlight)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-03-24 17:01:46 +01:00
|
|
|
function DocView:draw_line_text(line, x, y)
|
2021-05-26 19:16:56 +02:00
|
|
|
local default_font = self:get_font()
|
2021-09-11 04:22:30 +02:00
|
|
|
local tx, ty = x, y + self:get_line_text_y_offset()
|
2022-10-21 19:56:23 +02:00
|
|
|
local last_token = nil
|
|
|
|
local tokens = self.doc.highlighter:get_line(line).tokens
|
|
|
|
local tokens_count = #tokens
|
|
|
|
if string.sub(tokens[tokens_count], -1) == "\n" then
|
|
|
|
last_token = tokens_count - 1
|
|
|
|
end
|
|
|
|
for tidx, type, text in self.doc.highlighter:each_token(line) do
|
2020-04-18 16:24:45 +02:00
|
|
|
local color = style.syntax[type]
|
2021-05-26 19:16:56 +02:00
|
|
|
local font = style.syntax_fonts[type] or default_font
|
2022-10-21 19:56:23 +02:00
|
|
|
-- do not render newline, fixes issue #1164
|
|
|
|
if tidx == last_token then text = text:sub(1, -2) end
|
2021-09-11 04:22:30 +02:00
|
|
|
tx = renderer.draw_text(font, text, tx, ty, color)
|
2023-01-19 04:15:26 +01:00
|
|
|
if tx > self.position.x + self.size.x then break end
|
2020-04-18 16:24:45 +02:00
|
|
|
end
|
2022-03-24 17:01:46 +01:00
|
|
|
return self:get_line_height()
|
2020-04-18 16:24:45 +02:00
|
|
|
end
|
|
|
|
|
2021-06-20 22:16:35 +02:00
|
|
|
function DocView:draw_caret(x, y)
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
renderer.draw_rect(x, y, style.caret_width, lh, style.caret)
|
|
|
|
end
|
2020-04-18 16:24:45 +02:00
|
|
|
|
2022-03-24 17:01:46 +01:00
|
|
|
function DocView:draw_line_body(line, x, y)
|
2021-11-08 16:00:24 +01:00
|
|
|
-- draw highlight if any selection ends on this line
|
|
|
|
local draw_highlight = false
|
2021-12-20 12:20:06 +01:00
|
|
|
local hcl = config.highlight_current_line
|
|
|
|
if hcl ~= false then
|
|
|
|
for lidx, line1, col1, line2, col2 in self.doc:get_selections(false) do
|
2022-03-25 01:12:45 +01:00
|
|
|
if line1 == line then
|
2021-12-20 12:20:06 +01:00
|
|
|
if hcl == "no_selection" then
|
|
|
|
if (line1 ~= line2) or (col1 ~= col2) then
|
|
|
|
draw_highlight = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
draw_highlight = true
|
|
|
|
break
|
|
|
|
end
|
2021-11-08 16:00:24 +01:00
|
|
|
end
|
|
|
|
end
|
2021-12-20 12:20:06 +01:00
|
|
|
if draw_highlight and core.active_view == self then
|
2021-11-08 16:00:24 +01:00
|
|
|
self:draw_line_highlight(x + self.scroll.x, y)
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
-- draw selection if it overlaps this line
|
2022-03-24 17:01:46 +01:00
|
|
|
local lh = self:get_line_height()
|
2021-06-20 22:16:35 +02:00
|
|
|
for lidx, line1, col1, line2, col2 in self.doc:get_selections(true) do
|
2022-03-24 17:01:46 +01:00
|
|
|
if line >= line1 and line <= line2 then
|
|
|
|
local text = self.doc.lines[line]
|
|
|
|
if line1 ~= line then col1 = 1 end
|
|
|
|
if line2 ~= line then col2 = #text + 1 end
|
|
|
|
local x1 = x + self:get_col_x_offset(line, col1)
|
|
|
|
local x2 = x + self:get_col_x_offset(line, col2)
|
2021-08-29 23:54:57 +02:00
|
|
|
if x1 ~= x2 then
|
|
|
|
renderer.draw_rect(x1, y, x2 - x1, lh, style.selection)
|
|
|
|
end
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-06-20 22:16:35 +02:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
-- draw line's text
|
2022-03-24 17:01:46 +01:00
|
|
|
return self:draw_line_text(line, x, y)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-03-24 17:01:46 +01:00
|
|
|
function DocView:draw_line_gutter(line, x, y, width)
|
2019-12-28 12:16:32 +01:00
|
|
|
local color = style.line_number
|
2021-06-20 22:16:35 +02:00
|
|
|
for _, line1, _, line2 in self.doc:get_selections(true) do
|
2022-03-24 17:01:46 +01:00
|
|
|
if line >= line1 and line <= line2 then
|
2021-06-05 21:22:56 +02:00
|
|
|
color = style.line_number2
|
|
|
|
break
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2020-05-14 14:26:21 +02:00
|
|
|
x = x + style.padding.x
|
2022-03-24 17:01:46 +01:00
|
|
|
local lh = self:get_line_height()
|
2022-03-25 01:12:45 +01:00
|
|
|
common.draw_text(self:get_font(), color, line, "right", x, y, width, lh)
|
2022-03-24 17:01:46 +01:00
|
|
|
return lh
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-10-16 01:58:51 +02:00
|
|
|
function DocView:draw_ime_decoration(line1, col1, line2, col2)
|
|
|
|
local x, y = self:get_line_screen_position(line1)
|
|
|
|
local line_size = math.max(1, SCALE)
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
|
|
|
|
-- Draw IME underline
|
|
|
|
local x1 = self:get_col_x_offset(line1, col1)
|
|
|
|
local x2 = self:get_col_x_offset(line2, col2)
|
|
|
|
renderer.draw_rect(x + math.min(x1, x2), y + lh - line_size, math.abs(x1 - x2), line_size, style.text)
|
|
|
|
|
|
|
|
-- Draw IME selection
|
|
|
|
local col = math.min(col1, col2)
|
|
|
|
local from = col + self.ime_selection.from
|
|
|
|
local to = from + self.ime_selection.size
|
|
|
|
x1 = self:get_col_x_offset(line1, from)
|
|
|
|
if from ~= to then
|
|
|
|
x2 = self:get_col_x_offset(line1, to)
|
|
|
|
line_size = style.caret_width
|
|
|
|
renderer.draw_rect(x + math.min(x1, x2), y + lh - line_size, math.abs(x1 - x2), line_size, style.caret)
|
|
|
|
end
|
|
|
|
self:draw_caret(x + x1, y)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-20 22:16:35 +02:00
|
|
|
function DocView:draw_overlay()
|
|
|
|
if core.active_view == self then
|
|
|
|
local minline, maxline = self:get_visible_line_range()
|
|
|
|
-- draw caret if it overlaps this line
|
|
|
|
local T = config.blink_period
|
2022-10-16 01:58:51 +02:00
|
|
|
for _, line1, col1, line2, col2 in self.doc:get_selections() do
|
|
|
|
if line1 >= minline and line1 <= maxline
|
2021-06-20 22:16:35 +02:00
|
|
|
and system.window_has_focus() then
|
2022-10-16 01:58:51 +02:00
|
|
|
if ime.editing then
|
|
|
|
self:draw_ime_decoration(line1, col1, line2, col2)
|
|
|
|
else
|
|
|
|
if config.disable_blink
|
|
|
|
or (core.blink_timer - core.blink_start) % T < T / 2 then
|
|
|
|
self:draw_caret(self:get_line_screen_position(line1, col1))
|
|
|
|
end
|
2021-10-02 16:38:10 +02:00
|
|
|
end
|
2021-06-20 22:16:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function DocView:draw()
|
|
|
|
self:draw_background(style.background)
|
2021-11-20 03:20:49 +01:00
|
|
|
local _, indent_size = self.doc:get_indent_info()
|
|
|
|
self:get_font():set_tab_size(indent_size)
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local minline, maxline = self:get_visible_line_range()
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
|
2021-06-20 22:16:35 +02:00
|
|
|
local x, y = self:get_line_screen_position(minline)
|
2021-07-28 20:12:46 +02:00
|
|
|
local gw, gpad = self:get_gutter_width()
|
2019-12-28 12:16:32 +01:00
|
|
|
for i = minline, maxline do
|
2022-03-24 17:01:46 +01:00
|
|
|
y = y + (self:draw_line_gutter(i, self.position.x, y, gpad and gw - gpad or gw) or lh)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local pos = self.position
|
2021-06-20 22:16:35 +02:00
|
|
|
x, y = self:get_line_screen_position(minline)
|
2021-10-11 09:25:38 +02:00
|
|
|
-- the clip below ensure we don't write on the gutter region. On the
|
|
|
|
-- right side it is redundant with the Node's clip.
|
2021-10-10 21:44:16 +02:00
|
|
|
core.push_clip_rect(pos.x + gw, pos.y, self.size.x - gw, self.size.y)
|
2019-12-28 12:16:32 +01:00
|
|
|
for i = minline, maxline do
|
2022-03-24 17:01:46 +01:00
|
|
|
y = y + (self:draw_line_body(i, x, y) or lh)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-06-20 22:16:35 +02:00
|
|
|
self:draw_overlay()
|
2019-12-28 12:16:32 +01:00
|
|
|
core.pop_clip_rect()
|
|
|
|
|
|
|
|
self:draw_scrollbar()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return DocView
|