Zed refuses to recognize .h files as C files
Summary
I was trying to setup my C project with clang-format found that my settings weren't being respected.
Description
I was setting up a .clangd-format file for a C project and wanted zed to format the code on save, so I opened up the local project settings file and wrote the following config.
{
"languages": {
"C": {
"format_on_save": "on"
}
}
}
And this worked fine for the .c files, however Zed still recognized the .h files as C++ files.
Knowing this, I updated the config.
{
"file_types": {
"C": ["c", "h"]
},
"languages": {
"C": {
"format_on_save": "on"
}
}
}
To my surprise, the .h files still weren't auto completing, and I suspected Zed was refusing to recognize .h files as C files.
This guess was all but confirmed when I changed the config file a third time.
{
"file_types": {
"C": ["c", "h"]
},
"languages": {
"C": {
"format_on_save": "on"
},
"C++": {
"format_on_save": "on"
}
}
}
(I later remembered that the file type is at the bottom corner of the window) Now the config file does what I want it to now, but its still an issue that the "file_types" setting was ignored , contrary to its documented behavior.
Expected Behavior:
Zed should've set the language mode of the .h files to C and called clangd-format on save with the second version of the project config.
Actual Behavior:
Zed didn't do any formatting of .h files until the third version of the project config.
Zed Version and System Specs
Zed: v0.179.4 (Zed) OS: Linux X11 linuxmint 22.1 Memory: 7.7 GiB Architecture: x86_64 GPU: Intel(R) HD Graphics 6000 (BDW GT3) || Intel open-source Mesa driver || Mesa 24.2.8-1ubuntu1~24.04.1
https://github.com/dovakin0007/zed/tree/header-as-C
I have made a small change here I am not sure whether I can raise a PR for this cause now .h files doesn't work with C++ format on save by default. I am not sure whether that's the expected behavior. If that's the expected behavior I will make a PR or someone can create a PR. feel free to share any suggestions
Actually c/c++ both support .h files. The other issues is the config precedence. Like this case after changing the user config should take precedence before the the default ones. It's mostly an issue in the scoring match where it give c++ more score for file type match and uses the first match of extension and chooses that.
I am not from zed team. There might be more context here the way scoring match works but looking at the code the bug is with score match.
I investigated this, and it is because of this .rev() that C++ gets chosen before C. If removed the issue goes away. Although the correct fix would be for the score to not stop in the first occurrence, and prefer languages that are inside the project .zed/settings.json.
I am not able to reproduce what you have reported. Your second settings options correctly identifies a *.h file as C language and triggers format_on_save via clangd for me.
My understanding is that the "C" language mode for Zed does not dictate what clangd chooses to do with *.h files, which are ambiguous as to whether they are C++ or C. Clang being clang defaults to C++.
If this is a C-only projects the Zed C language docs include this suggestion:
Without that .clangd file if I have #include <math.h> in a header it will resolve to the C++ sdk, but with it (after restarting the clangd LSP) it will resolve to the C version:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/math.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h