From 56290613b2c3c34e8b3ba8f44d567fe003168119 Mon Sep 17 00:00:00 2001 From: Oscar Puente <156957451+orpuente-MS@users.noreply.github.com> Date: Mon, 8 Apr 2024 11:49:44 -0700 Subject: [PATCH 1/4] update project documents when manifest is saved --- vscode/src/extension.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/vscode/src/extension.ts b/vscode/src/extension.ts index 291020f966..6f56fbe396 100644 --- a/vscode/src/extension.ts +++ b/vscode/src/extension.ts @@ -184,6 +184,36 @@ async function activateLanguageService(extensionUri: vscode.Uri) { ...registerQSharpNotebookCellUpdateHandlers(languageService), ); + // Watch manifest changes and update each document in the same project as the manifest. + subscriptions.push( + vscode.workspace.onDidSaveTextDocument((manifest) => { + if ( + manifest.languageId === "json" && + manifest.uri.scheme === "file" && + manifest.fileName.endsWith("qsharp.json") + ) { + const project_folder = manifest.fileName.slice( + 0, + manifest.fileName.length - "qsharp.json".length, + ); + vscode.workspace.textDocuments.forEach((document) => { + if ( + !document.isClosed && + isQsharpDocument(document) && + // Check that the document is on the same project as the manifest. + document.fileName.startsWith(project_folder) + ) { + languageService.updateDocument( + document.uri.toString(), + document.version, + document.getText(), + ); + } + }); + } + }), + ); + // format document const isFormattingEnabled = getEnableFormating(); const formatterHandle = { From 7a66d2f26bb53e48e21c9673932edb485e77a8b4 Mon Sep 17 00:00:00 2001 From: Oscar Puente <156957451+orpuente-MS@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:30:45 -0700 Subject: [PATCH 2/4] move to registerDocumentUpdateHandlers --- vscode/src/extension.ts | 55 +++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/vscode/src/extension.ts b/vscode/src/extension.ts index 6f56fbe396..47c69b03ac 100644 --- a/vscode/src/extension.ts +++ b/vscode/src/extension.ts @@ -154,6 +154,31 @@ function registerDocumentUpdateHandlers(languageService: ILanguageService) { }), ); + // Watch manifest changes and update each document in the same project as the manifest. + subscriptions.push( + vscode.workspace.onDidSaveTextDocument((manifest) => { + if ( + manifest.languageId === "json" && + manifest.uri.scheme === "file" && + manifest.fileName.endsWith("qsharp.json") + ) { + const project_folder = manifest.fileName.slice( + 0, + manifest.fileName.length - "qsharp.json".length, + ); + vscode.workspace.textDocuments.forEach((document) => { + if ( + !document.isClosed && + // Check that the document is on the same project as the manifest. + document.fileName.startsWith(project_folder) + ) { + updateIfQsharpDocument(document); + } + }); + } + }), + ); + function updateIfQsharpDocument(document: vscode.TextDocument) { if (isQsharpDocument(document) && !isQsharpNotebookCell(document)) { // Regular (not notebook) Q# document. @@ -184,36 +209,6 @@ async function activateLanguageService(extensionUri: vscode.Uri) { ...registerQSharpNotebookCellUpdateHandlers(languageService), ); - // Watch manifest changes and update each document in the same project as the manifest. - subscriptions.push( - vscode.workspace.onDidSaveTextDocument((manifest) => { - if ( - manifest.languageId === "json" && - manifest.uri.scheme === "file" && - manifest.fileName.endsWith("qsharp.json") - ) { - const project_folder = manifest.fileName.slice( - 0, - manifest.fileName.length - "qsharp.json".length, - ); - vscode.workspace.textDocuments.forEach((document) => { - if ( - !document.isClosed && - isQsharpDocument(document) && - // Check that the document is on the same project as the manifest. - document.fileName.startsWith(project_folder) - ) { - languageService.updateDocument( - document.uri.toString(), - document.version, - document.getText(), - ); - } - }); - } - }), - ); - // format document const isFormattingEnabled = getEnableFormating(); const formatterHandle = { From 1812e68c57dd9e91e0dc42b32b83b742c561ce2c Mon Sep 17 00:00:00 2001 From: Oscar Puente <156957451+orpuente-MS@users.noreply.github.com> Date: Mon, 8 Apr 2024 16:01:21 -0700 Subject: [PATCH 3/4] add update on delete --- vscode/src/extension.ts | 48 +++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/vscode/src/extension.ts b/vscode/src/extension.ts index 47c69b03ac..2d673a54d4 100644 --- a/vscode/src/extension.ts +++ b/vscode/src/extension.ts @@ -157,28 +157,38 @@ function registerDocumentUpdateHandlers(languageService: ILanguageService) { // Watch manifest changes and update each document in the same project as the manifest. subscriptions.push( vscode.workspace.onDidSaveTextDocument((manifest) => { - if ( - manifest.languageId === "json" && - manifest.uri.scheme === "file" && - manifest.fileName.endsWith("qsharp.json") - ) { - const project_folder = manifest.fileName.slice( - 0, - manifest.fileName.length - "qsharp.json".length, - ); - vscode.workspace.textDocuments.forEach((document) => { - if ( - !document.isClosed && - // Check that the document is on the same project as the manifest. - document.fileName.startsWith(project_folder) - ) { - updateIfQsharpDocument(document); - } - }); - } + updateProjectDocuments(manifest.uri); + }), + ); + + subscriptions.push( + vscode.workspace.onDidDeleteFiles((event) => { + event.files.forEach((uri) => { + updateProjectDocuments(uri); + }); }), ); + // Checks if the URI belongs to a qsharp manifest, and updates all + // open documents in the same project as the manifest. + function updateProjectDocuments(manifest: vscode.Uri) { + if (manifest.scheme === "file" && manifest.fsPath.endsWith("qsharp.json")) { + const project_folder = manifest.fsPath.slice( + 0, + manifest.fsPath.length - "qsharp.json".length, + ); + vscode.workspace.textDocuments.forEach((document) => { + if ( + !document.isClosed && + // Check that the document is on the same project as the manifest. + document.fileName.startsWith(project_folder) + ) { + updateIfQsharpDocument(document); + } + }); + } + } + function updateIfQsharpDocument(document: vscode.TextDocument) { if (isQsharpDocument(document) && !isQsharpNotebookCell(document)) { // Regular (not notebook) Q# document. From d926640ac351c3ecbe5f79a789be5cfff1809510 Mon Sep 17 00:00:00 2001 From: Oscar Puente <156957451+orpuente-MS@users.noreply.github.com> Date: Mon, 8 Apr 2024 16:05:08 -0700 Subject: [PATCH 4/4] added docstring --- vscode/src/extension.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vscode/src/extension.ts b/vscode/src/extension.ts index 2d673a54d4..afed5722ae 100644 --- a/vscode/src/extension.ts +++ b/vscode/src/extension.ts @@ -161,6 +161,8 @@ function registerDocumentUpdateHandlers(languageService: ILanguageService) { }), ); + // Trigger an update on all .qs child documents when their manifest is deleted, + // so that they can get reparented to single-file-projects. subscriptions.push( vscode.workspace.onDidDeleteFiles((event) => { event.files.forEach((uri) => {