All files / languages/sparql/services document-connection-service.ts

97.05% Statements 165/170
87.2% Branches 75/86
97.05% Functions 33/34
97.56% Lines 160/164

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556                                  44x         44x   44x         44x                   44x               44x     44x 44x       44x     90x   90x 2x 2x           1x       3x       1x       5x                         4x 4x                         5x 2x     3x 3x   3x 1x     2x 2x   2x                         2x 2x   2x 2x 1x         1x 1x 1x     1x 1x 1x   1x 1x 1x     1x 1x 1x     1x 1x         2x 1x 1x 1x                     26x       26x 4x   22x 22x                   21x   21x 2x     19x                 7x 4x   3x 3x     5x                   4x   4x 1x     3x   3x 1x     2x   2x 1x   1x     2x 2x   2x                 1x   1x   1x                 2x                 46x 46x   46x                           5x   5x 4x     1x                   51x                     44x   44x 41x                     65x 65x   65x 40x     25x 32x   65x 24x     25x                 2x 1x   1x   1x                 2x                       5x 10x 1x     9x                         2x 4x 1x     3x   3x 2x   1x     3x                                 7x 7x   7x 14x   14x 12x 12x       7x       7x 7x   7x   7x 12x     7x                   4x   4x 1x     3x   3x 1x     2x 2x   2x                 33x 30x 30x                                     8x         8x 8x 8x   8x 13x 26x 14x     12x     12x     26x 10x 10x   10x 10x              
import * as vscode from 'vscode';
import { SparqlConnection } from './sparql-connection';
import { WORKSPACE_CONNECTION } from './workspace-store';
import { ISparqlConnectionRegistry } from './sparql-connection-registry.interface';
import { IDocumentConnectionService } from './document-connection-service.interface';
 
/**
 * Manages the association of SPARQL connections and inference settings with
 * individual documents and notebook cells. Document-scoped settings are stored
 * in workspace state; notebook cells carry them in their metadata so they travel
 * with the notebook. The service keeps those settings stable across file renames
 * and untitled-document saves, and inherits settings for newly added notebook cells.
 */
export class DocumentConnectionService implements IDocumentConnectionService {
	/**
	 * Workspace-state key prefix for per-document connection settings (`<prefix><documentUri>`).
	 */
	private readonly _documentConnectionStorageKeyPrefix = 'sparql.connection:';
 
	/**
	 * Workspace-state key prefix for per-document inference settings (`<prefix><documentUri>`).
	 */
	private readonly _documentInferenceStorageKeyPrefix = 'mentor.inference.document:';
 
	private _onDidChangeConnectionForDocument = new vscode.EventEmitter<vscode.Uri>();
 
	/**
	 * Fired when the active connection or inference setting for a document changes.
	 */
	public readonly onDidChangeConnectionForDocument = this._onDidChangeConnectionForDocument.event;
 
	/**
	 * Latest known text content of open (or recently open) untitled SPARQL documents, keyed by
	 * their untitled URI string. Maintained from `onDidOpenTextDocument` / `onDidChangeTextDocument`
	 * and seeded when a connection is set, because `onWillSaveTextDocument` does **not** fire for
	 * untitled documents being saved to disk for the first time. Used by `_migrateUntitledSave` to
	 * match a just-saved file back to the untitled document it came from so its per-document settings
	 * (connection, inference) can be migrated to the new file URI.
	 */
	private readonly _untitledSparqlSnapshots = new Map<string, string>();
 
	/**
	 * Caches notebook cells by their document URI. Cell lookups run once per
	 * cell per code-lens or diagnostics refresh, and scanning
	 * `notebook.getCells()` for each makes a refresh of an N-cell notebook
	 * O(N²). Cleared on any notebook change or close; entries repopulate lazily.
	 */
	private readonly _cellsByUri = new Map<string, vscode.NotebookCell>();
 
