11import { open } from '@theia/core/lib/browser/opener-service' ;
2+ import { ApplicationShell } from '@theia/core/lib/browser/shell/application-shell' ;
23import { MaybeArray , nls , SelectionService } from '@theia/core/lib/common' ;
34import {
45 CommandRegistry ,
@@ -11,7 +12,7 @@ import {
1112 UriCommandHandler ,
1213} from '@theia/core/lib/common/uri-command-handler' ;
1314import { inject , injectable } from '@theia/core/shared/inversify' ;
14- import { EditorManager } from '@theia/editor/lib/browser/editor-manager ' ;
15+ import { EditorWidget } from '@theia/editor/lib/browser/editor-widget ' ;
1516import { FileStat } from '@theia/filesystem/lib/common/files' ;
1617import {
1718 WorkspaceCommandContribution as TheiaWorkspaceCommandContribution ,
@@ -38,8 +39,8 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
3839 private readonly sketchesServiceClient : SketchesServiceClientImpl ;
3940 @inject ( CreateFeatures )
4041 private readonly createFeatures : CreateFeatures ;
41- @inject ( EditorManager )
42- private readonly editorManager : EditorManager ;
42+ @inject ( ApplicationShell )
43+ private readonly shell : ApplicationShell ;
4344 private _validationContext : ValidationContext | undefined ;
4445
4546 override registerCommands ( registry : CommandRegistry ) : void {
@@ -123,7 +124,8 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
123124 if ( errorMessage ) {
124125 return errorMessage ;
125126 }
126- // If user did not write the .ino extension or ended the user input with dot, run the default Theia validation with the inferred name.
127+ // It's a legacy behavior from IDE 1.x. Validate the file it were an `.ino` file.
128+ // If user did not write the `.ino` extension or ended the user input with dot, run the default Theia validation with the inferred name.
127129 if ( extension === '.ino' && ! userInput . endsWith ( '.ino' ) ) {
128130 errorMessage = await super . validateFileName (
129131 `${ name } ${ extension } ` ,
@@ -228,7 +230,7 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
228230 return new UriAwareCommandHandlerWithCurrentEditorFallback (
229231 delegate ,
230232 this . selectionService ,
231- this . editorManager ,
233+ this . shell ,
232234 { multi }
233235 ) ;
234236 }
@@ -304,16 +306,17 @@ export function parseFileInput(userInput: string): FileInput {
304306/**
305307 * By default, the Theia-based URI-aware command handler tries to retrieve the URI from the selection service.
306308 * Delete/Rename from the tab-bar toolbar (`...`) is not active if the selection was never inside an editor.
307- * This implementation falls back to the current editor if no URI can be retrieved from the parent classes.
308- * See https://github.com/arduino/arduino-ide/issues/1847.
309+ * This implementation falls back to the current current title of the main panel if no URI can be retrieved from the parent classes.
310+ * - https://github.com/arduino/arduino-ide/issues/1847
311+ * - https://github.com/eclipse-theia/theia/issues/12139
309312 */
310313class UriAwareCommandHandlerWithCurrentEditorFallback <
311314 T extends MaybeArray < URI >
312315> extends UriAwareCommandHandler < T > {
313316 constructor (
314317 delegate : UriCommandHandler < T > ,
315318 selectionService : SelectionService ,
316- private readonly editorManager : EditorManager ,
319+ private readonly shell : ApplicationShell ,
317320 options ?: UriAwareCommandHandler . Options
318321 ) {
319322 super ( selectionService , delegate , options ) ;
@@ -323,11 +326,21 @@ class UriAwareCommandHandlerWithCurrentEditorFallback<
323326 protected override getUri ( ...args : any [ ] ) : T | undefined {
324327 const uri = super . getUri ( args ) ;
325328 if ( ! uri || ( Array . isArray ( uri ) && ! uri . length ) ) {
326- const fallbackUri = this . editorManager . currentEditor ?. getResourceUri ( ) ;
329+ const fallbackUri = this . currentTitleOwnerUriFromMainPanel ;
327330 if ( fallbackUri ) {
328331 return ( this . isMulti ( ) ? [ fallbackUri ] : fallbackUri ) as T ;
329332 }
330333 }
331334 return uri ;
332335 }
336+
337+ // The `currentEditor` is broken after a rename. (https://github.com/eclipse-theia/theia/issues/12139)
338+ // `ApplicationShell#currentWidget` might provide a wrong result just as the `getFocusedCodeEditor` and `getFocusedCodeEditor` of the `MonacoEditorService`
339+ // Try to extract the URI from the current title of the main panel if it's an editor widget.
340+ private get currentTitleOwnerUriFromMainPanel ( ) : URI | undefined {
341+ const owner = this . shell . mainPanel . currentTitle ?. owner ;
342+ return owner instanceof EditorWidget
343+ ? owner . editor . getResourceUri ( )
344+ : undefined ;
345+ }
333346}
0 commit comments