diff options
Diffstat (limited to 'src/vs/editor/contrib/snippet/browser/snippetController2.ts')
-rw-r--r-- | src/vs/editor/contrib/snippet/browser/snippetController2.ts | 79 |
1 files changed, 30 insertions, 49 deletions
diff --git a/src/vs/editor/contrib/snippet/browser/snippetController2.ts b/src/vs/editor/contrib/snippet/browser/snippetController2.ts index 43e72f6ce57..862f3a20493 100644 --- a/src/vs/editor/contrib/snippet/browser/snippetController2.ts +++ b/src/vs/editor/contrib/snippet/browser/snippetController2.ts @@ -5,25 +5,26 @@ import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; import { DisposableStore } from 'vs/base/common/lifecycle'; +import { assertType } from 'vs/base/common/types'; import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; import { EditorCommand, registerEditorCommand, registerEditorContribution } from 'vs/editor/browser/editorExtensions'; import { Position } from 'vs/editor/common/core/position'; import { Range } from 'vs/editor/common/core/range'; -import { ISelection } from 'vs/editor/common/core/selection'; +import { ISelection, Selection } from 'vs/editor/common/core/selection'; import { IEditorContribution } from 'vs/editor/common/editorCommon'; import { EditorContextKeys } from 'vs/editor/common/editorContextKeys'; import { CompletionItem, CompletionItemKind, CompletionItemProvider } from 'vs/editor/common/languages'; import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry'; import { ITextModel } from 'vs/editor/common/model'; import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures'; -import { Choice, SnippetParser } from 'vs/editor/contrib/snippet/browser/snippetParser'; +import { Choice } from 'vs/editor/contrib/snippet/browser/snippetParser'; import { showSimpleSuggestions } from 'vs/editor/contrib/suggest/browser/suggest'; import { OvertypingCapturer } from 'vs/editor/contrib/suggest/browser/suggestOvertypingCapturer'; import { localize } from 'vs/nls'; import { ContextKeyExpr, IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey'; import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; import { ILogService } from 'vs/platform/log/common/log'; -import { SnippetSession } from './snippetSession'; +import { ISnippetEdit, SnippetSession } from './snippetSession'; export interface ISnippetInsertOptions { overwriteBefore: number; @@ -88,6 +89,19 @@ export class SnippetController2 implements IEditorContribution { this._snippetListener.dispose(); } + apply(edits: ISnippetEdit[], opts?: Partial<ISnippetInsertOptions>) { + try { + this._doInsert(edits, typeof opts === 'undefined' ? _defaultOptions : { ..._defaultOptions, ...opts }); + + } catch (e) { + this.cancel(); + this._logService.error(e); + this._logService.error('snippet_error'); + this._logService.error('insert_edits=', edits); + this._logService.error('existing_template=', this._session ? this._session._logInfo() : '<no_session>'); + } + } + insert( template: string, opts?: Partial<ISnippetInsertOptions> @@ -108,7 +122,7 @@ export class SnippetController2 implements IEditorContribution { } private _doInsert( - template: string, + template: string | ISnippetEdit[], opts: ISnippetInsertOptions ): void { if (!this._editor.hasModel()) { @@ -123,11 +137,17 @@ export class SnippetController2 implements IEditorContribution { this._editor.getModel().pushStackElement(); } + // don't merge + if (this._session && typeof template !== 'string') { + this.cancel(); + } + if (!this._session) { this._modelVersionId = this._editor.getModel().getAlternativeVersionId(); this._session = new SnippetSession(this._editor, template, opts, this._languageConfigurationService); this._session.insert(); } else { + assertType(typeof template === 'string'); this._session.merge(template, opts); } @@ -342,50 +362,11 @@ export function performSnippetEdit(editor: ICodeEditor, snippet: string, selecti return false; } editor.focus(); - editor.setSelections(selections ?? []); - controller.insert(snippet); - return controller.isInSnippet(); -} - - -export type ISnippetEdit = { - range: Range; - snippet: string; -}; - -// --- - -export function performSnippetEdits(editor: ICodeEditor, edits: ISnippetEdit[]) { - - if (!editor.hasModel()) { - return false; - } - if (edits.length === 0) { - return false; - } - - const model = editor.getModel(); - let newText = ''; - let last: ISnippetEdit | undefined; - edits.sort((a, b) => Range.compareRangesUsingStarts(a.range, b.range)); - - for (const item of edits) { - if (last) { - const between = Range.fromPositions(last.range.getEndPosition(), item.range.getStartPosition()); - const text = model.getValueInRange(between); - newText += SnippetParser.escape(text); - } - newText += item.snippet; - last = item; - } - - const controller = SnippetController2.get(editor); - if (!controller) { - return false; - } - model.pushStackElement(); - const range = Range.plusRange(edits[0].range, edits[edits.length - 1].range); - editor.setSelection(range); - controller.insert(newText, { undoStopBefore: false }); + controller.apply(selections.map(selection => { + return { + range: Selection.liftSelection(selection), + template: snippet + }; + })); return controller.isInSnippet(); } |