vscode-clangd icon indicating copy to clipboard operation
vscode-clangd copied to clipboard

C/C++ function folding when #if macros inside

Open zchrissirhcz opened this issue 3 years ago • 10 comments

C/C++ functions can't be folded if there is #if macros inside that function body.

Min reproduce:

void foo()
{

#if __ANDROID__
    printf("android\n");
#elif __linux__
    printf("linux\n");
#elif _MSC_VER
    printf("MSVC\n");
#endif

}

Snapshot: (can only fold macro regions, can't fold the whole function) image

Logs Please attach the clangd log from the "Output" window if you can. If possible, run with --log=verbose - note that the logs will include the contents of open files!

System information Clangd version (from the log, or clangd --version): 14.0.0-++20220115053019+cba72e4ce215-1~exp1~20220115173114.123 clangd extension version: v0.1.15 Operating system: Linux (ubuntu 20.04 with KDE Plasma)

zchrissirhcz avatar Feb 17 '22 06:02 zchrissirhcz

Folding range isn't yet implemented, so what you're seeing is VSCode's default indentation-based folding.

We are planning to work on this, but we're using it as a testbed for a new fast-parser so it won't be until late this year.

sam-mccall avatar Feb 17 '22 07:02 sam-mccall

sad : (

hybcloud avatar Apr 14 '22 14:04 hybcloud

As a work-around, I use vscode-explicit-folding plugin with settings.json add those lines:

    "explicitFolding.rules": {
        "*": {
            "begin": "{",
            "end": "}"
        },
        "javascriptreact": [
            {
                "begin": "{/*",
                "end": "*/}"
            },
            {
                "begin": "<",
                "end": "/>"
            }
        ]
    }

@hybcloud @sam-mccall Hope this helps clangd users before the mentioned refactor is done.

zchrissirhcz avatar Apr 14 '22 15:04 zchrissirhcz

Thanks! It should work in most situations, but I'm afraid that it'll corrupt if there was a { or } in string literal.

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2022年4月14日(星期四) 晚上11:07 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [clangd/vscode-clangd] C/C++ function folding when #if macros inside (Issue #297)

As a work-around, I use vscode-explicit-folding plugin with settings.json add those lines: "explicitFolding.rules": { "": { "begin": "{", "end": "}" }, "javascriptreact": [ { "begin": "{/", "end": "*/}" }, { "begin": "<", "end": "/>" } ] }

@hybcloud @sam-mccall Hope this helps clangd users before the mentioned refactor is done.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

hybcloud avatar Apr 15 '22 00:04 hybcloud

作为解决方法,我使用vscode-explicit-folding带有 settings.json 的插件添加这些行:

    "explicitFolding.rules": {
        "*": {
            "begin": "{",
            "end": "}"
        },
        "javascriptreact": [
            {
                "begin": "{/*",
                "end": "*/}"
            },
            {
                "begin": "<",
                "end": "/>"
            }
        ]
    }

@hybcloud @sam-mccall 希望这能在上述重构完成之前帮助 clangd 用户。 为什么我的显示是灰色的,不起作用 image

lichjian avatar Apr 28 '23 11:04 lichjian

Folding range isn't yet implemented, so what you're seeing is VSCode's default indentation-based folding.

We are planning to work on this, but we're using it as a testbed for a new fast-parser so it won't be until late this year.

Hi, sam-mccall, I still want this foldable feature. Its 2023 now, any progress about the fast-parser?

zchrissirhcz avatar Jun 20 '23 03:06 zchrissirhcz

It looks context-based folding is already supported since clangd v16.0.0. (https://github.com/clangd/clangd/issues/310) And it's working well on my end with clangd v16.0.6.

@zchrissirhcz Can you try out clang >= 16 and see if it works?

Edit: For older version of clangd, maybe you can make it by passing --folding-ranges argument to clangd server. You can edit arguments in vscode-clangd extension settings.

kon72 avatar Aug 13 '23 10:08 kon72

@kon72 Hi, I'm currently on Windows with clang 16.0.0. The IDE I'm using is VS2022, and installed the Clang Power Tools VS2022 extension to generate compile_commands.json. With an clangd server restart, the VSCode still cannot see folding in the line #if __ANDROID__.

Tried with .c and .cpp file extensions, not working on Windows: test.c:

#include <stdio.h>

int main() {
#if __ANDROID__
    printf("android\n");
#elif __linux__
    printf("linux\n");
#elif _MSC_VER
    printf("MSVC\n");
#endif
}

image

zchrissirhcz avatar Aug 14 '23 02:08 zchrissirhcz

Also tried on an remote Linux server with Clang 16.0.0, this time I can fold #if __ANDROID__ macros:

image

By the way, how can I let in-active code being "all green color", while keeps the active code the normal syntax highlighting?

zchrissirhcz avatar Aug 14 '23 02:08 zchrissirhcz

Folding of preprocessor branches is not currently implemented (https://github.com/clangd/clangd/issues/1661 is open for this).

In the Linux screenshot, the lack of inactive highlight + and lack of folding on the int main() line suggest that clangd is not working for some reason (please feel free to share logs in a new issue if you'd like help investigating why). VSCode then falls back on its client-side folding behaviour which folds the preprocessor branches but not the function.

HighCommander4 avatar Aug 14 '23 03:08 HighCommander4