Skip to content

Commit 4fa67ac

Browse files
authored
Show project dependencies hierarchically (#1339)
* Show project dependencies hierarchically Currently the Package Dependencies view lists all dependencies in a flat list. This list not only includes those explicitly defined in the project's Package.swift, but also all the dependencies of dependencies. As such its difficult to tell at a glance how a dependency is included. It can also be confusing to see dependencies in the list that you did not explicitly add. Instead, show a top level list that is only the dependencies explicitly defined in Package.swift. Expanding one shows any child dependencies of the dependency. The files in the dependency's folder are still shown as well, just after any child deps.
1 parent e17311d commit 4fa67ac

File tree

17 files changed

+495
-297
lines changed

17 files changed

+495
-297
lines changed

assets/test/dependencies/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
targets: [
1313
.executableTarget(
1414
name: "dependencies",
15-
dependencies: [.product(name: "MarkdownLib", package: "swift-markdown")],
15+
dependencies: [.product(name: "Markdown", package: "swift-markdown")],
1616
path: "Sources"),
1717
]
1818
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import MarkdownLib
1+
import Markdown
22

33
print("Test Asset:(dependencies)")
44
print(a)

assets/test/identity-different/Package.resolved

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/test/identity-different/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ let package = Package(
1313
],
1414
dependencies: [
1515
// Dependencies declare other packages that this package depends on.
16-
.package(name: "cmark", url: "https://github.com/apple/swift-cmark.git", .branch("gfm")),
16+
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.2"),
1717
],
1818
targets: [
1919
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2020
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2121
.target(
2222
name: "identity-different",
23-
dependencies: ["cmark"]),
23+
dependencies: [.product(name: "Logging", package: "swift-log")]),
2424
]
2525
)

package.json

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@
129129
"icon": "$(refresh)",
130130
"category": "Swift"
131131
},
132+
{
133+
"command": "swift.flatDependenciesList",
134+
"title": "Flat Dependencies List View",
135+
"icon": "$(list-flat)",
136+
"category": "Swift"
137+
},
138+
{
139+
"command": "swift.nestedDependenciesList",
140+
"title": "Nested Dependencies List View",
141+
"icon": "$(list-tree)",
142+
"category": "Swift"
143+
},
132144
{
133145
"command": "swift.cleanBuild",
134146
"title": "Clean Build Folder",
@@ -793,6 +805,14 @@
793805
"command": "swift.openPackage",
794806
"when": "swift.hasPackage"
795807
},
808+
{
809+
"command": "swift.flatDependenciesList",
810+
"when": "false"
811+
},
812+
{
813+
"command": "swift.nestedDependenciesList",
814+
"when": "false"
815+
},
796816
{
797817
"command": "swift.useLocalDependency",
798818
"when": "false"
@@ -898,17 +918,27 @@
898918
{
899919
"command": "swift.updateDependencies",
900920
"when": "view == packageDependencies",
901-
"group": "navigation"
921+
"group": "navigation@1"
902922
},
903923
{
904924
"command": "swift.resolveDependencies",
905925
"when": "view == packageDependencies",
906-
"group": "navigation"
926+
"group": "navigation@2"
907927
},
908928
{
909929
"command": "swift.resetPackage",
910930
"when": "view == packageDependencies",
911-
"group": "navigation"
931+
"group": "navigation@3"
932+
},
933+
{
934+
"command": "swift.flatDependenciesList",
935+
"when": "view == packageDependencies && !swift.flatDependenciesList",
936+
"group": "navigation@4"
937+
},
938+
{
939+
"command": "swift.nestedDependenciesList",
940+
"when": "view == packageDependencies && swift.flatDependenciesList",
941+
"group": "navigation@5"
912942
}
913943
],
914944
"view/item/context": [

src/FolderContext.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ export class FolderContext implements vscode.Disposable {
128128
await this.swiftPackage.reloadPackageResolved();
129129
}
130130

131+
/** reload workspace-state.json for this folder */
132+
async reloadWorkspaceState() {
133+
await this.swiftPackage.reloadWorkspaceState();
134+
}
135+
131136
/** Load Swift Plugins and store in Package */
132137
async loadSwiftPlugins() {
133-
const plugins = await SwiftPackage.loadPlugins(
134-
this.folder,
135-
this.workspaceContext.toolchain
136-
);
137-
this.swiftPackage.plugins = plugins;
138+
await this.swiftPackage.loadSwiftPlugins(this.workspaceContext.toolchain);
138139
}
139140

140141
/**

src/PackageWatcher.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import * as vscode from "vscode";
1616
import { FolderContext } from "./FolderContext";
1717
import { FolderOperation, WorkspaceContext } from "./WorkspaceContext";
18+
import { BuildFlags } from "./toolchain/BuildFlags";
1819

1920
/**
2021
* Watches for changes to **Package.swift** and **Package.resolved**.
@@ -25,6 +26,7 @@ import { FolderOperation, WorkspaceContext } from "./WorkspaceContext";
2526
export class PackageWatcher {
2627
private packageFileWatcher?: vscode.FileSystemWatcher;
2728
private resolvedFileWatcher?: vscode.FileSystemWatcher;
29+
private workspaceStateFileWatcher?: vscode.FileSystemWatcher;
2830

2931
constructor(
3032
private folderContext: FolderContext,
@@ -38,6 +40,7 @@ export class PackageWatcher {
3840
install() {
3941
this.packageFileWatcher = this.createPackageFileWatcher();
4042
this.resolvedFileWatcher = this.createResolvedFileWatcher();
43+
this.workspaceStateFileWatcher = this.createWorkspaceStateFileWatcher();
4144
}
4245

4346
/**
@@ -47,6 +50,7 @@ export class PackageWatcher {
4750
dispose() {
4851
this.packageFileWatcher?.dispose();
4952
this.resolvedFileWatcher?.dispose();
53+
this.workspaceStateFileWatcher?.dispose();
5054
}
5155

5256
private createPackageFileWatcher(): vscode.FileSystemWatcher {
@@ -69,6 +73,20 @@ export class PackageWatcher {
6973
return watcher;
7074
}
7175

76+
private createWorkspaceStateFileWatcher(): vscode.FileSystemWatcher {
77+
const uri = vscode.Uri.joinPath(
78+
vscode.Uri.file(
79+
BuildFlags.buildDirectoryFromWorkspacePath(this.folderContext.folder.fsPath, true)
80+
),
81+
"workspace-state.json"
82+
);
83+
const watcher = vscode.workspace.createFileSystemWatcher(uri.fsPath);
84+
watcher.onDidCreate(async () => await this.handleWorkspaceStateChange());
85+
watcher.onDidChange(async () => await this.handleWorkspaceStateChange());
86+
watcher.onDidDelete(async () => await this.handleWorkspaceStateChange());
87+
return watcher;
88+
}
89+
7290
/**
7391
* Handles a create or change event for **Package.swift**.
7492
*
@@ -95,4 +113,14 @@ export class PackageWatcher {
95113
this.workspaceContext.fireEvent(this.folderContext, FolderOperation.resolvedUpdated);
96114
}
97115
}
116+
117+
/**
118+
* Handles a create or change event for **.build/workspace-state.json**.
119+
*
120+
* This will resolve any changes in the workspace-state.
121+
*/
122+
private async handleWorkspaceStateChange() {
123+
await this.folderContext.reloadWorkspaceState();
124+
this.workspaceContext.fireEvent(this.folderContext, FolderOperation.workspaceStateUpdated);
125+
}
98126
}

0 commit comments

Comments
 (0)