@@ -171,30 +171,47 @@ const resolveLocalPath = (uri?: vscode.Uri): string | null => {
171171 return null ;
172172 }
173173
174- const normalized = path . normalize ( fsPath ) ;
175- return fs . existsSync ( normalized ) ? normalized : null ;
174+ return path . normalize ( fsPath ) ;
176175} ;
177176
178177export const analyzeScreenshot = async (
179178 context : vscode . ExtensionContext ,
180179 fileUri ?: vscode . Uri ,
181180) : Promise < void > => {
182181 try {
183- let screenshotPath : string | null = resolveLocalPath ( fileUri ) ;
182+ const INVALID_SELECTION_MESSAGE =
183+ 'Invalid file selection. Please select a local image file.' ;
184184
185- if ( ! screenshotPath ) {
186- screenshotPath = resolveLocalPath ( getActiveImageEditor ( ) ) ;
187- }
188-
189- if ( ! screenshotPath ) {
190- screenshotPath = resolveLocalPath ( await pickScreenshot ( ) ) ;
191- }
185+ let screenshotPath : string | null = null ;
192186
193- if ( ! screenshotPath ) {
194- vscode . window . showErrorMessage (
195- 'Invalid or unreadable screenshot. Please select a local image file.' ,
196- ) ;
197- return ;
187+ // If invoked from context menu with a URI, never fall back to the file picker.
188+ if ( fileUri ) {
189+ const candidate = resolveLocalPath ( fileUri ) ;
190+ if ( ! candidate || ! fs . existsSync ( candidate ) ) {
191+ vscode . window . showErrorMessage ( INVALID_SELECTION_MESSAGE ) ;
192+ return ;
193+ }
194+ screenshotPath = candidate ;
195+ } else {
196+ // If an image is open in the active editor, use it (and do not show the picker).
197+ const activeUri = getActiveImageEditor ( ) ;
198+ if ( activeUri ) {
199+ const candidate = resolveLocalPath ( activeUri ) ;
200+ if ( ! candidate || ! fs . existsSync ( candidate ) ) {
201+ vscode . window . showErrorMessage ( INVALID_SELECTION_MESSAGE ) ;
202+ return ;
203+ }
204+ screenshotPath = candidate ;
205+ } else {
206+ // Otherwise, prompt the user to pick a file.
207+ const pickedUri = await pickScreenshot ( ) ;
208+ const candidate = resolveLocalPath ( pickedUri ) ;
209+ if ( ! candidate || ! fs . existsSync ( candidate ) ) {
210+ vscode . window . showErrorMessage ( INVALID_SELECTION_MESSAGE ) ;
211+ return ;
212+ }
213+ screenshotPath = candidate ;
214+ }
198215 }
199216
200217 const metadata = await resolveMetadata ( ) ;
0 commit comments