coder / code-server

VS Code in the browser
https://coder.com
MIT License
68.57k stars 5.62k forks source link

Ability to open a file directly using a URL #1964

Open AbdealiLoKo opened 4 years ago

AbdealiLoKo commented 4 years ago

We're trying to setup a online developmenr environment for our users - and are trying code-server.

We're using it for static HTML pages for website development, and I was hoping to add a button on the HTML pages which says "Edit this page on code-server".

I was hoping to setup that up, I could provide the path / URL to code-server ... something like: mycodeserver.domain.com/?file=project/README.md And it would open that particular file in the code-server when the button is clicked..

I couldn't find any documentation or notes about whether this is possible right now

NOTE: I did see https://github.com/cdr/code-server/issues/1960 - but it didn't seem the similar

Edit: Should also support line and column syntax, for example: http://code-server:port/path=/dir/sub/file.ext:line:col

code-asher commented 4 years ago

Yeah I don't think this is currently possible it but it seems straightforward enough to implement.

nhooyr commented 3 years ago

So this was implemented in https://github.com/cdr/code-server/issues/1292

Will become more visible with https://github.com/cdr/code-server/issues/2179

code-asher commented 3 years ago

I think this is for editing a file from a link on a website or something in code-server. So clicking it would launch code-server and then open that file. Actually it would be even cooler maybe if we could handle web+code-server:// URLs and then they open in your existing code-server (or a new one if it's not open).

nhooyr commented 3 years ago

Oh my bad. I thought we supported ?path= for this or something along those lines @code-asher

In regards to the scheme URL, see https://github.com/cdr/code-server/issues/1571

code-asher commented 3 years ago

We have ?folder= and ?workspace= but nothing for individual files yet.

I should have linked that issue! Maybe we just roll this up into that one.

nhooyr commented 3 years ago

I think they're different enough, will reopen this.

dkebler commented 3 years ago

Just giving my two cents that this is a worthy enhancement. A lot of doc sites themes/builders have built in opening the corresponding file in a github repo for editing from the rendered page like this https://geekdocs.de/usage/configuration/ (see Edit this page link). If opening a file directly from url worked in code-server one could use this feature by just supplying the configuration values. I've already tried and it kinda works sans being able to open the exact page.

  geekdocRepo: https://my.domain.net
  geekdocEditPath: ?folder=/files/content

renders https://my.domain.net/?folder=/files/content/_index.md

which sadly does not open the page for editing. Might be nice if it opened the root folder tree in naviator and that file in the editor.

JammSpread commented 3 years ago

I think if code-server were to implement this feature, the query param folder should be replaced with a single param named path. I just think it would be more straightforward and wouldn't be as confusing as having two. And maybe possibly @dkebler's comments:

Might be nice if it opened the root folder tree in naviator and that file in the editor.

could be implemented via an option.

dkebler commented 3 years ago

probably need to continue support for ?folder= but ?path= could as @JammSpread says be used for both, to load a directory (i.e. folder) in the explorer and an actual file in editor if supplied in path string. Not sure if it's possible but if there is already an open tab then it should just load the file there instead of opening another instance.

jkaster commented 3 years ago

Is anyone currently working on this? Having ?path= support file names would allow me to use code-server rather than rolling my own solution, which would be a waste when code-server is so close. I haven’t looked at the code yet, but I’ll see if I can find where path is implemented. FWIW, my implementation would be ?path=/dir/file.ext:line and possibly optional column for those who would want that level of granularity.

jsjoeio commented 3 years ago

Is anyone currently working on this?

Not actively being worked on, but I can move it to On Deck so it's on our radar and ready to be worked on!

jkaster commented 3 years ago

Thanks so much for the responsiveness.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no activity occurs in the next 5 days.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no activity occurs in the next 5 days.

jkaster commented 2 years ago

Is anyone currently working on this?

Not actively being worked on, but I can move it to On Deck so it's on our radar and ready to be worked on!

So ... it's been a year. How long does On Deck go out?

jsjoeio commented 2 years ago

So ... it's been a year. How long does On Deck go out?

Great question! So there isn't an exact number. We won't be adding many new features to code-server unfortunately. We'll still keep this open though but it's not guaranteed that it will be worked on. On Deck just means it's something that could be picked up.

I'm going to move it to Backlog though since like I said, we won't be adding new features.

jkaster commented 2 years ago

Thanks very much for the reply.

jkaster commented 2 years ago

Is there some other browser-based editor the team is working on instead?

jsjoeio commented 2 years ago

Is there some other browser-based editor the team is working on instead?

Great question! No, but we want to stay as close to VS Code as possible (you can see our patches here). In the ideal world, we would have no patches and any features (like this one) would be implemented in VS Code.

Since Codespaces came out and the VS Code team has open sourced more of the web-side of VS Code, we've been able to eliminate some of our patches. Therefore, we try to introduce as few new patches as possible. Our plan right now is to fix bugs and stay close to VS Code.

If this type of feature request could be implemented upstream, then VS Code Web/Codespaces and code-server could all benefit. I haven't looked though to see if that's something they're working on, or how difficult it would be.

jkaster commented 2 years ago

Makes sense. Thanks!

mark19142218 commented 2 years ago

I'm not sure, but I think there is something like this in VS now. Unless it's an extension I forgot I installed.

