Difference between revisions of "HowTo Use OpenFOAM with Visual Studio Code"

From OpenFOAMWiki
(added link)
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
Because ccls needs to know how the files are compiled, you need to do a bit of configuration.
 
Because ccls needs to know how the files are compiled, you need to do a bit of configuration.
 
This guide shows how to set this up for OpenFOAM in [https://code.visualstudio.com/ Visual Studio Code].
 
This guide shows how to set this up for OpenFOAM in [https://code.visualstudio.com/ Visual Studio Code].
 
Please note, that this depends on OpenFOAM commits that still lie in the future (see https://develop.openfoam.com/Development/openfoam/-/issues/1936). I will remove this note once the commits get approved.
 
  
 
# Install VS Code
 
# Install VS Code
# Install the [https://marketplace.visualstudio.com/items?itemName=ccls-project.ccls ccls vs code extension]
+
# Install ccls and the the [https://marketplace.visualstudio.com/items?itemName=ccls-project.ccls ccls vs code extension]
 
# Install [https://github.com/rizsotto/Bear bear]
 
# Install [https://github.com/rizsotto/Bear bear]
# Rebuild OpenFOAM and all your custom libraries that use wmake
+
# Rebuild OpenFOAM and all your custom libraries that use wmake and pass the "-with-bear" option to wmake / Allwmake . If you get a "unknown option: '-with-bear' " error, it is because your OpenFOAM version is too old. But don't worry, I wrote a [https://develop.openfoam.com/Development/openfoam/uploads/3501dca35d4a7fcac7a87575a2ae35ab/bearOpenFOAM8.patch backport for OpenFOAM8] and a [https://develop.openfoam.com/Development/openfoam/uploads/e0610c08fac4a3bfa6c7ff85532dc9e4/bearOpenFOAM7.patch backport for OpenFOAM7]. If you use an even older version of OpenFOAM, you can easily adopt those backports. Note that this backported version does not require you to pass "-with-bear" and might be slower. If you want to check if you did this right, you can start compilation and run <code>ps -ef | grep bear</code>. There should be a process that looks a bit like this: <code>bear --append --output /home/volker/openfoam/platforms/linux64GccDPInt32Opt/lib/../compile_commands.json -- wmake -all</code>
 
+
# Run the command <code>openfoam-root-dir/bin/tools/vscode-settings > openfoam.code-workspace</code> (you need to [https://openfoam.org/download/source/setting-environment/ set the environment] for that)
You can now either setup a folder or a workspace to work with OpenFOAM.
+
 
+
Setting up a workspace
+
 
+
# Run the command setupVScodeFOAM --generate-workspace (you need to [https://openfoam.org/download/source/setting-environment/ set the environment] for that)
+
 
# In VS Code, select File -> Open Workspace and select the openfoam.code-workspace file generated by the previous step.
 
# In VS Code, select File -> Open Workspace and select the openfoam.code-workspace file generated by the previous step.
 
+
# You might want to check if openfoam.code-workspace looks something like this:
 
+
{
Setting up a folder:
+
    "folders": [
 
+
        {
# Run the command setupVScodeFOAM (you need to [https://openfoam.org/download/source/setting-environment/ set the environment] for that)
+
            "path": "/home/volker/openfoam"
# In VS Code, open the folder where you ran the setupVScodeFOAM command. Note that it does not matter whether the source files are in this folder or not.
+
        }
 
+
    ],
 
+
    "settings": {
 +
        "ccls.cache.directory": "/home/volker/openfoam/build/linux64GccDPInt32Opt/ccls-cache",
 +
 +
        "ccls.misc.compilationDatabaseDirectory": "/home/volker/openfoam/build/linux64GccDPInt32Opt",
 +
 +
        "C_Cpp.default.compileCommands": "/home/volker/openfoam/etc/openfoam wmake -with-bear -s -j",
 +
 +
        "C_Cpp.autocomplete": "disabled",
 +
        "C_Cpp.errorSquiggles": "disabled",
 +
        "C_Cpp.formatting": "disabled",
 +
        "C_Cpp.intelliSenseEngine": "disabled"
 +
    }
 +
}
  
 
Note that after doing the steps above,  "Go to definition" or similar features will only work after ccls finished generating its cache. This process can take a while (see if there is a "ccls" process that heavily uses the cpu).
 
Note that after doing the steps above,  "Go to definition" or similar features will only work after ccls finished generating its cache. This process can take a while (see if there is a "ccls" process that heavily uses the cpu).
Line 30: Line 35:
  
 
If you find that "Go to Definition" or some other features do not work on some specific function, please file a bug report for the [https://github.com/MaskRay/vscode-ccls/issues ccls plugin].
 
If you find that "Go to Definition" or some other features do not work on some specific function, please file a bug report for the [https://github.com/MaskRay/vscode-ccls/issues ccls plugin].
 +
 +
== Backports ==

Latest revision as of 13:42, 6 July 2023

The C++ language server (ccls) can read and analyze C++ source code to provide features like "Go to Definition" in many Editors. Because ccls needs to know how the files are compiled, you need to do a bit of configuration. This guide shows how to set this up for OpenFOAM in Visual Studio Code.

  1. Install VS Code
  2. Install ccls and the the ccls vs code extension
  3. Install bear
  4. Rebuild OpenFOAM and all your custom libraries that use wmake and pass the "-with-bear" option to wmake / Allwmake . If you get a "unknown option: '-with-bear' " error, it is because your OpenFOAM version is too old. But don't worry, I wrote a backport for OpenFOAM8 and a backport for OpenFOAM7. If you use an even older version of OpenFOAM, you can easily adopt those backports. Note that this backported version does not require you to pass "-with-bear" and might be slower. If you want to check if you did this right, you can start compilation and run ps -ef | grep bear. There should be a process that looks a bit like this: bear --append --output /home/volker/openfoam/platforms/linux64GccDPInt32Opt/lib/../compile_commands.json -- wmake -all
  5. Run the command openfoam-root-dir/bin/tools/vscode-settings > openfoam.code-workspace (you need to set the environment for that)
  6. In VS Code, select File -> Open Workspace and select the openfoam.code-workspace file generated by the previous step.
  7. You might want to check if openfoam.code-workspace looks something like this:
{
    "folders": [
        {
            "path": "/home/volker/openfoam"
        }
    ],
    "settings": {
        "ccls.cache.directory": "/home/volker/openfoam/build/linux64GccDPInt32Opt/ccls-cache",

        "ccls.misc.compilationDatabaseDirectory": "/home/volker/openfoam/build/linux64GccDPInt32Opt",

        "C_Cpp.default.compileCommands": "/home/volker/openfoam/etc/openfoam wmake -with-bear -s -j",

        "C_Cpp.autocomplete": "disabled",
        "C_Cpp.errorSquiggles": "disabled",
        "C_Cpp.formatting": "disabled",
        "C_Cpp.intelliSenseEngine": "disabled"
    }
}

Note that after doing the steps above, "Go to definition" or similar features will only work after ccls finished generating its cache. This process can take a while (see if there is a "ccls" process that heavily uses the cpu).

For questions about this setup ask Volker Weißmann.

If you find that "Go to Definition" or some other features do not work on some specific function, please file a bug report for the ccls plugin.

Backports