Skip to content

Commit

Permalink
Merge pull request #188 from statelyai/mellson/sta-1816-send-all-chan…
Browse files Browse the repository at this point in the history
…ges-to-the-visual-editor

Changes made outside VS Code are now sent to the visual editor
  • Loading branch information
mellson authored Jul 27, 2022
2 parents ec058a2 + 5b4df7f commit b5ae7d9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-hats-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stately-vscode": minor
---

Changes made outside VS Code are now sent to the visual editor.
2 changes: 1 addition & 1 deletion apps/extension/client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function activate(context: vscode.ExtensionContext) {
);

initiateVisualizer(context, client, addXStateUpdateListener);
initiateEditor(context, client);
initiateEditor(context);
initiateTypegen(context, client, addXStateUpdateListener);

context.subscriptions.push(
Expand Down
28 changes: 19 additions & 9 deletions apps/extension/client/src/initiateEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import * as path from "path";
import * as vscode from "vscode";
import { ColorThemeKind } from "vscode";
import type { LanguageClient } from "vscode-languageclient/node";
import { MachineConfig } from "xstate";
import { getAuth, SignInResult } from "./auth";
import { getBaseUrl } from "./constants";
Expand All @@ -18,10 +17,7 @@ import { getWebviewContent } from "./getWebviewContent";
import { handleDefinitionUpdate } from "./handleDefinitionUpdate";
import { handleNodeSelected } from "./handleNodeSelected";

export const initiateEditor = (
context: vscode.ExtensionContext,
client: LanguageClient
) => {
export const initiateEditor = (context: vscode.ExtensionContext) => {
const baseUrl = getBaseUrl();

let currentPanel: vscode.WebviewPanel | undefined = undefined;
Expand Down Expand Up @@ -142,13 +138,27 @@ export const initiateEditor = (
}
};

let lastSentPathAndText: string | undefined;
context.subscriptions.push(
vscode.workspace.onDidSaveTextDocument(async (document) => {
// We use onDidChange over onDidSave to catch changes made to the document outside of VS Code
vscode.workspace.onDidChangeTextDocument(({ document }) => {
// Only send the text if it isn't dirty, which should be the case after a save
if (document.isDirty) return;

const text = document.getText();
const result = parseMachinesFromFile(text);

if (result.machines.length > 0) {
result.machines.forEach((machine, index) => {
/*
* If we already sent the text, don't send it again.
* We need this because onDidChangeTextDocument gets called multiple times on a save.
* In theory multiple documents could have the same text content, so we prepend the path to the text to make it unique.
*/
if (document.uri.path + text === lastSentPathAndText) return;

const parsed = parseMachinesFromFile(text);
if (parsed.machines.length > 0) {
lastSentPathAndText = document.uri.path + text;

parsed.machines.forEach((machine, index) => {
sendMessage({
type: "RECEIVE_CONFIG_UPDATE_FROM_VSCODE",
config: machine.toConfig({ hashInlineImplementations: true })!,
Expand Down

0 comments on commit b5ae7d9

Please sign in to comment.