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

Builtin Zed prettier does not respect zed "tab_width" options #20235

Closed
1 task done
ikelos opened this issue Nov 5, 2024 · 2 comments
Closed
1 task done

Builtin Zed prettier does not respect zed "tab_width" options #20235

ikelos opened this issue Nov 5, 2024 · 2 comments
Labels
bug [core label] discoverability Feedback for discoverability of features, settings, etc editor Feedback for code editing, formatting, editor iterations, etc prettier Prettier tooling support

Comments

@ikelos
Copy link

ikelos commented Nov 5, 2024

Check for existing issues

  • Completed

Describe the bug / provide steps to reproduce it

The tab_size option is set to 4 by default (both globally and for javascript in particular) and the autoformatting on save is on by default. Open a Javascript file, ensure the indents are 4 spaces, save the file. The indents are changed to a tab size of 2. This is because the in-built prettier (allowed by default) has its own tab_width defaults, which are set to 2. This can be overridden in the zed config with:

{
    "languages": {
        "JavaScript": {
            "prettier": {
                "tab_width": 4
            }
        }
    }
}

However, this is extremely confusing and most users would expect tab_size at the top level, or at the language level to override the built-in prettier configuration.

This may be related to #12154 and #20009.

Environment

Zed: v0.159.7 (Zed)
OS: Linux Wayland gentoo 2.15
Memory: 15.3 GiB
Architecture: x86_64
GPU: Intel(R) UHD Graphics (TGL GT1) || Intel open-source Mesa driver || Mesa 24.2.6

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

In my view, javascript files, when saved by default, should adhere to the tab_width defaults of zed.

If applicable, attach your Zed.log file to this issue.

Not applicable, although when saving a broken JSON settings file, I discovered the following line:

2024-11-05T11:11:12.87589183Z [ERROR] default prettier instance failed to format buffer

Caused by:
    error during message '{"jsonrpc":"2.0","id":2,"method":"prettier/format","params":{"text":"..snip..","options":{"plugins":[],"parser":null,"filepath":"/home/mike/.config/zed/settings.json","prettierOptions":{"useTabs":false,"tabWidth":2,"printWidth":80}}}}' handling: SyntaxError: Unexpected token, expected "," (13:3)
      11 |   "buffer_font_size": 16,
      12 |   "tab_size": 4, "hard_tabs": false
    > 13 |   "theme": {
         |   ^
      14 |     "mode": "system",
      15 |     "light": "One Light",
      16 |     "dark": "One Dark"

Which caused me to investigate prettierOptions and eventually find that prettier has its own tab_width setting.

@jansol jansol added discoverability Feedback for discoverability of features, settings, etc editor Feedback for code editing, formatting, editor iterations, etc prettier Prettier tooling support and removed triage labels Nov 5, 2024
@github-actions github-actions bot added the triage label Nov 5, 2024
@JosephTLyons JosephTLyons added bug [core label] and removed triage labels Nov 5, 2024
@notpeter
Copy link
Member

I don't believe tab_width is a valid prettier setting. I believe it should be tabWidth but I would recommend against specifying these values for prettier in your Zed settings. Prettier defaults to tabWidth = 2, if you need to use a value of 4, you can set this value as:

  "languages": {
    "JavaScript": {
      "tab_size": 4
    }
  }

Zed will inject this into the prettier tabWidth setting here:

if !options.contains_key("tabWidth") {
options.insert(
"tabWidth".to_string(),
serde_json::Value::Number(serde_json::Number::from(
language_settings.tab_size.get(),
)),
);
}

I also recommend you explicit include .prettierc.json with similar prettier overrides per extension/language. This way prettier on the CLI and in Zed will behave the same.

I understand your confusion as to why a global setting of tab_size does not override the per-language values, this is because tab_size, hard_tabs, etc may be overriden by the config.toml for languages:


hard_tabs = true

The Zed defaults attempt to align with the most common defaults for a given language (e.g. hard tabs for go matching go fmt, tab_size = 2 for matching prettier defaults for JS/TS/json/jsonc, etc.)

Since the default.json tab_size is 4, the Javascript config.toml overrides that default to 2.

@notpeter notpeter closed this as not planned Won't fix, can't repro, duplicate, stale Feb 11, 2025
@Pumacks
Copy link

Pumacks commented Feb 21, 2025

Nahh this issue shouldnt be closed as not planned.
This is a valid feature.

I got Zed as a binary through pacman, this means i have to build this myself and have to edit these files for just adjusting the tabsize of the formatter.
plus i cant update zed through my package manager now.

please reopen this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug [core label] discoverability Feedback for discoverability of features, settings, etc editor Feedback for code editing, formatting, editor iterations, etc prettier Prettier tooling support
Projects
None yet
Development

No branches or pull requests

5 participants