Skip to content

Commit

Permalink
detect file type
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Feb 23, 2024
1 parent 6bf7fd0 commit 86a2c9b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 19 deletions.
31 changes: 20 additions & 11 deletions src/document.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::FileType;
use crate::Position;
use crate::Row;
use crate::SearchDirection;
Expand All @@ -8,22 +9,25 @@ use std::io::{Error, Write};
pub struct Document {
rows: Vec<Row>,
pub file_name: Option<String>,
dirty: bool
dirty: bool,
file_type: FileType
}

impl Document {
pub fn open(filename: &str) -> Result<Self, std::io::Error> {
let contents = fs::read_to_string(filename)?;
let file_type = FileType::from(filename);
let mut rows = Vec::new();
for value in contents.lines() {
let mut row = Row::from(value);
row.highlight(None);
row.highlight(file_type.highlighting_options(), None);
rows.push(row);
}
Ok(Self {
rows,
file_name: Some(filename.to_string()),
dirty: false
dirty: false,
file_type
})
}
pub fn row(&self, index: usize) -> Option<&Row> {
Expand All @@ -47,13 +51,13 @@ impl Document {
if at.y == self.rows.len() {
let mut row = Row::default();
row.insert(0, c);
row.highlight(None);
row.highlight(self.file_type.highlighting_options(), None);
self.rows.push(row);
} else {
#[allow(clippy::indexing_slicing)]
let row = &mut self.rows[at.y];
row.insert(at.x, c);
row.highlight(None);
row.highlight(self.file_type.highlighting_options(), None);
}
}
#[allow(clippy::indexing_slicing)]
Expand All @@ -68,11 +72,11 @@ impl Document {
let next_row = self.rows.remove(at.y + 1);
let row = &mut self.rows[at.y];
row.append(&next_row);
row.highlight(None);
row.highlight(self.file_type.highlighting_options(), None);
} else {
let row = &mut self.rows[at.y];
row.delete(at.x);
row.highlight(None);
row.highlight(self.file_type.highlighting_options(), None);
}
}
fn insert_newline(&mut self, at: &Position) {
Expand All @@ -86,16 +90,18 @@ impl Document {
#[allow(clippy::indexing_slicing)]
let current_row = &mut self.rows[at.y];
let mut new_row = current_row.split(at.x);
current_row.highlight(None);
new_row.highlight(None);
current_row.highlight(self.file_type.highlighting_options(), None);
new_row.highlight(self.file_type.highlighting_options(), None);
self.rows.insert(at.y + 1, new_row);
}
pub fn save(&mut self) -> Result<(), Error> {
if let Some(file_name) = &self.file_name {
let mut file = fs::File::create(file_name)?;
for row in &self.rows {
self.file_type = FileType::from(file_name);
for row in &mut self.rows {
file.write_all(row.as_bytes())?;
file.write_all(b"\n")?;
row.highlight(self.file_type.highlighting_options(), None)
}
self.dirty = false;
}
Expand Down Expand Up @@ -144,7 +150,10 @@ impl Document {
}
pub fn highlight(&mut self, word: Option<&str>) {
for row in &mut self.rows {
row.highlight(word);
row.highlight(self.file_type.highlighting_options(), word);
}
}
pub fn file_type(&self) -> String {
self.file_type.name()
}
}
3 changes: 2 additions & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ impl Editor {
);
status.truncate(width);
let line_indicator = format!(
"{}/{}",
"{} | {}/{}",
self.document.file_type(),
self.cursor_position.y.saturating_add(1),
self.document.len()
);
Expand Down
42 changes: 42 additions & 0 deletions src/filetype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pub struct FileType {
name: String,
hl_opts: HighlightingOptions
}

#[derive(Default, Copy, Clone)]
pub struct HighlightingOptions {
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 highlighting_options(&self) -> HighlightingOptions {
self.hl_opts
}
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()
}
}

impl HighlightingOptions {
pub fn numbers(&self) -> bool {
self.numbers
}
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
)]
mod document;
mod editor;
mod filetype;
mod row;
mod terminal;
mod highlighting;
pub use document::Document;
pub use row::Row;
pub use editor::SearchDirection;
use editor::Editor;
pub use filetype::FileType;
pub use filetype::HighlightingOptions;
pub use terminal::Terminal;
pub use editor::Position;

Expand Down
19 changes: 12 additions & 7 deletions src/row.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::highlighting;
use crate::HighlightingOptions;
use crate::SearchDirection;
use std::{cmp, usize};
use termion::color;
Expand Down Expand Up @@ -151,7 +152,7 @@ impl Row {
}
None
}
pub fn highlight(&mut self, word: Option<&str>) {
pub fn highlight(&mut self, opts: HighlightingOptions, word: Option<&str>) {
let mut highlighting = Vec::new();
let chars: Vec<char> = self.string.chars().collect();
let mut matches = Vec::new();
Expand Down Expand Up @@ -186,12 +187,16 @@ impl Row {
} else {
&highlighting::Type::None
};

if (c.is_ascii_digit()
&& (prev_is_separator || previous_highlight == &highlighting::Type::Number))
|| (c == &'.' && previous_highlight == &highlighting::Type::Number)
{
highlighting.push(highlighting::Type::Number);

if opts.numbers() {
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 {
highlighting.push(highlighting::Type::None);
}
} else {
highlighting.push(highlighting::Type::None);
}
Expand Down

0 comments on commit 86a2c9b

Please sign in to comment.