	constructor(
		private readonly _extensionContext: vscode.ExtensionContext,
		private readonly _connectionRegistry: ISparqlConnectionRegistry
	) {
		// Register with the extension context so the subscriptions are disposed
		// on deactivation instead of leaking.
		this._extensionContext.subscriptions.push(
			vscode.workspace.onDidChangeNotebookDocument(async e => {
				// Structural and metadata changes both invalidate cached cell lookups.
				this._cellsByUri.clear();
 
				for (const change of e.contentChanges) {
					Eif (change.addedCells.length > 0) {
						await this._inheritSettingsForNewCells(e.notebook, change.addedCells);
					}
				}
			}),
 
			vscode.workspace.onDidCloseNotebookDocument(() => {
				this._cellsByUri.clear();
			}),
 
			vscode.workspace.onDidOpenTextDocument(document => {
				this._snapshotUntitledSparqlDocument(document);
			}),
 
			vscode.workspace.onDidChangeTextDocument(e => {
				this._snapshotUntitledSparqlDocument(e.document);
			}),
 
			vscode.workspace.onDidSaveTextDocument(async document => {
				await this._migrateUntitledSave(document);
			}),
 
			this._onDidChangeConnectionForDocument
		);
	}
 
	/**
	 * Records the current text content of an untitled SPARQL document so it can later be matched
	 * to the on-disk file it is saved as. No-op for titled documents and non-SPARQL documents.
	 * @param document The document to snapshot.
	 */
	private _snapshotUntitledSparqlDocument(document: vscode.TextDocument): void {
		Eif (document.isUntitled && document.languageId === 'sparql') {
			this._untitledSparqlSnapshots.set(document.uri.toString(), document.getText());
		}
	}
 
	/**
	 * Migrates a just-saved document's per-document settings (connection, inference) from its
	 * previous untitled URI to its new on-disk URI, if it matches a tracked untitled document.
	 * Untitled documents have no `onDidRenameFiles` equivalent when saved to disk for the first
	 * time, and `onWillSaveTextDocument` does not fire for them, so the match is made by comparing
	 * the saved text content against snapshots captured while the document was still untitled.
	 * @param document The document that was just saved.
	 */
	private async _migrateUntitledSave(document: vscode.TextDocument): Promise<void> {
		if (document.isUntitled || document.languageId !== 'sparql') {
			return;
		}
 
		const content = document.getText();
		const match = [...this._untitledSparqlSnapshots].find(([, snapshot]) => snapshot === content);
 
		if (!match) {
			return;
		}
 
		const [oldUriStr] = match;
		this._untitledSparqlSnapshots.delete(oldUriStr);
 
		await this.handleFileRenames([{ oldUri: vscode.Uri.parse(oldUriStr), newUri: document.uri }]);
	}
 
	/**
	 * Inherits connection and inference settings for newly added notebook cells from the
	 * immediately preceding cell, so new cells start with the same context as their neighbour.
	 * @param notebook The notebook document that changed.
	 * @param addedCells The cells that were added.
	 */
	private async _inheritSettingsForNewCells(
		notebook: vscode.NotebookDocument,
		addedCells: readonly vscode.NotebookCell[]
	): Promise<void> {
		const cells = notebook.getCells();
		const edits: vscode.NotebookEdit[] = [];
 
		for (const addedCell of addedCells) {
			if (addedCell.metadata?.connectionId !== undefined || addedCell.metadata?.inferenceEnabled !== undefined) {
				continue;
			}
 
			let previousCell: vscode.NotebookCell | undefined;
 
			for (let i = addedCell.index - 1; i >= 0; i--) {
				previousCell = cells[i];
				break;
			}
 
			Eif (previousCell) {
				const inheritedMetadata: Record<string, unknown> = { ...addedCell.metadata };
				let hasInheritedSettings = false;
 
				Eif (typeof previousCell.metadata?.connectionId === 'string') {
					inheritedMetadata.connectionId = previousCell.metadata.connectionId;
					hasInheritedSettings = true;
				}
 
				Eif (typeof previousCell.metadata?.inferenceEnabled === 'boolean') {
					inheritedMetadata.inferenceEnabled = previousCell.metadata.inferenceEnabled;
					hasInheritedSettings = true;
				}
 
				Eif (hasInheritedSettings) {
					edits.push(vscode.NotebookEdit.updateCellMetadata(addedCell.index, inheritedMetadata));
				}
			}
		}
 
		if (edits.length > 0) {
			const workspaceEdit = new vscode.WorkspaceEdit();
			workspaceEdit.set(notebook.uri, edits);
			await vscode.workspace.applyEdit(workspaceEdit);
		}
	}
 
