Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot open a file using a preferred factory from launcher #15494

Open
jschaeck opened this issue Dec 5, 2023 · 3 comments
Open

Cannot open a file using a preferred factory from launcher #15494

jschaeck opened this issue Dec 5, 2023 · 3 comments

Comments

@jschaeck
Copy link

jschaeck commented Dec 5, 2023

Problem

Using the great extensibility of Jupyterlab, I was able to create my own editor for .py files. As I have an according widget registry, it is automatically used when double clicking a .py file or using the "Open from Path..." command

On the launcher, there is already a default python file entry. From a use case perspective, there is no need for me to replace that. What I need is that after creating a new .py file via launcher (or the alternative ways via Menu, Command Palette) my own editor is used (my widget factory is used) as the default

It seems the implementation of fileeditor:create-new is preventing this as it hard codes the factory to editor

const widget = (await commands.execute('docmanager:open', {
path: model.path,
factory: FACTORY
}))

Proposed Solution

Instead of hard coding editor, ask the document registry for the preferred widget factory for the path and use what is returned there (assuming this is possible but as in all other places my factory is used automatically, it seems to exist)

@jupyterlab-probot jupyterlab-probot bot added the status:Needs Triage Applied to new issues that need triage label Dec 5, 2023
@jschaeck jschaeck changed the title Command fileeditor:create-new - Do not hardcode WidgetFactory that is used to open file after creation Command fileeditor:create-new - Do not hard code WidgetFactory that is used to open file after creation Dec 5, 2023
@jschaeck
Copy link
Author

jschaeck commented Dec 5, 2023

Maybe a solution could also be to not pass the factory at all (which open path seems to do) as then in the document manager at some place this is called: const factory = this.registry.defaultWidgetFactory(newPath);

@krassowski
Copy link
Member

Let's take a step back. Why does the launcher depend on fileeditor and use fileeditor:create-new? This is the functionality:

async function createNew(
commands: CommandRegistry,
cwd: string,
ext: string = 'txt'
) {
const model = await commands.execute('docmanager:new-untitled', {
path: cwd,
type: 'file',
ext
});
if (model != undefined) {
const widget = (await commands.execute('docmanager:open', {
path: model.path,
factory: FACTORY
})) as unknown as IDocumentWidget;
widget.isUntitled = true;
return widget;
}
}

It feels justified that fileeditor has a hard-coded factory - it is what the command does (creates and opens in file editor). Launcher could possibly duplicate it but without the hardcoded factory, or maybe there should be a command in docmanager for "create-and-open"? But then, when opening from laucher we do want to ensure we open the editable representation. Think about a HTML file. If someone added a HTML tile in the launcher they would want to create an empty untitled.html and then open it in an editor rather than view rendering of an empty page, right?

So I think maybe we need to augment the factories to distinguish between "viewers" and "editors" and then docmanager could have "create-and-open" called from the launcher with factory: "prefer-editor" (or a boolean or something). Or maybe to avoid command creep we would just add this argument to docmanager:open and duplicate the above two-liner directly in launcher.

How does it sound?

@jschaeck
Copy link
Author

jschaeck commented Dec 8, 2023

Thanks for your reply.

If I understand correctly, you are saying that it is fine for file editor to hard code the extension in its own command , which I can understand. In that case, could file editor maybe changed to be configurable so that consumers can influence what it registers and what not to avoid overlap with custom functionality?

So instead of the approach to reuse an existing launcher icon with a custom factory, have a custom icon but be able to hide the default one?

@JasonWeill JasonWeill changed the title Command fileeditor:create-new - Do not hard code WidgetFactory that is used to open file after creation Cannot open a file using a preferred factory from launcher Dec 12, 2023
@JasonWeill JasonWeill removed the status:Needs Triage Applied to new issues that need triage label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants