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 | 19x 6x 19x 19x 19x 19x 19x 19x 6x 13x 13x 13x 6x 13x 13x 8x 5x 1x 1x 1x 4x 4x 1x 3x 1x 4x 5x 13x 13x 13x 34x 34x 26x 8x 34x 13x 5x 8x 8x 8x 8x 8x 7x 7x 8x 1x 7x 7x 13x 13x 13x 13x 7x 6x 7x 29x 29x 19x 10x 29x 7x 3x 3x 1x 2x 2x 1x 1x 1x 1x 7x 6x 6x 2x 2x 2x 2x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 19x 19x 19x 19x 1x 19x 19x 1x 19x 2x 19x 2x 19x | import * as vscode from 'vscode';
import { Uri } from "@faubulous/mentor-rdf";
import { RdfToken, IToken } from '@faubulous/mentor-rdf-parsers';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { IDocumentContextService } from '@src/services/document';
import { TurtleDocument } from '@src/languages/turtle/turtle-document';
import { TurtleFeatureProvider } from '@src/languages/turtle/turtle-feature-provider';
import { getIriFromIriReference, getIriFromPrefixedName, getNamespaceDefinition, getTokenPosition } from '@src/utilities';
import { getPrefixesWithErrorCode } from '@src/utilities/vscode/diagnostic';
import { INLINE_SINGLE_USE_BLANK_NODE_CODE } from '@src/languages/linters';
/**
* A provider for RDF document code actions.
*/
export class TurtleCodeActionsProvider extends TurtleFeatureProvider implements vscode.CodeActionProvider {
private get contextService() {
return container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
}
/**
* The kinds of code actions provided by this provider.
*/
public static readonly providedCodeActionKinds = [
vscode.CodeActionKind.QuickFix,
vscode.CodeActionKind.Refactor,
];
async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, actionContext: vscode.CodeActionContext): Promise<vscode.CodeAction[]> {
return [
...this._provideRefactoringActions(document, range, actionContext),
...this._provideInlineBlankNodesActions(document, actionContext),
...this._provideFixMissingPrefixesActions(document, actionContext)
];
}
/**
* Get code actions for single-use blank nodes that can be inlined.
*
* When the cursor is over a single-use blank node diagnostic, a QuickFix action is provided.
* When any single-use blank nodes exist in the document, a Refactor action is always offered.
* @param document An RDF document.
* @param actionContext The action context.
* @returns An array of code actions.
*/
private _provideInlineBlankNodesActions(document: vscode.TextDocument, actionContext: vscode.CodeActionContext): vscode.CodeAction[] {
const allDiagnostics = vscode.languages.getDiagnostics(document.uri)
.filter(d => d.code === INLINE_SINGLE_USE_BLANK_NODE_CODE);
Eif (allDiagnostics.length === 0) {
return [];
}
const result: vscode.CodeAction[] = [];
// QuickFix at each blank node definition site.
for (const diagnostic of actionContext.diagnostics) {
if (diagnostic.code !== INLINE_SINGLE_USE_BLANK_NODE_CODE) {
continue;
}
const fix = new vscode.CodeAction(
`Inline all single-use blank nodes (${allDiagnostics.length})`,
vscode.CodeActionKind.QuickFix,
);
fix.command = {
title: 'Inline Blank Nodes',
command: 'mentor.command.inlineBlankNodes',
arguments: [document.uri],
};
fix.diagnostics = [diagnostic];
fix.isPreferred = true;
result.push(fix);
}
// Refactor action always shown when single-use blank nodes exist.
result.push({
kind: vscode.CodeActionKind.Refactor,
title: `Inline single-use blank nodes (${allDiagnostics.length})`,
command: {
title: 'Inline Blank Nodes',
command: 'mentor.command.inlineBlankNodes',
arguments: [document.uri],
},
});
return result;
}
/**
* Get code actions that provide refactoring actions for the given document.
* @param document An RDF document context.
* @param range The range of the current edit.
* @param actionContext The action context.
* @returns An array of code actions.
*/
private _provideRefactoringActions(document: vscode.TextDocument, range: vscode.Range, actionContext: vscode.CodeActionContext): vscode.CodeAction[] {
const context = this.contextService.getDocumentContext(document, TurtleDocument);
if (!context) {
return [];
}
const result: vscode.CodeAction[] = [];
// Selection-based refactorings (range may span multiple lines).
const inlinePrefixesAction = this._createInlineSelectedPrefixesAction(document, context, range);
if (inlinePrefixesAction) {
result.push(inlinePrefixesAction);
}
// Token-based refactorings for the current position.
const token = context.getTokenAtPosition(range.start);
if (!token) {
return result;
}
switch (token.tokenType.name) {
case RdfToken.IRIREF.name: {
const namespaceIri = Uri.getNamespaceIri(getIriFromIriReference(token.image));
result.push({
kind: vscode.CodeActionKind.Refactor,
title: 'Define prefix for IRI',
isPreferred: true,
command: {
title: 'Define prefix for IRI',
command: 'mentor.command.implementPrefixForIri',
arguments: [document.uri, namespaceIri, token]
}
});
break;
}
case RdfToken.PNAME_NS.name:
case RdfToken.PREFIX.name:
case RdfToken.TTL_PREFIX.name: {
result.push({
kind: vscode.CodeActionKind.Refactor,
title: 'Sort prefixes',
isPreferred: true,
command: {
title: 'Sort prefixes',
command: 'mentor.command.sortPrefixes',
arguments: [document.uri, token]
}
});
// Add conversion actions for prefix definitions.
if (token.tokenType.name === RdfToken.PREFIX.name) {
result.push(this._createConvertPrefixAction(document, context, token, 'turtle'));
} else if (token.tokenType.name === RdfToken.TTL_PREFIX.name) {
result.push(this._createConvertPrefixAction(document, context, token, 'sparql'));
}
break;
}
}
return result;
}
/**
* Creates a refactor code action that removes prefix definitions within the selected range
* and rewrites all PNAME occurrences of those prefixes into IRIREFs.
*/
private _createInlineSelectedPrefixesAction(document: vscode.TextDocument, context: TurtleDocument, selection: vscode.Range): vscode.CodeAction | undefined {
const startLine = Math.min(selection.start.line, selection.end.line);
const endLine = Math.max(selection.start.line, selection.end.line);
// Find all prefix declaration tokens in the selected line range.
const selectedPrefixDecls = context.tokens.filter(t => {
const type = t.tokenType.name;
if (type !== RdfToken.PREFIX.name && type !== RdfToken.TTL_PREFIX.name) {
return false;
}
const line = (t.startLine ?? 1) - 1;
return line >= startLine && line <= endLine;
});
if (selectedPrefixDecls.length === 0) {
return;
}
const prefixToIri = new Map<string, string>();
const prefixDeclLines = new Set<number>();
for (const decl of selectedPrefixDecls) {
const def = getNamespaceDefinition(context.tokens, decl);
if (!def) continue;
prefixToIri.set(def.prefix, def.uri);
prefixDeclLines.add((decl.startLine ?? 1) - 1);
}
if (prefixToIri.size === 0) {
return;
}
const prefixList = Array.from(prefixToIri.keys()).sort();
const title = prefixList.length === 1
? `Inline prefix: ${prefixList[0]}`
: `Inline selected prefixes (${prefixList.length})`;
const action: vscode.CodeAction = {
kind: vscode.CodeActionKind.Refactor,
title,
isPreferred: false,
};
const edit = new vscode.WorkspaceEdit();
const prefixMap = Object.fromEntries(prefixToIri);
// 1) Remove the selected prefix definition lines.
for (const line of prefixDeclLines) {
if (line < 0 || line >= document.lineCount) continue;
edit.delete(document.uri, document.lineAt(line).rangeIncludingLineBreak);
}
// 2) Replace all occurrences of pnames using those prefixes with IRIREFs.
for (const t of context.tokens) {
const type = t.tokenType.name;
if (type !== RdfToken.PNAME_NS.name && type !== RdfToken.PNAME_LN.name) {
continue;
}
const tokenLine = (t.startLine ?? 1) - 1;
if (prefixDeclLines.has(tokenLine)) {
continue;
}
const prefix = t.image.split(':')[0];
if (!prefixToIri.has(prefix)) {
continue;
}
const expandedIri = getIriFromPrefixedName(prefixMap, t.image);
if (!expandedIri) {
continue;
}
const { start, end } = getTokenPosition(t);
const tokenRange = new vscode.Range(
new vscode.Position(start.line, start.character),
new vscode.Position(end.line, end.character)
);
edit.replace(document.uri, tokenRange, `<${expandedIri}>`);
}
if (edit.size > 0) {
action.edit = edit;
return action;
}
}
/**
* Create a code action for converting all prefix definitions between Turtle and SPARQL styles.
* @param document The text document.
* @param context The Turtle document context.
* @param token The prefix token (PREFIX or TTL_PREFIX).
* @param targetStyle The target style to convert to ('turtle' for @prefix, 'sparql' for PREFIX).
* @returns A code action for converting all prefix definitions.
*/
private _createConvertPrefixAction(document: vscode.TextDocument, context: TurtleDocument, token: IToken, targetStyle: 'turtle' | 'sparql'): vscode.CodeAction {
const title = targetStyle === 'turtle' ? 'Convert all to @prefix' : 'Convert all to PREFIX';
const action: vscode.CodeAction = {
kind: vscode.CodeActionKind.Refactor,
title,
isPreferred: false,
};
const edit = new vscode.WorkspaceEdit();
// Iterate over all tokens and convert all prefix definitions to the target style.
for (const t of context.tokens) {
if (t.tokenType.name === RdfToken.PREFIX.name || t.tokenType.name === RdfToken.TTL_PREFIX.name) {
const ns = getNamespaceDefinition(context.tokens, t);
Eif (ns) {
const line = (t.startLine ?? 1) - 1;
const lineRange = document.lineAt(line).range;
const newDefinition = targetStyle === 'turtle'
? `@prefix ${ns.prefix}: <${ns.uri}> .`
: `PREFIX ${ns.prefix}: <${ns.uri}>`;
edit.replace(document.uri, lineRange, newDefinition);
}
}
}
Eif (edit.size > 0) {
action.edit = edit;
}
return action;
}
/**
* Get a code action for defining missing prefixes.
* @param document An RDF document context.
* @param prefixes The prefixes to define.
* @returns Code actions for defining missing prefixes.
*/
private _provideFixMissingPrefixesActions(document: vscode.TextDocument, actionContext: vscode.CodeActionContext): vscode.CodeAction[] {
const result: vscode.CodeAction[] = [];
const documentDiagnostics = vscode.languages.getDiagnostics(document.uri);
// 1. Find all unused prefixes in the whole document, and add them as a repair option on top.
const undefinedPrefixes = getPrefixesWithErrorCode(document, documentDiagnostics, 'UndefinedNamespacePrefixError');
if (undefinedPrefixes.length > 0) {
// Fixing missing prefixes is implemented as a command instead of a static edit because
// the document may change in the meantime and the insert range may no longer be valid.
result.push({
kind: vscode.CodeActionKind.QuickFix,
title: 'Implement missing prefixes',
isPreferred: true,
command: {
title: 'Implement missing prefixes',
command: 'mentor.command.implementPrefixes',
arguments: [document.uri, Array.from(undefinedPrefixes)]
}
});
}
// Note, the unused prefix diagnostics contain the _whole_ line of the prefix definition, so we need to extract the prefix from it.
const unusedPrefixes = getPrefixesWithErrorCode(document, documentDiagnostics, 'UnusedNamespacePrefixHint');
if (unusedPrefixes.length > 0) {
result.push({
kind: vscode.CodeActionKind.QuickFix,
title: 'Remove unused prefixes',
isPreferred: true,
command: {
title: 'Remove unused prefixes',
command: 'mentor.command.deletePrefixes',
arguments: [document.uri, unusedPrefixes]
}
});
}
// 2. Find all unused prefixes in the context and add them as the second repair option.
for (let prefix of getPrefixesWithErrorCode(document, actionContext.diagnostics, 'UndefinedNamespacePrefixError')) {
result.push({
kind: vscode.CodeActionKind.QuickFix,
title: `Implement missing prefix: ${prefix}`,
isPreferred: false,
command: {
title: `Implement missing prefix: ${prefix}`,
command: 'mentor.command.implementPrefixes',
arguments: [document.uri, [prefix]]
}
});
}
for (let prefix of getPrefixesWithErrorCode(document, actionContext.diagnostics, 'UnusedNamespacePrefixHint')) {
result.push({
kind: vscode.CodeActionKind.QuickFix,
title: `Remove unused prefix: ${prefix}`,
isPreferred: false,
command: {
title: `Remove unused prefix: ${prefix}`,
command: 'mentor.command.deletePrefixes',
arguments: [document.uri, [prefix]]
}
});
}
return result;
}
} |