Repository navigation
ADFA-6835 | Show edit_file's closest lines and report a change that was never made #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4ababd1
ADFA-6835 | Show the file's closest lines when edit_file rejects an edit
davidschachterADFA 0ea705d
ADFA-6835 | Tell a run its file change failed instead of ending on "d…
davidschachterADFA f4e3196
ADFA-6835 | Tell the user when a failed file change was never redone
davidschachterADFA aaecc13
ADFA-6835 | Track a failed file change per path until it is redone
davidschachterADFA 6a93052
ADFA-6835 | Key a failed change by its file, and tell the user howeve…
davidschachterADFA b869b92
ADFA-6835 | Key a failed write as its own tool resolves the path
davidschachterADFA b0a09c4
ADFA-6835 | Let a warned run redo its change, and key a failed create…
davidschachterADFA c1a9810
ADFA-6835 | Count a repeated edit as failed once another write change…
davidschachterADFA 82ed8fa
ADFA-6835 | Count a repeated write that already worked as made; match…
davidschachterADFA ddbd07e
ADFA-6835 | Excuse a failed repeat only when nothing has written its …
davidschachterADFA 88230c5
ADFA-6835 | Test the repeat check with the real file tools; state its…
davidschachterADFA 7b8e04b
Merge branch 'main' into ADFA-6835
davidschachterADFA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/handlers/edit/NearMatchLines.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.itsaky.androidide.plugins.aicore.tool.handlers.edit | ||
|
|
||
| /** | ||
| * Finds the lines of a file that a failed `old_string` was probably aiming at, so a rejection can | ||
| * show them. A small model that guesses `old_string` and is told only "not found" guesses again; | ||
| * shown the real line, it can copy it. Matching is by shared words, never by regex, for the same | ||
| * reason as [FileTextMatcher]. | ||
| */ | ||
| object NearMatchLines { | ||
|
|
||
| /** One candidate line, with its 1-based number and its exact text (no line ending). */ | ||
| data class Line(val number: Int, val text: String) | ||
|
|
||
| private const val MAX_LINES = 5 | ||
| private const val MAX_LINE_CHARS = 200 | ||
| private const val MIN_WORD_CHARS = 3 | ||
| private val WORD = Regex("[A-Za-z0-9_]{$MIN_WORD_CHARS,}") | ||
|
|
||
| /** | ||
| * The lines of [text] sharing the most distinct words with [snippet]. | ||
| * @param text the file contents the edit was matched against. | ||
| * @param snippet the `old_string` that was not found or changed nothing. | ||
| * @return up to [MAX_LINES] lines, best match first and file order among equals; empty when | ||
| * the snippet has no word of [MIN_WORD_CHARS] or more characters or no line shares one. | ||
| */ | ||
| fun find(text: String, snippet: String): List<Line> { | ||
| val words = WORD.findAll(snippet).map { it.value.lowercase() }.distinct().toList() | ||
| if (words.isEmpty()) return emptyList() | ||
| return text.lineSequence() | ||
| .mapIndexed { index, raw -> Line(index + 1, raw.trimEnd('\r').take(MAX_LINE_CHARS)) } | ||
| .filter { it.text.isNotBlank() } | ||
| .map { line -> | ||
| val lower = line.text.lowercase() | ||
| line to words.count { lower.contains(it) } | ||
| } | ||
| .filter { (_, score) -> score > 0 } | ||
| .sortedByDescending { (_, score) -> score } | ||
| .take(MAX_LINES) | ||
| .map { (line, _) -> line } | ||
| .toList() | ||
| } | ||
|
|
||
| /** | ||
| * Renders [lines] for a rejection message, keeping leading and trailing spaces visible. | ||
| * @param lines the result of [find]. | ||
| * @return the text to append to a rejection, or an empty string when there are no lines. | ||
| */ | ||
| fun render(lines: List<Line>): String { | ||
| if (lines.isEmpty()) return "" | ||
| val body = lines.joinToString("\n") { "line ${it.number}: \"${it.text}\"" } | ||
| return "\nClosest lines in the file (the exact text is between the outer double quotes, " + | ||
| "spaces included):\n$body" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.