diff --git a/lua/mind/commands.lua b/lua/mind/commands.lua index 71f6359..e59ccff 100644 --- a/lua/mind/commands.lua +++ b/lua/mind/commands.lua @@ -2,12 +2,12 @@ local M = {} -local mind_data = require'mind.data' -local mind_indexing = require'mind.indexing' -local mind_keymap = require'mind.keymap' -local mind_node = require'mind.node' -local mind_ui = require'mind.ui' -local notify = require'mind.notify'.notify +local mind_data = require("mind.data") +local mind_indexing = require("mind.indexing") +local mind_keymap = require("mind.keymap") +local mind_node = require("mind.node") +local mind_ui = require("mind.ui") +local notify = require("mind.notify").notify M.commands = { toggle_node = function(args) @@ -24,23 +24,48 @@ M.commands = { end, add_above = function(args) - M.create_node_cursor(args.get_tree(), mind_node.MoveDir.ABOVE, args.save_tree, args.opts) + M.create_node_cursor( + args.get_tree(), + mind_node.MoveDir.ABOVE, + args.save_tree, + args.opts + ) end, add_below = function(args) - M.create_node_cursor(args.get_tree(), mind_node.MoveDir.BELOW, args.save_tree, args.opts) + M.create_node_cursor( + args.get_tree(), + mind_node.MoveDir.BELOW, + args.save_tree, + args.opts + ) end, add_inside_start = function(args) - M.create_node_cursor(args.get_tree(), mind_node.MoveDir.INSIDE_START, args.save_tree, args.opts) + M.create_node_cursor( + args.get_tree(), + mind_node.MoveDir.INSIDE_START, + args.save_tree, + args.opts + ) end, add_inside_end = function(args) - M.create_node_cursor(args.get_tree(), mind_node.MoveDir.INSIDE_END, args.save_tree, args.opts) + M.create_node_cursor( + args.get_tree(), + mind_node.MoveDir.INSIDE_END, + args.save_tree, + args.opts + ) end, add_inside_end_index = function(args) - M.create_node_index(args.get_tree(), mind_node.MoveDir.INSIDE_END, args.save_tree, args.opts) + M.create_node_index( + args.get_tree(), + mind_node.MoveDir.INSIDE_END, + args.save_tree, + args.opts + ) end, delete = function(args) @@ -97,19 +122,39 @@ M.commands = { end, move_above = function(args) - M.move_node_selected_cursor(args.get_tree(), mind_node.MoveDir.ABOVE, args.save_tree, args.opts) + M.move_node_selected_cursor( + args.get_tree(), + mind_node.MoveDir.ABOVE, + args.save_tree, + args.opts + ) end, move_below = function(args) - M.move_node_selected_cursor(args.get_tree(), mind_node.MoveDir.BELOW, args.save_tree, args.opts) + M.move_node_selected_cursor( + args.get_tree(), + mind_node.MoveDir.BELOW, + args.save_tree, + args.opts + ) end, move_inside_start = function(args) - M.move_node_selected_cursor(args.get_tree(), mind_node.MoveDir.INSIDE_START, args.save_tree, args.opts) + M.move_node_selected_cursor( + args.get_tree(), + mind_node.MoveDir.INSIDE_START, + args.save_tree, + args.opts + ) end, move_inside_end = function(args) - M.move_node_selected_cursor(args.get_tree(), mind_node.MoveDir.INSIDE_END, args.save_tree, args.opts) + M.move_node_selected_cursor( + args.get_tree(), + mind_node.MoveDir.INSIDE_END, + args.save_tree, + args.opts + ) end, } @@ -136,7 +181,7 @@ M.open_data = function(tree, node, directory, save_tree, opts) should_expand ) - if (data == nil) then + if data == nil then return end @@ -150,9 +195,9 @@ M.open_data = function(tree, node, directory, save_tree, opts) local winnr for _, tabpage_winnr in ipairs(vim.api.nvim_tabpage_list_wins(0)) do local bufnr = vim.api.nvim_win_get_buf(tabpage_winnr) - local buftype = vim.api.nvim_buf_get_option(bufnr, 'buftype') + local buftype = vim.api.nvim_get_option_value("buftype", { buf = bufnr }) - if buftype == '' then + if buftype == "" then winnr = tabpage_winnr break end @@ -160,23 +205,23 @@ M.open_data = function(tree, node, directory, save_tree, opts) -- pick the first window in the list; if it’s empty, we open a new one if winnr == nil then - vim.api.nvim_exec('rightb vsp ' .. data, false) + vim.api.nvim_exec2("rightb vsp " .. data, { output = false }) else vim.api.nvim_set_current_win(winnr) - vim.api.nvim_exec('e ' .. data, false) + vim.api.nvim_exec2("e " .. data, { output = false }) end - if opts.ui.close_on_file_open == true then - M.close() - end + if opts.ui.close_on_file_open == true then + M.close() + end end -- Delete the data file associated with a node. -- -- If it doesn’t exist, does nothing. M.delete_data = function(tree, node, save_tree, opts) - if (node.data == nil) then - notify('no files associated to this node', vim.log.levels.ERROR) + if node.data == nil then + notify("no files associated to this node", vim.log.levels.ERROR) return else mind_ui.with_confirmation("Delete file?", function() @@ -196,8 +241,8 @@ end M.open_data_line = function(tree, line, directory, save_tree, opts) local node = mind_node.get_node_by_line(tree, line) - if (node == nil) then - notify('cannot open data; no node', vim.log.levels.ERROR) + if node == nil then + notify("cannot open data; no node", vim.log.levels.ERROR) return end @@ -217,8 +262,8 @@ end M.delete_data_line = function(tree, line, save_tree, opts) local node = mind_node.get_node_by_line(tree, line) - if (node == nil) then - notify('cannot delete data; no node', vim.log.levels.ERROR) + if node == nil then + notify("cannot delete data; no node", vim.log.levels.ERROR) return end @@ -236,10 +281,12 @@ end M.open_data_index = function(tree, directory, save_tree, opts) mind_indexing.search_index( tree, - 'Open data / URL', + "Open data / URL", -- filter function function(node) - return opts.tree.automatic_data_creation or node.data ~= nil or node.url ~= nil + return opts.tree.automatic_data_creation + or node.data ~= nil + or node.url ~= nil end, -- sink function function(item) @@ -259,8 +306,11 @@ M.copy_node_link = function(node, reg, opts) local link = node.data or node.url if link ~= nil then - notify('link was copied') - vim.fn.setreg(reg or '"', string.format(opts.edit.copy_link_format or '%s', link)) + notify("link was copied") + vim.fn.setreg( + reg or '"', + string.format(opts.edit.copy_link_format or "%s", link) + ) end end @@ -281,7 +331,7 @@ end M.copy_node_link_index = function(tree, reg, opts) mind_indexing.search_index( tree, - 'Get a node link', + "Get a node link", -- filter function function(node) return node.data ~= nil or node.url ~= nil @@ -299,11 +349,11 @@ end -- For this to work, the node must not have any data associated with it. M.make_url_node = function(tree, node, save_tree, opts) if node.data ~= nil then - notify('cannot create URL node: data present', vim.log.levels.ERROR) + notify("cannot create URL node: data present", vim.log.levels.ERROR) return end - mind_ui.with_input('URL: ', 'https://', function(input) + mind_ui.with_input("URL: ", "https://", function(input) node.url = input save_tree() mind_ui.rerender(tree, opts) @@ -325,20 +375,20 @@ end -- Add a node as child of another node. M.create_node = function(tree, grand_parent, parent, node, dir, opts) - if (dir == mind_node.MoveDir.INSIDE_START) then + if dir == mind_node.MoveDir.INSIDE_START then mind_node.insert_node(parent, 1, node) - elseif (dir == mind_node.MoveDir.INSIDE_END) then + elseif dir == mind_node.MoveDir.INSIDE_END then mind_node.insert_node(parent, -1, node) - elseif (grand_parent ~= nil) then + elseif grand_parent ~= nil then local index = mind_node.find_parent_index(grand_parent, parent) - if (dir == mind_node.MoveDir.ABOVE) then + if dir == mind_node.MoveDir.ABOVE then mind_node.insert_node(grand_parent, index, node) - elseif (dir == mind_node.MoveDir.BELOW) then + elseif dir == mind_node.MoveDir.BELOW then mind_node.insert_node(grand_parent, index + 1, node) end else - notify('forbidden node creation', vim.log.levels.WARN) + notify("forbidden node creation", vim.log.levels.WARN) return end @@ -349,8 +399,8 @@ end M.create_node_line = function(tree, line, name, dir, save_tree, opts) local grand_parent, parent = mind_node.get_node_and_parent_by_line(tree, line) - if (parent == nil) then - notify('cannot create node on current line; no node', vim.log.levels.ERROR) + if parent == nil then + notify("cannot create node on current line; no node", vim.log.levels.ERROR) return end @@ -363,7 +413,7 @@ end -- Ask the user for input and the node in the tree at the given direction. M.create_node_cursor = function(tree, dir, save_tree, opts) mind_ui.with_cursor(function(line) - mind_ui.with_input('Node name: ', nil, function(input) + mind_ui.with_input("Node name: ", nil, function(input) M.create_node_line(tree, line, input, dir, save_tree, opts) end) end) @@ -373,12 +423,12 @@ end M.create_node_index = function(tree, dir, save_tree, opts) mind_indexing.search_index( tree, - 'Pick a node to create a new node in', + "Pick a node to create a new node in", -- filter function nil, -- sink function function(item) - mind_ui.with_input('Node name: ', nil, function(input) + mind_ui.with_input("Node name: ", nil, function(input) local node = mind_node.new_node(input) M.create_node(tree, item.parent, item.node, node, dir, opts) save_tree() @@ -392,23 +442,26 @@ end M.delete_node_line = function(tree, line, save_tree, opts) local parent, node = mind_node.get_node_and_parent_by_line(tree, line) - if (node == nil) then - notify('no node to delete', vim.log.levels.ERROR) + if node == nil then + notify("no node to delete", vim.log.levels.ERROR) return end - if (parent == nil) then - notify('cannot delete a node without parent', vim.log.levels.ERROR) + if parent == nil then + notify("cannot delete a node without parent", vim.log.levels.ERROR) return end local index = mind_node.find_parent_index(parent, node) - mind_ui.with_confirmation(string.format("Delete '%s'?", node.contents[1].text), function() - mind_node.delete_node(parent, index) - mind_ui.rerender(tree, opts) - save_tree() - end) + mind_ui.with_confirmation( + string.format("Delete '%s'?", node.contents[1].text), + function() + mind_node.delete_node(parent, index) + mind_ui.rerender(tree, opts) + save_tree() + end + ) end -- Delete the node under the cursor. @@ -420,7 +473,7 @@ end -- Rename a node. M.rename_node = function(tree, node, save_tree, opts) - mind_ui.with_input('Rename node: ', node.contents[1].text, function(input) + mind_ui.with_input("Rename node: ", node.contents[1].text, function(input) node.contents[1].text = input M.unselect_node() mind_ui.rerender(tree, opts) @@ -443,8 +496,8 @@ end -- Change the icon of a node. M.change_icon = function(tree, node, save_tree, opts) - mind_ui.with_input('Change icon: ', node.icon, function(input) - if input == ' ' then + mind_ui.with_input("Change icon: ", node.icon, function(input) + if input == " " then input = nil end @@ -469,25 +522,21 @@ end -- Change the icon of a node by selecting through the list of preset icons. M.change_icon_menu = function(tree, node, save_tree, opts) - local prompt = string.format('Pick an icon for %s', node.contents[1].text) + local prompt = string.format("Pick an icon for %s", node.contents[1].text) local format_item = function(item) - return string.format('%s: %s', item[1], item[2]) + return string.format("%s: %s", item[1], item[2]) end - vim.ui.select( - opts.ui.icon_preset, - { - prompt = prompt, - format_item = format_item - }, - function(item) - if item ~= nil then - node.icon = item[1] - save_tree() - mind_ui.rerender(tree, opts) - end + vim.ui.select(opts.ui.icon_preset, { + prompt = prompt, + format_item = format_item, + }, function(item) + if item ~= nil then + node.icon = item[1] + save_tree() + mind_ui.rerender(tree, opts) end - ) + end) end -- Change the icon of the node at a given line through the list of preset icons. @@ -523,10 +572,10 @@ end -- Select a node by path. M.select_node_path = function(tree, opts) - mind_ui.with_input('Path: /', nil, function(input) + mind_ui.with_input("Path: /", nil, function(input) local parent, node = mind_node.get_node_by_path( tree, - '/' .. input, + "/" .. input, opts.tree.automatic_creation ) @@ -538,7 +587,7 @@ end -- Unselect any selected node in the tree. M.unselect_node = function() - if (M.selected ~= nil) then + if M.selected ~= nil then M.selected.node.is_selected = nil M.selected = nil @@ -552,9 +601,9 @@ end -- something, you need to toggle the currently selected node. M.toggle_select_node_cursor = function(tree, opts) mind_ui.with_cursor(function(line) - if (M.selected ~= nil) then + if M.selected ~= nil then local node = mind_node.get_node_by_line(tree, line) - if (node == M.selected.node) then + if node == M.selected.node then M.unselect_node() mind_ui.rerender(tree, opts) else @@ -577,63 +626,63 @@ M.move_node = function( dir, opts ) - if (source_node == nil) then - notify('cannot move; no source node', vim.log.levels.WARN) + if source_node == nil then + notify("cannot move; no source node", vim.log.levels.WARN) return end - if (target_node == nil) then - notify('cannot move; no target node', vim.log.levels.WARN) + if target_node == nil then + notify("cannot move; no target node", vim.log.levels.WARN) return end -- if we move in the same tree, we can optimize - if (source_parent == target_parent) then + if source_parent == target_parent then -- compute the index of the nodes to move local source_i local target_i for k, child in ipairs(source_parent.children) do - if (child == target_node) then + if child == target_node then target_i = k - elseif (child == source_node) then + elseif child == source_node then source_i = k end - if (target_i ~= nil and source_i ~= nil) then + if target_i ~= nil and source_i ~= nil then break end end - if (target_i == nil or source_i == nil) then + if target_i == nil or source_i == nil then -- trying to move inside itsefl; abort M.unselect_node() mind_ui.rerender(tree, opts) return end - if (target_i == source_i) then + if target_i == source_i then -- same node; aborting - notify('not moving; source and target are the same node') + notify("not moving; source and target are the same node") M.unselect_node() mind_ui.rerender(tree, opts) return end - if (dir == mind_node.MoveDir.BELOW) then + if dir == mind_node.MoveDir.BELOW then mind_node.move_source_target_same_tree( source_parent, source_i, target_i + 1 ) - elseif (dir == mind_node.MoveDir.ABOVE) then + elseif dir == mind_node.MoveDir.ABOVE then mind_node.move_source_target_same_tree(source_parent, source_i, target_i) else -- we move inside, so first remove the node mind_node.delete_node(source_parent, source_i) - if (dir == mind_node.MoveDir.INSIDE_START) then + if dir == mind_node.MoveDir.INSIDE_START then mind_node.insert_node(target_node, 1, source_node) - elseif (dir == mind_node.MoveDir.INSIDE_END) then + elseif dir == mind_node.MoveDir.INSIDE_END then mind_node.insert_node(target_node, -1, source_node) end end @@ -645,13 +694,13 @@ M.move_node = function( -- then insert the previously deleted node in the new tree local target_i = mind_node.find_parent_index(target_parent, target_node) - if (dir == mind_node.MoveDir.BELOW) then + if dir == mind_node.MoveDir.BELOW then mind_node.insert_node(target_parent, target_i + 1, source_node) - elseif (dir == mind_node.MoveDir.ABOVE) then + elseif dir == mind_node.MoveDir.ABOVE then mind_node.insert_node(target_parent, target_i, source_node) - elseif (dir == mind_node.MoveDir.INSIDE_START) then + elseif dir == mind_node.MoveDir.INSIDE_START then mind_node.insert_node(target_node, 1, source_node) - elseif (dir == mind_node.MoveDir.INSIDE_END) then + elseif dir == mind_node.MoveDir.INSIDE_END then mind_node.insert_node(target_node, -1, source_node) end end @@ -662,8 +711,8 @@ end -- Move a selected node into a node at the given line. M.move_node_selected_line = function(tree, line, dir, save_tree, opts) - if (M.selected == nil) then - notify('cannot move; no selected node', vim.log.levels.ERROR) + if M.selected == nil then + notify("cannot move; no selected node", vim.log.levels.ERROR) M.unselect_node() mind_ui.rerender(tree, opts) return @@ -671,22 +720,14 @@ M.move_node_selected_line = function(tree, line, dir, save_tree, opts) local parent, node = mind_node.get_node_and_parent_by_line(tree, line) - if (parent == nil) then - notify('cannot move root', vim.log.levels.ERROR) + if parent == nil then + notify("cannot move root", vim.log.levels.ERROR) M.unselect_node() mind_ui.rerender(tree, opts) return end - M.move_node( - tree, - M.selected.parent, - M.selected.node, - parent, - node, - dir, - opts - ) + M.move_node(tree, M.selected.parent, M.selected.node, parent, node, dir, opts) save_tree() end @@ -706,7 +747,7 @@ M.toggle_node = function(tree, node, save_tree, opts) end -- Toggle (expand / collapse) a node at a given line. -M.toggle_node_line = function(tree, line, save_tree,opts) +M.toggle_node_line = function(tree, line, save_tree, opts) local node = mind_node.get_node_by_line(tree, line) M.toggle_node(tree, node, save_tree, opts) end @@ -736,16 +777,13 @@ M.open_tree = function(get_tree, data_dir, save_tree, opts) -- ensure that we close the tree if the window gets closed local id - id = vim.api.nvim_create_autocmd( - { 'WinClosed' }, - { - buffer = bufnr, - callback = function() - vim.api.nvim_del_autocmd(id) - M.close() - end - } - ) + id = vim.api.nvim_create_autocmd({ "WinClosed" }, { + buffer = bufnr, + callback = function() + vim.api.nvim_del_autocmd(id) + M.close() + end, + }) -- tree mind_ui.render(get_tree(), bufnr, opts) @@ -773,10 +811,10 @@ end -- name is a string. M.precompute_commands = function() for key, c in pairs(mind_keymap.keymaps.normal) do - if type(c) == 'string' then + if type(c) == "string" then local cmd = M.commands[mind_keymap.keymaps.normal[key]] - if (cmd ~= nil) then + if cmd ~= nil then mind_keymap.keymaps.normal[key] = cmd end elseif c == false then @@ -786,10 +824,10 @@ M.precompute_commands = function() end for key, c in pairs(mind_keymap.keymaps.selection) do - if type(c) == 'string' then + if type(c) == "string" then local cmd = M.commands[mind_keymap.keymaps.selection[key]] - if (cmd ~= nil) then + if cmd ~= nil then mind_keymap.keymaps.selection[key] = cmd end elseif c == false then diff --git a/lua/mind/data.lua b/lua/mind/data.lua index b6bcb36..2a42b78 100644 --- a/lua/mind/data.lua +++ b/lua/mind/data.lua @@ -2,8 +2,8 @@ local M = {} -local notify = require'mind.notify'.notify -local path = require'plenary.path' +local notify = require("mind.notify").notify +local path = require("plenary.path") -- Create a new random file in a given directory. -- @@ -16,20 +16,20 @@ M.new_data_file = function(dir, name, extension, content, should_expand) end -- filter the name - name = name:gsub('[^%w-]', ' ') -- remove anything that is not a word or a dash - name = name:gsub('%s+', '-') -- replace consecutive spaces with a single one - name = name:gsub('-+', '-') -- replace consecutive dashes with a single one - name = name:gsub('^-', '') -- remove leading dash - name = name:gsub('-$', '') -- remove trailing dash + name = name:gsub("[^%w-]", " ") -- remove anything that is not a word or a dash + name = name:gsub("%s+", "-") -- replace consecutive spaces with a single one + name = name:gsub("-+", "-") -- replace consecutive dashes with a single one + name = name:gsub("^-", "") -- remove leading dash + name = name:gsub("-$", "") -- remove trailing dash - local filename = vim.fn.strftime('%Y%m%d%H%M%S-') .. name .. extension + local filename = vim.fn.strftime("%Y%m%d%H%M%S-") .. name .. extension local p = path:new(dir, filename) local file_path = (should_expand and p:expand()) or tostring(p) - local file = io.open(file_path, 'w') + local file = io.open(file_path, "w") - if (file == nil) then - notify('cannot open data file: ' .. file_path) + if file == nil then + notify("cannot open data file: " .. file_path) return nil end @@ -44,5 +44,4 @@ M.delete_data_file = function(file_path) os.remove(file_path) end - return M diff --git a/lua/mind/defaults.lua b/lua/mind/defaults.lua index c22f991..8da207d 100644 --- a/lua/mind/defaults.lua +++ b/lua/mind/defaults.lua @@ -3,11 +3,11 @@ -- URL opener depends on the platform. local sysname = vim.loop.os_uname().sysname local url_open -if sysname == 'Linux' then - url_open = 'xdg-open' -elseif sysname == 'Darwin' then - url_open = 'open' -elseif sysname == 'Windows' then +if sysname == "Linux" then + url_open = "xdg-open" +elseif sysname == "Darwin" then + url_open = "open" +elseif sysname == "Windows" then url_open = 'start ""' end @@ -15,22 +15,22 @@ return { -- persistence, both for the tree state and data files persistence = { -- path where the global mind tree is stored - state_path = '~/.local/share/mind.nvim/mind.json', + state_path = "~/.local/share/mind.nvim/mind.json", -- directory where to create global data files - data_dir = '~/.local/share/mind.nvim/data', + data_dir = "~/.local/share/mind.nvim/data", }, -- edit options edit = { -- file extension to use when creating a data file - data_extension = '.md', + data_extension = ".md", -- default header to put in newly created data files - data_header = '# %s', + data_header = "# %s", -- format string for copied links - copy_link_format = '[](%s)' + copy_link_format = "[](%s)", }, -- tree options @@ -51,119 +51,124 @@ return { -- default width of the tree view window width = 30, + -- split to open window in + position = "left", + -- should mind window be closed when a file is opened close_on_file_open = false, -- marker used for empty indentation - empty_indent_marker = '│', + empty_indent_marker = "│", -- marker used for node indentation - node_indent_marker = '└', + node_indent_marker = "└", -- marker used to identify the root of the tree (left to its name) - root_marker = ' ', + root_marker = " ", -- marker used to identify a local root (right to its name) - local_marker = 'local', + local_marker = "local", -- marker used to show that a node has an associated data file - data_marker = ' ', + data_marker = " ", -- marker used to show that a node has an URL - url_marker = ' ', + url_marker = " ", -- marker used to show that a node is currently selected - select_marker = '', + select_marker = "", -- highlight options highlight = { -- highlight used on closed marks - closed_marker = 'LineNr', + closed_marker = "LineNr", -- highlight used on open marks - open_marker = 'LineNr', + open_marker = "LineNr", -- highlight used on the name of the root node - node_root = 'Function', + node_root = "Function", -- highlight used on regular nodes with no children - node_leaf = 'String', + node_leaf = "String", -- highlight used on regular nodes with children - node_parent = 'Title', + node_parent = "Title", -- highlight used on the local marker - local_marker = 'Comment', + local_marker = "Comment", -- highlight used on the data marker - data_marker = 'Comment', + data_marker = "Comment", -- highlight used on the url marker - url_marker = 'Comment', + url_marker = "Comment", -- highlight used on empty nodes (i.e. no children and no data) - modifier_empty = 'Comment', + modifier_empty = "Comment", -- highlight used on the selection marker - select_marker = 'Error', + select_marker = "Error", }, -- preset of icons icon_preset = { - { ' ', 'Sub-project' }, - { ' ', 'Journal, newspaper, weekly and daily news' }, - { ' ', 'For when you have an idea' }, - { ' ', 'Note taking?' }, - { '陼', 'Task management' }, - { ' ', 'Uncheck, empty square or backlog' }, - { ' ', 'Full square or on-going' }, - { ' ', 'Check or done' }, - { ' ', 'Trash bin, deleted, cancelled, etc.' }, - { ' ', 'GitHub' }, - { ' ', 'Monitoring' }, - { ' ', 'Internet, Earth, everyone!' }, - { ' ', 'Frozen, on-hold' }, - } + { " ", "Sub-project" }, + { " ", "Journal, newspaper, weekly and daily news" }, + { " ", "For when you have an idea" }, + { " ", "Note taking?" }, + { "陼", "Task management" }, + { " ", "Uncheck, empty square or backlog" }, + { " ", "Full square or on-going" }, + { " ", "Check or done" }, + { " ", "Trash bin, deleted, cancelled, etc." }, + { " ", "GitHub" }, + { " ", "Monitoring" }, + { " ", "Internet, Earth, everyone!" }, + { " ", "Frozen, on-hold" }, + { " ", "Data, knowledge" }, + { " ", "Neovim" }, + }, }, -- default keymaps; see 'mind.commands' for a list of commands that can be mapped to keys here keymaps = { -- keybindings when navigating the tree normally normal = { - [''] = 'open_data', - [''] = 'open_data_index', - [''] = 'toggle_node', - [''] = 'toggle_parent', - ['/'] = 'select_path', - ['$'] = 'change_icon_menu', - c = 'add_inside_end_index', - I = 'add_inside_start', - i = 'add_inside_end', - l = 'copy_node_link', - L = 'copy_node_link_index', - d = 'delete', - D = 'delete_file', - O = 'add_above', - o = 'add_below', - q = 'quit', - r = 'rename', - R = 'change_icon', - u = 'make_url', - x = 'select', + [""] = "open_data", + [""] = "open_data_index", + [""] = "toggle_node", + [""] = "toggle_parent", + ["/"] = "select_path", + ["$"] = "change_icon_menu", + c = "add_inside_end_index", + I = "add_inside_start", + i = "add_inside_end", + l = "copy_node_link", + L = "copy_node_link_index", + d = "delete", + D = "delete_file", + O = "add_above", + o = "add_below", + q = "quit", + r = "rename", + R = "change_icon", + u = "make_url", + x = "select", }, -- keybindings when a node is selected selection = { - [''] = 'open_data', - [''] = 'toggle_node', - [''] = 'toggle_parent', - ['/'] = 'select_path', - I = 'move_inside_start', - i = 'move_inside_end', - O = 'move_above', - o = 'move_below', - q = 'quit', - x = 'select', + [""] = "open_data", + [""] = "toggle_node", + [""] = "toggle_parent", + ["/"] = "select_path", + I = "move_inside_start", + i = "move_inside_end", + O = "move_above", + o = "move_below", + q = "quit", + x = "select", }, - } + }, } diff --git a/lua/mind/highlight.lua b/lua/mind/highlight.lua index 8c2eeed..640d167 100644 --- a/lua/mind/highlight.lua +++ b/lua/mind/highlight.lua @@ -4,34 +4,74 @@ local M = {} M.create_highlight_groups = function(opts) -- highlight used on closed marks - vim.api.nvim_set_hl(0, 'MindClosedMarker', { default = true, link = opts.ui.highlight.closed_marker }) + vim.api.nvim_set_hl( + 0, + "MindClosedMarker", + { default = true, link = opts.ui.highlight.closed_marker } + ) -- highlight used on open marks - vim.api.nvim_set_hl(0, 'MindOpenMarker', { default = true, link = opts.ui.highlight.open_marker }) + vim.api.nvim_set_hl( + 0, + "MindOpenMarker", + { default = true, link = opts.ui.highlight.open_marker } + ) -- highlight used on the name of the root node - vim.api.nvim_set_hl(0, 'MindNodeRoot', { default = true, link = opts.ui.highlight.node_root }) + vim.api.nvim_set_hl( + 0, + "MindNodeRoot", + { default = true, link = opts.ui.highlight.node_root } + ) -- highlight used on regular nodes with no children - vim.api.nvim_set_hl(0, 'MindNodeLeaf', { default = true, link = opts.ui.highlight.node_leaf }) + vim.api.nvim_set_hl( + 0, + "MindNodeLeaf", + { default = true, link = opts.ui.highlight.node_leaf } + ) -- highlight used on regular nodes with children - vim.api.nvim_set_hl(0, 'MindNodeParent', { default = true, link = opts.ui.highlight.node_parent }) + vim.api.nvim_set_hl( + 0, + "MindNodeParent", + { default = true, link = opts.ui.highlight.node_parent } + ) -- highlight used on the local marker - vim.api.nvim_set_hl(0, 'MindLocalMarker', { default = true, link = opts.ui.highlight.local_marker }) + vim.api.nvim_set_hl( + 0, + "MindLocalMarker", + { default = true, link = opts.ui.highlight.local_marker } + ) -- highlight used on the data marker - vim.api.nvim_set_hl(0, 'MindDataMarker', { default = true, link = opts.ui.highlight.data_marker }) + vim.api.nvim_set_hl( + 0, + "MindDataMarker", + { default = true, link = opts.ui.highlight.data_marker } + ) -- highlight used on the URL marker - vim.api.nvim_set_hl(0, 'MindURLMarker', { default = true, link = opts.ui.highlight.url_marker }) + vim.api.nvim_set_hl( + 0, + "MindURLMarker", + { default = true, link = opts.ui.highlight.url_marker } + ) -- highlight used on empty nodes (i.e. no children and no data) - vim.api.nvim_set_hl(0, 'MindModifierEmpty', { default = true, link = opts.ui.highlight.modifier_empty }) + vim.api.nvim_set_hl( + 0, + "MindModifierEmpty", + { default = true, link = opts.ui.highlight.modifier_empty } + ) -- highlight used on the selection marker - vim.api.nvim_set_hl(0, 'MindSelectMarker', { default = true, link = opts.ui.highlight.select_marker }) + vim.api.nvim_set_hl( + 0, + "MindSelectMarker", + { default = true, link = opts.ui.highlight.select_marker } + ) end return M diff --git a/lua/mind/indexing.lua b/lua/mind/indexing.lua index 10864f9..900c57a 100644 --- a/lua/mind/indexing.lua +++ b/lua/mind/indexing.lua @@ -9,14 +9,18 @@ M.index = {} -- Index a node and its children M.index_node = function(parent_path, parent, node, filter, opts) - local path = string.format('%s%s', parent_path, node.type and '/' or node.contents[1].text) + local path = string.format( + "%s%s", + parent_path, + node.type and "/" or node.contents[1].text + ) if filter == nil or filter(node) then M.index[#M.index + 1] = { path = path, parent = parent, node = node } end if node.children ~= nil then - local child_path = node.type and path or path .. '/' + local child_path = node.type and path or path .. "/" for _, child in ipairs(node.children) do M.index_node(child_path, node, child, filter, opts) end @@ -26,13 +30,13 @@ end -- Index a whole tree. M.index_tree = function(tree, filter, opts) M.index = {} - M.index_node('', nil, tree, filter, opts) + M.index_node("", nil, tree, filter, opts) end -- Search through the index. M.search_index = function(tree, prompt, filter, f, opts) local format_item = function(item) - local prefix = '' + local prefix = "" if item.node.data ~= nil then prefix = opts.ui.data_marker @@ -45,11 +49,15 @@ M.search_index = function(tree, prompt, filter, f, opts) M.index_tree(tree, filter, opts) - vim.ui.select(M.index, { prompt = prompt, format_item = format_item }, function(item) - if item ~= nil then - f(item) + vim.ui.select( + M.index, + { prompt = prompt, format_item = format_item }, + function(item) + if item ~= nil then + f(item) + end end - end) + ) end return M diff --git a/lua/mind/init.lua b/lua/mind/init.lua index afea03b..190e903 100644 --- a/lua/mind/init.lua +++ b/lua/mind/init.lua @@ -76,54 +76,50 @@ end -- Open the main tree. M.open_main = function() - M.wrap_main_tree_fn( - function(args) - mind_commands.open_tree( - args.get_tree, - args.opts.persistence.data_dir, - function() mind_state.save_main_state(args.opts) end, - args.opts - ) - end, - M.opts - ) + M.wrap_main_tree_fn(function(args) + mind_commands.open_tree( + args.get_tree, + args.opts.persistence.data_dir, + function() + mind_state.save_main_state(args.opts) + end, + args.opts + ) + end, M.opts) end -- Open a project tree. -- -- If `use_global` is set to `true`, will use the global persistence location. M.open_project = function(use_global) - M.wrap_project_tree_fn( - function(args) - mind_commands.open_tree( - args.get_tree, - args.data_dir, - use_global - and function() mind_state.save_main_state(args.opts) end - or function() mind_state.save_local_state() end, - args.opts - ) - end, - use_global, - M.opts - ) + M.wrap_project_tree_fn(function(args) + mind_commands.open_tree( + args.get_tree, + args.data_dir, + use_global and function() + mind_state.save_main_state(args.opts) + end or function() + mind_state.save_local_state() + end, + args.opts + ) + end, use_global, M.opts) end -- Open a smart project tree. M.open_smart_project = function() - M.wrap_smart_project_tree_fn( - function(args, use_global) - mind_commands.open_tree( - args.get_tree, - args.data_dir, - use_global - and function() mind_state.save_main_state(args.opts) end - or function() mind_state.save_local_state() end, - args.opts - ) - end, - M.opts - ) + M.wrap_smart_project_tree_fn(function(args, use_global) + mind_commands.open_tree( + args.get_tree, + args.data_dir, + use_global and function() + mind_state.save_main_state(args.opts) + end or function() + mind_state.save_local_state() + end, + args.opts + ) + end, M.opts) end -- Load state. @@ -133,7 +129,7 @@ end -- Wrap a function call expecting the main tree. M.wrap_main_tree_fn = function(f, opts) - opts = vim.tbl_deep_extend('force', M.opts, opts or {}) + opts = vim.tbl_deep_extend("force", M.opts, opts or {}) -- load the main tree mind_state.load_main_state(opts) @@ -141,7 +137,9 @@ M.wrap_main_tree_fn = function(f, opts) local args = { get_tree = mind_state.get_main_tree, data_dir = opts.persistence.data_dir, - save_tree = function() mind_state.save_main_state(opts) end, + save_tree = function() + mind_state.save_main_state(opts) + end, opts = opts, } @@ -152,13 +150,13 @@ end -- -- If the project tree doesn’t exist, it is automatically created. M.wrap_project_tree_fn = function(f, use_global, opts) - opts = vim.tbl_deep_extend('force', M.opts, opts or {}) + opts = vim.tbl_deep_extend("force", M.opts, opts or {}) local cwd = vim.fn.getcwd() if use_global then mind_state.load_main_state(opts) - if mind_state.state.projects[cwd]== nil then + if mind_state.state.projects[cwd] == nil then mind_state.new_global_project_tree(cwd, opts) end else @@ -170,15 +168,21 @@ M.wrap_project_tree_fn = function(f, use_global, opts) end end - local save_tree = - use_global and function() mind_state.save_main_state(opts) end - or function() mind_state.save_local_state() end + local save_tree = use_global + and function() + mind_state.save_main_state(opts) + end + or function() + mind_state.save_local_state() + end local args = { - get_tree = function() return mind_state.get_project_tree(use_global and cwd or nil) end, + get_tree = function() + return mind_state.get_project_tree(use_global and cwd or nil) + end, data_dir = mind_state.get_project_data_dir(use_global, opts), save_tree = save_tree, - opts = opts + opts = opts, } f(args) @@ -188,19 +192,23 @@ end -- -- If a local tree exists, wrap the local tree. Otherwise, wrap a global tree. M.wrap_smart_project_tree_fn = function(f, opts) - opts = vim.tbl_deep_extend('force', M.opts, opts or {}) + opts = vim.tbl_deep_extend("force", M.opts, opts or {}) local cwd = vim.fn.getcwd() - local p = path:new(cwd, '.mind') + local p = path:new(cwd, ".mind") if p:exists() and p:is_dir() then -- load the local state mind_state.load_local_state() local args = { - get_tree = function() return mind_state.get_project_tree() end, + get_tree = function() + return mind_state.get_project_tree() + end, data_dir = mind_state.get_project_data_dir(false, opts), - save_tree = function() mind_state.save_local_state() end, + save_tree = function() + mind_state.save_local_state() + end, opts = opts, } @@ -214,47 +222,66 @@ M.wrap_smart_project_tree_fn = function(f, opts) if tree ~= nil then -- a global project tree exists, use that local args = { - get_tree = function() return mind_state.get_project_tree(cwd) end, + get_tree = function() + return mind_state.get_project_tree(cwd) + end, data_dir = mind_state.get_project_data_dir(true, opts), - save_tree = function() mind_state.save_main_state(opts) end, + save_tree = function() + mind_state.save_main_state(opts) + end, opts = opts, } f(args, true) else -- prompt the user whether they want a global or local tree - mind_ui.with_input('What kind of project tree? (local/global) ', 'local', function(input) - local get_tree - local save_tree - local use_global - - if input == 'local' then - mind_state.new_local_tree(cwd, opts) - get_tree = function() return mind_state.get_project_tree() end - save_tree = function() mind_state.save_local_state() end - use_global = false - elseif input == 'global' then - mind_state.new_global_project_tree(cwd, opts) - get_tree = function() return mind_state.get_project_tree(cwd) end - save_tree = function() mind_state.save_main_state(opts) end - use_global = true - end - - if get_tree == nil then - notify('unrecognized project tree type, aborting', vim.log.levels.WARN) - return + mind_ui.with_input( + "What kind of project tree? (local/global) ", + "local", + function(input) + local get_tree + local save_tree + local use_global + + if input == "local" then + mind_state.new_local_tree(cwd, opts) + get_tree = function() + return mind_state.get_project_tree() + end + save_tree = function() + mind_state.save_local_state() + end + use_global = false + elseif input == "global" then + mind_state.new_global_project_tree(cwd, opts) + get_tree = function() + return mind_state.get_project_tree(cwd) + end + save_tree = function() + mind_state.save_main_state(opts) + end + use_global = true + end + + if get_tree == nil then + notify( + "unrecognized project tree type, aborting", + vim.log.levels.WARN + ) + return + end + + local args = { + get_tree = get_tree, + data_dir = mind_state.get_project_data_dir(use_global, opts), + save_tree = save_tree, + opts = opts, + } + + f(args, true) + save_tree() end - - local args = { - get_tree = get_tree, - data_dir = mind_state.get_project_data_dir(use_global, opts), - save_tree = save_tree, - opts = opts, - } - - f(args, true) - save_tree() - end) + ) end end end diff --git a/lua/mind/keymap.lua b/lua/mind/keymap.lua index db7f4a3..f52ad14 100644 --- a/lua/mind/keymap.lua +++ b/lua/mind/keymap.lua @@ -2,7 +2,7 @@ local M = {} -local notify = require'mind.notify'.notify +local notify = require("mind.notify").notify -- Selector for keymap. -- @@ -10,8 +10,8 @@ local notify = require'mind.notify'.notify -- currently active keymap. The keymap contains user-defined keybindings that will then be resolved when the user -- presses their defined keys. M.KeymapSelector = { - NORMAL = 'normal', - SELECTION = 'selection', + NORMAL = "normal", + SELECTION = "selection", } -- Keymaps. @@ -64,22 +64,22 @@ M.insert_keymaps = function(bufnr, get_tree, data_dir, save_tree, opts) get_tree = get_tree, data_dir = data_dir, save_tree = save_tree, - opts = opts + opts = opts, } for key, _ in pairs(keyset) do - vim.keymap.set('n', key, function() + vim.keymap.set("n", key, function() local keymap = M.get_keymap() - if (keymap == nil) then - notify('no active keymap', vim.log.levels.WARN) + if keymap == nil then + notify("no active keymap", vim.log.levels.WARN) return end local cmd = keymap[key] - if (cmd == nil) then - notify('no command bound to ' .. tostring(key), vim.log.levels.WARN) + if cmd == nil then + notify("no command bound to " .. tostring(key), vim.log.levels.WARN) return end diff --git a/lua/mind/node.lua b/lua/mind/node.lua index d45840f..0e5158f 100644 --- a/lua/mind/node.lua +++ b/lua/mind/node.lua @@ -1,6 +1,6 @@ -- Node and trees operations. -local notify = require'mind.notify'.notify +local notify = require("mind.notify").notify local M = {} @@ -32,10 +32,10 @@ M.MoveDir = { M.new_node = function(name, children) return { contents = { - { text = name } + { text = name }, }, is_expanded = false, - children = children + children = children, } end @@ -44,18 +44,18 @@ end -- `i` is rank of the node we want to get. If i is 0, then parent and node are returned as result. If not, this function -- will recurse into node.children (if any) and if node.is_expanded is true. local function get_dfs(parent, node, i) - if (i == 0) then + if i == 0 then return parent, node, i end i = i - 1 - if (node.children ~= nil and node.is_expanded) then + if node.children ~= nil and node.is_expanded then for _, child in ipairs(node.children) do local p, n p, n, i = get_dfs(node, child, i) - if (n ~= nil) then + if n ~= nil then return p, n, i end end @@ -86,13 +86,13 @@ end -- -- The function stops when it arrives at the end of paths, that is, when i == #paths + 1. local function get_node_by_path_rec(parent, tree, paths, i, create) - if (i == #paths + 1) then + if i == #paths + 1 then return parent, tree end local segment = paths[i] - if (tree.children == nil) then + if tree.children == nil then if create then tree.children = { M.new_node(segment) } else @@ -103,7 +103,7 @@ local function get_node_by_path_rec(parent, tree, paths, i, create) -- look for the child which name is the same as paths[i] for _, child in ipairs(tree.children) do - if (child.contents[1].text == segment) then + if child.contents[1].text == segment then return get_node_by_path_rec(tree, child, paths, i + 1, create) end end @@ -122,14 +122,14 @@ end -- -- If `create` is set to `true`, nodes are created automatically if they don’t exist. M.get_node_by_path = function(tree, path, create) - if (path == '/') then + if path == "/" then return nil, tree end - local split_path = vim.split(path, '/') + local split_path = vim.split(path, "/") - if (split_path[1] ~= '') then - notify('path must start with a leading slash (/)', vim.log.levels.WARN) + if split_path[1] ~= "" then + notify("path must start with a leading slash (/)", vim.log.levels.WARN) return end @@ -163,8 +163,8 @@ end -- -- If i is negative, it starts after the end. M.delete_node = function(tree, i) - if (tree.children == nil) then - notify('cannot delete node; no children', vim.log.levels.ERROR) + if tree.children == nil then + notify("cannot delete node; no children", vim.log.levels.ERROR) return end @@ -176,7 +176,7 @@ M.delete_node = function(tree, i) tree.children[k] = tree.children[k + 1] end - if (#tree.children == 0) then + if #tree.children == 0 then tree.children = nil end end @@ -184,7 +184,7 @@ end -- Find the parent index of a node in its parent’s children. M.find_parent_index = function(tree, node) for i, child in ipairs(tree.children) do - if (child == node) then + if child == node then return i end end @@ -193,11 +193,11 @@ end -- Move a source node at a target node in the same tree. M.move_source_target_same_tree = function(tree, src, tgt) -- do nothing if src == tgt - if (src == tgt) then + if src == tgt then return end - if (tgt < src) then + if tgt < src then -- if we want to move src to tgt with target before source local prev = tree.children[src] diff --git a/lua/mind/notify.lua b/lua/mind/notify.lua index d5b205e..47f0cac 100644 --- a/lua/mind/notify.lua +++ b/lua/mind/notify.lua @@ -3,7 +3,7 @@ local M = {} M.notify = function(msg, lvl) - vim.notify(msg, lvl, { title = 'Mind', icon = '' }) + vim.notify(msg, lvl, { title = "Mind", icon = "" }) end return M diff --git a/lua/mind/state.lua b/lua/mind/state.lua index 9dde603..837790f 100644 --- a/lua/mind/state.lua +++ b/lua/mind/state.lua @@ -1,8 +1,8 @@ local M = {} -local path = require'plenary.path' -local notify = require'mind.notify'.notify -local mind_node = require'mind.node' +local path = require("plenary.path") +local notify = require("mind.notify").notify +local mind_node = require("mind.node") -- Expand opts paths. -- @@ -27,12 +27,12 @@ M.get_project_tree = function(cwd) end M.new_global_project_tree = function(cwd, opts) - notify('creating a new global project tree') + notify("creating a new global project tree") local tree = { - uid = vim.fn.strftime('%Y%m%d%H%M%S'), + uid = vim.fn.strftime("%Y%m%d%H%M%S"), contents = { - { text = cwd:match('^.*/(.+)$') }, + { text = cwd:match("^.*/(.+)$") }, }, type = mind_node.TreeType.ROOT, icon = opts.ui.root_marker, @@ -44,12 +44,12 @@ M.new_global_project_tree = function(cwd, opts) end M.new_local_tree = function(cwd, opts) - notify('creating a new local tree') + notify("creating a new local tree") M.local_tree = { - uid = vim.fn.strftime('%Y%m%d%H%M%S'), + uid = vim.fn.strftime("%Y%m%d%H%M%S"), contents = { - { text = cwd:match('^.*/(.+)$') }, + { text = cwd:match("^.*/(.+)$") }, }, type = mind_node.TreeType.LOCAL_ROOT, icon = opts.ui.root_marker, @@ -64,9 +64,9 @@ M.load_main_state = function(opts) M.state = { -- Main tree, used when no specific project is wanted. tree = { - uid = vim.fn.strftime('%Y%m%d%H%M%S'), + uid = vim.fn.strftime("%Y%m%d%H%M%S"), contents = { - { text = 'Main' }, + { text = "Main" }, }, type = mind_node.TreeType.ROOT, icon = opts.ui.root_marker, @@ -76,33 +76,33 @@ M.load_main_state = function(opts) projects = {}, } - if (opts.persistence.state_path == nil) then - notify('cannot load shit', vim.log.levels.ERROR) + if opts.persistence.state_path == nil then + notify("cannot load shit", vim.log.levels.ERROR) return end - local file = io.open(opts.persistence.state_path, 'r') + local file = io.open(opts.persistence.state_path, "r") - if (file ~= nil) then - local encoded = file:read('*a') + if file ~= nil then + local encoded = file:read("*a") file:close() - if (encoded ~= nil) then + if encoded ~= nil then M.state = vim.json.decode(encoded) end end -- ensure we have a UID - M.state.tree.uid = M.state.tree.uid or vim.fn.strftime('%Y%m%d%H%M%S') + M.state.tree.uid = M.state.tree.uid or vim.fn.strftime("%Y%m%d%H%M%S") for _, tree in ipairs(M.state.projects) do - tree.uid = tree.uid or vim.fn.strftime('%Y%m%d%H%M%S') + tree.uid = tree.uid or vim.fn.strftime("%Y%m%d%H%M%S") end end -- Save the main state. M.save_main_state = function(opts) - if (opts.persistence.state_path == nil) then + if opts.persistence.state_path == nil then return end @@ -113,11 +113,11 @@ M.save_main_state = function(opts) state_path:touch({ parents = true }) end - local file = io.open(opts.persistence.state_path, 'w') + local file = io.open(opts.persistence.state_path, "w") - if (file == nil) then + if file == nil then notify( - string.format('cannot save main state at %s', opts.persistence.state_path), + string.format("cannot save main state at %s", opts.persistence.state_path), vim.log.levels.ERROR ) else @@ -133,22 +133,22 @@ M.load_local_state = function() M.local_tree = nil local cwd = vim.fn.getcwd() - local local_mind = path:new(cwd, '.mind') - if (local_mind:is_dir()) then + local local_mind = path:new(cwd, ".mind") + if local_mind:is_dir() then -- we have a local mind; read the projects state from there - local file = io.open(path:new(cwd, '.mind', 'state.json'):expand(), 'r') + local file = io.open(path:new(cwd, ".mind", "state.json"):expand(), "r") - if (file == nil) then - notify('cannot open local Mind tree') + if file == nil then + notify("cannot open local Mind tree") else - local encoded = file:read('*a') + local encoded = file:read("*a") file:close() - if (encoded ~= nil) then + if encoded ~= nil then M.local_tree = vim.json.decode(encoded) -- ensure we have a UID - M.local_tree.uid = M.local_tree.uid or vim.fn.strftime('%Y%m%d%H%M%S') + M.local_tree.uid = M.local_tree.uid or vim.fn.strftime("%Y%m%d%H%M%S") M.local_cwd = cwd end @@ -162,25 +162,34 @@ M.save_local_state = function() local cwd = vim.fn.getcwd() if M.local_cwd and cwd ~= M.local_cwd then - notify('refusing to save local state: differs from when it was loaded', vim.log.levels.ERROR) - notify(string.format('hint: loaded as %s, try saving as %s', M.local_cwd, cwd), vim.log.levels.INFO) - notify(string.format('hint: cd to %s in order to save the tree', M.local_cwd), vim.log.levels.INFO) + notify( + "refusing to save local state: differs from when it was loaded", + vim.log.levels.ERROR + ) + notify( + string.format("hint: loaded as %s, try saving as %s", M.local_cwd, cwd), + vim.log.levels.INFO + ) + notify( + string.format("hint: cd to %s in order to save the tree", M.local_cwd), + vim.log.levels.INFO + ) return end - local local_mind = path:new(cwd, '.mind') + local local_mind = path:new(cwd, ".mind") -- ensure the path exists if not local_mind:exists() then local_mind:mkdir({ parents = true }) end - if (local_mind:is_dir()) then + if local_mind:is_dir() then -- we have a local mind - local file = io.open(path:new(cwd, '.mind', 'state.json'):expand(), 'w') + local file = io.open(path:new(cwd, ".mind", "state.json"):expand(), "w") - if (file == nil) then - notify(string.format('cannot save local project at %s', cwd), 4) + if file == nil then + notify(string.format("cannot save local project at %s", cwd), 4) else local encoded = vim.json.encode(M.local_tree) file:write(encoded) @@ -208,7 +217,7 @@ M.get_project_data_dir = function(use_global, opts) return opts.persistence.data_dir end - return '.mind/data' + return ".mind/data" end return M diff --git a/lua/mind/ui.lua b/lua/mind/ui.lua index e34937e..7388961 100644 --- a/lua/mind/ui.lua +++ b/lua/mind/ui.lua @@ -1,7 +1,7 @@ -- Everything relating to the UI. local M = {} -local mind_node = require'mind.node' +local mind_node = require("mind.node") -- A per-tree render cache. -- @@ -14,25 +14,25 @@ M.render_cache = {} -- Get the highlight group to use for a node given its status. local function node_hl(node) - if (node.type == mind_node.TreeType.ROOT) then - return 'MindNodeRoot' - elseif (node.type == mind_node.TreeType.LOCAL_ROOT) then - return 'MindNodeRoot' - elseif (node.children ~= nil) then - return 'MindNodeParent' + if node.type == mind_node.TreeType.ROOT then + return "MindNodeRoot" + elseif node.type == mind_node.TreeType.LOCAL_ROOT then + return "MindNodeRoot" + elseif node.children ~= nil then + return "MindNodeParent" else - return 'MindNodeLeaf' + return "MindNodeLeaf" end end -- Compute the text line to display for a given node local function node_to_line(node, opts) - local name = '' + local name = "" local partial_hls = {} local node_group = node_hl(node) -- the icon goes first - if (node.icon ~= nil) then + if node.icon ~= nil then name = node.icon partial_hls[#partial_hls + 1] = { group = node_group, @@ -46,56 +46,56 @@ local function node_to_line(node, opts) partial_hls[#partial_hls + 1] = { group = node_group, - width = #content.text + width = #content.text, } end -- special case for the first highlight: - if (node.type == nil) then - if (node.children ~= nil) then - partial_hls[#partial_hls - #node.contents + 1].group = 'MindNodeParent' - elseif (node.data == nil and node.url == nil) then - partial_hls[#partial_hls - #node.contents + 1].group = 'MindModifierEmpty' + if node.type == nil then + if node.children ~= nil then + partial_hls[#partial_hls - #node.contents + 1].group = "MindNodeParent" + elseif node.data == nil and node.url == nil then + partial_hls[#partial_hls - #node.contents + 1].group = "MindModifierEmpty" end end -- special marker for local roots - if (node.type == mind_node.TreeType.LOCAL_ROOT) then - local marker = ' ' .. opts.ui.local_marker + if node.type == mind_node.TreeType.LOCAL_ROOT then + local marker = " " .. opts.ui.local_marker name = name .. marker partial_hls[#partial_hls + 1] = { - group = 'MindLocalMarker', + group = "MindLocalMarker", width = #marker, } end -- special marker for data / URL nodes if node.data ~= nil then - local marker = ' ' .. opts.ui.data_marker + local marker = " " .. opts.ui.data_marker name = name .. marker partial_hls[#partial_hls + 1] = { - group = 'MindDataMarker', + group = "MindDataMarker", width = #marker, } elseif node.url ~= nil then - local marker = ' ' .. opts.ui.url_marker + local marker = " " .. opts.ui.url_marker name = name .. marker partial_hls[#partial_hls + 1] = { - group = 'MindURLMarker', + group = "MindURLMarker", width = #marker, } end -- special marker for selection - if (node.is_selected) then - local marker = ' ' .. opts.ui.select_marker + if node.is_selected then + local marker = " " .. opts.ui.select_marker name = name .. marker partial_hls[#partial_hls + 1] = { - group = 'MindSelectMarker', + group = "MindSelectMarker", width = #marker, } end @@ -114,12 +114,12 @@ local function render_node(node, indent, is_last, lines, hls, opts) line = indent indent = indent else - line = indent .. opts.ui.node_indent_marker .. ' ' - indent = indent .. ' ' + line = indent .. opts.ui.node_indent_marker .. " " + indent = indent .. " " end else - line = indent .. opts.ui.empty_indent_marker .. ' ' - indent = indent .. opts.ui.empty_indent_marker .. ' ' + line = indent .. opts.ui.empty_indent_marker .. " " + indent = indent .. opts.ui.empty_indent_marker .. " " end local name, partial_hls = node_to_line(node, opts) @@ -127,22 +127,22 @@ local function render_node(node, indent, is_last, lines, hls, opts) local hl_line = #lines hls[#hls + 1] = { - group = 'MindOpenMarker', + group = "MindOpenMarker", line = hl_line, col_start = 0, col_end = #line, } - if (node.children ~= nil) then - if (node.is_expanded) then - local mark = ' ' + if node.children ~= nil then + if node.is_expanded then + local mark = " " local hl_col_end = hl_col_start + #mark hls[#hls + 1] = { - group = 'MindOpenMarker', + group = "MindOpenMarker", line = hl_line, col_start = hl_col_start, - col_end = hl_col_end + col_end = hl_col_end, } lines[#lines + 1] = line .. mark .. name @@ -155,7 +155,7 @@ local function render_node(node, indent, is_last, lines, hls, opts) group = hl.group, line = hl_line, col_start = hl_col_start, - col_end = hl_col_end + col_end = hl_col_end, } end @@ -165,14 +165,14 @@ local function render_node(node, indent, is_last, lines, hls, opts) end render_node(node.children[#node.children], indent, true, lines, hls, opts) else - local mark = ' ' + local mark = " " local hl_col_end = hl_col_start + #mark hls[#hls + 1] = { - group = 'MindClosedMarker', + group = "MindClosedMarker", line = hl_line, col_start = hl_col_start, - col_end = hl_col_end + col_end = hl_col_end, } lines[#lines + 1] = line .. mark .. name @@ -184,7 +184,7 @@ local function render_node(node, indent, is_last, lines, hls, opts) group = hl.group, line = hl_line, col_start = hl_col_start, - col_end = hl_col_end + col_end = hl_col_end, } end end @@ -195,7 +195,12 @@ local function render_node(node, indent, is_last, lines, hls, opts) for _, hl in ipairs(partial_hls) do hl_col_start = hl_col_end hl_col_end = hl_col_start + hl.width - hls[#hls + 1] = { group = hl.group, line = hl_line, col_start = hl_col_start, col_end = hl_col_end } + hls[#hls + 1] = { + group = hl.group, + line = hl_line, + col_start = hl_col_start, + col_end = hl_col_end, + } end end end @@ -204,7 +209,7 @@ end local function render_tree(tree, opts) local lines = {} local hls = {} - render_node(tree, '', true, lines, hls, opts) + render_node(tree, "", true, lines, hls, opts) return lines, hls end @@ -282,7 +287,7 @@ end -- Ask the user for input with a given prompt and run the tree function on it. M.with_input = function(prompt, default, f) vim.ui.input({ prompt = prompt, default = default }, function(input) - if (input ~= nil) then + if input ~= nil then f(input) end end) diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..421c3d9 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,3 @@ +indent_type = "Spaces" +indent_width = 2 +column_width = 80