01
已實作功能
02
開始使用
- 取得儲存庫,透過 README 確認公開版本的開發狀態與構成。
- 執行 just check 檢查專案,確認 LanguageEditor 與 TextContent 契約後整合到實作中。
請準備 Rust 與必要的開發工具,並在導入前透過連結的 README 確認環境需求。執行範例是功能入口,不保證在所有環境中運作。
依據use katana_language_editor::{LanguageEditor, TextContent};
pub fn set_markdown(editor: &mut impl LanguageEditor, markdown: &str) {
editor.set_content(TextContent {
text: markdown.to_owned(),
language: Some("markdown".to_owned()),
});
}01 / KLE
使用情境
將編輯器實現與 UI 框架分開
中立 crate 提供 TextContent、CursorPosition、Selection、EditorEvent 和 LanguageEditor,讓 KatanA 不直接依賴 egui 型別。即使替換輸入 UI,也能保持宿主依賴的 trait 契約。
嘗試使用 egui MVP 作為連線點
katana-language-editor-egui 接收 EditorConfig,繪製 egui TextEdit,並將內容更改釋出到 ContentChanged 事件。公開版本是腳手架,Floem crate不是實現,而是依賴項和trait 實現的骨架。
02 / KLE
從輸入到輸出的流程
傳遞正文和設定
宿主在 TextContent 中設定正文和 language,並通過 EditorConfig 配置字型大小、行號、自動換行和 tab 寬度。EguiLanguageEditor::set_content 替換當前正文。
將 UI 輸入轉換為事件
當檢測到 egui TextEdit response.changed() 時,實現會將當前 TextContent 作為 EditorEvent::ContentChanged 放入待處理佇列中。中立的crate不會將 egui 的 Response 型別洩露給公開合約。
宿主收集事件
宿主使用 LanguageEditor::poll_events() 檢索事件並使用 content() 讀取正文。由於public egui MVP中的cursor()返回預設的CursorPosition,因此實際的游標同步和選擇協調不能被視為已完成的功能。
03 / KLE
主要 API 與 CLI
TextContent — 文字內容- TextContent 的 text 是正文,language 是任意語言識別符號。它以 framework-neutral 資料保留 Markdown 等輸入。
游標位置和選擇- CursorPosition 有 line 和 column,Selection 有 start 和 end 範圍。公開型別雖然存在,但 egui MVP 尚未實現返回真實 cursor。
EditorConfig — 編輯器設定- EditorConfig 指定 font_size、line_numbers、word_wrap、tab_size。在 egui 實現中它用於計算 visible rows,但不代表完整編輯功能。
語言編輯器和編輯器事件- LanguageEditor 的 content、set_content、cursor 和 poll_events 是與 host 的 trait 契約。EditorEvent 定義了 ContentChanged、CursorMoved 和 SelectionChanged。
04 / KLE
透過範例理解
將 Markdown 主體設定為 egui MVP
此程式碼示例在 EditorConfig 中建立顯示設定,將 TextContent 設定為 EguiLanguageEditor,並讀取文本。併入實屏的宿主每幀呼叫show(ui),並在輸入後新增處理以poll_events()收集ContentChanged。
use katana_language_editor::{EditorConfig, LanguageEditor, TextContent};
use katana_language_editor_egui::EguiLanguageEditor;
fn main() {
let mut editor = EguiLanguageEditor::new(EditorConfig {
font_size: Some(14.0),
line_numbers: true,
word_wrap: true,
tab_size: 4,
});
editor.set_content(TextContent {
text: "# KatanA\n".to_owned(),
language: Some("markdown".to_owned()),
});
println!("{}", editor.content().text);
}05 / KLE
整合條件與責任邊界
先依賴中立 crate
KatanA 和其他宿主引用 katana-language-editor 的trait 和 DTO,並且特定於 UI 的包被分離為介面卡。正如自述檔案中所設計的,我們的政策是即使從 egui 轉移到自定義輸入介面時也保持對宿主端的中立依賴。
在腳手架範圍內接受
公開的 README 清楚地說明了 Scaffolding 並解釋了 Floem 在 v0.1.0 中仍將是一個可編譯的框架。在引入時,我們將針對正文設定和ContentChanged連線,並將游標/選擇和完成編輯操作視為未實現的風險。
依據
內容根據調查時點的公開儲存庫。可透過連結中的固定提交確認實作與使用方式。