Skip to content

Commit 6dde517

Browse files
authored
Merge pull request microsoft#146312 from microsoft/joh/snippetTextEdit
2 parents 63a4ec7 + 5055dbe commit 6dde517

File tree

10 files changed

+88
-10
lines changed

10 files changed

+88
-10
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 { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
1616
import { ITextModel } from 'vs/editor/common/model';
1717
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
@@ -331,3 +331,16 @@ registerEditorCommand(new CommandCtor({
331331
// primary: KeyCode.Enter,
332332
// }
333333
}));
334+
335+
336+
// ---
337+
338+
export function performSnippetEdit(editor: ICodeEditor, edit: SnippetTextEdit) {
339+
const controller = SnippetController2.get(editor);
340+
if (!controller) {
341+
return false;
342+
}
343+
editor.setSelection(edit.range);
344+
controller.insert(edit.snippet);
345+
return controller.isInSnippet();
346+
}

src/vs/monaco.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6694,6 +6694,11 @@ declare namespace monaco.languages {
66946694
eol?: editor.EndOfLineSequence;
66956695
};
66966696

6697+
export interface SnippetTextEdit {
6698+
range: IRange;
6699+
snippet: string;
6700+
}
6701+
66976702
/**
66986703
* Interface used to format a model
66996704
*/

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { IDataTransfer, IDataTransferItem } from 'vs/workbench/common/dnd';
3333
import { extractEditorsDropData } from 'vs/workbench/browser/dnd';
3434
import { Mimes } from 'vs/base/common/mime';
3535
import { distinct } from 'vs/base/common/arrays';
36+
import { performSnippetEdit } from 'vs/editor/contrib/snippet/browser/snippetController2';
3637

3738
export interface IMainThreadEditorLocator {
3839
getEditor(id: string): MainThreadTextEditor | undefined;
@@ -88,7 +89,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
8889
this._registeredDecorationTypes = Object.create(null);
8990
}
9091

91-
public dispose(): void {
92+
dispose(): void {
9293
Object.keys(this._textEditorsListenersMap).forEach((editorId) => {
9394
dispose(this._textEditorsListenersMap[editorId]);
9495
});
@@ -139,14 +140,16 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
139140
}
140141

141142
private async onDropIntoEditor(editor: ICodeEditor, position: IPosition, dragEvent: DragEvent) {
142-
if (!dragEvent.dataTransfer) {
143+
if (!dragEvent.dataTransfer || !editor.hasModel()) {
143144
return;
144145
}
145146
const id = this._editorLocator.getIdOfCodeEditor(editor);
146147
if (typeof id !== 'string') {
147148
return;
148149
}
149150

151+
const modelVersionNow = editor.getModel().getVersionId();
152+
150153
const textEditorDataTransfer: IDataTransfer = new Map<string, IDataTransferItem>();
151154
for (const item of dragEvent.dataTransfer.items) {
152155
if (item.kind === 'string') {
@@ -173,9 +176,19 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
173176
}
174177
}
175178

176-
if (textEditorDataTransfer.size > 0) {
177-
const dataTransferDto = await DataTransferConverter.toDataTransferDTO(textEditorDataTransfer);
178-
return this._proxy.$textEditorHandleDrop(id, position, dataTransferDto);
179+
if (textEditorDataTransfer.size === 0) {
180+
return;
181+
}
182+
183+
const dataTransferDto = await DataTransferConverter.toDataTransferDTO(textEditorDataTransfer);
184+
const edits = await this._proxy.$textEditorHandleDrop(id, position, dataTransferDto);
185+
if (edits.length === 0) {
186+
return;
187+
}
188+
189+
if (editor.getModel().getVersionId() === modelVersionNow) {
190+
const [first] = edits; // TODO@jrieken define how to pick the "one snippet edit";
191+
performSnippetEdit(editor, first);
179192
}
180193
}
181194

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
@@ -1329,7 +1329,7 @@ export interface ISelectionChangeEvent {
13291329
export interface ExtHostEditorsShape {
13301330
$acceptEditorPropertiesChanged(id: string, props: IEditorPropertiesChangeData): void;
13311331
$acceptEditorPositionData(data: ITextEditorPositionData): void;
1332-
$textEditorHandleDrop(id: string, position: IPosition, dataTransferDto: DataTransferDTO): Promise<void>;
1332+
$textEditorHandleDrop(id: string, position: IPosition, dataTransferDto: DataTransferDTO): Promise<Dto<languages.SnippetTextEdit[]>>;
13331333
}
13341334

13351335
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
@@ -558,6 +558,15 @@ export namespace TextEdit {
558558
}
559559
}
560560

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

563572
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)