Skip to content

Commit

Permalink
wip: update ServiceContext.commands
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Apr 25, 2023
1 parent b98334e commit bc678fa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 52 deletions.
107 changes: 61 additions & 46 deletions packages/language-service/src/baseLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,58 +112,73 @@ function createLanguageServicePluginContext(
} : undefined,
documents: textDocumentMapper,
commands: {
createRenameCommand(uri, position) {
const source = toSourceLocation(uri, position, data => typeof data.rename === 'object' ? !!data.rename.normalize : !!data.rename);
if (!source) {
return;
}
return vscode.Command.create(
'',
'editor.action.rename',
source.uri,
source.position,
);
rename: {
create(uri, position) {
const source = toSourceLocation(uri, position, data => typeof data.rename === 'object' ? !!data.rename.normalize : !!data.rename);
if (!source) {
return;
}
return vscode.Command.create(
'',
'editor.action.rename',
source.uri,
source.position,
);
},
is(command) {
return command.command === 'editor.action.rename';
},
},
createShowReferencesCommand(uri, position, locations) {
const source = toSourceLocation(uri, position);
if (!source) {
return;
}
const sourceReferences: vscode.Location[] = [];
for (const reference of locations) {
if (context.documents.isVirtualFileUri(reference.uri)) {
for (const [_, map] of context.documents.getMapsByVirtualFileUri(reference.uri)) {
const range = map.toSourceRange(reference.range);
if (range) {
sourceReferences.push({ uri: map.sourceFileDocument.uri, range });
showReferences: {
create(uri, position, locations) {
const source = toSourceLocation(uri, position);
if (!source) {
return;
}
const sourceReferences: vscode.Location[] = [];
for (const reference of locations) {
if (context.documents.isVirtualFileUri(reference.uri)) {
for (const [_, map] of context.documents.getMapsByVirtualFileUri(reference.uri)) {
const range = map.toSourceRange(reference.range);
if (range) {
sourceReferences.push({ uri: map.sourceFileDocument.uri, range });
}
}
}
else {
sourceReferences.push(reference);
}
}
else {
sourceReferences.push(reference);
}
}
return vscode.Command.create(
locations.length === 1 ? '1 reference' : `${locations.length} references`,
'editor.action.showReferences',
source.uri,
source.position,
sourceReferences,
);
return vscode.Command.create(
locations.length === 1 ? '1 reference' : `${locations.length} references`,
'editor.action.showReferences',
source.uri,
source.position,
sourceReferences,
);
},
is(command) {
return command.command === 'editor.action.showReferences';
},
},
createSetSelectionCommand(position: vscode.Position) {
return vscode.Command.create(
'',
'setSelection',
{
selection: {
selectionStartLineNumber: position.line + 1,
positionLineNumber: position.line + 1,
selectionStartColumn: position.character + 1,
positionColumn: position.character + 1,
setSelection: {
create(position: vscode.Position) {
return vscode.Command.create(
'',
'setSelection',
{
selection: {
selectionStartLineNumber: position.line + 1,
positionLineNumber: position.line + 1,
selectionStartColumn: position.character + 1,
positionColumn: position.character + 1,
},
},
},
);
);
},
is(command) {
return command.command === 'setSelection';
}
},
},
getTextDocument,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function register(context: ServiceContext) {
references = await plugin.resolveReferencesCodeLensLocations(document, data.range, references, token);
}

item.command = context.commands.createShowReferencesCommand(data.uri, data.range.start, references);
item.command = context.commands.showReferences.create(data.uri, data.range.start, references);
}

return item;
Expand Down
14 changes: 9 additions & 5 deletions packages/language-service/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ interface FileSystemHost {
onDidChangeWatchedFiles(cb: (params: vscode.DidChangeWatchedFilesParams) => void): () => void,
}

export interface Commands {
createShowReferencesCommand(uri: string, position: vscode.Position, locations: vscode.Location[]): vscode.Command | undefined;
createRenameCommand(uri: string, position: vscode.Position): vscode.Command | undefined;
createSetSelectionCommand(position: vscode.Position): vscode.Command | undefined;
interface Command<T> {
create: T;
is(value: vscode.Command): boolean;
}

export interface ServiceContext extends ServiceOptions {
Expand All @@ -47,7 +46,12 @@ export interface ServiceContext extends ServiceOptions {
languageServiceHost: ts.LanguageServiceHost;
languageService: ts.LanguageService;
} | undefined;
commands: Commands;

commands: {
showReferences: Command<(uri: string, position: vscode.Position, locations: vscode.Location[]) => vscode.Command | undefined>;
rename: Command<(uri: string, position: vscode.Position) => vscode.Command | undefined>;
setSelection: Command<(position: vscode.Position) => vscode.Command | undefined>;
};

/** @private */
core: LanguageContext;
Expand Down

0 comments on commit bc678fa

Please sign in to comment.