Skip to content

Commit

Permalink
Add remote uri support (oliverschwendener#1211)
Browse files Browse the repository at this point in the history
* Add remote uri support

* Use id in log

* Remove relative import
  • Loading branch information
IrishBruse authored Sep 19, 2024
1 parent 0998789 commit e8803eb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
65 changes: 45 additions & 20 deletions src/main/Extensions/VSCode/VSCodeExtension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AssetPathResolver } from "@Core/AssetPathResolver";
import type { Extension } from "@Core/Extension";
import type { FileImageGenerator } from "@Core/ImageGenerator";
import type { Logger } from "@Core/Logger";
import type { SettingsManager } from "@Core/SettingsManager";
import type { OperatingSystem, SearchResultItem } from "@common/Core";
import type { Image } from "@common/Core/Image";
Expand All @@ -17,6 +18,7 @@ type VscodeRecent = {
id: string;
configPath: string;
};
label?: string;
};

export class VSCodeExtension implements Extension {
Expand Down Expand Up @@ -44,20 +46,25 @@ export class VSCodeExtension implements Extension {
public constructor(
private readonly operatingSystem: OperatingSystem,
private readonly assetPathResolver: AssetPathResolver,
private readonly logger: Logger,
private readonly settingsManager: SettingsManager,
private readonly fileImageGenerator: FileImageGenerator,
) {}

async getSearchResultItems(): Promise<SearchResultItem[]> {
const recentPaths = this.getRecents();
const searchResults: SearchResultItem[] = [];

const searchItems = await Promise.all(
recentPaths.map((recent) => {
return this.getSearchItem(recent);
}),
);
for (const recent of this.getRecents()) {
try {
searchResults.push(await this.getSearchItem(recent));
} catch (error) {
const uri = this.getUri(recent);
this.logger.error(this.id + ": " + uri + " " + error);
}
}

this.recents = searchResults;

this.recents = searchItems;
return [];
}

Expand All @@ -74,17 +81,36 @@ export class VSCodeExtension implements Extension {
);
}

getUri = (recent: VscodeRecent) => recent.fileUri ?? recent.folderUri ?? recent.workspace.configPath;
getPath = (uri: string) => {
const decodedUri = decodeURIComponent(uri);
if (uri.startsWith("file://")) {
const url = new URL(decodedUri);
return Url.fileURLToPath(url, { windows: this.operatingSystem === "Windows" });
}
return decodedUri;
};

async getSearchItem(recent: VscodeRecent): Promise<SearchResultItem> {
const uri = recent.fileUri ?? recent.folderUri ?? recent.workspace.configPath;
const uri = this.getUri(recent);

let description: string;
let fileType: string;
let commandArg: string;

if (recent.fileUri) {
description = "File";
fileType = "File";
commandArg = "--file-uri";
} else if (recent.folderUri) {
description = "Folder";
fileType = "Folder";
commandArg = "--folder-uri";
} else if (recent.workspace) {
description = "Workspace";
fileType = "Workspace";
commandArg = "--file-uri";
}

// Overide description for remote
if (!uri.startsWith("file://")) {
fileType = "Remote " + fileType;
}

let img: Image | undefined;
Expand All @@ -98,24 +124,23 @@ export class VSCodeExtension implements Extension {
img = this.getImage();
}

const url = new URL(decodeURIComponent(uri));
const path = Url.fileURLToPath(url, { windows: this.operatingSystem === "Windows" });
const path = this.getPath(uri);

const template = this.settingsManager.getValue<string>(
`extension[${this.id}].command`,
this.getSettingDefaultValue("command"),
);

const command = template.replace("%s", path);
const command = template.replace("%s", `${commandArg} ${uri}`);

return {
id: "vscode-" + path,
name: Path.basename(path),
description,
id: "vscode-" + uri,
name: recent.label ?? Path.basename(path),
description: fileType,
image: img,
defaultAction: {
handlerId: "Commandline",
description: `Open ${description} in VSCode`,
description: `Open ${fileType} in VSCode`,
argument: command,
},
};
Expand Down Expand Up @@ -158,7 +183,7 @@ export class VSCodeExtension implements Extension {
prefixDescription:
"The prefix to trigger visual studio code. Open recently opened files and projects: <prefix> <command>",
command: "Command",
commandTooltip: "Use %s where the selected file/project should go in the command be sure to quote it",
commandTooltip: "Use %s where the selected file/project should go it uses the --file/folder-uri switch",
},
};
}
Expand Down
1 change: 1 addition & 0 deletions src/main/Extensions/VSCode/VSCodeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class VSCodeModule implements ExtensionModule {
extension: new VSCodeExtension(
dependencyRegistry.get("OperatingSystem"),
dependencyRegistry.get("AssetPathResolver"),
dependencyRegistry.get("Logger"),
dependencyRegistry.get("SettingsManager"),
dependencyRegistry.get("FileImageGenerator"),
),
Expand Down

0 comments on commit e8803eb

Please sign in to comment.