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 | 41x 60x 41x 2x 1x 41x 41x 2x 1x 3x 4x 3x 5x 5x 3x 2x 12x 12x 10x 10x 10x 10x 9x 9x 8x 8x 7x 12x 5x 4x 4x 4x 4x 3x 3x 3x 2x | import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { RdfToken } from '@faubulous/mentor-rdf-parsers';
import { ServiceToken } from '@src/services/tokens';
import { IDocumentContextService } from '@src/services/document';
import { TurtlePrefixDefinitionService } from '@src/languages/turtle/services/turtle-prefix-definition-service';
import { TurtleDocument } from '@src/languages/turtle/turtle-document';
import { getConfig } from '@src/utilities/vscode/config';
/**
* Describes a pending prefix that should be auto-defined once fresh tokens arrive.
*/
interface PendingPrefix {
/**
* The document URI where the prefix was typed.
*/
documentUri: string;
/**
* The position where the colon was typed.
*/
position: vscode.Position;
}
/**
* A provider that automatically defines namespace prefixes when a colon is typed
* in a prefixed name. This listens to document changes to capture the intent of
* writing a prefix, then waits for fresh tokens from the language server before
* processing the auto-define logic.
*/
export class TurtleAutoDefinePrefixProvider implements vscode.Disposable {
private _pendingPrefix: PendingPrefix | undefined;
private readonly _disposables: vscode.Disposable[] = [];
constructor(languages: string[]) {
const filter = languages.map(language => ({ language }));
this._disposables.push(
vscode.workspace.onDidChangeTextDocument(e => {
if (!filter.some(f => f.language === e.document.languageId)) return;
this._onDidChangeTextDocument(e);
})
);
const contextService = container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
this._disposables.push(
contextService.onDidChangeDocumentContext(context => {
if (context) {
this._onDidChangeDocumentContext(context.uri.toString());
}
})
);
}
dispose(): void {
for (const d of this._disposables) {
d.dispose();
}
this._disposables.length = 0;
}
/**
* Captures the intent to auto-define a prefix when a colon is typed.
* The actual processing is deferred until fresh tokens arrive.
*/
private _onDidChangeTextDocument(e: vscode.TextDocumentChangeEvent): void {
const change = e.contentChanges[0];
if (!change?.text.endsWith(':')) return;
if (!getConfig().get('prefixes.autoDefinePrefixes')) return;
this._pendingPrefix = {
documentUri: e.document.uri.toString(),
position: change.range.start
};
}
/**
* Processes the pending prefix when fresh tokens arrive from the language server.
*/
private async _onDidChangeDocumentContext(uri: string): Promise<void> {
const pending = this._pendingPrefix;
if (!pending || pending.documentUri !== uri) return;
// Clear the pending prefix immediately to avoid processing it twice.
this._pendingPrefix = undefined;
const contextService = container.resolve<IDocumentContextService>(ServiceToken.DocumentContextService);
const document = vscode.workspace.textDocuments.find(d => d.uri.toString() === uri);
if (!document) return;
const context = contextService.getContext(document, TurtleDocument);
if (!context) return;
const n = context.getTokenIndexAtPosition(pending.position);
if (n < 1) return;
const previousToken = context.tokens[n - 1]?.image.toLowerCase();
// Do not auto-implement prefixes when manually typing a prefix definition.
if (previousToken === 'prefix' || previousToken === '@prefix') return;
// Do not implement prefixes for URI schemes.
if (previousToken === '<') return;
const currentToken = context.tokens[n];
Eif (currentToken && currentToken.image && currentToken.tokenType.name === RdfToken.PNAME_NS.name) {
const prefix = currentToken.image.substring(0, currentToken.image.length - 1);
// Do not implement prefixes that are already defined.
if (context.namespaces[prefix]) return;
const service = container.resolve<TurtlePrefixDefinitionService>(ServiceToken.TurtlePrefixDefinitionService);
const edit = await service.implementPrefixes(document, [{ prefix: prefix, namespaceIri: undefined }]);
if (edit.size > 0) {
vscode.workspace.applyEdit(edit);
}
}
}
}
|