Skip to content

Commit 974c507

Browse files
authored
Merge pull request #2839 from Kilo-Org/mark/move-helper-methods
move autocompletion helper methods to specific strategies
2 parents 75acbab + 27a6960 commit 974c507

File tree

3 files changed

+91
-90
lines changed

3 files changed

+91
-90
lines changed

src/services/ghost/strategies/BasePromptStrategy.ts

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -135,36 +135,6 @@ EXAMPLE:
135135
return result
136136
}
137137

138-
/**
139-
* Gets surrounding code context (lines before and after cursor)
140-
*/
141-
protected getSurroundingCode(
142-
document: TextDocument,
143-
range: Range,
144-
linesBefore: number = 10,
145-
linesAfter: number = 10,
146-
): { before: string; after: string; currentLine: string } {
147-
const currentLineNum = range.start.line
148-
const startLine = Math.max(0, currentLineNum - linesBefore)
149-
const endLine = Math.min(document.lineCount - 1, currentLineNum + linesAfter)
150-
151-
let before = ""
152-
let after = ""
153-
const currentLine = document.lineAt(currentLineNum).text
154-
155-
// Get lines before cursor
156-
for (let i = startLine; i < currentLineNum; i++) {
157-
before += document.lineAt(i).text + "\n"
158-
}
159-
160-
// Get lines after cursor
161-
for (let i = currentLineNum + 1; i <= endLine; i++) {
162-
after += document.lineAt(i).text + "\n"
163-
}
164-
165-
return { before, after, currentLine }
166-
}
167-
168138
/**
169139
* Formats the document with cursor marker for the prompt
170140
*/
@@ -176,64 +146,4 @@ EXAMPLE:
176146
${codeWithCursor}
177147
\`\`\``
178148
}
179-
180-
/**
181-
* Gets the file path from the document
182-
*/
183-
protected getFilePath(document: TextDocument): string {
184-
return document.uri.toString()
185-
}
186-
187-
/**
188-
* Formats selected text for inclusion in prompts
189-
*/
190-
protected formatSelectedText(document: TextDocument, range: Range): string {
191-
if (range.isEmpty) return ""
192-
193-
const selectedText = document.getText(range)
194-
const startLine = range.start.line + 1
195-
const endLine = range.end.line + 1
196-
197-
return `## Selected Code (Lines ${startLine}-${endLine})
198-
\`\`\`${document.languageId}
199-
${selectedText}
200-
\`\`\``
201-
}
202-
203-
/**
204-
* Formats recent operations for inclusion in prompts
205-
*/
206-
protected formatRecentOperations(operations: any[]): string {
207-
if (!operations || operations.length === 0) return ""
208-
209-
let result = "## Recent Actions\n"
210-
operations.slice(0, 5).forEach((op, index) => {
211-
result += `${index + 1}. ${op.description}\n`
212-
if (op.content) {
213-
result += ` \`\`\`\n ${op.content}\n \`\`\`\n`
214-
}
215-
})
216-
217-
return result
218-
}
219-
220-
/**
221-
* Helper to check if a line appears to be incomplete
222-
*/
223-
protected isIncompleteStatement(line: string): boolean {
224-
const trimmed = line.trim()
225-
226-
// Check for common incomplete patterns
227-
const incompletePatterns = [
228-
/^(if|else if|while|for|switch|try|catch)\s*\(.*\)\s*$/, // Control structures without body
229-
/^(function|class|interface|type|enum)\s+\w+.*[^{]$/, // Declarations without body
230-
/[,\+\-\*\/\=\|\&]\s*$/, // Operators at end
231-
/^(const|let|var)\s+\w+\s*=\s*$/, // Variable declaration without value
232-
/\.\s*$/, // Property access incomplete
233-
/\(\s*$/, // Opening parenthesis
234-
/^\s*\.\w*$/, // Method chaining incomplete
235-
]
236-
237-
return incompletePatterns.some((pattern) => pattern.test(trimmed))
238-
}
239149
}

src/services/ghost/strategies/NewLineCompletionStrategy.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,71 @@ Important:
270270
}
271271
return false
272272
}
273+
274+
/**
275+
* Formats recent operations for inclusion in prompts
276+
*/
277+
private formatRecentOperations(operations: any[]): string {
278+
if (!operations || operations.length === 0) return ""
279+
280+
let result = "## Recent Actions\n"
281+
operations.slice(0, 5).forEach((op, index) => {
282+
result += `${index + 1}. ${op.description}\n`
283+
if (op.content) {
284+
result += ` \`\`\`\n ${op.content}\n \`\`\`\n`
285+
}
286+
})
287+
288+
return result
289+
}
290+
291+
/**
292+
* Helper to check if a line appears to be incomplete
293+
*/
294+
private isIncompleteStatement(line: string): boolean {
295+
const trimmed = line.trim()
296+
297+
// Check for common incomplete patterns
298+
const incompletePatterns = [
299+
/^(if|else if|while|for|switch|try|catch)\s*\(.*\)\s*$/, // Control structures without body
300+
/^(function|class|interface|type|enum)\s+\w+.*[^{]$/, // Declarations without body
301+
/[,\+\-\*\/\=\|\&]\s*$/, // Operators at end
302+
/^(const|let|var)\s+\w+\s*=\s*$/, // Variable declaration without value
303+
/\.\s*$/, // Property access incomplete
304+
/\(\s*$/, // Opening parenthesis
305+
/^\s*\.\w*$/, // Method chaining incomplete
306+
]
307+
308+
return incompletePatterns.some((pattern) => pattern.test(trimmed))
309+
}
310+
311+
/**
312+
* Gets surrounding code context (lines before and after cursor)
313+
*/
314+
private getSurroundingCode(
315+
document: TextDocument,
316+
range: Range,
317+
linesBefore: number = 10,
318+
linesAfter: number = 10,
319+
): { before: string; after: string; currentLine: string } {
320+
const currentLineNum = range.start.line
321+
const startLine = Math.max(0, currentLineNum - linesBefore)
322+
const endLine = Math.min(document.lineCount - 1, currentLineNum + linesAfter)
323+
324+
let before = ""
325+
let after = ""
326+
const currentLine = document.lineAt(currentLineNum).text
327+
328+
// Get lines before cursor
329+
for (let i = startLine; i < currentLineNum; i++) {
330+
before += document.lineAt(i).text + "\n"
331+
}
332+
333+
// Get lines after cursor
334+
for (let i = currentLineNum + 1; i <= endLine; i++) {
335+
after += document.lineAt(i).text + "\n"
336+
}
337+
338+
return { before, after, currentLine }
339+
}
273340
}

src/services/ghost/strategies/UserRequestStrategy.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Range, TextDocument } from "vscode"
12
import { GhostSuggestionContext } from "../types"
23
import { UseCaseType } from "../types/PromptStrategy"
34
import { BasePromptStrategy } from "./BasePromptStrategy"
@@ -154,4 +155,27 @@ Common Request Patterns:
154155
// Use the standard prompt building
155156
return super.getUserPrompt(context)
156157
}
158+
159+
/**
160+
* Gets the file path from the document
161+
*/
162+
private getFilePath(document: TextDocument): string {
163+
return document.uri.toString()
164+
}
165+
166+
/**
167+
* Formats selected text for inclusion in prompts
168+
*/
169+
private formatSelectedText(document: TextDocument, range: Range): string {
170+
if (range.isEmpty) return ""
171+
172+
const selectedText = document.getText(range)
173+
const startLine = range.start.line + 1
174+
const endLine = range.end.line + 1
175+
176+
return `## Selected Code (Lines ${startLine}-${endLine})
177+
\`\`\`${document.languageId}
178+
${selectedText}
179+
\`\`\``
180+
}
157181
}

0 commit comments

Comments
 (0)