From a36a7049be176c76f33af67fd044891a7f5eec9f Mon Sep 17 00:00:00 2001 From: LudoPinelli <93927273+LudoPinelli@users.noreply.github.com> Date: Sat, 27 Jan 2024 16:09:36 +0100 Subject: [PATCH] refactor: replaced deprecated code --- lua/mind/commands.lua | 10 +++- lua/mind/init.lua | 110 +++++++++++++++++------------------------- lua/mind/ui.lua | 33 ++++++++----- 3 files changed, 73 insertions(+), 80 deletions(-) diff --git a/lua/mind/commands.lua b/lua/mind/commands.lua index a262c80..71f6359 100644 --- a/lua/mind/commands.lua +++ b/lua/mind/commands.lua @@ -56,7 +56,12 @@ M.commands = { end, open_data = function(args) - M.open_data_cursor(args.get_tree(), args.data_dir, args.save_tree, args.opts) + M.open_data_cursor( + args.get_tree(), + args.data_dir, + args.save_tree, + args.opts + ) end, open_data_index = function(args) @@ -118,10 +123,11 @@ M.open_data = function(tree, node, directory, save_tree, opts) end local data = node.data - if (data == nil) then + if data == nil then local contents = string.format(opts.edit.data_header, node.contents[1].text) local should_expand = tree.type ~= mind_node.TreeType.LOCAL_ROOT + ---@diagnostic disable-next-line: cast-local-type data = mind_data.new_data_file( directory, node.contents[1].text, diff --git a/lua/mind/init.lua b/lua/mind/init.lua index a973821..afea03b 100644 --- a/lua/mind/init.lua +++ b/lua/mind/init.lua @@ -1,83 +1,59 @@ -local mind_commands = require'mind.commands' -local mind_highlight = require'mind.highlight' -local mind_keymap = require'mind.keymap' -local mind_node = require'mind.node' -local mind_state = require'mind.state' -local mind_ui = require'mind.ui' -local notify = require'mind.notify'.notify -local path = require'plenary.path' +local mind_commands = require("mind.commands") +local mind_highlight = require("mind.highlight") +local mind_keymap = require("mind.keymap") +local mind_node = require("mind.node") +local mind_state = require("mind.state") +local mind_ui = require("mind.ui") +local notify = require("mind.notify").notify +local path = require("plenary.path") local M = {} local function toggle_main() if mind_ui.render_cache and mind_ui.render_cache.bufnr then - require 'mind'.close() + require("mind").close() else - require 'mind'.open_main() + require("mind").open_main() end end local function create_user_commands() - vim.api.nvim_create_user_command( - 'MindToggleMain', - function() - toggle_main() - end, - { desc = 'Toggle the main Mind tree', } - ) - - vim.api.nvim_create_user_command( - 'MindOpenMain', - function() - require'mind'.open_main() - end, - { desc = 'Open the main Mind tree', } - ) - - vim.api.nvim_create_user_command( - 'MindOpenProject', - function(opts) - require'mind'.open_project(opts.fargs[1] == 'global') - end, - { - nargs = '?', - desc = 'Open the project Mind tree', - } - ) - - vim.api.nvim_create_user_command( - 'MindOpenSmartProject', - function() - require'mind'.open_smart_project() - end, - { - desc = 'Open the project Mind tree', - } - ) - - vim.api.nvim_create_user_command( - 'MindReloadState', - function() - require'mind'.reload_state() - end, - { - desc = 'Reload Mind internal state', - } - ) - - vim.api.nvim_create_user_command( - 'MindClose', - function(opts) - require'mind'.close() - end, - { - desc = 'Close main or project Mind tree if open', - } - ) + vim.api.nvim_create_user_command("MindToggleMain", function() + toggle_main() + end, { desc = "Toggle the main Mind tree" }) + + vim.api.nvim_create_user_command("MindOpenMain", function() + require("mind").open_main() + end, { desc = "Open the main Mind tree" }) + + vim.api.nvim_create_user_command("MindOpenProject", function(opts) + require("mind").open_project(opts.fargs[1] == "global") + end, { + nargs = "?", + desc = "Open the project Mind tree", + }) + + vim.api.nvim_create_user_command("MindOpenSmartProject", function() + require("mind").open_smart_project() + end, { + desc = "Open the project Mind tree", + }) + + vim.api.nvim_create_user_command("MindReloadState", function() + require("mind").reload_state() + end, { + desc = "Reload Mind internal state", + }) + + vim.api.nvim_create_user_command("MindClose", function(opts) + require("mind").close() + end, { + desc = "Close main or project Mind tree if open", + }) end M.setup = function(opts) - M.opts = vim.tbl_deep_extend('force', require'mind.defaults', opts or {}) + M.opts = vim.tbl_deep_extend("force", require("mind.defaults"), opts or {}) -- ensure the paths are expanded mind_state.expand_opts_paths(M.opts) diff --git a/lua/mind/ui.lua b/lua/mind/ui.lua index c75d94d..e34937e 100644 --- a/lua/mind/ui.lua +++ b/lua/mind/ui.lua @@ -216,16 +216,20 @@ M.open_window = function(opts) bufnr = M.render_cache.bufnr else bufnr = vim.api.nvim_create_buf(false, false) - vim.api.nvim_buf_set_option(bufnr, 'filetype', 'mind') - vim.api.nvim_buf_set_option(bufnr, 'buftype', 'nofile') + vim.api.nvim_set_option_value("filetype", "mind", { buf = bufnr }) + vim.api.nvim_set_option_value("buftype", "nofile", { buf = bufnr }) -- window - vim.api.nvim_exec("vsp", false) - vim.api.nvim_exec("wincmd H", false) + vim.api.nvim_exec2("vsp", { output = false }) + if opts.ui.position == "right" then + vim.api.nvim_exec2("wincmd L", { output = false }) + elseif opts.ui.position == "left" then + vim.api.nvim_exec2("wincmd H", { output = false }) + end vim.api.nvim_win_set_width(0, opts.ui.width) vim.api.nvim_win_set_buf(0, bufnr) - vim.api.nvim_win_set_option(0, 'nu', false) - vim.api.nvim_win_set_option(0, 'rnu', false) + vim.api.nvim_set_option_value("nu", false, { win = 0 }) + vim.api.nvim_set_option_value("rnu", false, { win = 0 }) end return bufnr @@ -235,17 +239,24 @@ end M.render = function(tree, bufnr, opts) local lines, hls = render_tree(tree, opts) - vim.api.nvim_buf_set_option(bufnr, 'modifiable', true) + vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr }) -- set the lines for the whole buffer, replacing everything vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines) -- apply all the highlights at once for _, hl in ipairs(hls) do - vim.api.nvim_buf_add_highlight(bufnr, 0, hl.group, hl.line, hl.col_start, hl.col_end) + vim.api.nvim_buf_add_highlight( + bufnr, + 0, + hl.group, + hl.line, + hl.col_start, + hl.col_end + ) end - vim.api.nvim_buf_set_option(bufnr, 'modifiable', false) + vim.api.nvim_set_option_value("modifiable", false, { buf = bufnr }) M.render_cache = { tree_uid = tree.uid, bufnr = bufnr } end @@ -279,8 +290,8 @@ end -- Run a command by asking for confirmation before. If the answer is 'y', run the command, otherwise abort. M.with_confirmation = function(prompt, f) - vim.ui.input({ prompt = prompt .. ' (y/N) ' }, function(input) - if (input ~= nil and input == 'y') then + vim.ui.input({ prompt = prompt .. " (y/N) " }, function(input) + if input ~= nil and string.lower(input) == "y" then f() end end)