zed icon indicating copy to clipboard operation
zed copied to clipboard

Automatically detect language for pasted code

Open wzxu opened this issue 2 years ago • 2 comments

Check for existing issues

  • [X] Completed

Describe the feature

Currently, when I paste copied code into a new empty document, it stays as Plain Text. In VS Code, it'll automatically detect and set the language. Would be nice if Zed supports this too.

If applicable, add mockups / screenshots to help present your vision of the feature

No response

wzxu avatar Mar 27 '23 07:03 wzxu

Another use case is standard input content. Example: git diff | zed -n - would then be auto-detected as a diff/patch.

dzuelke avatar Feb 21 '25 15:02 dzuelke

yes yes yes, it is MUST!

yigitkonur avatar May 09 '25 15:05 yigitkonur

PLEASE!!!

KKKZOZ avatar Aug 16 '25 08:08 KKKZOZ

VSCode uses guesslang to do this, but that repo hasn't been touched in 4 years. I wonder if there might be some low-hanging fruit here for a more modern / faster implementation with LLMs

valtism avatar Sep 18 '25 01:09 valtism

I guess it shouldn't be too hard to integrate Magika? https://opensource.googleblog.com/2025/11/announcing-magika-10-now-faster-smarter.html

DGolubets avatar Nov 10 '25 10:11 DGolubets

Oh wow that looks amazing! Would love for Zed to integrate that

valtism avatar Nov 10 '25 10:11 valtism

Based on https://docs.rs/magika/latest/magika/ seems not that hard to use?

The main question would be how to integrate this in Zed better:

  • one large concern is that tokio is in the dependencies: there's GlobalTokio GPUI global to help handle such cases in Zed, but the dependency footprint alone is a concern, we'd better have buffer, editor and other "base" crates without these extra dependencies if possible, or, at least, disabled by default.

Finding the right place to add this dependency and where to call it from seems to be the hardest part of the PR. Drafting (can be wrong), I would try to initialize and set the GPUI global in zed crate (have that global type available for both editor and zed crates) and use that global in the editor. The crate is kept on zed.rs level only, so no real magika types go beyond zed level.

  • all the magika::Session::new()-related questions: is it worth keeping this as some cx.global, what is the footprint of this extra thing and how slow is it to cold start instead?

That will influence whether that global from the previous bullet will have that session running, or will be just a code that cold-starts it every time.

  • since the code is pasted, we cannot use file paths to determine the language, yet we can react to EditorEvent::Edited event somewhere externally, or to multi_buffer::Event::Edited inside the editor itself: then, we will know which buffer got the change and has no language yet, we can take the buffer's text and put it into that global to determine the language.

One gotcha here is that we do not need the entire text to make a decision, we should at least limit that with some "3 screens of text max" heuristics at least, so we do not send megabytes of text accidentally.


Then, all that's left is to use https://docs.rs/magika/latest/magika/enum.ContentType.html and https://github.com/zed-industries/zed/blob/0149de4b54c55df8ec9ebc6a1da5b43c68e407e9/crates/language/src/language_registry.rs#L114

to derive a language, and call https://github.com/zed-industries/zed/blob/0149de4b54c55df8ec9ebc6a1da5b43c68e407e9/crates/language_selector/src/language_selector.rs#L206-L208 to set it.

Apparently, we have to use some hardcoded bits to unite both language worlds, like https://github.com/zed-industries/zed/blob/0149de4b54c55df8ec9ebc6a1da5b43c68e407e9/crates/extensions_ui/src/extension_suggest.rs#L14-L24 does, which seems ok.

This all seems to belong to the code that is inside the global mentioned above.

Lastly, given that this uses an AI model under the hood, we should respect the users disable_ai setting and thus only have the feature here enabled whenever that is not set to true.

SomeoneToIgnore avatar Nov 10 '25 13:11 SomeoneToIgnore

hi! been lurking for a while now but i'd definitely like to give this a shot, may I?

aditya-giri avatar Nov 17 '25 21:11 aditya-giri