Skip to content

Commit

Permalink
add default settings command
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed May 21, 2024
1 parent 60c8b8d commit bf10ad0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
48 changes: 37 additions & 11 deletions crates/ferrite-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ pub struct Buffer {
col_pos: usize,
rope: Rope,
file: Option<PathBuf>,
name: String,
dirty: bool,
read_only: bool,
pub read_only: bool,
pub read_only_file: bool,
last_edit: Instant,
last_click: Instant,
Expand Down Expand Up @@ -103,6 +104,7 @@ impl Default for Buffer {
col_pos: 0,
rope: Rope::new(),
file: None,
name: String::new(),
encoding: encoding_rs::UTF_8,
indent: Indentation::Tabs(NonZeroUsize::new(1).unwrap()),
dirty: false,
Expand Down Expand Up @@ -143,7 +145,7 @@ impl Buffer {
}
}

pub fn with_path(path: impl Into<PathBuf>) -> Self {
pub fn with_path(path: impl Into<PathBuf>) -> Result<Self, anyhow::Error> {
let path = path.into();
let mut syntax = Syntax::new(get_buffer_proxy());
if let Some(language) = get_language_from_path(&path) {
Expand All @@ -153,10 +155,34 @@ impl Buffer {
syntax.update_text(Rope::new());
}

Self {
let Some(name) = path.file_name() else {
anyhow::bail!("path has no filename name");
};
let name = name.to_string_lossy().into();

Ok(Self {
name,
file: Some(path),
syntax: Some(syntax),
..Default::default()
})
}

pub fn with_name(name: impl Into<String>) -> Self {
let name = name.into();
let path = Path::new(&name);
let mut syntax = Syntax::new(get_buffer_proxy());
if let Some(language) = get_language_from_path(path) {
if let Err(err) = syntax.set_language(language) {
tracing::error!("Error setting language: {err}");
}
syntax.update_text(Rope::new());
}

Self {
name,
syntax: Some(syntax),
..Default::default()
}
}

Expand All @@ -179,10 +205,13 @@ impl Buffer {
syntax.update_text(rope.clone());
}

let name = path.file_name().unwrap().to_string_lossy().into();

Ok(Self {
indent: Indentation::detect_indent_rope(rope.slice(..)),
rope,
read_only_file,
name,
file: Some(path.into()),
encoding,
syntax: Some(syntax),
Expand All @@ -206,6 +235,9 @@ impl Buffer {

pub fn set_text(&mut self, text: &str) {
self.rope = Rope::from(text);
if let Some(ref mut syntax) = self.syntax {
syntax.update_text(self.rope.clone());
}
}

#[allow(dead_code)]
Expand Down Expand Up @@ -233,14 +265,8 @@ impl Buffer {
self.view_columns
}

pub fn name(&self) -> Option<String> {
Some(
self.file
.as_ref()?
.file_name()?
.to_string_lossy()
.to_string(),
)
pub fn name(&self) -> &str {
&self.name
}

pub fn language_name(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion crates/ferrite-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Default for PickerConfig {
}
}

const DEFAULT_CONFIG: &str = include_str!("../../../config/default.toml");
pub const DEFAULT_CONFIG: &str = include_str!("../../../config/default.toml");

impl Config {
pub fn create_default_config(overwrite: bool) -> Result<()> {
Expand Down
22 changes: 11 additions & 11 deletions crates/ferrite-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
buffer::{self, encoding::get_encoding, Buffer},
byte_size::format_byte_size,
clipboard,
config::{Config, ConfigWatcher},
config::{Config, ConfigWatcher, DEFAULT_CONFIG},
event_loop_proxy::{EventLoopControlFlow, EventLoopProxy, UserEvent},
git::branch::BranchWatcher,
indent::Indentation,
Expand Down Expand Up @@ -93,7 +93,7 @@ impl Engine {
let buffer = match Buffer::from_file(file) {
Ok(buffer) => buffer,
Err(err) => match err.kind() {
io::ErrorKind::NotFound => Buffer::with_path(file),
io::ErrorKind::NotFound => Buffer::with_path(file)?,
_ => Err(err)?,
},
};
Expand Down Expand Up @@ -627,6 +627,7 @@ impl Engine {
Command::BrowseBuffers => self.open_buffer_picker(),
Command::BrowseWorkspace => self.open_file_picker(),
Command::OpenConfig => self.open_config(),
Command::DefaultConfig => self.open_default_config(),
Command::ForceClose => self.force_close_current_buffer(),
Command::Close => self.close_current_buffer(),
Command::Paste => {
Expand Down Expand Up @@ -808,7 +809,7 @@ impl Engine {
.iter()
.filter_map(|(_, buffer)| {
if buffer.is_dirty() {
Some(buffer.name().unwrap_or_else(|| "scratch".into()))
Some(buffer.name().to_string())
} else {
None
}
Expand Down Expand Up @@ -839,21 +840,14 @@ impl Engine {
pub fn open_buffer_picker(&mut self) {
self.palette.reset();
self.file_finder = None;
let mut scratch_buffer_number = 1;
let buffers: Vec<_> = self
.workspace
.buffers
.iter()
.map(|(id, buffer)| BufferItem {
id,
dirty: buffer.is_dirty(),
name: buffer
.file()
.map(|path| path.to_string_lossy().into_owned())
.unwrap_or_else(|| {
scratch_buffer_number += 1;
format!("[Scratch] {scratch_buffer_number}")
}),
name: buffer.name().to_string(),
})
.collect();

Expand All @@ -879,6 +873,12 @@ impl Engine {
}
}

pub fn open_default_config(&mut self) {
let mut buffer = Buffer::with_name("default_config.toml");
buffer.set_text(DEFAULT_CONFIG);
self.insert_buffer(buffer, true);
}

pub fn close_current_buffer(&mut self) {
// TODO make this close any buffer
if let Some(buffer) = self.get_current_buffer() {
Expand Down
1 change: 1 addition & 0 deletions crates/ferrite-core/src/palette/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Command {
BrowseBuffers,
BrowseWorkspace,
OpenConfig,
DefaultConfig,
ForceClose,
Close,
Paste,
Expand Down
2 changes: 2 additions & 0 deletions crates/ferrite-core/src/palette/cmd_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn parse_cmd(input: &str) -> Result<Command, CommandParseError> {
("buffers", [..]) => Command::BrowseBuffers,
("browse", [..]) => Command::BrowseWorkspace,
("config", [..]) => Command::OpenConfig,
("default-config", [..]) => Command::DefaultConfig,
("close!", [..]) => Command::ForceClose,
("close", [..]) => Command::Close,
("paste", [..]) => Command::Paste,
Expand Down Expand Up @@ -124,6 +125,7 @@ static COMMANDS: Lazy<Vec<CommandTemplate>> = Lazy::new(|| {
CommandTemplate::new("buffers", None, true),
CommandTemplate::new("browse", None, true),
CommandTemplate::new("config", None, true),
CommandTemplate::new("default-config", None, true),
CommandTemplate::new("close!", None, true),
CommandTemplate::new("close", None, true),
CommandTemplate::new("paste", None, true),
Expand Down
3 changes: 1 addition & 2 deletions crates/ferrite-tui/src/widgets/editor_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ impl StatefulWidget for EditorWidget<'_> {

line_buffer.clear();
}

let mut ruler_cells = Vec::new();
if !view.lines.is_empty() && config.show_indent_rulers {
// TODO fix empty line gaps in blocks using tree-sitter indent queries
Expand Down Expand Up @@ -383,7 +382,7 @@ impl StatefulWidget for EditorWidget<'_> {
config: &self.config.info_line,
focus: self.has_focus,
encoding: buffer.encoding,
file: buffer.file(),
name: buffer.name().to_string(),
line: buffer.cursor_pos().1 + 1,
column: buffer.cursor_grapheme_column() + 1,
dirty: buffer.is_dirty(),
Expand Down
11 changes: 2 additions & 9 deletions crates/ferrite-tui/src/widgets/info_line.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::Path;

use encoding_rs::Encoding;
use ferrite_core::{byte_size::format_byte_size, config::InfoLineConfig, theme::EditorTheme};
use tui::{style::Style, widgets::Widget};
Expand All @@ -12,7 +10,7 @@ pub struct InfoLine<'a> {
pub config: &'a InfoLineConfig,
pub focus: bool,
pub encoding: &'static Encoding,
pub file: Option<&'a Path>,
pub name: String,
pub column: usize,
pub line: usize,
pub dirty: bool,
Expand All @@ -27,12 +25,7 @@ impl InfoLine<'_> {
pub fn get_info_item(&self, item: &str) -> Option<String> {
match item {
"file" => {
let mut file = self
.file
.map(|p| p.to_string_lossy())
.unwrap_or_else(|| "[scratch]".into())
.to_string();

let mut file = self.name.clone();
if self.dirty {
file += " *";
}
Expand Down

0 comments on commit bf10ad0

Please sign in to comment.