From e3c42d865e312a559fa5a1b0781eb2a50e160765 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Tue, 3 Aug 2021 15:44:14 +0800 Subject: [PATCH] remove data/plugins/contextmenu.lua moved the code throughout rootview, keymap and commands --- data/core/commands/contextmenu.lua | 9 +++++ data/core/init.lua | 38 +++++++++++++++++++ data/core/keymap.lua | 4 +- data/core/rootview.lua | 5 +++ data/plugins/contextmenu.lua | 59 ------------------------------ 5 files changed, 55 insertions(+), 60 deletions(-) create mode 100644 data/core/commands/contextmenu.lua delete mode 100644 data/plugins/contextmenu.lua diff --git a/data/core/commands/contextmenu.lua b/data/core/commands/contextmenu.lua new file mode 100644 index 00000000..8da4907a --- /dev/null +++ b/data/core/commands/contextmenu.lua @@ -0,0 +1,9 @@ +local core = require "core" +local command = require "core.command" + + +command.add(nil, { + ["context-menu:show"] = function() + core.context_menu:show(core.active_view.position.x, core.active_view.position.y) + end +}) diff --git a/data/core/init.lua b/data/core/init.lua index 6fbdde48..509c788e 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -9,6 +9,7 @@ local RootView local StatusView local TitleView local CommandView +local ContextMenu local NagView local DocView local Doc @@ -417,6 +418,40 @@ local function reload_on_user_module_save() end +local function find_selected_occurence() + local doc = core.active_view.doc + if not doc or not doc:has_selection() then return end + + -- get selection. If #358 is merged we won't need this + local text = {} + for idx, line1, col1, line2, col2 in doc:get_selections() do + if line1 ~= line2 or col1 ~= col2 then + local t = doc:get_text(line1, col1, line2, col2) + if t ~= "" then text[#text + 1] = t end + end + end + text = table.concat(text, "\n") + + command.perform "find-replace:find" + core.command_view:set_text(text) + core.command_view:submit() +end + + +local function setup_context_menu() + local info = keymap.reverse_map["find-replace:find"] + core.context_menu:register("core.docview", { + { text = "Find Occurence...", command = find_selected_occurence, info = info }, + ContextMenu.DIVIDER, + { text = "Cut", command = "doc:cut" }, + { text = "Copy", command = "doc:copy" }, + { text = "Paste", command = "doc:paste" }, + ContextMenu.DIVIDER, + { text = "Command Palette...", command = "core:find-command" } + }) +end + + function core.init() command = require "core.command" keymap = require "core.keymap" @@ -424,6 +459,7 @@ function core.init() StatusView = require "core.statusview" TitleView = require "core.titleview" CommandView = require "core.commandview" + ContextMenu = require "core.contextmenu" NagView = require "core.nagview" DocView = require "core.docview" Doc = require "core.doc" @@ -498,6 +534,7 @@ function core.init() core.root_view = RootView() core.command_view = CommandView() + core.context_menu = ContextMenu() core.status_view = StatusView() core.nag_view = NagView() core.title_view = TitleView() @@ -560,6 +597,7 @@ function core.init() end) end + setup_context_menu() reload_on_user_module_save() end diff --git a/data/core/keymap.lua b/data/core/keymap.lua index 86be82b1..8d612bbb 100644 --- a/data/core/keymap.lua +++ b/data/core/keymap.lua @@ -206,7 +206,9 @@ keymap.add_direct { ["shift+pageup"] = "doc:select-to-previous-page", ["shift+pagedown"] = "doc:select-to-next-page", ["ctrl+shift+up"] = "doc:create-cursor-previous-line", - ["ctrl+shift+down"] = "doc:create-cursor-next-line" + ["ctrl+shift+down"] = "doc:create-cursor-next-line", + + ["menu"] = "context-menu:show" } return keymap diff --git a/data/core/rootview.lua b/data/core/rootview.lua index 36ab148d..47605d77 100644 --- a/data/core/rootview.lua +++ b/data/core/rootview.lua @@ -741,6 +741,7 @@ end -- Function to intercept mouse pressed events on the active view. -- Do nothing by default. function RootView.on_view_mouse_pressed(button, x, y, clicks) + return core.context_menu:on_mouse_pressed(button, x, y, clicks) end @@ -801,6 +802,8 @@ function RootView:on_mouse_moved(x, y, dx, dy) return end + if core.context_menu:on_mouse_moved(x, y, dx, dy) then return end + if self.dragged_divider then local node = self.dragged_divider if node.type == "hsplit" then @@ -874,11 +877,13 @@ function RootView:update() copy_position_and_size(self.root_node, self) self.root_node:update() self.root_node:update_layout() + core.context_menu:update() end function RootView:draw() self.root_node:draw() + core.context_menu:draw() while #self.deferred_draws > 0 do local t = table.remove(self.deferred_draws) t.fn(table.unpack(t)) diff --git a/data/plugins/contextmenu.lua b/data/plugins/contextmenu.lua deleted file mode 100644 index c0fe49fd..00000000 --- a/data/plugins/contextmenu.lua +++ /dev/null @@ -1,59 +0,0 @@ --- mod-version:1 -- lite-xl 1.16 -local core = require "core" -local command = require "core.command" -local keymap = require "core.keymap" -local ContextMenu = require "core.contextmenu" -local RootView = require "core.rootview" - -local menu = ContextMenu() -local on_view_mouse_pressed = RootView.on_view_mouse_pressed -local on_mouse_moved = RootView.on_mouse_moved -local root_view_update = RootView.update -local root_view_draw = RootView.draw - -function RootView:on_mouse_moved(...) - if menu:on_mouse_moved(...) then return end - on_mouse_moved(self, ...) -end - -function RootView.on_view_mouse_pressed(button, x, y, clicks) - -- We give the priority to the menu to process mouse pressed events. - local handled = menu:on_mouse_pressed(button, x, y, clicks) - return handled or on_view_mouse_pressed(button, x, y, clicks) -end - -function RootView:update(...) - root_view_update(self, ...) - menu:update() -end - -function RootView:draw(...) - root_view_draw(self, ...) - menu:draw() -end - -command.add(nil, { - ["context:show"] = function() - menu:show(core.active_view.position.x, core.active_view.position.y) - end -}) - -keymap.add { - ["menu"] = "context:show" -} - -if require("plugins.scale") then - menu:register("core.docview", { - { text = "Font +", command = "scale:increase" }, - { text = "Font -", command = "scale:decrease" }, - { text = "Font Reset", command = "scale:reset" }, - ContextMenu.DIVIDER, - { text = "Find", command = "find-replace:find" }, - { text = "Replace", command = "find-replace:replace" }, - ContextMenu.DIVIDER, - { text = "Find Pattern", command = "find-replace:find-pattern" }, - { text = "Replace Pattern", command = "find-replace:replace-pattern" }, - }) -end - -return menu