Overview
CodeView is a native view component (UIView/NSView) that displays code blocks with syntax highlighting, line numbers, and interactive controls. It uses Tree-sitter for fast, accurate syntax highlighting across multiple programming languages.
Class Definition
Properties
The theme used for styling the code view, including fonts and colors.
The programming language identifier for syntax highlighting. When empty, displays code icon. Supported languages include: swift, python, javascript, typescript, go, rust, c, cpp, java, ruby, bash, json, html, css, csharp, kotlin, sql, yaml, and their aliases.
The code content to display. When set, automatically triggers syntax highlighting and updates line numbers.
A dictionary mapping NSRange to PlatformColor for syntax highlighting. Generated by CodeHighlighter.
Optional closure called when the preview button is tapped. Receives the language name and attributed text.
Methods
Calculates the intrinsic height required to display the given content.Parameters:
content: The code string to measuretheme: The theme to use for font sizing
Subviews
The top bar containing the language label and action buttons.
Label displaying the programming language name.
Button that copies the code content to the clipboard.
Button that triggers the preview action. Only visible when
previewAction is set.Custom view that displays line numbers aligned with the code.
The text view displaying the syntax-highlighted code.
Scroll view containing the text view for horizontal scrolling.
CodeHighlighter
Overview
CodeHighlighter is a singleton that provides syntax highlighting using Tree-sitter grammars. It maintains an internal cache for performance.
Singleton Instance
Types
Methods
Highlights code content and returns a color map.Parameters:
key: Optional cache key (computed if nil)content: The code to highlightlanguage: Language identifiertheme: Theme for color selection
Generates a cache key for content and language.
HighlightMap Extension
Applies the highlight map to a string, producing an attributed string.Parameters:
content: The plain text contenttheme: Theme providing fonts and base colors
Supported Languages
The following languages are supported with their aliases:- Swift:
swift - Python:
python,py,python3 - JavaScript:
javascript,js,jsx - TypeScript:
typescript,ts - TSX:
tsx - Go:
go,golang - Rust:
rust,rs - C:
c,h - C++:
cpp,c++,cc,cxx,hpp - Java:
java - Ruby:
ruby,rb - Bash:
bash,sh,shell,zsh - JSON:
json,jsonc - HTML:
html,htm - CSS:
css - C#:
csharp,c#,cs - Kotlin:
kotlin,kt,kts - SQL:
sql - YAML:
yaml,yml
CodeViewConfiguration
Internal configuration constants for code view layout:Example Usage
Accessibility
CodeView provides accessibility support:
accessibilityLabel: ” code block” (e.g., “Swift code block”)accessibilityValue: The code content as plain text- Individual UI elements are configured for proper VoiceOver navigation
Related Components
- LineNumberView - Displays line numbers
- MarkdownTheme - Theme customization