Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit b4017ea

Browse files
committed
copy commands, history picker
1 parent 6495852 commit b4017ea

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

src/locations/index.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as vscode from 'vscode';
77
import { SymbolsTree } from '../tree';
8-
import { LocationTreeInput, ReferenceItem } from './model';
8+
import { FileItem, LocationsModel, LocationTreeInput, ReferenceItem } from './model';
99

1010
export function register(tree: SymbolsTree, context: vscode.ExtensionContext): void {
1111

@@ -28,6 +28,41 @@ export function register(tree: SymbolsTree, context: vscode.ExtensionContext): v
2828
preserveFocus
2929
});
3030
}
31-
})
31+
}),
32+
vscode.commands.registerCommand('references-view.copy', copyCommand),
33+
vscode.commands.registerCommand('references-view.copyAll', copyAllCommand),
34+
vscode.commands.registerCommand('references-view.copyPath', copyPathCommand),
3235
);
3336
}
37+
38+
const copyAllCommand = async (item: ReferenceItem | FileItem | unknown) => {
39+
if (item instanceof ReferenceItem) {
40+
copyCommand(item.file.model);
41+
} else if (item instanceof FileItem) {
42+
copyCommand(item.model);
43+
}
44+
};
45+
46+
const copyCommand = async (item: LocationsModel | ReferenceItem | FileItem | unknown) => {
47+
let val: string | undefined;
48+
if (item instanceof LocationsModel) {
49+
val = await item.asCopyText();
50+
} else if (item instanceof ReferenceItem) {
51+
val = await item.asCopyText();
52+
} else if (item instanceof FileItem) {
53+
val = await item.asCopyText();
54+
}
55+
if (val) {
56+
await vscode.env.clipboard.writeText(val);
57+
}
58+
};
59+
60+
const copyPathCommand = (item: FileItem | unknown) => {
61+
if (item instanceof FileItem) {
62+
if (item.uri.scheme === 'file') {
63+
vscode.env.clipboard.writeText(item.uri.fsPath);
64+
} else {
65+
vscode.env.clipboard.writeText(item.uri.toString(true));
66+
}
67+
}
68+
};

src/locations/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class LocationTreeInput implements SymbolTreeInput {
4545
}
4646
}
4747

48-
class LocationsModel implements SymbolItemNavigation<FileItem | ReferenceItem>, SymbolItemEditorHighlights<FileItem | ReferenceItem> {
48+
export class LocationsModel implements SymbolItemNavigation<FileItem | ReferenceItem>, SymbolItemEditorHighlights<FileItem | ReferenceItem> {
4949

5050
private _onDidChange = new vscode.EventEmitter<FileItem | ReferenceItem | undefined>();
5151
readonly onDidChangeTreeData = this._onDidChange.event;
@@ -292,7 +292,7 @@ class LocationsTreeDataProvider implements Required<vscode.TreeDataProvider<File
292292
}
293293
}
294294

295-
class FileItem {
295+
export class FileItem {
296296

297297
constructor(
298298
readonly uri: vscode.Uri,

src/tree.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,16 @@ class TreeDataProviderDelegate implements vscode.TreeDataProvider<undefined> {
180180
// --- history
181181

182182
class HistoryItem {
183+
184+
readonly description: string;
185+
183186
constructor(
184187
readonly word: string,
185188
readonly anchor: WordAnchor,
186189
readonly input: SymbolTreeInput,
187-
) { }
190+
) {
191+
this.description = `${vscode.workspace.asRelativePath(input.uri)}${input.title.toLocaleLowerCase()}`;
192+
}
188193
}
189194

190195
class TreeInputHistory implements vscode.TreeDataProvider<HistoryItem>{
@@ -221,7 +226,22 @@ class TreeInputHistory implements vscode.TreeDataProvider<HistoryItem>{
221226
const position = item.anchor.getPosition() ?? item.input.position;
222227
return vscode.commands.executeCommand('vscode.open', item.input.uri, { selection: new vscode.Range(position, position) });
223228
}
224-
})
229+
}),
230+
vscode.commands.registerCommand('references-view.pickFromHistory', async () => {
231+
interface HistoryPick extends vscode.QuickPickItem {
232+
item: HistoryItem;
233+
}
234+
const entries = await this.getChildren();
235+
const picks = entries.map(item => <HistoryPick>{
236+
label: item.word,
237+
description: item.description,
238+
item
239+
});
240+
const pick = await vscode.window.showQuickPick(picks, { placeHolder: 'Select previous reference search' });
241+
if (pick) {
242+
this._reRunHistoryItem(pick.item);
243+
}
244+
}),
225245
);
226246
}
227247

@@ -264,10 +284,10 @@ class TreeInputHistory implements vscode.TreeDataProvider<HistoryItem>{
264284

265285
// --- tree data provider
266286

267-
getTreeItem(element: HistoryItem): vscode.TreeItem {
268-
const result = new vscode.TreeItem(element.word);
269-
result.description = `${vscode.workspace.asRelativePath(element.input.uri)}${element.input.title.toLocaleLowerCase()}`;
270-
// result.command = { command: 'references-view.SHOW', arguments: [element], title: 'Rerun' };
287+
getTreeItem(item: HistoryItem): vscode.TreeItem {
288+
const result = new vscode.TreeItem(item.word);
289+
result.description = item.description;
290+
result.command = { command: '_references-view.showHistoryItem', arguments: [item], title: 'Rerun' };
271291
result.collapsibleState = vscode.TreeItemCollapsibleState.None;
272292
result.contextValue = 'history-item';
273293
return result;

0 commit comments

Comments
 (0)