Skip to content

Commit

Permalink
Snippet text edits
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Solano committed Dec 15, 2023
1 parent 692e892 commit 1b2004f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
2 changes: 2 additions & 0 deletions client/src/common/protocolConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,8 @@ export function createConverter(uriConverter: URIConverter | undefined, trustMar
for (const edit of change.edits) {
if (ls.AnnotatedTextEdit.is(edit)) {
result.replace(uri, asRange(edit.range), edit.newText, asMetadata(edit.annotationId));
} else if (ls.SnippetTextEdit.is(edit)) {
result.replace(uri, asRange(edit.range), edit.snippet.value, asMetadata(edit.annotationId));
} else {
result.replace(uri, asRange(edit.range), edit.newText);
}
Expand Down
9 changes: 8 additions & 1 deletion protocol/src/common/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4094,6 +4094,13 @@ export interface WorkspaceEditClientCapabilities {
* @since 3.16.0
*/
changeAnnotationSupport?: ChangeAnnotationsSupportOptions;

/**
* Whether the client supports snippets as text edits.
*
* @since 3.18.0
*/
snippetEditSupport?: boolean;
}

/**
Expand Down Expand Up @@ -4207,7 +4214,7 @@ export {
DidChangeNotebookDocumentNotification, DidSaveNotebookDocumentParams, DidSaveNotebookDocumentNotification, DidCloseNotebookDocumentParams,
DidCloseNotebookDocumentNotification,
// Inline Completions
InlineCompletionClientCapabilities, InlineCompletionOptions, InlineCompletionParams, InlineCompletionRegistrationOptions, InlineCompletionRequest
InlineCompletionClientCapabilities, InlineCompletionOptions, InlineCompletionParams, InlineCompletionRegistrationOptions, InlineCompletionRequest,
};

// To be backwards compatible
Expand Down
65 changes: 57 additions & 8 deletions types/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,11 @@ export interface TextDocumentEdit {
*
* @since 3.16.0 - support for AnnotatedTextEdit. This is guarded using a
* client capability.
*
* @since 3.18.0 - support for SnippetTextEdit. This is guarded using a
* client capability.
*/
edits: (TextEdit | AnnotatedTextEdit)[];
edits: (TextEdit | AnnotatedTextEdit | SnippetTextEdit)[];
}

/**
Expand All @@ -1038,7 +1041,7 @@ export namespace TextDocumentEdit {
/**
* Creates a new `TextDocumentEdit`
*/
export function create(textDocument: OptionalVersionedTextDocumentIdentifier, edits: (TextEdit | AnnotatedTextEdit)[]): TextDocumentEdit {
export function create(textDocument: OptionalVersionedTextDocumentIdentifier, edits: (TextEdit | AnnotatedTextEdit | SnippetTextEdit)[]): TextDocumentEdit {
return { textDocument, edits };
}

Expand Down Expand Up @@ -1330,8 +1333,11 @@ export interface TextEditChange {
*
* @since 3.16.0 - support for annotated text edits. This is usually
* guarded using a client capability.
*
* @since 3.18.0 - support for snippet text edits. This is usually
* guarded using a client capability.
*/
all(): (TextEdit | AnnotatedTextEdit)[];
all(): (TextEdit | AnnotatedTextEdit | SnippetTextEdit)[];

/**
* Clears the edits for this change.
Expand All @@ -1345,8 +1351,11 @@ export interface TextEditChange {
*
* @since 3.16.0 - support for annotated text edits. This is usually
* guarded using a client capability.
*
* @since 3.18.0 - support for snippet text edits. This is usually
* guarded using a client capability.
*/
add(edit: TextEdit | AnnotatedTextEdit): void;
add(edit: TextEdit | AnnotatedTextEdit | SnippetTextEdit): void;

/**
* Insert the given text at the given position.
Expand Down Expand Up @@ -1380,10 +1389,10 @@ export interface TextEditChange {

class TextEditChangeImpl implements TextEditChange {

private edits: (TextEdit | AnnotatedTextEdit)[];
private edits: (TextEdit | AnnotatedTextEdit | SnippetTextEdit)[];
private changeAnnotations: ChangeAnnotations | undefined;

public constructor(edits: (TextEdit | AnnotatedTextEdit)[], changeAnnotations?: ChangeAnnotations) {
public constructor(edits: (TextEdit | AnnotatedTextEdit | SnippetTextEdit)[], changeAnnotations?: ChangeAnnotations) {
this.edits = edits;
this.changeAnnotations = changeAnnotations;
}
Expand Down Expand Up @@ -1451,11 +1460,11 @@ class TextEditChangeImpl implements TextEditChange {
}
}

public add(edit: TextEdit | AnnotatedTextEdit): void {
public add(edit: TextEdit | AnnotatedTextEdit | SnippetTextEdit): void {
this.edits.push(edit);
}

public all(): (TextEdit | AnnotatedTextEdit)[] {
public all(): (TextEdit | AnnotatedTextEdit | SnippetTextEdit)[] {
return this.edits;
}

Expand All @@ -1470,6 +1479,39 @@ class TextEditChangeImpl implements TextEditChange {
}
}

/**
* An interactive text edit.
*
* @since 3.18.0
* @proposed
*/
export interface SnippetTextEdit {
/**
* The range of the text document to be manipulated.
*/
range: Range;

/**
* The snippet to be inserted.
*/
snippet: StringValue;

/**
* The actual identifier of the snippet edit.
*/
annotationId: ChangeAnnotationIdentifier;
}

export namespace SnippetTextEdit {
export function is(value: any): value is SnippetTextEdit {
const candidate = value as SnippetTextEdit;
return Is.objectLiteral(candidate)
&& Range.is(candidate.range)
&& StringValue.is(candidate.snippet)
&& (ChangeAnnotation.is(candidate.annotationId) || ChangeAnnotationIdentifier.is(candidate.annotationId));
}
}

/**
* A helper class
*/
Expand Down Expand Up @@ -4289,6 +4331,13 @@ export namespace StringValue {
export function createSnippet(value: string): StringValue {
return { kind: 'snippet', value };
}

export function is(value: any): value is StringValue {
const candidate = value as StringValue;
return Is.objectLiteral(candidate)
&& candidate.kind === 'snippet'
&& Is.string(candidate.value);
}
}

/**
Expand Down

0 comments on commit 1b2004f

Please sign in to comment.