Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions autoload/augment.vim
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ function! s:CommandChatToggle(range, args) abort
call augment#chat#Toggle()
endfunction

function! s:CommandChatApply(range, args) abort
echo "Augment:Applying code blocks..."
call augment#chat#ApplyCodeBlocks()
echo "Augment: Applied code blocks!"
endfunction

" Handle user commands
let s:command_handlers = {
\ 'log': function('s:CommandLog'),
Expand All @@ -244,6 +250,7 @@ let s:command_handlers = {
\ 'chat': function('s:CommandChat'),
\ 'chat-new': function('s:CommandChatNew'),
\ 'chat-toggle': function('s:CommandChatToggle'),
\ 'chat-apply': function('s:CommandChatApply'),
\ }

function! augment#Command(range, args) abort range
Expand Down
74 changes: 74 additions & 0 deletions autoload/augment/chat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function! s:ResetChatContents() abort
\ . '`:Augment chat-new` Start a new conversation'
\ . "\n"
\ . '`:Augment chat-toggle` Toggle the chat panel visibility'
\ . "\n"
\ . '`:Augment chat-apply` Apply code blocks from the chat panel'
\ . "\n\n")
endfunction

Expand Down Expand Up @@ -215,3 +217,75 @@ function! augment#chat#GetSelectedText() abort
let [line_end, col_end] = getpos("'>")[1:2]
return s:GetBufSelection(line_start, col_start, line_end, col_end)
endfunction

function! augment#chat#ApplyCodeBlocks() abort
call augment#log#Info('Applying code blocks... in the function')
let history = augment#chat#GetHistory()
if empty(history)
call augment#log#Error('No chat history found')
return
endif

" Iterate through all messages in history
for msg in history
let response = msg.response_text
let lines = split(response, "\n")
let in_block = 0
let current_block = {'path': '', 'content': []}

for line in lines
" Check for code block start with path
let block_start = matchlist(line, '````\([^[:space:]]\+\)\s\+path=\(.\+\)\s\+mode=\(.\+\)$')
if !empty(block_start)
let in_block = 1
let current_block.path = block_start[2]
let current_block.content = []
continue
endif

" Check for code block end
if line =~# '^````$'
if in_block && !empty(current_block.path)
" Apply the changes
call s:ApplyCodeBlock(current_block)
endif
let in_block = 0
let current_block = {'path': '', 'content': []}
continue
endif

" Collect content lines
if in_block
call add(current_block.content, line)
endif
endfor
endfor
endfunction

function! s:ApplyCodeBlock(block) abort
" Ensure the directory exists
let dir = fnamemodify(a:block.path, ':h')
if !isdirectory(dir)
call mkdir(dir, 'p')
endif

" Check if file exists
if filereadable(a:block.path)
" Read existing content
let existing_content = readfile(a:block.path)

" Add a newline between existing content and new content if needed
if !empty(existing_content) && !empty(a:block.content)
call add(existing_content, '')
endif

" Append new content
call extend(existing_content, a:block.content)
call writefile(existing_content, a:block.path)
call augment#log#Info('Appended changes to: ' . a:block.path)
else
" Create new file
call writefile(a:block.content, a:block.path)
call augment#log#Info('Created new file: ' . a:block.path)
endif
endfunction
2 changes: 1 addition & 1 deletion autoload/augment/version.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
" MIT License - See LICENSE.md for full terms

function! augment#version#Version() abort
return '0.25.1'
return '0.25.2'
endfunction
3 changes: 3 additions & 0 deletions doc/augment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ The following commands are provided:
`:Augment chat-toggle`
Open/close the chat conversation window.

`:Augment chat-apply`
Apply markdown code blocks from the chat window.


------------------------------------------------------------------------------
Options *augment-options*
Expand Down