From 75ab71865559b8ee155489ed49bfd46a5f98acb1 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sat, 24 Jan 2026 21:24:32 -0600 Subject: [PATCH] feat: add highlighting to editor --- rustfmt.toml | 1 + src/components/editor.rs | 46 ++++++++++++++++++++-------------------- src/file.rs | 9 +++++++- 3 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..b196eaa --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +tab_spaces = 2 diff --git a/src/components/editor.rs b/src/components/editor.rs index d2a52fd..7133580 100644 --- a/src/components/editor.rs +++ b/src/components/editor.rs @@ -1,32 +1,32 @@ use iced::{ - Element, Length, Theme, - widget::{container, markdown, row, text_editor}, + Element, Length, Theme, highlighter, + widget::{container, markdown, row, text_editor}, }; use crate::{constants, file::File, message::Message, state::Mode}; pub fn view<'a>(file: &'a File, mode: Mode, font_size: u32) -> Element<'a, Message> { - match mode { - Mode::Edit => { - let editor = text_editor(file.content()) - .size(font_size) - .height(Length::Fill) - .on_action(Message::Edit); + match mode { + Mode::Edit => { + let editor = text_editor(file.content()) + .highlight(file.extension().unwrap_or("txt"), highlighter::Theme::Base16Ocean) + .size(font_size) + .height(Length::Fill) + .on_action(Message::Edit); - container(row![editor]).height(Length::Fill).into() - } - Mode::Preview => { - let mut style: markdown::Style = Theme::Dark.into(); - style.font = constants::CUSTOM_FONT; - - let settings = markdown::Settings::with_text_size(font_size, style); - - let markdown_preview = - markdown::view(file.markdown(), settings).map(Message::LinkClicked); - - container(row![markdown_preview]) - .height(Length::Fill) - .into() - } + container(row![editor]).height(Length::Fill).into() } + Mode::Preview => { + let mut style: markdown::Style = Theme::Dark.into(); + style.font = constants::CUSTOM_FONT; + + let settings = markdown::Settings::with_text_size(font_size, style); + + let markdown_preview = markdown::view(file.markdown(), settings).map(Message::LinkClicked); + + container(row![markdown_preview]) + .height(Length::Fill) + .into() + } + } } diff --git a/src/file.rs b/src/file.rs index fcf1ea4..8f4fa34 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{ffi, path::{Path, PathBuf}}; use iced::widget::{markdown, text_editor}; @@ -58,6 +58,13 @@ impl File { self.path = path; } + pub fn extension(&self) -> Option<&str> { + self.path + .as_deref() + .and_then(Path::extension) + .and_then(ffi::OsStr::to_str) + } + pub fn display_name(&self) -> &str { self.path .as_deref()