Skip to content

Commit

Permalink
Prepare test setup for text edits
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Feb 6, 2024
1 parent fc8e656 commit ab4d742
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 41 deletions.
36 changes: 23 additions & 13 deletions new-packages/language-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,25 @@ connection.onInitialize((params) => {
return [];
}

const fileName = server.env.uriToFileName(textDocument.uri);

// TODO: a range is returned here regardless of the extraction status (extraction could error)
// DX has to account for this somehow or results with errors have to be ignored (this would be slower but it might be a good tradeoff)
return xstateProject
.findMachines(server.env.uriToFileName(textDocument.uri))
.map((range, index) => ({
command: {
title: 'Open Visual Editor',
command: 'stately-xstate/edit-machine',
arguments: [textDocument.uri, index],
},
range,
}));
.findMachines(fileName)
.map((range, index) => {
return {
command: {
title: 'Open Visual Editor',
command: 'stately-xstate/edit-machine',
arguments: [textDocument.uri, index],
},
range: xstateProject.getLinesAndCharactersRange(
fileName,
range,
),
};
});
},
};
},
Expand Down Expand Up @@ -94,10 +101,13 @@ connection.onRequest(applyPatches, async ({ uri, machineIndex, patches }) => {
patches,
});

return edits.map(({ fileName, ...rest }) => ({
...rest,
uri: server.env.fileNameToUri(fileName),
}));
return edits.map(({ fileName, range, ...rest }) => {
return {
...rest,
uri: server.env.fileNameToUri(fileName),
range: xstateProject.getLinesAndCharactersRange(fileName, range),
};
});
});

connection.onInitialized(() => {
Expand Down
9 changes: 7 additions & 2 deletions new-packages/language-server/src/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { ExtractorDigraphDef, Patch } from '@xstate/ts-project';
import type {
ExtractorDigraphDef,
LinesAndCharactersRange,
Patch,
} from '@xstate/ts-project';
import * as vscode from 'vscode-languageserver-protocol';

type DistributiveOmit<T, K extends PropertyKey> = T extends unknown
Expand All @@ -7,9 +11,10 @@ type DistributiveOmit<T, K extends PropertyKey> = T extends unknown

type TextEdit = DistributiveOmit<
import('@xstate/ts-project').TextEdit,
'fileName'
'fileName' | 'range'
> & {
uri: string;
range: LinesAndCharactersRange;
};

export const getMachineAtIndex = new vscode.RequestType<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { createTestProject, replaceUniqueIds, testdir, ts } from './utils';
import { createTestProject, replaceUniqueIds, testdir, ts } from '../utils';

test('should extract a string entry action (direct)', async () => {
const tmpPath = await testdir({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { createTestProject, replaceUniqueIds, testdir, ts } from './utils';
import { createTestProject, replaceUniqueIds, testdir, ts } from '../utils';

test('should extract an actor with string src (direct)', async () => {
const tmpPath = await testdir({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { createTestProject, replaceUniqueIds, testdir, ts } from './utils';
import { createTestProject, replaceUniqueIds, testdir, ts } from '../utils';

test('should extract a machine with empty config', async () => {
const tmpPath = await testdir({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { createTestProject, replaceUniqueIds, testdir, ts } from './utils';
import { createTestProject, replaceUniqueIds, testdir, ts } from '../utils';

test('should extract guard from transition (string)', async () => {
const tmpPath = await testdir({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { createTestProject, replaceUniqueIds, testdir, ts } from './utils';
import { createTestProject, replaceUniqueIds, testdir, ts } from '../utils';

test('should extract a simple state', async () => {
const tmpPath = await testdir({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { createTestProject, replaceUniqueIds, testdir, ts } from './utils';
import { createTestProject, replaceUniqueIds, testdir, ts } from '../utils';

test('should extract transition to a sibling (direct string)', async () => {
const tmpPath = await testdir({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect, test } from 'vitest';
import { createTestProject, testdir, ts } from '../utils';

test('should rename an identifier state key to another identifier state key', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";
createMachine({
type: "parallel",
states: {
foo: {},
bar: {},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'rename_state',
path: ['foo'],
name: 'NEW_NAME',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(
`
{
"index.ts": "import { createMachine } from "xstate";
createMachine({
type: "parallel",
states: {
"NEW_NAME": {},
bar: {},
},
});",
}
`,
);
});
Loading

0 comments on commit ab4d742

Please sign in to comment.