-
Couldn't load subscription status.
- Fork 145
Add support for multi-root workspaces #810
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
Draft
DaanDeMeyer
wants to merge
1
commit into
clangd:master
Choose a base branch
from
DaanDeMeyer:multiroot
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,70 +3,26 @@ | |
| import * as vscode from 'vscode'; | ||
| import * as vscodelc from 'vscode-languageclient/node'; | ||
|
|
||
| import {ClangdContext} from './clangd-context'; | ||
| import {ClangdContext, ClangdLanguageClient} from './clangd-context'; | ||
| import type {ASTParams, ASTNode} from '../api/vscode-clangd'; | ||
|
|
||
| const ASTRequestMethod = 'textDocument/ast'; | ||
|
|
||
| export function activate(context: ClangdContext) { | ||
| const feature = new ASTFeature(context); | ||
| context.client.registerFeature(feature); | ||
| } | ||
|
|
||
| const ASTRequestType = | ||
| new vscodelc.RequestType<ASTParams, ASTNode|null, void>(ASTRequestMethod); | ||
|
|
||
| class ASTFeature implements vscodelc.StaticFeature { | ||
| constructor(private context: ClangdContext) { | ||
| // The adapter holds the currently inspected node. | ||
| const adapter = new TreeAdapter(); | ||
| // Create the AST view, showing data from the adapter. | ||
| const tree = | ||
| vscode.window.createTreeView('clangd.ast', {treeDataProvider: adapter}); | ||
| context.subscriptions.push( | ||
| tree, | ||
| // Ensure the AST view is visible exactly when the adapter has a node. | ||
| // clangd.ast.hasData controls the view visibility (package.json). | ||
| adapter.onDidChangeTreeData((_) => { | ||
| vscode.commands.executeCommand('setContext', 'clangd.ast.hasData', | ||
| adapter.hasRoot()); | ||
| // Work around https://github.com/microsoft/vscode/issues/90005 | ||
| // Show the AST tree even if it's been collapsed or closed. | ||
| // reveal(root) fails here: "Data tree node not found". | ||
| if (adapter.hasRoot()) | ||
| // @ts-ignore | ||
| tree.reveal(null); | ||
| }), | ||
| // Create the "Show AST" command for the context menu. | ||
| // It's only shown if the feature is dynamicaly available (package.json) | ||
| vscode.commands.registerTextEditorCommand( | ||
| 'clangd.ast', | ||
| async (editor, _edit) => { | ||
| const converter = this.context.client.code2ProtocolConverter; | ||
| const item = | ||
| await this.context.client.sendRequest(ASTRequestType, { | ||
| textDocument: | ||
| converter.asTextDocumentIdentifier(editor.document), | ||
| range: converter.asRange(editor.selection), | ||
| }); | ||
| if (!item) | ||
| vscode.window.showInformationMessage( | ||
| 'No AST node at selection'); | ||
| adapter.setRoot(item ?? undefined, editor.document.uri); | ||
| }), | ||
| // Clicking "close" will empty the adapter, which in turn hides the | ||
| // view. | ||
| vscode.commands.registerCommand( | ||
| 'clangd.ast.close', () => adapter.setRoot(undefined, undefined))); | ||
| export class ASTFeature implements vscodelc.StaticFeature { | ||
| constructor(client: ClangdLanguageClient) { | ||
| client.registerFeature(this); | ||
| } | ||
|
|
||
| fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {} | ||
|
|
||
| // The "Show AST" command is enabled if the server advertises the capability. | ||
| initialize(capabilities: vscodelc.ServerCapabilities, | ||
| _documentSelector: vscodelc.DocumentSelector|undefined) { | ||
| vscode.commands.executeCommand('setContext', 'clangd.ast.supported', | ||
| 'astProvider' in capabilities); | ||
| if ('astProvider' in capabilities) | ||
| vscode.commands.executeCommand('setContext', 'clangd.ast.supported', true); | ||
| } | ||
| getState(): vscodelc.FeatureState { return {kind: 'static'}; } | ||
| clear() {} | ||
|
|
@@ -108,10 +64,55 @@ function describe(role: string, kind: string): string { | |
| } | ||
|
|
||
| // Map a root ASTNode onto a VSCode tree. | ||
| class TreeAdapter implements vscode.TreeDataProvider<ASTNode> { | ||
| export class ASTProvider implements vscode.TreeDataProvider<ASTNode> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do these changes have to do with Multi-Root? I seem to be missing some context |
||
| private root?: ASTNode; | ||
| private doc?: vscode.Uri; | ||
|
|
||
| constructor(context: ClangdContext) { | ||
| // Create the AST view, showing data from the adapter. | ||
| const tree = | ||
| vscode.window.createTreeView('clangd.ast', {treeDataProvider: this}); | ||
| context.subscriptions.push( | ||
| tree, | ||
| // Ensure the AST view is visible exactly when the adapter has a node. | ||
| // clangd.ast.hasData controls the view visibility (package.json). | ||
| this.onDidChangeTreeData((_) => { | ||
| vscode.commands.executeCommand('setContext', 'clangd.ast.hasData', | ||
| this.hasRoot()); | ||
| // Work around https://github.com/microsoft/vscode/issues/90005 | ||
| // Show the AST tree even if it's been collapsed or closed. | ||
| // reveal(root) fails here: "Data tree node not found". | ||
| if (this.hasRoot()) | ||
| // @ts-ignore | ||
| tree.reveal(null); | ||
| }), | ||
| // Create the "Show AST" command for the context menu. | ||
| // It's only shown if the feature is dynamicaly available (package.json) | ||
| vscode.commands.registerTextEditorCommand( | ||
| 'clangd.ast', | ||
| async (editor, _edit) => { | ||
| const client = context.getActiveClient(); | ||
| if (client === undefined) | ||
| return; | ||
|
|
||
| const converter = client.code2ProtocolConverter; | ||
| const item = | ||
| await client.sendRequest(ASTRequestType, { | ||
| textDocument: | ||
| converter.asTextDocumentIdentifier(editor.document), | ||
| range: converter.asRange(editor.selection), | ||
| }); | ||
| if (!item) | ||
| vscode.window.showInformationMessage( | ||
| 'No AST node at selection'); | ||
| this.setRoot(item ?? undefined, editor.document.uri); | ||
| }), | ||
| // Clicking "close" will empty the adapter, which in turn hides the | ||
| // view. | ||
| vscode.commands.registerCommand( | ||
| 'clangd.ast.close', () => this.setRoot(undefined, undefined))); | ||
| } | ||
|
|
||
| hasRoot(): boolean { return this.root !== undefined; } | ||
|
|
||
| setRoot(newRoot: ASTNode|undefined, newDoc: vscode.Uri|undefined) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we print out a warning when API1 is used in combination with multiple roots?