JoosepAlviste / nvim-ts-context-commentstring

Neovim treesitter plugin for setting the commentstring based on the cursor location in a file.
MIT License
1.16k stars 35 forks source link

How to setup comment strings with neovim treesitter and commentstring? #4

Closed eduardoarandah closed 3 years ago

eduardoarandah commented 3 years ago

I've been trying to figure out how to setup commentstring for a php file.

I'm trying Treesitterplayground to get information, but have no luck setting up correct comment style.

What is the procedure to find out that info?

image

plugins:

Plug 'tpope/vim-commentary'
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}  
Plug 'nvim-treesitter/playground'
Plug 'JoosepAlviste/nvim-ts-context-commentstring'

treesitter config:

if (has("nvim"))
lua <<EOF
require'nvim-treesitter.configs'.setup {
  ensure_installed ={ "javascript", "json", "css", "php", "html", "python", "bash", "regex", "ruby", "yaml", "jsonc", "tsx", "lua", "vue" }, -- one of "all", "maintained" (parsers with maintainers), or a list of languages
  highlight = {
    enable = true,
  },
  indent = {
    enable = false
  },
  context_commentstring = {
    enable = true,
    config = {
    php = {
      element = "XXXX %s ",
      start_tag = "YYYYYYY %s zzz",
      attribute = "ZZZZZZz %s zzz",
      html = "111111 %s zzz"
    }
    }
  },
  playground = {
    enable = true,
    disable = {},
    updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
    persist_queries = false, -- Whether the query persists across vim sessions
    keybindings = {
      toggle_query_editor = 'o',
      toggle_hl_groups = 'i',
      toggle_injected_languages = 't',
      toggle_anonymous_nodes = 'a',
      toggle_language_display = 'I',
      focus_language = 'f',
      unfocus_language = 'F',
      update = 'R',
      goto_node = '<cr>',
      show_help = '?',
    },
  }
}
EOF
endif
JoosepAlviste commented 3 years ago

Hey! The tree that the PHP treesitter parser creates seems to be a bit weird and it doesn't seem to match the output in the treesitter playground.

For example, in this simple case, the playground shows the tags, but in reality, the HTML part is just text (everything up to <?php):

image

(I checked the current node under the cursor with :lua print(require'nvim-treesitter.ts_utils'.get_node_at_cursor(0):type()))

This means that there is no container node that can be used as a check for this plugin. The only nodes are text and program.

However, if I add a PHP end tag (?>), then there is a text_interpolation node:

image

text_interpolation seems like a good check for setting <!-- %s -->.

Everything inside text_interpolation is once again a text node. This means that any style or script tags cannot be detected 😕

Also, if the PHP tag is wrapped in an HTML tag, then the parser will break:

image

This is the best result I managed to get:

https://user-images.githubusercontent.com/9450943/112734545-1efcbb80-8f4f-11eb-9125-3f25cd6c378b.mov

I used this config:

  php = {
    text_interpolation = '<!-- %s -->',
  },

And setlocal commentstring=//\ %s in after/ftplugin/php.vim


I'm hoping that once https://github.com/JoosepAlviste/nvim-ts-context-commentstring/issues/3 is implemented, we could use language tree to improve this.

JoosepAlviste commented 3 years ago

Hey @eduardoarandah, just wanted to let you know that #3 has been implemented and PHP should work a bit better now.

However, I still didn't get the injected CSS and JS working because it doesn't seem like they even get injected.

Anyways, if you manage to inject those languages, then the current solution should work 😄 I just couldn't figure out how to get the injection working.

Other than that, let me know if there are any other problems with PHP.