Skip to content

LSP: In Zed, using clangd cannot fold precompiled macros (#if/#ifdef/#elif/#else/#endif) #37077

@luckzylp

Description

@luckzylp

Summary

The latest clangd already [Implement simple folding for preprocessor branches] (llvm/llvm-project#140959), The folding for preprocessor branches still cannot be supported in zed using the latest clangd.

Description

DESCRIPTION_HERE

Steps to reproduce:

  1. Open a C code project.
  2. Configure to use the latest clangd language server.
  3. cmake generates compile_commands.json.

Expected Behavior:

  1. Support for code folding is expected in areas with precompiled macros.

Actual Behavior:

  1. Can not see any that could fold the precompiled macro area.

Analysis

  1. In the log of clangd LSP, zed didn't send clangd textDocument/foldingRange request, should use the tree - sitter provide code folding function.
  2. clangd lsp as follows,
// Send:
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///D:/TestProject/src/test.c","languageId":"c","version":0,"text":"\n#include \"uart.h\"\n\n\n#ifdef PRINT_EN\n#if (defined(SDCC) || defined(__SDCC))\nint putchar(int ch)\n#elif (defined(__C51__) || defined(__CX51__))\nchar putchar(char ch)\n#elif (defined(__IAR_SYSTEMS_C__) || defined(__IAR_SYSTEMS_CPP__) || defined(__iar_compiler_version__) || \\\n       defined(__IAR_SYSTEMS_ICC__))\ntypdef FILE void *;\nint fputc(int ch, FILE *f)\n#else\n#error \"Not support compiler!!!\"\n#endif\n{\n    UART1_PutChar(ch);\n    return ch;\n}\n#endif\n#endif\n"}}}

// Receive:
{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[{"code":"pp_hash_error","message":"\"Not support compiler!!!\"","range":{"end":{"character":6,"line":14},"start":{"character":1,"line":14}},"relatedInformation":[],"severity":1,"source":"clang"},{"code":"expected_either","message":"Expected identifier or '('","range":{"end":{"character":1,"line":16},"start":{"character":0,"line":16}},"relatedInformation":[],"severity":1,"source":"clang"},{"code":"pp_endif_without_if","message":"#endif without #if","range":{"end":{"character":6,"line":21},"start":{"character":1,"line":21}},"relatedInformation":[],"severity":1,"source":"clang"},{"code":"unused-includes","codeDescription":{"href":"https://clangd.llvm.org/guides/include-cleaner"},"message":"Included header uart.h is not used directly (fix available)","range":{"end":{"character":17,"line":1},"start":{"character":0,"line":1}},"relatedInformation":[],"severity":2,"source":"clangd","tags":[1]}],"uri":"file:///D:/TestProject/src/test.c","version":0}}

// Receive:
{"jsonrpc":"2.0","method":"textDocument/inactiveRegions","params":{"regions":[{"end":{"character":26,"line":12},"start":{"character":0,"line":6}}],"textDocument":{"uri":"file:///D:/TestProject/src/test.c"}}}

// Send:
{"jsonrpc":"2.0","id":272,"method":"textDocument/documentHighlight","params":{"textDocument":{"uri":"file:///D:/TestProject/src/test.c"},"position":{"line":12,"character":26}}}

// Receive:
{"id":272,"jsonrpc":"2.0","result":[]}

// Send:
{"jsonrpc":"2.0","id":273,"method":"textDocument/codeAction","params":{"textDocument":{"uri":"file:///D:/TestProject/src/test.c"},"range":{"start":{"line":12,"character":26},"end":{"line":12,"character":26}},"context":{"diagnostics":[{"range":{"start":{"line":6,"character":0},"end":{"line":12,"character":26}},"severity":3,"source":"clangd","message":"inactive region"}],"only":["quickfix","refactor","info"]}}}

// Receive:
{"id":273,"jsonrpc":"2.0","result":[]}

A screenshot of the result from zed

Image

A screenshot of the result in VScode, using the same clangd server, but only the clangd extension

Image

Zed Version and System Specs

Zed: v0.203.0 (Zed Dev 0a9f407)
OS: Windows 10.0.26100
Memory: 15.7 GiB
Architecture: x86_64
GPU: Intel(R) Iris(R) Xe Graphics || Intel Corporation || 32.0.101.5972

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions