forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportFileOpener.ts
More file actions
66 lines (59 loc) · 2.6 KB
/
exportFileOpener.ts
File metadata and controls
66 lines (59 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { inject, injectable } from 'inversify';
import { Position, Uri } from 'vscode';
import { IApplicationShell, IDocumentManager } from '../../common/application/types';
import { PYTHON_LANGUAGE } from '../../common/constants';
import { IBrowserService } from '../../common/types';
import * as localize from '../../common/utils/localize';
import { sendTelemetryEvent } from '../../telemetry';
import { Telemetry } from '../constants';
import { IDataScienceFileSystem } from '../types';
import { ExportFormat } from './types';
@injectable()
export class ExportFileOpener {
constructor(
@inject(IDocumentManager) protected readonly documentManager: IDocumentManager,
@inject(IDataScienceFileSystem) private readonly fs: IDataScienceFileSystem,
@inject(IApplicationShell) private readonly applicationShell: IApplicationShell,
@inject(IBrowserService) private readonly browserService: IBrowserService
) {}
public async openFile(format: ExportFormat, uri: Uri) {
if (format === ExportFormat.python) {
await this.openPythonFile(uri);
sendTelemetryEvent(Telemetry.ExportNotebookAs, undefined, {
format: format,
successful: true,
opened: true
});
} else {
const opened = await this.askOpenFile(uri);
sendTelemetryEvent(Telemetry.ExportNotebookAs, undefined, {
format: format,
successful: true,
opened: opened
});
}
}
private async openPythonFile(uri: Uri): Promise<void> {
const contents = await this.fs.readFile(uri);
await this.fs.delete(uri);
const doc = await this.documentManager.openTextDocument({ language: PYTHON_LANGUAGE, content: contents });
const editor = await this.documentManager.showTextDocument(doc);
// Edit the document so that it is dirty (add a space at the end)
editor.edit((editBuilder) => {
editBuilder.insert(new Position(editor.document.lineCount, 0), '\n');
});
}
private async askOpenFile(uri: Uri): Promise<boolean> {
const yes = localize.DataScience.openExportFileYes();
const no = localize.DataScience.openExportFileNo();
const items = [yes, no];
const selected = await this.applicationShell
.showInformationMessage(localize.DataScience.openExportedFileMessage(), ...items)
.then((item) => item);
if (selected === yes) {
this.browserService.launch(uri.toString());
return true;
}
return false;
}
}