[VS Code] Integration of Clangd into Language Server to Improve Code Suggestion
Problem to solve
The development landscape, especially for those working with C and C++ languages, demands a sophisticated level of code analysis and suggestion quality. GitLab’s current language server implementation provides a base level of support but lacks the depth and efficiency in handling complex language constructs and providing contextually relevant code suggestions and error diagnostics. This gap can lead to decreased developer productivity and increased frustration, particularly for developers working on large-scale, performance-sensitive projects.
Proposal
To address the limitations of the current language server implementation for C and C++ developments, we propose the integration of clangd into GitLab's language server. Clangd is a highly regarded language server for C and C++, known for its precise code completion, diagnostics, and navigation capabilities.
The recent updates to clangd, notably version 0.1.29 released on July 12, 2024, have introduced enhancements that make it an even more compelling option for integration:
import * as vscode from 'vscode';
import type { ClangdExtension, ASTParams, ASTNode } from '@clangd/vscode-clangd';
const CLANGD_EXTENSION = 'llvm-vs-code-extensions.vscode-clangd';
const CLANGD_API_VERSION = 1;
const ASTRequestMethod = 'textDocument/ast';
const provideHover = async (document: vscode.TextDocument, position: vscode.Position, _token: vscode.CancellationToken): Promise<vscode.Hover | undefined> => {
const clangdExtension = vscode.extensions.getExtension<ClangdExtension>(CLANGD_EXTENSION);
if (clangdExtension) {
const api = (await clangdExtension.activate()).getApi(CLANGD_API_VERSION);
const textDocument = api.languageClient.code2ProtocolConverter.asTextDocumentIdentifier(document);
const range = api.languageClient.code2ProtocolConverter.asRange(new vscode.Range(position, position));
const params: ASTParams = { textDocument, range };
const ast: ASTNode | undefined = await api.languageClient.sendRequest(ASTRequestMethod, params);
if (!ast) {
return undefined;
}
return {
contents: [ast.kind]
};
}
};
vscode.languages.registerHoverProvider(['c', 'cpp'], { provideHover });
Further details
Clangd now exposes an API to other VSCode extensions, enabling custom requests to the clangd server (#575 (closed)). This feature opens new possibilities for deeper integration and functionality within GitLab’s environment.
Integrating clangd would bring several improvements to GitLab's IDE:
- Advanced Code Analysis and Suggestions: Leverage clangd's powerful engine for real-time code analysis and suggestions, significantly improving the development experience for C and C++ projects.
- Customizable Environment: Utilize clangd’s API and configuration options to customize and optimize the coding environment per project or developer preferences.
- Consistency Across Environments: Ensure a consistent coding experience across different IDEs by adopting a widely used and supported language server.
Links / references
Version 0.1.29: July 12, 2024
- vscode-clangd now exposes an API to other VSCode extensions, allowing them to make requests of their own to the clangd server #575 (closed)
- The predefined variable ${workspaceFolderBasename} is now recognized in settings values such as "clangd.arguments" #147 (closed)
- Bug fixes to inactive region highlighting
- Decorations are now cleared on clangd restart #600 (closed)
- Decorations are now updated when their settings are changed #613 (closed)