Open wss29 opened 2 months ago
Can you give an example of a feature you have in mind?
Is your feature's implementation going to be talking to the clangd server? If so, are you using a forked/modified clangd server that e.g. supports a new kind of LSP request?
Hi @HighCommander4 my feature needs to extract functions in the c/c++ file, so I need to talk to the clangd server, just like the internal ast feature but need some custom operation after getting ast node. currently, I just use the original clangd server, do not have new LSP request. maybe in the future, I need new LSP request
hi @HighCommander4 thanks for your replay. It seems that should work, but how could I use that API, I do not find a package of @clangd/vscode-clangd in npm. Should I wrap the files in the API folder into a module myself?
I do not find a package of @clangd/vscode-clangd in npm
Could you file an issue about publishing that package please?
In the meantime, you'll need to check out the vscode-clangd repo and refer to the package using a local path the way I do in this example repo.
Hi @HighCommander4 I have published an issue on 670, I have used your API it is ok except for a little problem with ast node. I use following code to get ast of a c file,
const ast: ASTNode | undefined = await api.languageClient.sendRequest(ASTType, { textDocument: { uri: args } });
the problem is that the above code only works when the file has opened in vscode, if I directly call and do not open the text document in vscode, I will get 'ResponseError: trying to get AST for non-added document ' is it possible not open file and get ast node of a c file just by pass uri directly?
is it possible not open file and get ast node of a c file just by pass uri directly?
Clangd does require that the file be opened on the server with textDocument/didOpen before any operations (such as requesting an AST) are performed on it.
However, opening the file on the server doesn't necessarily require opening it in the editor; I think you could use the client object exposed in the extension API to send the didOpen
notification from your extension's code.
hi @HighCommander4 I indeed try 'textDocument/didOpen', but when I send a request of 'textDocument/didOpen', I get error 'method not found' does that mean @clangd/vscode-clangd not support 'textDocument/didOpen'? and what is the value of the version should I set in TextDocumentItem
HI @HighCommander4 After fixing two problems. it can open file and get ast node of a c file just by pass uri directly
const clangdExtension = vscode.extensions.getExtension<ClangdExtension>(CLANGD_EXTENSION);
if (!clangdExtension) {
return undefined;
}
if (!clangdExtension.isActive) {
await clangdExtension.activate();
}
try {
const api = clangdExtension.exports.getApi(CLANGD_API_VERSION);
const file = vscode.Uri.parse(args).fsPath;
await fs.promises.access(file, fs.constants.F_OK);
const textContent = await fs.promises.readFile(file, 'utf8');
await api.languageClient.sendNotification('textDocument/didOpen', {
textDocument: { uri: args, languageId: 'c', version: 1, text: textContent },
});
const ast: ASTNode | undefined = await api.languageClient.sendRequest(ASTType, { textDocument: { uri: args } });
if (!ast || !ast.children) {
return [];
}
const ret = ast.children
.filter(o => o.kind === 'Function' || o.kind === 'Method')
.map(o =>
JSON.stringify({
role: o.role,
kind: o.kind,
detail: o.detail,
range: o.range,
})
);
return ret;
} catch (error) {
console.log(error);
}
Glad you figured it out. For reference, the standard Language Server Protocol messages and notifications (such as didOpen
) are documented at https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/.
Note: we mostly implement features in the clangd language server, and rely on Microsoft's LSP client framework to expose these in VSCode. Features requiring a lot of VSCode-specific work are unlikely to be implemented. Hi,
I try to create my clanged context like clangd, but when I install my extension and vscode-clangd I will get Error: command 'clangd.applyFix' already exists. is it possible to install these two extension meanwhile?