eclipse-theia / theia

Eclipse Theia is a cloud & desktop IDE framework implemented in TypeScript.
http://theia-ide.org
Eclipse Public License 2.0
19.87k stars 2.48k forks source link

`showTextDocument` doesn't work with URI argument from `editor/context` menu command #13949

Open t1m0thyj opened 1 month ago

t1m0thyj commented 1 month ago

Bug Description:

The vscode.window.showTextDocument method supports only Theia URIs, and is incompatible with the VSCode URI passed as an argument when an editor/context menu command is executed.

Steps to Reproduce:

  1. Create a VS Code extension that contributes an editor/context menu command named theia.sampleCommand, and registers a handler like the following:
    vscode.commands.registerCommand("theia.sampleCommand", async (uri) => {
        // Ensure document is open and not in preview mode
        vscode.window.showTextDocument(uri, { preview: false });
        // Do other stuff
        vscode.window.showInformationMessage(uri.toString());
    });
  2. Install the extension in Theia and trigger the command by clicking it in editor context menu.
  3. Note that the info message never appears, and there is an error in the console:
    Uncaught (in promise) 
    TypeError: Cannot read properties of undefined (reading 'toString')
        at DocumentsExtImpl.showDocument (:3000/home/theia/nod…documents.js:192:63)
        at Object.showTextDocument (:3000/home/theia/nod…n-context.js:232:33)
        at Object.<anonymous> (:3000/home/theia/.th…ension.js:33:299442)
        at Generator.next (<anonymous>)
        at :3000/home/theia/.th…ension.js:33:276506
        at new Promise (<anonymous>)
        at r (:3000/home/theia/.th…ension.js:33:276251)
        at t.submitJcl (:3000/home/theia/.th…ension.js:33:299223)
        at Object.<anonymous> (:3000/home/theia/.th…ension.js:90:501340)
        at Generator.next (<anonymous>)

Additional Information

I believe I've tracked this down to the following line in showTextDocument, which has an instanceof check for Theia URIs: https://github.com/eclipse-theia/theia/blob/7cbfc7a6d99c0317f5e741875dd9097e4eb74c97/packages/plugin-ext/src/plugin/plugin-context.ts#L442

When editor/context menu command is executed, the argument passed is an instance of CodeUri which is populated by the getSelectedResources method: https://github.com/eclipse-theia/theia/blob/7cbfc7a6d99c0317f5e741875dd9097e4eb74c97/packages/plugin-ext/src/main/browser/menus/plugin-menu-command-adapter.ts#L324-L335

Since the URI passed to the command handler is a VSCode URI (from @theia/core/shared/vscode-uri), it fails the instanceof check that expects a Theia URI, resulting in document.uri evaluating to undefined.

msujew commented 1 month ago

Hey @t1m0thyj, thanks for the report and the investigation on the issue.

Since you already looked into this, are you interested in contributing a fix?

t1m0thyj commented 1 month ago

@msujew Sure, I'd be happy to. Which approach would you prefer for a fix?

  1. Make the instanceof check less restrictive by checking for VS Code URI
  2. Update the getSelectedResources method to return an array of Theia URIs

I'm leaning towards the first option since it seems like a less impactful change.

msujew commented 1 month ago

@t1m0thyj I also think the first option is better 👍