1- import { EditorView , basicSetup } from 'codemirror' ;
1+ import { EditorView } from '@ codemirror/view ' ;
22import { setDiagnostics } from '@codemirror/lint' ;
33import { examples } from './examples.mjs' ;
44import { diagnosticHtml , editorDiagnostic } from './diagnostics.mjs' ;
55import { editorJava , editorTheme } from './editor-theme.mjs' ;
6+ import { editorSetup } from './editor-setup.mjs' ;
67
78const root = document . querySelector ( '#lj-playground' ) ;
89const example = document . querySelector ( '#lj-example' ) ;
@@ -18,7 +19,7 @@ let source;
1819let timer ;
1920const view = new EditorView ( {
2021 doc : examples . positive ,
21- extensions : [ basicSetup , editorJava , editorTheme , EditorView . contentAttributes . of ( { 'aria-label' : 'Java source code' } ) ,
22+ extensions : [ editorSetup , editorJava , editorTheme , EditorView . contentAttributes . of ( { 'aria-label' : 'Java source code' } ) ,
2223 EditorView . updateListener . of ( update => {
2324 if ( update . docChanged ) {
2425 queueMicrotask ( ( ) => view . dispatch ( setDiagnostics ( view . state , [ ] ) ) ) ;
@@ -57,6 +58,20 @@ function render(result) {
5758 const pre = document . createElement ( 'pre' ) ;
5859 const code = document . createElement ( 'code' ) ;
5960 code . innerHTML = diagnosticHtml ( issues . map ( issue => issue . output ) . join ( '\n' ) + ( result . details || '' ) ) ;
61+ for ( const link of code . querySelectorAll ( 'a[data-line]' ) ) {
62+ const lineNumber = Number ( link . dataset . line ) ;
63+ if ( lineNumber < 1 || lineNumber > view . state . doc . lines ) continue ;
64+ link . onclick = event => {
65+ event . preventDefault ( ) ;
66+ const issue = issues . find ( issue => issue . line === lineNumber ) ;
67+ const mark = issue && editorDiagnostic ( issue , view . state . doc . length ) ;
68+ const line = view . state . doc . line ( lineNumber ) ;
69+ const anchor = mark ?. from ?? line . from + line . text . search ( / \S | $ / ) ;
70+ view . dom . scrollIntoView ( { block : 'center' , inline : 'nearest' } ) ;
71+ view . dispatch ( { selection : { anchor } , effects : EditorView . scrollIntoView ( anchor , { y : 'center' } ) } ) ;
72+ view . focus ( ) ;
73+ } ;
74+ }
6075 pre . append ( code ) ;
6176 results . append ( pre ) ;
6277 }
0 commit comments