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

How to use it with a custom subdirectory?

Open 0xFA11 opened this issue 1 year ago • 4 comments

Hello,

First of all, huge thank you for this extension and centralizing Unreal+VSCode+clangd efforts into one extension!

I was wondering if it'd be possible to configure project creation to use a subdirectory?

Let's say I have this hierarchy:

  • .vscode/
  • MyUnrealProject/
    • Content/
    • Source/
    • MyUnrealProject.uproject
  • OtherStuff/
    • include/
    • src/
    • CMakeLists.txt
  • README.md

so the main idea behind is to use both Unreal project and another C++ project or something else side by side with each other.

any suggestions, do you see this working?

0xFA11 avatar Oct 28 '24 11:10 0xFA11

I probably wouldn't add to my extension to do it automatically but it is already set up to easily do it manually.

Take a look at the .clangd file and how it's set up.

  • At the top of the .clangd file it has 3 dashes --- .
  • These signify a page of rules.
  • You can create a new page of rules at the bottom of of your current .clangd file.

For example(.clangd):

# rest of .clangd is above here
---
If:
  PathMatch:
    - OtherStuff/.*\.(cpp|h)
CompileFlags:
  CompilationDatabase: OtherStuff/compileCommandsDirectory

  1. PathMatch: means only these directories and files with these extension types(cpp and h) will be affected by these rules. Note: You may need to change the extensions if you're OtherStuff project is not following the Unreal convention of cpp and h.
  2. CompilationDatabase: Since you are using CMake you can have CMake create a compile_commands.json file for your OtherStuff project. Specify the directory that the compile commands file, created by CMake, is in. Note: You can also use a general compile_flags.txt instead of a compile_commands.json. Just specify its directory here.
  3. The blank line at the end is on purpose. Make sure to have one. I've seen yaml files fail because there was none.

This is all you should need! As long as CMake is setup correctly and there are no errors in your CMake created compile_commands.json

boocs avatar Oct 28 '24 15:10 boocs

Run extension command "Create Unreal clangd project" on your Unreal project

it could've been much easier if this command would detect or ask for sub-directory for Unreal project root.

I guess, I need to open the sub-directory on its own, run the command and then open parent directory again and modify generated configuration files, right?

what's unfortunate is that I already have a .clangd on the parent folder, which means I somehow need to either symlink or have 2 separate .clangd configurations (same goes for .vscode/settings.json etc I assume)

0xFA11 avatar Oct 28 '24 15:10 0xFA11

I misread your directory structure. I'll have to look at it again.

boocs avatar Oct 28 '24 19:10 boocs

Yep you can have multiple .clangd files in your project. You don't have to use the multipage system of one file.

Your source files will use the closest parent .clangd file.

So your parent .clangd could have something like this and you shouldn't have to modify it after creating the unreal stuff:

# Separate .clangd in your parent directory
---
If:
  PathMatch:
    - OtherStuff/.*\.(cpp|h)
CompileFlags:
  CompilationDatabase: OtherStuff/compileCommandsDirectory

So now the parent .clangd file will affect OtherStuff and the Unreal .clangd file, that my extension creates, should affect only the Unreal stuff.


But... I see what you're saying now and it won't work with my extension because I assume the Unreal project is the main workspace in creation and function of the extension. It would need a big rewrite.

You may not like this but you may be able to use the Unreal workspace as your main project even with the directory structure you have now.

  • You may need to exclude the unreal project from your parent directory's vscode cfg file with the vscode setting "files.exclude". It might work without this but it might be visually better with it.
  • Then add the parent directory as a workspace in your Unreal project's *.code-workspace file.
  • Then open the project with your Unreal project's *.code-worksacpe file.

I'm not sure if that'll work or not or if that is something your dead set against but it's the only way to use my extension.

Do note that, if you do this, running my extensions Update Compile Commands will refresh the project *.code-workspace file and remove the new workspace entry in the *.code-workspace file. My extension has a setting that you can specify a list of settings to backup before the *.code-workspace gets overwritten. I think it'll work. Would have to test...

image

boocs avatar Oct 28 '24 20:10 boocs