Skip to content

Commit

Permalink
Add image previews in hovers and fix various errors (#594)
Browse files Browse the repository at this point in the history
* Fix weak comparisons
* Fix errors when converting resource paths
* Fix type error in register_capabilities
* Add image previews on document link hover
* Add image previews to ExtResource hovers
  • Loading branch information
DaelonSuzuka authored Feb 21, 2024
1 parent 0c9c255 commit 51c89a6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
14 changes: 10 additions & 4 deletions src/providers/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ export class GDDocumentationProvider implements CustomReadonlyEditorProvider {

public register_capabilities(message: NotificationMessage) {
for (const gdclass of (message.params as GodotCapabilities).native_classes) {
this.classInfo[gdclass.name] = gdclass;
this.classInfo.set(gdclass.name, gdclass);
}
for (const gdclass of (message.params as GodotCapabilities).native_classes) {
for (const gdclass of this.classInfo.values()) {
if (gdclass.inherits) {
const extended_classes = this.classInfo[gdclass.inherits].extended_classes || [];
if (!this.classInfo.has(gdclass.inherits)) {
this.classInfo.set(gdclass.inherits, {
name: gdclass.inherits,
inherits: "",
});
}
const extended_classes = this.classInfo.get(gdclass.inherits).extended_classes || [];
extended_classes.push(gdclass.name);
this.classInfo[gdclass.inherits].extended_classes = extended_classes;
this.classInfo.get(gdclass.inherits).extended_classes = extended_classes;
}
}
this.ready = true;
Expand Down
24 changes: 19 additions & 5 deletions src/providers/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ export class GDHoverProvider implements HoverProvider {

const contents = new MarkdownString();
contents.appendMarkdown(links);
contents.appendMarkdown("---");
const uri = await convert_resource_path_to_uri(resource.path);
contents.appendMarkdown("\n---\n");
contents.appendCodeblock(definition, "gdresource");
if (resource.type === "Texture") {
contents.appendMarkdown("\n---\n");
contents.appendMarkdown(`<img src="${uri}" min-width=100px max-width=500px/>\n`);
contents.supportHtml = true;
contents.isTrusted = true;
}
if (resource.type === "Script") {
contents.appendMarkdown("---");
const uri = await convert_resource_path_to_uri(resource.path);
contents.appendMarkdown("\n---\n");
const text = (await vscode.workspace.openTextDocument(uri)).getText();
contents.appendCodeblock(text, "gdscript");
}
Expand Down Expand Up @@ -93,14 +99,22 @@ export class GDHoverProvider implements HoverProvider {
type = "gdscene";
} else if (link.endsWith(".tres")) {
type = "gdresource";
} else if (link.endsWith(".png") || link.endsWith(".svg")) {
type = "image";
} else {
return;
}

const uri = await convert_resource_path_to_uri(link);
const text = (await vscode.workspace.openTextDocument(uri)).getText();
const contents = new MarkdownString();
contents.appendCodeblock(text, type);
if (type === "image") {
contents.appendMarkdown(`<img src="${uri}" min-width=100px max-width=500px/>`);
contents.supportHtml = true;
contents.isTrusted = true;
} else {
const text = (await vscode.workspace.openTextDocument(uri)).getText();
contents.appendCodeblock(text, type);
}
const hover = new Hover(contents);
return hover;
}
Expand Down
41 changes: 27 additions & 14 deletions src/utils/project_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ let projectFile: string | undefined = undefined;

export async function get_project_dir(): Promise<string | undefined> {
let file = "";
if (vscode.workspace.workspaceFolders != undefined) {
if (vscode.workspace.workspaceFolders !== undefined) {
const files = await vscode.workspace.findFiles("**/project.godot");

if (files.length == 0) {
if (files.length === 0) {
return undefined;
} else if (files.length == 1) {
}
if (files.length === 1) {
file = files[0].fsPath;
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
return undefined;
Expand All @@ -34,6 +35,13 @@ export async function get_project_dir(): Promise<string | undefined> {
return projectDir;
}

export async function get_project_file(): Promise<string | undefined> {
if (projectDir === undefined || projectFile === undefined) {
await get_project_dir();
}
return projectFile;
}

let projectVersion: string | undefined = undefined;

export async function get_project_version(): Promise<string | undefined> {
Expand Down Expand Up @@ -66,25 +74,30 @@ export function find_project_file(start: string, depth: number = 20) {
// TODO: rename this, it's actually more like "find_parent_project_file"
// This function appears to be fast enough, but if speed is ever an issue,
// memoizing the result should be straightforward
if (start === ".") {
if (fs.existsSync("project.godot") && fs.statSync("project.godot").isFile()) {
return "project.godot";
}
return null;
}
const folder = path.dirname(start);
if (start == folder) {
if (start === folder) {
return null;
}
const projectFile = path.join(folder, "project.godot");
const projFile = path.join(folder, "project.godot");

if (fs.existsSync(projectFile) && fs.statSync(projectFile).isFile()) {
return projectFile;
} else {
if (depth === 0) {
return null;
}
return find_project_file(folder, depth - 1);
if (fs.existsSync(projFile) && fs.statSync(projFile).isFile()) {
return projFile;
}
if (depth === 0) {
return null;
}
return find_project_file(folder, depth - 1);
}

export async function convert_resource_path_to_uri(resPath: string): Promise<vscode.Uri | null> {
const dir = find_project_file(resPath).replace("project.godot", "");
return vscode.Uri.joinPath(vscode.Uri.file(dir), resPath.substring(6));
const dir = await get_project_dir();
return vscode.Uri.joinPath(vscode.Uri.file(dir), resPath.substring("res://".length));
}

type VERIFY_STATUS = "SUCCESS" | "WRONG_VERSION" | "INVALID_EXE";
Expand Down

0 comments on commit 51c89a6

Please sign in to comment.