nvim-treesitter / nvim-treesitter

Nvim Treesitter configurations and abstraction layer
Apache License 2.0
10.82k stars 899 forks source link

Unexpected indentation after closing tag in tsx/jsx #3986

Closed wyattlefevre closed 1 year ago

wyattlefevre commented 1 year ago

Describe the bug

In tsx/jsx files, treesitter appears to be indenting incorrectly on lines directly beneath closing tags where the closing tag is own its own line.

To Reproduce

Create a simple tsx or jsx file and create a new line beneath a closing tag that is on its own line.

Expected behavior

Indentation should be in line with closing tag. Instead, the cursor is indented one too many times.

Output of :checkhealth nvim-treesitter

nvim-treesitter: require("nvim-treesitter.health").check()
========================================================================
## Installation
  - OK: `tree-sitter` found 0.20.7 (b268e412ad4848380166af153300464e5a1cf83f) (parser generator, only needed for :TSInstallFromGrammar)
  - OK: `node` found v18.12.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 14.0.0 (clang-1400.0.29.202)
  - OK: Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

## OS Info:
{
  machine = "x86_64",
  release = "22.1.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:54 PDT 2022; root:xnu-8792.41.9~2/RELEASE_X86_64"
}

## Parser/Features H L F I J
  - lua            ✓ ✓ ✓ ✓ ✓
  - pascal         ✓ ✓ ✓ ✓ ✓
  - gitattributes  ✓ . . . ✓
  - phpdoc         ✓ . . . .
  - git_rebase     ✓ . . . ✓
  - comment        ✓ . . . .
  - gitcommit      ✓ . . . ✓
  - jsdoc          ✓ . . . .
  - blueprint      ✓ . . . .
  - sparql         ✓ ✓ ✓ ✓ ✓
  - sql            ✓ . . . ✓
  - gdscript       ✓ ✓ . ✓ ✓
  - bash           ✓ ✓ ✓ . ✓
  - arduino        ✓ ✓ ✓ ✓ ✓
  - html           ✓ ✓ ✓ ✓ ✓
  - godot_resource ✓ ✓ ✓ . .
  - markdown       ✓ . ✓ . ✓
  - mermaid        ✓ . . . .
  - hcl            ✓ . ✓ ✓ ✓
  - ebnf           ✓ . . . .
  - glimmer        ✓ . . . .
  - verilog        ✓ ✓ ✓ . ✓
  - latex          ✓ . ✓ . ✓
  - svelte         ✓ . ✓ ✓ ✓
  - foam           ✓ ✓ ✓ ✓ ✓
  - beancount      ✓ . ✓ . .
  - c_sharp        ✓ ✓ ✓ . ✓
  - rnoweb         ✓ . ✓ . ✓
  - bibtex         ✓ . ✓ ✓ .
  - agda           ✓ . ✓ . .
  - zig            ✓ . ✓ ✓ ✓
  - cmake          ✓ . ✓ . .
  - meson          ✓ . ✓ . ✓
  - qmljs          ✓ . ✓ . .
  - help           ✓ . . . ✓
  - racket         ✓ . ✓ . ✓
  - pioasm         ✓ . . . ✓
  - hjson          ✓ ✓ ✓ ✓ ✓
  - rego           ✓ . . . ✓
  - hocon          ✓ . . . ✓
  - llvm           ✓ . . . .
  - clojure        ✓ ✓ ✓ . ✓
  - prisma         ✓ . . . .
  - make           ✓ . ✓ . ✓
  - rasi           ✓ ✓ ✓ ✓ .
  - cpp            ✓ ✓ ✓ ✓ ✓
  - cuda           ✓ ✓ ✓ ✓ ✓
  - norg           . . . . .
  - v              ✓ ✓ ✓ ✓ ✓
  - hlsl           ✓ ✓ ✓ ✓ ✓
  - regex          ✓ . . . .
  - vala           ✓ . ✓ . .
  - dot            ✓ . . . ✓
  - lalrpop        ✓ ✓ . . ✓
  - rust           ✓ ✓ ✓ ✓ ✓
  - solidity       ✓ . . . .
  - ledger         ✓ . ✓ ✓ ✓
  - cooklang       ✓ . . . .
  - elvish         ✓ . . . ✓
  - astro          ✓ ✓ ✓ ✓ ✓
  - gomod          ✓ . . . ✓
  - wgsl           ✓ . ✓ ✓ .
  - gowork         ✓ . . . ✓
  - surface        ✓ . ✓ ✓ ✓
  - m68k           ✓ ✓ ✓ . ✓
  - graphql        ✓ . . ✓ ✓
  - eex            ✓ . . . ✓
  - heex           ✓ ✓ ✓ ✓ ✓
  - ocaml          ✓ ✓ ✓ . ✓
  - perl           ✓ . ✓ . ✓
  - ocaml_interface✓ ✓ ✓ . ✓
  - fish           ✓ ✓ ✓ ✓ ✓
  - ocamllex       ✓ . . . ✓
  - php            ✓ ✓ ✓ ✓ ✓
  - java           ✓ ✓ . ✓ ✓
  - swift          ✓ ✓ . . .
  - kotlin         ✓ ✓ ✓ . ✓
  - tiger          ✓ ✓ ✓ ✓ ✓
  - todotxt        ✓ . . . .
  - sxhkdrc        ✓ . ✓ . ✓
  - jsonnet        ✓ . . . .
  - css            ✓ . ✓ ✓ ✓
  - scss           ✓ . . ✓ .
  - erlang         ✓ . ✓ . .
  - d              ✓ . ✓ ✓ ✓
  - r              ✓ ✓ . ✓ ✓
  - python         ✓ ✓ ✓ ✓ ✓
  - proto          ✓ . ✓ . .
  - scala          ✓ . ✓ . ✓
  - supercollider  ✓ ✓ ✓ ✓ ✓
  - slint          ✓ . . ✓ .
  - markdown_inline✓ . . . ✓
  - tlaplus        ✓ ✓ ✓ . ✓
  - jq             ✓ . . . ✓
  - awk            ✓ . . . ✓
  - toml           ✓ ✓ ✓ ✓ ✓
  - vhs            ✓ . . . .
  - diff           ✓ . . . .
  - twig           ✓ . . . ✓
  - nickel         ✓ . . . .
  - org            . . . . .
  - pug            ✓ . . . ✓
  - tsx            ✓ ✓ ✓ ✓ ✓
  - menhir         ✓ . . . ✓
  - vue            ✓ . ✓ ✓ ✓
  - http           ✓ . . . ✓
  - devicetree     ✓ ✓ ✓ ✓ ✓
  - jsonc          ✓ ✓ ✓ ✓ ✓
  - gleam          ✓ ✓ ✓ ✓ ✓
  - commonlisp     ✓ ✓ ✓ . .
  - elixir         ✓ ✓ ✓ ✓ ✓
  - yaml           ✓ ✓ ✓ ✓ ✓
  - ruby           ✓ ✓ ✓ ✓ ✓
  - yang           ✓ . ✓ ✓ ✓
  - glsl           ✓ ✓ ✓ ✓ ✓
  - c              ✓ ✓ ✓ ✓ ✓
  - ninja          ✓ . ✓ ✓ .
  - fusion         ✓ ✓ ✓ ✓ .
  - dockerfile     ✓ . . . ✓
  - nix            ✓ ✓ ✓ . ✓
  - json           ✓ ✓ ✓ ✓ .
  - vim            ✓ ✓ ✓ . ✓
  - dart           ✓ ✓ . ✓ ✓
  - json5          ✓ . . . ✓
  - typescript     ✓ ✓ ✓ ✓ ✓
  - rst            ✓ ✓ . . ✓
  - query          ✓ ✓ ✓ ✓ ✓
  - fennel         ✓ ✓ . . ✓
  - scheme         ✓ . ✓ . ✓
  - teal           ✓ ✓ ✓ ✓ ✓
  - turtle         ✓ ✓ ✓ ✓ ✓
  - go             ✓ ✓ ✓ ✓ ✓
  - ql             ✓ ✓ ✓ ✓ ✓
  - julia          ✓ ✓ ✓ ✓ ✓
  - gitignore      ✓ . . . .
  - javascript     ✓ ✓ ✓ ✓ ✓

  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 v0.8.1
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by brew@Ventura

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/Cellar/neovim/0.8.1/share/nvim"

