Skip to content
项目

CodeEditApp/CodeEditSourceEditor

在 GitHub 查看

关于

使用 Swift 编写、由 tree-sitter 驱动的代码编辑器视图。

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

CodeEditSourceEditor

An Xcode-inspired code editor view written in Swift powered by tree-sitter for CodeEdit. Features include syntax highlighting (based on the provided theme), code completion, find and replace, text diff, validation, current line highlighting, minimap, inline messages (warnings and errors), bracket matching, and more.

social-cover-textview

GitHub release Github Tests GitHub Repo stars GitHub forks Discord Badge

[!IMPORTANT] CodeEditSourceEditor is currently in development and it is not ready for production use.
Please check back later for updates on this project. Contributors are welcome as we build out the features mentioned above!

Documentation

This package is fully documented here.

Usage (SwiftUI)

CodeEditSourceEditor provides two APIs for creating an editor: SwiftUI and AppKit. The SwiftUI API provides extremely customizable and flexible configuration options, including two-way bindings for state like cursor positions and scroll position.

For more complex features that require access to the underlying text view or text storage, we've developed the TextViewCoordinators API. Using this API, developers can inject custom behavior into the editor as events happen, without having to work with state or bindings.

import CodeEditSourceEditor

struct ContentView: View {
    @State var text = "let x = 1.0"
    
   /// Automatically updates with cursor positions, scroll position, find panel text.
    /// Everything in this object is two-way, use it to update cursor positions, scroll position, etc.
    @State var editorState = SourceEditorState()
    
    /// Configure the editor's appearance, features, and editing behavior...
    @State var theme = EditorTheme(...)
    @State var font = NSFont.monospacedSystemFont(ofSize: 11, weight: .regular)
    @State var indentOption = .spaces(count: 4)

    /// *Powerful* customization options with our text view coordinators API 
    @State var autoCompleteCoordinator = AutoCompleteCoordinator()

    var body: some View { 
        SourceEditor(
            $text,
            language: language,
            // Tons of customization options, with good defaults to get started quickly.
            configuration: SourceEditorConfiguration(
                appearance: .init(theme: theme, font: font),
                behavior: .init(indentOption: indentOption)
            ),
            state: $editorState,
            coordinators: [autoCompleteCoordinator]
        )
    }
    
    /// Autocompletes "Hello" to "Hello world!" whenever it's typed.
    final class AutoCompleteCoordinator: TextViewCoordinator {
        func prepareCoordinator(controller: TextViewController) { }

        func textViewDidChangeText(controller: TextViewController) {
            for cursorPosition in controller.cursorPositions where cursorPosition.range.location >= 5 {
                let location = cursorPosition.range.location
                let previousRange = NSRange(start: location - 5, end: location)
                let string = (controller.text as NSString).substring(with: previousRange)

                if string.lowercased() == "hello" {
                    controller.textView.replaceCharacters(in: NSRange(location: location, length: 0), with: " world!")
                }
            }
        }
    }
}

An AppKit API is also available.

Currently Supported Languages

See this issue https://github.com/CodeEditApp/CodeEditLanguages/issues/10 on CodeEditLanguages for more information on supported languages.

Dependencies

Special thanks to Matt Massicotte for the great work he's done!

Package Source Author
SwiftTreeSitter GitHub Matt Massicotte

License

Licensed under the MIT license.

Related Repositories

        CodeEdit        

CodeEditTextView

CodeEditLanguages

CodeEditCLI

     CodeEditKit     

探索相关项目

加载中…