Search code examples
visual-studiovisual-studio-2019clionjetbrains-ide

CLion equivalent of Visual Studio C++ "filters"


Context

In Visual Studio IDE, for C++ solutions, you can use so called "filters" to simulate source files tree/directories, while usually keeping all source code for each project in each project's root. Main merit being ability to directly #include "foo.h"

I've been trying to "switch" some solutions over to CLion, because of its multiplatform focus and trying to add support for CMake+GCC into already existing VS solutions, as no cross-platform IDE has native support for .sln/.vcxproj files

Now - I know that some larger CMake projects (as in solution/group of subprojects) often use plain filesystem subdirectories to further break-up its code in each subproject, but since some of "solutions" I'm trying to add CMake support to, consider of few hundred source files, I can't really go about refactoring everything manually and changing #include "service_foo.h" into #include service/service_foo.h in each of them.

Also another thing to consider is that I'm not migrating solution to work with CMake - I'm adding support for CMake builds/CLion IDE support alongside MSBuild toolchain/VS IDE support, so I don't want to introduce any breaking changes.

I'm still pretty new to JetBrains IDE ecosystem, so I wondered whether there is something that could provide this kind of functionality out of box, or perhaps with a plugin.

Question

Is there any way to sort source files into groups without making on-filesystem changes, that is in project view toolbox in CLion IDE?

Couldn't find anything in documentation so far, but it would be something alike to Code Hierarchy, but for .h/.cpp files instead.

Example

Filesystem-wise structure (as visible on-disk)
$REPO_ROOT is repository as provided with *.vcxproj, .idea/ etc.

Main CMakeList.txt: $REPO_ROOT/CmakeList.txt
Example subprojects: $REPO_ROOT/foo_slib/ $REPO_ROOT/program/
Example Source files: $REPO_ROOT/program/main.cpp $REPO_ROOT/program/gui_init.h $REPO_ROOT/program/VulkanDelegate.h 

Project/solution root-wise structure (as seen in project toolbox views in IDEs)
$VIEW_ROOT is main project open for CLion or .sln open for MSVS as seen in Project View in each IDE

Main CmakeList.txt: $VIEW_ROOT/CMAKE/CmakeList.txt
Example subprojects: $VIEW_ROOT/foo_slib/ $VIEW_ROOT/program/
Example Source files: $VIEW_ROOT/program/main.cpp $VIEW_ROOT/program/GUI/gui_init.h $VIEW_ROOT/program/Delegates/VulkanDelegate.h

Solution

  • You surely do NOT need to edit source files and change include paths. Nothing should be able to stop you from achieving the same #include paths with compiler include path option, -I<path>. In CLion, it's controlled through CMake's target_include_directories directive. You may also have to figure out how the projects are linked.

    You can add files and folders to CLion's favorites. You can have multiple groups of favorites, so this can emulate arbitrary virtual folders in the project.

    For a more elaborate query language, there are Scopes. You can set them up, and then they will appear in the Views dropdown of the Project view, each as a separate View.