Skip to content

Commit bd71718

Browse files
committed
add SnippetTextEdit, allow drop-handler to return them
1 parent a3e739f commit bd71718

File tree

10 files changed

+83
-9
lines changed

10 files changed

+83
-9
lines changed

src/vs/editor/common/languages.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,11 @@ export interface DocumentSymbolProvider {
12931293

12941294
export type TextEdit = { range: IRange; text: string; eol?: model.EndOfLineSequence };
12951295

1296+
export interface SnippetTextEdit {
1297+
range: IRange;
1298+
snippet: string;
1299+
}
1300+
12961301
/**
12971302
* Interface used to format a model
12981303
*/

src/vs/editor/contrib/snippet/browser/snippetController2.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Position } from 'vs/editor/common/core/position';
1111
import { Range } from 'vs/editor/common/core/range';
1212
import { IEditorContribution } from 'vs/editor/common/editorCommon';
1313
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
14-
import { CompletionItem, CompletionItemKind, CompletionItemProvider } from 'vs/editor/common/languages';
14+
import { CompletionItem, CompletionItemKind, CompletionItemProvider, SnippetTextEdit } from 'vs/editor/common/languages';
1515
import { ITextModel } from 'vs/editor/common/model';
1616
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
1717
import { Choice } from 'vs/editor/contrib/snippet/browser/snippetParser';
@@ -329,3 +329,16 @@ registerEditorCommand(new CommandCtor({
329329
// primary: KeyCode.Enter,
330330
// }
331331
}));
332+
333+
334+
// ---
335+
336+
export function performSnippetEdit(editor: ICodeEditor, edit: SnippetTextEdit) {
337+
const controller = SnippetController2.get(editor);
338+
if (!controller) {
339+
return false;
340+
}
341+
editor.setSelection(edit.range);
342+
controller.insert(edit.snippet);
343+
return controller.isInSnippet();
344+
}

src/vs/monaco.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6688,6 +6688,11 @@ declare namespace monaco.languages {
66886688
eol?: editor.EndOfLineSequence;
66896689
};
66906690

6691+
export interface SnippetTextEdit {
6692+
range: IRange;
6693+
snippet: string;
6694+
}
6695+
66916696
/**
66926697
* Interface used to format a model
66936698
*/

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { IDataTransfer, IDataTransferItem } from 'vs/workbench/common/dnd';
3737
import { extractEditorsDropData } from 'vs/workbench/browser/dnd';
3838
import { Mimes } from 'vs/base/common/mime';
3939
import { distinct } from 'vs/base/common/arrays';
40+
import { performSnippetEdit } from 'vs/editor/contrib/snippet/browser/snippetController2';
4041

