Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow share providers to return text #184203

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/vs/workbench/api/browser/mainThreadShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class MainThreadShare implements MainThreadShareShape {
selector,
priority,
provideShare: async (item: IShareableItem) => {
return URI.revive(await this.proxy.$provideShare(handle, item, new CancellationTokenSource().token));
const result = await this.proxy.$provideShare(handle, item, new CancellationTokenSource().token);
return typeof result === 'string' ? result : URI.revive(result);
}
};
this.providers.set(handle, provider);
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ export interface ExtHostQuickDiffShape {
}

export interface ExtHostShareShape {
$provideShare(handle: number, shareableItem: IShareableItemDto, token: CancellationToken): Promise<UriComponents | undefined>;
$provideShare(handle: number, shareableItem: IShareableItemDto, token: CancellationToken): Promise<UriComponents | string | undefined>;
}

export interface ExtHostTaskShape {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHostShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ExtHostShare implements ExtHostShareShape {
this.proxy = mainContext.getProxy(MainContext.MainThreadShare);
}

async $provideShare(handle: number, shareableItem: IShareableItemDto, token: CancellationToken): Promise<UriComponents | undefined> {
async $provideShare(handle: number, shareableItem: IShareableItemDto, token: CancellationToken): Promise<UriComponents | string | undefined> {
const provider = this.providers.get(handle);
const result = await provider?.provideShare({ selection: Range.to(shareableItem.selection), resourceUri: URI.revive(shareableItem.resourceUri) }, token);
return result ?? undefined;
Expand Down
13 changes: 7 additions & 6 deletions src/vs/workbench/contrib/share/browser/share.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,29 @@ class ShareWorkbenchContribution {
const progressService = accessor.get(IProgressService);
const selection = accessor.get(ICodeEditorService).getActiveCodeEditor()?.getSelection() ?? undefined;

const uri = await progressService.withProgress({
const result = await progressService.withProgress({
location: ProgressLocation.Window,
detail: localize('generating link', 'Generating link...')
}, async () => shareService.provideShare({ resourceUri, selection }, new CancellationTokenSource().token));

if (uri) {
const uriText = uri.toString();
if (result) {
const uriText = result.toString();
const isResultText = typeof result === 'string';
await clipboardService.writeText(uriText);

dialogService.prompt(
{
type: Severity.Info,
message: localize('shareSuccess', 'Copied link to clipboard!'),
message: isResultText ? localize('shareTextSuccess', 'Copied text to clipboard!') : localize('shareSuccess', 'Copied link to clipboard!'),
custom: {
icon: Codicon.check,
markdownDetails: [{
markdown: new MarkdownString(`<div aria-label='${uriText}'>${uriText}</div>`, { supportHtml: true }),
classes: ['share-dialog-input']
classes: [isResultText ? 'share-dialog-input-text' : 'share-dialog-input-link']
}]
},
cancelButton: localize('close', 'Close'),
buttons: [{ label: localize('open link', 'Open Link'), run: () => { urlService.open(uri, { openExternal: true }); } }]
buttons: isResultText ? [] : [{ label: localize('open link', 'Open Link'), run: () => { urlService.open(result, { openExternal: true }); } }]
}
);
}
Expand Down
11 changes: 7 additions & 4 deletions src/vs/workbench/contrib/share/browser/share.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

div.share-dialog-input {
div.share-dialog-input-text,
div.share-dialog-input-link {
border: 1px solid var(--vscode-input-border, transparent);
border-radius: 2px;
color: var(--vscode-input-foreground);
background-color: var(--vscode-input-background);
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
padding: 2px;
user-select: all;
line-height: 24px;
}

div.share-dialog-input-link {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/share/browser/shareService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ShareService implements IShareService {
return [];
}

async provideShare(item: IShareableItem, token: CancellationToken): Promise<URI | undefined> {
async provideShare(item: IShareableItem, token: CancellationToken): Promise<URI | string | undefined> {
const language = this.codeEditorService.getActiveCodeEditor()?.getModel()?.getLanguageId() ?? '';
const providers = [...this._providers.values()]
.filter((p) => score(p.selector, item.resourceUri, language, true, undefined, undefined) > 0)
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/contrib/share/common/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface IShareProvider {
readonly priority: number;
readonly selector: LanguageSelector;
prepareShare?(item: IShareableItem, token: CancellationToken): Thenable<boolean | undefined>;
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | undefined>;
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | string | undefined>;
}

export const IShareService = createDecorator<IShareService>('shareService');
Expand All @@ -31,5 +31,5 @@ export interface IShareService {

registerShareProvider(provider: IShareProvider): IDisposable;
getShareActions(): ISubmenuItem[];
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | undefined>;
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | string | undefined>;
}
5 changes: 3 additions & 2 deletions src/vscode-dts/vscode.proposed.shareProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ declare module 'vscode' {
*
* @param item Data about an item which can be shared.
* @param token A cancellation token.
* @returns An {@link Uri} which will be copied to the user's clipboard and presented in a confirmation dialog.
* @returns A {@link Uri} representing an external link or sharing text. The provider result
* will be copied to the user's clipboard and presented in a confirmation dialog.
*/
provideShare(item: ShareableItem, token: CancellationToken): ProviderResult<Uri>;
provideShare(item: ShareableItem, token: CancellationToken): ProviderResult<Uri | string>;
}

export namespace window {
Expand Down