Skip to content

Commit

Permalink
Support TextEdit in completion responses (#4073)
Browse files Browse the repository at this point in the history
This is the vscode side of OmniSharp/omnisharp-roslyn#1941. This is written in such a fashion as to be mergeable now, as it will fall back to InsertText if TextEdit is not present in the completion response. After some appropriate period of time, we can remove the InsertText component.
  • Loading branch information
333fred authored Sep 18, 2020
1 parent 797eecc commit 7120374
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/features/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,26 @@ export default class OmnisharpCompletionProvider extends AbstractProvider implem
}

private _convertToVscodeCompletionItem(omnisharpCompletion: protocol.OmnisharpCompletionItem): CompletionItem {
let docs: MarkdownString | undefined = omnisharpCompletion.Documentation ? new MarkdownString(omnisharpCompletion.Documentation, false) : undefined;
const docs: MarkdownString | undefined = omnisharpCompletion.Documentation ? new MarkdownString(omnisharpCompletion.Documentation, false) : undefined;

const mapTextEdit = function (edit: protocol.LinePositionSpanTextChange): TextEdit {
const mapRange = function (edit: protocol.LinePositionSpanTextChange): Range {
const newStart = new Position(edit.StartLine - 1, edit.StartColumn - 1);
const newEnd = new Position(edit.EndLine - 1, edit.EndColumn - 1);
const newRange = new Range(newStart, newEnd);
return new TextEdit(newRange, edit.NewText);
return new Range(newStart, newEnd);
};

const mapTextEdit = function (edit: protocol.LinePositionSpanTextChange): TextEdit {
return new TextEdit(mapRange(edit), edit.NewText);
};

const additionalTextEdits = omnisharpCompletion.AdditionalTextEdits?.map(mapTextEdit);

const newText = omnisharpCompletion.TextEdit?.NewText ?? omnisharpCompletion.InsertText;
const insertText = omnisharpCompletion.InsertTextFormat === InsertTextFormat.Snippet
? new SnippetString(omnisharpCompletion.InsertText)
: omnisharpCompletion.InsertText;
? new SnippetString(newText)
: newText;

const insertRange = omnisharpCompletion.TextEdit ? mapRange(omnisharpCompletion.TextEdit) : undefined;

return {
label: omnisharpCompletion.Label,
Expand All @@ -84,6 +90,7 @@ export default class OmnisharpCompletionProvider extends AbstractProvider implem
preselect: omnisharpCompletion.Preselect,
filterText: omnisharpCompletion.FilterText,
insertText: insertText,
range: insertRange,
tags: omnisharpCompletion.Tags,
sortText: omnisharpCompletion.SortText,
additionalTextEdits: additionalTextEdits,
Expand Down
1 change: 1 addition & 0 deletions src/omnisharp/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ export interface OmnisharpCompletionItem {
FilterText?: string;
InsertText?: string;
InsertTextFormat?: InsertTextFormat;
TextEdit?: LinePositionSpanTextChange;
CommitCharacters?: string[];
AdditionalTextEdits?: LinePositionSpanTextChange[];
Data: any;
Expand Down

0 comments on commit 7120374

Please sign in to comment.