From e57ce550b82eb2413e33cafe444158d0b7945891 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 10 Sep 2026 14:46:34 -0700 Subject: [PATCH] feat: navigate code from markdown links --- playground/package-lock.json | 1 + playground/package.json | 1 + playground/src/main.ts | 99 +++++++++++++++++++++++++++++------- playground/src/style.css | 7 +++ 4 files changed, 90 insertions(+), 18 deletions(-) diff --git a/playground/package-lock.json b/playground/package-lock.json index 211fe20cd84..99db52285cb 100644 --- a/playground/package-lock.json +++ b/playground/package-lock.json @@ -10,6 +10,7 @@ "@codemirror/lang-rust": "^6.0.2", "@codemirror/lsp-client": "^6.2.5", "@codemirror/theme-one-dark": "^6.1.3", + "@codemirror/view": "^6.43.11", "codemirror": "^6.0.2", "vite": "^7.1.5", "ws": "^8.18.3" diff --git a/playground/package.json b/playground/package.json index 69bfd5129fd..6acbf58179a 100644 --- a/playground/package.json +++ b/playground/package.json @@ -11,6 +11,7 @@ "@codemirror/lang-rust": "^6.0.2", "@codemirror/lsp-client": "^6.2.5", "@codemirror/theme-one-dark": "^6.1.3", + "@codemirror/view": "^6.43.11", "codemirror": "^6.0.2", "vite": "^7.1.5", "ws": "^8.18.3" diff --git a/playground/src/main.ts b/playground/src/main.ts index 664d451973f..d44c017207c 100644 --- a/playground/src/main.ts +++ b/playground/src/main.ts @@ -1,5 +1,5 @@ -import { rust } from '@codemirror/lang-rust'; import { markdown } from '@codemirror/lang-markdown'; +import { rust } from '@codemirror/lang-rust'; import { languageServerExtensions, jumpToDefinition, @@ -10,6 +10,7 @@ import { type WorkspaceFile, } from '@codemirror/lsp-client'; import { oneDark } from '@codemirror/theme-one-dark'; +import { Decoration, MatchDecorator, ViewPlugin } from '@codemirror/view'; import { EditorView, basicSetup } from 'codemirror'; import './style.css'; @@ -18,7 +19,7 @@ const STORAGE_KEY = 'litellm-rust-playground-files'; const GUIDE_PATH = 'GUIDE.md'; const DEFAULT_GUIDE = `# Prove the connection -Cmd-click \`build_proof\` in \`src/main.rs\`. The Rust editor opens its definition in \`src/proof.rs\`. +Click [the \`build_proof\` definition](playground://src/proof.rs#L9), or Cmd-click \`build_proof\` in \`src/main.rs\`. Hover \`litellm_core::Error\` for dependency type information. @@ -147,6 +148,50 @@ const commandClickDefinition = EditorView.domEventHandlers({ }, }); +const playgroundLinkMatcher = new MatchDecorator({ + regexp: /\[[^\]\n]+\]\(playground:\/\/[^)\s]+\)/g, + decoration: Decoration.mark({ class: 'cm-playground-link' }), +}); + +const playgroundLinkDecorations = ViewPlugin.fromClass( + class { + public decorations; + + public constructor(view: EditorView) { + this.decorations = playgroundLinkMatcher.createDeco(view); + } + + public update(update: Parameters[0]) { + this.decorations = playgroundLinkMatcher.updateDeco(update, this.decorations); + } + }, + { decorations: instance => instance.decorations }, +); + +const markdownFileNavigation = (openTarget: (target: string) => boolean) => + EditorView.domEventHandlers({ + mousedown(event, view) { + if (event.button !== 0) { + return false; + } + const position = view.posAtCoords({ x: event.clientX, y: event.clientY }); + if (position === null) { + return false; + } + const line = view.state.doc.lineAt(position); + const offset = position - line.from; + const matches = line.text.matchAll(/\[([^\]\n]+)\]\((playground:\/\/[^)\s]+)\)/g); + for (const match of matches) { + const start = match.index; + if (offset >= start && offset <= start + match[0].length && openTarget(match[2])) { + event.preventDefault(); + return true; + } + } + return false; + }, + }); + const connectTransport = (url: string): Promise => new Promise((resolve, reject) => { const socket = new WebSocket(url); @@ -213,22 +258,6 @@ const main = async () => { const buttons = new Map(); let activeUri = info.files[0]?.uri ?? ''; - const markdownView = new EditorView({ - doc: storedFiles[GUIDE_PATH] ?? DEFAULT_GUIDE, - extensions: [ - basicSetup, - markdown(), - oneDark, - EditorView.lineWrapping, - EditorView.updateListener.of(update => { - if (update.docChanged) { - saveStoredFile(GUIDE_PATH, update.state.doc.toString()); - } - }), - ], - parent: markdownEditorParent, - }); - const showFile = (uri: string) => { const view = views.get(uri); if (!view) { @@ -246,6 +275,22 @@ const main = async () => { return view; }; + const openFileTarget = (target: string) => { + const match = /^playground:\/\/(.+?)(?:#L(\d+))?$/.exec(target); + const file = match ? info.files.find(candidate => candidate.path === match[1]) : undefined; + const view = file ? showFile(file.uri) : null; + if (!view) { + return false; + } + const requestedLine = Number(match?.[2] ?? 1); + const line = view.state.doc.line(Math.min(Math.max(requestedLine, 1), view.state.doc.lines)); + view.dispatch({ + selection: { anchor: line.from }, + effects: EditorView.scrollIntoView(line.from, { y: 'center' }), + }); + return true; + }; + const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'; const transport = await connectTransport(`${protocol}//${location.host}/lsp`); const client = new LSPClient({ @@ -302,6 +347,24 @@ const main = async () => { views.set(file.uri, view); }); + const markdownView = new EditorView({ + doc: storedFiles[GUIDE_PATH] ?? DEFAULT_GUIDE, + extensions: [ + basicSetup, + markdown(), + oneDark, + playgroundLinkDecorations, + markdownFileNavigation(openFileTarget), + EditorView.lineWrapping, + EditorView.updateListener.of(update => { + if (update.docChanged) { + saveStoredFile(GUIDE_PATH, update.state.doc.toString()); + } + }), + ], + parent: markdownEditorParent, + }); + showFile(activeUri); lspStatus.textContent = 'rust-analyzer connected'; lspStatus.dataset.state = 'ready'; diff --git a/playground/src/style.css b/playground/src/style.css index 0b6402ace96..964f6c311a5 100644 --- a/playground/src/style.css +++ b/playground/src/style.css @@ -290,6 +290,13 @@ h1 { line-height: 1.65; } +.cm-playground-link { + color: #79b8ff; + cursor: pointer; + text-decoration: underline; + text-underline-offset: 0.18em; +} + .output-panel { display: grid; grid-template-rows: auto 1fr;