1- import { GhostSuggestionContext , extractPrefix } from "../types"
1+ import { AutocompleteInput } from "../types"
22import { CURSOR_MARKER } from "../ghostConstants"
3- import { isCommentLine , extractComment , cleanComment } from "./CommentHelpers"
3+ import { isCommentLine , cleanComment } from "./CommentHelpers"
44import type { TextDocument , Range } from "vscode"
55
66export function getBaseSystemInstructions ( ) : string {
@@ -55,15 +55,6 @@ export function addCursorMarker(document: TextDocument, range?: Range): string {
5555 return `${ beforeCursor } ${ CURSOR_MARKER } ${ afterCursor } `
5656}
5757
58- export function formatDocumentWithCursor ( document : TextDocument , range ?: Range , languageId ?: string ) : string {
59- const lang = languageId || document . languageId
60- const codeWithCursor = addCursorMarker ( document , range )
61-
62- return `\`\`\`${ lang }
63- ${ codeWithCursor }
64- \`\`\``
65- }
66-
6758export class AutoTriggerStrategy {
6859 shouldTreatAsComment ( prefix : string , languageId : string ) : boolean {
6960 const lines = prefix . split ( "\n" )
@@ -79,22 +70,24 @@ export class AutoTriggerStrategy {
7970 }
8071 }
8172
82- getPrompts ( context : GhostSuggestionContext ) : {
73+ getPrompts (
74+ autocompleteInput : AutocompleteInput ,
75+ prefix : string ,
76+ suffix : string ,
77+ languageId : string ,
78+ ) : {
8379 systemPrompt : string
8480 userPrompt : string
8581 } {
86- const prefix = extractPrefix ( context )
87- const languageId = context . document ?. languageId || ""
88-
8982 if ( this . shouldTreatAsComment ( prefix , languageId ) ) {
9083 return {
9184 systemPrompt : this . getCommentsSystemInstructions ( ) ,
92- userPrompt : this . getCommentsUserPrompt ( context ) ,
85+ userPrompt : this . getCommentsUserPrompt ( prefix , suffix , languageId ) ,
9386 }
9487 } else {
9588 return {
9689 systemPrompt : this . getSystemInstructions ( ) ,
97- userPrompt : this . getUserPrompt ( context ) ,
90+ userPrompt : this . getUserPrompt ( autocompleteInput , prefix , suffix , languageId ) ,
9891 }
9992 }
10093 }
@@ -112,32 +105,29 @@ Provide non-intrusive completions after a typing pause. Be conservative and help
112105 /**
113106 * Build minimal prompt for auto-trigger
114107 */
115- getUserPrompt ( context : GhostSuggestionContext ) : string {
108+ getUserPrompt ( autocompleteInput : AutocompleteInput , prefix : string , suffix : string , languageId : string ) : string {
116109 let prompt = ""
117110
118- // Start with recent typing context
119- if ( context . recentOperations && context . recentOperations . length > 0 ) {
111+ // Start with recent typing context from autocompleteInput
112+ if ( autocompleteInput . recentlyEditedRanges && autocompleteInput . recentlyEditedRanges . length > 0 ) {
120113 prompt += "## Recent Typing\n"
121- context . recentOperations . forEach ( ( op , index ) => {
122- prompt += `${ index + 1 } . ${ op . description } \n`
114+ autocompleteInput . recentlyEditedRanges . forEach ( ( range , index ) => {
115+ const description = `Edited ${ range . filepath } at line ${ range . range . start . line } `
116+ prompt += `${ index + 1 } . ${ description } \n`
123117 } )
124118 prompt += "\n"
125119 }
126120
127- // Add current position
128- if ( context . range && context . document ) {
129- const line = context . range . start . line + 1
130- const char = context . range . start . character + 1
131- prompt += `## Current Position\n`
132- prompt += `Line ${ line } , Character ${ char } \n\n`
133- }
121+ // Add current position from autocompleteInput
122+ const line = autocompleteInput . pos . line + 1
123+ const char = autocompleteInput . pos . character + 1
124+ prompt += `## Current Position\n`
125+ prompt += `Line ${ line } , Character ${ char } \n\n`
134126
135127 // Add the full document with cursor marker
136- if ( context . document ) {
137- prompt += "## Full Code\n"
138- prompt += formatDocumentWithCursor ( context . document , context . range )
139- prompt += "\n\n"
140- }
128+ const codeWithCursor = `${ prefix } ${ CURSOR_MARKER } ${ suffix } `
129+ prompt += "## Full Code\n"
130+ prompt += `\`\`\`${ languageId } \n${ codeWithCursor } \n\`\`\`\n\n`
141131
142132 // Add specific instructions
143133 prompt += "## Instructions\n"
@@ -189,23 +179,29 @@ Provide non-intrusive completions after a typing pause. Be conservative and help
189179 )
190180 }
191181
192- getCommentsUserPrompt ( context : GhostSuggestionContext ) : string {
193- if ( ! context . document || ! context . range ) {
194- return "No context available for comment-driven generation."
195- }
182+ getCommentsUserPrompt ( prefix : string , suffix : string , languageId : string ) : string {
183+ // Extract the comment from the prefix
184+ const lines = prefix . split ( "\n" )
185+ const lastLine = lines [ lines . length - 1 ]
186+ const previousLine = lines . length > 1 ? lines [ lines . length - 2 ] : ""
196187
197- const language = context . document . languageId
198- const comment = cleanComment ( extractComment ( context . document , context . range . start . line ) , language )
188+ // Determine which line contains the comment
189+ const commentLine = isCommentLine ( lastLine , languageId ) ? lastLine : previousLine
190+ const comment = cleanComment ( commentLine , languageId )
191+
192+ const codeWithCursor = `${ prefix } ${ CURSOR_MARKER } ${ suffix } `
199193
200194 let prompt = `## Comment-Driven Development
201- - Language: ${ language }
195+ - Language: ${ languageId }
202196- Comment to Implement:
203197\`\`\`
204198${ comment }
205199\`\`\`
206200
207201## Full Code
208- ${ formatDocumentWithCursor ( context . document , context . range ) }
202+ \`\`\`${ languageId }
203+ ${ codeWithCursor }
204+ \`\`\`
209205
210206## Instructions
211207Generate code that implements the functionality described in the comment.
0 commit comments