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
Apply IntelliJ formatting to the edited code snippet that comes from the agent #1454
Conversation
a906a2f
to
80b6d56
Compare
src/main/kotlin/com/sourcegraph/cody/edit/sessions/FixupSession.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
33d57cd
to
b21406b
Compare
// when the user selects a different snippet in the UI. | ||
// FIXME this causes a problem when the entire file becomes shorter than the original | ||
// selection range. | ||
selectionRange!!.let { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use !!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that !!
is justified here. However, I just confirmed that this FIXME still needs to be fixed...
src/main/kotlin/com/sourcegraph/cody/edit/sessions/FixupSession.kt
Outdated
Show resolved
Hide resolved
// by calling triggerFixupAsync() which in turn calls ensureSelectionRange(). | ||
// The value of this property does not change | ||
// when the user selects a different snippet in the UI. | ||
// FIXME this causes a problem when the entire file becomes shorter than the original |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that going to be fixed as part of this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have refactored FixupSession
to use the RangeMarker
from the IntelliJ API. Unlike the dumb data classes such as Range
, TextRange
etc., the RangeMarker
remains tied to its document, and updates itself whenever the document is modified. The downside is that we need to acquire a read lock before creating a new RangeMarker
.
This has eliminated the problems with shortening the file. However, I've run into other problems...
317a713
to
6a60cdf
Compare
@@ -34,6 +34,8 @@ class CodyFormatter { | |||
.createFileFromText("TEMP", file.fileType, appendedString) | |||
|
|||
val codeStyleManager = CodeStyleManager.getInstance(project) | |||
// This will fail if cursor < range.startOffset + completionText.length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not crash whole edit if formatter fails.
Instead we should backoff from formatting, log the error, and continue edit without reformatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this is the current behaviour. However, the root cause (incorrect ranges being passed) still needs to be fixed.
LGTM |
Test plan
Tested manually, mostly on this file.
Things that are confirmed to work (prompts might need to be reworded)
Undoing these operations also works perfectly.
Intentionally misformatting the file
This flow technically works as expected, but the result might confuse the user.
;
) in the same line.FixupSession.performInlineEdits
, you'll see that the edit arrives from the agent without indents (as requested), and is later formatted by IntelliJ.Things that don't work
These things seem to be broken in master.