|
1 | | -import * as vscode from 'vscode' |
2 | | -import * as nls from 'vscode-nls' |
3 | | -import { getConfiguration } from '../config/config' |
4 | | -import { extractDates } from '../extraction/extract' |
5 | | -import type { Telemetry } from '../telemetry/telemetry' |
6 | | -import type { Notifier } from '../ui/notifier' |
7 | | -import type { StatusBar } from '../ui/statusBar' |
8 | | -import { handleSafetyChecks } from '../utils/safety' |
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import * as nls from 'vscode-nls'; |
| 3 | +import { getConfiguration } from '../config/config'; |
| 4 | +import { extractDates } from '../extraction/extract'; |
| 5 | +import type { Telemetry } from '../telemetry/telemetry'; |
| 6 | +import type { Notifier } from '../ui/notifier'; |
| 7 | +import type { StatusBar } from '../ui/statusBar'; |
| 8 | +import { handleSafetyChecks } from '../utils/safety'; |
9 | 9 |
|
10 | | -const localize = nls.config({ messageFormat: nls.MessageFormat.file })() |
| 10 | +const localize = nls.config({ messageFormat: nls.MessageFormat.file })(); |
11 | 11 |
|
12 | 12 | export function registerExtractCommand( |
13 | | - context: vscode.ExtensionContext, |
14 | | - deps: Readonly<{ |
15 | | - telemetry: Telemetry |
16 | | - notifier: Notifier |
17 | | - statusBar: StatusBar |
18 | | - }>, |
| 13 | + context: vscode.ExtensionContext, |
| 14 | + deps: Readonly<{ |
| 15 | + telemetry: Telemetry; |
| 16 | + notifier: Notifier; |
| 17 | + statusBar: StatusBar; |
| 18 | + }>, |
19 | 19 | ): void { |
20 | | - const command = vscode.commands.registerCommand('dates-le.extractDates', async () => { |
21 | | - deps.telemetry.event('command-extract-dates') |
| 20 | + const command = vscode.commands.registerCommand( |
| 21 | + 'dates-le.extractDates', |
| 22 | + async () => { |
| 23 | + deps.telemetry.event('command-extract-dates'); |
22 | 24 |
|
23 | | - const editor = vscode.window.activeTextEditor |
24 | | - if (!editor) { |
25 | | - deps.notifier.showWarning(localize('runtime.extract.no-editor', 'No active editor found')) |
26 | | - return |
27 | | - } |
| 25 | + const editor = vscode.window.activeTextEditor; |
| 26 | + if (!editor) { |
| 27 | + deps.notifier.showWarning( |
| 28 | + localize('runtime.extract.no-editor', 'No active editor found'), |
| 29 | + ); |
| 30 | + return; |
| 31 | + } |
28 | 32 |
|
29 | | - const document = editor.document |
30 | | - const config = getConfiguration() |
| 33 | + const document = editor.document; |
| 34 | + const config = getConfiguration(); |
31 | 35 |
|
32 | | - // Safety checks |
33 | | - const safetyResult = handleSafetyChecks(document, config) |
34 | | - if (!safetyResult.proceed) { |
35 | | - deps.notifier.showWarning(safetyResult.message) |
36 | | - return |
37 | | - } |
| 36 | + // Safety checks |
| 37 | + const safetyResult = handleSafetyChecks(document, config); |
| 38 | + if (!safetyResult.proceed) { |
| 39 | + deps.notifier.showWarning(safetyResult.message); |
| 40 | + return; |
| 41 | + } |
38 | 42 |
|
39 | | - try { |
40 | | - deps.statusBar.showProgress(localize('runtime.extract.progress', 'Extracting dates...')) |
| 43 | + try { |
| 44 | + deps.statusBar.showProgress( |
| 45 | + localize('runtime.extract.progress', 'Extracting dates...'), |
| 46 | + ); |
41 | 47 |
|
42 | | - const result = await extractDates(document.getText(), document.languageId) |
| 48 | + const result = await extractDates( |
| 49 | + document.getText(), |
| 50 | + document.languageId, |
| 51 | + ); |
43 | 52 |
|
44 | | - if (!result.success) { |
45 | | - deps.notifier.showError( |
46 | | - localize( |
47 | | - 'runtime.extract.failed', |
48 | | - 'Failed to extract dates: {0}', |
49 | | - result.errors[0]?.message || 'Unknown error', |
50 | | - ), |
51 | | - ) |
52 | | - return |
53 | | - } |
| 53 | + if (!result.success) { |
| 54 | + deps.notifier.showError( |
| 55 | + localize( |
| 56 | + 'runtime.extract.failed', |
| 57 | + 'Failed to extract dates: {0}', |
| 58 | + result.errors[0]?.message || 'Unknown error', |
| 59 | + ), |
| 60 | + ); |
| 61 | + return; |
| 62 | + } |
54 | 63 |
|
55 | | - if (result.dates.length === 0) { |
56 | | - deps.notifier.showInfo( |
57 | | - localize('runtime.extract.no-dates', 'No dates found in the current document'), |
58 | | - ) |
59 | | - return |
60 | | - } |
| 64 | + if (result.dates.length === 0) { |
| 65 | + deps.notifier.showInfo( |
| 66 | + localize( |
| 67 | + 'runtime.extract.no-dates', |
| 68 | + 'No dates found in the current document', |
| 69 | + ), |
| 70 | + ); |
| 71 | + return; |
| 72 | + } |
61 | 73 |
|
62 | | - // Output dates in original format (one per line) |
63 | | - const dateValues = result.dates.map((date) => date.value) |
| 74 | + // Output dates in original format (one per line) |
| 75 | + const dateValues = result.dates.map((date) => date.value); |
64 | 76 |
|
65 | | - // Open results with enhanced error handling |
66 | | - try { |
67 | | - if (config.openResultsSideBySide) { |
68 | | - const doc = await vscode.workspace.openTextDocument({ |
69 | | - content: dateValues.join('\n'), |
70 | | - language: 'plaintext', |
71 | | - }) |
72 | | - await vscode.window.showTextDocument(doc, vscode.ViewColumn.Beside) |
73 | | - } else { |
74 | | - // Replace current selection or entire document |
75 | | - const edit = new vscode.WorkspaceEdit() |
76 | | - edit.replace( |
77 | | - document.uri, |
78 | | - new vscode.Range(0, 0, document.lineCount, 0), |
79 | | - dateValues.join('\n'), |
80 | | - ) |
81 | | - await vscode.workspace.applyEdit(edit) |
82 | | - } |
83 | | - } catch (error) { |
84 | | - deps.notifier.showError( |
85 | | - localize( |
86 | | - 'runtime.extract.open-failed', |
87 | | - 'Failed to open results: {0}', |
88 | | - error instanceof Error ? error.message : 'Unknown error', |
89 | | - ), |
90 | | - ) |
91 | | - return |
92 | | - } |
| 77 | + // Open results with enhanced error handling |
| 78 | + try { |
| 79 | + if (config.openResultsSideBySide) { |
| 80 | + const doc = await vscode.workspace.openTextDocument({ |
| 81 | + content: dateValues.join('\n'), |
| 82 | + language: 'plaintext', |
| 83 | + }); |
| 84 | + await vscode.window.showTextDocument(doc, vscode.ViewColumn.Beside); |
| 85 | + } else { |
| 86 | + // Replace current selection or entire document |
| 87 | + const edit = new vscode.WorkspaceEdit(); |
| 88 | + edit.replace( |
| 89 | + document.uri, |
| 90 | + new vscode.Range(0, 0, document.lineCount, 0), |
| 91 | + dateValues.join('\n'), |
| 92 | + ); |
| 93 | + await vscode.workspace.applyEdit(edit); |
| 94 | + } |
| 95 | + } catch (error) { |
| 96 | + deps.notifier.showError( |
| 97 | + localize( |
| 98 | + 'runtime.extract.open-failed', |
| 99 | + 'Failed to open results: {0}', |
| 100 | + error instanceof Error ? error.message : 'Unknown error', |
| 101 | + ), |
| 102 | + ); |
| 103 | + return; |
| 104 | + } |
93 | 105 |
|
94 | | - // Copy to clipboard if enabled |
95 | | - if (config.copyToClipboardEnabled) { |
96 | | - try { |
97 | | - const clipboardText = dateValues.join('\n') |
98 | | - // Check clipboard text length to prevent memory issues |
99 | | - if (clipboardText.length > 1000000) { |
100 | | - // 1MB limit |
101 | | - deps.notifier.showWarning( |
102 | | - localize( |
103 | | - 'runtime.extract.clipboard.too-large', |
104 | | - 'Results too large for clipboard ({0} characters), skipping clipboard copy', |
105 | | - clipboardText.length, |
106 | | - ), |
107 | | - ) |
108 | | - } else { |
109 | | - await vscode.env.clipboard.writeText(clipboardText) |
110 | | - deps.notifier.showInfo( |
111 | | - localize( |
112 | | - 'runtime.extract.success.clipboard', |
113 | | - 'Extracted {0} dates and copied to clipboard', |
114 | | - result.dates.length, |
115 | | - ), |
116 | | - ) |
117 | | - } |
118 | | - } catch (error) { |
119 | | - deps.notifier.showWarning( |
120 | | - localize( |
121 | | - 'runtime.extract.clipboard.failed', |
122 | | - 'Failed to copy to clipboard: {0}', |
123 | | - error instanceof Error ? error.message : 'Unknown error', |
124 | | - ), |
125 | | - ) |
126 | | - deps.notifier.showInfo( |
127 | | - localize('runtime.extract.success', 'Extracted {0} dates', result.dates.length), |
128 | | - ) |
129 | | - } |
130 | | - } else { |
131 | | - deps.notifier.showInfo( |
132 | | - localize('runtime.extract.success', 'Extracted {0} dates', result.dates.length), |
133 | | - ) |
134 | | - } |
| 106 | + // Copy to clipboard if enabled |
| 107 | + if (config.copyToClipboardEnabled) { |
| 108 | + try { |
| 109 | + const clipboardText = dateValues.join('\n'); |
| 110 | + // Check clipboard text length to prevent memory issues |
| 111 | + if (clipboardText.length > 1000000) { |
| 112 | + // 1MB limit |
| 113 | + deps.notifier.showWarning( |
| 114 | + localize( |
| 115 | + 'runtime.extract.clipboard.too-large', |
| 116 | + 'Results too large for clipboard ({0} characters), skipping clipboard copy', |
| 117 | + clipboardText.length, |
| 118 | + ), |
| 119 | + ); |
| 120 | + } else { |
| 121 | + await vscode.env.clipboard.writeText(clipboardText); |
| 122 | + deps.notifier.showInfo( |
| 123 | + localize( |
| 124 | + 'runtime.extract.success.clipboard', |
| 125 | + 'Extracted {0} dates and copied to clipboard', |
| 126 | + result.dates.length, |
| 127 | + ), |
| 128 | + ); |
| 129 | + } |
| 130 | + } catch (error) { |
| 131 | + deps.notifier.showWarning( |
| 132 | + localize( |
| 133 | + 'runtime.extract.clipboard.failed', |
| 134 | + 'Failed to copy to clipboard: {0}', |
| 135 | + error instanceof Error ? error.message : 'Unknown error', |
| 136 | + ), |
| 137 | + ); |
| 138 | + deps.notifier.showInfo( |
| 139 | + localize( |
| 140 | + 'runtime.extract.success', |
| 141 | + 'Extracted {0} dates', |
| 142 | + result.dates.length, |
| 143 | + ), |
| 144 | + ); |
| 145 | + } |
| 146 | + } else { |
| 147 | + deps.notifier.showInfo( |
| 148 | + localize( |
| 149 | + 'runtime.extract.success', |
| 150 | + 'Extracted {0} dates', |
| 151 | + result.dates.length, |
| 152 | + ), |
| 153 | + ); |
| 154 | + } |
135 | 155 |
|
136 | | - deps.telemetry.event('extract-success', { count: result.dates.length }) |
137 | | - } catch (error) { |
138 | | - const message = error instanceof Error ? error.message : 'Unknown error occurred' |
139 | | - deps.notifier.showError(`Extraction failed: ${message}`) |
140 | | - deps.telemetry.event('extract-error', { error: message }) |
141 | | - } finally { |
142 | | - deps.statusBar.hideProgress() |
143 | | - } |
144 | | - }) |
| 156 | + deps.telemetry.event('extract-success', { count: result.dates.length }); |
| 157 | + } catch (error) { |
| 158 | + const message = |
| 159 | + error instanceof Error ? error.message : 'Unknown error occurred'; |
| 160 | + deps.notifier.showError(`Extraction failed: ${message}`); |
| 161 | + deps.telemetry.event('extract-error', { error: message }); |
| 162 | + } finally { |
| 163 | + deps.statusBar.hideProgress(); |
| 164 | + } |
| 165 | + }, |
| 166 | + ); |
145 | 167 |
|
146 | | - context.subscriptions.push(command) |
| 168 | + context.subscriptions.push(command); |
147 | 169 | } |
0 commit comments