Run :checkhealth for more info

Additional context

https://user-images.githubusercontent.com/25963718/208246624-eeecd829-5c41-4ca5-84be-435c44a63edb.mov

local configs = require("nvim-treesitter.configs")
configs.setup({
  ensure_installed = "all",
  sync_install = false,
  ignore_install = { "elm", "embedded_template", "fortran", "hack", "haskell" }, -- List of parsers to ignore installing
  highlight = {
    enable = true, -- false will disable the whole extension
    disable = { "" }, -- list of language that will be disabled
    additional_vim_regex_highlighting = true,
  },
  indent = { enable = true, disable = { "yaml", "ruby", "python", "golang", "c", "html" } },
  context_commentstring = {
    enable = true,
    enable_autcmd = false,
  },
})
lucario387 commented 1 year ago

@wyattlefevre ello, can you have a look at #4086, and use the queries in your files to check if in works or not? You can branch to that or paste the below into init.lua Just wanna check if it works on your end or not (or is this the intended behavior, sorry i have little jsx knowledge, but the issue seems similar to #4076), then I'll add some tests to ask for approval

vim.treesitter.query.set_query("jsx", "indents", [[
; query
[
  (jsx_fragment)
  (jsx_element)
  (jsx_self_closing_element)
] @indent

(parenthesized_expression) @indent

(jsx_closing_element ">" @indent_end)
(jsx_self_closing_element ">" @indent_end)
(
  jsx_fragment
    ("<" ">" "<" "/" ">" @indent_end) @branch ;this is to replicate the fragment so that we can mark the indent end
)

[
  (jsx_closing_element)
  ">"
] @branch
]])
wyattlefevre commented 1 year ago

@lucario387 sorry I'm just getting to this. The new update seems to have fixed things on my end. Thank you so much!