Skip to content

Commit

Permalink
Add copy feature to call hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
nagi1999a authored and Bo-Yi Tsai committed Jan 7, 2025
1 parent dc1cfc0 commit 04fe2a6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
65 changes: 65 additions & 0 deletions extensions/references-view/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@
"command": "references-view.copyPath",
"title": "%cmd.references-view.copyPath%"
},
{
"command": "references-view.copyCallName",
"title": "%cmd.references-view.copyCallName%"
},
{
"command": "references-view.copyCallHierarchy",
"title": "%cmd.references-view.copyCallHierarchy%"
},
{
"command": "references-view.copyCallHierarchyShort",
"title": "%cmd.references-view.copyCallHierarchyShort%"
},
{
"command": "references-view.copyCallHierarchyReversed",
"title": "%cmd.references-view.copyCallHierarchyReversed%"
},
{
"command": "references-view.copyCallHierarchyReversedShort",
"title": "%cmd.references-view.copyCallHierarchyReversedShort%"
},
{
"command": "references-view.refind",
"title": "%cmd.references-view.refind%",
Expand Down Expand Up @@ -291,6 +311,31 @@
"group": "2@3",
"when": "view == references-view.tree && viewItem == file-item || view == references-view.tree && viewItem == reference-item"
},
{
"command": "references-view.copyCallName",
"group": "1",
"when": "view == references-view.tree && viewItem == call-item"
},
{
"command": "references-view.copyCallHierarchy",
"group": "1",
"when": "view == references-view.tree && viewItem == call-item"
},
{
"command": "references-view.copyCallHierarchyShort",
"group": "1",
"when": "view == references-view.tree && viewItem == call-item"
},
{
"command": "references-view.copyCallHierarchyReversed",
"group": "1",
"when": "view == references-view.tree && viewItem == call-item"
},
{
"command": "references-view.copyCallHierarchyReversedShort",
"group": "1",
"when": "view == references-view.tree && viewItem == call-item"
},
{
"command": "references-view.showOutgoingCalls",
"group": "1",
Expand Down Expand Up @@ -337,6 +382,26 @@
"command": "references-view.copyPath",
"when": "never"
},
{
"command": "references-view.copyCallName",
"when": "never"
},
{
"command": "references-view.copyCallHierarchy",
"when": "never"
},
{
"command": "references-view.copyCallHierarchyShort",
"when": "never"
},
{
"command": "references-view.copyCallHierarchyReversed",
"when": "never"
},
{
"command": "references-view.copyCallHierarchyReversedShort",
"when": "never"
},
{
"command": "references-view.refind",
"when": "never"
Expand Down
5 changes: 5 additions & 0 deletions extensions/references-view/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"cmd.references-view.showOutgoingCalls": "Show Outgoing Calls",
"cmd.references-view.showIncomingCalls": "Show Incoming Calls",
"cmd.references-view.removeCallItem": "Dismiss",
"cmd.references-view.copyCallName": "Copy",
"cmd.references-view.copyCallHierarchy": "Copy Hierarchy",
"cmd.references-view.copyCallHierarchyShort": "Copy Hierarchy (Short)",
"cmd.references-view.copyCallHierarchyReversed": "Copy Hierarchy (Reversed)",
"cmd.references-view.copyCallHierarchyReversedShort": "Copy Hierarchy (Reversed, Short)",
"cmd.references-view.next": "Go to Next Reference",
"cmd.references-view.prev": "Go to Previous Reference",
"cmd.references-view.showTypeHierarchy": "Show Type Hierarchy",
Expand Down
40 changes: 40 additions & 0 deletions extensions/references-view/src/calls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,50 @@ export function register(tree: SymbolsTree, context: vscode.ExtensionContext): v
vscode.commands.registerCommand('references-view.showCallHierarchy', showCallHierarchy),
vscode.commands.registerCommand('references-view.showOutgoingCalls', (item: CallItem | unknown) => setCallsDirection(CallsDirection.Outgoing, item)),
vscode.commands.registerCommand('references-view.showIncomingCalls', (item: CallItem | unknown) => setCallsDirection(CallsDirection.Incoming, item)),
vscode.commands.registerCommand('references-view.copyCallName', (item: CallItem | unknown) => copyCall(item, 'name')),
vscode.commands.registerCommand('references-view.copyCallHierarchy', (item: CallItem | unknown) => copyCall(item, 'hierarchy', false)),
vscode.commands.registerCommand('references-view.copyCallHierarchyShort', (item: CallItem | unknown) => copyCall(item, 'hierarchy', false, true)),
vscode.commands.registerCommand('references-view.copyCallHierarchyReversed', (item: CallItem | unknown) => copyCall(item, 'hierarchy', true, false)),
vscode.commands.registerCommand('references-view.copyCallHierarchyReversedShort', (item: CallItem | unknown) => copyCall(item, 'hierarchy', true, true)),
vscode.commands.registerCommand('references-view.removeCallItem', removeCallItem)
);
}

async function copyCall(item: CallItem | unknown, type: 'hierarchy' | 'name', reverse: boolean = false, short: boolean = false) {
let val = '';
if (item instanceof CallItem) {
if (type === 'hierarchy') {
const space = 2;
const prefix = '> ';
let hierarchy: Array<String> = new Array();
let anchor: CallItem | undefined = item;
let level = 0;
while (anchor !== undefined) {
let itemString = anchor.item.name;
if (!short) {
itemString += ` (${vscode.SymbolKind[anchor.item.kind]} @ ${vscode.workspace.asRelativePath(anchor.item.uri)})`;
}
hierarchy.push(itemString);
anchor = anchor.parent;
}
if (reverse) {
hierarchy = hierarchy.reverse();
}
while (hierarchy.length !== 0) {
val += `${' '.repeat(space * level)}${(level !== 0) ? prefix : ''}${hierarchy.pop()}\n`;
level++;
}
val = val.trim();
}
else if (type === 'name') {
val = item.item.name;
}
}
if (val) {
await vscode.env.clipboard.writeText(val);
}
}

function removeCallItem(item: CallItem | unknown): void {
if (item instanceof CallItem) {
item.remove();
Expand Down

0 comments on commit 04fe2a6

Please sign in to comment.