Skip to content

feat: better association of .js to Source #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"type": "string",
"default": "https://frontend.cloud.heyzec.dedyn.io",
"description": "URL to the Source Academy frontend"
},
"source-academy.workspaceFolder": {
"type": "string",
"default": ".sourceacademy",
"description": "Location to store code locally. If not absolute, it will be relative to the home directory."
}
}
},
Expand Down
17 changes: 17 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { setupStatusBar } from "./statusbar/status";
import { registerAllCommands } from "./commands";
import { activateLspClient, deactivateLspClient } from "./lsp/client";
import { LanguageClient } from "vscode-languageclient/node";
import { canonicaliseLocation } from "./utils/misc";
import config from "./utils/config";

// TODO: Don't expose this object directly, create an interface via a wrapper class
export let client: LanguageClient;
Expand Down Expand Up @@ -34,6 +36,21 @@ export function activate(context: vscode.ExtensionContext) {
"assets",
"icon.svg",
);

// TODO: Prompt the user to make this folder the default, and then set back to the config store.

// Update user's workspace settings to associate .js to Source
const workspaceFolder = canonicaliseLocation(config.workspaceFolder);
if (
vscode.workspace.workspaceFolders
?.map((wf) => wf.uri.fsPath)
.includes(workspaceFolder)
) {
const workspaceConfig = vscode.workspace.getConfiguration();
workspaceConfig.update("files.associations", {
"*.js": "source",
});
}
}

// This method is called when your extension is deactivated
Expand Down
5 changes: 4 additions & 1 deletion src/utils/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ export default {
type: "string",
default: "https://frontend.cloud.heyzec.dedyn.io",
},
workspaceFolder: { type: "string", default: "" },
workspaceFolder: {
type: "string",
default: ".sourceacademy",
},
} satisfies Record<string, ConfigSchemaUnion>;
10 changes: 3 additions & 7 deletions src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as os from "os";
import config from "../utils/config";
import { VscWorkspaceLocation } from "./messages";
import path from "path";
import { canonicaliseLocation } from "./misc";

export class Editor {
editor?: vscode.TextEditor;
Expand Down Expand Up @@ -49,13 +50,7 @@ export class Editor {
self.assessmentName = assessmentName;
self.questionId = questionId;

let workspaceFolder = config.workspaceFolder;
if (!workspaceFolder) {
workspaceFolder = path.join(os.homedir(), ".sourceacademy");
// TODO: Prompt the user to make this folder the default, and then set back to the config store.
} else if (!path.isAbsolute(workspaceFolder)) {
workspaceFolder = path.join(os.homedir(), workspaceFolder);
}
const workspaceFolder = canonicaliseLocation(config.workspaceFolder);

const filePath = path.join(
workspaceFolder,
Expand Down Expand Up @@ -90,6 +85,7 @@ export class Editor {
preview: false,
viewColumn: vscode.ViewColumn.One,
});
vscode.languages.setTextDocumentLanguage(editor.document, "source");
editor.selection = new vscode.Selection(
editor.document.positionAt(0),
editor.document.positionAt(1),
Expand Down
9 changes: 9 additions & 0 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from "path";
import * as os from "os";

export function canonicaliseLocation(location: string) {
if (path.isAbsolute(location)) {
return location;
}
return path.join(os.homedir(), location);
}