Skip to content

Commit 0ca7661

Browse files
Merge pull request #3897 from Kilo-Org/eamon/simplify
Simplify HelperVars
2 parents 5af20cd + c5f7bcf commit 0ca7661

File tree

1 file changed

+60
-116
lines changed

1 file changed

+60
-116
lines changed

src/services/continuedev/core/autocomplete/util/HelperVars.ts

Lines changed: 60 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -10,138 +10,82 @@ import { AutocompleteInput } from "./types"
1010
* A collection of variables that are often accessed throughout the autocomplete pipeline
1111
* It's noisy to re-calculate all the time or inject them into each function
1212
*/
13-
export class HelperVars {
13+
export interface HelperVars {
1414
lang: AutocompleteLanguageInfo
1515
treePath: AstPath | undefined
16-
workspaceUris: string[] = []
17-
18-
private _fileContents: string | undefined
19-
private _fileLines: string[] | undefined
20-
private _fullPrefix: string | undefined
21-
private _fullSuffix: string | undefined
22-
private _prunedPrefix: string | undefined
23-
private _prunedSuffix: string | undefined
24-
25-
private constructor(
26-
public readonly input: AutocompleteInput,
27-
public readonly options: TabAutocompleteOptions,
28-
public readonly modelName: string,
29-
private readonly ide: IDE,
30-
) {
31-
this.lang = languageForFilepath(input.filepath)
32-
}
33-
34-
private async init() {
35-
// Don't do anything if already initialized
36-
if (this._fileContents !== undefined) {
37-
return
38-
}
39-
this.workspaceUris = await this.ide.getWorkspaceDirs()
40-
41-
this._fileContents = this.input.manuallyPassFileContents ?? (await this.ide.readFile(this.filepath))
42-
43-
this._fileLines = this._fileContents.split("\n")
44-
45-
// Construct full prefix/suffix (a few edge cases handled in here)
46-
const { prefix: fullPrefix, suffix: fullSuffix } = await constructInitialPrefixSuffix(this.input, this.ide)
47-
this._fullPrefix = fullPrefix
48-
this._fullSuffix = fullSuffix
49-
50-
const { prunedPrefix, prunedSuffix } = this.prunePrefixSuffix()
51-
this._prunedPrefix = prunedPrefix
52-
this._prunedSuffix = prunedSuffix
53-
54-
try {
55-
const ast = await getAst(this.filepath, fullPrefix + fullSuffix)
56-
if (ast) {
57-
this.treePath = await getTreePathAtCursor(ast, fullPrefix.length)
58-
}
59-
} catch (e) {
60-
console.error("Failed to parse AST", e)
61-
}
62-
}
16+
workspaceUris: string[]
17+
fileContents: string
18+
fileLines: string[]
19+
fullPrefix: string
20+
fullSuffix: string
21+
prunedPrefix: string
22+
prunedSuffix: string
23+
input: AutocompleteInput
24+
options: TabAutocompleteOptions
25+
modelName: string
26+
filepath: string
27+
pos: any // Using any to match original getter return type inference, or import Position
28+
prunedCaretWindow: string
29+
}
6330

64-
static async create(
31+
export const HelperVars = {
32+
create: async (
6533
input: AutocompleteInput,
6634
options: TabAutocompleteOptions,
6735
modelName: string,
6836
ide: IDE,
69-
): Promise<HelperVars> {
70-
const instance = new HelperVars(input, options, modelName, ide)
71-
await instance.init()
72-
return instance
73-
}
37+
): Promise<HelperVars> => {
38+
const lang = languageForFilepath(input.filepath)
39+
const workspaceUris = await ide.getWorkspaceDirs()
40+
const fileContents = input.manuallyPassFileContents ?? (await ide.readFile(input.filepath))
41+
const fileLines = fileContents.split("\n")
42+
43+
// Construct full prefix/suffix (a few edge cases handled in here)
44+
const { prefix: fullPrefix, suffix: fullSuffix } = await constructInitialPrefixSuffix(input, ide)
7445

75-
prunePrefixSuffix() {
7646
// Construct basic prefix
77-
const maxPrefixTokens = this.options.maxPromptTokens * this.options.prefixPercentage
78-
const prunedPrefix = pruneLinesFromTop(this.fullPrefix, maxPrefixTokens, this.modelName)
47+
const maxPrefixTokens = options.maxPromptTokens * options.prefixPercentage
48+
const prunedPrefix = pruneLinesFromTop(fullPrefix, maxPrefixTokens, modelName)
7949

8050
// Construct suffix
8151
const maxSuffixTokens = Math.min(
82-
this.options.maxPromptTokens - countTokens(prunedPrefix, this.modelName),
83-
this.options.maxSuffixPercentage * this.options.maxPromptTokens,
52+
options.maxPromptTokens - countTokens(prunedPrefix, modelName),
53+
options.maxSuffixPercentage * options.maxPromptTokens,
8454
)
85-
const prunedSuffix = pruneLinesFromBottom(this.fullSuffix, maxSuffixTokens, this.modelName)
55+
const prunedSuffix = pruneLinesFromBottom(fullSuffix, maxSuffixTokens, modelName)
56+
57+
let treePath: AstPath | undefined
58+
try {
59+
const ast = await getAst(input.filepath, fullPrefix + fullSuffix)
60+
if (ast) {
61+
treePath = await getTreePathAtCursor(ast, fullPrefix.length)
62+
}
63+
} catch (e) {
64+
console.error("Failed to parse AST", e)
65+
}
8666

8767
return {
68+
lang,
69+
treePath,
70+
workspaceUris,
71+
fileContents,
72+
fileLines,
73+
fullPrefix,
74+
fullSuffix,
8875
prunedPrefix,
8976
prunedSuffix,
77+
input,
78+
options,
79+
modelName,
80+
get filepath() {
81+
return input.filepath
82+
},
83+
get pos() {
84+
return input.pos
85+
},
86+
get prunedCaretWindow() {
87+
return prunedPrefix + prunedSuffix
88+
},
9089
}
91-
}
92-
93-
// Fast access
94-
get filepath() {
95-
return this.input.filepath
96-
}
97-
get pos() {
98-
return this.input.pos
99-
}
100-
101-
get prunedCaretWindow() {
102-
return this.prunedPrefix + this.prunedSuffix
103-
}
104-
105-
// Getters for lazy access
106-
get fileContents(): string {
107-
if (this._fileContents === undefined) {
108-
throw new Error("HelperVars must be initialized before accessing fileContents")
109-
}
110-
return this._fileContents
111-
}
112-
113-
get fileLines(): string[] {
114-
if (this._fileLines === undefined) {
115-
throw new Error("HelperVars must be initialized before accessing fileLines")
116-
}
117-
return this._fileLines
118-
}
119-
120-
get fullPrefix(): string {
121-
if (this._fullPrefix === undefined) {
122-
throw new Error("HelperVars must be initialized before accessing fullPrefix")
123-
}
124-
return this._fullPrefix
125-
}
126-
127-
get fullSuffix(): string {
128-
if (this._fullSuffix === undefined) {
129-
throw new Error("HelperVars must be initialized before accessing fullSuffix")
130-
}
131-
return this._fullSuffix
132-
}
133-
134-
get prunedPrefix(): string {
135-
if (this._prunedPrefix === undefined) {
136-
throw new Error("HelperVars must be initialized before accessing prunedPrefix")
137-
}
138-
return this._prunedPrefix
139-
}
140-
141-
get prunedSuffix(): string {
142-
if (this._prunedSuffix === undefined) {
143-
throw new Error("HelperVars must be initialized before accessing prunedSuffix")
144-
}
145-
return this._prunedSuffix
146-
}
90+
},
14791
}

0 commit comments

Comments
 (0)