Skip to content

Commit de06d78

Browse files
authored
Suggestion from the beginning of the line (#48)
If between the cursor and the start of the line there are only spaces and tabs - generate suggestion as if the cursor was at the start of the line.
1 parent e439630 commit de06d78

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/completion.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ export class Completion {
5353
this.app.logger.addEventLog(group, "TOO_LONG_SUFFIX_RETURN", "")
5454
return null
5555
}
56-
const prompt = linePrefix;
56+
let prompt = linePrefix;
57+
let spacesToRemove = 0;
58+
if (this.isOnlySpacesOrTabs(prompt)) {
59+
prompt = "";
60+
spacesToRemove = linePrefix.length //in case of tabs probably less spaces will be removed, but better to keep it simple
61+
}
5762
const inputPrefix = prefixLines.join('\n') + '\n';
5863
const inputSuffix = lineSuffix + '\n' + suffixLines.join('\n') + '\n';
5964

@@ -112,7 +117,7 @@ export class Completion {
112117
}, 0);
113118
this.isRequestInProgress = false
114119
this.app.logger.addEventLog(group, "NORMAL_RETURN", suggestionLines[0])
115-
return [this.getCompletion(completion, position)];
120+
return [this.getCompletion(this.removeLeadingSpaces(completion, spacesToRemove), position)];
116121
} catch (err) {
117122
console.error("Error fetching llama completion:", err);
118123
vscode.window.showInformationMessage(this.app.extConfig.getUiText(`Error getting response. Please check if llama.cpp server is running.`)??"");
@@ -127,6 +132,21 @@ export class Completion {
127132
}
128133
}
129134

135+
private isOnlySpacesOrTabs = (str: string): boolean => {
136+
// Regular expression to match only spaces and tabs
137+
return /^[ \t]*$/.test(str);
138+
}
139+
140+
private removeLeadingSpaces = (str: string, n: number): string => {
141+
let i = 0;
142+
// Count up to 'n' leading spaces
143+
while (i < str.length && i < n && str[i] === ' ') {
144+
i++;
145+
}
146+
147+
return str.slice(i);
148+
}
149+
130150
private getCachedCompletion = (hashKey: string, inputPrefix: string, inputSuffix: string, prompt: string) => {
131151
let result = this.app.lruResultCache.get(hashKey);
132152
if (result != undefined) return result

0 commit comments

Comments
 (0)