-
Notifications
You must be signed in to change notification settings - Fork 298
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
Conversation
@@ -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] |
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 it something normal to have both undefined and null as a possible return type? for me it looks weird
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.
It's typical declaration in our protocol, please look at the rest of the file.
scheme: 'untitled', | ||
path: `${uuid.v4()}.${extension}`, | ||
}), | ||
shouldOpenInClient: false, |
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.
nice 🚀
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.
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) |
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.
Unrelated change but it was looking like bug to me.
6fa7ee8
to
996d0a9
Compare
throw new Error(errorMessage) | ||
} | ||
|
||
const result = await this.request('textDocument/openUntitledDocument', { |
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.
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) { |
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.
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.
const doc = await this.client.openNewDocument(vscode.workspace, newFileUri) | ||
if (!doc) { | ||
throw new Error(`Cannot create file for the fixup: ${newFileUri.toString()}`) | ||
} |
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.
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
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.
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.
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.
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
996d0a9
to
207572d
Compare
…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
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 ofuntitled://
).As a result that change slightly modifies behaviour of the
openTextDocument
.One could now call
openTextDocument
withuntitled://abc
parameter, and as a result get aTextDocument
which uri points tofile://abc
(or any other path, really).Test plan