Skip to content

feat(nix): add Nix language support for nested scope detection#179

Open
parkers0405 wants to merge 2 commits into
shellRaining:mainfrom
parkers0405:main
Open

feat(nix): add Nix language support for nested scope detection#179
parkers0405 wants to merge 2 commits into
shellRaining:mainfrom
parkers0405:main

Conversation

@parkers0405
Copy link
Copy Markdown

@parkers0405 parkers0405 commented Jan 29, 2026

Summary

Adds Nix-specific treesitter node types for better chunk indicator support in Nix files.

Problem

Currently, hlchunk only shows chunk indicators for top-level scopes in Nix files. Nix has many nested scope types (let expressions, attribute sets, lambda functions, etc.) that weren't being detected.

Solution

Add nix.lua with Nix-specific treesitter node types as a table (not array):

  • let_expression - let ... in ... blocks
  • attrset_expression - { ... } attribute sets
  • rec_attrset_expression - rec { ... } recursive sets
  • list_expression - [ ... ] lists
  • lambda_expression - arg: body functions
  • function_expression - { args }: body functions
  • with_expression - with ...; ... expressions
  • if_expression - conditionals
  • assert_expression - assertions
  • binding - name = value; bindings

Benefits

Better visual feedback when editing Nix flakes and configurations. Users can now see chunk indicators for:

  • Input/output blocks in flakes
  • Each host configuration
  • Let bindings and their scopes
  • Nested attribute sets
  • Function definitions

Especially useful for NixOS configurations and flake structures.

Testing

Tested on NixOS with Nix flakes and configuration files. Chunk indicators now appear for all nested scopes.

Parker Settle added 2 commits January 28, 2026 21:20
- Add node_type field to Scope class to track treesitter node type
- Pass node_type from chunkHelper to render function
- Add node_type_styles config option for pattern-based style mapping
- Create dynamic highlight groups based on node type patterns
- Also includes virt_text_repeat_linebreak fix for wrapped lines

Example config:
  node_type_styles = {
    ["^func"] = { fg = "#99aee5" },  -- functions: blue
    ["^if"] = { fg = "#c2a2e3" },    -- conditionals: purple
    ["^for"] = { fg = "#fbdf90" },   -- loops: yellow
  }

This allows different colored chunk indicators for different scope types
(functions, loops, conditionals, etc.) when use_treesitter is enabled.
- Add nix.lua with Nix-specific treesitter node types
- Enables chunk indicators for let expressions, attrsets, lambdas, etc.
- Provides better visual feedback for Nix flake structure

This allows hlchunk to show arrows/indicators for all nested scopes
in Nix files (inputs, outputs, let blocks, attribute sets, etc.)
instead of just top-level scopes.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Nix Tree-sitter node-type coverage for chunk detection and introduces node-type-aware chunk highlighting so chunk indicators can be styled differently based on the matched Tree-sitter scope type.

Changes:

  • Add nix.lua Tree-sitter node-type table and register it under ts_node_type/init.lua.
  • Extend HlChunk.Scope / Tree-sitter chunk range detection to carry the matched node_type.
  • Add node_type_styles configuration and apply it during chunk rendering via dynamically created highlight groups.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
lua/hlchunk/utils/ts_node_type/nix.lua Adds Nix-specific Tree-sitter node types to improve nested-scope detection.
lua/hlchunk/utils/ts_node_type/init.lua Registers the new nix node-type map for filetype lookups.
lua/hlchunk/utils/scope.lua Extends Scope to optionally store the Tree-sitter node_type.
lua/hlchunk/utils/chunkHelper.lua Propagates node_type from Tree-sitter nodes into the returned Scope.
lua/hlchunk/mods/chunk/init.lua Selects highlight group based on Scope.node_type and configured patterns.
lua/hlchunk/mods/chunk/chunk_conf.lua Introduces node_type_styles config option for node-type-based styling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +143 to +144
-- Try to match node_type against configured patterns
for pattern, style in pairs(self.conf.node_type_styles) do
Comment on lines +146 to +150
-- Create a dynamic highlight group for this style
hl_group_counter = hl_group_counter + 1
local hl_name = "HLChunkNodeType" .. hl_group_counter
api.nvim_set_hl(0, hl_name, style)
return hl_name
Comment on lines +43 to +47
-- Node type to style mapping (patterns matched against treesitter node type)
-- If a node type matches multiple patterns, first match wins
-- If no match, falls back to default style[1]
node_type_styles = {
-- Example config (users can override):
Comment on lines 79 to +83
local node_type = cursor_node:type()
local node_start, _, node_end, _ = cursor_node:range()
if node_start ~= node_end and is_suit_type(node_type) then
return cursor_node:has_error() and chunkHelper.CHUNK_RANGE_RET.CHUNK_ERR or chunkHelper.CHUNK_RANGE_RET.OK,
Scope(pos.bufnr, node_start, node_end)
Scope(pos.bufnr, node_start, node_end, node_type)
rec_attrset_expression = true, -- rec { ... }
list_expression = true, -- [ ... ]
lambda_expression = true, -- arg: body
function_expression = true, -- { args }: body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants