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

Provide a command to create a new untitled file with a specific language set #152920

Closed
karrtikr opened this issue Jun 22, 2022 · 14 comments · Fixed by #153872
Closed

Provide a command to create a new untitled file with a specific language set #152920

karrtikr opened this issue Jun 22, 2022 · 14 comments · Fixed by #153872
Assignees
Labels
api feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities insiders-released Patch has been released in VS Code Insiders verification-needed Verification of issue is requested verified Verification succeeded workbench-untitled-editors Managing of untitled editors in workbench window
Milestone

Comments

@karrtikr
Copy link
Contributor

VSCode provides workbench.action.files.newUntitledFile using which we can create a new untitled file, which then instructs to set a particular language:

newcreatepythonfile

We're working on our walkthrough in Python extension which directly offers to create a new python file, it would be great if the command supports an argument which accepts the language to create the file for.

cc/ @lramos15

Unfortunately we cannot use VSCode APIs to accomplish this as extension is not guaranteed to be activated when the walkthrough is opened, so the command isn't available when the button is clicked, see:

weirdcreatepythonfileexp

@bpasero
Copy link
Member

bpasero commented Jun 23, 2022

Can you not use this API?

export function setTextDocumentLanguage(document: TextDocument, languageId: string): Thenable<TextDocument>;

@bpasero bpasero added the info-needed Issue requires more information from poster label Jun 23, 2022
@karrtikr
Copy link
Contributor Author

Extension takes some while to activate, so any commands we register ourselves using the API aren't available at startup.

@bpasero
Copy link
Member

bpasero commented Jun 23, 2022

I am a bit confused, how can there be a python specific welcome command without the python extension?

@bpasero
Copy link
Member

bpasero commented Jun 23, 2022

I think such a change would be relatively easy, similar to the view type argument:

KeybindingsRegistry.registerCommandAndKeybindingRule({
weight: KeybindingWeight.WorkbenchContrib,
when: null,
primary: isWeb ? (isWindows ? KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyCode.KeyN) : KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KeyN) : KeyMod.CtrlCmd | KeyCode.KeyN,
secondary: isWeb ? [KeyMod.CtrlCmd | KeyCode.KeyN] : undefined,
id: NEW_UNTITLED_FILE_COMMAND_ID,
description: {
description: NEW_UNTITLED_FILE_LABEL,
args: [
{
isOptional: true,
name: 'viewType',
description: 'The editor view type',
schema: {
'type': 'object',
'required': ['viewType'],
'properties': {
'viewType': {
'type': 'string'
}
}
}
}
]
},
handler: async (accessor, args?: { viewType: string }) => {
const editorService = accessor.get(IEditorService);
await editorService.openEditor({
resource: undefined,
options: {
override: args?.viewType,
pinned: true
}
});
}
});

I think this can come in as a PR, if it turns out to be working.

@karrtikr
Copy link
Contributor Author

karrtikr commented Jun 23, 2022

I am a bit confused, how can there be a python specific welcome command without the python extension?

Opening python specific getting started doesn't require a command, it uses package.json directly so extension doesn't need to finish activating.

@karrtikr
Copy link
Contributor Author

I think such a change would be relatively easy, similar to the view type argument:

That's great. Just to doublecheck, is it possible to call workbench.action.files.newUntitledFile with arguments in package.json directly?

@bpasero
Copy link
Member

bpasero commented Jun 23, 2022

Hm, I am not entirely sure, @sandy081 do you know? Or @jrieken?

@jrieken
Copy link
Member

jrieken commented Jun 24, 2022

Just to doublecheck, is it possible to call workbench.action.files.newUntitledFile with arguments in package.json directly?

You mean define a keybinding for said command with arguments or how/where are the arguments defined? Technically you don't "call" a command from package.json, you can only reference them

@lramos15
Copy link
Member

Just to doublecheck, is it possible to call workbench.action.files.newUntitledFile with arguments in package.json directly?

