{"id":30838809,"url":"https://github.com/dmtrkovalenko/fold-imports.nvim","last_synced_at":"2025-09-06T18:12:25.081Z","repository":{"id":305237549,"uuid":"1022343561","full_name":"dmtrKovalenko/fold-imports.nvim","owner":"dmtrKovalenko","description":"Neovim plugin to automatically fold imports in any language using treesitter","archived":false,"fork":false,"pushed_at":"2025-07-23T21:08:44.000Z","size":49,"stargazers_count":46,"open_issues_count":2,"forks_count":2,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-04T18:53:45.511Z","etag":null,"topics":["neovim-plugin"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmtrKovalenko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-18T22:10:34.000Z","updated_at":"2025-08-28T22:47:12.000Z","dependencies_parsed_at":"2025-07-19T03:21:53.571Z","dependency_job_id":null,"html_url":"https://github.com/dmtrKovalenko/fold-imports.nvim","commit_stats":null,"previous_names":["dmtrkovalenko/fold-imports.nvim"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/dmtrKovalenko/fold-imports.nvim","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmtrKovalenko%2Ffold-imports.nvim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmtrKovalenko%2Ffold-imports.nvim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmtrKovalenko%2Ffold-imports.nvim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmtrKovalenko%2Ffold-imports.nvim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmtrKovalenko","download_url":"https://codeload.github.com/dmtrKovalenko/fold-imports.nvim/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmtrKovalenko%2Ffold-imports.nvim/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273941527,"owners_count":25195104,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["neovim-plugin"],"created_at":"2025-09-06T18:12:23.926Z","updated_at":"2025-09-06T18:12:25.073Z","avatar_url":"https://github.com/dmtrKovalenko.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fold-imports.nvim\n\n![Lua](https://img.shields.io/badge/Made%20with%20Lua-blueviolet.svg?style=for-the-badge\u0026logo=lua)\n![Neovim](https://img.shields.io/badge/NeoVim-%2357A143.svg?\u0026style=for-the-badge\u0026logo=neovim\u0026logoColor=white)\n\nA Neovim plugin that automatically folds import/export statements in multiple programming languages using treesitter. Reduces visual clutter by folding verbose import blocks and showing the CODE.\n\nhttps://github.com/user-attachments/assets/a1c54774-ee8a-4595-9653-bc400a34336a\n\n## Installation\n\n### Using [lazy.nvim](https://github.com/folke/lazy.nvim)\n\n```lua\n{\n  \"dmtrKovalenko/fold-imports.nvim\",\n  opts = {},\n  event = \"BufRead\"\n}\n```\n\n### Using [packer.nvim](https://github.com/wbthomason/packer.nvim)\n\n```lua\nuse {\n  \"dmtrKovalenko/fold-imports.nvim\",\n  config = function()\n    require(\"fold_imports\").setup()\n  end,\n}\n```\n\n## Quick Start\n\nThe plugin works out of the box with zero configuration:\n\n```lua\nrequire(\"fold_imports\").setup()\n```\n\nOpen any supported file and imports will be automatically folded.\n\n## Configuration\n\n### Default Configuration\n\n```lua\nrequire(\"fold_imports\").setup({{\n  enabled = true,\n  auto_fold = true,\n  fold_level = 0,\n  -- refolds imports after lsp code edits (e.g. code actions)\n  auto_fold_after_code_action = true,\n  -- custom fold text for import sections\n  custom_fold_text = true,\n  fold_text_format = \"Folded imports (%d lines)\",\n  -- maximum lines for a single import statement to be considered for folding\n  max_import_lines = 50,\n  languages = {\n    typescript = {\n      enabled = true,\n      parsers = { \"typescript\", \"tsx\" },\n      queries = {\n        \"(import_statement) @import\",\n        \"(export_statement (export_clause) @export)\",\n      },\n      filetypes = { \"typescript\", \"typescriptreact\" },\n      patterns = { \"*.ts\", \"*.tsx\" },\n    },\n    javascript = {\n      enabled = true,\n      parsers = { \"javascript\", \"jsx\" },\n      queries = {\n        \"(import_statement) @import\",\n        \"(export_statement (export_clause) @export)\",\n      },\n      filetypes = { \"javascript\", \"javascriptreact\" },\n      patterns = { \"*.js\", \"*.jsx\", \"*.mjs\" },\n    },\n    rust = {\n      enabled = true,\n      parsers = { \"rust\" },\n      queries = {\n        \"(use_declaration) @import\",\n        \"(mod_item) @import\",\n      },\n      filetypes = { \"rust\" },\n      patterns = { \"*.rs\" },\n    },\n    c = {\n      enabled = true,\n      parsers = { \"c\" },\n      queries = {\n        \"(preproc_include) @import\",\n        \"(preproc_def) @import\",\n      },\n      filetypes = { \"c\" },\n      patterns = { \"*.c\", \"*.h\" },\n    },\n    cpp = {\n      enabled = true,\n      parsers = { \"cpp\" },\n      queries = {\n        \"(preproc_include) @import\",\n        \"(preproc_def) @import\",\n        \"(using_declaration) @import\",\n        \"(namespace_alias_definition) @import\",\n        \"(linkage_specification) @import\",\n      },\n      filetypes = { \"cpp\" },\n      patterns = { \"*.cpp\", \"*.cxx\", \"*.cc\", \"*.hpp\", \"*.hxx\", \"*.hh\" },\n    },\n    ocaml = {\n      enabled = true,\n      parsers = { \"ocaml\" },\n      queries = {\n        \"(open_module) @import\",\n      },\n      filetypes = { \"ocaml\" },\n      patterns = { \"*.ml\", \"*.mli\" },\n    },\n    zig = {\n      enabled = true,\n      parsers = { \"zig\" },\n      queries = {\n        '(variable_declaration (identifier) (builtin_function (builtin_identifier) @builtin (#eq? @builtin \"@import\"))) @import',\n      },\n      filetypes = { \"zig\" },\n      patterns = { \"*.zig\" },\n    },\n    python = {\n      enabled = true,\n      parsers = { \"python\" },\n      queries = {\n        \"(import_statement) @import\",\n        \"(import_from_statement) @import\",\n        \"(future_import_statement) @import\",\n      },\n      filetypes = { \"python\" },\n      patterns = { \"*.py\", \"*.pyi\" },\n    },\n    go = {\n      enabled = true,\n      parsers = { \"go\" },\n      queries = {\n        \"(import_declaration) @import\", -- For import blocks\n        \"(import_spec) @import\", -- For individual imports within blocks\n      },\n      filetypes = { \"go\" },\n      patterns = { \"*.go\" },\n    },\n    dart = {\n        enabled = true,\n        parsers = { \"dart\" },\n        queries = {\n            \"(import_or_export) @import\",\n        },\n        filetypes = { \"dart\" },\n        patterns = { \"*.dart\" },\n    },\n  },\n}})\n```\n\n### Custom Language Configuration\n\nDisable specific languages or add custom Tree-sitter queries:\n\n```lua\nrequire(\"fold_imports\").setup({\n  languages = {\n    -- Disable TypeScript folding\n    typescript = {\n      enabled = false,\n    },\n    -- Add your language with a query\n    python = {\n      enabled = true,\n      queries = {\n        -- To see what to put here run `:InspectTree` and put cursor over the import statement\n        \"(import_statement) @import\",\n        \"(import_from_statement) @import\",\n      },\n    },\n  },\n})\n```\n\n### Disable Auto-folding\n\nFor manual control only:\n\n```lua\nrequire(\"fold_imports\").setup({\n  auto_fold = false,\n})\n```\n\nThen use `:FoldImports` command to fold imports manually.\n\n## Commands\n\n| Command              | Description                                      |\n| -------------------- | ------------------------------------------------ |\n| `:FoldImports`       | Manually fold imports in the current buffer      |\n| `:FoldImportsToggle` | Enable/disable the plugin                        |\n| `:FoldImportsDebug`  | Debug Tree-sitter matches for the current buffer |\n\n## API\n\nThe plugin exposes a Lua API for programmatic control:\n\n```lua\nlocal fold_imports = require(\"fold_imports\")\n\n-- Manually fold imports\nfold_imports.fold_imports()\n\n-- Enable the plugin\nfold_imports.enable()\n\n-- Disable the plugin\nfold_imports.disable()\n\n-- Toggle the plugin\nfold_imports.toggle()\n```\n\n## How It Works\n\n1. **Tree-sitter Integration**: Uses Tree-sitter parsers to accurately identify import statements in different languages\n2. **Smart Grouping**: Groups adjacent import statements, ignoring empty lines between them\n3. **Intelligent Folding**: Creates folds for:\n   - Multi-line import groups\n   - Multiple separate import groups\n   - Single-line imports when there are multiple imports total\n4. **LSP Integration**: Monitors LSP text edits and re-folds imports when changes occur near existing import folds\n5. **Performance**: Uses retries with exponential backoff and optimizes the runtime\n6. Git Conflicts detection: Detects git conflicts using various popular plugins like [git-conflict.nvim](https://github.com/akinsho/git-conflict.nvim)\n\n## Language Support\n\n### TypeScript/JavaScript\n\n- **Supported**: Import declarations, export statements with export clauses\n- **Examples**: `import { foo } from 'bar'`, `export { baz }`\n\n### Rust\n\n- **Supported**: Use declarations, module items\n- **Examples**: `use std::collections::HashMap;`, `mod utils;`\n\n### Python\n\n- **Supported**: Import statements, from-import statements, future imports\n- **Examples**: `import os`, `from typing import List`, `from __future__ import annotations`\n\n### C/C++\n\n- **Supported**: Preprocessor includes, defines, using declarations (C++), namespace aliases (C++)\n- **Examples**: `#include \u003cstdio.h\u003e`, `using namespace std;`\n\n### OCaml\n\n- **Supported**: Open module statements\n- **Examples**: `open List`, `open Printf`\n\n### Zig\n\n- **Supported**: Variable declarations with @import builtin\n- **Examples**: `const std = @import(\"std\");`\n\n### go\n\n- **Supported**: Import statements\n- **Examples**: `import fmt`\n\n## Troubleshooting\n\n### Imports Not Folding\n\n1. **Check Tree-sitter parser**: Ensure the required Tree-sitter parser is installed:\n\n   ```vim\n   :TSInstall typescript javascript rust python c cpp ocaml zig\n   ```\n\n2. **Debug Tree-sitter matches**: Use the debug command to see what's being detected:\n\n   ```vim\n   :FoldImportsDebug\n   ```\n\n3. **Verify filetype**: Check that your file has the correct filetype:\n   ```vim\n   :set filetype?\n   ```\n\n### Custom Queries Not Working\n\nTree-sitter queries are language-specific. Use `:TSPlayground` to explore the syntax tree and write custom queries. The query format follows Tree-sitter query syntax.\n\n## Requirements\n\n- **Neovim**: \u003e= 0.8.0\n- **Tree-sitter**: Built-in Neovim Tree-sitter support\n- **Parsers**: Language-specific Tree-sitter parsers (installed via `:TSInstall`)\n\n## License\n\nMIT License - see [LICENSE](LICENSE) file for details.\n\n## Author\n\nCreated by [@neogoose_btw](https://x.com/neogoose_btw)\n\nBuilt with [tree-sitter](https://tree-sitter.github.io/) for accurate parsing. Template based on [nvim-plugin-template](https://github.com/ellisonleao/nvim-plugin-template).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmtrkovalenko%2Ffold-imports.nvim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmtrkovalenko%2Ffold-imports.nvim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmtrkovalenko%2Ffold-imports.nvim/lists"}