AnyEvent-MCP

 view release on metacpan or  search on metacpan

vim/plugin/mcp-vim.vim  view on Meta::CPAN

          \ 'filetype':    getbufvar (info.bufnr, '&filetype'),
          \ })
  endfor

  return {
        \ 'cwd':            getcwd (),
        \ 'current_buffer': bufnr ('%'),
        \ 'cursor':         {'line': line ('.'), 'column': col ('.')},
        \ 'buffers':        buffers,
        \ }
endfunction

function! s:rpc_call_open (path) abort
  let buf = bufadd (a:path)

  if !buf
    throw 'rpc/cannot_add_buffer'
  endif

  call setbufvar (buf, '&buflisted', 1)
  let info = s:rpc_bufinfo (buf)

  return {
        \ 'buffer':      buf,
        \ 'filename':    info.name,
        \ 'changedtick': info.changedtick,
        \ 'line_count':  info.linecount,
        \ 'modified':    info.changed,
        \ }
endfunction

function! s:rpc_call_apply_patch (buf, patch, max_offsets, expected_changedtick) abort
  return s:rpc_in_buffer (a:buf, {-> s:rpc_apply_patch (
        \ a:patch,
        \ a:max_offsets,
        \ a:expected_changedtick
        \ )}, v:false)
endfunction

function! s:rpc_apply_patch (patch, max_offsets, expected_changedtick) abort
  call s:rpc_expect_tick (a:expected_changedtick)

  if !&modifiable
    throw 'rpc/not_modifiable'
  endif

  let old_lines = getline (1, '$')
  let oldfile   = tempname ()
  let newfile   = tempname ()

  try
    " Patch complete logical lines, independently of the buffer's &endofline.
    " writefile(..., 'b') needs a final empty list item to write the last NL.
    if writefile (old_lines + [''], oldfile, 'b')
      throw 'rpc/patch_write_failed'
    endif

    " patch does all unified-diff parsing/matching.  It never modifies the
    " Vim buffer.  Keep rejects out of the filesystem.
    let cmd = 'LC_ALL=C patch --unified --batch --forward --get=0 --fuzz=0'
          \ . ' --reject-file=- --no-backup-if-mismatch'
          \ . ' -o ' . shellescape (newfile)
          \ . ' ' . shellescape (oldfile)
          \ . ' 2>&1'

    let output = system (cmd, a:patch)
    let status = v:shell_error

    if status
      throw printf (
            \ 'rpc/patch_failed: exit=%d %s',
            \ status,
            \ substitute (trim (output), "\n", ' | ', 'g')
            \ )
    endif

    " --fuzz=0 still permits relocating whole hunks.  The MCP wrapper rejects
    " ambiguous pure insertions and limits relocation by distinct old-side lines.
    for line in split (output, "\n")
      let match = matchlist (line, '^Hunk #\(\d\+\).*\<offset\s\+\([+-]\=\d\+\)\s\+lines\?\>')

      if !empty (match)
        let hunk   = str2nr (match[1])
        let offset = str2nr (match[2])

        if hunk < 1 || hunk > len (a:max_offsets)
          throw 'rpc/patch_bad_output: ' . substitute (trim (output), "\n", ' | ', 'g')
        endif

        if abs (offset) > a:max_offsets[hunk - 1]
          throw printf ('rpc/patch_needs_more_context: hunk=%d offset=%d max=%d %s',
                \ hunk, offset, a:max_offsets[hunk - 1],
                \ substitute (trim (output), "\n", ' | ', 'g'))
        endif
      endif
    endfor

    " Require complete lines in nonempty patch output.  A diff containing
    " a new-side no-EOL marker can otherwise remove the final NL.
    " Empty output represents Vim's single empty logical line.
    let new_lines = readfile (newfile, 'b')

    if !empty (new_lines)
      if new_lines[-1] !=# ''
        throw 'rpc/patch_missing_eol'
      endif
      call remove (new_lines, -1)
    endif

    " Vim always has at least one logical line.
    if empty (new_lines)
      let new_lines = ['']
    endif

    let hunks = diff (old_lines, new_lines, {'output': 'indices'})

    if empty (hunks)
      return {
            \ 'changedtick': b:changedtick,
            \ 'hunks':       0,
            \ 'noop':        v:true,



( run in 1.449 second using v1.01-cache-2.11-cpan-99fa8243a42 )