code-server icon indicating copy to clipboard operation
code-server copied to clipboard

Ability to open a file directly using a URL

Open AbdealiLoKo opened this issue 4 years ago • 29 comments

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

AbdealiLoKo avatar Aug 12 '20 05:08 AbdealiLoKo

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

code-asher avatar Aug 12 '20 16:08 code-asher

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

nhooyr avatar Dec 08 '20 15:12 nhooyr

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).

code-asher avatar Dec 08 '20 20:12 code-asher

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

nhooyr avatar Dec 09 '20 00:12 nhooyr

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.

code-asher avatar Dec 09 '20 17:12 code-asher

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

nhooyr avatar Dec 09 '20 21:12 nhooyr

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.

dkebler avatar Dec 21 '20 20:12 dkebler

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.

JammSpread avatar Dec 21 '20 22:12 JammSpread

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.

dkebler avatar Dec 22 '20 22:12 dkebler

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.

jkaster avatar Apr 10 '21 17:04 jkaster

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!

jsjoeio avatar Apr 12 '21 17:04 jsjoeio

Thanks so much for the responsiveness.

jkaster avatar Apr 12 '21 17:04 jkaster

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] avatar Oct 09 '21 20:10 stale[bot]

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] avatar Apr 09 '22 17:04 stale[bot]

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?

jkaster avatar Apr 11 '22 18:04 jkaster

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.

jsjoeio avatar Apr 11 '22 23:04 jsjoeio

Thanks very much for the reply.

jkaster avatar Apr 12 '22 00:04 jkaster

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

jkaster avatar Apr 12 '22 00:04 jkaster

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.

jsjoeio avatar Apr 12 '22 00:04 jsjoeio

Makes sense. Thanks!

jkaster avatar Apr 12 '22 16:04 jkaster

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?

mark19142218 avatar Nov 16 '22 10:11 mark19142218

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

jsjoeio avatar Nov 30 '22 16:11 jsjoeio

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:

  • Where query params seems to be handled: https://github.com/microsoft/vscode/blob/426ecc23472bc88ed7cab45ed0e72be1476b0585/src/vs/code/browser/workbench/workbench.ts#L291
    • There is this payload param, which seems to be a Map serialized as string pairs.
  • Mentions of opening files from payload: https://github.com/microsoft/vscode/blob/426ecc23472bc88ed7cab45ed0e72be1476b0585/src/vs/workbench/services/host/browser/browserHostService.ts#L343
  • URIs seem to be transformed via https://github.com/microsoft/vscode/blob/426ecc23472bc88ed7cab45ed0e72be1476b0585/src/vs/workbench/api/node/uriTransformer.ts

sleexyz avatar May 12 '23 18:05 sleexyz

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

jkaster avatar May 13 '23 00:05 jkaster

Nice find!!

code-asher avatar May 16 '23 18:05 code-asher

Thank you, this works beautifully for my use case.

bsiranosian avatar May 18 '23 17:05 bsiranosian

@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?

hoffm386 avatar Sep 11 '23 19:09 hoffm386

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.

itsamemarkus avatar Nov 12 '23 13:11 itsamemarkus

@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.

code-asher avatar Nov 16 '23 01:11 code-asher