forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfactory.ts
More file actions
72 lines (68 loc) · 3.39 KB
/
factory.ts
File metadata and controls
72 lines (68 loc) · 3.39 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
67
68
69
70
71
72
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable } from 'inversify';
import { Uri } from 'vscode';
import * as path from 'path';
import { IInterpreterService } from '../../interpreter/contracts';
import { IServiceContainer } from '../../ioc/types';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { IWorkspaceService } from '../application/types';
import { IFileSystem } from '../platform/types';
import { TerminalService } from './service';
import { SynchronousTerminalService } from './syncTerminalService';
import { ITerminalService, ITerminalServiceFactory, TerminalCreationOptions } from './types';
import { ExternalTerminalService } from './externalTerminalService';
@injectable()
export class TerminalServiceFactory implements ITerminalServiceFactory {
private terminalServices: Map<string, TerminalService | ExternalTerminalService>;
constructor(
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
@inject(IFileSystem) private fs: IFileSystem,
@inject(IInterpreterService) private interpreterService: IInterpreterService,
) {
this.terminalServices = new Map<string, TerminalService>();
}
public getTerminalService(options: TerminalCreationOptions & { newTerminalPerFile?: boolean }): ITerminalService {
const resource = options?.resource;
const title = options?.title;
let terminalTitle = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python';
const interpreter = options?.interpreter;
const id = this.getTerminalId(terminalTitle, resource, interpreter, options.newTerminalPerFile);
if (!this.terminalServices.has(id)) {
if (resource && options.newTerminalPerFile) {
terminalTitle = `${terminalTitle}: ${path.basename(resource.fsPath).replace('.py', '')}`;
}
options.title = terminalTitle;
const terminalService = new ExternalTerminalService(this.serviceContainer, options);
// const terminalService = new TerminalService(this.serviceContainer, options);
this.terminalServices.set(id, terminalService);
}
// Decorate terminal service with the synchronous service.
return new SynchronousTerminalService(
this.fs,
this.interpreterService,
this.terminalServices.get(id)!,
interpreter,
);
}
public createTerminalService(resource?: Uri, title?: string): ITerminalService {
title = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python';
return new ExternalTerminalService(this.serviceContainer, { resource, title });
// return new TerminalService(this.serviceContainer, { resource, title });
}
private getTerminalId(
title: string,
resource?: Uri,
interpreter?: PythonEnvironment,
newTerminalPerFile?: boolean,
): string {
if (!resource && !interpreter) {
return title;
}
const workspaceFolder = this.serviceContainer
.get<IWorkspaceService>(IWorkspaceService)
.getWorkspaceFolder(resource || undefined);
const fileId = resource && newTerminalPerFile ? resource.fsPath : '';
return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}:${fileId}`;
}
}