Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect commentstring inside <?php ?> tags #112

Open
nipunlakshank opened this issue May 23, 2024 · 2 comments
Open

Incorrect commentstring inside <?php ?> tags #112

nipunlakshank opened this issue May 23, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@nipunlakshank
Copy link

nipunlakshank commented May 23, 2024

Minimal reproducible full config

-- Install lazy.nvim automatically
local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system {
    'git',
    'clone',
    '--filter=blob:none',
    'https://github.com/folke/lazy.nvim.git',
    '--branch=stable', -- latest stable release
    lazypath,
  }
end
vim.opt.rtp:prepend(lazypath)

-- Or some other small value (Vim default is 4000)
vim.opt.updatetime = 200

require('lazy').setup {
  {
    'nvim-treesitter/nvim-treesitter',
    build = ':TSUpdate',
    config = function()
      require('nvim-treesitter.configs').setup {
        ensure_installed = { 'vim', 'lua', 'php' },
        highlight = {
          enable = true,
        },
      }
    end,
  },`
`  {
    'numToStr/Comment.nvim',
    lazy = true,
    event = { "BufReadPre", "BufNewFile" },
    dependencies = {
          'JoosepAlviste/nvim-ts-context-commentstring',
    }
    config = function()
      require('Comment').setup {
          pre_hook = require("ts_context_commentstring.integrations.comment_nvim").create_pre_hook(),
      }
    end,
  },
}

Description

In php files commenting inside a <php ?> tag that is inside an html tag results in adding a html commentstring instead of php commentstring
Ex:

<html>
<?php
for ($i = 0; $i < 10; $i++) {
    print_r($i);
}
?>
... more html or php
</html>

Steps to reproduce

  • Make a file called something.php
  • Write some html
  • Now inside an html tag, open a tag
  • Inside that php tag write some multiline php (not in one line)
  • Now try commenting something inside that php tag

Ex:

<html>
<?php
for ($i = 0; $i < 10; $i++) {
    // try commenting below line
    print_r($i);
}
?>
</html>

Expected behavior

<html>
<?php
for ($i = 0; $i < 10; $i++) {
    // after commenting below line
    // print_r($i);
}
?>
</html>

Actual behavior

<html>
<?php
for ($i = 0; $i < 10; $i++) {
    // after commenting below line
    <!-- print_r($i); -->
}
?>
</html>

Additional context

No response

@nipunlakshank nipunlakshank added the bug Something isn't working label May 23, 2024
@JoosepAlviste
Copy link
Owner

Hey! It looks like this is indeed a problem. I think that it's because the PHP treesitter parser is kinda weird. The PHP and HTML trees both cover the entire buffer and aren't nested inside each other. When we're looping over the language trees here, we first see PHP and then HTML. This means that the HTML tree will always overwrite the PHP one.

I'm not sure what the best solution would be, we might need to add a special case for PHP in that function (something like "if previous tree PHP and new tree HTML, then don't overwrite"), though that might break the cases if we actually do want to use the HTML commentstring inside the PHP parts.

Will need to play around with this a little bit since there isn't an obvious solution to this 😕

@mokorana
Copy link

mokorana commented Dec 3, 2024

Is there any progress on this one? Can we somehow help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants