Generate VSCode configuration files
Visual Studio Code (VSCode) is a very popular IDE: a consistent favorite for years.
I propose we should automatically generate the necessary config files for VSCode to work properly with KDE projects.
Having these config files in place:
- Enables LSP:
- Documentation / definition popup
- Go to definition
- Autocomplete / IntelliSense
- Sets the build directory to the same one kdesrc-build uses
- Enables build / run / debug in the IDE
I see 2 clear advantages:
- VSCode users don't have to set up the same config for every project they work on
- Reduce the barrier to entry for potential new contributors
I think the second point is especially important, as a roadblock like non-functional LSP could make the difference for someone new considering contributing.
I poked around and tried my hand at implementing this:
our %defaultGlobalFlags = (
"delete-my-patches" => 0, # Should only be set from cmdline
"delete-my-settings" => 0, # Should only be set from cmdline
"disable-agent-check" => 0, # If true we don't check on ssh-agent
"disable-snapshots" => 1, # 2016-07-31 Temp. disabled until kde.org fixed to supply snapshots
"compile-commands-export" => 1, # 2021-02-06 allow to generate compile_commands.json via cmake, for clangd tooling
"compile-commands-linking" => 0, # 2021-02-06 link generated compile_commands.json back to the source directory
+ "generate-vscode-config" => 0, # Create a .vscode folder in the project with default configs to automatigally enable appropriate LSP / build location / etc.
"ignore-kde-structure" => 0, # Whether to use kde dir structure like extragear/network. Deprecated as of late December 2020/January 2021
"include-dependencies" => 0, # 2019-08-31 Made negatable from cmdline (NB: false here but true in rcfile)
"install-after-build" => 1,
"install-environment-driver" => 1, # Setup ~/.config/kde-env-*.sh for login scripts
"install-session-driver" => 0, # Above, + ~/.xsession
"purge-old-logs" => 1,
"run-tests" => 0, # 1 = make test, upload = make Experimental
"stop-on-failure" => 0,
"use-clean-install" => 0,
"use-idle-io-priority" => 0,
"use-stable-kde" => 0,
"use-inactive-modules" => 0,
);
sub generateVSCodeConfig
{
# Only proceed if the setting to generate VSCode config is present.
if (!$module->getOption('generate-vscode-config')) {
return;
}
my $builddir = $module->fullpath('build');
my $srcdir = $module->fullpath('source');
my $configDir = "$srcdir/.vscode";
# Only proceed if the .vscode directory doesn't already exist
# so we don't overwrite any custom settings the user may have.
if (-e $configDir) {
return;
}
# c_cpp_properties.json configures C++, CMake & IntelliSense.
my $cCppPropertiesJson = '{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "${default}",
"compileCommands": "${workspaceFolder}/compile_commands.json"
}
],
"version": 4
}
';
# settings.json has our builds going to the same location as kdesrc-build,
# so we don't have to re-compile when running or debugging from VSCode.
my $settingsJson = "{
\"cmake.buildDirectory\": \"$builddir\"
}
";
open my $cppConfigFile, '>', "$configDir/c_cpp_properties.json"
or _throw("Couldn't open new c_cpp_properties.json: $!");
print $cppConfigFile $cCppPropertiesJson
or _throw("Couldn't write to $configDir/c_cpp_properties.json: $!");
close $cppConfigFile
or _throw("Error closing $configDir/c_cpp_properties.json: $!");
open my $settingsFile, '>', "$configDir/settings.json"
or _throw("Couldn't open new settings.json: $!");
print $settingsFile $settingsJson
or _throw("Couldn't write to $configDir/settings.json: $!");
close $settingsFile
or _throw("Error closing $configDir/settings.json: $!");
return 1;
}
Something isn't right though, because all I get when run is:
Couldn't load the base platform for kdesrc-build!
More detail might be available in error-305523.log
No mention of where this log is, and I didn't find it poking around the src/build directories.
I am not familiar with Perl, so any assistance or suggestions on how to proceed would be greatly appreciated.