Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ jobs:

- run: pnpm run build
- run: pnpm lint
- run: node --test tests/*.test.ts

- name: Install Netlify
if: env.SITE_ID
Expand Down
14 changes: 8 additions & 6 deletions src/components/IoContainer.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { Icon } from "@iconify/vue";
import { useMediaQuery } from "@vueuse/core";
import { computed } from "vue";
import { computed, useTemplateRef } from "vue";
import { useOxc } from "~/composables/oxc";
import { Splitter, SplitterPanel, SplitterResizeHandle } from "~/ui/splitter";
import CopyContainer from "./CopyContainer.vue";
Expand All @@ -13,6 +13,7 @@ const { oxc, error, monacoLanguage, options } = await useOxc();

const isMdAndUp = useMediaQuery("(min-width: 768px)");
const splitterDirection = computed(() => (isMdAndUp.value ? "horizontal" : "vertical"));
const inputRef = useTemplateRef("inputRef");

const errorStr = computed(() => {
return Array.isArray(error.value)
Expand Down Expand Up @@ -41,18 +42,19 @@ function stringifyError(error: unknown) {
<div class="h-full min-h-0 min-w-0 flex flex-1 flex-col">
<div class="min-h-0 flex-1">
<InputEditor
ref="inputRef"
:language="monacoLanguage"
:filename="`/test.${options.parser.extension}`"
main
/>
</div>

<div
<DiagnosticPanel
v-if="oxc.getDiagnostics().length"
class="min-h-0 flex flex-1 flex-col gap-2 overflow-auto border-t px-3 py-2 text-sm font-mono opacity-80"
>
<DiagnosticPanel />
</div>
:filename="`test.${options.parser.extension}`"
:get-position-at="inputRef?.getPositionAt"
@navigate="(start, end) => inputRef?.revealRange(start, end)"
/>
</div>
</SplitterPanel>

Expand Down
23 changes: 22 additions & 1 deletion src/components/MonacoEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const emit = defineEmits(["editorWillMount", "editorDidMount", "change", "update
const container = ref<HTMLElement>();
const instance = shallowRef<monaco.editor.IStandaloneCodeEditor>();
const model = shallowRef<monaco.editor.ITextModel>(initModel());
let pendingSelection: [monaco.IPosition, monaco.IPosition] | undefined;

watchEffect((onCleanup) => {
const dispose = model.value.onDidChangeContent(() => {
Expand Down Expand Up @@ -127,7 +128,13 @@ onMounted(async () => {
} catch (error) {
console.warn("Failed to load editor highlighting; using the built-in theme:", error);
}
if (!model.value.isDisposed()) initMonaco();
if (!model.value.isDisposed()) {
initMonaco();
if (pendingSelection) {
revealRange(...pendingSelection);
pendingSelection = undefined;
}
}
});

onBeforeUnmount(() => {
Expand All @@ -137,8 +144,22 @@ onBeforeUnmount(() => {
});

const getPositionAt = (offset: number) => model.value.getPositionAt(offset);

function revealRange(start: monaco.IPosition, end: monaco.IPosition) {
const editor = instance.value;
if (!editor) {
pendingSelection = [start, end];
return;
}
const range = monaco.Range.fromPositions(start, end);
editor.setSelection(range);
editor.revealRangeInCenterIfOutsideViewport(range);
editor.focus();
}

defineExpose({
getPositionAt,
revealRange,
});
</script>

Expand Down
18 changes: 12 additions & 6 deletions src/components/input/InputEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as monaco from "monaco-editor/editor/editor.api";
import { computed, ref, useTemplateRef, watchEffect } from "vue";
import { useOxc } from "~/composables/oxc";
import { editorValue } from "~/composables/state";
import { createDiagnosticPositionMapper } from "~/utils/diagnostic-offsets";
import MonacoEditor from "../MonacoEditor.vue";

defineProps<{
Expand All @@ -14,17 +15,16 @@ defineProps<{
const { oxc } = await useOxc();

const monacoRef = useTemplateRef("monacoRef");
const getPositionAt = computed(() => monacoRef.value?.getPositionAt);
const toPosition = computed(() => createDiagnosticPositionMapper(editorValue.value));
// Resolve the lazy map only when a diagnostic label actually needs a position.
const getPositionAt = (byteOffset: number) => toPosition.value(byteOffset);
const markers = ref<monaco.editor.IMarkerData[]>([]);

watchEffect(() => {
const getPos = getPositionAt.value;
if (!getPos) return;

const diagnostics = oxc.value.getDiagnostics();
markers.value = diagnostics.map((d) => {
const startPos = getPos(d.labels[0]?.start ?? 0);
const endPos = getPos(d.labels[0]?.end ?? 0);
const startPos = getPositionAt(d.labels[0]?.start ?? 0);
const endPos = getPositionAt(d.labels[0]?.end ?? 0);
return {
severity:
d.severity === "Warning" ? monaco.MarkerSeverity.Warning : monaco.MarkerSeverity.Error,
Expand All @@ -36,6 +36,12 @@ watchEffect(() => {
};
});
});

defineExpose({
getPositionAt,
revealRange: (start: number, end: number) =>
monacoRef.value?.revealRange(getPositionAt(start), getPositionAt(end)),
});
</script>

<template>
Expand Down
93 changes: 93 additions & 0 deletions src/components/output/DiagnosticCodeframe.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<script setup lang="ts">
import { computed, ref, watchEffect } from "vue";
import { createHighlighter } from "~/utils/shiki";
import { alignDiagnosticSource, type DiagnosticToken } from "~/utils/diagnostic-codeframe";

const props = defineProps<{
codeframe: string;
source: string;
severityColor: string;
}>();

const tokens = ref<DiagnosticToken[][]>([]);
const lines = computed(() =>
props.codeframe.split("\n").map((text) => {
// Keep the compiler's gutter, annotations, and spacing intact. Only the
// numbered source lines are JavaScript; the rest is diagnostic prose.
const source = /^(\s*(\d+)\s*[|│] )(.*)$/u.exec(text);
const gutter = source ? undefined : /^(\s*\|\s?)(.*)$/u.exec(text);
const decoration = /^\s*(?:,-\[|`-+)/u.test(text);
return {
text,
prefix: source?.[1],
source: source?.[3],
lineNumber: source ? Number(source[2]) : undefined,
gutter: gutter?.[1],
prose: gutter?.[2] ?? text,
decoration,
annotation: text.includes("^") || /^\s*[!x×]/u.test(text),
};
}),
);
const highlightedLines = computed(() =>
lines.value.map((line) => {
const sourceTokens = line.lineNumber && tokens.value[line.lineNumber - 1];
const aligned =
sourceTokens && line.source !== undefined
? alignDiagnosticSource(line.source, sourceTokens)
: undefined;
return {
...line,
prefix: (line.prefix ?? "") + (aligned?.gutter ?? ""),
tokens: aligned?.tokens,
};
}),
);

watchEffect(async (onCleanup) => {
// Tokenize the original file so excerpts inside comments or templates retain
// their lexical context; diagnostic arrows never enter the tokenizer.
const source = props.source;
tokens.value = [];
if (!source) return;
let stale = false;
onCleanup(() => (stale = true));
try {
const highlighter = await createHighlighter();
const result = highlighter.codeToTokens(source, {
lang: "tsx",
themes: { light: "oxc-light", dark: "oxc-dark" },
});
if (!stale) tokens.value = result.tokens;
} catch {
// The complete plain code frame remains available if highlighting fails.
}
});
</script>

<template>
<pre
class="diagnostic-codeframe overflow-x-auto px-3 py-2 text-xs leading-relaxed"
:class="severityColor"
tabindex="0"
><template v-for="(line, i) in highlightedLines" :key="i"><template v-if="line.source !== undefined"><span class="text-muted-foreground">{{ line.prefix }}</span><template v-if="line.tokens"><span v-for="(token, j) in line.tokens" :key="j" class="diagnostic-token" :style="{ '--token-light': token.htmlStyle?.color, '--token-dark': token.htmlStyle?.['--shiki-dark'] }">{{ token.content }}</span></template><span v-else class="text-foreground">{{ line.source }}</span></template><template v-else><span v-if="line.gutter" class="text-muted-foreground">{{ line.gutter }}</span><span :class="line.annotation ? severityColor : line.decoration ? 'text-muted-foreground' : 'text-foreground'">{{ line.prose }}</span></template>{{ i < highlightedLines.length - 1 ? "\n" : "" }}</template></pre>
</template>

<style scoped>
.diagnostic-codeframe {
border-left: 2px solid color-mix(in srgb, currentColor 30%, transparent);
}

.diagnostic-token {
color: var(--token-light);
}

:global(.dark .diagnostic-token) {
color: var(--token-dark);
}

.diagnostic-codeframe:focus-visible {
outline: 2px solid hsl(var(--ring));
outline-offset: -2px;
}
</style>
Loading
Loading