madox2 / vim-ai

AI-powered code assistant for Vim. OpenAI and ChatGPT plugin for Vim and Neovim.
MIT License
629 stars 53 forks source link

Wrapping selections for code higlighting #36

Open GordianDziwis opened 1 year ago

GordianDziwis commented 1 year ago

I have wrapped AIChatRun calls with those functions for getting a nicer prompt:

function! CodeReviewFn(range) range
  let l:lines = trim(join(getline(a:firstline, a:lastline), "\n"))
  let l:initial_prompt = ['>>> system', 'As a clean coding expert, review the code with the highest standards. Make the code more expressive and concise, using comments only when necessary. Also, consider the best practices for ' . &filetype . '.']
  let l:prompt = '```' . &filetype  . "\n" . l:lines . "\n```"
  let l:config = {
  \  'options': {
  \    'initial_prompt': l:initial_prompt,
  \  },
  \}
  '<,'>call vim_ai#AIChatRun(v:false, l:config, l:prompt)
endfunction
command! -range AIChatCodeReview <line1>,<line2>call CodeReviewFn(<range>)

function! g:vim_ai_mod.AIChatWrapper(range, ...) range
  let l:lines = trim(join(getline(a:firstline, a:lastline), "\n"))
  let l:instruction = a:0 ? a:1 : ''
  let l:prompt = l:instruction . "\n```" . &filetype  . "\n" . l:lines . "\n```"
  '<,'>call vim_ai#AIChatRun(v:false, {}, l:prompt)
endfunction
command! -range -nargs=? AIChatWrapper <line1>,<line2>call g:vim_ai_mod.AIChatWrapper(<range>, <f-args>)

And by the way with treesitter there is no need to define embedded syntaxes, this is already included.

madox2 commented 1 year ago

Looks nice, you could do similar thing but without &filetype annotation with a selection boundary:

let g:vim_ai_chat = {
\  "options": {
\    "selection_boundary":  "```",
\  },
\}
GordianDziwis commented 1 year ago

This is nicer, but it is not possible to add &filetype for syntax highlighting. Maybe having a pre_selection_boundary and a post_selection_boundary could be the solution?

madox2 commented 1 year ago

In this case I can imagine making it a little bit smarter - if the boundary is ```, it is considered a standard markdown boundary and the filetype is added automatically