Adds leftTrim function, fixes bug in inlineCompletionToGhostText.

This commit is contained in:
Henning Dieterichs 2021-05-31 18:54:03 +02:00
parent 3a259a7a6a
commit 79048992ef
No known key found for this signature in database
GPG key ID: 771381EFFDB9EC06

View file

@ -428,23 +428,35 @@ export interface NormalizedInlineCompletion extends InlineCompletion {
range: Range; range: Range;
} }
function leftTrim(str: string): string {
return str.replace(/^\s+/, '');
}
export function inlineCompletionToGhostText(inlineCompletion: NormalizedInlineCompletion, textModel: ITextModel): GhostText | undefined { export function inlineCompletionToGhostText(inlineCompletion: NormalizedInlineCompletion, textModel: ITextModel): GhostText | undefined {
// This is a single line string // This is a single line string
const valueToBeReplaced = textModel.getValueInRange(inlineCompletion.range); const valueToBeReplaced = textModel.getValueInRange(inlineCompletion.range);
// valueToBeReplaced === ws1 + valueToBeReplacedTrimmed
const valueToBeReplacedTrimmed = valueToBeReplaced.trimLeft(); let remainingInsertText: string;
// inlineCompletion.text === ws2 + insertTextTrimmed
const insertTextTrimmed = inlineCompletion.text.trimLeft(); // Consider these cases
if (!insertTextTrimmed.startsWith(valueToBeReplacedTrimmed)) { // valueToBeReplaced -> inlineCompletion.text
// if ws1 === ws2, then inlineCompletion.text does not start with valueToBeReplaced // "\t\tfoo" -> "\t\tfoobar" (+"bar")
return undefined; // "\t" -> "\t\tfoobar" (+"\tfoobar")
// "\t\tfoo" -> "\t\t\tfoobar" (+"\t", +"bar")
// "\t\tfoo" -> "\tfoobar" (-"\t", +"\bar")
if (inlineCompletion.text.startsWith(valueToBeReplaced)) {
remainingInsertText = inlineCompletion.text.substr(valueToBeReplaced.length);
} else {
const valueToBeReplacedTrimmed = leftTrim(valueToBeReplaced);
const insertTextTrimmed = leftTrim(inlineCompletion.text);
if (!insertTextTrimmed.startsWith(valueToBeReplacedTrimmed)) {
return undefined;
}
remainingInsertText = insertTextTrimmed.substr(valueToBeReplacedTrimmed.length);
} }
const position = inlineCompletion.range.getEndPosition(); const position = inlineCompletion.range.getEndPosition();
const remainingInsertText = insertTextTrimmed.substr(valueToBeReplacedTrimmed.length);
// if ws1 === ws2, then
// remainingInsertText === (ws2 + insertTextTrimmed).substr(ws1 + valueToBeReplacedTrimmed.length);
// === inlineCompletion.text.substr(valueToBeReplaced.length)
const lines = strings.splitLines(remainingInsertText); const lines = strings.splitLines(remainingInsertText);