Skip to content

Commit 5504c4b

Browse files
authored
Allow share providers to return text (#184203)
1 parent fa84a90 commit 5504c4b

File tree

8 files changed

+24
-18
lines changed

8 files changed

+24
-18
lines changed

src/vs/workbench/api/browser/mainThreadShare.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export class MainThreadShare implements MainThreadShareShape {
3131
selector,
3232
priority,
3333
provideShare: async (item: IShareableItem) => {
34-
return URI.revive(await this.proxy.$provideShare(handle, item, new CancellationTokenSource().token));
34+
const result = await this.proxy.$provideShare(handle, item, new CancellationTokenSource().token);
35+
return typeof result === 'string' ? result : URI.revive(result);
3536
}
3637
};
3738
this.providers.set(handle, provider);

src/vs/workbench/api/common/extHost.protocol.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2032,7 +2032,7 @@ export interface ExtHostQuickDiffShape {
20322032
}
20332033

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

20382038
export interface ExtHostTaskShape {

src/vs/workbench/api/common/extHostShare.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class ExtHostShare implements ExtHostShareShape {
2323
this.proxy = mainContext.getProxy(MainContext.MainThreadShare);
2424
}
2525

26-
async $provideShare(handle: number, shareableItem: IShareableItemDto, token: CancellationToken): Promise<UriComponents | undefined> {
26+
async $provideShare(handle: number, shareableItem: IShareableItemDto, token: CancellationToken): Promise<UriComponents | string | undefined> {
2727
const provider = this.providers.get(handle);
2828
const result = await provider?.provideShare({ selection: Range.to(shareableItem.selection), resourceUri: URI.revive(shareableItem.resourceUri) }, token);
2929
return result ?? undefined;

src/vs/workbench/contrib/share/browser/share.contribution.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,29 @@ class ShareWorkbenchContribution {
8686
const progressService = accessor.get(IProgressService);
8787
const selection = accessor.get(ICodeEditorService).getActiveCodeEditor()?.getSelection() ?? undefined;
8888

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

94-
if (uri) {
95-
const uriText = uri.toString();
94+
if (result) {
95+
const uriText = result.toString();
96+
const isResultText = typeof result === 'string';
9697
await clipboardService.writeText(uriText);
9798

9899
dialogService.prompt(
99100
{
100101
type: Severity.Info,
101-
message: localize('shareSuccess', 'Copied link to clipboard!'),
102+
message: isResultText ? localize('shareTextSuccess', 'Copied text to clipboard!') : localize('shareSuccess', 'Copied link to clipboard!'),
102103
custom: {
103104
icon: Codicon.check,
104105
markdownDetails: [{
105106
markdown: new MarkdownString(`<div aria-label='${uriText}'>${uriText}</div>`, { supportHtml: true }),
106-
classes: ['share-dialog-input']
107+
classes: [isResultText ? 'share-dialog-input-text' : 'share-dialog-input-link']
107108
}]
108109
},
109110
cancelButton: localize('close', 'Close'),
110-
buttons: [{ label: localize('open link', 'Open Link'), run: () => { urlService.open(uri, { openExternal: true }); } }]
111+
buttons: isResultText ? [] : [{ label: localize('open link', 'Open Link'), run: () => { urlService.open(result, { openExternal: true }); } }]
111112
}
112113
);
113114
}

src/vs/workbench/contrib/share/browser/share.css

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
div.share-dialog-input {
6+
div.share-dialog-input-text,
7+
div.share-dialog-input-link {
78
border: 1px solid var(--vscode-input-border, transparent);
89
border-radius: 2px;
910
color: var(--vscode-input-foreground);
1011
background-color: var(--vscode-input-background);
11-
overflow: hidden;
12-
white-space: nowrap;
13-
text-overflow: ellipsis;
1412
padding: 2px;
1513
user-select: all;
1614
line-height: 24px;
1715
}
1816

17+
div.share-dialog-input-link {
18+
overflow: hidden;
19+
white-space: nowrap;
20+
text-overflow: ellipsis;
21+
}

src/vs/workbench/contrib/share/browser/shareService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class ShareService implements IShareService {
4848
return [];
4949
}
5050

51-
async provideShare(item: IShareableItem, token: CancellationToken): Promise<URI | undefined> {
51+
async provideShare(item: IShareableItem, token: CancellationToken): Promise<URI | string | undefined> {
5252
const language = this.codeEditorService.getActiveCodeEditor()?.getModel()?.getLanguageId() ?? '';
5353
const providers = [...this._providers.values()]
5454
.filter((p) => score(p.selector, item.resourceUri, language, true, undefined, undefined) > 0)

src/vs/workbench/contrib/share/common/share.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface IShareProvider {
2222
readonly priority: number;
2323
readonly selector: LanguageSelector;
2424
prepareShare?(item: IShareableItem, token: CancellationToken): Thenable<boolean | undefined>;
25-
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | undefined>;
25+
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | string | undefined>;
2626
}
2727

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

3232
registerShareProvider(provider: IShareProvider): IDisposable;
3333
getShareActions(): ISubmenuItem[];
34-
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | undefined>;
34+
provideShare(item: IShareableItem, token: CancellationToken): Thenable<URI | string | undefined>;
3535
}

src/vscode-dts/vscode.proposed.shareProvider.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ declare module 'vscode' {
4747
*
4848
* @param item Data about an item which can be shared.
4949
* @param token A cancellation token.
50-
* @returns An {@link Uri} which will be copied to the user's clipboard and presented in a confirmation dialog.
50+
* @returns A {@link Uri} representing an external link or sharing text. The provider result
51+
* will be copied to the user's clipboard and presented in a confirmation dialog.
5152
*/
52-
provideShare(item: ShareableItem, token: CancellationToken): ProviderResult<Uri>;
53+
provideShare(item: ShareableItem, token: CancellationToken): ProviderResult<Uri | string>;
5354
}
5455

5556
export namespace window {

0 commit comments

Comments
 (0)