Skip to content
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

Allow clients to redefine untitled files protocol during their creation #4841

Merged
merged 1 commit into from
Jul 31, 2024

Conversation

pkukielka
Copy link
Contributor

@pkukielka pkukielka commented Jul 11, 2024

Closes https://linear.app/sourcegraph/issue/CODY-2004/bug-cody-recreates-the-file-that-was-created-by-generate-unit-tests

Changes

Some IDEs (maybe even most IDEs except VSC) does not have notion of unsaved files.
Keeping two different models in IDE and in the agent is problematic and leads to hard to track errors.
It is easier to let client be honest and notify agent about real protocol which it is using (most likely file:// instead of untitled://).

As a result that change slightly modifies behaviour of the openTextDocument.
One could now call openTextDocument with untitled://abc parameter, and as a result get a TextDocument which uri points to file://abc (or any other path, really).

Test plan

  1. Build JetBrains branch with this PR using CODY_DIR env var
  2. Ask cody to generate tests for some file which does not have tests yet
  3. Accept the changes
  4. Delete that newly created file
  5. Perform autocomplete in some other file
  6. Make sure test file was not recreated

@@ -294,7 +294,7 @@ export type ServerRequests = {
'window/showMessage': [ShowWindowMessageParams, string | null]

'textDocument/edit': [TextDocumentEditParams, boolean]
'textDocument/openUntitledDocument': [UntitledTextDocument, boolean]
'textDocument/openUntitledDocument': [UntitledTextDocument, ProtocolTextDocument | undefined | null]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it something normal to have both undefined and null as a possible return type? for me it looks weird

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's typical declaration in our protocol, please look at the rest of the file.

scheme: 'untitled',
path: `${uuid.v4()}.${extension}`,
}),
shouldOpenInClient: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 🚀

Copy link
Contributor

@mkondratek mkondratek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works and looks good

@@ -482,7 +482,7 @@ export class Agent extends MessageHandler implements ExtensionClient {

this.registerNotification('textDocument/didSave', async params => {
const uri = vscode.Uri.parse(params.uri)
const document = await vscode.workspace.openTextDocument(uri)
const document = await this.workspace.openTextDocument(uri)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change but it was looking like bug to me.

@pkukielka pkukielka force-pushed the pkukielka/fix-untitled-files branch from 6fa7ee8 to 996d0a9 Compare July 16, 2024 13:21
throw new Error(errorMessage)
}

const result = await this.request('textDocument/openUntitledDocument', {
Copy link
Contributor Author

@pkukielka pkukielka Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this call was in the openTextDocument, I think now it is in much more logical and better isolated place (openTextDocument should not have side effects like creating new files, and we cannot guarantee that for openUntitledDocument implementation as e.g. IntelliJ creates those files on the disk.).

@@ -301,16 +301,14 @@ const _workspace: typeof vscode.workspace = {
throw new Error('workspaceDocuments is uninitialized')
}

const result = toUri(uriOrString)
if (result) {
if (result.uri.scheme === 'untitled' && result.shouldOpenInClient) {
Copy link
Contributor Author

@pkukielka pkukielka Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was source of some of our problems, as VSC does not expect any side effects from this function, but we do not control client implementation.

Comment on lines +981 to +991
const doc = await this.client.openNewDocument(vscode.workspace, newFileUri)
if (!doc) {
throw new Error(`Cannot create file for the fixup: ${newFileUri.toString()}`)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this not just be await vscode.workspace.openTextDocument(newFileUri), or will that cause problems?

My worry is that it won't be clear to everyone that we need to use this openNewDocument method. and we will introduce bugs with untitled files elsewhere

Copy link
Contributor Author

@pkukielka pkukielka Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need to use it for all untitled:// files (even more, we shouldn't), only files which we want to present to the users. I do not think there is right now any other such use case other than edits. Perhaps we should change the name to reflect that fact better.

Problem is fundamental:
Jetbrains do not have untitled:// (or unsaved) files support.
They need to be represented as normal file://
And unless we want to do a manual file mapping for every URI used in the protocol, and send request to IntelliJ for every openTextDocument call, this is best option we are left with.

I'm very open to discussion though. If you would like to jump on the call @umpox I can explain all corner cases.

Copy link
Contributor

@umpox umpox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving on the basis that this works and fixes a bug.

Will follow up when I'm back from OOO on if we can make this more robust for future usage too

@pkukielka pkukielka force-pushed the pkukielka/fix-untitled-files branch from 996d0a9 to 207572d Compare July 31, 2024 10:43
@pkukielka pkukielka merged commit 6443a0b into main Jul 31, 2024
19 checks passed
@pkukielka pkukielka deleted the pkukielka/fix-untitled-files branch July 31, 2024 12:09
pkukielka added a commit to sourcegraph/jetbrains that referenced this pull request Jul 31, 2024
…on (#1903)

Closes
https://linear.app/sourcegraph/issue/CODY-2004/bug-cody-recreates-the-file-that-was-created-by-generate-unit-tests

## Changes

Some IDEs (maybe even most IDEs except VSC) does not have notion of
unsaved files.
Keeping two different models in IDE and in the agent is problematic and
leads to hard to track errors.
It is easier to let client be honest and notify agent about real
protocol which it is using (most likely `file://` instead of
`untitled://`).

As a result that change slightly modifies behaviour of the
`openTextDocument`.
One could now call `openTextDocument` with `untitled://abc` parameter,
and as a result get a `TextDocument` which uri points to `file://abc`
(or any other path, really).

## Test plan

1. Build JetBrains with this PR using CODY_DIR env var and [this
PR](sourcegraph/cody#4841)
2. Ask cody to generate tests for some file which does not have tests
yet
3. Accept the changes
4. Delete that newly created file
5. Perform autocomplete in some other file
6. Make sure test file was not recreated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants