mirror of
https://github.com/godotengine/godot-vscode-plugin.git
synced 2026-01-04 10:09:58 +03:00
Add image previews in hovers and fix various errors (#594)
* 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
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -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> {
|
||||
@@ -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
|
||||
const folder = path.dirname(start);
|
||||
if (start == folder) {
|
||||
if (start === ".") {
|
||||
if (fs.existsSync("project.godot") && fs.statSync("project.godot").isFile()) {
|
||||
return "project.godot";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const projectFile = 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);
|
||||
const folder = path.dirname(start);
|
||||
if (start === folder) {
|
||||
return null;
|
||||
}
|
||||
const projFile = path.join(folder, "project.godot");
|
||||
|
||||
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";
|
||||
|
||||
Reference in New Issue
Block a user