Revert horizontal scroll implementation

This commit is contained in:
Guldoman 2021-09-08 03:59:12 +02:00 committed by Francesco
parent 474952645c
commit 0a52861129
4 changed files with 6 additions and 147 deletions

View File

@ -393,5 +393,4 @@ function common.rm(path, recursively)
return true return true
end end
return common return common

View File

@ -32,7 +32,6 @@ end
function Doc:reset() function Doc:reset()
self.lines = { "\n" } self.lines = { "\n" }
self.long_lines = { line_numbers = { }, length = 0 }
self.selections = { 1, 1, 1, 1 } self.selections = { 1, 1, 1, 1 }
self.cursor_clipboard = {} self.cursor_clipboard = {}
self.undo_stack = { idx = 1 } self.undo_stack = { idx = 1 }
@ -61,33 +60,19 @@ end
function Doc:load(filename) function Doc:load(filename)
local fp = assert( io.open(filename, "rb") ) local fp = assert( io.open(filename, "rb") )
local max_length = 0
local line_numbers = { }
self:reset() self:reset()
self.lines = {} self.lines = {}
local i = 1
for line in fp:lines() do for line in fp:lines() do
if line:byte(-1) == 13 then if line:byte(-1) == 13 then
line = line:sub(1, -2) line = line:sub(1, -2)
self.crlf = true self.crlf = true
end end
table.insert(self.lines, line .. "\n") table.insert(self.lines, line .. "\n")
local line_len = #line + 1 -- account for newline
if line_len >= max_length then
max_length = line_len
line_numbers[i] = line_len
end
i = i + 1
end
for n, len in pairs(line_numbers) do
line_numbers[n] = len >= max_length and len or nil
end end
if #self.lines == 0 then if #self.lines == 0 then
table.insert(self.lines, "\n") table.insert(self.lines, "\n")
end end
fp:close() fp:close()
self.long_lines.line_numbers = line_numbers
self.long_lines.length = max_length
self:reset_syntax() self:reset_syntax()
end end
@ -362,20 +347,6 @@ function Doc:raw_insert(line, col, text, undo_stack, time)
push_undo(undo_stack, time, "selection", unpack(self.selections)) push_undo(undo_stack, time, "selection", unpack(self.selections))
push_undo(undo_stack, time, "remove", line, col, line2, col2) push_undo(undo_stack, time, "remove", line, col, line2, col2)
if #lines > 1 then
-- Need to shift all the subsequent long lines
local line_numbers = { }
for n, len in pairs(self.long_lines.line_numbers) do
if n > line then
line_numbers[n + #lines - 1] = len
else
line_numbers[n] = len
end
end
self.long_lines.line_numbers = line_numbers
end
self:update_max_line_len_range(line, line2)
-- update highlighter and assure selection is in bounds -- update highlighter and assure selection is in bounds
self.highlighter:invalidate(line) self.highlighter:invalidate(line)
self:sanitize_selection() self:sanitize_selection()
@ -403,23 +374,6 @@ function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time)
self:set_selections(idx, cline1 - line_removal, ccol1 - column_removal, cline2 - line_removal, ccol2 - column_removal) self:set_selections(idx, cline1 - line_removal, ccol1 - column_removal, cline2 - line_removal, ccol2 - column_removal)
end end
local nlines = line2 - line1 + 1
if nlines > 1 then
-- Need to shift all the subsequent long lines
local line_numbers = { }
for n, len in pairs(self.long_lines.line_numbers) do
if n > line2 then
line_numbers[n - nlines + 1] = len
elseif n > line1 then
line_numbers[n] = nil -- invalidate any line that has been deleted
else
line_numbers[n] = len
end
end
self.long_lines.line_numbers = line_numbers
end
self:update_max_line_len_range(line1, line2)
-- update highlighter and assure selection is in bounds -- update highlighter and assure selection is in bounds
self.highlighter:invalidate(line1) self.highlighter:invalidate(line1)
self:sanitize_selection() self:sanitize_selection()
@ -585,36 +539,6 @@ function Doc:indent_text(unindent, line1, col1, line2, col2)
return line1, col1 + #text, line1, col1 + #text return line1, col1 + #text, line1, col1 + #text
end end
function Doc:update_max_line_len_range(start_line, end_line)
local line_numbers = self.long_lines.line_numbers
local max_length = self.long_lines.length
end_line = math.min(end_line, #self.lines)
for line=start_line,end_line do
local line_len = #self.lines[line]
if line_len >= max_length then
max_length = line_len
line_numbers[line] = line_len
else
if line_numbers[line] then line_numbers[line] = nil end
end
end
for n, len in pairs(line_numbers) do
line_numbers[n] = len >= max_length and len or nil
end
if not next(line_numbers) then
-- Recalc needed
self.long_lines.length = 0
self.long_lines.line_numbers = line_numbers
return self:update_max_line_len_range(1, #self.lines)
end
self.long_lines.line_numbers = line_numbers
self.long_lines.length = max_length
end
-- For plugins to add custom actions of document change -- For plugins to add custom actions of document change
function Doc:on_text_change(type) function Doc:on_text_change(type)
end end

View File

@ -101,19 +101,6 @@ function DocView:get_scrollable_size()
return self:get_line_height() * (#self.doc.lines - 1) + self.size.y return self:get_line_height() * (#self.doc.lines - 1) + self.size.y
end end
function DocView:get_h_scrollable_size()
local doc_change_id = self.doc:get_change_id()
if self.last_doc_change_id ~= doc_change_id then
self.last_doc_change_id = doc_change_id
local xmargin = 3 * self:get_font():get_width(' ') -- from DocView:scroll_to_make_visible
-- TODO: make Doc calculate the real longest line in pixels, not in characters,
-- as the current implementation only works for monospace fonts
local long_line = next(self.doc.long_lines.line_numbers) or 1
self.h_scrollable_size = self:get_col_x_offset(long_line, self.doc.long_lines.length)
+ self:get_gutter_width() + xmargin
end
return self.h_scrollable_size
end
function DocView:get_font() function DocView:get_font()
return style[self.font] return style[self.font]
@ -285,8 +272,7 @@ end
function DocView:on_mouse_moved(x, y, ...) function DocView:on_mouse_moved(x, y, ...)
DocView.super.on_mouse_moved(self, x, y, ...) DocView.super.on_mouse_moved(self, x, y, ...)
if self:scrollbar_overlaps_point(x, y) or self.dragging_scrollbar if self:scrollbar_overlaps_point(x, y) or self.dragging_scrollbar then
or self:h_scrollbar_overlaps_point(x, y) or self.dragging_h_scrollbar then
self.cursor = "arrow" self.cursor = "arrow"
else else
self.cursor = "ibeam" self.cursor = "ibeam"
@ -457,7 +443,6 @@ function DocView:draw()
core.pop_clip_rect() core.pop_clip_rect()
self:draw_scrollbar() self:draw_scrollbar()
self:draw_h_scrollbar()
end end

View File

@ -3,7 +3,6 @@ local config = require "core.config"
local style = require "core.style" local style = require "core.style"
local common = require "core.common" local common = require "core.common"
local Object = require "core.object" local Object = require "core.object"
local keymap = require "core.keymap"
local View = Object:extend() local View = Object:extend()
@ -57,11 +56,6 @@ function View:get_scrollable_size()
end end
function View:get_h_scrollable_size()
return 0
end
function View:get_scrollbar_rect() function View:get_scrollbar_rect()
local sz = self:get_scrollable_size() local sz = self:get_scrollable_size()
if sz <= self.size.y or sz == math.huge then if sz <= self.size.y or sz == math.huge then
@ -76,29 +70,9 @@ function View:get_scrollbar_rect()
end end
function View:get_h_scrollbar_rect()
local sz = self:get_h_scrollable_size()
if sz <= self.size.x or sz == math.huge then
return 0, 0, 0, 0
end
local w = math.max(20, self.size.x * self.size.x / sz)
return
self.position.x + self.scroll.x * (self.size.x - w) / (sz - self.size.x),
self.position.y + self.size.y - style.scrollbar_size,
w,
style.scrollbar_size
end
function View:scrollbar_overlaps_point(x, y) function View:scrollbar_overlaps_point(x, y)
local sx, sy, sw, sh = self:get_scrollbar_rect() local sx, sy, sw, sh = self:get_scrollbar_rect()
return x >= sx - sw * 3 and x < sx + sw and y >= sy and y <= sy + sh return x >= sx - sw * 3 and x < sx + sw and y >= sy and y < sy + sh
end
function View:h_scrollbar_overlaps_point(x, y)
local sx, sy, sw, sh = self:get_h_scrollbar_rect()
return x >= sx and x <= sx + sw and y > sy - sh * 3 and y <= sy + sh
end end
@ -106,16 +80,12 @@ function View:on_mouse_pressed(button, x, y, clicks)
if self:scrollbar_overlaps_point(x, y) then if self:scrollbar_overlaps_point(x, y) then
self.dragging_scrollbar = true self.dragging_scrollbar = true
return true return true
elseif self:h_scrollbar_overlaps_point(x, y) then
self.dragging_h_scrollbar = true
return true
end end
end end
function View:on_mouse_released(button, x, y) function View:on_mouse_released(button, x, y)
self.dragging_scrollbar = false self.dragging_scrollbar = false
self.dragging_h_scrollbar = false
end end
@ -123,12 +93,8 @@ function View:on_mouse_moved(x, y, dx, dy)
if self.dragging_scrollbar then if self.dragging_scrollbar then
local delta = self:get_scrollable_size() / self.size.y * dy local delta = self:get_scrollable_size() / self.size.y * dy
self.scroll.to.y = self.scroll.to.y + delta self.scroll.to.y = self.scroll.to.y + delta
elseif self.dragging_h_scrollbar then
local delta = self:get_h_scrollable_size() / self.size.x * dx
self.scroll.to.x = self.scroll.to.x + delta
end end
self.hovered_scrollbar = self:scrollbar_overlaps_point(x, y) self.hovered_scrollbar = self:scrollbar_overlaps_point(x, y)
self.hovered_h_scrollbar = self:h_scrollbar_overlaps_point(x, y)
end end
@ -137,13 +103,9 @@ function View:on_text_input(text)
end end
function View:on_mouse_wheel(quant) function View:on_mouse_wheel(y)
if self.scrollable then if self.scrollable then
if keymap.modkeys["shift"] then self.scroll.to.y = self.scroll.to.y + y * -config.mouse_wheel_scroll
self.scroll.to.x = self.scroll.to.x + quant * -config.mouse_wheel_scroll
else
self.scroll.to.y = self.scroll.to.y + quant * -config.mouse_wheel_scroll
end
end end
end end
@ -163,10 +125,8 @@ end
function View:clamp_scroll_position() function View:clamp_scroll_position()
local max_x = self:get_h_scrollable_size() - self.size.x local max = self:get_scrollable_size() - self.size.y
local max_y = self:get_scrollable_size() - self.size.y self.scroll.to.y = common.clamp(self.scroll.to.y, 0, max)
self.scroll.to.x = common.clamp(self.scroll.to.x, 0, max_x)
self.scroll.to.y = common.clamp(self.scroll.to.y, 0, max_y)
end end
@ -192,15 +152,6 @@ function View:draw_scrollbar()
end end
function View:draw_h_scrollbar()
local x, y, w, h = self:get_h_scrollbar_rect()
local highlight = self.hovered_h_scrollbar and not self.hovered_scrollbar
or self.dragging_h_scrollbar
local color = highlight and style.scrollbar2 or style.scrollbar
renderer.draw_rect(x, y, w, h, color)
end
function View:draw() function View:draw()
end end