Skip to content

show inline diagnostics #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions content/tutorial/common/src/__client.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,14 @@ if (import.meta.hot) {
'*'
);
});

import.meta.hot.on('svelte:warnings', (data) => {
parent.postMessage(
{
type: 'warnings',
data
},
'*'
);
});
}
6 changes: 6 additions & 0 deletions content/tutorial/common/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const config = {
// Don't do this in your own apps unless you know what you're doing!
// See https://kit.svelte.dev/docs/configuration#csrf for more info.
csrf: false
},

vitePlugin: {
experimental: {
sendWarningsToBrowser: true
}
}
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@codemirror/lang-html": "^6.4.2",
"@codemirror/lang-javascript": "^6.1.4",
"@codemirror/language": "^6.6.0",
"@codemirror/lint": "^6.2.0",
"@codemirror/state": "^6.2.0",
"@codemirror/view": "^6.9.2",
"@fontsource/roboto-mono": "^4.5.10",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions src/routes/tutorial/[slug]/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import { EditorState } from '@codemirror/state';
import { indentWithTab } from '@codemirror/commands';
import { indentUnit } from '@codemirror/language';
import { setDiagnostics } from '@codemirror/lint';
import { javascript } from '@codemirror/lang-javascript';
import { html } from '@codemirror/lang-html';
import { svelte } from '@replit/codemirror-lang-svelte';
import { tags } from '@lezer/highlight';
import { HighlightStyle } from '@codemirror/language';
import { syntaxHighlighting } from '@codemirror/language';
import { afterNavigate, beforeNavigate } from '$app/navigation';
import { files, selected_file, selected_name, update_file } from './state.js';
import { files, selected_file, selected_name, update_file, warnings } from './state.js';
import './codemirror.css';

// TODO add more styles (selection ranges, etc)
Expand Down Expand Up @@ -49,7 +50,31 @@
theme
];

$: if (editor_view) select_state($selected_name);
$: if (editor_view) {
select_state($selected_name);

if ($selected_name) {
const current_warnings = $warnings[$selected_name];

if (current_warnings) {
const diagnostics = current_warnings.map((warning) => {
/** @type {import('@codemirror/lint').Diagnostic} */
const diagnostic = {
from: warning.start.character,
to: warning.end.character,
severity: 'warning',
message: warning.message
};

return diagnostic;
});

const transaction = setDiagnostics(editor_view.state, diagnostics);

editor_view.dispatch(transaction);
}
}
}

$: reset($files);

Expand Down Expand Up @@ -178,6 +203,9 @@
reset($files);

select_state($selected_name);

// clear warnings
warnings.set({});
});
</script>

Expand Down
6 changes: 6 additions & 0 deletions src/routes/tutorial/[slug]/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Chrome from './Chrome.svelte';
import Loading from './Loading.svelte';
import { base, error, logs, progress, subscribe } from './adapter';
import { warnings } from './state';

/** @type {import('$lib/types').Exercise} */
export let exercise;
Expand Down Expand Up @@ -61,6 +62,11 @@
}, 1000);
} else if (e.data.type === 'ping-pause') {
clearTimeout(timeout);
} else if (e.data.type === 'warnings') {
warnings.update(($warnings) => ({
...$warnings,
[e.data.data.normalizedFilename]: e.data.data.allWarnings
}));
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/routes/tutorial/[slug]/codemirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@
color: var(--sk-text-2);
}

.cm-editor .cm-tooltip {
border: none;
border-radius: 2px;
overflow: hidden;
margin: 0.4rem 0;
filter: drop-shadow(1px 2px 5px rgba(0,0,0,0.1));
}
.cm-editor .cm-tooltip-hover {}
.cm-editor .cm-tooltip-below {}
.cm-editor .cm-tooltip-lint {}
.cm-editor .cm-tooltip-section {}
.cm-editor .cm-diagnostic {
border: none;
padding: 0.2rem 0.8rem;
}
.cm-editor .cm-diagnostic-warning {
/* background: hsl(36, 100%, 32%); */
background: hsl(39, 100%, 10%);
}
.cm-editor .cm-diagnosticText {}


@media (prefers-color-scheme: dark) {
.cm-editor .cm-activeLineGutter {
background-color: var(--sk-back-3);
Expand All @@ -57,4 +79,8 @@
.cm-editor .cm-activeLine {
background-color: var(--sk-back-2);
}

.cm-editor .cm-diagnostic-warning {
background: hsl(39, 100%, 20%);
}
}
27 changes: 24 additions & 3 deletions src/routes/tutorial/[slug]/state.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { derived, writable } from 'svelte/store';
import * as adapter from './adapter.js';

/** @type {import('svelte/store').Writable<import('$lib/types').Stub[]>} */
/**
* @template T
* @typedef {import('svelte/store').Writable<T>} Writable<T>
*/

// TODO would be nice if svelte exported this type (maybe it does already?)
/**
* @typedef {{
* code: string;
* start: { line: number, column: number, character: number };
* end: { line: number, column: number, character: number };
* pos: number;
* filename: string;
* frame: string;
* message: string;
* }} CompilerWarning
*/

/** @type {Writable<import('$lib/types').Stub[]>} */
export const files = writable([]);

/** @type {import('svelte/store').Writable<Record<string, import('$lib/types').Stub>>} */
/** @type {Writable<Record<string, import('$lib/types').Stub>>} */
export const solution = writable({});

/** @type {import('svelte/store').Writable<string | null>} */
/** @type {Writable<Record<string, CompilerWarning[]>>} */
export const warnings = writable({});

/** @type {Writable<string | null>} */
export const selected_name = writable(null);

export const selected_file = derived([files, selected_name], ([$files, $selected_name]) => {
Expand Down