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

Add scaffold for ts drop to add import #228997

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { ITypeScriptServiceClient } from '../typescriptService';
import { UriList } from '../utils/uriList';

const mimes = Object.freeze({
uriList: 'text/uri-list',
});


class MyDocumentDropEditProvider implements vscode.DocumentDropEditProvider {

constructor(
private readonly client: ITypeScriptServiceClient,
) { }

async provideDocumentDropEdits(
document: vscode.TextDocument,
position: vscode.Position,
dataTransfer: vscode.DataTransfer,
token: vscode.CancellationToken
): Promise<vscode.DocumentDropEdit | undefined> {
const text = dataTransfer.get(mimes.uriList)?.value;
if (!text) {
return;
}

const uriList = UriList.from(text);
if (!uriList.entries.length) {
return;
}

const response = await this.client.execute('getDropOrPasteEdit', {}, token);
if (!response || response.type !== 'response' || !response.body) {

}
const edit = new vscode.DocumentDropEdit(uriList.entries[0].str);

return edit;
}
}

export function register(
selector: vscode.DocumentSelector,
client: ITypeScriptServiceClient,
): vscode.Disposable {
return vscode.languages.registerDocumentDropEditProvider(selector, new MyDocumentDropEditProvider(client));
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default class LanguageProvider extends Disposable {
import('./languageFeatures/directiveCommentCompletions').then(provider => this._register(provider.register(selector, this.client))),
import('./languageFeatures/documentHighlight').then(provider => this._register(provider.register(selector, this.client))),
import('./languageFeatures/documentSymbol').then(provider => this._register(provider.register(selector, this.client, cachedNavTreeResponse))),
import('./languageFeatures/dropOrPasteResource').then(provider => this._register(provider.register(selector, this.client))),
import('./languageFeatures/fileReferences').then(provider => this._register(provider.register(this.client, this.commandManager))),
import('./languageFeatures/fixAll').then(provider => this._register(provider.register(selector, this.client, this.fileConfigurationManager, this.client.diagnosticsManager))),
import('./languageFeatures/folding').then(provider => this._register(provider.register(selector, this.client))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ interface StandardTsServerRequests {
'linkedEditingRange': [Proto.FileLocationRequestArgs, Proto.LinkedEditingRangeResponse];
'mapCode': [Proto.MapCodeRequestArgs, Proto.MapCodeResponse];
'getPasteEdits': [Proto.GetPasteEditsRequestArgs, Proto.GetPasteEditsResponse];

// Experimental

// FIll in right types here
// You can define the types in `extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts` before they are officially published
'getDropOrPasteEdit': [Proto.FileLocationRequestArgs, Proto.DefinitionResponse];
}

interface NoResponseTsServerRequests {
Expand Down
35 changes: 35 additions & 0 deletions extensions/typescript-language-features/src/utils/uriList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { coalesce } from './arrays';
import * as vscode from 'vscode';

function splitUriList(str: string): string[] {
return str.split('\r\n');
}

function parseUriList(str: string): string[] {
return splitUriList(str)
.filter(value => !value.startsWith('#')) // Remove comments
.map(value => value.trim());
}

export class UriList {

static from(str: string): UriList {
return new UriList(coalesce(parseUriList(str).map(line => {
try {
return { uri: vscode.Uri.parse(line), str: line };
} catch {
// Uri parse failure
return undefined;
}
})));
}

private constructor(
public readonly entries: ReadonlyArray<{ readonly uri: vscode.Uri; readonly str: string }>
) { }
}
Loading