2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local common = require "core.common"
|
|
|
|
local command = require "core.command"
|
|
|
|
local config = require "core.config"
|
|
|
|
local keymap = require "core.keymap"
|
|
|
|
local style = require "core.style"
|
|
|
|
local View = require "core.view"
|
|
|
|
|
2020-05-16 10:44:31 +02:00
|
|
|
config.treeview_size = 200 * SCALE
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local function get_depth(filename)
|
2020-12-28 10:26:27 +01:00
|
|
|
local n = 1
|
2019-12-28 12:16:32 +01:00
|
|
|
for sep in filename:gmatch("[\\/]") do
|
|
|
|
n = n + 1
|
|
|
|
end
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-16 10:44:31 +02:00
|
|
|
local TreeView = View:extend()
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function TreeView:new()
|
|
|
|
TreeView.super.new(self)
|
|
|
|
self.scrollable = true
|
|
|
|
self.visible = true
|
2021-02-15 12:18:43 +01:00
|
|
|
self.init_size = config.treeview_size
|
2019-12-28 12:16:32 +01:00
|
|
|
self.cache = {}
|
2020-12-26 21:22:57 +01:00
|
|
|
self.last = {}
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2020-12-26 16:48:08 +01:00
|
|
|
|
|
|
|
function TreeView:get_cached(item, dirname)
|
2020-12-26 21:22:57 +01:00
|
|
|
local dir_cache = self.cache[dirname]
|
|
|
|
if not dir_cache then
|
|
|
|
dir_cache = {}
|
|
|
|
self.cache[dirname] = dir_cache
|
|
|
|
end
|
2021-01-19 16:39:15 +01:00
|
|
|
-- to discriminate top directories from regular files or subdirectories
|
|
|
|
-- we add ':' at the end of the top directories' filename. it will be
|
|
|
|
-- used only to identify the entry into the cache.
|
|
|
|
local cache_name = item.filename .. (item.topdir and ":" or "")
|
|
|
|
local t = dir_cache[cache_name]
|
2019-12-28 12:16:32 +01:00
|
|
|
if not t then
|
|
|
|
t = {}
|
2021-01-10 12:56:15 +01:00
|
|
|
local basename = common.basename(item.filename)
|
2020-12-28 10:26:27 +01:00
|
|
|
if item.topdir then
|
2020-12-27 11:32:52 +01:00
|
|
|
t.filename = basename
|
|
|
|
t.expanded = true
|
2020-12-28 10:26:27 +01:00
|
|
|
t.depth = 0
|
2020-12-28 15:51:16 +01:00
|
|
|
t.abs_filename = dirname
|
2020-12-28 10:26:27 +01:00
|
|
|
else
|
|
|
|
t.filename = item.filename
|
|
|
|
t.depth = get_depth(item.filename)
|
2020-12-28 15:51:16 +01:00
|
|
|
t.abs_filename = dirname .. PATHSEP .. item.filename
|
2020-12-27 11:32:52 +01:00
|
|
|
end
|
2020-12-27 09:47:58 +01:00
|
|
|
t.name = basename
|
2019-12-28 12:16:32 +01:00
|
|
|
t.type = item.type
|
2021-01-19 16:39:15 +01:00
|
|
|
dir_cache[cache_name] = t
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:get_name()
|
|
|
|
return "Project"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:get_item_height()
|
|
|
|
return style.font:get_height() + style.padding.y
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:check_cache()
|
|
|
|
-- invalidate cache's skip values if project_files has changed
|
2020-12-26 21:22:57 +01:00
|
|
|
for i = 1, #core.project_directories do
|
|
|
|
local dir = core.project_directories[i]
|
2020-12-26 23:29:18 +01:00
|
|
|
local last_files = self.last[dir.name]
|
2020-12-26 21:22:57 +01:00
|
|
|
if not last_files then
|
2020-12-26 23:29:18 +01:00
|
|
|
self.last[dir.name] = dir.files
|
2020-12-26 21:22:57 +01:00
|
|
|
else
|
|
|
|
if dir.files ~= last_files then
|
2020-12-26 23:29:18 +01:00
|
|
|
for _, v in pairs(self.cache[dir.name]) do
|
2020-12-26 21:22:57 +01:00
|
|
|
v.skip = nil
|
|
|
|
end
|
2020-12-26 23:29:18 +01:00
|
|
|
self.last[dir.name] = dir.files
|
2020-12-26 21:22:57 +01:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:each_item()
|
|
|
|
return coroutine.wrap(function()
|
|
|
|
self:check_cache()
|
2020-12-30 16:15:33 +01:00
|
|
|
local count_lines = 0
|
2019-12-28 12:16:32 +01:00
|
|
|
local ox, oy = self:get_content_offset()
|
|
|
|
local y = oy + style.padding.y
|
|
|
|
local w = self.size.x
|
|
|
|
local h = self:get_item_height()
|
|
|
|
|
2020-12-26 21:22:57 +01:00
|
|
|
for k = 1, #core.project_directories do
|
|
|
|
local dir = core.project_directories[k]
|
2020-12-26 23:29:18 +01:00
|
|
|
local dir_cached = self:get_cached(dir.item, dir.name)
|
2020-12-26 21:22:57 +01:00
|
|
|
coroutine.yield(dir_cached, ox, y, w, h)
|
2020-12-30 16:15:33 +01:00
|
|
|
count_lines = count_lines + 1
|
2019-12-28 12:16:32 +01:00
|
|
|
y = y + h
|
2020-12-26 21:22:57 +01:00
|
|
|
local i = 1
|
2020-12-27 11:32:52 +01:00
|
|
|
while i <= #dir.files and dir_cached.expanded do
|
2020-12-26 21:22:57 +01:00
|
|
|
local item = dir.files[i]
|
2020-12-26 23:29:18 +01:00
|
|
|
local cached = self:get_cached(item, dir.name)
|
2020-12-26 21:22:57 +01:00
|
|
|
|
|
|
|
coroutine.yield(cached, ox, y, w, h)
|
2020-12-30 16:15:33 +01:00
|
|
|
count_lines = count_lines + 1
|
2020-12-26 21:22:57 +01:00
|
|
|
y = y + h
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
if not cached.expanded then
|
|
|
|
if cached.skip then
|
|
|
|
i = cached.skip
|
|
|
|
else
|
|
|
|
local depth = cached.depth
|
|
|
|
while i <= #dir.files do
|
2020-12-27 09:47:58 +01:00
|
|
|
if get_depth(dir.files[i].filename) <= depth then break end
|
2020-12-26 21:22:57 +01:00
|
|
|
i = i + 1
|
2020-12-26 16:48:08 +01:00
|
|
|
end
|
2020-12-26 21:22:57 +01:00
|
|
|
cached.skip = i
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
2020-12-26 16:48:08 +01:00
|
|
|
end -- while files
|
|
|
|
end -- for directories
|
2020-12-30 16:15:33 +01:00
|
|
|
self.count_lines = count_lines
|
2019-12-28 12:16:32 +01:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-30 16:15:33 +01:00
|
|
|
function TreeView:on_mouse_moved(px, py, ...)
|
|
|
|
TreeView.super.on_mouse_moved(self, px, py, ...)
|
|
|
|
if self.dragging_scrollbar then return end
|
2019-12-28 12:16:32 +01:00
|
|
|
self.hovered_item = nil
|
|
|
|
for item, x,y,w,h in self:each_item() do
|
|
|
|
if px > x and py > y and px <= x + w and py <= y + h then
|
|
|
|
self.hovered_item = item
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-28 16:18:03 +01:00
|
|
|
local function create_directory_in(item)
|
|
|
|
local path = item.abs_filename
|
2020-12-30 12:00:51 +01:00
|
|
|
core.command_view:enter("Create directory in " .. path, function(text)
|
2020-12-28 16:18:03 +01:00
|
|
|
local dirname = path .. PATHSEP .. text
|
|
|
|
local success, err = system.mkdir(dirname)
|
|
|
|
if not success then
|
|
|
|
core.error("cannot create directory %q: %s", dirname, err)
|
|
|
|
end
|
|
|
|
item.expanded = true
|
2020-12-30 14:31:36 +01:00
|
|
|
core.reschedule_project_scan()
|
2020-12-28 16:18:03 +01:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-30 16:15:33 +01:00
|
|
|
function TreeView:on_mouse_pressed(button, x, y, clicks)
|
|
|
|
local caught = TreeView.super.on_mouse_pressed(self, button, x, y, clicks)
|
|
|
|
if caught then
|
|
|
|
return
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
if not self.hovered_item then
|
|
|
|
return
|
|
|
|
elseif self.hovered_item.type == "dir" then
|
2020-12-30 12:00:51 +01:00
|
|
|
if keymap.modkeys["ctrl"] and button == "left" then
|
2020-12-28 16:18:03 +01:00
|
|
|
create_directory_in(self.hovered_item)
|
2020-12-28 15:51:16 +01:00
|
|
|
else
|
|
|
|
self.hovered_item.expanded = not self.hovered_item.expanded
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
else
|
|
|
|
core.try(function()
|
2020-12-27 09:47:58 +01:00
|
|
|
core.root_view:open_doc(core.open_doc(self.hovered_item.abs_filename))
|
2019-12-28 12:16:32 +01:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:update()
|
|
|
|
-- update width
|
2020-05-07 15:03:37 +02:00
|
|
|
if self.init_size then
|
2021-02-15 12:18:43 +01:00
|
|
|
self.size.x = self.init_size
|
|
|
|
self.init_size = nil
|
|
|
|
elseif self.goto_size then
|
|
|
|
if self.goto_size ~= self.size.x then
|
|
|
|
self:move_towards(self.size, "x", self.goto_size)
|
|
|
|
else
|
|
|
|
self.goto_size = nil
|
|
|
|
end
|
2020-05-07 15:03:37 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
TreeView.super.update(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-30 16:15:33 +01:00
|
|
|
function TreeView:get_scrollable_size()
|
|
|
|
return self.count_lines and self:get_item_height() * (self.count_lines + 1) or math.huge
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function TreeView:draw()
|
|
|
|
self:draw_background(style.background2)
|
|
|
|
|
|
|
|
local icon_width = style.icon_font:get_width("D")
|
|
|
|
local spacing = style.font:get_width(" ") * 2
|
|
|
|
|
|
|
|
local doc = core.active_view.doc
|
|
|
|
local active_filename = doc and system.absolute_path(doc.filename or "")
|
|
|
|
|
|
|
|
for item, x,y,w,h in self:each_item() do
|
|
|
|
local color = style.text
|
|
|
|
|
|
|
|
-- highlight active_view doc
|
|
|
|
if item.abs_filename == active_filename then
|
|
|
|
color = style.accent
|
|
|
|
end
|
|
|
|
|
|
|
|
-- hovered item background
|
|
|
|
if item == self.hovered_item then
|
|
|
|
renderer.draw_rect(x, y, w, h, style.line_highlight)
|
|
|
|
color = style.accent
|
|
|
|
end
|
|
|
|
|
|
|
|
-- icons
|
2020-05-17 17:59:19 +02:00
|
|
|
x = x + item.depth * style.padding.x + style.padding.x
|
2019-12-28 12:16:32 +01:00
|
|
|
if item.type == "dir" then
|
2020-03-26 00:36:56 +01:00
|
|
|
local icon1 = item.expanded and "-" or "+"
|
2019-12-28 12:16:32 +01:00
|
|
|
local icon2 = item.expanded and "D" or "d"
|
|
|
|
common.draw_text(style.icon_font, color, icon1, nil, x, y, 0, h)
|
|
|
|
x = x + style.padding.x
|
|
|
|
common.draw_text(style.icon_font, color, icon2, nil, x, y, 0, h)
|
|
|
|
x = x + icon_width
|
|
|
|
else
|
|
|
|
x = x + style.padding.x
|
|
|
|
common.draw_text(style.icon_font, color, "f", nil, x, y, 0, h)
|
|
|
|
x = x + icon_width
|
|
|
|
end
|
|
|
|
|
|
|
|
-- text
|
|
|
|
x = x + spacing
|
|
|
|
x = common.draw_text(style.font, color, item.name, nil, x, y, 0, h)
|
|
|
|
end
|
2020-12-30 16:15:33 +01:00
|
|
|
|
|
|
|
self:draw_scrollbar()
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- init
|
|
|
|
local view = TreeView()
|
|
|
|
local node = core.root_view:get_active_node()
|
2021-02-15 00:36:39 +01:00
|
|
|
local treeview_node = node:split("left", view, {x = true}, true)
|
|
|
|
|
2021-02-15 15:45:56 +01:00
|
|
|
-- The toolbarview plugin is special because it is plugged inside
|
|
|
|
-- a treeview pane which is itelf provided in a plugin.
|
|
|
|
-- We therefore break the usual plugin's logic that would require each
|
|
|
|
-- plugin to be independent of each other. In addition it is not the
|
|
|
|
-- plugin module that plug itself in the active node but it is plugged here
|
|
|
|
-- in the treeview node.
|
2021-02-15 00:36:39 +01:00
|
|
|
local toolbar_plugin, ToolbarView = core.try(require, "plugins.toolbarview")
|
2021-02-15 15:45:56 +01:00
|
|
|
if config.toolbarview ~= false and toolbar_plugin then
|
|
|
|
local toolbar_view = ToolbarView()
|
|
|
|
treeview_node:split("down", toolbar_view, {y = true})
|
|
|
|
command.add(nil, {
|
|
|
|
["toolbar:toggle"] = function()
|
|
|
|
toolbar_view:toggle_visible()
|
|
|
|
end,
|
|
|
|
})
|
2021-02-15 00:36:39 +01:00
|
|
|
end
|
2021-02-09 18:41:32 +01:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
-- register commands and keymap
|
|
|
|
command.add(nil, {
|
|
|
|
["treeview:toggle"] = function()
|
2021-02-15 12:18:43 +01:00
|
|
|
if view.visible then
|
|
|
|
view.previous_size = view.size.x
|
|
|
|
view.visible = false
|
|
|
|
view.goto_size = 0
|
|
|
|
else
|
|
|
|
view.visible = true
|
|
|
|
view.goto_size = view.previous_size
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2021-02-16 09:41:03 +01:00
|
|
|
keymap.add { ["ctrl+\\"] = "treeview:toggle" }
|