Skip to content
This repository was archived by the owner on Sep 19, 2025. It is now read-only.

Commit 54e1c98

Browse files
committed
Remove non-worker implementations of APIs
1 parent df61f04 commit 54e1c98

File tree

12 files changed

+27
-390
lines changed

12 files changed

+27
-390
lines changed

README.md

Lines changed: 4 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -35,138 +35,7 @@ have direct dependencies to:
3535
Below are recipes for setting up this code - check out the StackBlitz
3636
demos above if you want easily copy-paste-able code!
3737

38-
## Setup (main thread)
39-
40-
This is the simplest way to use this code: you'll be running
41-
the TypeScript server on the same processing as the rest of the web
42-
application. To run the TypeScript server in a worker (which can yield
43-
performance benefits, at the cost of complexity), see the next
44-
section.
45-
46-
_This is designed to scale up to more complex scenarios, so there
47-
is some assembly required. We could encapsulate more, but that would
48-
mean removing important points of control._
49-
50-
1. Create a TypeScript environment.
51-
52-
We don't create a TypeScript environment for you. This you bring, and it probably
53-
is used for other parts of your application. The simplest setup would be something like this,
54-
using [@typescript/vfs](https://www.npmjs.com/package/@typescript/vfs):
55-
56-
```ts
57-
import {
58-
createDefaultMapFromCDN,
59-
createSystem,
60-
createVirtualTypeScriptEnvironment,
61-
} from "@typescript/vfs";
62-
63-
const fsMap = await createDefaultMapFromCDN(
64-
{ target: ts.ScriptTarget.ES2022 },
65-
"3.7.3",
66-
true,
67-
ts,
68-
);
69-
const system = createSystem(fsMap);
70-
const compilerOpts = {};
71-
const env = createVirtualTypeScriptEnvironment(system, [], ts, compilerOpts);
72-
```
73-
74-
2. Install the facet and the sync extension:
75-
76-
The facet configures the rest of the extensions
77-
with the right path and environment.
78-
79-
When you make changes in your
80-
editor, the sync extension mirrors them to the TypeScript environment using
81-
`createFile` and `updateFile` in the TypeScript compiler.
82-
83-
_Note, also, that we're supplying a path._ These extensions
84-
use file paths in order to differentiate between different editor
85-
instances and to allow editors to import & export to & from
86-
one another. So each extension has a required `path` parameter,
87-
as well as the `env` parameter which should be your TypeScript
88-
environment.
89-
90-
```ts
91-
import { tsSync, tsFacet } from "@valtown/codemirror-ts";
92-
93-
let path = "index.ts";
94-
95-
let editor = new EditorView({
96-
extensions: [
97-
basicSetup,
98-
javascript({
99-
typescript: true,
100-
jsx: true,
101-
}),
102-
tsFacet.of({ env, path }),
103-
tsSync(),
104-
],
105-
parent: document.querySelector("#editor"),
106-
});
107-
```
108-
109-
### Linting
110-
111-
The `tsLinter` extension can be initialized
112-
like this and added to the `extensions` array in the setup
113-
of your CodeMirror instance.
114-
115-
```ts
116-
tsLinter();
117-
```
118-
119-
This uses the [@codemirror/lint](https://codemirror.net/docs/ref/#lint)
120-
package and grabs diagnostics from the TypeScript environment.
121-
122-
_If you want to modify how lints are handled, you can use
123-
the `getLints({ env, path })` method and wire it up with
124-
CodeMirror's linter method yourself._
125-
126-
### Autocompletion
127-
128-
To make it possible to combine different autocompletion
129-
sources, we expose a [`CompletionSource`](https://codemirror.net/docs/ref/#autocomplete.autocompletion) which you can use with the CodeMirror `autocomplete` method:
130-
131-
```ts
132-
autocompletion({
133-
override: [tsAutocomplete()],
134-
});
135-
```
136-
137-
_We expose a lower-level interface to autocompletions with the
138-
`getAutocompletion({ env, path, context })` method that takes
139-
a `CompletionContext` parameter._
140-
141-
### Hover
142-
143-
The hover definition can be used like the following:
144-
145-
```ts
146-
tsHover();
147-
```
148-
149-
Which automatically uses a default renderer. However, you can
150-
customize this to your heart's content, and use your web framework
151-
to render custom UI if you want to, using the `renderTooltip` option.
152-
153-
```ts
154-
tsHover({
155-
renderTooltip: (info: HoverInfo) => {
156-
const div = document.createElement("div");
157-
if (info.quickInfo?.displayParts) {
158-
for (let part of info.quickInfo.displayParts) {
159-
const span = div.appendChild(document.createElement("span"));
160-
span.className = `quick-info-${part.kind}`;
161-
span.innerText = part.text;
162-
}
163-
}
164-
return { dom: div };
165-
},
166-
});
167-
```
168-
169-
## Setup (worker)
38+
## Setup
17039

17140
Using a [Worker](https://developer.mozilla.org/en-US/docs/Web/API/Worker), you can
17241
run TypeScript separately from the rest of your JavaScript, which can make
@@ -304,9 +173,11 @@ Comlink is [lightweight](https://bundlephobia.com/package/[email protected]) (4.7kb
304173
This module uses TypeScript’s public APIs to power its functionality:
305174
it _doesn't_ use the [Language Server Protocol](https://en.wikipedia.org/wiki/Language_Server_Protocol), which is
306175
a specification developed by Microsoft and intended for functionality like
307-
this. TypeScript itself does not have a first-party LSP implementation
176+
this. [TypeScript itself does not have a first-party LSP implementation](https://github.com/microsoft/TypeScript/issues/39459)
308177
and LSP is usually used across a network. Most good TypeScript language
309178
tooling, like VS Code’s autocompletion, does not use the LSP specification.
179+
Unfortunately, most TypeScript language tooling in other editors is based directly
180+
off of the VS Code implementation.
310181

311182
### ❤️ Other great CodeMirror plugins
312183

demo/index.html

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,8 @@ <h2>Currently supported</h2>
3232
<li>Diagnostics</li>
3333
<li>Go to definition</li>
3434
</ul>
35-
<h2>Demos</h2>
35+
<h2>Demo</h2>
3636

37-
<h3>TypeScript running on the page</h3>
38-
<div id="app" class='border'>
39-
<div id="editor"></div>
40-
</div>
41-
42-
<h3>TypeScript environment in a web worker</h3>
4337
<div id="app" class='border'>
4438
<div id="editor-worker"></div>
4539
</div>

demo/index.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,16 @@ import {
88
} from "@typescript/vfs";
99
import ts from "typescript";
1010
import {
11-
tsLinter,
1211
tsLinterWorker,
13-
tsHover,
1412
tsHoverWorker,
15-
tsAutocomplete,
1613
tsAutocompleteWorker,
17-
tsSync,
1814
tsSyncWorker,
19-
tsFacet,
2015
tsFacetWorker,
2116
tsGotoWorker,
2217
} from "../src/index.js";
2318
import * as Comlink from "comlink";
2419
import { WorkerShape } from "../src/worker.js";
2520

26-
(async () => {
27-
const fsMap = await createDefaultMapFromCDN(
28-
{ target: ts.ScriptTarget.ES2022 },
29-
ts.version,
30-
true,
31-
ts,
32-
);
33-
const system = createSystem(fsMap);
34-
const env = createVirtualTypeScriptEnvironment(system, [], ts, {
35-
lib: ["ES2022"],
36-
});
37-
38-
const path = "index.ts";
39-
40-
let editor = new EditorView({
41-
doc: `let hasAnError: string = 10;
42-
43-
function increment(num: number) {
44-
return num + 1;
45-
}
46-
47-
increment('not a number');`,
48-
extensions: [
49-
basicSetup,
50-
javascript({
51-
typescript: true,
52-
jsx: true,
53-
}),
54-
tsFacet.of({ env, path }),
55-
tsSync(),
56-
tsLinter(),
57-
autocompletion({
58-
override: [tsAutocomplete()],
59-
}),
60-
tsHover(),
61-
],
62-
parent: document.querySelector("#editor")!,
63-
});
64-
})();
65-
6621
function renderDisplayParts(dp: ts.SymbolDisplayPart[]) {
6722
const div = document.createElement("div");
6823
for (const part of dp) {

src/autocomplete/tsAutocomplete.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {
2-
CompletionContext,
3-
CompletionResult,
4-
CompletionSource,
2+
CompletionContext,
3+
CompletionResult,
4+
CompletionSource,
55
} from "@codemirror/autocomplete";
66
import { tsFacetWorker } from "../index.js";
77
import { deserializeCompletions } from "./deserializeCompletions.js";
@@ -12,25 +12,25 @@ import type { AutocompleteOptions } from "./types.js";
1212
* the TypeScript environment in a web worker.
1313
*/
1414
export function tsAutocompleteWorker(
15-
opts: AutocompleteOptions = {},
15+
opts: AutocompleteOptions = {},
1616
): CompletionSource {
17-
return async (
18-
context: CompletionContext,
19-
): Promise<CompletionResult | null> => {
20-
const config = context.state.facet(tsFacetWorker);
21-
if (!config?.worker) return null;
22-
const completion = deserializeCompletions(
23-
await config.worker.getAutocompletion({
24-
path: config.path,
25-
// Reduce this object so that it's serializable.
26-
context: {
27-
pos: context.pos,
28-
explicit: context.explicit,
29-
},
30-
}),
31-
opts,
32-
);
17+
return async (
18+
context: CompletionContext,
19+
): Promise<CompletionResult | null> => {
20+
const config = context.state.facet(tsFacetWorker);
21+
if (!config?.worker) return null;
22+
const completion = deserializeCompletions(
23+
await config.worker.getAutocompletion({
24+
path: config.path,
25+
// Reduce this object so that it's serializable.
26+
context: {
27+
pos: context.pos,
28+
explicit: context.explicit,
29+
},
30+
}),
31+
opts,
32+
);
3333

34-
return completion;
35-
};
34+
return completion;
35+
};
3636
}

src/facet/tsFacet.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/hover/tsHover.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)