summaryrefslogtreecommitdiff
path: root/plugin/vim-ai.vim
blob: 1e9407f83c903c7dbe369df9dd459863f47382fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
let s:plugin_root = expand('<sfile>:p:h:h')
let s:complete_py = s:plugin_root . "/py/complete.py"
let s:chat_py = s:plugin_root . "/py/chat.py"
let s:debug = 0

function! ScratchWindow()
  below new
  setlocal buftype=nofile
  setlocal bufhidden=hide
  setlocal noswapfile
  setlocal ft=aichat
endfunction

function! AIRun(...) range
  let lines = trim(join(getline(a:firstline, a:lastline), "\n"))
  let selection = trim(@*)
  let is_selection = lines != "" && lines == selection
  let has_instruction = a:0
  let prompt = ""
  if has_instruction
    if is_selection
      let prompt = a:1 . ":\n" . lines
    else
      let prompt = a:1
    endif
  else
    let prompt = lines
  endif

  if s:debug
    echo prompt
  endif

  echo "Completing..."
  let output = system("echo " . shellescape(prompt) . " | python3 " . s:complete_py . " ")
  let output = trim(output)

  execute "normal! " . a:lastline . "G"
  set paste
  execute "normal! o" . output . "\<Esc>"
  set nopaste
  execute "normal! " . a:lastline . "G"
endfunction

function! AIEditRun(...) range
  let has_instruction = a:0
  let prompt = trim(join(getline(a:firstline, a:lastline), "\n"))
  if has_instruction
    let prompt = a:1 . ":\n" . prompt
  endif

  let buff_lastline = line('$')

  if s:debug
    echo prompt
  endif

  echo "Editing..."
  let output = system("echo " . shellescape(prompt) . " | python3 " . s:complete_py . " ")
  let output = trim(output)

  execute a:firstline . ',' . a:lastline . 'd'

  set paste
  if a:lastline == buff_lastline
    execute "normal! o" . output . "\<Esc>"
  else
    execute "normal! O" . output . "\<Esc>"
  endif
  set nopaste
endfunction

function! AIChatRun(...) range
  let lines = trim(join(getline(a:firstline, a:lastline), "\n"))
  let selection = trim(@*)
  let is_selection = lines != "" && lines == selection
  let has_instruction = a:0

  let prompt = ""
  if search('^>>> user$', 'nw') != 0
    " inside chat window
    let prompt = trim(join(getline(1, '$'), "\n"))
  else
    " outside chat window
    call ScratchWindow()
    if has_instruction
      if is_selection
        let prompt = a:1 . ":\n" . lines
      else
        let prompt = a:1
      endif
    else
      if is_selection
        let prompt = lines
      else
        execute "normal i>>> user\<Enter>\<Enter>"
        return
      endif
    endif
  endif

  if s:debug
    echo prompt
  endif

  echo "Answering..."
  let output = system("echo " . shellescape(prompt) . " | python3 " . s:chat_py . " ")

  set paste
  execute "normal! ggdG"
  execute "normal! i" . output . "\<Esc>"
  set nopaste
endfunction

command! -range -nargs=? AI <line1>,<line2>call AIRun(<f-args>)
command! -range -nargs=? AIEdit <line1>,<line2>call AIEditRun(<f-args>)
command! -range -nargs=? AIChat <line1>,<line2>call AIChatRun(<f-args>)