Skip to content

Commit

Permalink
Standardize text/uri-list handling (#152756)
Browse files Browse the repository at this point in the history
This creates a common set of functions for creating and parsing the `text/uri-list` mime type. As part of this, I also aligned us with the standard, which uses `\r\n` between lines instead of just `\n`
  • Loading branch information
mjbvz committed Jun 22, 2022
1 parent 09fa374 commit 321423d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function tryGetUriListSnippet(document: vscode.TextDocument, dataTr
}

const uris: vscode.Uri[] = [];
for (const resource of urlList.split('\n')) {
for (const resource of urlList.split('\r\n')) {
try {
uris.push(vscode.Uri.parse(resource));
} catch {
Expand Down
13 changes: 11 additions & 2 deletions src/vs/editor/browser/dnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,21 @@ export function addExternalEditorsDropData(dataTransfer: VSDataTransfer, dragEve
}

if (editorData.length) {
const str = distinct(editorData).join('\n');
dataTransfer.replace(Mimes.uriList, createStringDataTransferItem(str));
dataTransfer.replace(Mimes.uriList, createStringDataTransferItem(UriList.create(editorData)));
}
}

for (const internal of INTERNAL_DND_MIME_TYPES) {
dataTransfer.delete(internal);
}
}

export const UriList = Object.freeze({
// http://amundsen.com/hypermedia/urilist/
create: (entries: ReadonlyArray<string | URI>): string => {
return distinct(entries.map(x => x.toString())).join('\r\n');
},
parse: (str: string): string[] => {
return str.split('\r\n').filter(value => !value.startsWith('#'));
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createStringDataTransferItem, VSDataTransfer } from 'vs/base/common/dat
import { Disposable } from 'vs/base/common/lifecycle';
import { Mimes } from 'vs/base/common/mime';
import { generateUuid } from 'vs/base/common/uuid';
import { toVSDataTransfer } from 'vs/editor/browser/dnd';
import { toVSDataTransfer, UriList } from 'vs/editor/browser/dnd';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { IBulkEditService, ResourceEdit } from 'vs/editor/browser/services/bulkEditService';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
Expand Down Expand Up @@ -176,8 +176,7 @@ export class CopyPasteController extends Disposable implements IEditorContributi
if (!dataTransfer.has(Mimes.uriList)) {
const resources = await this._clipboardService.readResources();
if (resources.length) {
const value = resources.join('\n');
dataTransfer.append(Mimes.uriList, createStringDataTransferItem(value));
dataTransfer.append(Mimes.uriList, createStringDataTransferItem(UriList.create(resources)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Disposable } from 'vs/base/common/lifecycle';
import { Mimes } from 'vs/base/common/mime';
import { relativePath } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { addExternalEditorsDropData, toVSDataTransfer } from 'vs/editor/browser/dnd';
import { addExternalEditorsDropData, toVSDataTransfer, UriList } from 'vs/editor/browser/dnd';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { IBulkEditService, ResourceEdit } from 'vs/editor/browser/services/bulkEditService';
Expand Down Expand Up @@ -137,9 +137,9 @@ class DefaultOnDropProvider implements DocumentOnDropEditProvider {
return undefined;
}

private getUriListInsertText(urlList: string): string | undefined {
private getUriListInsertText(strUriList: string): string | undefined {
const uris: URI[] = [];
for (const resource of urlList.split('\n')) {
for (const resource of UriList.parse(strUriList)) {
try {
uris.push(URI.parse(resource));
} catch {
Expand Down
8 changes: 2 additions & 6 deletions src/vs/workbench/browser/dnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { FileAccess, Schemas } from 'vs/base/common/network';
import { isWindows } from 'vs/base/common/platform';
import { basename, isEqual } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { UriList } from 'vs/editor/browser/dnd';
import { CodeDataTransfers, createDraggedEditorInputFromRawResourcesData, Extensions, extractEditorsAndFilesDropData, IDragAndDropContributionRegistry, IDraggedResourceEditorInput, IResourceStat } from 'vs/platform/dnd/browser/dnd';
import { IFileService } from 'vs/platform/files/common/files';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function extractTreeDropData(dataTransfer: VSDataTransfer): Promise
if (dataTransfer.has(resourcesKey)) {
try {
const asString = await dataTransfer.get(resourcesKey)?.asString();
const rawResourcesData = JSON.stringify(asString?.split('\n').filter(value => !value.startsWith('#')));
const rawResourcesData = JSON.stringify(UriList.parse(asString ?? ''));
editors.push(...createDraggedEditorInputFromRawResourcesData(rawResourcesData));
} catch (error) {
// Invalid transfer
Expand All @@ -70,11 +71,6 @@ export async function extractTreeDropData(dataTransfer: VSDataTransfer): Promise
return editors;
}

export function convertResourceUrlsToUriList(resourceUrls: string): string {
const asJson: URI[] = JSON.parse(resourceUrls);
return asJson.map(uri => uri.toString()).join('\n');
}

export interface IResourcesDropHandlerOptions {

/**
Expand Down

0 comments on commit 321423d

Please sign in to comment.