Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding option to follow symlinks when resolving symbols #707

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
"type": "boolean",
"default": true,
"description": "Enable hovers provided by the language server"
},
"clangd.followSymlinks": {
"type": "boolean",
"default": false,
"description": "Follow symlinks when resolving a symbol"
}
}
},
Expand Down
67 changes: 67 additions & 0 deletions src/clangd-context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import * as vscode from 'vscode';
import * as vscodelc from 'vscode-languageclient/node';

Expand Down Expand Up @@ -104,6 +105,11 @@ export class ClangdContext implements vscode.Disposable {
// We also mark the list as incomplete to force retrieving new rankings.
// See https://github.com/microsoft/language-server-protocol/issues/898
middleware: {
provideDeclaration: this.remapDefinitionSymlink.bind(this),
provideDefinition: this.remapDefinitionSymlink.bind(this),
provideTypeDefinition: this.remapDefinitionSymlink.bind(this),
provideImplementation: this.remapDefinitionSymlink.bind(this),

provideCompletionItem: async (document, position, context, token,
next) => {
if (!config.get<boolean>('enableCodeCompletion'))
Expand Down Expand Up @@ -206,4 +212,65 @@ export class ClangdContext implements vscode.Disposable {
this.client.stop();
this.subscriptions = []
}

private async remapDefinitionSymlink(
document: vscode.TextDocument, position: vscode.Position,
token: vscode.CancellationToken,
next: vscodelc.ProvideDeclarationSignature) {
let definition = await next(document, position, token);
if (!config.get<boolean>('followSymlinks')) {
return definition;
}
if (definition) {
if (Array.isArray(definition)) {
const res = await Promise.all(definition.map(async (e) => {
if ('targetUri' in e)
return e;
else {
if (e.uri.scheme != 'file')
return e;

const s =
await vscode.workspace.fs.stat(vscode.Uri.file(e.uri.path));
if (s.type & vscode.FileType.SymbolicLink) {
const p = await this.safeFollowSymLink(e.uri.fsPath)
return new vscode.Location(vscode.Uri.file(p), e.range)
}

return e;
}
})) as vscode.Declaration

return res;
} else {
if (definition.uri.scheme != 'file')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been preferable to use the vscode filesystem api here, but while it has functionality for checking if a file is a symlink, it doesn't actually have a function to follow it. Gating behind a file uri scheme should avoid situations where you've got some remote filesystem open and we can't actually follow the symlinks

return definition;

const s = await vscode.workspace.fs.stat(
vscode.Uri.file(definition.uri.path));
if (s.type & vscode.FileType.SymbolicLink) {
const p = await this.safeFollowSymLink(definition.uri.fsPath)
return new vscode.Location(vscode.Uri.file(p), definition.range)
}

return definition;
}
}
return definition;
}

/**
* Attempt to resolve a symlink, returning the path unchanged if unable
* @param linkPath file path to resolve
* @returns
*/
private async safeFollowSymLink(linkPath: string): Promise<string> {
return await new Promise(
(then, _) => {fs.readlink(linkPath, (err, resolvedPath) => {
if (err) {
then(linkPath);
}
then(resolvedPath);
})})
}
}