If I right click a line or selection in code-server, there is 'Share' > 'Copy vscode.dev Link'.

But the link goes to vscode.dev (as promised). It looks like it uses git info to build the link (I'm testing with a project from Github).

Can it be patched to work within code-server without going to vscode.dev?

jsjoeio commented 1 year ago

It might be possible but we would have to look into this. Help is welcome!

sleexyz commented 1 year ago

Did some spelunking into the VSCode codebase, and it seems like this functionality is already available OOB!

To open /foo/bar in the folder /foo:

/?folder=/foo&payload=[["openFile","vscode-remote:///foo/bar"]]

To open /foo/bar at line 1, column 1, in the folder /foo:

/?folder=/foo&payload=[["gotoLineMode","true"],["openFile","vscode-remote:///foo/bar:1:1"]]

This also works for workspaces specified via workspace (as opposed to folder).


References:

jkaster commented 1 year ago

Good find @sleexyz! I'll have to get back to trying this again soon

code-asher commented 1 year ago

Nice find!!

bsiranosian commented 1 year ago

Thank you, this works beautifully for my use case.

hoffm386 commented 1 year ago

@sleexyz when I try this, it opens an empty file at that location, even if there is already a file there Screen Shot 2023-09-06 at 3 47 36 PM I can even open the original file side-by-side with it Screen Shot 2023-09-06 at 5 06 43 PM But then if I hit command-S, it overwrites the original file with the empty version Screen Shot 2023-09-06 at 5 07 26 PM

So, I'm pretty sure that I have the right URL, and it's not just opening the empty file in a random location. Any ideas for troubleshooting? Or more general advice for finding answers in the VS Code source since code-server's documentation is so sparse?

itsamemarkus commented 1 year ago

thanks for the find @sleexyz . This really helps. Now the ultimate dream would be a copy link button.

In case somone is interested, why I need that: My use case is that I have a jupyter notebook and I link to code examples from my knowledgebase.

code-asher commented 1 year ago

@hoffm386 Sorry for the lack of a response but I hope late is better than never. code-server does not document much of VS Code, for that VS Code's own documentation is probably a better bet, wherever that might be if they even have it; I have had the most luck just reading through the source code and adding logging statements or stepping through. For this kind of thing, since it is native VS Code functionality, code-server is actually not even necessary, you can just run their web stuff directly, which can be more effective since it narrows the search scope.

@itsamemarkus If you frame it as a feature request to Codespaces, you could try submitting it upstream to https://github.com/microsoft/vscode. If it gains enough traction they might implement. Sounds like a cool idea.

jswhisperer commented 1 week ago
  • Where query para

Did some spelunking into the VSCode codebase, and it seems like this functionality is already available OOB!

To open /foo/bar in the folder /foo:

/?folder=/foo&payload=[["openFile","vscode-remote:///foo/bar"]]

To open /foo/bar at line 1, column 1, in the folder /foo:

/?folder=/foo&payload=[["gotoLineMode","true"],["openFile","vscode-remote:///foo/bar:1:1"]]

This also works for workspaces specified via workspace (as opposed to folder).

References:

Anyway to open a github repo automatically (clone) via url like https://code.visualstudio.com/docs/editor/vscode-web#_github-repos

code-asher commented 1 week ago

Anyway to open a github repo automatically (clone) via url like https://code.visualstudio.com/docs/editor/vscode-web#_github-repos

No, this is not currently possible with code-server, as far as I know. There might be an extension that does it; it would probably more sense to implement in an extension or in VS Code core than code-server.

jswhisperer commented 1 week ago

Anyway to open a github repo automatically (clone) via url like https://code.visualstudio.com/docs/editor/vscode-web#_github-repos

No, this is not currently possible with code-server, as far as I know. There might be an extension that does it; it would probably more sense to implement in an extension or in VS Code core than code-server.

For sure I understand, tbf this is in *vscode core there is an extension that goes most the way

 Visual Studio Code
vscode://robertohuertasm.open-vscode?repo=licensebat&uri=https://github.com/licensebat/licensebat.git&ref=6ec2f28d98d61f8d56cabeb5028abcd432f5bf41&file=licensebat-cli/src/cli.rs&range=17:5

# Visual Studio Code Insiders
vscode-insiders://robertohuertasm.open-vscode?repo=licensebat&uri=https://github.com/licensebat/licensebat.git&ref=6ec2f28d98d61f8d56cabeb5028abcd432f5bf41&file=licensebat-cli/src/cli.rs&range=17:5

# VSCodium
vscodium://robertohuertasm.open-vscode?repo=licensebat&uri=https://github.com/licensebat/licensebat.git&ref=6ec2f28d98d61f8d56cabeb5028abcd432f5bf41&file=licensebat-cli/src/cli.rs&range=17:5

I managed to adjust the manifest of code-server to handle custom protocols, I'm looking at just adding a server route to code-server to listen and handle. Will see how far I get :)

for reference:

{
  "name": "code-server",
  "short_name": "code-server",
  ...
  "protocol_handlers": [
    {
      "protocol": "web+coder",
      "url": "/robertohuertasm.open-vscode?%s"
    },
    {
      "protocol": "web+codey",
      "url": "/openRepo/https://github.com/robertohuertasm/vscode-open?branch=master&%s"
    }
  ],
...
}
code-asher commented 1 week ago

That seems promising!! Definitely interested in seeing what you come up with.