	/**
	 * Gets the effective inference setting for a document or notebook cell.
	 * Priority: document/cell setting → connection setting → global default.
	 * @param documentUri The URI of the document or notebook cell.
	 * @returns `true` if inference is enabled, `false` otherwise.
	 */
	getInferenceEnabledForDocument(documentUri: vscode.Uri): boolean {
		const documentSetting = documentUri.scheme === 'vscode-notebook-cell'
			? this._getInferenceEnabledForCell(documentUri)
			: this._extensionContext.workspaceState.get<boolean | undefined>(`${this._documentInferenceStorageKeyPrefix}${documentUri.toString()}`, undefined);
 
		if (documentSetting !== undefined) {
			return documentSetting;
		} else {
			const connection = this.getConnectionForDocument(documentUri);
			return this._connectionRegistry.getInferenceEnabled(connection.id);
		}
	}
 
	/**
	 * Reads the inference setting from a notebook cell's metadata.
	 * @param cellUri The URI of the notebook cell.
	 * @returns The cell-level inference setting, or `undefined` if not set.
	 */
	private _getInferenceEnabledForCell(cellUri: vscode.Uri): boolean | undefined {
		const inferenceEnabled = this._getCellFromUri(cellUri)?.metadata?.inferenceEnabled;
 
		if (typeof inferenceEnabled === 'boolean') {
			return inferenceEnabled;
		}
 
		return undefined;
	}
 
	/**
	 * Sets the inference setting for a document or notebook cell.
	 * @param documentUri The URI of the document or notebook cell.
	 * @param inferenceEnabled `true` to enable inference, `false` to disable, `undefined` to clear.
	 */
	async setInferenceEnabledForDocument(documentUri: vscode.Uri, inferenceEnabled: boolean | undefined): Promise<void> {
		if (documentUri.scheme === 'vscode-notebook-cell') {
			await this._setInferenceEnabledForCell(documentUri, inferenceEnabled);
		} else {
			const key = `${this._documentInferenceStorageKeyPrefix}${documentUri.toString()}`;
			await this._extensionContext.workspaceState.update(key, inferenceEnabled);
		}
 
		this._onDidChangeConnectionForDocument.fire(documentUri);
	}
 
	/**
	 * Sets the inference setting on a notebook cell's metadata.
	 * @param cellUri The URI of the notebook cell.
	 * @param inferenceEnabled The new inference setting, or `undefined` to clear.
	 * @throws If the notebook or the cell cannot be found.
	 */
	private async _setInferenceEnabledForCell(cellUri: vscode.Uri, inferenceEnabled: boolean | undefined): Promise<void> {
		const notebook = this._getNotebookFromCellUri(cellUri);
 
		if (!notebook) {
			throw new Error('Notebook document not found for the given cell URI: ' + cellUri.toString());
		}
 
		const cell = notebook.getCells().find(cell => cell.document.uri.toString() === cellUri.toString());
 
		if (!cell) {
			throw new Error('Cell not found in the notebook for the given cell URI: ' + cellUri.toString());
		}
 
		const metadata = { ...cell.metadata };
 
		if (inferenceEnabled === undefined) {
			delete metadata.inferenceEnabled;
		} else {
			metadata.inferenceEnabled = inferenceEnabled;
		}
 
		const workspaceEdit = new vscode.WorkspaceEdit();
		workspaceEdit.set(notebook.uri, [vscode.NotebookEdit.updateCellMetadata(cell.index, metadata)]);
 
		await vscode.workspace.applyEdit(workspaceEdit);
	}
 
	/**
	 * Toggles the inference setting for a document or notebook cell.
	 * @param documentUri The URI of the document or notebook cell.
	 * @returns The new inference-enabled state.
	 */
	async toggleInferenceEnabledForDocument(documentUri: vscode.Uri): Promise<boolean> {
		const newValue = !this.getInferenceEnabledForDocument(documentUri);
 
		await this.setInferenceEnabledForDocument(documentUri, newValue);
 
		return newValue;
	}
 
	/**
	 * Notifies listeners that the connection or inference settings for a document have changed.
	 * Use this after bulk updates to cell metadata.
	 * @param documentUri The URI of the document that changed.
	 */
	public notifyDocumentConnectionChanged(documentUri: vscode.Uri): void {
		this._onDidChangeConnectionForDocument.fire(documentUri);
	}
 
	/**
	 * Gets the configured SPARQL connection for a document or notebook cell.
	 * @param documentIri The URI of the document or notebook cell.
	 * @returns The associated connection, or the workspace store if none is set.
	 */
	getConnectionForDocument(documentIri: vscode.Uri | string): SparqlConnection {
		const uri = typeof documentIri === 'string' ? vscode.Uri.parse(documentIri) : documentIri;
		const connectionId = this._getStoredConnectionId(uri);
 
		return this._connectionRegistry.getConnection(connectionId ?? '') ?? WORKSPACE_CONNECTION;
	}
 
	/**
	 * Returns the stored connection id of a document or notebook cell when that id
	 * no longer resolves to a registered connection — e.g. the connection was
	 * deleted, or it lives in the user settings of another machine. Callers use
	 * this to surface that {@link getConnectionForDocument} silently fell back to
	 * the workspace store rather than honoring the stored binding.
	 * @param documentUri The URI of the document or notebook cell.
	 * @returns The dangling connection id, or `undefined` when no binding is
	 * stored or the stored binding resolves.
	 */
	getUnresolvedConnectionId(documentUri: vscode.Uri): string | undefined {
		const connectionId = this._getStoredConnectionId(documentUri);
 
		if (connectionId && !this._connectionRegistry.getConnection(connectionId)) {
			return connectionId;
		}
 
		return undefined;
	}
 
	/**
	 * Reads the raw stored connection id for a document (workspace state) or
	 * notebook cell (cell metadata), without resolving it against the registry.
	 * @param uri The URI of the document or notebook cell.
	 * @returns The stored connection id, or `undefined` when none is set.
	 */
	private _getStoredConnectionId(uri: vscode.Uri): string | undefined {
		return uri.scheme === 'vscode-notebook-cell'
			? this._getConnectionIdForCell(uri)
			: this._extensionContext.workspaceState.get<string>(`${this._documentConnectionStorageKeyPrefix}${uri.toString()}`);
	}
 
	/**
	 * Reads the connection ID from a notebook cell's metadata.
	 * @param cellUri The URI of the notebook cell.
	 * @returns The connection ID, or `undefined` if none is set.
	 */
	private _getConnectionIdForCell(cellUri: vscode.Uri): string | undefined {
		const connectionId = this._getCellFromUri(cellUri)?.metadata?.connectionId;
 
		if (typeof connectionId === 'string') {
			return connectionId;
		}
	}
 
	/**
	 * Finds a notebook cell by its document URI, served from {@link _cellsByUri}
	 * when possible.
	 * @param cellUri The URI of the notebook cell.
	 * @returns The cell, or `undefined` when no open notebook contains it.
	 */
	private _getCellFromUri(cellUri: vscode.Uri): vscode.NotebookCell | undefined {
		const key = cellUri.toString();
		const cached = this._cellsByUri.get(key);
 
		if (cached) {
			return cached;
		}
 
		const notebook = this._getNotebookFromCellUri(cellUri);
		const cell = notebook?.getCells().find(cell => cell.document.uri.toString() === key);
 
		if (cell) {
			this._cellsByUri.set(key, cell);
		}
 
		return cell;
	}
 
	/**
	 * Associates a SPARQL connection with a document or notebook cell.
	 * @param documentUri The URI of the document or notebook cell.
	 * @param connectionId The ID of the connection to set.
	 */
	async setQuerySourceForDocument(documentUri: vscode.Uri, connectionId: string): Promise<void> {
		if (documentUri.scheme === 'vscode-notebook-cell') {
			await this.setConnectionForCell(documentUri, connectionId);
		} else {
			this._extensionContext.workspaceState.update(`${this._documentConnectionStorageKeyPrefix}${documentUri.toString()}`, connectionId);
 
			Iif (documentUri.scheme === 'untitled') {
				const document = vscode.workspace.textDocuments.find(d => d.uri.toString() === documentUri.toString());
 
				if (document) {
					this._snapshotUntitledSparqlDocument(document);
				}
			}
		}
 
		this._onDidChangeConnectionForDocument.fire(documentUri);
	}
 
	/**
	 * Sets the SPARQL connection for every cell of a notebook in one bulk
	 * metadata edit, then fires {@link onDidChangeConnectionForDocument} once per
	 * changed cell.
	 * @param notebook The notebook document.
	 * @param connectionId The ID of the connection to set.
	 * @returns The cells whose metadata was changed.
	 */
	async setConnectionForNotebook(notebook: vscode.NotebookDocument, connectionId: string): Promise<vscode.NotebookCell[]> {
		return this._updateNotebookCellMetadata(notebook, cell => {
			if (cell.metadata?.connectionId === connectionId) {
				return undefined;
			}
 
			return { ...cell.metadata, connectionId };
		});
	}
 
	/**
	 * Sets the inference setting for every cell of a notebook in one bulk
	 * metadata edit, then fires {@link onDidChangeConnectionForDocument} once per
	 * changed cell.
	 * @param notebook The notebook document.
	 * @param inferenceEnabled `true`/`false` to set, `undefined` to clear the cell setting.
	 * @returns The cells whose metadata was changed.
	 */
	async setInferenceEnabledForNotebook(notebook: vscode.NotebookDocument, inferenceEnabled: boolean | undefined): Promise<vscode.NotebookCell[]> {
		return this._updateNotebookCellMetadata(notebook, cell => {
			if (cell.metadata?.inferenceEnabled === inferenceEnabled) {
				return undefined;
			}
 
			const metadata = { ...cell.metadata };
 
			if (inferenceEnabled === undefined) {
				delete metadata.inferenceEnabled;
			} else {
				metadata.inferenceEnabled = inferenceEnabled;
			}
 
			return metadata;
		});
	}
 
	/**
	 * Applies a metadata update to every cell of a notebook as one workspace edit
	 * and fires {@link onDidChangeConnectionForDocument} for each changed cell.
	 * Firing per cell matters: URI-scoped consumers (e.g. the FROM-graph linter)
	 * resolve the event URI against cell documents — a notebook URI matches nothing.
	 * @param notebook The notebook document.
	 * @param update Returns the new metadata for a cell, or `undefined` to leave it unchanged.
	 * @returns The cells whose metadata was changed.
	 */
	private async _updateNotebookCellMetadata(
		notebook: vscode.NotebookDocument,
		update: (cell: vscode.NotebookCell) => { [key: string]: any } | undefined
	): Promise<vscode.NotebookCell[]> {
		const changed: vscode.NotebookCell[] = [];
		const edits: vscode.NotebookEdit[] = [];
 
		for (const cell of notebook.getCells()) {
			const metadata = update(cell);
 
			if (metadata) {
				changed.push(cell);
				edits.push(vscode.NotebookEdit.updateCellMetadata(cell.index, metadata));
			}
		}
 
		Iif (edits.length === 0) {
			return changed;
		}
 
		const workspaceEdit = new vscode.WorkspaceEdit();
		workspaceEdit.set(notebook.uri, edits);
 
		await vscode.workspace.applyEdit(workspaceEdit);
 
		for (const cell of changed) {
			this._onDidChangeConnectionForDocument.fire(cell.document.uri);
		}
 
		return changed;
	}
 
	/**
	 * Sets the connection for a specific notebook cell by editing its metadata.
	 * @param cellUri The URI of the notebook cell.
	 * @param connectionId The ID of the connection to set.
	 * @throws If the notebook or the cell cannot be found.
	 */
	async setConnectionForCell(cellUri: vscode.Uri, connectionId: string): Promise<void> {
		const notebook = this._getNotebookFromCellUri(cellUri);
 
		if (!notebook) {
			throw new Error('Notebook document not found for the given cell URI: ' + cellUri.toString());
		}
 
		const cell = notebook.getCells().find(cell => cell.document.uri.toString() === cellUri.toString());
 
		if (!cell) {
			throw new Error('Cell not found in the notebook for the given cell URI: ' + cellUri.toString());
		}
 
		const workspaceEdit = new vscode.WorkspaceEdit();
		workspaceEdit.set(notebook.uri, [vscode.NotebookEdit.updateCellMetadata(cell.index, { ...cell.metadata, connectionId })]);
 
		await vscode.workspace.applyEdit(workspaceEdit);
	}
 
	/**
	 * Finds the containing NotebookDocument for a given cell URI by matching paths.
	 * @param cellUri The URI of the notebook cell.
	 * @returns The containing notebook, or `undefined` if not found.
	 */
	private _getNotebookFromCellUri(cellUri: vscode.Uri): vscode.NotebookDocument | undefined {
		for (const notebook of vscode.workspace.notebookDocuments) {
			Eif (notebook.uri.path === cellUri.path) {
				return notebook;
			}
		}
	}
 
	/**
	 * Updates all document-scoped workspace state keys (SPARQL connection and inference settings)
	 * when files or folders are renamed in the workspace.
	 *
	 * Both prefixes use the full absolute `file://` URI as the key suffix. For folder renames the
	 * match is done by URI prefix (with a trailing `/` guard to avoid accidentally matching sibling
	 * folders that share a common name prefix).
	 *
	 * Notebook cell settings are stored in cell metadata and travel with the notebook automatically —
	 * they do not need to be migrated here.
	 *
	 * @param files The list of file rename events from `vscode.workspace.onDidRenameFiles`.
	 */
	async handleFileRenames(files: ReadonlyArray<{ oldUri: vscode.Uri; newUri: vscode.Uri }>): Promise<void> {
		const prefixes = [
			this._documentConnectionStorageKeyPrefix,
			this._documentInferenceStorageKeyPrefix,
		];
 
		for (const { oldUri, newUri } of files) {
			const oldUriStr = oldUri.toString();
			const newUriStr = newUri.toString();
 
			for (const key of this._extensionContext.workspaceState.keys()) {
				for (const prefix of prefixes) {
					if (!key.startsWith(prefix)) {
						continue;
					}
 
					const uriPart = key.slice(prefix.length);
 
					const isMatch =
						uriPart === oldUriStr ||
						uriPart.startsWith(oldUriStr + '/');
 
					if (isMatch) {
						const newKey = prefix + newUriStr + uriPart.slice(oldUriStr.length);
						const value = this._extensionContext.workspaceState.get(key);
 
						await this._extensionContext.workspaceState.update(newKey, value);
						await this._extensionContext.workspaceState.update(key, undefined);
					}
				}
			}
		}
	}
}