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

[vscode] Support keepWhitespace in SnippetTextEdit and insertSnippet #15176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,10 @@ export interface ApplyEditsOptions extends UndoStopOptions {
setEndOfLine: EndOfLine | undefined;
}

export interface SnippetEditOptions extends UndoStopOptions {
keepWhitespace?: boolean;
}

export interface ThemeColor {
id: string;
}
Expand Down Expand Up @@ -1345,7 +1349,7 @@ export interface TextEditorsMain {
$trySetSelections(id: string, selections: Selection[]): Promise<void>;
$tryApplyEdits(id: string, modelVersionId: number, edits: SingleEditOperation[], opts: ApplyEditsOptions): Promise<boolean>;
$tryApplyWorkspaceEdit(workspaceEditDto: WorkspaceEditDto, metadata?: WorkspaceEditMetadataDto): Promise<boolean>;
$tryInsertSnippet(id: string, template: string, selections: Range[], opts: UndoStopOptions): Promise<boolean>;
$tryInsertSnippet(id: string, template: string, selections: Range[], opts: SnippetEditOptions): Promise<boolean>;
$save(uri: UriComponents): PromiseLike<UriComponents | undefined>;
$saveAs(uri: UriComponents): PromiseLike<UriComponents | undefined>;
$saveAll(includeUntitled?: boolean): Promise<boolean>;
Expand Down Expand Up @@ -1545,7 +1549,7 @@ export interface WorkspaceFileEditDto {
export interface WorkspaceTextEditDto {
resource: UriComponents;
modelVersionId?: number;
textEdit: TextEdit & { insertAsSnippet?: boolean };
textEdit: TextEdit & { insertAsSnippet?: boolean, keepWhitespace?: boolean };
metadata?: WorkspaceEditEntryMetadataDto;
}
export namespace WorkspaceTextEditDto {
Expand Down
24 changes: 18 additions & 6 deletions packages/plugin-ext/src/main/browser/text-editor-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
TextEditorRevealType,
SingleEditOperation,
ApplyEditsOptions,
UndoStopOptions,
SnippetEditOptions,
DecorationOptions
} from '../../common/plugin-api-rpc';
import { Range } from '../../common/plugin-api-rpc-model';
Expand Down Expand Up @@ -281,7 +281,7 @@ export class TextEditorMain implements Disposable {
return true;
}

insertSnippet(template: string, ranges: Range[], opts: UndoStopOptions): boolean {
insertSnippet(template: string, ranges: Range[], opts: SnippetEditOptions): boolean {
const snippetController: SnippetController2 | null | undefined = this.editor?.getControl().getContribution('snippetController2');

if (!snippetController || !this.editor) { return false; }
Expand All @@ -290,7 +290,13 @@ export class TextEditorMain implements Disposable {
this.editor.getControl().setSelections(selections);
this.editor.focus();

snippetController.insert(template, 0, 0, opts.undoStopBefore, opts.undoStopAfter);
snippetController.insert(template, {
undoStopBefore: opts.undoStopBefore,
undoStopAfter: opts.undoStopAfter,
adjustWhitespace: !opts.keepWhitespace,
overwriteBefore: 0,
overwriteAfter: 0
});

return true;
}
Expand Down Expand Up @@ -324,11 +330,17 @@ export class TextEditorMain implements Disposable {
}
}

interface SnippetInsertOptions {
overwriteBefore: number,
overwriteAfter: number,
undoStopBefore: boolean,
undoStopAfter: boolean,
adjustWhitespace: boolean
}

// TODO move to monaco typings!
interface SnippetController2 extends monaco.editor.IEditorContribution {
insert(template: string,
overwriteBefore: number, overwriteAfter: number,
undoStopBefore: boolean, undoStopAfter: boolean): void;
insert(template: string, options?: Partial<SnippetInsertOptions>): void;
finish(): void;
cancel(): void;
dispose(): void;
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-ext/src/main/browser/text-editors-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import {
TextEditorRevealType,
SingleEditOperation,
ApplyEditsOptions,
UndoStopOptions,
DecorationRenderOptions,
ThemeDecorationInstanceRenderOptions,
DecorationOptions,
WorkspaceEditDto,
WorkspaceNotebookCellEditDto,
DocumentsMain,
WorkspaceEditMetadataDto,
SnippetEditOptions,
} from '../../common/plugin-api-rpc';
import { Range, TextDocumentShowOptions } from '../../common/plugin-api-rpc-model';
import { EditorsAndDocumentsMain } from './editors-and-documents-main';
Expand Down Expand Up @@ -157,7 +157,7 @@ export class TextEditorsMainImpl implements TextEditorsMain, Disposable {
}
}

$tryInsertSnippet(id: string, template: string, ranges: Range[], opts: UndoStopOptions): Promise<boolean> {
$tryInsertSnippet(id: string, template: string, ranges: Range[], opts: SnippetEditOptions): Promise<boolean> {
if (!this.editorsAndDocuments.getEditor(id)) {
return Promise.reject(disposed(`TextEditor(${id})`));
}
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,12 @@ export function fromTextEdit(edit: theia.TextEdit): model.TextEdit {
};
}

function fromSnippetTextEdit(edit: theia.SnippetTextEdit): model.TextEdit & { insertAsSnippet?: boolean } {
function fromSnippetTextEdit(edit: theia.SnippetTextEdit): model.TextEdit & { insertAsSnippet?: boolean, keepWhitespace?: boolean } {
return {
text: edit.snippet.value,
range: fromRange(edit.range),
insertAsSnippet: true
insertAsSnippet: true,
keepWhitespace: edit.keepWhitespace
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/plugin/types-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,7 @@ export class NotebookRange implements theia.NotebookRange {
export class SnippetTextEdit implements theia.SnippetTextEdit {
range: Range;
snippet: SnippetString;
keepWhitespace?: boolean;

static isSnippetTextEdit(thing: unknown): thing is SnippetTextEdit {
return thing instanceof SnippetTextEdit || isObject<SnippetTextEdit>(thing)
Expand Down
20 changes: 19 additions & 1 deletion packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,20 @@ export module '@theia/plugin' {
* @return A promise that resolves with a value indicating if the snippet could be inserted. Note that the promise does not signal
* that the snippet is completely filled-in or accepted.
*/
insertSnippet(snippet: SnippetString, location?: Position | Range | Position[] | Range[], options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;
insertSnippet(snippet: SnippetString, location?: Position | Range | Position[] | Range[], options?: {
/**
* Add undo stop before making the edits.
*/
readonly undoStopBefore: boolean;
/**
* Add undo stop after making the edits.
*/
readonly undoStopAfter: boolean;
/**
* Keep whitespace of the {@link SnippetString.value} as is.
*/
readonly keepWhitespace?: boolean;
}): Thenable<boolean>;

/**
* Adds a set of decorations to the text editor. If a set of decorations already exists with
Expand Down Expand Up @@ -16299,6 +16312,11 @@ export module '@theia/plugin' {
*/
snippet: SnippetString;

/**
* Whether the snippet edit should be applied with existing whitespace preserved.
*/
keepWhitespace?: boolean;

/**
* Create a new snippet edit.
*
Expand Down
Loading