@@ -156,7 +156,7 @@ function openChatPanel(context: vscode.ExtensionContext) {
156156
157157async function handleChatMessage ( message : any ) {
158158 const editor = vscode . window . activeTextEditor ;
159-
159+
160160 // Show loading state
161161 currentPanel ?. webview . postMessage ( {
162162 type : 'aiResponse' ,
@@ -169,28 +169,31 @@ async function handleChatMessage(message: any) {
169169 let context = '' ;
170170 let taskType = 'chat' ;
171171
172- // Get context from active editor if available
172+ // Smart context gathering
173173 if ( editor ) {
174174 const selection = editor . selection ;
175175 const selectedText = editor . document . getText ( selection ) ;
176-
176+ const document = editor . document ;
177+ const cursorPosition = editor . selection . active ;
178+ // Get up to 40 lines before and after cursor for context
179+ const startLine = Math . max ( 0 , cursorPosition . line - 40 ) ;
180+ const endLine = Math . min ( document . lineCount - 1 , cursorPosition . line + 40 ) ;
181+ const range = new vscode . Range ( startLine , 0 , endLine , document . lineAt ( endLine ) . text . length ) ;
182+ const surroundingContext = document . getText ( range ) ;
183+
177184 if ( selectedText ) {
178- context = selectedText ;
185+ context = selectedText + '\n\n--- Surrounding Context ---\n' + surroundingContext ;
179186 taskType = 'code' ;
180187 } else {
181- // Get surrounding context
182- const document = editor . document ;
183- const cursorPosition = editor . selection . active ;
184- const startLine = Math . max ( 0 , cursorPosition . line - 10 ) ;
185- const endLine = Math . min ( document . lineCount - 1 , cursorPosition . line + 10 ) ;
186- const range = new vscode . Range ( startLine , 0 , endLine , document . lineAt ( endLine ) . text . length ) ;
187- context = document . getText ( range ) ;
188+ context = surroundingContext ;
188189 }
189190 }
190191
191192 const response = await callAI ( message . prompt , taskType , context , message . model ) ;
193+ console . log ( 'AI response from server:' , response ) ;
192194
193195 if ( response . error ) {
196+ console . error ( 'AI error response:' , response . error ) ;
194197 currentPanel ?. webview . postMessage ( {
195198 type : 'error' ,
196199 content : response . error
@@ -200,16 +203,26 @@ async function handleChatMessage(message: any) {
200203 }
201204
202205 const isCode = response . completion_type === 'code' ;
206+ console . log ( 'Sending message to webview:' , isCode ? 'codeResponse' : 'chatResponse' , response . completion ) ;
203207
204- currentPanel ?. webview . postMessage ( {
205- type : 'aiResponse' ,
206- content : response . completion ,
207- rawContent : response . raw_completion ,
208- isCode : isCode ,
209- loading : false
210- } ) ;
208+ if ( isCode ) {
209+ currentPanel ?. webview . postMessage ( {
210+ type : 'codeResponse' ,
211+ code : response . completion ,
212+ rawContent : response . raw_completion ,
213+ explanation : 'Preview and insert, replace, or diff with selection.' ,
214+ loading : false
215+ } ) ;
216+ } else {
217+ currentPanel ?. webview . postMessage ( {
218+ type : 'chatResponse' ,
219+ content : response . completion ,
220+ loading : false
221+ } ) ;
222+ }
211223
212224 } catch ( err : any ) {
225+ console . error ( 'Exception in handleChatMessage:' , err ) ;
213226 currentPanel ?. webview . postMessage ( {
214227 type : 'error' ,
215228 content : `Error: ${ err . message } `
@@ -249,8 +262,14 @@ function insertCodeInEditor(code: string) {
249262 return ;
250263 }
251264
265+ // Smart insertion: insert code at cursor, auto-indent
252266 editor . edit ( ( editBuilder ) => {
253- editBuilder . insert ( editor . selection . active , code ) ;
267+ const position = editor . selection . active ;
268+ // Indent code to match current line
269+ const currentLine = editor . document . lineAt ( position . line ) ;
270+ const indent = currentLine . text . match ( / ^ \s * / ) ?. [ 0 ] || '' ;
271+ const indentedCode = code . split ( '\n' ) . map ( ( line , i ) => i === 0 ? line : indent + line ) . join ( '\n' ) ;
272+ editBuilder . insert ( position , indentedCode ) ;
254273 } ) ;
255274}
256275
@@ -263,7 +282,7 @@ async function applyCodeEdit(code: string) {
263282
264283 const selection = editor . selection ;
265284
266- // Show diff first
285+ // Show diff preview before applying edit
267286 const originalDocument = editor . document ;
268287 const originalText = originalDocument . getText ( selection ) ;
269288
@@ -278,7 +297,8 @@ async function applyCodeEdit(code: string) {
278297 'vscode.diff' ,
279298 originalDocument . uri ,
280299 newDoc . uri ,
281- 'Original ← → AI Suggestion'
300+ 'Original ← → AI Suggestion' ,
301+ { preview : true }
282302 ) ;
283303
284304 // Ask user to apply
0 commit comments