forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Project views to python extension
- Loading branch information
1 parent
1c8b855
commit e38c056
Showing
11 changed files
with
471 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { Disposable } from 'vscode'; | ||
import { getPythonProjectView } from './projectsView'; | ||
|
||
export function registerProjectFeatures(disposables: Disposable[]): void { | ||
const projectView = getPythonProjectView(); | ||
disposables.push(projectView); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as path from 'path'; | ||
import { Event, EventEmitter, Uri, WorkspaceFolder } from 'vscode'; | ||
import { PythonProject, PythonProjectsApi, PythonProjectsChangedEvent } from './types'; | ||
import { getWorkspaceFolders } from '../common/vscodeApis/workspaceApis'; | ||
|
||
class PythonProjectsManagerImpl implements PythonProjectsApi { | ||
private _projects: Map<string, PythonProject> = new Map<string, PythonProject>(); | ||
|
||
private readonly _onDidChangePythonProjects = new EventEmitter<PythonProjectsChangedEvent>(); | ||
|
||
constructor(projects: readonly PythonProject[]) { | ||
projects.forEach((project) => this.addProject(project)); | ||
} | ||
|
||
addPythonProjects(...pythonProjects: PythonProject[]): void { | ||
const addedProjects: PythonProject[] = []; | ||
pythonProjects.forEach((project) => { | ||
if (this.addProject(project)) { | ||
addedProjects.push(project); | ||
} | ||
}); | ||
if (addedProjects.length > 0) { | ||
this._onDidChangePythonProjects.fire({ added: addedProjects, removed: [] }); | ||
} | ||
} | ||
|
||
removePythonProjects(...pythonProjects: PythonProject[]): boolean { | ||
const removedProjects: PythonProject[] = []; | ||
pythonProjects.forEach((project) => { | ||
if (this._projects.delete(project.uri.toString())) { | ||
removedProjects.push(project); | ||
} | ||
}); | ||
if (removedProjects.length > 0) { | ||
this._onDidChangePythonProjects.fire({ added: [], removed: removedProjects }); | ||
} | ||
return removedProjects.length > 0; | ||
} | ||
|
||
onDidChangePythonProjects: Event<PythonProjectsChangedEvent> = this._onDidChangePythonProjects.event; | ||
|
||
get pythonProjects(): readonly PythonProject[] { | ||
return Array.from(this._projects.values()); | ||
} | ||
|
||
getPythonProject(uri: Uri): PythonProject | undefined { | ||
const project: PythonProject | undefined = this._projects.get(uri.toString()); | ||
if (project) { | ||
return project; | ||
} | ||
// Check if given uri is a child of a project | ||
const projects = Array.from(this._projects.values()) | ||
.sort((a, b) => a.uri.fsPath.length - b.uri.fsPath.length) | ||
.reverse(); | ||
|
||
const normalizedUriPath = path.normalize(uri.fsPath); | ||
for (const p of projects) { | ||
const normalizedWorkspacePath = path.normalize(p.uri.fsPath); | ||
if (normalizedWorkspacePath === normalizedUriPath) { | ||
return p; | ||
} | ||
let parentPath = path.dirname(normalizedUriPath); | ||
while (parentPath !== path.dirname(parentPath)) { | ||
if (normalizedWorkspacePath === parentPath) { | ||
return p; | ||
} | ||
parentPath = path.dirname(parentPath); | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
private addProject(project: PythonProject): boolean { | ||
if (this._projects.has(project.uri.toString())) { | ||
return false; | ||
} | ||
this._projects.set(project.uri.toString(), project); | ||
return true; | ||
} | ||
} | ||
|
||
let _projectManager: PythonProjectsApi | undefined; | ||
export function getPythonProjectsApi(): PythonProjectsApi { | ||
if (!_projectManager) { | ||
const workspaces: readonly WorkspaceFolder[] = getWorkspaceFolders() ?? []; | ||
_projectManager = new PythonProjectsManagerImpl(workspaces.map((w) => ({ uri: w.uri, name: w.name }))); | ||
} | ||
return _projectManager; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { | ||
Disposable, | ||
Event, | ||
EventEmitter, | ||
ProviderResult, | ||
TreeDataProvider, | ||
TreeItem, | ||
TreeView, | ||
Uri, | ||
window, | ||
} from 'vscode'; | ||
import { getPythonProjectsApi } from './projectsManager'; | ||
import { PythonProject, PythonProjectsApi, PythonProjectsChangedEvent } from './types'; | ||
import { | ||
ProjectEnvironmentInfoViewItem, | ||
ProjectEnvironmentViewItem, | ||
ProjectNoEnvironmentViewItem, | ||
ProjectPackageViewItem, | ||
ProjectPackagesViewItem, | ||
ProjectViewItem, | ||
PythonProjectTreeItem, | ||
} from './viewItems/types'; | ||
|
||
const PROJECT_TREE_VIEW_ID = 'python-projects'; | ||
|
||
export interface PythonProjectsViewApi extends Disposable { | ||
revealEnvironment(uri: Uri | PythonProject): void; | ||
} | ||
|
||
class PythonProjectViewImpl implements PythonProjectsViewApi, TreeDataProvider<PythonProjectTreeItem> { | ||
private disposables: Disposable[] = []; | ||
|
||
private _treeView: TreeView<PythonProjectTreeItem> | undefined; | ||
|
||
private _treeDataChanged: EventEmitter<void | PythonProjectTreeItem | PythonProjectTreeItem[] | null | undefined>; | ||
|
||
private _projectViews: Map<string, ProjectViewItem> = new Map<string, ProjectViewItem>(); | ||
|
||
private _projectEnvironments: Map<string, ProjectEnvironmentViewItem> = new Map< | ||
string, | ||
ProjectEnvironmentViewItem | ||
>(); | ||
|
||
constructor(private readonly api: PythonProjectsApi) { | ||
this._treeView = window.createTreeView(PROJECT_TREE_VIEW_ID, { treeDataProvider: this }); | ||
this._treeDataChanged = new EventEmitter< | ||
void | PythonProjectTreeItem | PythonProjectTreeItem[] | null | undefined | ||
>(); | ||
this.onDidChangeTreeData = this._treeDataChanged.event; | ||
this.disposables.push( | ||
this._treeView, | ||
this._treeDataChanged, | ||
this.api.onDidChangePythonProjects((e) => { | ||
this.onDidChangePythonProjects(e); | ||
}), | ||
); | ||
|
||
this.api.pythonProjects.forEach((project) => { | ||
this.addProject(project); | ||
}); | ||
} | ||
|
||
onDidChangeTreeData?: Event<void | PythonProjectTreeItem | PythonProjectTreeItem[] | null | undefined>; | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
getTreeItem(element: PythonProjectTreeItem): TreeItem { | ||
return element.getTreeItem(); | ||
} | ||
|
||
getChildren(element?: PythonProjectTreeItem | undefined): ProviderResult<PythonProjectTreeItem[]> { | ||
if (element === undefined) { | ||
return Array.from(this._projectViews.values()); | ||
} | ||
|
||
if (element.kind === 'project') { | ||
const { project } = element as ProjectViewItem; | ||
const envItem = this._projectEnvironments.get(project.uri.toString()); | ||
return envItem ? [envItem] : [new ProjectNoEnvironmentViewItem(project)]; | ||
} | ||
|
||
if (element.kind === 'environment') { | ||
return [ | ||
new ProjectEnvironmentInfoViewItem('Environment Manager: '), | ||
new ProjectEnvironmentInfoViewItem('Package Manager: '), | ||
new ProjectEnvironmentInfoViewItem('Python: '), | ||
new ProjectPackagesViewItem(), | ||
]; | ||
} | ||
|
||
if (element.kind === 'packages') { | ||
return [new ProjectPackageViewItem('Not Supported')]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
getParent?(element: PythonProjectTreeItem): ProviderResult<PythonProjectTreeItem> { | ||
return element.parent; | ||
} | ||
|
||
dispose() { | ||
this.disposables.forEach((d) => d.dispose()); | ||
} | ||
|
||
revealEnvironment(uri: Uri | PythonProject): void { | ||
const project = uri instanceof Uri ? this.api.getPythonProject(uri) : uri; | ||
if (!project) { | ||
return; | ||
} | ||
const envItem = this._projectEnvironments.get(project.uri.toString()); | ||
if (envItem) { | ||
this._treeView?.reveal(envItem); | ||
} | ||
} | ||
|
||
private onDidChangePythonProjects(e: PythonProjectsChangedEvent) { | ||
e.added.forEach((project) => this.addProject(project)); | ||
e.removed.forEach((project) => this.removeProject(project)); | ||
this._treeDataChanged.fire(undefined); | ||
} | ||
|
||
private addProject(project: PythonProject) { | ||
const projectView = new ProjectViewItem(project); | ||
this._projectViews.set(project.uri.toString(), projectView); | ||
} | ||
|
||
private removeProject(project: PythonProject) { | ||
this._projectViews.delete(project.uri.toString()); | ||
} | ||
} | ||
|
||
let _projectViews: PythonProjectsViewApi | undefined; | ||
export function getPythonProjectView(): PythonProjectsViewApi { | ||
if (!_projectViews) { | ||
_projectViews = new PythonProjectViewImpl(getPythonProjectsApi()); | ||
} | ||
return _projectViews; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. |
Oops, something went wrong.