Skip to content

Commit

Permalink
Implement state keys updating (#463)
Browse files Browse the repository at this point in the history
* Implement state keys updating

* Prepare test setup for text edits (#464)

* Prepare test setup for text edits

* Improve how renamed state keys are printed (#465)

* Improve how renamed state keys are printed

* Implement initial state edits (#466)

* Implement initial state edits

* Implement initial state insertions before existing states property (#467)
  • Loading branch information
Andarist authored Feb 13, 2024
1 parent 0b7ee6d commit 3db40df
Show file tree
Hide file tree
Showing 19 changed files with 1,718 additions and 79 deletions.
51 changes: 38 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,28 @@ connection.onRequest(applyPatches, async ({ uri, machineIndex, patches }) => {
patches,
});

return edits.map(({ fileName, ...rest }) => ({
...rest,
uri: server.env.fileNameToUri(fileName),
}));
return edits.map((edit) => {
if (edit.type === 'replace') {
return {
type: 'replace' as const,
uri: server.env.fileNameToUri(edit.fileName),
range: xstateProject.getLinesAndCharactersRange(
edit.fileName,
edit.range,
),
newText: edit.newText,
};
}
return {
type: edit.type,
uri: server.env.fileNameToUri(edit.fileName),
position: xstateProject.getLineAndCharacterOfPosition(
edit.fileName,
edit.position,
),
newText: edit.newText,
};
});
});

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

type DistributiveOmit<T, K extends PropertyKey> = T extends unknown
? Omit<T, K>
: never;

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

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
Loading

0 comments on commit 3db40df

Please sign in to comment.