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

Feature Request: Open new file buffer via API #12283

Closed
Jakobud opened this issue Sep 19, 2016 · 11 comments · Fixed by #18747
Closed

Feature Request: Open new file buffer via API #12283

Jakobud opened this issue Sep 19, 2016 · 11 comments · Fixed by #18747
Assignees
Labels
api feature-request Request for new features or functionality verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@Jakobud
Copy link

Jakobud commented Sep 19, 2016

  • VSCode Version: 1.5.2
  • OS Version: OSX 10.10.5, Win 7 x64

Is it possible to open a new file buffer via the API? Essentially, when you use your mouse (or keyboard shortcuts) and goto File menu > New File, it creates a new Untitled file buffer in the workspace. When you Save the file, it opens a Save File dialog where you choose where to save it along with the filename. Is it possible to create a new file buffer in this same manner via the API? If not, can this feature be added?

I have messed around with the API quite a bit in this area and if it is currently possible, it's not clear in the documentation.

I know you can do this:

window.openTextDocument(Uri.parse("untitled:c:\\project\\myfile.txt"))
  .then((document) => {
    window.showTextDocument(document);
  });

But this requires that you specify the file path for this new "untitled" file. When you hit save, it forces the file to be saved at "c:\project\myfile.txt". The user is not given the chance to specify where to save the file or what filename to give it.

To make a long story short, I essentially want the same functionality as going to File menu > New File. Does that make sense?

@jrieken
Copy link
Member

jrieken commented Jan 17, 2017

@bpasero is working on this

@bpasero bpasero added this to the January 2017 milestone Jan 17, 2017
@bpasero
Copy link
Member

bpasero commented Jan 17, 2017

@jrieken the one (little) challenging thing is that in ensureDocumentData we seem to need the URI upfront, but if we have untitled:// the URI is only known at a later point. I would need to return the URI from $tryOpenDocument but that code runs async because it needs to talk to the main side where the untitled URI is basically being created.

@jrieken
Copy link
Member

jrieken commented Jan 17, 2017

Yeah, we could check if only the scheme is set and then immediately call another method that simply creates a new document. Tho saying that makes me think if we should just expose that directly
on the API. Something like createTextDocument(language:string, ...)? That will be easier to understand than the scheme-only uri's

@bpasero
Copy link
Member

bpasero commented Jan 17, 2017

@jrieken I like the more explicit createTextDocument without URI, let me try to sketch something up so we can discuss further via PR.

@llali
Copy link
Member

llali commented Jan 17, 2017

@bpasero your proposal in #17917 sounds great. The only question would be the name of the file when the language is specified. The untitled document command right now creates documents as "Untitled-1", Untitled-2" ...
it would be nice to have an option to have different names for untitled document if it has specific language. For example for SQL documents it would be "SQLQuery-1", "SQLQuery-2" ...

@bpasero
Copy link
Member

bpasero commented Jan 17, 2017

Not sure about that to be fair.

@jrieken
Copy link
Member

jrieken commented Jan 18, 2017

@bpasero re #12283 (comment) there is one catch it forget to mention. We have a rule that createXYZ-functions are ctor replacements for interfaces we own. The consequence is that they must return sync and cannot return a promise with the result. That might make things more complicated and if it becomes to hard we should thing another overload of openTextDocument

@bpasero
Copy link
Member

bpasero commented Jan 19, 2017

A new overload was added to the openTextDocument method that allows to open an untitled document with optional language. For example you can call:

openTextDocument({ language: 'xml' })

To create a text document with XML as language. If you leave out the language options, it will just create a text document with plain text language.

@bpasero bpasero added the verification-needed Verification of issue is requested label Jan 19, 2017
@llali
Copy link
Member

llali commented Jan 19, 2017

@bpasero so the openTextDocument gets an uri now which has to be a valid path even if it's untitled. is it going to be the same or with the new option if I don't pass any uri it would be untitled?

@bpasero
Copy link
Member

bpasero commented Jan 19, 2017

@llali with the new API you no longer pass in a URI and you end up exactly in the same spot as if a user would have hit CtrlCmd+N to open a new untitled file. The optional language ID can be used to define the language of that document upfront.

@bpasero
Copy link
Member

bpasero commented Jan 23, 2017

  • To verify:
    A new overload was added to the openTextDocument method that allows to open an untitled document with optional language. For example you can call:

openTextDocument({ language: 'xml' })

To create a text document with XML as language. If you leave out the language options, it will just create a text document with plain text language.

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
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 verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants