From de8ccfb9aa5391ba926d19edc2a0cebd026afcaa Mon Sep 17 00:00:00 2001 From: Carlos Gutierrez Date: Sat, 10 Jan 2026 22:34:10 -0500 Subject: [PATCH] fix: critical bugs and add documentation - Fix missing path separator in glob patterns (files were not found) - Fix nvim-tree setup overriding user config on every IdeaTree call - Fix deprecated nvim API (nvim_buf_set_option, nvim_win_set_option) - Fix missing arguments in sidebar.open() call in list.lua - Remove unused variable in tags.lua - Update help documentation with all commands - Fix README.md (telescope reference, add changelog link) - Add CHANGELOG.md - Add llms.txt for AI/LLM context --- CHANGELOG.md | 49 +++++++ README.md | 8 +- doc/ideaDrop.txt | 213 +++++++++++++++++++++++++++---- llms.txt | 111 ++++++++++++++++ lua/ideaDrop/features/list.lua | 5 +- lua/ideaDrop/features/search.lua | 6 +- lua/ideaDrop/features/tags.lua | 5 +- lua/ideaDrop/ui/sidebar.lua | 18 +-- lua/ideaDrop/ui/tree.lua | 149 ++++++++------------- 9 files changed, 425 insertions(+), 139 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 llms.txt diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..8f555fc --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,49 @@ +# Changelog + +All notable changes to ideaDrop.nvim will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Fixed + +- **Critical**: Fixed glob pattern bug where files were not being found due to missing path separator (`/`) between directory and pattern in `list.lua`, `tags.lua`, and `search.lua` +- **Critical**: Fixed nvim-tree integration that was overriding user's nvim-tree configuration on every `:IdeaTree` call. Now uses nvim-tree API directly without calling `setup()` +- Fixed deprecated Neovim API usage: replaced `vim.api.nvim_buf_set_option()` and `vim.api.nvim_win_set_option()` with `vim.bo[]` and `vim.wo[]` in `sidebar.lua` +- Fixed missing arguments in `sidebar.open()` call in `list.lua` which could cause unexpected behavior +- Removed unused variable in `tags.lua` (`filename` in `show_files_with_tag` function) + +### Changed + +- Updated help documentation (`doc/ideaDrop.txt`) to include all commands: `IdeaBuffer`, `IdeaRight`, `IdeaTree`, tag commands, and search commands +- Improved nvim-tree integration to preserve user's existing nvim-tree configuration + +### Added + +- Added `CHANGELOG.md` to track project changes +- Added `llms.txt` for AI/LLM context about the project + +## [1.0.0] - Initial Release + +### Added + +- Multiple view modes: floating window, current buffer, right-side buffer +- Smart tagging system with `#tag` format +- Advanced fuzzy search through titles and content +- nvim-tree integration for file browsing +- Markdown support with syntax highlighting +- Auto-save functionality +- Date-based file organization +- Nested folder support + +### Commands + +- `:Idea` - Open idea in floating window +- `:IdeaBuffer` - Open idea in current buffer +- `:IdeaRight` - Open idea in right-side buffer +- `:IdeaTree` - Open file tree browser +- `:IdeaTags` - Browse and filter by tags +- `:IdeaAddTag` / `:IdeaRemoveTag` - Manage tags +- `:IdeaSearch` / `:IdeaSearchContent` / `:IdeaSearchTitle` - Search functionality diff --git a/README.md b/README.md index 80b724a..134ce2d 100644 --- a/README.md +++ b/README.md @@ -215,9 +215,13 @@ This plugin is built with: - **Lua**: Core functionality - **Neovim API**: Native Neovim integration - **nvim-tree**: File tree browsing -- **telescope**: Search functionality +- **vim.ui.select**: Native picker for search and tag selection - **Markdown**: Rich text support +## 📋 Changelog + +See [CHANGELOG.md](CHANGELOG.md) for a detailed list of changes. + ## 📄 License MIT License - feel free to use this plugin in your own projects! @@ -239,7 +243,7 @@ Contributions are welcome! Please feel free to submit a Pull Request. 1. **Module not found errors**: Ensure all dependencies are installed 2. **Tree not opening**: Make sure nvim-tree is properly configured -3. **Search not working**: Verify telescope is installed and configured +3. **Search not working**: Verify your idea directory path is correct 4. **Tags not showing**: Check that your idea directory exists and contains markdown files ### Getting Help diff --git a/doc/ideaDrop.txt b/doc/ideaDrop.txt index 6852cdb..42e7f62 100644 --- a/doc/ideaDrop.txt +++ b/doc/ideaDrop.txt @@ -3,33 +3,55 @@ ============================================================================== INTRODUCTION *ideaDrop-introduction* -ideaDrop.nvim is a simple Neovim plugin that allows you to drop and organize -your ideas in floating Markdown sidebars. It provides a clean and -distraction-free interface for quick note-taking and idea organization. +ideaDrop.nvim is a powerful Neovim plugin for capturing, organizing, and +managing your ideas with multiple view modes, tagging system, and advanced +search capabilities. ============================================================================== INSTALLATION *ideaDrop-installation* Using lazy.nvim: - +>lua { - dir = "path/ideaDrop", + "CarGDev/ideadrop.nvim", name = "ideaDrop", + dependencies = { + "nvim-tree/nvim-tree.lua", + "nvim-tree/nvim-web-devicons", + }, config = function() require("ideaDrop").setup({ - idea_dir = "path/ideaDrop", + idea_dir = "/path/to/your/ideas", }) end, } +< + +Using packer: +>lua + use { + "CarGDev/ideadrop.nvim", + requires = { + "nvim-tree/nvim-tree.lua", + "nvim-tree/nvim-web-devicons", + }, + config = function() + require("ideaDrop").setup({ + idea_dir = "/path/to/your/ideas", + }) + end, + } +< ============================================================================== CONFIGURATION *ideaDrop-configuration* ideaDrop.nvim can be configured by passing a table to the setup function: - +>lua require("ideaDrop").setup({ - idea_dir = "/path/to/your/ideas", -- Directory to store idea files + idea_dir = "/path/to/your/ideas", }) +< OPTIONS *ideaDrop-options* @@ -40,37 +62,182 @@ idea_dir (string) ~ ============================================================================== COMMANDS *ideaDrop-commands* +CORE COMMANDS ~ + :Idea *:Idea* Opens today's idea file in a floating sidebar. :Idea {name} *:Idea-name* - Opens or creates an idea file with the specified name. + Opens or creates an idea file with the specified name in floating window. Example: `:Idea project/vision` :Idea listAll *:Idea-listAll* - Opens a fuzzy picker to select from existing ideas. + Opens a picker to select from existing ideas. + +:IdeaBuffer *:IdeaBuffer* + Opens today's idea file in the current buffer. + +:IdeaBuffer {name} *:IdeaBuffer-name* + Opens or creates an idea file in the current buffer. + +:IdeaRight *:IdeaRight* + Opens today's idea file in a persistent right-side buffer. + +:IdeaRight {name} *:IdeaRight-name* + Opens or creates an idea file in the right-side buffer. + Example: `:IdeaRight meetings/standup` + +:IdeaTree *:IdeaTree* + Opens nvim-tree file browser focused on the idea directory. + +TAG COMMANDS ~ + +:IdeaTags *:IdeaTags* + Shows a tag picker to browse all tags and filter files by tag. + +:IdeaAddTag {tag} *:IdeaAddTag* + Adds a tag to the current idea file. + Example: `:IdeaAddTag work` + +:IdeaRemoveTag {tag} *:IdeaRemoveTag* + Removes a tag from the current idea file. + Example: `:IdeaRemoveTag todo` + +:IdeaSearchTag {tag} *:IdeaSearchTag* + Searches for all files containing a specific tag. + Example: `:IdeaSearchTag project` + +SEARCH COMMANDS ~ + +:IdeaSearch {query} *:IdeaSearch* + Fuzzy search through idea titles and content. + Example: `:IdeaSearch machine learning` + +:IdeaSearchContent {query} *:IdeaSearchContent* + Search only in idea content (not titles). + Example: `:IdeaSearchContent algorithm` + +:IdeaSearchTitle {query} *:IdeaSearchTitle* + Search only in idea file names/titles. + Example: `:IdeaSearchTitle project` + +============================================================================== +KEYMAPS *ideaDrop-keymaps* + +Buffer-local keymaps (in right-side buffer): + + Toggle tree view + Refresh current file + +Suggested global keymaps (add to your config): +>lua + vim.keymap.set("n", "id", ":IdeaRight", { desc = "Open today's idea" }) + vim.keymap.set("n", "in", ":IdeaRight ", { desc = "Open named idea" }) + vim.keymap.set("n", "it", ":IdeaTree", { desc = "Open idea tree" }) + vim.keymap.set("n", "is", ":IdeaSearch ", { desc = "Search ideas" }) + vim.keymap.set("n", "ig", ":IdeaTags", { desc = "Browse tags" }) + vim.keymap.set("n", "if", ":Idea", { desc = "Open idea in float" }) +< ============================================================================== FEATURES *ideaDrop-features* -- Markdown editor in a floating sidebar -- Automatic save on close -- Date-based and custom named notes -- Folder support (e.g., project/vision.md) -- Fuzzy finder for existing ideas -- Clean and distraction-free interface +VIEW MODES ~ + +1. Floating Window (`:Idea`) + - Opens ideas in a floating window + - Good for quick notes + +2. Current Buffer (`:IdeaBuffer`) + - Opens ideas in the current buffer + - Replaces current content + +3. Right-Side Buffer (`:IdeaRight`) - Recommended + - Persistent buffer on the right side + - Stays open while you work + +4. Tree Browser (`:IdeaTree`) + - Full file tree on the left side + - Uses nvim-tree integration + +TAGGING SYSTEM ~ + +Tags use the `#tag` format in your markdown files: +>markdown + # My Idea Title + + This is my idea content. + + #work #project-x #feature #todo +< + +- Tags are automatically detected and indexed +- Filter ideas by tags using `:IdeaTags` +- Add/remove tags with `:IdeaAddTag` and `:IdeaRemoveTag` + +SEARCH FEATURES ~ + +- Fuzzy search through titles and content +- Real-time results as you type +- Jump to specific lines in search results +- Separate content and title search options + +AUTO-SAVE ~ + +All changes are automatically saved when: +- Closing a floating window +- Writing the buffer (`:w`) +- Closing the right-side window ============================================================================== EXAMPLES *ideaDrop-examples* -1. Open today's idea: - :Idea +1. Open today's idea in right buffer: +>vim + :IdeaRight +< -2. Create a new idea in a project folder: - :Idea project/nextgen +2. Create a nested folder structure: +>vim + :IdeaRight meetings/2024-01-15 + :IdeaRight projects/app/features +< -3. List and select from existing ideas: - :Idea listAll +3. Add tags to current idea: +>vim + :IdeaAddTag work + :IdeaAddTag todo +< + +4. Search for ideas: +>vim + :IdeaSearch "machine learning" + :IdeaSearchContent "algorithm" +< + +5. Browse and filter by tags: +>vim + :IdeaTags +< ============================================================================== -vim:tw=78:ts=8:ft=help:norl: \ No newline at end of file +TROUBLESHOOTING *ideaDrop-troubleshooting* + +Common Issues ~ + +1. Module not found errors + - Ensure all dependencies are installed (nvim-tree, nvim-web-devicons) + +2. Tree not opening + - Make sure nvim-tree is properly configured in your Neovim setup + +3. Tags not showing + - Check that your idea directory exists and contains markdown files + - Ensure tags use the `#tag` format + +4. Search not working + - Verify your idea directory path is correct + - Check that markdown files exist in the directory + +============================================================================== +vim:tw=78:ts=8:ft=help:norl: diff --git a/llms.txt b/llms.txt new file mode 100644 index 0000000..b10a68d --- /dev/null +++ b/llms.txt @@ -0,0 +1,111 @@ +# ideaDrop.nvim - LLM Context + +> A Neovim plugin for capturing, organizing, and managing ideas with markdown support. + +## Project Overview + +ideaDrop.nvim is a Lua-based Neovim plugin that provides a distraction-free environment for note-taking and idea management. It features multiple view modes, a tagging system, and search capabilities. + +## Tech Stack + +- **Language**: Lua +- **Platform**: Neovim (requires Neovim 0.8+) +- **Dependencies**: nvim-tree/nvim-tree.lua, nvim-tree/nvim-web-devicons + +## Project Structure + +``` +ideaDrop.nvim/ +├── lua/ideaDrop/ +│ ├── init.lua # Main entry point +│ ├── core/ +│ │ ├── init.lua # Core module with setup and commands +│ │ └── config.lua # Configuration management +│ ├── ui/ +│ │ ├── sidebar.lua # Floating/buffer/right-side window management +│ │ └── tree.lua # nvim-tree integration +│ ├── features/ +│ │ ├── list.lua # File listing functionality +│ │ ├── tags.lua # Tag extraction and management +│ │ └── search.lua # Fuzzy search implementation +│ └── utils/ +│ ├── utils.lua # Utility functions +│ ├── keymaps.lua # Keymap setup +│ └── constants.lua # Constants and defaults +├── doc/ +│ └── ideaDrop.txt # Vim help documentation +├── README.md +├── CONTRIBUTING.md +├── CHANGELOG.md +└── LICENSE +``` + +## Key Features + +1. **Multiple View Modes** + - Floating window (`:Idea`) + - Current buffer (`:IdeaBuffer`) + - Right-side persistent buffer (`:IdeaRight`) + - Tree browser (`:IdeaTree`) + +2. **Tagging System** + - Uses `#tag` format in markdown files + - Tag extraction, caching, and filtering + - Commands: `:IdeaTags`, `:IdeaAddTag`, `:IdeaRemoveTag`, `:IdeaSearchTag` + +3. **Search Functionality** + - Fuzzy search through titles and content + - Commands: `:IdeaSearch`, `:IdeaSearchContent`, `:IdeaSearchTitle` + +4. **Auto-save** + - Automatic saving on window close + - Uses `BufWriteCmd` autocmd for custom save handling + +## Configuration + +```lua +require("ideaDrop").setup({ + idea_dir = "/path/to/your/ideas", -- Directory for storing idea files +}) +``` + +## Code Conventions + +- Type annotations using LuaCATS/EmmyLua format (`---@param`, `---@return`, `---@class`) +- Module pattern with local `M = {}` tables +- Neovim API usage: + - `vim.bo[buf]` for buffer options + - `vim.wo[win]` for window options + - `vim.api.nvim_*` for other API calls +- Error handling with `pcall` for optional dependencies +- Notifications via `vim.notify()` with appropriate log levels + +## Important Implementation Details + +- **Glob patterns**: Must include `/` separator: `path .. "/**/*.md"` +- **nvim-tree**: Use API directly (`require("nvim-tree.api")`) without calling `setup()` to preserve user config +- **Buffer management**: Right-side buffer uses `buftype = "acwrite"` for custom save handling +- **File paths**: Use `vim.fn.fnameescape()` for safe path handling + +## Commands Reference + +| Command | Description | +|---------|-------------| +| `:Idea [name]` | Open in floating window | +| `:IdeaBuffer [name]` | Open in current buffer | +| `:IdeaRight [name]` | Open in right-side buffer | +| `:IdeaTree` | Open file tree browser | +| `:IdeaTags` | Browse tags | +| `:IdeaAddTag {tag}` | Add tag to current file | +| `:IdeaRemoveTag {tag}` | Remove tag from current file | +| `:IdeaSearchTag {tag}` | Search files by tag | +| `:IdeaSearch {query}` | Fuzzy search all | +| `:IdeaSearchContent {query}` | Search content only | +| `:IdeaSearchTitle {query}` | Search titles only | + +## Development Notes + +- Help tags file at `doc/tags` is auto-generated +- Uses `vim.ui.select()` for picker interfaces +- Tag cache invalidation via `tag_cache_dirty` flag +- Markdown files default template includes title and bullet point diff --git a/lua/ideaDrop/features/list.lua b/lua/ideaDrop/features/list.lua index 2f0a025..ba127d7 100644 --- a/lua/ideaDrop/features/list.lua +++ b/lua/ideaDrop/features/list.lua @@ -11,7 +11,7 @@ local M = {} function M.list_all() local path = config.options.idea_dir -- Find all .md files recursively - local files = vim.fn.glob(path .. "**/*.md", false, true) + local files = vim.fn.glob(path .. "/**/*.md", false, true) if #files == 0 then vim.notify("📂 No idea files found", vim.log.levels.INFO) @@ -21,7 +21,8 @@ function M.list_all() -- Present file selection UI vim.ui.select(files, { prompt = "📂 Select an idea file to open:" }, function(choice) if choice then - sidebar.open(choice) -- Open the selected file in sidebar + local filename = vim.fn.fnamemodify(choice, ":t") + sidebar.open(choice, filename, false) -- Open the selected file in sidebar end end) end diff --git a/lua/ideaDrop/features/search.lua b/lua/ideaDrop/features/search.lua index 572d793..d114d1f 100644 --- a/lua/ideaDrop/features/search.lua +++ b/lua/ideaDrop/features/search.lua @@ -71,7 +71,7 @@ function M.fuzzy_search(query) local results = {} -- Find all .md files recursively - local files = vim.fn.glob(idea_path .. "**/*.md", false, true) + local files = vim.fn.glob(idea_path .. "/**/*.md", false, true) for _, file in ipairs(files) do if vim.fn.filereadable(file) == 1 then @@ -199,7 +199,7 @@ function M.search_in_content(query) local results = {} -- Find all .md files recursively - local files = vim.fn.glob(idea_path .. "**/*.md", false, true) + local files = vim.fn.glob(idea_path .. "/**/*.md", false, true) for _, file in ipairs(files) do if vim.fn.filereadable(file) == 1 then @@ -257,7 +257,7 @@ function M.search_by_title(query) local results = {} -- Find all .md files recursively - local files = vim.fn.glob(idea_path .. "**/*.md", false, true) + local files = vim.fn.glob(idea_path .. "/**/*.md", false, true) for _, file in ipairs(files) do if vim.fn.filereadable(file) == 1 then diff --git a/lua/ideaDrop/features/tags.lua b/lua/ideaDrop/features/tags.lua index 07da0ba..a47ac58 100644 --- a/lua/ideaDrop/features/tags.lua +++ b/lua/ideaDrop/features/tags.lua @@ -80,7 +80,7 @@ function M.get_all_tags() local seen = {} -- Find all .md files recursively - local files = vim.fn.glob(idea_path .. "**/*.md", false, true) + local files = vim.fn.glob(idea_path .. "/**/*.md", false, true) for _, file in ipairs(files) do if vim.fn.filereadable(file) == 1 then @@ -203,7 +203,7 @@ function M.get_files_by_tag(tag) local matching_files = {} -- Find all .md files recursively - local files = vim.fn.glob(idea_path .. "**/*.md", false, true) + local files = vim.fn.glob(idea_path .. "/**/*.md", false, true) for _, file in ipairs(files) do if vim.fn.filereadable(file) == 1 then @@ -264,7 +264,6 @@ function M.show_files_with_tag(tag) -- Format file names for display local file_choices = {} for _, file in ipairs(files) do - local filename = vim.fn.fnamemodify(file, ":t") local relative_path = file:sub(#config.options.idea_dir + 2) -- Remove idea_dir + "/" table.insert(file_choices, relative_path) end diff --git a/lua/ideaDrop/ui/sidebar.lua b/lua/ideaDrop/ui/sidebar.lua index 652472e..535db96 100644 --- a/lua/ideaDrop/ui/sidebar.lua +++ b/lua/ideaDrop/ui/sidebar.lua @@ -29,7 +29,7 @@ function M.open(file, filename, use_buffer) -- Create a new buffer local buf = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_option(buf, "filetype", "markdown") + vim.bo[buf].filetype = "markdown" -- Calculate window dimensions (30% of screen width, 80% of screen height) local width = math.floor(vim.o.columns * 0.3) @@ -124,9 +124,9 @@ function M.open_right_side(file, filename) -- Create buffer if it doesn't exist if not right_side_buf or not vim.api.nvim_buf_is_valid(right_side_buf) then right_side_buf = vim.api.nvim_create_buf(false, false) - vim.api.nvim_buf_set_option(right_side_buf, "filetype", "markdown") - vim.api.nvim_buf_set_option(right_side_buf, "buftype", "acwrite") - vim.api.nvim_buf_set_option(right_side_buf, "bufhidden", "hide") + vim.bo[right_side_buf].filetype = "markdown" + vim.bo[right_side_buf].buftype = "acwrite" + vim.bo[right_side_buf].bufhidden = "hide" -- Set up autosave for the right-side buffer vim.api.nvim_create_autocmd("BufWriteCmd", { @@ -190,11 +190,11 @@ function M.open_right_side(file, filename) }) -- Set window options - vim.api.nvim_win_set_option(right_side_win, "wrap", true) - vim.api.nvim_win_set_option(right_side_win, "number", true) - vim.api.nvim_win_set_option(right_side_win, "relativenumber", false) - vim.api.nvim_win_set_option(right_side_win, "cursorline", true) - vim.api.nvim_win_set_option(right_side_win, "winhl", "Normal:Normal,FloatBorder:FloatBorder") + vim.wo[right_side_win].wrap = true + vim.wo[right_side_win].number = true + vim.wo[right_side_win].relativenumber = false + vim.wo[right_side_win].cursorline = true + vim.wo[right_side_win].winhl = "Normal:Normal,FloatBorder:FloatBorder" -- Set up autosave on window close vim.api.nvim_create_autocmd("WinClosed", { diff --git a/lua/ideaDrop/ui/tree.lua b/lua/ideaDrop/ui/tree.lua index 8e1c3f4..7aa77c6 100644 --- a/lua/ideaDrop/ui/tree.lua +++ b/lua/ideaDrop/ui/tree.lua @@ -8,6 +8,7 @@ local M = {} -- Tree state local tree_callback = nil local original_cwd = nil +local ideadrop_tree_autocmd_group = nil ---Opens nvim-tree focused on the idea directory ---@param callback fun(file_path: string): nil Callback function when a file is selected @@ -16,8 +17,8 @@ function M.open_tree_window(callback) tree_callback = callback -- Check if nvim-tree is available - local has_nvim_tree, nvim_tree = pcall(require, "nvim-tree") - if not has_nvim_tree then + local has_nvim_tree_api, nvim_tree_api = pcall(require, "nvim-tree.api") + if not has_nvim_tree_api then vim.notify("❌ nvim-tree is not installed. Please install nvim-tree to use this feature.", vim.log.levels.ERROR) return end @@ -27,112 +28,66 @@ function M.open_tree_window(callback) -- Change to idea directory local idea_path = config.options.idea_dir - if vim.fn.isdirectory(idea_path) == 1 then - vim.cmd("cd " .. vim.fn.fnameescape(idea_path)) - else - vim.notify("❌ Idea directory not found: " .. idea_path, vim.log.levels.ERROR) - return + if vim.fn.isdirectory(idea_path) == 0 then + -- Create the directory if it doesn't exist + vim.fn.mkdir(idea_path, "p") end - -- Set up nvim-tree to open on the left side - nvim_tree.setup({ - view = { - side = "left", - width = 30, - }, - actions = { - open_file = { - quit_on_open = false, - }, - }, - on_attach = function(bufnr) - -- Override the default file opening behavior - local api = require("nvim-tree.api") - - -- Map Enter to custom handler - vim.keymap.set("n", "", function() - local node = api.tree.get_node_under_cursor() - if node and node.type == "file" then - -- Call our callback with the selected file - if tree_callback then - tree_callback(node.absolute_path) - end + -- Create autocmd group for ideaDrop tree handling + if ideadrop_tree_autocmd_group then + vim.api.nvim_del_augroup_by_id(ideadrop_tree_autocmd_group) + end + ideadrop_tree_autocmd_group = vim.api.nvim_create_augroup("IdeaDropTree", { clear = true }) + + -- Set up autocmd to handle file selection from nvim-tree + vim.api.nvim_create_autocmd("BufEnter", { + group = ideadrop_tree_autocmd_group, + callback = function(args) + local bufname = vim.api.nvim_buf_get_name(args.buf) + -- Check if this is a markdown file in the idea directory + if bufname:match("%.md$") and bufname:find(idea_path, 1, true) then + -- Call our callback with the selected file + if tree_callback then -- Close nvim-tree - api.tree.close() + nvim_tree_api.tree.close() -- Restore original working directory if original_cwd then vim.cmd("cd " .. vim.fn.fnameescape(original_cwd)) end - else - -- Default behavior for directories - api.node.open.edit() + -- Use vim.schedule to avoid issues with buffer switching + vim.schedule(function() + tree_callback(bufname) + end) + -- Clear the autocmd group after handling + vim.api.nvim_del_augroup_by_id(ideadrop_tree_autocmd_group) + ideadrop_tree_autocmd_group = nil end - end, { buffer = bufnr, noremap = true, silent = true }) - - -- Map 'q' to close tree and restore directory - vim.keymap.set("n", "q", function() - api.tree.close() - if original_cwd then - vim.cmd("cd " .. vim.fn.fnameescape(original_cwd)) - end - end, { buffer = bufnr, noremap = true, silent = true }) - - -- Keep other default mappings - vim.keymap.set("n", "", api.tree.change_root_to_node, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "", api.node.open.replace_tree_buffer, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "", api.node.show_info_popup, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "", api.fs.rename_sub, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "", api.node.open.tab, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "", api.node.open.vertical, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "", api.node.open.horizontal, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "", api.node.navigate.parent_close, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "", api.node.open.preview, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", ">", api.node.navigate.sibling.next, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "<", api.node.navigate.sibling.prev, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", ".", api.node.run.cmd, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "-", api.tree.change_root_to_parent, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "a", api.fs.create, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "bmv", api.marks.bulk.move, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "B", api.tree.toggle_no_buffer_filter, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "c", api.fs.copy.node, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "C", api.tree.toggle_git_clean_filter, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "[c", api.node.navigate.git.prev, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "]c", api.node.navigate.git.next, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "d", api.fs.remove, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "D", api.fs.trash, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "E", api.tree.expand_all, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "e", api.fs.rename_basename, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "]e", api.node.navigate.diagnostics.next, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "[e", api.node.navigate.diagnostics.prev, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "F", api.live_filter.clear, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "f", api.live_filter.start, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "g?", api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "gy", api.fs.copy.absolute_path, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "H", api.tree.toggle_hidden_filter, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "I", api.tree.toggle_gitignore_filter, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "J", api.node.navigate.sibling.last, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "K", api.node.navigate.sibling.first, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "m", api.marks.toggle, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "o", api.node.open.edit, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "O", api.node.open.no_window_picker, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "P", api.node.navigate.parent, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "r", api.fs.rename, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "R", api.tree.reload, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "s", api.node.run.system, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "S", api.tree.search_node, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "U", api.tree.toggle_custom_filter, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "W", api.tree.collapse_all, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "x", api.fs.cut, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "y", api.fs.copy.filename, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "Y", api.fs.copy.relative_path, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "<2-LeftMouse>", api.node.open.edit, { buffer = bufnr, noremap = true, silent = true }) - vim.keymap.set("n", "<2-RightMouse>", api.tree.change_root_to_node, { buffer = bufnr, noremap = true, silent = true }) + end end, }) - -- Open nvim-tree using the correct API - local api = require("nvim-tree.api") - api.tree.open() + -- Set up autocmd to restore cwd when nvim-tree is closed + vim.api.nvim_create_autocmd("BufLeave", { + group = ideadrop_tree_autocmd_group, + pattern = "NvimTree_*", + callback = function() + -- Restore original working directory when leaving nvim-tree + if original_cwd then + vim.schedule(function() + -- Only restore if we're not in an idea file + local current_buf = vim.api.nvim_get_current_buf() + local bufname = vim.api.nvim_buf_get_name(current_buf) + if not (bufname:match("%.md$") and bufname:find(idea_path, 1, true)) then + vim.cmd("cd " .. vim.fn.fnameescape(original_cwd)) + end + end) + end + end, + }) + + -- Open nvim-tree in the idea directory (without calling setup) + -- This preserves the user's nvim-tree configuration + nvim_tree_api.tree.open({ path = idea_path }) end return M \ No newline at end of file