Get Involved/development/IDE configuration/Visual Studio Code: Difference between revisions

From KDE Community Wiki
Line 154: Line 154:
==Working on a project==
==Working on a project==


We work on a project in VSCode by opening it as a folder:
If there is a file <code>~/kde/src/utilities/kcalc/CMakePresets.json</code> please delete it. Because it makes it really hard to use the correct CMake build configuration in VSCode. Deleting this file should be temporary. Be careful not to git commit this deleted file. E.g. do not stage to commit the deleted file <code>CMakePresets.json</code>.


* <code>File</code> -> <code>Open Folder</code>
Open the kcalc project (i.e. the git work directory, i.e. the directory where kdesrc-build has git cloned the git repository of kcalc) <code>~/kde/src/utilities/kcalc</code>.
* Navigate to <code>~/kde/src/dolphin</code> and press <code>OK</code>


{{Info|Along the bottom of the window are buttons to configure, build, run, debug, etc.}}
In VSCode, in the bottom status bar:
* In the button "Click to select the current build variant" the CMake build configuration selected should be "Debug". I.e. the button should say "CMake: [Debug]: Ready".
* Ignore the button "No active kit" with tooltip "Click to change the active kit". Or select the "gcc" from your Linux distribution.
* In the right hand side there should be a button "Linux" with tooltip "C/C++ Configuration".
* In the button "Set the default build target", select your preferred CMake target, in our case "kcalc". In the button "Select the target to launch" select one of the executables which are created using CMake targets, in our case "kcalc".
* Ctrl+Shift+P > "CMake: Configure". It should say <code>[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_INSTALL_PREFIX:STRING=~/kde/usr -S~/kde/src/utilities/kcalc -B~/kde/build/utilities/kcalc -G "Unix Makefiles"</code> and <code>Build files have been written to: ~/kde/build/utilities/kcalc</code>.
* Press the button "Build" with gear icon with tooltip "Build the selected target: [kcalc]". It should say <code>[proc] Executing command: /usr/bin/cmake --build ~/kde/build/utilities/kcalc --config</code>.

Revision as of 00:47, 15 August 2022

Microsoft Visual Studio Code (VSCode) is a popular cross-platform, general-purpose, open source IDE. Thanks to its powerful extensions ecosystem it supports many languages as well as deep customization options for themes, fonts, keyboard controls, and more.

Installing

VSCode is available as a deb, rpm, portable, and in the Arch User Repository.

https://code.visualstudio.com/Download

Extensions

Once VSCode is installed we need some extensions to enable support for the languages to work on KDE projects.

  1. C/C++ Extension Pack - Enables support for C++ and CMake.
  1. Qt tools - Enables some Qt support.
  1. QML - Enables syntax highlighting for QML.

Optional:

Zeal is an application that allows browsing documentation offline.

Dash is a VSCode extension that enables a hotkey (Ctrl + H) to instantly open the item under the cursor in Zeal.

These paired together make looking up documentation while working on code very quick and easy.

kdesrc-build configuration

Needed to enable Language Server Protocol support:

In ~/.config/kdesrc-buildrc ensure these two options are in the global section and set to true:

    compile-commands-linking true
    compile-commands-export true

Other recommended settings:

global
...
    cmake-options -DCMAKE_BUILD_TYPE=Debug
    directory-layout invent
...
end global

Configuration

We will use kcalc (the KDE calculator app) as an example. Build kcalc:

kdesrc-build kcalc

Either open VSCode, from the VSCode main menu > File > Open Folder... [Ctrl+K Ctrl+O] > select ~/kde/src/utilities/kcalc/.

Or, in a terminal,

cd ~/kde/src/utilities/kcalc
code .

The directory opened in VSCode is also known as the VSCode workspace folder.

VSCode has two sets of settings: "User" settings (e.g. ~/.config/Code/User/settings.json) and "Workspace" settings (e.g. ~/kde/src/utilities/kcalc/.vscode/settings.json). You can see these in VSCode main menu > File > Preferences > Settings Ctrl+Comma > at the top there are two tabs: "User" and "Workspace"

VSCode stores settings related to a specific project in the top-level directory of that project, in a hidden directory named .vscode.

~/kde/src/utilities/kcalc/.vscode/settings.json

Create this file. Make it have the content:

{
    "cmake.buildDirectory": "${workspaceFolder}/../../build/dolphin"
}

~/kde/src/utilities/kcalc/.vscode/c_cpp_properties.json

This config enables the correct settings to support C++, CMake & IntelliSense.

Create this file. Make it have the content:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}",
            "compileCommands": "${workspaceFolder}/compile_commands.json"
        }
    ],
    "version": 4
}

User settings

These settings apply once to every project.

In VSCode open the command palette with Ctrl + Shift + P > "Preferences: Open User Settings (JSON)". On my machine the file has the content:

{
    "editor.wordWrap": "on",
    "cmake.environment": {
      "PATH": "~/kde/usr/bin:${env:PATH}",
      "LD_LIBRARY_PATH": "~/kde/usr/lib/x86_64-linux-gnu:${env:LD_LIBRARY_PATH}",
      "XDG_DATA_DIRS": "~/kde/usr/share:${env:XDG_DATA_DIRS}",
      "XDG_CONFIG_DIRS": "~/kde/usr/etc/xdg:${env:XDG_CONFIG_DIRS}",
      "QT_PLUGIN_PATH": "~/kde/usr/lib/plugins:${env:QT_PLUGIN_PATH}",
      "QML2_IMPORT_PATH": "~/kde/usr/lib/qml:${env:QML2_IMPORT_PATH}",
      "QT_QUICK_CONTROLS_STYLE_PATH": "~/kde/usr/lib/qml/QtQuick/Controls.2/:${env:QT_QUICK_CONTROLS_STYLE_PATH}",
    },
    "cmake.generator": "Unix Makefiles",
    "cmake.installPrefix": "~/kde/usr",
}

Or, alternatively, from VSCode main menu > File > Preferences > Settings Ctrl+Comma > User > Extensions > CMake Tools > scroll through the list and make sure that you keep the defaults, plus you configure the following:

"cmake.environment": {
  "PATH": "~/kde/usr/bin:${env:PATH}",
  "LD_LIBRARY_PATH": "~/kde/usr/lib/x86_64-linux-gnu:${env:LD_LIBRARY_PATH}",
  "XDG_DATA_DIRS": "~/kde/usr/share:${env:XDG_DATA_DIRS}",
  "XDG_CONFIG_DIRS": "~/kde/usr/etc/xdg:${env:XDG_CONFIG_DIRS}",
  "QT_PLUGIN_PATH": "~/kde/usr/lib/plugins:${env:QT_PLUGIN_PATH}",
  "QML2_IMPORT_PATH": "~/kde/usr/lib/qml:${env:QML2_IMPORT_PATH}",
  "QT_QUICK_CONTROLS_STYLE_PATH": "~/kde/usr/lib/qml/QtQuick/Controls.2/:${env:QT_QUICK_CONTROLS_STYLE_PATH}"
}

"cmake.generator": "Unix Makefiles"

"cmake.installPrefix": "~/kde/usr"

E.g. scroll to "Cmake: Environment Environment variables to set when running CMake commands." > Add Item > Key: PATH , Value: ~/kde/usr/bin:${env:PATH}. Then, while you hover with the mouse over this settings, or wile you edit this setting, on the left hand side of "Cmake: Environment" a "gear" icon will appear > click on it > Copy Settings as JSON > make sure that the contents of the clipboard is equal to the JSON snippet from above, for "cmake.environment".

After you finish configuring VSCode, close all VSCode windows.

Working on a project

If there is a file ~/kde/src/utilities/kcalc/CMakePresets.json please delete it. Because it makes it really hard to use the correct CMake build configuration in VSCode. Deleting this file should be temporary. Be careful not to git commit this deleted file. E.g. do not stage to commit the deleted file CMakePresets.json.

Open the kcalc project (i.e. the git work directory, i.e. the directory where kdesrc-build has git cloned the git repository of kcalc) ~/kde/src/utilities/kcalc.

In VSCode, in the bottom status bar:

  • In the button "Click to select the current build variant" the CMake build configuration selected should be "Debug". I.e. the button should say "CMake: [Debug]: Ready".
  • Ignore the button "No active kit" with tooltip "Click to change the active kit". Or select the "gcc" from your Linux distribution.
  • In the right hand side there should be a button "Linux" with tooltip "C/C++ Configuration".
  • In the button "Set the default build target", select your preferred CMake target, in our case "kcalc". In the button "Select the target to launch" select one of the executables which are created using CMake targets, in our case "kcalc".
  • Ctrl+Shift+P > "CMake: Configure". It should say [proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_INSTALL_PREFIX:STRING=~/kde/usr -S~/kde/src/utilities/kcalc -B~/kde/build/utilities/kcalc -G "Unix Makefiles" and Build files have been written to: ~/kde/build/utilities/kcalc.
  • Press the button "Build" with gear icon with tooltip "Build the selected target: [kcalc]". It should say [proc] Executing command: /usr/bin/cmake --build ~/kde/build/utilities/kcalc --config.