Skip to content

Commit e21cbd4

Browse files
committed
move diagnostics logic into workspace, expose workspace.current_compiled
1 parent 49b12d8 commit e21cbd4

File tree

4 files changed

+58
-47
lines changed

4 files changed

+58
-47
lines changed

packages/editor/src/lib/Editor.svelte

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { BROWSER } from 'esm-env';
33
import { setDiagnostics } from '@codemirror/lint';
44
import { EditorView } from '@codemirror/view';
5-
import type { Diagnostic } from '@codemirror/lint';
65
import { Workspace, type File } from './Workspace.svelte.js';
76
import './codemirror.css';
87
@@ -34,50 +33,8 @@
3433
};
3534
});
3635
37-
// TODO move into workspace
3836
$effect(() => {
39-
const diagnostics: Diagnostic[] = [];
40-
41-
const error = workspace.compiled[workspace.current.name]?.error;
42-
const current_warnings = workspace.compiled[workspace.current.name]?.result?.warnings ?? [];
43-
44-
if (error) {
45-
diagnostics.push({
46-
severity: 'error',
47-
from: error.position![0],
48-
to: error.position![1],
49-
message: error.message,
50-
renderMessage: () => {
51-
const span = document.createElement('span');
52-
span.innerHTML = `${error.message
53-
.replace(/&/g, '&')
54-
.replace(/</g, '&lt;')
55-
.replace(/`(.+?)`/g, `<code>$1</code>`)} <strong>(${error.code})</strong>`;
56-
57-
return span;
58-
}
59-
});
60-
}
61-
62-
for (const warning of current_warnings) {
63-
diagnostics.push({
64-
severity: 'warning',
65-
from: warning.start!.character,
66-
to: warning.end!.character,
67-
message: warning.message,
68-
renderMessage: () => {
69-
const span = document.createElement('span');
70-
span.innerHTML = `${warning.message
71-
.replace(/&/g, '&amp;')
72-
.replace(/</g, '&lt;')
73-
.replace(/`(.+?)`/g, `<code>$1</code>`)} <strong>(${warning.code})</strong>`;
74-
75-
return span;
76-
}
77-
});
78-
}
79-
80-
const transaction = setDiagnostics(editor_view.state, diagnostics);
37+
const transaction = setDiagnostics(editor_view.state, workspace.diagnostics);
8138
editor_view.dispatch(transaction);
8239
});
8340
</script>

packages/editor/src/lib/Workspace.svelte.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { indentWithTab } from '@codemirror/commands';
1313
import { indentUnit } from '@codemirror/language';
1414
import { theme } from './theme';
1515
import { untrack } from 'svelte';
16+
import type { Diagnostic } from '@codemirror/lint';
1617

1718
export interface File {
1819
type: 'file';
@@ -102,6 +103,51 @@ export class Workspace {
102103
states = new Map<string, EditorState>();
103104
#view: EditorView | null = null;
104105

106+
diagnostics = $derived.by(() => {
107+
const diagnostics: Diagnostic[] = [];
108+
109+
const error = this.current_compiled?.error;
110+
const warnings = this.current_compiled?.result?.warnings ?? [];
111+
112+
if (error) {
113+
diagnostics.push({
114+
severity: 'error',
115+
from: error.position![0],
116+
to: error.position![1],
117+
message: error.message,
118+
renderMessage: () => {
119+
const span = document.createElement('span');
120+
span.innerHTML = `${error.message
121+
.replace(/&/g, '&amp;')
122+
.replace(/</g, '&lt;')
123+
.replace(/`(.+?)`/g, `<code>$1</code>`)} <strong>(${error.code})</strong>`;
124+
125+
return span;
126+
}
127+
});
128+
}
129+
130+
for (const warning of warnings) {
131+
diagnostics.push({
132+
severity: 'warning',
133+
from: warning.start!.character,
134+
to: warning.end!.character,
135+
message: warning.message,
136+
renderMessage: () => {
137+
const span = document.createElement('span');
138+
span.innerHTML = `${warning.message
139+
.replace(/&/g, '&amp;')
140+
.replace(/</g, '&lt;')
141+
.replace(/`(.+?)`/g, `<code>$1</code>`)} <strong>(${warning.code})</strong>`;
142+
143+
return span;
144+
}
145+
});
146+
}
147+
148+
return diagnostics;
149+
});
150+
105151
constructor(
106152
files: Item[],
107153
{
@@ -141,6 +187,14 @@ export class Workspace {
141187
return this.#current;
142188
}
143189

190+
get current_compiled() {
191+
if (this.#current.name in this.compiled) {
192+
return this.compiled[this.#current.name];
193+
}
194+
195+
return null;
196+
}
197+
144198
add(item: Item) {
145199
this.#create_directories(item);
146200
this.#files = this.#files.concat(item);

packages/repl/src/lib/Output/Output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
6262
let markdown = $derived(is_markdown ? (marked.parse(workspace.current!.contents) as string) : '');
6363
64-
let current = $derived(workspace.compiled[workspace.current.name!]);
64+
let current = $derived(workspace.current_compiled);
6565
6666
// TODO this effect is a bit of a code smell
6767
$effect(() => {

packages/repl/src/lib/Repl.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@
154154
155155
let runes = $derived(
156156
workspace.current.name.endsWith('.svelte.js') ||
157-
(workspace.compiled[workspace.current.name!]?.result?.metadata.runes ?? false)
157+
(workspace.current_compiled?.result?.metadata.runes ?? false)
158158
);
159159
160-
let migration = $derived(workspace.compiled[workspace.current.name!]?.migration);
160+
let migration = $derived(workspace.current_compiled?.migration);
161161
let can_migrate = $derived(migration ? migration.code !== workspace.current?.contents : false);
162162
</script>
163163

0 commit comments

Comments
 (0)