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 | 49x 49x 49x 8x 8x 2x 6x 6x 2x 4x 2x 2x 4x 4x 2x 2x 1x 1x 21x 21x 21x 1x 20x 20x 4x 4x 4x 4x 10x 10x 4x 16x 16x 1x 15x 3x 3x 3x 15x 15x 7x 7x 7x 7x 6x 6x 3x 2x 12x 3x 9x 9x 8x 8x 8x 7x 7x 6x 8x 8x 14x 8x 8x 4x 4x 4x 3x 26x 26x 20x 6x 6x 21x 6x 6x 17x 3x 3x 10x | import * as vscode from 'vscode';
import { isTemplate, symbols as templateSymbols, type TemplateSymbol } from 'triplate';
import { IDocumentContextService } from '@src/services/document';
import { IWorkspaceRenameService } from '@src/services/core';
import { RdfToken, isVariableToken, type IToken } from '@faubulous/mentor-rdf-parsers';
import { getIriFromToken, getTokenAtPosition, isPrefixTokenAtPosition } from '@src/utilities';
import { TurtleDocument } from '@src/languages/turtle/turtle-document';
import { TurtleFeatureProvider } from '@src/languages/turtle/turtle-feature-provider';
/**
* Any name-bearing Triplate symbol (`paramDecl` / `paramRef` / `bindingKey` and the
* loop `loopDecl` / `loopRef`).
*/
type NamedSymbol = Extract<TemplateSymbol, { name: string }>;
/**
* Provides renaming for URIs, resources labels and prefixes.
*/
export class TurtleRenameProvider extends TurtleFeatureProvider implements vscode.RenameProvider {
/**
* Triplate symbol kinds that name a template parameter (declaration, reference, binding key).
*/
readonly _parameterSymbols = new Set(['paramDecl', 'paramRef', 'bindingKey']);
/**
* @param contextService The document context service.
* @param _renameService Extends IRI renames to the other indexed documents;
* renames stay within the document when it is not provided.
*/
constructor(private readonly contextService: IDocumentContextService, private readonly _renameService?: IWorkspaceRenameService) {
super();
}
public async prepareRename(document: vscode.TextDocument, position: vscode.Position): Promise<vscode.Range | null> {
const context = this.contextService.getDocumentContext(document, TurtleDocument);
if (!context) {
return null;
}
// Triplate template rename (a parameter across its declaration ↔ ${ref} ↔ example
// binding key, or a loop variable across its {% for %} declaration and in-scope
// references) takes precedence: these sites are not RDF tokens, so the token logic
// below can't see them.
const symbols = this._templateSymbols(document);
if (symbols) {
const offset = document.offsetAt(position);
const hit = this._templateRenameGroupAt(symbols, offset).find(s => offset >= s.start && offset <= s.end);
Eif (hit) {
return new vscode.Range(document.positionAt(hit.start), document.positionAt(hit.end));
}
}
const token = getTokenAtPosition(context.tokens, position);
if (!token) {
throw new Error('No token found at the given position.');
}
if (isPrefixTokenAtPosition(token, position)) {
return this.getPrefixEditRange(token);
} else {
return this.getLabelEditRange(token);
}
}
public async provideRenameEdits(document: vscode.TextDocument, position: vscode.Position, newName: string): Promise<vscode.WorkspaceEdit> {
const edits = new vscode.WorkspaceEdit();
const context = this.contextService.getDocumentContext(document, TurtleDocument);
if (!context) {
return edits;
}
// Triplate template rename: rewrite every site that renames together with the
// symbol under the cursor (a parameter group by name, or a loop variable by
// scope), then stop — these are not RDF symbols.
const templateSymbolsAtUri = this._templateSymbols(document);
if (templateSymbolsAtUri) {
const offset = document.offsetAt(position);
const group = this._templateRenameGroupAt(templateSymbolsAtUri, offset);
Eif (group.length > 0) {
for (const symbol of group) {
const range = new vscode.Range(document.positionAt(symbol.start), document.positionAt(symbol.end));
edits.replace(document.uri, range, newName);
}
return edits;
}
}
const token = getTokenAtPosition(context.tokens, position);
if (!token) {
return edits;
}
if (isPrefixTokenAtPosition(token, position)) {
const i = token.image.indexOf(":");
const prefix = token.image.substring(0, i);
for (const t of context.tokens) {
const tokenType = t.tokenType.name;
switch (tokenType) {
case RdfToken.PNAME_NS.name:
case RdfToken.PNAME_LN.name: {
const p = t.image.split(":")[0];
Eif (p === prefix) {
const r = this.getPrefixEditRange(t);
if (!r) continue;
edits.replace(document.uri, r, newName);
}
break;
}
}
}
if (edits.size > 0) {
context.updateNamespacePrefix(prefix, newName);
}
} else if (isVariableToken(token)) {
this._applyVariableRename(edits, document, context, token, position, newName);
} else {
const u = getIriFromToken(context.namespaces, token);
if (!u) return edits;
for (const range of context.references.getRanges(u)) {
const token = getTokenAtPosition(context.tokens, range.start);
if (!token) continue;
const editRange = this.getLabelEditRange(token);
if (!editRange) continue;
edits.replace(document.uri, editRange, newName);
}
// Prefixes, variables and blank nodes are local to the document; only an IRI
// is renamed in the other documents that reference it.
Eif (this._isIriToken(token)) {
await this._renameService?.addEditsInOtherFiles(edits, u, newName, document.uri);
}
}
return edits;
}
/**
* Indicates whether a token denotes an IRI, as a prefixed name or an IRI reference.
* @param token A token.
*/
private _isIriToken(token: IToken): boolean {
const tokenType = token.tokenType.name;
return tokenType === RdfToken.PNAME_LN.name || tokenType === RdfToken.IRIREF.name;
}
/**
* Renames the variable `token` across the document. The base behaviour is purely
* textual: every token with the same image is rewritten. SPARQL overrides this with
* scope-aware resolution (see `SparqlRenameProvider`).
*/
protected _applyVariableRename(edits: vscode.WorkspaceEdit, document: vscode.TextDocument, context: TurtleDocument, token: IToken, _position: vscode.Position, newName: string): void {
for (const t of context.tokens.filter(t => t.image === token.image)) {
const range = this.getLabelEditRange(t);
if (!range) continue;
edits.replace(document.uri, range, newName);
}
}
/**
* Returns the positioned Triplate symbols for `document`, or `null` if it is not a
* template or cannot be read. Uses the tolerant reader so rename keeps working while
* the template is mid-edit.
*/
private _templateSymbols(document: vscode.TextDocument): TemplateSymbol[] | null {
const text = typeof document.getText === 'function' ? document.getText() : '';
if (!isTemplate(text)) {
return null;
}
try {
return templateSymbols(text);
} catch {
return null;
}
}
/**
* Returns every symbol that should be renamed together with the one at `offset`, or
* an empty array if `offset` is not on a renameable template symbol.
*
* Parameters group by `name`, gated to names that have a `paramDecl` so a stray
* reference without a declaration falls through to RDF rename. Loop variables group
* by `scope` id, which keeps shadowed and same-named loops independent.
*/
private _templateRenameGroupAt(symbols: TemplateSymbol[], offset: number): TemplateSymbol[] {
const hit = symbols.find(s => offset >= s.start && offset <= s.end
&& (this._parameterSymbols.has(s.kind) || s.kind === 'loopDecl' || s.kind === 'loopRef'));
Iif (!hit) {
return [];
}
if (hit.kind === 'loopDecl' || hit.kind === 'loopRef') {
return symbols.filter(s => (s.kind === 'loopDecl' || s.kind === 'loopRef') && s.scope === hit.scope);
}
const name = (hit as NamedSymbol).name;
Iif (!symbols.some(s => s.kind === 'paramDecl' && s.name === name)) {
return [];
}
return symbols.filter(s => this._parameterSymbols.has(s.kind) && (s as NamedSymbol).name === name);
}
} |