Skip to content

Commit

Permalink
fix: patch for incorrect currentEditor handling
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
  • Loading branch information
Akos Kitta committed Feb 2, 2023
1 parent 743c9bf commit 38e0806
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { open } from '@theia/core/lib/browser/opener-service';
import { ApplicationShell } from '@theia/core/lib/browser/shell/application-shell';
import { MaybeArray, nls, SelectionService } from '@theia/core/lib/common';
import {
CommandRegistry,
Expand All @@ -11,7 +12,7 @@ import {
UriCommandHandler,
} from '@theia/core/lib/common/uri-command-handler';
import { inject, injectable } from '@theia/core/shared/inversify';
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
import { EditorWidget } from '@theia/editor/lib/browser/editor-widget';
import { FileStat } from '@theia/filesystem/lib/common/files';
import {
WorkspaceCommandContribution as TheiaWorkspaceCommandContribution,
Expand All @@ -38,8 +39,8 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
private readonly sketchesServiceClient: SketchesServiceClientImpl;
@inject(CreateFeatures)
private readonly createFeatures: CreateFeatures;
@inject(EditorManager)
private readonly editorManager: EditorManager;
@inject(ApplicationShell)
private readonly shell: ApplicationShell;
private _validationContext: ValidationContext | undefined;

override registerCommands(registry: CommandRegistry): void {
Expand Down Expand Up @@ -123,7 +124,8 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
if (errorMessage) {
return errorMessage;
}
// If user did not write the .ino extension or ended the user input with dot, run the default Theia validation with the inferred name.
// It's a legacy behavior from IDE 1.x. Validate the file it were an `.ino` file.
// If user did not write the `.ino` extension or ended the user input with dot, run the default Theia validation with the inferred name.
if (extension === '.ino' && !userInput.endsWith('.ino')) {
errorMessage = await super.validateFileName(
`${name}${extension}`,
Expand Down Expand Up @@ -228,7 +230,7 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
return new UriAwareCommandHandlerWithCurrentEditorFallback(
delegate,
this.selectionService,
this.editorManager,
this.shell,
{ multi }
);
}
Expand Down Expand Up @@ -304,16 +306,17 @@ export function parseFileInput(userInput: string): FileInput {
/**
* By default, the Theia-based URI-aware command handler tries to retrieve the URI from the selection service.
* Delete/Rename from the tab-bar toolbar (`...`) is not active if the selection was never inside an editor.
* This implementation falls back to the current editor if no URI can be retrieved from the parent classes.
* See https://github.com/arduino/arduino-ide/issues/1847.
* This implementation falls back to the current current title of the main panel if no URI can be retrieved from the parent classes.
* - https://github.com/arduino/arduino-ide/issues/1847
* - https://github.com/eclipse-theia/theia/issues/12139
*/
class UriAwareCommandHandlerWithCurrentEditorFallback<
T extends MaybeArray<URI>
> extends UriAwareCommandHandler<T> {
constructor(
delegate: UriCommandHandler<T>,
selectionService: SelectionService,
private readonly editorManager: EditorManager,
private readonly shell: ApplicationShell,
options?: UriAwareCommandHandler.Options
) {
super(selectionService, delegate, options);
Expand All @@ -323,11 +326,21 @@ class UriAwareCommandHandlerWithCurrentEditorFallback<
protected override getUri(...args: any[]): T | undefined {
const uri = super.getUri(args);
if (!uri || (Array.isArray(uri) && !uri.length)) {
const fallbackUri = this.editorManager.currentEditor?.getResourceUri();
const fallbackUri = this.currentTitleOwnerUriFromMainPanel;
if (fallbackUri) {
return (this.isMulti() ? [fallbackUri] : fallbackUri) as T;
}
}
return uri;
}

// The `currentEditor` is broken after a rename. (https://github.com/eclipse-theia/theia/issues/12139)
// `ApplicationShell#currentWidget` might provide a wrong result just as the `getFocusedCodeEditor` and `getFocusedCodeEditor` of the `MonacoEditorService`
// Try to extract the URI from the current title of the main panel if it's an editor widget.
private get currentTitleOwnerUriFromMainPanel(): URI | undefined {
const owner = this.shell.mainPanel.currentTitle?.owner;
return owner instanceof EditorWidget
? owner.editor.getResourceUri()
: undefined;
}
}

0 comments on commit 38e0806

Please sign in to comment.