4142
export function reviveWorkspaceEditDto2(data: IWorkspaceEditDto | undefined): ResourceEdit[] {
4243
if (!data?.edits) {
@@ -111,7 +112,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
111112
this._registeredDecorationTypes = Object.create(null);
112113
}
113114

114-
public dispose(): void {
115+
dispose(): void {
115116
Object.keys(this._textEditorsListenersMap).forEach((editorId) => {
116117
dispose(this._textEditorsListenersMap[editorId]);
117118
});
@@ -196,10 +197,18 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
196197
}
197198
}
198199

199-
if (textEditorDataTransfer.size > 0) {
200-
const dataTransferDto = await DataTransferConverter.toDataTransferDTO(textEditorDataTransfer);
201-
return this._proxy.$textEditorHandleDrop(id, position, dataTransferDto);
200+
if (textEditorDataTransfer.size === 0) {
201+
return;
202202
}
203+
204+
const dataTransferDto = await DataTransferConverter.toDataTransferDTO(textEditorDataTransfer);
205+
const edits = await this._proxy.$textEditorHandleDrop(id, position, dataTransferDto);
206+
if (edits.length === 0) {
207+
return;
208+
}
209+
210+
const [first] = edits; // TODO define how to pick the "one snippet edit";
211+
performSnippetEdit(editor, first);
203212
}
204213

205214
// --- from extension host process

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
12431243
SignatureHelpTriggerKind: extHostTypes.SignatureHelpTriggerKind,
12441244
SignatureInformation: extHostTypes.SignatureInformation,
12451245
SnippetString: extHostTypes.SnippetString,
1246+
SnippetTextEdit: extHostTypes.SnippetTextEdit,
12461247
SourceBreakpoint: extHostTypes.SourceBreakpoint,
12471248
StandardTokenType: extHostTypes.StandardTokenType,
12481249
StatusBarAlignment: extHostTypes.StatusBarAlignment,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ export interface ISelectionChangeEvent {
13241324
export interface ExtHostEditorsShape {
13251325
$acceptEditorPropertiesChanged(id: string, props: IEditorPropertiesChangeData): void;
13261326
$acceptEditorPositionData(data: ITextEditorPositionData): void;
1327-
$textEditorHandleDrop(id: string, position: IPosition, dataTransferDto: DataTransferDTO): Promise<void>;
1327+
$textEditorHandleDrop(id: string, position: IPosition, dataTransferDto: DataTransferDTO): Promise<Dto<languages.SnippetTextEdit[]>>;
13281328
}
13291329

13301330
export interface IDocumentsAndEditorsDelta {

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import { ExtHostEditorsShape, IEditorPropertiesChangeData, IMainContext, ITextDo
99
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
1010
import { ExtHostTextEditor, TextEditorDecorationType } from 'vs/workbench/api/common/extHostTextEditor';
1111
import * as TypeConverters from 'vs/workbench/api/common/extHostTypeConverters';
12-
import { TextEditorSelectionChangeKind } from 'vs/workbench/api/common/extHostTypes';
12+
import { SnippetTextEdit, TextEditorSelectionChangeKind } from 'vs/workbench/api/common/extHostTypes';
1313
import * as vscode from 'vscode';
1414
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
1515
import { DataTransferConverter, DataTransferDTO } from 'vs/workbench/api/common/shared/dataTransfer';
1616
import { IPosition } from 'vs/editor/common/core/position';
1717
import { CancellationToken } from 'vs/base/common/cancellation';
18+
import { Dto } from 'vs/workbench/services/extensions/common/proxyIdentifier';
19+
import * as languages from 'vs/editor/common/languages';
1820

1921
export class ExtHostEditors implements ExtHostEditorsShape {
2022

@@ -167,7 +169,7 @@ export class ExtHostEditors implements ExtHostEditorsShape {
167169

168170
// --- Text editor drag and drop
169171

170-
async $textEditorHandleDrop(id: string, position: IPosition, dataTransferDto: DataTransferDTO): Promise<void> {
172+
async $textEditorHandleDrop(id: string, position: IPosition, dataTransferDto: DataTransferDTO): Promise<Dto<languages.SnippetTextEdit[]>> {
171173
const textEditor = this._extHostDocumentsAndEditors.getEditor(id);
172174
if (!textEditor) {
173175
throw new Error('Unknown text editor');
@@ -182,6 +184,15 @@ export class ExtHostEditors implements ExtHostEditorsShape {
182184
dataTransfer: dataTransfer
183185
});
184186

185-
await this._onWillDropOnTextEditor.fireAsync(event, CancellationToken.None);
187+
const edits: SnippetTextEdit[] = [];
188+
189+
await this._onWillDropOnTextEditor.fireAsync(event, CancellationToken.None, async p => {
190+
const value = await p;
191+
if (value instanceof SnippetTextEdit) {
192+
edits.push(value);
193+
}
194+
});
195+
196+
return edits.map(TypeConverters.SnippetTextEdit.from);
186197
}
187198
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,15 @@ export namespace TextEdit {
557557
}
558558
}
559559

560+
export namespace SnippetTextEdit {
561+
export function from(edit: vscode.SnippetTextEdit): languages.SnippetTextEdit {
562+
return {
563+
range: Range.from(edit.range),
564+
snippet: edit.snippet.value
565+
};
566+
}
567+
}
568+
560569
export namespace WorkspaceEdit {
561570

562571
export interface IVersionInformationProvider {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,17 @@ export class TextEdit {
598598
}
599599
}
600600

601+
export class SnippetTextEdit implements vscode.SnippetTextEdit {
602+
603+
range: vscode.Range;
604+
snippet: vscode.SnippetString;
605+
606+
constructor(range: Range, snippet: SnippetString) {
607+
this.range = range;
608+
this.snippet = snippet;
609+
}
610+
}
611+
601612
export interface IFileOperationOptions {
602613
overwrite?: boolean;
603614
ignoreIfExists?: boolean;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ declare module 'vscode' {
77

88
// https://github.com/microsoft/vscode/issues/142990
99

10+
export class SnippetTextEdit {
11+
snippet: SnippetString;
12+
range: Range;
13+
constructor(range: Range, snippet: SnippetString);
14+
}
15+
16+
1017
export interface TextEditorDropEvent {
1118
/**
1219
* The {@link TextEditor} the resource was dropped onto.
@@ -43,6 +50,9 @@ declare module 'vscode' {
4350
*/
4451
waitUntil(thenable: Thenable<any>): void;
4552

53+
//
54+
waitUntil(thenable: Thenable<SnippetTextEdit>): void;
55+
4656
token: CancellationToken;
4757
}
4858

0 commit comments

Comments
 (0)