Display file type

This commit is contained in:
Timothy Warren 2021-03-16 10:28:41 -04:00
parent d207cb7884
commit b765f894fc
5 changed files with 50 additions and 3 deletions

View File

@ -1,3 +1,4 @@
use crate::FileType;
use crate::Position;
use crate::Row;
use crate::SearchDirection;
@ -9,6 +10,7 @@ pub struct Document {
rows: Vec<Row>,
pub file_name: Option<String>,
dirty: bool,
file_type: FileType,
}
impl Document {
@ -27,9 +29,14 @@ impl Document {
rows,
file_name: Some(filename.to_string()),
dirty: false,
file_type: FileType::from(filename),
})
}
pub fn file_type(&self) -> String {
self.file_type.name()
}
pub fn row(&self, index: usize) -> Option<&Row> {
self.rows.get(index)
}
@ -100,6 +107,7 @@ impl Document {
}
// File has been cleaned! (Saved)
self.file_type = FileType::from(file_name);
self.dirty = false;
}

View File

@ -136,7 +136,8 @@ impl Editor {
);
let line_indicator = format!(
"{}/{}",
"{} | {}/{}",
self.document.file_type(),
self.cursor_position.y.saturating_add(1),
self.document.len()
);

35
src/filetype.rs Normal file
View File

@ -0,0 +1,35 @@
pub struct FileType {
name: String,
hl_opts: HighlightingOptions,
}
#[derive(Default)]
pub struct HighlightingOptions {
pub numbers: bool,
}
impl Default for FileType {
fn default() -> Self {
Self {
name: String::from("No filetype"),
hl_opts: HighlightingOptions::default(),
}
}
}
impl FileType {
pub fn name(&self) -> String {
self.name.clone()
}
pub fn from(file_name: &str) -> Self {
if file_name.ends_with(".rs") {
return Self {
name: String::from("Rust"),
hl_opts: HighlightingOptions { numbers: true },
};
}
Self::default()
}
}

View File

@ -2,6 +2,7 @@
#![allow(clippy::missing_errors_doc, clippy::must_use_candidate, clippy::panic)]
mod document;
mod editor;
mod filetype;
mod highlighting;
mod row;
mod terminal;
@ -10,6 +11,7 @@ pub use document::Document;
use editor::Editor;
pub use editor::Position;
pub use editor::SearchDirection;
pub use filetype::FileType;
pub use row::Row;
pub use terminal::Terminal;

View File

@ -230,8 +230,9 @@ impl Row {
&highlighting::Type::None
};
if c.is_ascii_digit()
&& (prev_is_separator || previous_highlight == &highlighting::Type::Number)
if (c.is_ascii_digit()
&& (prev_is_separator || previous_highlight == &highlighting::Type::Number))
|| (c == &'.' && previous_highlight == &highlighting::Type::Number)
{
highlighting.push(highlighting::Type::Number)
} else {