diff --git a/autoload/augment.vim b/autoload/augment.vim index b244711..1846815 100644 --- a/autoload/augment.vim +++ b/autoload/augment.vim @@ -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'), @@ -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 diff --git a/autoload/augment/chat.vim b/autoload/augment/chat.vim index a3a9acd..3d86e83 100644 --- a/autoload/augment/chat.vim +++ b/autoload/augment/chat.vim @@ -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 @@ -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 \ No newline at end of file diff --git a/autoload/augment/version.vim b/autoload/augment/version.vim index fdebaf7..a882219 100644 --- a/autoload/augment/version.vim +++ b/autoload/augment/version.vim @@ -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 diff --git a/doc/augment.txt b/doc/augment.txt index 9fd1053..7014e77 100644 --- a/doc/augment.txt +++ b/doc/augment.txt @@ -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*