Skip to content

Commit

Permalink
refactor: replaced deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoPinelli committed Jan 27, 2024
1 parent 0e1774d commit a36a704
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 80 deletions.
10 changes: 8 additions & 2 deletions lua/mind/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
110 changes: 43 additions & 67 deletions lua/mind/init.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
33 changes: 22 additions & 11 deletions lua/mind/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a36a704

Please sign in to comment.