# editor: Add language detection for untitled buffers \(\#61201\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/690c7fac64a4ed013ad92d8794333a6548858e2f)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 690c7fac64a4ed013ad92d8794333a6548858e2f

Requested commit: 690c7fac64a4ed013ad92d8794333a6548858e2f

Commit: 690c7fac64a4ed013ad92d8794333a6548858e2f

Tree: 7b54a94a2f9914547edb0657e93ce2eda68a2c57

Author: ᴀᴍᴛᴏᴀᴇʀ

Committer: GitHub

## Message

```
editor: Add language detection for untitled buffers (#61201)

# Objective

Closes #4868.

Untitled buffers start as Plain Text and require users to select a
language manually before receiving syntax highlighting. Add lightweight
automatic language detection for code entered or pasted into untitled
buffers.

## Solution

This builds on [Max Stevens's earlier language-detection
work](https://github.com/zed-industries/zed/pull/43057), replacing
Magika with [Betlang](https://github.com/DioxusLabs/betlang).

While researching smaller and faster alternatives to Magika, I came
across Betlang, a recently introduced language detection library
developed by DioxusLabs for dioxus-code. The fact that it comes from
DioxusLabs gave me more confidence in evaluating this relatively new
dependency for Zed. Betlang embeds an approximately 50 KB model, is
MIT-licensed, and depends only on `fearless_simd`, making it well suited
to Zed's cross-platform embedding requirements.

Detection runs on the background executor with bounded input sampling.
It is restricted to untitled buffers, skips content shorter than 20
bytes, and requires at least 50% confidence. These limits have worked
well in local testing. In release builds on Linux with an Intel Core
i5-13600KF, the `betlang::detect` call alone typically completes within
3 ms when processing the maximum sampled input.

## Testing

I added a test for language detection in untitled buffers that covers
both manually entered and pasted content. The test passes successfully.
I also manually tested the feature to verify that the overall experience
works well.

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

## Showcase



https://github.com/user-attachments/assets/96e28ad7-2968-4325-9aff-37fe813a2da7



---

Release Notes:

- Added automatic language detection for untitled buffers.

---------

Co-authored-by: Max Stevens <maxstevens2708@gmail.com>
```

## Parents

- [339d96b5a81a8fd8434b766e501c6a4c27f59b60](https://git.cafe/gitcafe/zed/commit/339d96b5a81a8fd8434b766e501c6a4c27f59b60?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/690c7fac64a4ed013ad92d8794333a6548858e2f?format=markdown)
