Skip to content

Commenting using gc loads copilot-chat syntax in unrelated markdown buffers #1572

Description

@liskin

Reproducer:

  1. nvim x.md
  2. add two lines:
    a/b
    x
    
  3. go to the "x" line and comment it using the gc mapping
  4. observe that "/b" is suddenly highlighted as CopilotChatPrompt despite this being a normal markdown buffer

Cause:

vim.treesitter.language.register('markdown', 'copilot-chat')

vim.api.nvim_create_autocmd('FileType', {
pattern = 'copilot-chat',
group = group,
callback = vim.schedule_wrap(function()
vim.cmd.syntax('match CopilotChatResource "#\\S\\+"')
vim.cmd.syntax('match CopilotChatTool "@\\S\\+"')
vim.cmd.syntax('match CopilotChatPrompt "/\\S\\+"')
vim.cmd.syntax('match CopilotChatModel "\\$\\S\\+"')
vim.cmd.syntax('match CopilotChatUri "##\\S\\+"')
end),
})

https://github.com/neovim/neovim/blob/release-0.12/runtime/lua/vim/_comment.lua#L47-L49

vim.filetype.get_option triggers FileType autocommands the first time it's called for a given filetype, so the first markdown buffer where gc is used will have syntax rules for copilot-chat unfortunately.

One might argue that this is a neovim bug actually - using all filetypes associated with markdown, even if we know the exact filetype in that buffer, seems quite stupid. I'm not sure what the right way to fix this is, yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions