From a2015ced1eb7f98f506259acd13b1c59913381e2 Mon Sep 17 00:00:00 2001 From: Thomas Belote Date: Thu, 20 Mar 2025 10:33:02 -0400 Subject: [PATCH 1/8] adding apply code blocks to chat --- autoload/augment/chat.vim | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/autoload/augment/chat.vim b/autoload/augment/chat.vim index a3a9acd..5858fd7 100644 --- a/autoload/augment/chat.vim +++ b/autoload/augment/chat.vim @@ -215,3 +215,60 @@ 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 + 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\+\(.\+\)$') + 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 + + " Create or overwrite the file + call writefile(a:block.content, a:block.path) + + " Log success + call augment#log#Info('Applied changes to: ' . a:block.path) +endfunction From 79812217eb7c6bfbafed49b0ac22a31fda39615b Mon Sep 17 00:00:00 2001 From: Thomas Belote Date: Thu, 20 Mar 2025 10:39:07 -0400 Subject: [PATCH 2/8] increment version --- autoload/augment/version.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 6acca2444177f7fd55a399a990483b9a4d6cd632 Mon Sep 17 00:00:00 2001 From: Thomas Belote Date: Thu, 20 Mar 2025 10:49:15 -0400 Subject: [PATCH 3/8] adding a command --- autoload/augment.vim | 5 +++++ autoload/augment/version.vim | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/autoload/augment.vim b/autoload/augment.vim index b244711..89c334c 100644 --- a/autoload/augment.vim +++ b/autoload/augment.vim @@ -233,6 +233,10 @@ function! s:CommandChatToggle(range, args) abort call augment#chat#Toggle() endfunction +function! s:CommandChatApply(range, args) abort + call augment#chat#ApplyCodeBlocks() +endfunction + " Handle user commands let s:command_handlers = { \ 'log': function('s:CommandLog'), @@ -244,6 +248,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/version.vim b/autoload/augment/version.vim index a882219..a651d37 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.2' + return '0.25.3' endfunction From df89e530ad653b42167e9c61cc48fb15f1ff7c61 Mon Sep 17 00:00:00 2001 From: Thomas Belote Date: Thu, 20 Mar 2025 10:57:09 -0400 Subject: [PATCH 4/8] rev --- autoload/augment.vim | 1 + autoload/augment/chat.vim | 3 +++ autoload/augment/version.vim | 2 +- doc/augment.txt | 3 +++ 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/autoload/augment.vim b/autoload/augment.vim index 89c334c..2ba4552 100644 --- a/autoload/augment.vim +++ b/autoload/augment.vim @@ -234,6 +234,7 @@ function! s:CommandChatToggle(range, args) abort endfunction function! s:CommandChatApply(range, args) abort + echo "Applying code blocks..." call augment#chat#ApplyCodeBlocks() endfunction diff --git a/autoload/augment/chat.vim b/autoload/augment/chat.vim index 5858fd7..673c534 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 @@ -217,6 +219,7 @@ function! augment#chat#GetSelectedText() abort endfunction function! augment#chat#ApplyCodeBlocks() abort + call augment#log#Info('Applying code blocks...') let history = augment#chat#GetHistory() if empty(history) call augment#log#Error('No chat history found') diff --git a/autoload/augment/version.vim b/autoload/augment/version.vim index a651d37..3d2292d 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.3' + return '0.25.4' 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* From ec14adbf1bb897b5f3ef034d243a12c82bd3e09e Mon Sep 17 00:00:00 2001 From: Thomas Belote Date: Thu, 20 Mar 2025 10:59:51 -0400 Subject: [PATCH 5/8] more logging --- autoload/augment/chat.vim | 6 +++--- autoload/augment/version.vim | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/autoload/augment/chat.vim b/autoload/augment/chat.vim index 673c534..e3ec14c 100644 --- a/autoload/augment/chat.vim +++ b/autoload/augment/chat.vim @@ -219,7 +219,7 @@ function! augment#chat#GetSelectedText() abort endfunction function! augment#chat#ApplyCodeBlocks() abort - call augment#log#Info('Applying code blocks...') + 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') @@ -235,7 +235,7 @@ function! augment#chat#ApplyCodeBlocks() abort for line in lines " Check for code block start with path - let block_start = matchlist(line, '```\([^[:space:]]\+\)\s\+\(.\+\)$') + let block_start = matchlist(line, '````\([^[:space:]]\+\)\s\+\(.\+\)$') if !empty(block_start) let in_block = 1 let current_block.path = block_start[2] @@ -244,7 +244,7 @@ function! augment#chat#ApplyCodeBlocks() abort endif " Check for code block end - if line =~# '^```$' + if line =~# '^````$' if in_block && !empty(current_block.path) " Apply the changes call s:ApplyCodeBlock(current_block) diff --git a/autoload/augment/version.vim b/autoload/augment/version.vim index 3d2292d..9d7079d 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.4' + return '0.25.5' endfunction From 12ef530dce73dcee6c81aa04b99ca390d3bd809e Mon Sep 17 00:00:00 2001 From: Thomas Belote Date: Thu, 20 Mar 2025 11:04:30 -0400 Subject: [PATCH 6/8] fixing path maybe --- autoload/augment/chat.vim | 2 +- autoload/augment/version.vim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/augment/chat.vim b/autoload/augment/chat.vim index e3ec14c..bb087f5 100644 --- a/autoload/augment/chat.vim +++ b/autoload/augment/chat.vim @@ -235,7 +235,7 @@ function! augment#chat#ApplyCodeBlocks() abort for line in lines " Check for code block start with path - let block_start = matchlist(line, '````\([^[:space:]]\+\)\s\+\(.\+\)$') + 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] diff --git a/autoload/augment/version.vim b/autoload/augment/version.vim index 9d7079d..32a357e 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.5' + return '0.25.6' endfunction From 422c459c99d8dc6dffc537a3ef9b19230f1e3364 Mon Sep 17 00:00:00 2001 From: Thomas Belote Date: Thu, 20 Mar 2025 11:06:44 -0400 Subject: [PATCH 7/8] more text --- autoload/augment.vim | 3 ++- autoload/augment/version.vim | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/autoload/augment.vim b/autoload/augment.vim index 2ba4552..1846815 100644 --- a/autoload/augment.vim +++ b/autoload/augment.vim @@ -234,8 +234,9 @@ function! s:CommandChatToggle(range, args) abort endfunction function! s:CommandChatApply(range, args) abort - echo "Applying code blocks..." + echo "Augment:Applying code blocks..." call augment#chat#ApplyCodeBlocks() + echo "Augment: Applied code blocks!" endfunction " Handle user commands diff --git a/autoload/augment/version.vim b/autoload/augment/version.vim index 32a357e..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.6' + return '0.25.2' endfunction From 4cf73bd761dd25dbeb7b39c906a6b4b0529c3a63 Mon Sep 17 00:00:00 2001 From: Thomas Belote Date: Thu, 20 Mar 2025 11:17:14 -0400 Subject: [PATCH 8/8] append if existing --- autoload/augment/chat.vim | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/autoload/augment/chat.vim b/autoload/augment/chat.vim index bb087f5..3d86e83 100644 --- a/autoload/augment/chat.vim +++ b/autoload/augment/chat.vim @@ -269,9 +269,23 @@ function! s:ApplyCodeBlock(block) abort call mkdir(dir, 'p') endif - " Create or overwrite the file - call writefile(a:block.content, a:block.path) + " 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 - " Log success - call augment#log#Info('Applied changes to: ' . a:block.path) -endfunction + " 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