Skip to content

Commit af85548

Browse files
committed
feat: work with Source chapter and code prepend
1 parent ba2b409 commit af85548

File tree

8 files changed

+70
-9
lines changed

8 files changed

+70
-9
lines changed

language-configuration.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
{ "open": "'", "close": "'" },
2424
{ "open": "\"", "close": "\"" }
2525
],
26+
"folding": {
27+
"markers": {
28+
"start": "// PREPEND -- DO NOT EDIT",
29+
"end": "// END PREPEND"
30+
}
31+
},
2632
"wordPattern": "[_$a-zA-Z][_$a-zA-Z0-9]*",
2733
"indentationRules": {
2834
"increaseIndentPattern": "^((?!\\/\\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"esbuild-plugin-fileloc": "^0.0.6",
108108
"esbuild-plugin-ignore": "^1.1.1",
109109
"esbuild-plugin-polyfill-node": "^0.3.0",
110+
"lodash": "^4.17.21",
110111
"react": "^19.0.0",
111112
"react-dom": "^19.0.0",
112113
"vscode-languageclient": "^9.0.1"

src/commands/showPanel.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { setWebviewContent } from "../utils/webview";
1212
import config from "../utils/config";
1313
import { Editor } from "../utils/editor";
1414
import { FRONTEND_ELEMENT_ID } from "../constants";
15+
import { client } from "../extension";
16+
import _ from "lodash";
1517

1618
let panel: vscode.WebviewPanel | null = null;
1719
// This needs to be a reference to active
@@ -21,6 +23,8 @@ export let activeEditor: Editor | null = null;
2123
const messageQueue: MessageType[] = [];
2224
let handling = false;
2325

26+
// TODO: Remove panel and handling message logic out of the commands/ directory
27+
2428
async function handleMessage(
2529
context: vscode.ExtensionContext,
2630
message: MessageType,
@@ -43,8 +47,21 @@ async function handleMessage(
4347
message.workspaceLocation,
4448
message.assessmentName,
4549
message.questionId,
50+
message.prepend,
4651
message.initialCode,
4752
);
53+
activeEditor.uri;
54+
const info = context.globalState.get("info") ?? {};
55+
if (activeEditor.uri) {
56+
// TODO: Write our own wrapper to set nested keys easily, removing lodash
57+
// @ts-ignore
58+
_.set(info, `["${activeEditor.uri}"].chapter`, message.chapter);
59+
const nPrependLines = message.prepend.split("\n").length;
60+
_.set(info, `["${activeEditor.uri}"].prepend`, nPrependLines);
61+
context.globalState.update("info", info);
62+
client.sendNotification("source/publishInfo", info);
63+
}
64+
4865
panel?.reveal(vscode.ViewColumn.Two);
4966
console.log(
5067
`EXTENSION: NewEditor: activeEditor set to ${activeEditor.assessmentName}_${activeEditor.questionId}`,

src/extension.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { setupStatusBar } from "./statusbar/status";
55
import { evalEditor } from "./commands/evalEditor";
66
import { registerAllCommands } from "./commands";
77
import { activateLspClient, deactivateLspClient } from "./lsp/client";
8+
import { LanguageClient } from "vscode-languageclient/node";
9+
10+
// TODO: Don't expose this object directly, create an interface via a wrapper class
11+
export let client: LanguageClient;
812

913
// This method is called when your extension is activated
1014
// Your extension is activated the very first time the command is executed
@@ -13,7 +17,14 @@ export function activate(context: vscode.ExtensionContext) {
1317

1418
context.subscriptions.push(setupStatusBar(context));
1519

16-
activateLspClient(context);
20+
client = activateLspClient(context);
21+
22+
// const info = {
23+
// "file:///home/heyzec/.sourceacademy/playground_1.js": { chapter: 4 },
24+
// };
25+
const info = context.globalState.get("info") ?? {};
26+
27+
client.sendNotification("source/publishInfo", info);
1728
}
1829

1930
// This method is called when your extension is deactivated

src/lsp/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function activateLspClient(context: ExtensionContext) {
5050

5151
// Start the client. This will also launch the server
5252
client.start();
53+
return client;
5354

5455
// TODO: Combine this functionality with existing language selector
5556
// context.subscriptions.push(

src/utils/editor.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class Editor {
1212
questionId: number;
1313
onChangeCallback?: (editor: Editor) => void;
1414
code: string | null = null;
15+
uri: string | null = null;
1516

1617
// For debugging purposes
1718
replaceTime: number = 0;
@@ -36,10 +37,12 @@ export class Editor {
3637
return this.editor?.document.getText();
3738
}
3839

40+
// TODO: This method is too loaded, it's not obvious it also shows the editor
3941
static async create(
4042
workspaceLocation: VscWorkspaceLocation,
4143
assessmentName: string,
4244
questionId: number,
45+
prepend: string = "",
4346
initialCode: string = "",
4447
): Promise<Editor> {
4548
const self = new Editor(workspaceLocation, assessmentName, questionId);
@@ -58,24 +61,37 @@ export class Editor {
5861
workspaceFolder,
5962
`${assessmentName}_${questionId}.js`,
6063
);
64+
65+
const uri = vscode.Uri.file(filePath);
66+
self.uri = uri.toString();
67+
68+
const contents = [
69+
"// PREPEND -- DO NOT EDIT",
70+
prepend,
71+
"// END PREPEND",
72+
initialCode,
73+
].join("\n");
74+
6175
await vscode.workspace.fs.readFile(vscode.Uri.file(filePath)).then(
6276
() => null,
6377
async () => {
6478
self.log(`Opening file failed, creating at ${filePath}`);
6579
await vscode.workspace.fs.writeFile(
66-
vscode.Uri.file(filePath),
67-
new TextEncoder().encode(initialCode),
80+
uri,
81+
new TextEncoder().encode(contents),
6882
);
6983
},
7084
);
7185

72-
const editor = await vscode.window.showTextDocument(
73-
vscode.Uri.file(filePath),
74-
{
75-
preview: false,
76-
viewColumn: vscode.ViewColumn.One,
77-
},
86+
const editor = await vscode.window.showTextDocument(uri, {
87+
preview: false,
88+
viewColumn: vscode.ViewColumn.One,
89+
});
90+
editor.selection = new vscode.Selection(
91+
editor.document.positionAt(0),
92+
editor.document.positionAt(1),
7893
);
94+
vscode.commands.executeCommand("editor.fold");
7995

8096
self.editor = editor;
8197
vscode.workspace.onDidChangeTextDocument(() => {

src/utils/messages.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ const Messages = createMessages({
2222
workspaceLocation: VscWorkspaceLocation,
2323
assessmentName: string,
2424
questionId: number,
25+
chapter: number,
26+
prepend: string,
2527
initialCode: string,
2628
) => ({
2729
workspaceLocation,
2830
assessmentName,
2931
questionId,
32+
chapter,
33+
prepend,
3034
initialCode,
3135
}),
3236
Text: (workspaceLocation: VscWorkspaceLocation, code: string) => ({

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,11 @@ lodash.once@^4.0.0:
12371237
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
12381238
integrity sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==
12391239

1240+
lodash@^4.17.21:
1241+
version "4.17.21"
1242+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
1243+
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
1244+
12401245
lru-cache@^11.0.0:
12411246
version "11.0.2"
12421247
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-11.0.2.tgz#fbd8e7cf8211f5e7e5d91905c415a3f55755ca39"

0 commit comments

Comments
 (0)