Skip to main content

CodeEditor

Struct CodeEditor 

Source
pub struct CodeEditor {
    pub syntax: &'static Syntax,
    pub wrap: bool,
    pub tab_width: usize,
    pub hard_tabs: bool,
    /* private fields */
}
Expand description

A code editor’s state (Ui::code_editor): the text and the editing state. Kept by the app, one per editor, as a Table is.

use taut::{CodeEditor, Syntax};
let mut code = CodeEditor::new("fn main() {}\n", &Syntax::RUST);
code.wrap = true; // long lines go on to the next row, in place of scrolling sideways
assert_eq!(code.text(), "fn main() {}\n");
assert_eq!(code.cursor(), (1, 1)); // line and column of the cursor, counted from 1

Fields§

§syntax: &'static Syntax

Highlighting: Syntax::RUST, or the app’s own; Syntax::PLAIN highlights nothing.

§wrap: bool

Wraps long lines instead of scrolling sideways.

§tab_width: usize

Tab width in columns; also the indent width.

§hard_tabs: bool

Tab indents with a tab character instead of spaces.

Implementations§

Source§

impl CodeEditor

Source

pub fn new(text: impl Into<String>, rules: &'static Syntax) -> CodeEditor

The text to edit, and its highlighting rules.

Examples found in repository?
examples/gallery.rs (line 1304)
1246pub fn app() -> App {
1247    App {
1248        name: "Gallery".into(),
1249        presses: 0,
1250        page: 0,
1251        room: 0,
1252        filter: String::new(),
1253        notes: String::new(),
1254        renaming: None,
1255        light: false,
1256        contrast: false,
1257        system: false,
1258        spacing: 1,
1259        ticked: false,
1260        on: true,
1261        folded_open: true,
1262        second: false,
1263        level: 72.0,
1264        bold: false,
1265        starred: true,
1266        span: 1,
1267        busy: false,
1268        from: 20.0,
1269        to: 70.0,
1270        followed: 0,
1271        finding: false,
1272        colour: 6,
1273        wanted: String::new(),
1274        picture: None,
1275        photo: None,
1276        drawings: None,
1277        animation: None,
1278        playing: false,
1279        // `TAUT_FONT=some.ttf` writes the whole window with that font, in place of the system's.
1280        letters: std::env::var_os("TAUT_FONT").and_then(|file| std::fs::read(file).ok()).and_then(FontFile::read),
1281        place: String::new(),
1282        shape: None,
1283        selected_row: 1,
1284        tree_open: [true, true],
1285        amount: 4.0,
1286        off: true,
1287        count: 12,
1288        day: Date::today(),
1289        alarm: Time::new(7, 30, 0),
1290        meeting: (Date::today(), Time::new(14, 0, 0)),
1291        away: (Date::today(), Date::from_days(Date::today().days() + 6)),
1292        marked: 3.0,
1293        zoom: 100.0,
1294        within: 2.5,
1295        fruit: "pear",
1296        faint: true,
1297        hide: false,
1298        tint: Theme::dark().accent,
1299        drag_lists: [vec!["Apples", "Pears", "Plums"], vec!["Bread"]],
1300        table: Table { frozen: 1, ..Default::default() },
1301        selection: Selection::default(),
1302        files: (0..5000u32).map(|n| (format!("file {n:04}"), n * 7919 % 4096 + 1, ["text", "picture", "sound", "folder"][n as usize * 5 % 4])).collect(),
1303        tools: Panel { open: false, ..Panel::new(520.0, 150.0, 300.0, 220.0) },
1304        code: CodeEditor::new(include_str!("gallery.rs"), &Syntax::RUST),
1305        lamp: Lamp { name: "Desk".into(), power: 40.0, on: true, kind: LampKind::Spot, tint: Color::rgb(0xffcf7a), rooms: vec!["Study".into(), "Hall".into()] },
1306        inspector: Inspector::default(),
1307        beside: SecondWindow { open: false, ..SecondWindow::new(380.0, 240.0) },
1308        file_dialog: FileDialog::default(),
1309        dock: Dock::of(&["Files"]).beside(Dock::of(&["Editor", "Preview"]).above(Dock::of(&["Log"]), 0.7), 0.25),
1310        step: 2,
1311        results: 0,
1312        query: String::new(),
1313        topics: vec!["mountains".into(), "walks".into()],
1314        topic: String::new(),
1315        stars: 3.5,
1316        face: None,
1317        liked: [true, false],
1318        loading: true,
1319        banner: true,
1320        styles: [true, false, false],
1321        section: Some(0),
1322        slices: ChartView::default(),
1323        view: ChartView::default(),
1324        dots: [0, 1, 2, 3].map(|row| (0..80u32).map(|n| chance(n + 80 * row)).collect()),
1325        live: false,
1326        load: (0..600).map(|nth| 0.5 + (nth as f32 / 40.0).sin() * 0.2).collect(),
1327        fed: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |since| since.as_secs_f64()),
1328        folders: TreeView::default(),
1329        writing: Markdown::parse(WRITING),
1330    }
1331}
Source

pub fn text(&self) -> &str

The current text.

Source

pub fn set_text(&mut self, text: impl Into<String>)

Replaces all the text: the undo history is cleared and the cursor goes to the start.

Source

pub fn cursor(&self) -> (usize, usize)

The primary cursor’s line and column, both from 1 (column in characters).

Source

pub fn go_to_line(&mut self, line: usize)

Puts the cursor at the start of a line (from 1), to be scrolled into view.

Trait Implementations§

Source§

impl Debug for CodeEditor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Its length in bytes and lines, and its settings; not the text.

Source§

impl Default for CodeEditor

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.