1
1
import { open } from '@theia/core/lib/browser/opener-service' ;
2
+ import { ApplicationShell } from '@theia/core/lib/browser/shell/application-shell' ;
2
3
import { MaybeArray , nls , SelectionService } from '@theia/core/lib/common' ;
3
4
import {
4
5
CommandRegistry ,
@@ -11,7 +12,7 @@ import {
11
12
UriCommandHandler ,
12
13
} from '@theia/core/lib/common/uri-command-handler' ;
13
14
import { 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 ' ;
15
16
import { FileStat } from '@theia/filesystem/lib/common/files' ;
16
17
import {
17
18
WorkspaceCommandContribution as TheiaWorkspaceCommandContribution ,
@@ -38,8 +39,8 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
38
39
private readonly sketchesServiceClient : SketchesServiceClientImpl ;
39
40
@inject ( CreateFeatures )
40
41
private readonly createFeatures : CreateFeatures ;
41
- @inject ( EditorManager )
42
- private readonly editorManager : EditorManager ;
42
+ @inject ( ApplicationShell )
43
+ private readonly shell : ApplicationShell ;
43
44
private _validationContext : ValidationContext | undefined ;
44
45
45
46
override registerCommands ( registry : CommandRegistry ) : void {
@@ -123,7 +124,8 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
123
124
if ( errorMessage ) {
124
125
return errorMessage ;
125
126
}
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.
127
129
if ( extension === '.ino' && ! userInput . endsWith ( '.ino' ) ) {
128
130
errorMessage = await super . validateFileName (
129
131
`${ name } ${ extension } ` ,
@@ -228,7 +230,7 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
228
230
return new UriAwareCommandHandlerWithCurrentEditorFallback (
229
231
delegate ,
230
232
this . selectionService ,
231
- this . editorManager ,
233
+ this . shell ,
232
234
{ multi }
233
235
) ;
234
236
}
@@ -304,16 +306,17 @@ export function parseFileInput(userInput: string): FileInput {
304
306
/**
305
307
* By default, the Theia-based URI-aware command handler tries to retrieve the URI from the selection service.
306
308
* 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
309
312
*/
310
313
class UriAwareCommandHandlerWithCurrentEditorFallback <
311
314
T extends MaybeArray < URI >
312
315
> extends UriAwareCommandHandler < T > {
313
316
constructor (
314
317
delegate : UriCommandHandler < T > ,
315
318
selectionService : SelectionService ,
316
- private readonly editorManager : EditorManager ,
319
+ private readonly shell : ApplicationShell ,
317
320
options ?: UriAwareCommandHandler . Options
318
321
) {
319
322
super ( selectionService , delegate , options ) ;
@@ -323,11 +326,21 @@ class UriAwareCommandHandlerWithCurrentEditorFallback<
323
326
protected override getUri ( ...args : any [ ] ) : T | undefined {
324
327
const uri = super . getUri ( args ) ;
325
328
if ( ! uri || ( Array . isArray ( uri ) && ! uri . length ) ) {
326
- const fallbackUri = this . editorManager . currentEditor ?. getResourceUri ( ) ;
329
+ const fallbackUri = this . currentTitleOwnerUriFromMainPanel ;
327
330
if ( fallbackUri ) {
328
331
return ( this . isMulti ( ) ? [ fallbackUri ] : fallbackUri ) as T ;
329
332
}
330
333
}
331
334
return uri ;
332
335
}
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
+ }
333
346
}
0 commit comments