microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.52k stars 1.55k forks source link

Code folding didn't work as expected in directives and block comment #407

Closed LeThiHyVong closed 4 years ago

LeThiHyVong commented 7 years ago

In my code there are many places look like this

#if SOME_CONST_EXP
;//       some code
#else
      // some code
#endif

or

if SOME_CONST_EXP
;//      some code
#endif

or

/*************************************
//
//
//
//
*************************************/

EDIT (@bobbrow): (inserting request for folding on multiple consecutive single-line comments from #1443)

// Comment
// Comment
// Comment
// Comment

The code folding only work correctly between #else and #endif. From #if to next directive (#else in 1st case or #endif in 2nd case), if all line is started with ';//', the code folding didn't work. From #endif to next directive, it automatically group the code outside the directive for all time. The coding folder didn't work for 3rd, too.

codefolding

System spec:

Please correct this.

sean-mcmanus commented 4 years ago

@ret394 @KaeLL Yeah, our extension doesn't process unsaved files, so it'll fall back to use the previous code folding behavior provided by VS. You can upvote the unsaved file issue at https://github.com/microsoft/vscode-cpptools/issues/3343 .

KaeLL commented 4 years ago

The file was saved, for heaven's sake.
Open VS, open a new file, write that code, save it as trash.c, and there'll be no collapse arrow for the curly brackets (the main function block of code), only the individual preprocessor directives.
Indent the directives by 1 tab, and the collapse arrow shows up for the curly brackets and disappear for the directives, proving it's still indentation based folding. I'm using the 0.28.0 version of the extension, with the 1.45.1 version of VSCode on Clear Linux. Here's my settings.json

{
    "editor.fontFamily": "'Cascadia Code', 'Source Code Pro Medium', 'Ubuntu Mono', 'Cascadia Mono'",
    "editor.fontSize": 16,
    "editor.accessibilitySupport": "off",
    "editor.wordWrap": "on",
    "editor.renderControlCharacters": true,
    "editor.renderWhitespace": "all",
    "editor.smoothScrolling": true,
    "editor.fontLigatures": true,
    "editor.cursorSmoothCaretAnimation": true,
    "editor.minimap.enabled": false,
    "editor.quickSuggestionsDelay": 0,
    "editor.renderLineHighlight": "all",

    "[c]": {
        "editor.wordWrapColumn": 8000,
        "editor.acceptSuggestionOnEnter": "off",
        "editor.detectIndentation": false,
        "editor.dragAndDrop": false,
        "editor.emptySelectionClipboard": false,
        "editor.insertSpaces": false,
        "editor.showFoldingControls": "always",
        "editor.wordBasedSuggestions": false,
        "editor.acceptSuggestionOnCommitCharacter": false,
        "editor.autoClosingBrackets": "beforeWhitespace",
        "editor.autoClosingQuotes": "beforeWhitespace",
        "editor.autoSurround": "brackets",
        "editor.copyWithSyntaxHighlighting": false,
        // "editor.defaultFormatter": "ms-vscode.cpptools",
        "editor.foldingStrategy": "indentation",
        // "editor.formatOnType": true,
        "editor.largeFileOptimizations": false,
    },

    "files.enableTrash": false,
    // "files.insertFinalNewline": true,
    "files.trimFinalNewlines": true,
    "files.trimTrailingWhitespace": true,
    "files.hotExit": "off",
    // "files.watcherExclude": {
    // //   "**/.**":true
    //  "**/*.old/**":true,
    // },
    // "search.exclude": {
    //  "**/*.old/**":true,
    //  "**/[.]":true,
    //  "**/esp-idf/**":true
    // },

    "workbench.editor.enablePreview": false,
    "workbench.editor.enablePreviewFromQuickOpen": false,
    "workbench.editor.highlightModifiedTabs": true,
    "workbench.settings.editor": "json",
    "workbench.settings.useSplitJSON": true,
    "workbench.settings.openDefaultSettings": true,
    "workbench.colorTheme": "Default High Contrast",
    "workbench.colorCustomizations": {
        // "editorWhitespace.foreground": "#000000a0",
        "editorWhitespace.foreground": "#ffffff50",
        // "editor.selectionBackground": "#ffffffc2",
        // "editor.selectionHighlightBackground": "#00000000"
    },

    "window.menuBarVisibility": "toggle",
    "terminal.integrated.fontFamily": "'Cascadia Mono', 'Source Code Pro Medium', 'Ubuntu Mono'",
    "terminal.integrated.cursorBlinking": true,
    "terminal.integrated.cursorStyle": "line",
    "terminal.integrated.fontSize": 16,
    "terminal.integrated.scrollback": 18446744073709551615,

    "C_Cpp.clang_format_path": "/usr/bin/clang-format",

    "C_Cpp.experimentalFeatures": "Enabled",
    "C_Cpp.updateChannel": "Insiders",
    "C_Cpp.loggingLevel": "Information",

    "diffEditor.ignoreTrimWhitespace": false,
    "breadcrumbs.enabled": true,
    "breadcrumbs.symbolSortOrder": "type",
    "files.associations": {
        "*.h": "c"
    },
    "window.zoomLevel": 0,
    "C_Cpp.autoAddFileAssociations": false,
    "C_Cpp.clang_format_sortIncludes": false,
    "C_Cpp.default.browse.limitSymbolsToIncludedHeaders": false,
    "C_Cpp.default.cppStandard": "c++20",
    "C_Cpp.default.cStandard": "c11",
    "C_Cpp.default.intelliSenseMode": "gcc-x64",
    "C_Cpp.suggestSnippets": false,
    "C_Cpp.vcpkg.enabled": false,
}

I have nothing more to add. The issue exists, but if it's happening only to me, then it's not happening and it's fixed for all intents and purposes, and I no longer care enough to pursue to the end. Might as well close the thread. Good day.

sean-mcmanus commented 4 years ago

@KaeLL It's caused by your setting "editor.foldingStrategy": "indentation". The issue should be fixed if you remove that.

yuchongbo commented 4 years ago

I tried using the .28 version cpp extension, vscode version 1.45.1 and found an interesting thing. the case block cannot be folded in [switch] statement in an exist .cpp file, but when I copied the same content to a new created cpp file, case block folding works!

switch_case_folding
akbyrd commented 4 years ago

VS Code seems to cache the folding regions of files. When you open a folder or workspace it saves your open files and some other state. I think folding is part of that state. I haven't played around with it enough to know how to reliably reset that state. Maybe editing the file? Closing all files and clearing editor history? Just a thought.

sean-mcmanus commented 4 years ago

@yuchongbo The case folding is being tracked by https://github.com/microsoft/vscode-cpptools/issues/5523 . The fact that it starts working when it's copied to unsaved/new file is due to the fact that our extension only works on "file"-based URIs (and not the "untitled" URI scheme), which is tracked by https://github.com/microsoft/vscode-cpptools/issues/3343 .

sean-mcmanus commented 3 years ago

@akbyrd Your issue 4 is fixed with https://github.com/microsoft/vscode-cpptools/releases/tag/1.2.0-insiders2 (i.e. #5429 ).