Skip to content

maxbol/treesorter.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

treesorter.nvim

Simple POC of a plugin to sort code units using treesitter.

treesorter-github.mov

Installation

Using lazy.nvim:

return {
  "maxbol/treesorter.nvim",
  cmd = "TSort",
  config = function()
    require("treesorter").setup()
  end,
  dependencies = {
    -- Optional, but highly recommended
    "nvim-treesitter/nvim-treesitter-textobjects"
  },
}

Usage

To sort units of a certain type, use the TSort command. For example, to sort function definitions in a C file:

:TSort function_definition

This will result in all functions in the most narrow scope from the cursor position gets resorted in alphabetical order.

If you want to sort multiple types of nodes, simply add them additional arguments:

:TSort function_definition declaration

This will sort functions and variable declarations individually and keep them separate.

If instead you want to sort these node types as a single list of nodes, you can sort nodes of multiple types together using groups of types. Members of a single group are connected using a + sign:

:TSort function_definition+declaration method

You can use TSort in visual mode to only sort nodes within the selected range:

:'<,'>TSort function_definition

Usage with LUA:

require("treesorter").sort({ groups = { "function_definition+declaration", "method" } })
require("treesorter").sort({ groups = { "function_definition" }, range = { start = 1, end = 2 } })
require("treesorter").sort({ groups = { "function_definition" }, bufnr = 1 })
require("treesorter").sort({ groups = { "function_definition" }, pos = { 1, 2 } })

Using textobject captures

If nvim-treesitter-textobjects is installed, you can directly use treesitter captures instead of types in your sort groups, i.e:

:TSort @function.outer

This is handy for when you might want to create a global mapping that uses the same sortgroup regardless of filetype:

vim.api.nvim_set_keymap("n", "<leader>sf", ":TSort @function.outer<CR>", { noremap = true, silent = true })

About

Simple plugin to sort code units using treesitter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages