All files / languages/sparql/providers sparql-rename-provider.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 3/3
100% Lines 9/9

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                                          5x 5x 9x   5x 1x 1x     23x   11x   11x        
import * as vscode from 'vscode';
import { sparqlVariableSymbols, type IToken } from '@faubulous/mentor-rdf-parsers';
import { TurtleDocument } from '@src/languages/turtle/turtle-document';
import { TurtleRenameProvider } from '@src/languages/turtle/providers/turtle-rename-provider';
 
/**
 * Renaming for SPARQL. Inherits prefix / IRI / Triplate-template rename from
 * {@link TurtleRenameProvider} and replaces only the variable handling with a
 * scope-aware version: a sub-SELECT is its own variable scope, while projected
 * variables co-refer with the parent (see `sparqlVariableSymbols`).
 */
export class SparqlRenameProvider extends TurtleRenameProvider {
	/**
	 * Renames the variable under the cursor across every occurrence that co-refers
	 * with it in scope. Mirrors the Triplate template-rename path: find the symbol at
	 * the cursor, then rewrite every symbol sharing its `binding`. Falls back to the
	 * base textual rename when the document does not parse as SPARQL (mid-edit, or a
	 * Triplate template with `${…}` holes), in which case `sparqlVariableSymbols`
	 * returns `[]`.
	 */
	protected override _applyVariableRename(edits: vscode.WorkspaceEdit, document: vscode.TextDocument, context: TurtleDocument, token: IToken, position: vscode.Position, newName: string): void {
		const symbols = sparqlVariableSymbols(document.getText());
		const offset = document.offsetAt(position);
		const target = symbols.find(s => offset >= s.start && offset <= s.end);
 
		if (!target) {
			super._applyVariableRename(edits, document, context, token, position, newName);
			return;
		}
 
		for (const symbol of symbols.filter(s => s.binding === target.binding)) {
			// Replace the variable name only, keeping the leading `?`/`$` sigil.
			const range = new vscode.Range(document.positionAt(symbol.start + 1), document.positionAt(symbol.end));
 
			edits.replace(document.uri, range, newName);
		}
	}
}