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

Editor includes unnecessary header files in C #14411

Closed
1 task done
motherboardMage opened this issue Jul 13, 2024 · 5 comments
Closed
1 task done

Editor includes unnecessary header files in C #14411

motherboardMage opened this issue Jul 13, 2024 · 5 comments
Labels
bug [core label] c C programming language support language server failure Language server doesn't work as expected language server An umbrella label for all language servers language An umbrella label for all programming languages syntax behaviors

Comments

@motherboardMage
Copy link

Check for existing issues

  • Completed

Describe the bug / provide steps to reproduce it

The issue:

While typing certain keywords or functions in the editor and using auto-complete to complete the line, the editor automatically includes any header files that the keywords or functions are a part of. While this is extremely useful, sometimes the editor includes headers that are unsupported or unavailable on the specific operating system or compiler, or headers that are completely unnecessary for the code to behave as intended or ones that have been included in another header file in the code already.

Here are some screen recordings:

Screen.Recording.2024-07-13.at.11.11.18.PM.mov

As you can see, the editor prompts me to auto complete the "NULL" keyword and as I do it, it includes the "cstddef" header file which is both, unnecessary for the program to run and not available to the compiler. The standard libraries required by the program are already included in the linkedList.h header file.

Screenshot 2024-07-13 at 11 41 20 PM

Steps to reproduce:

  1. Create a new C file and include the standard input-output header (stdio.h)
  2. In your function, create a pointer variable of any type and start assigning it the value NULL
  3. While you type the first few letters of the keyword, the editor will prompt you to autocomplete it and show you the library "cstddef" in which NULL is supposed to be defined.
  4. While the NULL keyword is highlighted in the autocomplete menu, press enter or return to complete the keyword.
  5. The "cstddef" library should get included automatically and underlined in red as it is not available to clang.

There were also instances where libraries that have been already included in another header file get included again by the editor, in the same manner.

Screen.Recording.2024-07-13.at.11.55.35.PM.mov

I have raised and seen other people raise similar issues with formatting in the past and I believe all of them have to do something with the language server itself.

#13924 (comment)
#4842 (comment)
#12154 (comment)

Environment

Zed: v0.143.7 (Zed)
OS: macOS 14.5.0
Memory: 8 GiB
Architecture: x86_64

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

No response

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

Zed.log
@SomeoneToIgnore
Copy link
Contributor

SomeoneToIgnore commented Jul 13, 2024

This looks like the work of the LSP server underneath, which is clangd.
Does this server work better in any of the editors?

Zed itself knows nothing about languages — it barely has a tree-sitter syntax tree information and that's it.

I have raised and seen other people raise similar issues with formatting in the past and I believe all of them have to do something with the language server itself.

How is formatting related to the current issue?

@motherboardMage
Copy link
Author

motherboardMage commented Jul 13, 2024

How is formatting related to the current issue?

As far as I have been able to understand, one of the functions of the language server is holding required information for features like formatting, code completion, code highlighting etc. and frankly, to me the issues in these posts seem similar enough to include here. I am not very well versed with this technology so pardon me for any trouble caused.

@SomeoneToIgnore
Copy link
Contributor

No troubles at all, but to restate again: there's nothing on Zed side we can do except

  • finding discrepancies between Zed's and other editors' behavior with the same language server — if that's the case, we need a code example and the description/video how other editors that use clangd are better in importing

  • changing to another language server for that language — then we need a new server name and, again, a code example with the description on how this server is better

@chakkawat-sonaruea
Copy link

chakkawat-sonaruea commented Jul 14, 2024

This is not Zed problem. I am also get suggested NULL in nvim that used clangd, as follow:
Screenshot_2024-07-14_18-00-42

I want to point out that clangd mention C++ in the website, so it would suggest that clangd is config to work with C++ out of the box.
That why you see something C++ elements like std::nullptr_t in your first video.

If your project intent to only use C, I would recommand use compiler flag -xc for clang so that clangd to treat all files as C.

One way to configure clangd is with .clangd file.
If you didn't familiar with config clangd with .clangd file, create .clangd file in the root of your project folder.

And here are example of said config to using -xc to make clang treat all file as C.

CompileFlags:
  Add: [ -xc ]

After this restart Zed to make sure the change apply. If nothing wrong, you should have only autocomplete for C.

Here is desired behavior:
Screenshot_2024-07-14_19-11-25

@SomeoneToIgnore SomeoneToIgnore closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2024
@Moshyfawn Moshyfawn added language An umbrella label for all programming languages syntax behaviors c C programming language support language server failure Language server doesn't work as expected language server An umbrella label for all language servers and removed triage labels Jul 15, 2024
@pfirsich
Copy link

pfirsich commented Nov 11, 2024

This is a problem in multiple context, even when using C++. The way to disable this is to add the -header-insertion=never option to clangd. It would be nice if Zed had this as a configuration file, but as a workaround solution, you can add it to your .clangd file like so:

CompileFlags:
  Add: [ -header-insertion=never ]

EDIT: Unfortunately this doesn't work. If someone knows how to fix it, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug [core label] c C programming language support language server failure Language server doesn't work as expected language server An umbrella label for all language servers language An umbrella label for all programming languages syntax behaviors
Projects
None yet
Development

No branches or pull requests

6 participants