Skip to content

Commit

Permalink
fix(vscode:extension): use closure to pass actual index to the webvie…
Browse files Browse the repository at this point in the history
…w at the start of a preview command
  • Loading branch information
emil14 committed Nov 18, 2023
1 parent 5198edb commit ad5d9c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
31 changes: 17 additions & 14 deletions web/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import { getWebviewContent, sendMsgToWebview } from "./webview";
let lspClient: LanguageClient;

export async function activate(context: ExtensionContext) {
window.showInformationMessage(
"Neva module detected, start indexing workdir."
);
console.info("neva module detected, start indexing workdir");

// Run language server, initialize client and establish connection
lspClient = setupLsp(context);
Expand All @@ -27,10 +25,12 @@ export async function activate(context: ExtensionContext) {
window.showWarningMessage(message);
});

console.info("language-server started, client connection established");

// Listen to language server events and update current indexed module state
let initialIndex: unknown;
lspClient.onNotification("neva/workdir_indexed", (newIndex: unknown) => {
window.showInformationMessage("Workdir indexed.");
console.info("workdir has been successfully indexed");
initialIndex = newIndex;
});

Expand All @@ -40,13 +40,15 @@ export async function activate(context: ExtensionContext) {
"neva.openPreview",
getPreviewCommand(
context,
initialIndex, // note that initial index could be undefined in case langauge server hasn't indexed workdir yet
(f: GenericNotificationHandler) => {
lspClient.onNotification("neva/workdir_indexed", f);
() => initialIndex, // note that we must use closure so function call gets actual value and not undefined
(handler: GenericNotificationHandler) => {
lspClient.onNotification("neva/workdir_indexed", handler); // register webview update-function
}
)
)
);

console.info("preview command registered");
}

export function deactivate(): Thenable<void> | undefined {
Expand All @@ -55,22 +57,22 @@ export function deactivate(): Thenable<void> | undefined {

function getPreviewCommand(
context: ExtensionContext,
initialIndex: unknown,
getInitialIndex: () => unknown,
onWebviewCreated: (f: GenericNotificationHandler) => void
): () => void {
let panel: WebviewPanel | undefined;

return () => {
const initialIndex = getInitialIndex();

if (!window.activeTextEditor) {
window.showWarningMessage(
"You need to open neva file before calling this command."
);
window.showWarningMessage("You need to open neva file to open preview.");
return;
}

if (!initialIndex) {
window.showWarningMessage(
"Working directory is not indexed yet. Please wait for a little bit."
"Working directory is not indexed yet. Just wait for a little bit."
);
}

Expand All @@ -80,6 +82,7 @@ function getPreviewCommand(

if (panel) {
panel.reveal(column);
console.info("existing panel revealed");
return;
}

Expand All @@ -98,10 +101,8 @@ function getPreviewCommand(

panel.webview.html = getWebviewContent(panel.webview, context.extensionUri);

// send initial index to webview
sendMsgToWebview(panel, window.activeTextEditor!.document, initialIndex);

// subscribe to further language server updates
onWebviewCreated((update: unknown) => {
sendMsgToWebview(panel!, window.activeTextEditor!.document, update);
});
Expand All @@ -113,5 +114,7 @@ function getPreviewCommand(
null,
context.subscriptions
);

console.info("existing panel not found, new panel has been created");
};
}
4 changes: 2 additions & 2 deletions web/src/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export function getWebviewContent(webview: Webview, extensionUri: Uri) {
export function sendMsgToWebview(
panel: WebviewPanel,
document: TextDocument,
parsedProgram: any
parsedProgram: unknown
) {
panel.webview.postMessage({
workspaceUri: workspace.workspaceFolders![0].uri,
openedDocument: document,
programState: parsedProgram,
isDarkTheme: window.activeColorTheme.kind === ColorThemeKind.Dark,
});
console.log("vscode-neva: message to webview sent", parsedProgram);
console.log("vscode-neva: message to webview sent: ", parsedProgram);
}

export function getNonce() {
Expand Down

0 comments on commit ad5d9c1

Please sign in to comment.