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

Add support for "data flow into" hierarchies #17

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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 31 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "C/C++/ObjC language server supporting cross references, hierarchies, completion and semantic highlighting",
"author": "ccls-project",
"license": "MIT",
"version": "0.1.17",
"version": "0.1.18",
"publisher": "ccls-project",
"preview": true,
"engines": {
Expand Down Expand Up @@ -33,6 +33,11 @@
"id": "ccls.callHierarchy",
"name": "Call Hierarchy",
"when": "extension.ccls.callHierarchyVisible"
},
{
"id": "ccls.dataFlowInto",
"name": "Data Flow Hierarchy",
"when": "extension.ccls.dataFlowHierarchyVisible"
}
]
},
Expand All @@ -49,14 +54,19 @@
"group": "[email protected]"
},
{
"command": "ccls.vars",
"command": "ccls.dataFlowInto",
"when": "resourceLangId == cpp",
"group": "[email protected]"
},
{
"command": "ccls.base",
"command": "ccls.vars",
"when": "resourceLangId == cpp",
"group": "[email protected]"
},
{
"command": "ccls.base",
"when": "resourceLangId == cpp",
"group": "[email protected]"
}
],
"view/title": [
Expand All @@ -69,6 +79,11 @@
"command": "ccls.closeCallHierarchy",
"when": "view == ccls.callHierarchy",
"group": "navigation"
},
{
"command": "ccls.closeDataFlowHierarchy",
"when": "view == ccls.dataFlowInto",
"group": "navigation"
}
],
"view/item/context": [
Expand All @@ -86,6 +101,10 @@
"command": "ccls.closeCallHierarchy",
"when": "false"
},
{
"command": "ccls.closeDataFlowHierarchy",
"when": "false"
},
{
"command": "ccls.gotoForTreeView",
"when": "false"
Expand Down Expand Up @@ -126,6 +145,15 @@
"category": "ccls",
"command": "ccls.vars"
},
{
"title": "Show Data Flow Into",
"category": "ccls",
"command": "ccls.dataFlowInto"
},
{
"title": "Close",
"command": "ccls.closeDataFlowHierarchy"
},
{
"title": "Show Cross References",
"category": "ccls",
Expand Down
63 changes: 63 additions & 0 deletions src/dataFlowHierarchy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Event, EventEmitter, TreeDataProvider, TreeItem, TreeItemCollapsibleState, Range, Position, workspace } from 'vscode';
import { LanguageClient } from 'vscode-languageclient/lib/main';
import * as ls from 'vscode-languageserver-types';
import { parseUri } from './extension';

export class DataFlowHierarchyNode {
// These properties come directly from the language server.
id: number;
location: ls.Location;
children: DataFlowHierarchyNode[];
}

export class DataFlowHierarchyProvider implements TreeDataProvider<DataFlowHierarchyNode> {
root: DataFlowHierarchyNode;
readonly onDidChangeEmitter: EventEmitter<any> = new EventEmitter<any>();
readonly onDidChangeTreeData: Event<any> = this.onDidChangeEmitter.event;

constructor(
readonly languageClient: LanguageClient, readonly baseDark: string, readonly baseLight: string) { }

async getTreeItem(element: DataFlowHierarchyNode): Promise<TreeItem> {
let collapseState = TreeItemCollapsibleState.None;
if (element.children.length > 0)
collapseState = TreeItemCollapsibleState.Expanded;

let light = this.baseLight;
let dark = this.baseDark;

let parentFile = await workspace.openTextDocument(parseUri(element.location.uri));
let label = parentFile.getText(
new Range(
new Position(element.location.range.start.line, element.location.range.start.character),
new Position(element.location.range.end.line, element.location.range.end.character),
)
);
//let label = element.location.uri.toString();
if (element.location) {
const path = parseUri(element.location.uri).path;
const name = path.substr(path.lastIndexOf('/') + 1);
label += ` (${name}:${element.location.range.start.line + 1})`;
}

return {
collapsibleState: collapseState,
command: {
arguments: [element, element.children.length > 0],
command: 'ccls.hackGotoForTreeView',
title: 'Goto',
},
contextValue: 'cclsGoto',
iconPath: { light, dark },
label,
};
}

getChildren(element?: DataFlowHierarchyNode): DataFlowHierarchyNode[] | Thenable<DataFlowHierarchyNode[]> {
if (!this.root)
return [];
if (!element)
return [this.root];
return element.children;
}
}
33 changes: 32 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import * as ls from "vscode-languageserver-types";

import { CallHierarchyNode, CallHierarchyProvider } from "./callHierarchy";
import { CclsErrorHandler } from "./cclsErrorHandler";
import { DataFlowHierarchyNode, DataFlowHierarchyProvider } from "./dataFlowHierarchy";
import { InheritanceHierarchyNode, InheritanceHierarchyProvider } from "./inheritanceHierarchy";
import { jumpToUriAtPosition } from "./vscodeUtils";

Expand Down Expand Up @@ -711,6 +712,36 @@ export function activate(context: ExtensionContext) {
});
})();

// DataFlow Hierarchy
(() => {
const baseDark = context.asAbsolutePath(path.join('resources', 'base-dark.svg'));
const baseLight = context.asAbsolutePath(path.join('resources', 'base-light.svg'));
const dataFlowHierarchyProvider = new DataFlowHierarchyProvider(
languageClient, baseDark, baseLight);
window.registerTreeDataProvider('ccls.dataFlowInto', dataFlowHierarchyProvider);
commands.registerTextEditorCommand('ccls.dataFlowInto', (editor) => {
setContext('extension.ccls.dataFlowHierarchyVisible', true);
const position = editor.selection.active;
const uri = editor.document.uri;
languageClient
.sendRequest('$ccls/dataFlowInto', {
position,
textDocument: {
uri: uri.toString(),
},
})
.then((callNode: DataFlowHierarchyNode) => {
dataFlowHierarchyProvider.root = callNode;
dataFlowHierarchyProvider.onDidChangeEmitter.fire();
});
});
commands.registerCommand('ccls.closeDataFlowHierarchy', (e) => {
setContext('extension.ccls.dataFlowHierarchyVisible', false);
dataFlowHierarchyProvider.root = undefined;
dataFlowHierarchyProvider.onDidChangeEmitter.fire();
});
})();

// Common between tree views.
(() => {
commands.registerCommand(
Expand All @@ -729,7 +760,7 @@ export function activate(context: ExtensionContext) {
let lastGotoClickTime: number;
commands.registerCommand(
'ccls.hackGotoForTreeView',
(node: InheritanceHierarchyNode|CallHierarchyNode,
(node: InheritanceHierarchyNode|CallHierarchyNode|DataFlowHierarchyNode,
hasChildren: boolean) => {
if (!node.location)
return;
Expand Down