11'use client' ;
22
3- import { useExtensionWithDependency } from "@/hooks/useExtensionWithDependency" ;
4- import { useSyntaxHighlightingExtension } from "@/hooks/useSyntaxHighlightingExtension" ;
5- import { useThemeNormalized } from "@/hooks/useThemeNormalized" ;
3+ import { getSyntaxHighlightingExtension } from "@/hooks/useSyntaxHighlightingExtension" ;
64import { lineOffsetExtension } from "@/lib/extensions/lineOffsetExtension" ;
75import { SearchResultRange } from "@/lib/types" ;
8- import CodeMirror , { Decoration , DecorationSet , EditorState , EditorView , ReactCodeMirrorRef , StateField , Transaction } from "@uiw/react-codemirror" ;
6+ import { defaultHighlightStyle , syntaxHighlighting } from "@codemirror/language" ;
7+ import { EditorState , StateField , Transaction } from "@codemirror/state" ;
8+ import { defaultLightThemeOption , oneDarkHighlightStyle , oneDarkTheme } from "@uiw/react-codemirror" ;
9+ import { Decoration , DecorationSet , EditorView , lineNumbers } from "@codemirror/view" ;
910import { useMemo , useRef } from "react" ;
11+ import { LightweightCodeMirror , CodeMirrorRef } from "./lightweightCodeMirror" ;
12+ import { useThemeNormalized } from "@/hooks/useThemeNormalized" ;
1013
1114const markDecoration = Decoration . mark ( {
1215 class : "cm-searchMatch-selected"
@@ -25,13 +28,22 @@ export const CodePreview = ({
2528 ranges,
2629 lineOffset,
2730} : CodePreviewProps ) => {
28- const editorRef = useRef < ReactCodeMirrorRef > ( null ) ;
31+ const editorRef = useRef < CodeMirrorRef > ( null ) ;
2932 const { theme } = useThemeNormalized ( ) ;
3033
31- const syntaxHighlighting = useSyntaxHighlightingExtension ( language , editorRef . current ?. view ) ;
32-
33- const rangeHighlighting = useExtensionWithDependency ( editorRef . current ?. view ?? null , ( ) => {
34+ const extensions = useMemo ( ( ) => {
3435 return [
36+ EditorView . editable . of ( false ) ,
37+ ...( theme === 'dark' ? [
38+ syntaxHighlighting ( oneDarkHighlightStyle ) ,
39+ oneDarkTheme ,
40+ ] : [
41+ syntaxHighlighting ( defaultHighlightStyle ) ,
42+ defaultLightThemeOption ,
43+ ] ) ,
44+ lineNumbers ( ) ,
45+ lineOffsetExtension ( lineOffset ) ,
46+ getSyntaxHighlightingExtension ( language ) ,
3547 StateField . define < DecorationSet > ( {
3648 create ( editorState : EditorState ) {
3749 const document = editorState . doc ;
@@ -61,7 +73,8 @@ export const CodePreview = ({
6173 const from = document . line ( startLine ) . from + Start . Column - 1 ;
6274 const to = document . line ( endLine ) . from + End . Column - 1 ;
6375 return markDecoration . range ( from , to ) ;
64- } ) ;
76+ } )
77+ . sort ( ( a , b ) => a . from - b . from ) ;
6578
6679 return Decoration . set ( decorations ) ;
6780 } ,
@@ -70,56 +83,15 @@ export const CodePreview = ({
7083 } ,
7184 provide : ( field ) => EditorView . decorations . from ( field ) ,
7285 } ) ,
73- ] ;
74- } , [ ranges , lineOffset ] ) ;
75-
76- const extensions = useMemo ( ( ) => {
77- return [
78- syntaxHighlighting ,
79- EditorView . lineWrapping ,
80- lineOffsetExtension ( lineOffset ) ,
81- rangeHighlighting ,
82- ] ;
83- } , [ syntaxHighlighting , lineOffset , rangeHighlighting ] ) ;
86+ ]
87+ } , [ language , lineOffset , ranges , theme ] ) ;
8488
8589 return (
86- < CodeMirror
90+ < LightweightCodeMirror
8791 ref = { editorRef }
88- readOnly = { true }
89- editable = { false }
9092 value = { content }
91- theme = { theme === "dark" ? "dark" : "light" }
92- basicSetup = { {
93- lineNumbers : true ,
94- syntaxHighlighting : true ,
95-
96- // Disable all this other stuff...
97- ... {
98- foldGutter : false ,
99- highlightActiveLineGutter : false ,
100- highlightSpecialChars : false ,
101- history : false ,
102- drawSelection : false ,
103- dropCursor : false ,
104- allowMultipleSelections : false ,
105- indentOnInput : false ,
106- bracketMatching : false ,
107- closeBrackets : false ,
108- autocompletion : false ,
109- rectangularSelection : false ,
110- crosshairCursor : false ,
111- highlightActiveLine : false ,
112- highlightSelectionMatches : false ,
113- closeBracketsKeymap : false ,
114- defaultKeymap : false ,
115- searchKeymap : false ,
116- historyKeymap : false ,
117- foldKeymap : false ,
118- completionKeymap : false ,
119- lintKeymap : false ,
120- }
121- } }
12293 extensions = { extensions }
12394 />
12495 )
96+
12597}
0 commit comments