|
| 1 | +import * as path from 'path'; |
1 | 2 | import * as vscode from 'vscode'; |
2 | 3 | import { LineCounter } from './services/LineCounter'; |
3 | 4 | import { GitignoreService } from './services/GitignoreService'; |
4 | 5 | import { WorkspaceLineCounter } from './services/WorkspaceLineCounter'; |
5 | 6 | import { LineCounterByFileFormat } from './services/LineCounterByFileFormat'; |
| 7 | +import { StatsTreeProvider } from './views/StatsTreeProvider'; |
| 8 | +import { StatsService } from './services/StatsService'; |
6 | 9 |
|
7 | 10 | export function activate(context: vscode.ExtensionContext) { |
8 | 11 | const lineCounter = new LineCounter(); |
9 | 12 | const gitignoreService = new GitignoreService(); |
10 | 13 | const workspaceLineCounter = new WorkspaceLineCounter(lineCounter, gitignoreService); |
11 | 14 | const lineCounterByFileFormat = new LineCounterByFileFormat(workspaceLineCounter); |
| 15 | + const statsService = new StatsService(lineCounter, gitignoreService); |
| 16 | + const statsProvider = new StatsTreeProvider(statsService); |
12 | 17 |
|
13 | 18 | const fileline = vscode.commands.registerCommand('codecount.countLines', () => { |
14 | 19 | const editor = vscode.window.activeTextEditor; |
@@ -66,10 +71,87 @@ export function activate(context: vscode.ExtensionContext) { |
66 | 71 | ); |
67 | 72 | }); |
68 | 73 |
|
| 74 | + const refreshStats = vscode.commands.registerCommand('codecount.refreshStats', () => { |
| 75 | + statsProvider.refresh(); |
| 76 | + }); |
| 77 | + |
| 78 | + const gitRepoMissing = vscode.commands.registerCommand('codecount.gitRepoMissing', () => { |
| 79 | + vscode.window.showWarningMessage('Git repo hasn’t been defined.'); |
| 80 | + }); |
| 81 | + |
| 82 | + const openContributorFile = vscode.commands.registerCommand( |
| 83 | + 'codecount.openContributorFile', |
| 84 | + async (args?: { author?: string; filePath?: string }) => { |
| 85 | + const author = args?.author; |
| 86 | + const filePath = args?.filePath; |
| 87 | + if (!author || !filePath) { |
| 88 | + return; |
| 89 | + } |
| 90 | + const rootPath = await statsService.getGitRootPath(); |
| 91 | + if (!rootPath) { |
| 92 | + vscode.window.showWarningMessage('Git repository not found.'); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const absolutePath = vscode.Uri.file(path.join(rootPath, filePath)); |
| 97 | + try { |
| 98 | + await vscode.workspace.fs.stat(absolutePath); |
| 99 | + const document = await vscode.workspace.openTextDocument(absolutePath); |
| 100 | + await vscode.window.showTextDocument(document, { preview: false }); |
| 101 | + } catch { |
| 102 | + vscode.window.showWarningMessage('File not found in workspace.'); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const history = await statsService.getContributorFileHistory(author, filePath); |
| 107 | + if (!history.available) { |
| 108 | + vscode.window.showWarningMessage('Git repository not found.'); |
| 109 | + return; |
| 110 | + } |
| 111 | + if (history.entries.length === 0) { |
| 112 | + vscode.window.showInformationMessage('No history found for this contributor.'); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const pick = await vscode.window.showQuickPick( |
| 117 | + history.entries.map((entry) => ({ |
| 118 | + label: `${entry.date} ${entry.hash}`, |
| 119 | + hash: entry.hash |
| 120 | + })), |
| 121 | + { |
| 122 | + placeHolder: 'Select a commit to view changes' |
| 123 | + } |
| 124 | + ); |
| 125 | + if (!pick) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + const gitPath = path.join(rootPath, filePath); |
| 130 | + const encode = (ref: string) => |
| 131 | + vscode.Uri.parse( |
| 132 | + `git:${gitPath}?${encodeURIComponent(JSON.stringify({ path: gitPath, ref }))}` |
| 133 | + ); |
| 134 | + |
| 135 | + const left = encode(`${pick.hash}^`); |
| 136 | + const right = encode(pick.hash); |
| 137 | + await vscode.commands.executeCommand( |
| 138 | + 'vscode.diff', |
| 139 | + left, |
| 140 | + right, |
| 141 | + `Changes by ${author}: ${filePath} (${pick.hash})` |
| 142 | + ); |
| 143 | + } |
| 144 | + ); |
| 145 | + |
69 | 146 | context.subscriptions.push(fileline); |
70 | 147 | context.subscriptions.push(fileslines); |
71 | 148 | context.subscriptions.push(fileslinesByExtension); |
| 149 | + context.subscriptions.push(refreshStats); |
| 150 | + context.subscriptions.push(gitRepoMissing); |
| 151 | + context.subscriptions.push(openContributorFile); |
| 152 | + context.subscriptions.push( |
| 153 | + vscode.window.registerTreeDataProvider('codecount.stats', statsProvider) |
| 154 | + ); |
72 | 155 | } |
73 | 156 |
|
74 | | -// This method is called when your extension is deactivated |
75 | 157 | export function deactivate() {} |
0 commit comments