Skip to content

shimman-dev/laundry.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

18 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

laundry.nvim ๐Ÿงบ

When you need to fold a lengthy stack of imports in your preferred language. ๐Ÿ‘”

pinny doing laundry

Requirements

passed on my first audition

Features

  • Automatically folds import statements in your code
  • Language-specific fold configurations
  • Currently supports TypeScript, JavaScript, and Go (wow three whole languages? Visit the CONTRIBUTING.md to learn how to add additional languages)
  • Configurable minimum fold thresholds

Installation

lazy.nvim
{
    "shimman-dev/laundry.nvim",
	priority = 1000,
    requires = { 'nvim-treesitter/nvim-treesitter' },
	event = { "BufReadPost", "BufNewFile" },
    dependencies = {
        "nvim-treesitter/nvim-treesitter",
    },
	---@module 'laundry'
	---@type LaundryConfig
    opts = {
        auto_fold = true, -- default value is false, need to opt-in for plugin
    }
}
packer.nvim
use {
    'shimman-dev/laundry.nvim',
    requires = { 'nvim-treesitter/nvim-treesitter' },
    config = function()
        require('laundry').setup({
            auto_fold = true, -- default value is false, need to opt-in for plugin
        })
    end
}

Commands

  • :LaundryFold - Manually fold import statements in the current buffer, respects min_fold_lines
  • :LaundryForceFold - Manually fold import statements in the current buffer, ignores min_fold_lines

Notes

  • If min_fold_lines is not set in either the global config or language-specific config, no folding will occur
  • Language-specific settings take precedence over global settings unless overridden by user configuration

Configuration

Everything but the kitchen sink options (lazy.nvim):

{
    "shimman-dev/laundry.nvim",
    dependencies = {
        "nvim-treesitter/nvim-treesitter",
    },
    opts = {
		-- NOTE: not initially set to true, must opt-in the plugin
        -- Whether to automatically fold imports when opening files
        auto_fold = false,

        -- Global minimum number of import lines before folding
        -- If not set, will use language-specific settings
        -- min_fold_lines = 25,

        -- Filetypes that will be folded (if `auto_fold` is true)
        enabled_filetypes = {
            "typescript",
            "typescriptreact",
            "javascript",
            "javascriptreact",
            "go",
        },

        -- Language-specific configurations
        languages = {
            -- Override settings for TypeScript
            typescript = {
                min_fold_lines = 10,  -- Only fold if there are >=10 lines of imports
            },
        },
    }
}

Adding Support for Additional Languages

Example showing how to add Python support:

require('laundry').setup({
    auto_fold = true,
    enabled_filetypes = { "python" },  -- Enable for Python
    languages = {
        python = {
            -- Patterns determine which files this config applies to
            file_patterns = { "*.py" },

            -- These are the TreeSitter node types that represent imports
            -- For Python they are:
            -- - import_statement: for "import foo"
            -- - import_from_statement: for "from foo import bar"
            import_node_types = { 
                "import_statement",
                "import_from_statement"
            },

            -- Most languages don't need aliases, but some like
            -- TypeScript/JavaScript might have multiple file extensions
            aliases = {},

            -- Only fold when there are >=10 import lines
            min_fold_lines = 10,
        }
    }
})

To find the correct TreeSitter node types for other languages:

  1. Open a file of your target language
  2. Place your cursor on an import statement
  3. Run :InspectTree to see the syntax tree
  4. Look for the node type that encompasses the import statement
Examples of Different Configurations
  1. Minimal setup (uses defaults):
require('laundry').setup()
  1. Opt-in auto-folding:
require('laundry').setup({
    auto_fold = true,
})
  1. Auto-fold with custom threshold:
require('laundry').setup({
    auto_fold = true,
    min_fold_lines = 3,
})
  1. Language-specific thresholds:
require('laundry').setup({
    auto_fold = true,
    languages = {
        typescript = { min_fold_lines = 10 },
        javascript = { min_fold_lines = 8 },
        python = { min_fold_lines = 5 },
    }
})
  1. Manual folding only (no auto-fold):
require('laundry').setup({
    auto_fold = false, -- Fold when using :LaundryForceFold
})

Current Language Support

Language Support Matrix

Todo

  • whimmsy up the README.md
  • create an art icon
  • create a CONTRIBUTING.md
  • create a benchmark tests (folding 1k, 10k, 100k, 1000k imports)
  • create a basic unit test suite
  • in a future neovim release: folding can be handled via the LSP will need to account for this change. Will need to handle LSP and treesitter options.

About

๐Ÿ‘” folding our imports into tidy little piles ๐Ÿงบ

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages