mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
- unified 'vscode' imports
- unified naming of private fields to `_xyz` - use `vscode.workspace.fs` instead of `fs`
This commit is contained in:
@ -1,12 +1,9 @@
|
||||
import {
|
||||
Range, Position, CallHierarchyProvider, TextDocument, CancellationToken, CallHierarchyItem,
|
||||
SymbolKind, ProviderResult, CallHierarchyIncomingCall, CallHierarchyOutgoingCall, workspace, Uri
|
||||
} from 'vscode';
|
||||
import * as vscode from 'vscode';
|
||||
import { FoodPyramid, FoodRelation } from './model';
|
||||
|
||||
export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
export class FoodPyramidHierarchyProvider implements vscode.CallHierarchyProvider {
|
||||
|
||||
prepareCallHierarchy(document: TextDocument, position: Position, token: CancellationToken): CallHierarchyItem | undefined {
|
||||
prepareCallHierarchy(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.CallHierarchyItem | undefined {
|
||||
let range = document.getWordRangeAtPosition(position);
|
||||
if (range) {
|
||||
let word = document.getText(range);
|
||||
@ -16,14 +13,14 @@ export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
}
|
||||
}
|
||||
|
||||
async provideCallHierarchyOutgoingCalls(item: CallHierarchyItem, token: CancellationToken): Promise<CallHierarchyOutgoingCall[] | undefined> {
|
||||
let document = await workspace.openTextDocument(item.uri);
|
||||
async provideCallHierarchyOutgoingCalls(item: vscode.CallHierarchyItem, token: vscode.CancellationToken): Promise<vscode.CallHierarchyOutgoingCall[] | undefined> {
|
||||
let document = await vscode.workspace.openTextDocument(item.uri);
|
||||
let parser = new FoodPyramidParser();
|
||||
parser.parse(document);
|
||||
let model = parser.getModel();
|
||||
let originRelation = model.getRelationAt(item.range);
|
||||
|
||||
let outgoingCallItems: CallHierarchyOutgoingCall[] = [];
|
||||
let outgoingCallItems: vscode.CallHierarchyOutgoingCall[] = [];
|
||||
|
||||
if (model.isVerb(item.name)) {
|
||||
let outgoingCalls = model.getVerbRelations(item.name)
|
||||
@ -32,7 +29,7 @@ export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
outgoingCalls.forEach(relation => {
|
||||
let outgoingCallRange = relation.getRangeOf(relation.object);
|
||||
let verbItem = this.createCallHierarchyItem(relation.object, 'noun', document, outgoingCallRange);
|
||||
let outgoingCallItem = new CallHierarchyOutgoingCall(verbItem, [outgoingCallRange]);
|
||||
let outgoingCallItem = new vscode.CallHierarchyOutgoingCall(verbItem, [outgoingCallRange]);
|
||||
outgoingCallItems.push(outgoingCallItem);
|
||||
});
|
||||
}
|
||||
@ -42,7 +39,7 @@ export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
outgoingCallMap.forEach((relations, verb) => {
|
||||
let outgoingCallRanges = relations.map(relation => relation.getRangeOf(verb));
|
||||
let verbItem = this.createCallHierarchyItem(verb, 'verb', document, outgoingCallRanges[0]);
|
||||
let outgoingCallItem = new CallHierarchyOutgoingCall(verbItem, outgoingCallRanges);
|
||||
let outgoingCallItem = new vscode.CallHierarchyOutgoingCall(verbItem, outgoingCallRanges);
|
||||
outgoingCallItems.push(outgoingCallItem);
|
||||
});
|
||||
}
|
||||
@ -50,14 +47,14 @@ export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
return outgoingCallItems;
|
||||
}
|
||||
|
||||
async provideCallHierarchyIncomingCalls(item: CallHierarchyItem, token: CancellationToken): Promise<CallHierarchyIncomingCall[]> {
|
||||
let document = await workspace.openTextDocument(item.uri);
|
||||
async provideCallHierarchyIncomingCalls(item: vscode.CallHierarchyItem, token: vscode.CancellationToken): Promise<vscode.CallHierarchyIncomingCall[]> {
|
||||
let document = await vscode.workspace.openTextDocument(item.uri);
|
||||
let parser = new FoodPyramidParser();
|
||||
parser.parse(document);
|
||||
let model = parser.getModel();
|
||||
let originRelation = model.getRelationAt(item.range);
|
||||
|
||||
let outgoingCallItems: CallHierarchyIncomingCall[] = [];
|
||||
let outgoingCallItems: vscode.CallHierarchyIncomingCall[] = [];
|
||||
|
||||
if (model.isVerb(item.name)) {
|
||||
let outgoingCalls = model.getVerbRelations(item.name)
|
||||
@ -66,7 +63,7 @@ export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
outgoingCalls.forEach(relation => {
|
||||
let outgoingCallRange = relation.getRangeOf(relation.subject);
|
||||
let verbItem = this.createCallHierarchyItem(relation.subject, 'noun', document, outgoingCallRange);
|
||||
let outgoingCallItem = new CallHierarchyIncomingCall(verbItem, [outgoingCallRange]);
|
||||
let outgoingCallItem = new vscode.CallHierarchyIncomingCall(verbItem, [outgoingCallRange]);
|
||||
outgoingCallItems.push(outgoingCallItem);
|
||||
});
|
||||
}
|
||||
@ -76,7 +73,7 @@ export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
outgoingCallMap.forEach((relations, verb) => {
|
||||
let outgoingCallRanges = relations.map(relation => relation.getRangeOf(verb));
|
||||
let verbItem = this.createCallHierarchyItem(verb, 'verb-inverted', document, outgoingCallRanges[0]);
|
||||
let outgoingCallItem = new CallHierarchyIncomingCall(verbItem, outgoingCallRanges);
|
||||
let outgoingCallItem = new vscode.CallHierarchyIncomingCall(verbItem, outgoingCallRanges);
|
||||
outgoingCallItems.push(outgoingCallItem);
|
||||
});
|
||||
}
|
||||
@ -84,8 +81,8 @@ export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
return outgoingCallItems;
|
||||
}
|
||||
|
||||
private createCallHierarchyItem(word: string, type: string, document: TextDocument, range: Range): CallHierarchyItem {
|
||||
return new CallHierarchyItem(SymbolKind.Object, word, `(${type})`, document.uri, range, range);
|
||||
private createCallHierarchyItem(word: string, type: string, document: vscode.TextDocument, range: vscode.Range): vscode.CallHierarchyItem {
|
||||
return new vscode.CallHierarchyItem(vscode.SymbolKind.Object, word, `(${type})`, document.uri, range, range);
|
||||
}
|
||||
|
||||
}
|
||||
@ -94,19 +91,19 @@ export class FoodPyramidHierarchyProvider implements CallHierarchyProvider {
|
||||
* Sample parser of the document text into the [FoodPyramid](#FoodPyramid) model.
|
||||
*/
|
||||
class FoodPyramidParser {
|
||||
private model = new FoodPyramid();
|
||||
private _model = new FoodPyramid();
|
||||
|
||||
getModel(): FoodPyramid {
|
||||
return this.model;
|
||||
return this._model;
|
||||
}
|
||||
|
||||
parse(textDocument: TextDocument): void {
|
||||
parse(textDocument: vscode.TextDocument): void {
|
||||
let pattern = /^(\w+)\s+(\w+)\s+(\w+).$/gm;
|
||||
let match: RegExpExecArray | null;
|
||||
while (match = pattern.exec(textDocument.getText())) {
|
||||
let startPosition = textDocument.positionAt(match.index);
|
||||
let range = new Range(startPosition, startPosition.translate({ characterDelta: match[0].length }));
|
||||
this.model.addRelation(new FoodRelation(match[1], match[2], match[3], match[0], range));
|
||||
let range = new vscode.Range(startPosition, startPosition.translate({ characterDelta: match[0].length }));
|
||||
this._model.addRelation(new FoodRelation(match[1], match[2], match[3], match[0], range));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
// The module 'vscode' contains the VS Code extensibility API
|
||||
// Import the module and reference it with the alias vscode in your code below
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import { FoodPyramidHierarchyProvider } from './FoodPyramidHierarchyProvider';
|
||||
|
||||
// this method is called when your extension is activated
|
||||
@ -19,10 +18,10 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
}
|
||||
|
||||
async function showSampleText(context: vscode.ExtensionContext): Promise<void> {
|
||||
fs.readFile(context.asAbsolutePath('sample.txt'), async (err, sampleText) => {
|
||||
let doc = await vscode.workspace.openTextDocument({ language: 'plaintext', content: sampleText.toString("utf-8") });
|
||||
vscode.window.showTextDocument(doc);
|
||||
});
|
||||
let sampleTextEncoded = await vscode.workspace.fs.readFile(vscode.Uri.file(context.asAbsolutePath('sample.txt')));
|
||||
let sampleText = new TextDecoder('utf-8').decode(sampleTextEncoded);
|
||||
let doc = await vscode.workspace.openTextDocument({ language: 'plaintext', content: sampleText });
|
||||
vscode.window.showTextDocument(doc);
|
||||
}
|
||||
|
||||
// this method is called when your extension is deactivated
|
||||
|
||||
@ -1,51 +1,48 @@
|
||||
import {
|
||||
Range, Position, CallHierarchyProvider, TextDocument, CancellationToken, CallHierarchyItem,
|
||||
SymbolKind, ProviderResult, CallHierarchyIncomingCall, CallHierarchyOutgoingCall, workspace, Uri
|
||||
} from 'vscode';
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
/**
|
||||
* Sample model of what the text in the document contains.
|
||||
*/
|
||||
export class FoodPyramid {
|
||||
private relations: FoodRelation[] = [];
|
||||
private nouns = new Set<string>();
|
||||
private verbs = new Set<string>();
|
||||
private _relations: FoodRelation[] = [];
|
||||
private _nouns = new Set<string>();
|
||||
private _verbs = new Set<string>();
|
||||
|
||||
getRelationAt(wordRange: Range): FoodRelation | undefined {
|
||||
return this.relations.find(relation => relation.range.contains(wordRange));
|
||||
getRelationAt(wordRange: vscode.Range): FoodRelation | undefined {
|
||||
return this._relations.find(relation => relation.range.contains(wordRange));
|
||||
}
|
||||
|
||||
addRelation(relation: FoodRelation): void {
|
||||
this.relations.push(relation);
|
||||
this.nouns.add(relation.object).add(relation.subject);
|
||||
this.verbs.add(relation.verb);
|
||||
this._relations.push(relation);
|
||||
this._nouns.add(relation.object).add(relation.subject);
|
||||
this._verbs.add(relation.verb);
|
||||
}
|
||||
|
||||
isVerb(name: string): boolean {
|
||||
return this.verbs.has(name.toLowerCase());
|
||||
return this._verbs.has(name.toLowerCase());
|
||||
}
|
||||
|
||||
isNoun(name: string): boolean {
|
||||
return this.nouns.has(name.toLowerCase());
|
||||
return this._nouns.has(name.toLowerCase());
|
||||
}
|
||||
|
||||
getVerbRelations(verb: string): FoodRelation[] {
|
||||
return this.relations
|
||||
return this._relations
|
||||
.filter(relation => relation.verb === verb.toLowerCase());
|
||||
}
|
||||
|
||||
getNounRelations(noun: string): FoodRelation[] {
|
||||
return this.relations
|
||||
return this._relations
|
||||
.filter(relation => relation.involves(noun));
|
||||
}
|
||||
|
||||
getSubjectRelations(subject: string): FoodRelation[] {
|
||||
return this.relations
|
||||
return this._relations
|
||||
.filter(relation => relation.subject === subject.toLowerCase());
|
||||
}
|
||||
|
||||
getObjectRelations(object: string): FoodRelation[] {
|
||||
return this.relations
|
||||
return this._relations
|
||||
.filter(relation => relation.object === object.toLowerCase());
|
||||
}
|
||||
}
|
||||
@ -59,7 +56,7 @@ export class FoodRelation {
|
||||
private _object: string;
|
||||
|
||||
constructor(subject: string, verb: string, object: string,
|
||||
private readonly originalText: string, public readonly range: Range) {
|
||||
private readonly originalText: string, public readonly range: vscode.Range) {
|
||||
|
||||
this._subject = subject.toLowerCase();
|
||||
this._verb = verb.toLowerCase();
|
||||
@ -83,9 +80,9 @@ export class FoodRelation {
|
||||
return this._subject === needle || this._object === needle;
|
||||
}
|
||||
|
||||
getRangeOf(word: string): Range {
|
||||
getRangeOf(word: string): vscode.Range {
|
||||
let indexOfWord = new RegExp("\\b" + word + "\\b", "i").exec(this.originalText)!.index;
|
||||
return new Range(this.range.start.translate({ characterDelta: indexOfWord }),
|
||||
return new vscode.Range(this.range.start.translate({ characterDelta: indexOfWord }),
|
||||
this.range.start.translate({ characterDelta: indexOfWord + word.length }));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user