krzyzanowskim / STTextView-Plugin-Neon

Source Code Syntax Highlighting
MIT License
33 stars 13 forks source link
plugin sttextview textview

STTextView Source Code Syntax Highlighting with TreeSitter and Neon.

https://github.com/user-attachments/assets/910b9862-c682-4dcc-ae0e-dbb55e8a3fe5

Installation

Add the plugin package as a dependency of your application, then register/add it to the STTextView instance:

import STPluginNeon

textView.addPlugin(
    NeonPlugin(
        theme: .default,
        language: .swift
    )
)

SwiftUI:

import SwiftUI
import STTextViewUI
import STPluginNeon

struct ContentView: View {
    @State private var text: AttributedString = ""
    @State private var selection: NSRange?
    var body: some View {
        STTextViewUI.TextView(
            text: $text,
            selection: $selection,
            options: [.wrapLines, .highlightSelectedLine],
            plugins: [NeonPlugin(theme: .default, language: .swift)]
        )
        .textViewFont(.monospacedDigitSystemFont(ofSize: NSFont.systemFontSize, weight: .regular))
        .onAppear {
            loadContent()
        }
    }

    private func loadContent() {
        // (....)
        self.text = AttributedString(string)
    }
}