You mean define a keybinding for said command with arguments or how/where are the arguments defined? Technically you don't "call" a command from package.json, you can only reference them

I believe kartik is referring to a command link in the statically defined walkthroughs within the package.json

@karrtikr
Copy link
Contributor Author

I believe kartik is referring to a command link in the statically defined walkthroughs within the package.json

Yes

Technically you don't "call" a command from package.json, you can only reference them

Right, can I reference such a command with arguments in walkthroughs within package.json? In this case it would be something like:

[Create Python File](command:toSide:workbench.action.files.newUntitledFile?<args>)

where <args> refers to args to command which instructs to open a python file.

@bpasero bpasero added the api label Jun 25, 2022
@yume-chan
Copy link
Contributor

can I reference such a command with arguments in walkthroughs

Yes, for example GitLens does this:

https://github.com/gitkraken/vscode-gitlens/blob/7b5205412dc333b7bc744ca00011f73e3304cfa2/walkthroughs/getting-started/1-setup.md?plain=1#L9

[GitLens: Welcome (Quick Setup)](command:gitlens.showWelcomePage?%22quick-setup%22)

where refers to args to command which instructs to open a python file.

No, workbench.action.files.newUntitledFile doesn't support specifying language in args.

id: NEW_UNTITLED_FILE_COMMAND_ID,
description: {
description: NEW_UNTITLED_FILE_LABEL,
args: [
{
isOptional: true,
name: 'viewType',
description: 'The editor view type',
schema: {
'type': 'object',
'required': ['viewType'],
'properties': {
'viewType': {
'type': 'string'
}
}
}
}
]
},


as extension is not guaranteed to be activated when the walkthrough is opened

You can activate your extension when the command is invoked. GitLens also does this:

https://github.com/gitkraken/vscode-gitlens/blob/7b5205412dc333b7bc744ca00011f73e3304cfa2/package.json#L89

"activationEvents": [
    "...",
    "onCommand:gitlens.showWelcomePage",
    "..."
]

@karrtikr
Copy link
Contributor Author

Yes, for example GitLens does this:

Gotcha, thanks. I assume it's URI encoded.

No, workbench.action.files.newUntitledFile doesn't support specifying language in args.

That is what this feature request is for :)

You can activate your extension when the command is invoked. GitLens also does this:

We don't have our own specific open walkthrough command, so our walkthrough is opened before commands finish registering, i.e our command may not be registered by the time Create Python File is clicked.

@bpasero bpasero added help wanted Issues identified as good community contribution opportunities and removed info-needed Issue requires more information from poster labels Jun 26, 2022
@bpasero bpasero added this to the Backlog milestone Jun 26, 2022
@bpasero bpasero added the feature-request Request for new features or functionality label Jun 26, 2022
@bpasero
Copy link
Member

bpasero commented Jun 26, 2022

Open for a contribution after validation it works from welcome 👍

@jrieken
Copy link
Member

jrieken commented Jun 27, 2022

@karrtikr Just fyi that the "format" of such arguments is the following: JSON.stringify the arguments, then encodeURIComponent the result, and use that as URI query.

@vscodenpa vscodenpa added the unreleased Patch has not yet been released in VS Code Insiders label Jul 1, 2022
@bpasero bpasero modified the milestones: Backlog, July 2022 Jul 2, 2022
@bpasero bpasero assigned karrtikr and unassigned bpasero and lramos15 Jul 2, 2022
@vscodenpa vscodenpa added insiders-released Patch has been released in VS Code Insiders and removed unreleased Patch has not yet been released in VS Code Insiders labels Jul 8, 2022
@rzhao271 rzhao271 added verification-needed Verification of issue is requested verified Verification succeeded labels Jul 28, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Aug 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities insiders-released Patch has been released in VS Code Insiders verification-needed Verification of issue is requested verified Verification succeeded workbench-untitled-editors Managing of untitled editors in workbench window
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants