-
Notifications
You must be signed in to change notification settings - Fork 4
Refactor CodeEditor to CodeMirror #35
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
warunalakshitha
merged 5 commits into
ballerina-platform:main
from
snelusha:feat/codemirror
Apr 8, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0ccec1d
Refactor CodeEditor to CodeMirror with Shiki integration
snelusha dbde397
Add Vim support to code editor
snelusha 695245c
Update Ballerina mode to activate only for Ballerina language
snelusha 63762fd
Refactor editor mode initialization and reset
snelusha c163e67
Fix stale closure for Vim write command
snelusha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,164 +1,214 @@ | ||
| import * as React from "react"; | ||
|
|
||
| import { createHighlighter } from "shiki"; | ||
| import { basicSetup } from "codemirror"; | ||
| import { Compartment, Prec } from "@codemirror/state"; | ||
| import { StreamLanguage, indentUnit } from "@codemirror/language"; | ||
| import { autocompletion } from "@codemirror/autocomplete"; | ||
| import { EditorView, keymap } from "@codemirror/view"; | ||
| import { indentWithTab } from "@codemirror/commands"; | ||
| import { clike } from "@codemirror/legacy-modes/mode/clike"; | ||
| import { Vim, vim } from "@replit/codemirror-vim"; | ||
|
|
||
| import { ShikiEditor } from "@/components/shiki-editor"; | ||
|
|
||
| import { useEditorStore } from "@/stores/editor-store"; | ||
| import { useFileTreeActions } from "@/stores/file-tree-store"; | ||
|
|
||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| import type { BundledLanguage, BundledTheme, HighlighterGeneric } from "shiki"; | ||
| import type { KeyBinding } from "@codemirror/view"; | ||
| import type { Extension } from "@codemirror/state"; | ||
|
|
||
| export type EditorLanguage = "ballerina" | "toml" | "text"; | ||
|
|
||
| type HotkeyMap = Record<string, () => void>; | ||
|
|
||
| interface CodeEditorProps { | ||
| value?: string; | ||
| onChange?: (value: string) => void; | ||
| language?: string; | ||
| hotkeys?: HotkeyMap; | ||
| language?: EditorLanguage; | ||
| className?: string; | ||
| } | ||
|
|
||
| const sharedClasses = | ||
| "p-4 leading-[22.5px] font-sans whitespace-pre overflow-auto absolute inset-0 box-border [tab-size:2]"; | ||
| const INDENT = " "; | ||
|
|
||
| // This is a hack to bring smart indentation for Ballerina | ||
| // since there's no official CodeMirror support | ||
| const ballerinaMode = StreamLanguage.define( | ||
| clike({ | ||
| name: "ballerina", | ||
| }), | ||
| ); | ||
|
|
||
| function buildHotkeyExtension(hotkeysRef: React.RefObject<HotkeyMap>) { | ||
| const bindings: KeyBinding[] = Object.keys(hotkeysRef.current ?? {}).map( | ||
| (key) => ({ | ||
| key, | ||
| run: () => { | ||
| hotkeysRef.current?.[key]?.(); | ||
| return true; | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| return Prec.highest(keymap.of(bindings)); | ||
| } | ||
|
|
||
| function escapeHtml(html: string) { | ||
| return html | ||
| .replace(/&/g, "&") | ||
| .replace(/</g, "<") | ||
| .replace(/>/g, ">") | ||
| .replace(/"/g, """) | ||
| .replace(/'/g, "'"); | ||
| function baseExtensions(hotkeysRef: React.RefObject<HotkeyMap>): Extension[] { | ||
| return [ | ||
| buildHotkeyExtension(hotkeysRef), | ||
| basicSetup, | ||
| indentUnit.of(INDENT), | ||
| keymap.of([indentWithTab]), | ||
| theme, | ||
| autocompletion({ | ||
| activateOnTyping: false, | ||
| override: [], | ||
| }), | ||
| ]; | ||
| } | ||
|
|
||
| const theme = EditorView.theme({ | ||
| "&": { | ||
| fontSize: "12.5px", | ||
| height: "100%", | ||
| }, | ||
| ".cm-scroller": { | ||
| fontFamily: "var(--font-sans), ui-monospace, monospace", | ||
| overflow: "auto", | ||
| scrollbarWidth: "none", | ||
| msOverflowStyle: "none", | ||
| }, | ||
| ".cm-scroller::-webkit-scrollbar": { | ||
| display: "none", | ||
| }, | ||
| ".cm-content": { | ||
| paddingTop: "1rem", | ||
| lineHeight: "180%", | ||
| }, | ||
| ".cm-line": { | ||
| lineHeight: "inherit", | ||
| }, | ||
| ".cm-gutters": { | ||
| paddingLeft: "0.5rem", | ||
| backgroundColor: "transparent", | ||
| border: "none", | ||
| color: "var(--muted-foreground)", | ||
| userSelect: "none", | ||
| }, | ||
| ".cm-activeLineGutter": { | ||
| backgroundColor: "transparent", | ||
| }, | ||
| ".cm-activeLine": { | ||
| backgroundColor: "transparent", | ||
| }, | ||
| "&.cm-focused .cm-selectionBackground": { | ||
| backgroundColor: "rgba(59, 130, 246, 0.1) !important", | ||
| }, | ||
| ".cm-matchingBracket, .cm-nonmatchingBracket": { | ||
| outline: "none", | ||
| borderRadius: "0", | ||
| }, | ||
| ".cm-vim-panel": { | ||
| backgroundColor: "var(--background)", | ||
| color: "var(--foreground)", | ||
| }, | ||
| ".cm-vim-panel input": { | ||
| fontFamily: "var(--font-sans), ui-monospace, monospace !important", | ||
| }, | ||
| ".cm-vim-message": { | ||
| color: "var(--muted-foreground) !important", | ||
| }, | ||
| }); | ||
|
|
||
| export function CodeEditor({ | ||
| value = "", | ||
| value, | ||
| onChange, | ||
| hotkeys = {}, | ||
| language = "ballerina", | ||
|
snelusha marked this conversation as resolved.
|
||
| className, | ||
| }: CodeEditorProps) { | ||
| const [highlighted, setHighlighted] = React.useState(""); | ||
| const highlighterRef = React.useRef<HighlighterGeneric< | ||
| BundledLanguage, | ||
| BundledTheme | ||
| > | null>(null); | ||
| const textareaRef = React.useRef<HTMLTextAreaElement>(null); | ||
| const preRef = React.useRef<HTMLPreElement>(null); | ||
|
|
||
| const renderHighlight = React.useCallback( | ||
| ( | ||
| code: string, | ||
| hl?: HighlighterGeneric<BundledLanguage, BundledTheme> | null, | ||
| ) => { | ||
| const instance = hl || highlighterRef.current; | ||
| if (!instance) return; | ||
|
|
||
| try { | ||
| const html = instance.codeToHtml(code || " ", { | ||
| lang: language, | ||
| theme: "github-light", | ||
| }); | ||
| const inner = html | ||
| .replace(/^<pre[^>]*><code[^>]*>/, "") | ||
| .replace(/<\/code><\/pre>$/, ""); | ||
| setHighlighted(inner); | ||
| } catch { | ||
| setHighlighted(escapeHtml(code)); | ||
| } | ||
| }, | ||
| [language], | ||
| ); | ||
| const parentRef = React.useRef<HTMLDivElement>(null); | ||
| const editorRef = React.useRef<ShikiEditor | null>(null); | ||
|
|
||
| const languageCompartment = React.useRef(new Compartment()); | ||
| const vimCompartment = React.useRef(new Compartment()); | ||
|
|
||
| const onChangeRef = React.useRef(onChange); | ||
| onChangeRef.current = onChange; | ||
|
|
||
| const hotkeysRef = React.useRef(hotkeys); | ||
| hotkeysRef.current = hotkeys; | ||
|
|
||
| const vimEnabled = useEditorStore((s) => s.editorMode) === "vim"; | ||
|
|
||
| const { saveFile } = useFileTreeActions(); | ||
|
|
||
| const saveFileRef = React.useRef(saveFile); | ||
| saveFileRef.current = saveFile; | ||
|
|
||
|
snelusha marked this conversation as resolved.
|
||
| // biome-ignore lint/correctness/useExhaustiveDependencies: editor is recreated only on lang change; value is synced separately | ||
| React.useEffect(() => { | ||
| let isMounted = true; | ||
|
|
||
| async function initShiki() { | ||
| try { | ||
| const hl = await createHighlighter({ | ||
| themes: ["github-light"], | ||
| langs: ["ballerina", "toml"], | ||
| }); | ||
|
|
||
| if (isMounted) { | ||
| highlighterRef.current = hl; | ||
| renderHighlight(textareaRef.current?.value ?? "", hl); | ||
| } | ||
| } catch { | ||
| if (isMounted) | ||
| setHighlighted(escapeHtml(textareaRef.current?.value ?? "")); | ||
| } | ||
| } | ||
|
|
||
| initShiki(); | ||
| const parent = parentRef.current; | ||
| if (!parent) return; | ||
|
|
||
| const editor = new ShikiEditor({ | ||
| parent, | ||
| doc: value, | ||
|
warunalakshitha marked this conversation as resolved.
|
||
| lang: language, | ||
| themes: { | ||
| light: "github-light", | ||
| }, | ||
| defaultColor: "light", | ||
| themeStyle: "cm", | ||
| onUpdate: (update) => { | ||
| if (update.docChanged) | ||
| onChangeRef.current?.(update.state.doc.toString()); | ||
| }, | ||
| extensions: [ | ||
| ...baseExtensions(hotkeysRef), | ||
| languageCompartment.current.of( | ||
| language === "ballerina" ? ballerinaMode : [], | ||
| ), | ||
| vimCompartment.current.of(vimEnabled ? vim() : []), | ||
| ], | ||
| }); | ||
|
|
||
| editorRef.current = editor; | ||
|
|
||
| return () => { | ||
| isMounted = false; | ||
| highlighterRef.current?.dispose(); | ||
| highlighterRef.current = null; | ||
| editorRef.current?.destroy(); | ||
| editorRef.current = null; | ||
| }; | ||
| }, [renderHighlight]); | ||
| }, []); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| React.useEffect(() => { | ||
| renderHighlight(value); | ||
| }, [value, renderHighlight]); | ||
|
|
||
| const syncScroll = React.useCallback(() => { | ||
| if (textareaRef.current && preRef.current) { | ||
| preRef.current.scrollTop = textareaRef.current.scrollTop; | ||
| preRef.current.scrollLeft = textareaRef.current.scrollLeft; | ||
| } | ||
| }, []); | ||
| const editor = editorRef.current; | ||
| if (!editor) return; | ||
| editor.reconfigure( | ||
| languageCompartment.current, | ||
| language === "ballerina" ? ballerinaMode : [], | ||
| ); | ||
| }, [language]); | ||
|
|
||
| const handleKeyDown = React.useCallback( | ||
| (e: React.KeyboardEvent<HTMLTextAreaElement>) => { | ||
| if (e.key !== "Tab") return; | ||
|
|
||
| e.preventDefault(); | ||
| const target = e.currentTarget; | ||
| const { selectionStart, selectionEnd, value: currentValue } = target; | ||
|
|
||
| const newValue = | ||
| currentValue.slice(0, selectionStart) + | ||
| " " + | ||
| currentValue.slice(selectionEnd); | ||
| onChange?.(newValue); | ||
|
|
||
| setTimeout(() => { | ||
| if (textareaRef.current) { | ||
| textareaRef.current.setSelectionRange( | ||
| selectionStart + 4, | ||
| selectionStart + 4, | ||
| ); | ||
| } | ||
| }, 0); | ||
| }, | ||
| [onChange], | ||
| ); | ||
| React.useEffect(() => { | ||
| const editor = editorRef.current; | ||
| if (!editor) return; | ||
| editor.reconfigure(vimCompartment.current, vimEnabled ? vim() : []); | ||
| }, [vimEnabled]); | ||
|
|
||
| React.useEffect(() => { | ||
| Vim.defineEx("write", "w", () => saveFileRef.current?.()); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <div | ||
| ref={parentRef} | ||
| className={cn( | ||
| "text-[13px] relative flex overflow-hidden h-full min-h-37.5", | ||
| "relative overflow-hidden h-full min-h-37.5 cm-editor-host", | ||
| className, | ||
| )} | ||
| > | ||
| <div className="relative grow"> | ||
| <pre | ||
| ref={preRef} | ||
| aria-hidden="true" | ||
| className={cn(sharedClasses, "z-10 pointer-events-none no-scrollbar")} | ||
| // biome-ignore lint/security/noDangerouslySetInnerHtml: content is sanitized via Shiki's HTML escaping | ||
| dangerouslySetInnerHTML={{ __html: `${highlighted}\n` }} | ||
| /> | ||
| <textarea | ||
| ref={textareaRef} | ||
| value={value} | ||
| onChange={(e) => onChange?.(e.target.value)} | ||
| onScroll={syncScroll} | ||
| onKeyDown={handleKeyDown} | ||
| spellCheck={false} | ||
| autoCapitalize="off" | ||
| autoComplete="off" | ||
| autoCorrect="off" | ||
| className={cn( | ||
| sharedClasses, | ||
| "z-20 bg-transparent text-transparent caret-blue-500 outline-none resize-none no-scrollbar", | ||
| )} | ||
| /> | ||
| </div> | ||
| </div> | ||
| /> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.