Skip to content

Commit

Permalink
For parts with location and command: give link gesture to location an…
Browse files Browse the repository at this point in the history
…d append command to context menu, add more jsdoc for API, remove dead code, #16221 (comment)
  • Loading branch information
jrieken committed Jan 20, 2022
1 parent 190fba9 commit a24000a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/vs/editor/contrib/inlayHints/inlayHintsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,12 @@ export class InlayHintsController implements IEditorContribution {
const label = this._getInlayHintLabelPart(e);
if (label) {
const part = label.part;
if (languages.Command.is(part.command)) {
// command -> execute it
this._commandService.executeCommand(part.command.id, ...(part.command.arguments ?? [])).catch(err => this._notificationService.error(err));

} else if (part.location) {
if (part.location) {
// location -> execute go to def
this._instaService.invokeFunction(goToDefinitionWithLocation, e, this._editor as IActiveCodeEditor, part.location);
} else if (languages.Command.is(part.command)) {
// command -> execute it
this._commandService.executeCommand(part.command.id, ...(part.command.arguments ?? [])).catch(err => this._notificationService.error(err));
}
}
});
Expand Down
12 changes: 11 additions & 1 deletion src/vs/editor/contrib/inlayHints/inlayHintsLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as dom from 'vs/base/browser/dom';
import { Action, IAction } from 'vs/base/common/actions';
import { Action, IAction, Separator } from 'vs/base/common/actions';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IActiveCodeEditor, ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorExtensionsRegistry } from 'vs/editor/browser/editorExtensions';
Expand All @@ -17,6 +17,7 @@ import { ClickLinkMouseEvent } from 'vs/editor/contrib/gotoSymbol/link/clickLink
import { RenderedInlayHintLabelPart } from 'vs/editor/contrib/inlayHints/inlayHintsController';
import { PeekContext } from 'vs/editor/contrib/peekView/peekView';
import { isIMenuItem, MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
Expand All @@ -25,6 +26,7 @@ export async function showGoToContextMenu(accessor: ServicesAccessor, editor: IC

const resolverService = accessor.get(ITextModelService);
const contextMenuService = accessor.get(IContextMenuService);
const commandService = accessor.get(ICommandService);
const instaService = accessor.get(IInstantiationService);

await part.item.resolve(CancellationToken.None);
Expand Down Expand Up @@ -55,6 +57,14 @@ export async function showGoToContextMenu(accessor: ServicesAccessor, editor: IC
}
}

if (part.part.command) {
const { command } = part.part;
menuActions.push(new Separator());
menuActions.push(new Action(command.id, command.title, undefined, true, () => {
commandService.executeCommand(command.id, ...(command.arguments ?? []));
}));
}

// show context menu
const useShadowDOM = editor.getOption(EditorOption.useShadowDOM);
contextMenuService.showContextMenu({
Expand Down
15 changes: 0 additions & 15 deletions src/vs/platform/actions/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,6 @@ export const MenuRegistry: IMenuRegistry = new class implements IMenuRegistry {
}
};

export class ExecuteCommandAction extends Action {

constructor(
id: string,
label: string,
@ICommandService private readonly _commandService: ICommandService) {

super(id, label);
}

override run(...args: any[]): Promise<void> {
return this._commandService.executeCommand(this.id, ...args);
}
}

export class SubmenuItemAction extends SubmenuAction {

constructor(
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/api/common/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,8 @@ class InlayHintsAdapter {
result.tooltip = part.tooltip && typeConvert.MarkdownString.from(part.tooltip);
if (Location.isLocation(part.location)) {
result.location = typeConvert.location.from(part.location);
} else if (part.command) {
}
if (part.command) {
result.command = this._commands.toInternal(part.command, disposables);
}
return result;
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/api/common/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,8 @@ export namespace InlayHintLabelPart {
: part.tooltip;
if (modes.Command.is(part.command)) {
result.command = converter.fromInternal(part.command);
} else if (part.location) {
}
if (part.location) {
result.location = location.to(part.location);
}
return result;
Expand Down
22 changes: 21 additions & 1 deletion src/vscode-dts/vscode.proposed.inlayHints.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,29 @@ declare module 'vscode' {
*/
tooltip?: string | MarkdownString | undefined;

// invokes provider
/**
* An optional {@link Location source code location} that represents this label
* part.
*
* The editor will use this location for the hover and for code navigation features: This
* part will become a clickable link that resolves to the definition of the symbol at the
* given location (not neccessarily the location itself), it shows the hover that shows at
* the given location, and it shows a context menu with further code navigation commands.
*
* *Note* that this property can be set late during
* {@link InlayHintsProvider.resolveInlayHint resolving} of inlay hints.
*/
location?: Location | undefined;

/**
* An optional command for this label part.
*
* The editor renders parts with commands as clickable links. The command is added to the context menu
* when a label part defines {@link InlayHintLabelPart.location location} and {@link InlayHintLabelPart.command command} .
*
* *Note* that this property can be set late during
* {@link InlayHintsProvider.resolveInlayHint resolving} of inlay hints.
*/
command?: Command | undefined;

// todo@api
Expand Down

0 comments on commit a24000a

Please sign in to comment.