Indentation issue in YAML Sequence/Arrays

Open
#5,653 0 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
yaml
Domain
tooling

Research direction

Reproduce the issue in a YAML buffer with nvim-treesitter indentation enabled, using the sequence followed by foo: bar shown in the report. Trace the YAML indentation entry point used by nvim-treesitter and verify that inserting a new sequence item preserves the sequence indentation in both file layouts.

Written by the indexing model from the issue text.

Description

Describe the bug

When adding elements to a YAML sequence/array, typing a dash (-) causes the line to be de-indented to column 1 of the current line.

This seems to only happen if the YAML sequence/array is superceded by other content.

To Reproduce
  1. Create a new YAML file: vim /tmp/test.yaml

  2. Insert a sequence/array:

    test:
      - 1
      - 2
      - 3 # so far so good
    
  3. Insert arbitrary content after the sequence/array:

    test:
      - 1
      - 2
      - 3 # so far so good
    
    foo: bar
    
  4. Go back to add a new element to the test array:

    test:
      - 1
      - 2
      - 3 # Good!
    - 4 # Bad! This got de-indented to column 1
    
    foo: bar
    
  5. Move foo: bar to the beginning of the file and add a new element to the array:

    foo: bar
    
    test:
      - 1
      - 2
      - 3 # Good!
      - 4 # Good! 
    
Expected behavior

New elements added to YAML sequences/arrays should be indented correctly.

Output of :checkhealth nvim-treesitter
==============================================================================
nvim-treesitter: require("nvim-treesitter.health").check()

Installation ~
- OK `tree-sitter` found 0.20.8 (parser generator, only needed for :TSInstallFromGrammar)
- OK `node` found v21.1.0 (only needed for :TSInstallFromGrammar)
- OK `git` executable found.
- OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
  Version: Apple clang version 15.0.0 (clang-1500.0.40.1)
- OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

OS Info:
{
  machine = "arm64",
  release = "23.1.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103"
} ~

Parser/Features         H L F I J
  - bash                ✓ ✓ ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - comment             ✓ . . . .
  - csv                 ✓ . . . .
  - diff                ✓ . . . .
  - dockerfile          ✓ . . . ✓
  - git_config          ✓ . . . .
  - git_rebase          ✓ . . . ✓
  - gitattributes       ✓ . . . ✓
  - gitcommit           ✓ . . . ✓
  - gitignore           ✓ . . . .
  - go                  ✓ ✓ ✓ ✓ ✓
  - hcl                 ✓ . ✓ ✓ ✓
  - http                ✓ . . . ✓
  - ini                 ✓ . ✓ . .
  - jq                  ✓ . . . ✓
  - json                ✓ ✓ ✓ ✓ .
  - jsonc               ✓ ✓ ✓ ✓ ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - make                ✓ . ✓ . ✓
  - markdown            ✓ . ✓ ✓ ✓
  - markdown_inline     ✓ . . . ✓
  - mermaid             ✓ . . . .
  - python              ✓ ✓ ✓ ✓ ✓
  - query               ✓ ✓ ✓ ✓ ✓
  - requirements        ✓ . . . ✓
  - rust                ✓ ✓ ✓ ✓ ✓
  - terraform           ✓ . ✓ ✓ ✓
  - toml                ✓ ✓ ✓ ✓ ✓
  - vhs                 ✓ . . . .
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓
  - yaml                ✓ ✓ ✓ ✓ ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang} ~

Output of nvim --version
$ nvim  --version
NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1699524327

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/opt/homebrew/Cellar/neovim/0.9.4/share/nvim"

Run :checkhealth for more info
Additional context

Workaround:

Disable nvim-treesitter indentation for YAML filetypes.

indent = {
  disable = { 'yaml' }
}
Dominant language
Tree-sitter Query
Stars
14.4k
Forks
1.4k
Avg merge
4h 35m
Merged PRs (30d)
13

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nvim-treesitter/nvim-treesitter

All issues in nvim-treesitter/nvim-treesitter

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.