vim-ai icon indicating copy to clipboard operation
vim-ai copied to clipboard

offer markdown highlighting in aichat files

Open Konfekt opened this issue 1 year ago • 14 comments
trafficstars

Markdown highlighting within aichat files is controlled by a global variable g:aichat_markdown.

The Vim documentation and the syntax file for aichat have been updated. Markdown highlighting is disabled by default when editing .aichat files, but can be enabled by a user variable.

Konfekt avatar Mar 08 '24 15:03 Konfekt

What is the motivation behind this feature? Enabling full markdown got into conflict with the current aichat file highlighting: image

madox2 avatar Mar 09 '24 12:03 madox2

The output is often markdown with code in codeblock. For highlighting, including codeblocks, just reuse that of markdown.vim and add the vim-ai specificities (for prompt markers). Is in the screenshot code_syntax_enabled = 1 set ?

Konfekt avatar Mar 09 '24 12:03 Konfekt

Yes, the screenshot is taken with code_syntax_enabled = 1. To problem is that markdown is applied to the whole file, not just to the fenced replies.

madox2 avatar Mar 09 '24 13:03 madox2

Yes. What is the correct syntax highlighting for the user input?

One could explicitly override it, but in practice it is often simple text.

This is just a suggestion that seems more maintainable than the ftplugin/aichat.vim with the added benefit of highlighting the markdown output itself (and not only the code blocks).

Konfekt avatar Mar 09 '24 13:03 Konfekt

Regarding the correct highlighting of the user input, an idea could have been to add code fence markers for the current file type, except for text selected in a comment syntax region. However, in the end, users might rather (ab)use the source code file as a scratchpad for sending text into the chat, so that this assumption is perhaps a bit patronizing.

Konfekt avatar Mar 09 '24 15:03 Konfekt

I am not sure is syntax highlighting of the user input is needed. If you for instance use ChatGPT on web, you cannot use formatting your prompt. And it wouldn't make any difference to the model too. I would say formatting code blocks should be enough but I might be wrong

madox2 avatar Mar 09 '24 17:03 madox2

I am not sure is syntax highlighting of the user input is needed.

I agree with you. Markdown highlighting was meant to highlight the output, not input, since the output is often markdown. One can define a user input region to drop syntax highlighting there.

Konfekt avatar Mar 09 '24 19:03 Konfekt

User input is now using default highlighting, whereas the output uses built-in markdown highlighting, in particular for the fenced code blocks. Since (ChatGPT's) output often uses markdown formatting, this (if g:aichat_markdown is set) improves readability and adds highlighting for programming languages not contained in s:filetype_dict in ftplugin/aichat.vim (line 12):

screenshot-2024-03-10-085151 in contrast to screenshot-2024-03-10_085255

Konfekt avatar Mar 10 '24 07:03 Konfekt

Still having the same issue (see screenshot in the comments above). I think you tried to fix it with >>> <<<, that that' boundary isn't respected in my environment

madox2 avatar Mar 24 '24 13:03 madox2

This is somewhat to be expected, as I had the impression that code_syntax_enabled = 1 replicates what g:markdown_fenced_languages= ['cpp', 'vim', 'sh', 'dosini', 'javascript', 'javascriptreact', 'typescript', 'typescriptreact'] would achieve, and therefore becomes dispensable. But I might be completely wrong, as I rarely used fenced highlighting, and there's a point in making both settings work at the same time?

Konfekt avatar Mar 24 '24 18:03 Konfekt

Yes, I think that this solution is much better and I would like to replace the current highlighting with this one. But first I need to get this working. I have found that there is a conflict with the plugin preservim/vim-markdown that I use, which causes the weird highlighting I posted in the screenshot.

madox2 avatar Mar 24 '24 19:03 madox2

Hard to say from afar, but https://github.com/preservim/vim-markdown/blob/a657e697376909c41475a686eeef7fc7a4972d94/ftplugin/markdown.vim#L789 shows that this also does what the built-in markdown ftplugin tries to achieve, so a careful setup is needed to avoid mutual interference

Konfekt avatar Mar 24 '24 20:03 Konfekt

Maybe this way around the highlighting is more robust?

Konfekt avatar Mar 26 '24 15:03 Konfekt

I came here wanting to do the same thing, thanks for your diff it works flawlessly for me !

dargor avatar Jul 19 '24 21:07 dargor