From a5a35a1edec3ee769e5e1b93fe1b9b1673059a9b Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Tue, 27 Jun 2023 16:48:47 -0700 Subject: checkpoint: first version of smoother diffs --- continuedev/src/continuedev/steps/core/core.py | 76 ++++++++++++++++++-------- 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/continuedev/src/continuedev/steps/core/core.py b/continuedev/src/continuedev/steps/core/core.py index ac3fda38..16ca18a1 100644 --- a/continuedev/src/continuedev/steps/core/core.py +++ b/continuedev/src/continuedev/steps/core/core.py @@ -280,8 +280,14 @@ class DefaultModelEditCodeStep(Step): lines = [] unfinished_line = "" + # Don't end the block until you've matched N simultaneous lines + # This helps avoid many tiny blocks + LINES_TO_MATCH_BEFORE_ENDING_BLOCK = 2 + matched_lines_at_end_of_block = 0 + index_of_last_matched_line = -1 + async def handle_generated_line(line: str): - nonlocal lines, current_block_start, current_line_in_file, original_lines, original_lines_below_previous_blocks, current_block_lines, offset_from_blocks + nonlocal lines, current_block_start, current_line_in_file, original_lines, original_lines_below_previous_blocks, current_block_lines, offset_from_blocks, matched_lines_at_end_of_block, index_of_last_matched_line, LINES_TO_MATCH_BEFORE_ENDING_BLOCK # Highlight the line to show progress await sdk.ide.highlightCode(RangeInFile(filepath=rif.filepath, range=Range.from_shorthand( @@ -295,28 +301,52 @@ class DefaultModelEditCodeStep(Step): return # We are in a block currently, and checking for whether it should be ended - for i in range(len(original_lines_below_previous_blocks)): - og_line = original_lines_below_previous_blocks[i] - if og_line == line: - - # Insert the suggestion - replacement = "\n".join(current_block_lines) - await sdk.ide.showSuggestion(FileEdit( - filepath=rif.filepath, - range=Range.from_shorthand( - current_block_start, 0, current_block_start + i, 0), - replacement=replacement - )) - if replacement == "": - current_line_in_file += 1 - - # Reset current block / update variables - original_lines_below_previous_blocks = original_lines_below_previous_blocks[ - i + 1:] - offset_from_blocks += len(current_block_lines) - current_block_lines = [] - current_block_start = -1 - return + if matched_lines_at_end_of_block == 0: + # Find the first matching line + for i in range(len(original_lines_below_previous_blocks)): + og_line = original_lines_below_previous_blocks[i] + # TODO: It's a bit sus to be disqualifying empty lines. + # What you ideally do is find ALL matches, and then throw them out as you check the following lines + if og_line == line and og_line.strip() != "": + matched_lines_at_end_of_block = 1 + index_of_last_matched_line = i + break + else: + # Check if the next line matches + index_of_line_to_match = index_of_last_matched_line + matched_lines_at_end_of_block + if len(original_lines_below_previous_blocks) > index_of_line_to_match and original_lines_below_previous_blocks[index_of_line_to_match] == line: + if matched_lines_at_end_of_block >= LINES_TO_MATCH_BEFORE_ENDING_BLOCK: + # We've matched the required number of lines, insert suggestion! + + # But first, remove the lines that were matched, because they shouldn't be a part of the block + # Remove matched_lines_at_end_of_block lines from current_block_lines + current_block_lines = current_block_lines[:- + matched_lines_at_end_of_block] + + # Insert the suggestion + replacement = "\n".join(current_block_lines) + await sdk.ide.showSuggestion(FileEdit( + filepath=rif.filepath, + range=Range.from_shorthand( + current_block_start, 0, current_block_start + index_of_last_matched_line, 0), + replacement=replacement + )) + if replacement == "": + current_line_in_file += 1 + + # Reset current block / update variables + original_lines_below_previous_blocks = original_lines_below_previous_blocks[ + index_of_line_to_match + 1:] + offset_from_blocks += len(current_block_lines) + current_block_lines = [] + current_block_start = -1 + return + else: + matched_lines_at_end_of_block += 1 + else: + # We matched some lines, but didn't make it to N + # So this block should continue on with the matched lines as a part of it + matched_lines_at_end_of_block = 0 current_block_lines.append(line) -- cgit v1.2.3-70-g09d2 From 18dbf82507f6b1c91581ea7e017e1e40ff479d1f Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Tue, 27 Jun 2023 23:20:15 -0700 Subject: backtrack lines at end after ending block --- continuedev/src/continuedev/steps/core/core.py | 16 +++++++++++----- extension/src/suggestions.ts | 16 ++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/continuedev/src/continuedev/steps/core/core.py b/continuedev/src/continuedev/steps/core/core.py index 16ca18a1..7d5d52b2 100644 --- a/continuedev/src/continuedev/steps/core/core.py +++ b/continuedev/src/continuedev/steps/core/core.py @@ -318,17 +318,20 @@ class DefaultModelEditCodeStep(Step): if matched_lines_at_end_of_block >= LINES_TO_MATCH_BEFORE_ENDING_BLOCK: # We've matched the required number of lines, insert suggestion! - # But first, remove the lines that were matched, because they shouldn't be a part of the block - # Remove matched_lines_at_end_of_block lines from current_block_lines - current_block_lines = current_block_lines[:- - matched_lines_at_end_of_block] + # We added some lines to the block that were matched (including maybe some blank lines) + # So here we will strip all matching lines from the end of current_block_lines + lines_stripped = [] + index_of_end_of_block = index_of_line_to_match + while len(current_block_lines) > 0 and current_block_lines[-1] == original_lines_below_previous_blocks[index_of_end_of_block - 1]: + lines_stripped.append(current_block_lines.pop()) + index_of_end_of_block -= 1 # Insert the suggestion replacement = "\n".join(current_block_lines) await sdk.ide.showSuggestion(FileEdit( filepath=rif.filepath, range=Range.from_shorthand( - current_block_start, 0, current_block_start + index_of_last_matched_line, 0), + current_block_start, 0, current_block_start + index_of_end_of_block, 0), replacement=replacement )) if replacement == "": @@ -340,6 +343,9 @@ class DefaultModelEditCodeStep(Step): offset_from_blocks += len(current_block_lines) current_block_lines = [] current_block_start = -1 + matched_lines_at_end_of_block = 0 + index_of_last_matched_line = -1 + return else: matched_lines_at_end_of_block += 1 diff --git a/extension/src/suggestions.ts b/extension/src/suggestions.ts index 6e5f52ac..c9e29ed5 100644 --- a/extension/src/suggestions.ts +++ b/extension/src/suggestions.ts @@ -37,18 +37,18 @@ const oldDecorationType = vscode.window.createTextEditorDecorationType({ const newSelDecorationType = vscode.window.createTextEditorDecorationType({ backgroundColor: "rgb(0, 255, 0, 0.25)", isWholeLine: true, - after: { - contentText: "Press ctrl+shift+enter to accept", - margin: "0 0 0 1em", - }, + // after: { + // contentText: "Press ctrl+shift+enter to accept", + // margin: "0 0 0 1em", + // }, }); const oldSelDecorationType = vscode.window.createTextEditorDecorationType({ backgroundColor: "rgb(255, 0, 0, 0.25)", isWholeLine: true, - after: { - contentText: "Press ctrl+shift+enter to reject", - margin: "0 0 0 1em", - }, + // after: { + // contentText: "Press ctrl+shift+enter to reject", + // margin: "0 0 0 1em", + // }, }); export function rerenderDecorations(editorUri: string) { -- cgit v1.2.3-70-g09d2 From 77520f0ce1f6a78347393c249a50c6a597edd5c8 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Wed, 28 Jun 2023 13:26:04 -0700 Subject: block offset --- continuedev/src/continuedev/steps/core/core.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/continuedev/src/continuedev/steps/core/core.py b/continuedev/src/continuedev/steps/core/core.py index 7d5d52b2..f6c2f02b 100644 --- a/continuedev/src/continuedev/steps/core/core.py +++ b/continuedev/src/continuedev/steps/core/core.py @@ -290,8 +290,9 @@ class DefaultModelEditCodeStep(Step): nonlocal lines, current_block_start, current_line_in_file, original_lines, original_lines_below_previous_blocks, current_block_lines, offset_from_blocks, matched_lines_at_end_of_block, index_of_last_matched_line, LINES_TO_MATCH_BEFORE_ENDING_BLOCK # Highlight the line to show progress + line_to_highlight = current_line_in_file - len(current_block_lines) await sdk.ide.highlightCode(RangeInFile(filepath=rif.filepath, range=Range.from_shorthand( - current_line_in_file, 0, current_line_in_file, 0)), "#FFFFFF22" if len(current_block_lines) == 0 else "#FFFF0022") + line_to_highlight, 0, line_to_highlight, 0)), "#FFFFFF22" if len(current_block_lines) == 0 else "#00FF0022") if len(current_block_lines) == 0: current_block_start = current_line_in_file @@ -331,7 +332,7 @@ class DefaultModelEditCodeStep(Step): await sdk.ide.showSuggestion(FileEdit( filepath=rif.filepath, range=Range.from_shorthand( - current_block_start, 0, current_block_start + index_of_end_of_block, 0), + current_block_start + offset_from_blocks, 0, current_block_start + offset_from_blocks + index_of_end_of_block, 0), replacement=replacement )) if replacement == "": @@ -419,17 +420,19 @@ class DefaultModelEditCodeStep(Step): if len(current_block_lines) > 0: # We have a chance to back-track here for blank lines that are repeats of the suffix num_to_remove = 0 - if repeating_file_suffix: - for i in range(-1, -len(current_block_lines) - 1, -1): - if current_block_lines[i].strip() == "": - num_to_remove += 1 + for i in range(-1, -len(current_block_lines) - 1, -1): + if len(original_lines_below_previous_blocks) == 0: + break + if current_block_lines[i] == original_lines_below_previous_blocks[-1]: + num_to_remove += 1 + original_lines_below_previous_blocks.pop() current_block_lines = current_block_lines[:- num_to_remove] if num_to_remove > 0 else current_block_lines await sdk.ide.showSuggestion(FileEdit( filepath=rif.filepath, range=Range.from_shorthand( - current_block_start, 0, current_block_start + len(original_lines_below_previous_blocks), 0), + current_block_start + offset_from_blocks, 0, current_block_start + offset_from_blocks + len(original_lines_below_previous_blocks), 0), replacement="\n".join(current_block_lines) )) -- cgit v1.2.3-70-g09d2