Skip to content

Commit

Permalink
update rustfmt settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Dec 29, 2024
1 parent fa553db commit 63c0caa
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 514 deletions.
1 change: 1 addition & 0 deletions .github/workflows/fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- 'src/**.rs'
- Cargo.toml
- Cargo.lock
- rustfmt.toml

env:
CARGO_TERM_COLOR: always
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- 'src/**.rs'
- Cargo.toml
- Cargo.lock
- rustfmt.toml

env:
CARGO_TERM_COLOR: always
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- 'src/**.rs'
- Cargo.toml
- Cargo.lock

env:
CARGO_TERM_COLOR: always

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ syntect-tui = "3.0.5"
syntect = "5.2.0"
tempfile = "3.14.0"
tree-sitter = "=0.24.4"
# FIXME: https://github.com/kyu08/fzf-make/issues/410
# The latest tag is 0.1.0 and it is old now. So, I am using the latest commit hash.
tree-sitter-just = { git = "https://github.com/IndianBoy42/tree-sitter-just.git", rev = "f6d2930" }
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
edition = "2021"
max_width = 120
fn_call_width = 120
31 changes: 9 additions & 22 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,11 @@ pub fn history_file_path() -> Option<(PathBuf, String)> {
Ok(_) => {
// When testing
let cwd = env::current_dir().unwrap();
Some((
cwd.join(PathBuf::from("test_data/history")),
HISTORY_FILE_NAME.to_string(),
))
Some((cwd.join(PathBuf::from("test_data/history")), HISTORY_FILE_NAME.to_string()))
}
_ => {
home_dir().map(|home_dir| (home_dir.join(PathBuf::from(".config/fzf-make")), HISTORY_FILE_NAME.to_string()))
}
_ => home_dir().map(|home_dir| {
(
home_dir.join(PathBuf::from(".config/fzf-make")),
HISTORY_FILE_NAME.to_string(),
)
}),
}
}

Expand All @@ -161,11 +155,7 @@ pub fn create_or_update_history_file(
fs::create_dir_all(history_directory_path.clone())?;
}
let mut history_file = File::create(history_directory_path.join(history_file_name))?;
history_file.write_all(
toml::to_string(&Histories::from(new_history))
.unwrap()
.as_bytes(),
)?;
history_file.write_all(toml::to_string(&Histories::from(new_history)).unwrap().as_bytes())?;
history_file.flush()?;

Ok(())
Expand Down Expand Up @@ -264,18 +254,15 @@ args = "app1 build"
content: r#"
"#
.to_string(),
expect: Err(anyhow::anyhow!("TOML parse error at line 1, column 1\n |\n1 | \n | ^\nmissing field `histories`\n")),
expect: Err(anyhow::anyhow!(
"TOML parse error at line 1, column 1\n |\n1 | \n | ^\nmissing field `histories`\n"
)),
},
];

for case in cases {
match case.expect {
Ok(v) => assert_eq!(
v,
parse_history(case.content).unwrap(),
"\nFailed: 🚨{:?}🚨\n",
case.title,
),
Ok(v) => assert_eq!(v, parse_history(case.content).unwrap(), "\nFailed: 🚨{:?}🚨\n", case.title,),
Err(e) => assert_eq!(
e.to_string(),
parse_history(case.content).unwrap_err().to_string(),
Expand Down
53 changes: 12 additions & 41 deletions src/file/toml_old.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ impl Histories {
for h in self.history.clone() {
let mut commands: Vec<fzf_make_toml::HistoryCommand> = vec![];
for c in h.executed_targets {
commands.push(fzf_make_toml::HistoryCommand::new(
model::runner_type::RunnerType::Make,
c,
));
commands.push(fzf_make_toml::HistoryCommand::new(model::runner_type::RunnerType::Make, c));
}
// NOTE: In old format, the path includes the file name but new format does not.
let mut makefile_path = PathBuf::from(h.path);
Expand Down Expand Up @@ -69,25 +66,14 @@ mod test {
after: fzf_make_toml::Histories::new(vec![fzf_make_toml::History::new(
PathBuf::from("path"),
vec![
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"command1".to_string(),
),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"command2".to_string(),
),
fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "command1".to_string()),
fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "command2".to_string()),
],
)]),
}];

for case in cases {
assert_eq!(
case.after,
case.before.into_histories(),
"\nFailed: 🚨{:?}🚨\n",
case.title,
)
assert_eq!(case.after, case.before.into_histories(), "\nFailed: 🚨{:?}🚨\n", case.title,)
}
}

Expand Down Expand Up @@ -115,14 +101,8 @@ executed-targets = ["run", "echo1"]
fzf_make_toml::History::new(
PathBuf::from("/Users/user/code/fzf-make"),
vec![
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"test".to_string(),
),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"check".to_string(),
),
fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "test".to_string()),
fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "check".to_string()),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"spell-check".to_string(),
Expand All @@ -132,14 +112,8 @@ executed-targets = ["run", "echo1"]
fzf_make_toml::History::new(
PathBuf::from("/Users/user/code/golang/go-playground"),
vec![
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"run".to_string(),
),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"echo1".to_string(),
),
fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "run".to_string()),
fzf_make_toml::HistoryCommand::new(runner_type::RunnerType::Make, "echo1".to_string()),
],
),
])),
Expand All @@ -149,18 +123,15 @@ executed-targets = ["run", "echo1"]
content: r#"
"#
.to_string(),
expect: Err(anyhow::anyhow!("TOML parse error at line 1, column 1\n |\n1 | \n | ^\nmissing field `history`\n")),
expect: Err(anyhow::anyhow!(
"TOML parse error at line 1, column 1\n |\n1 | \n | ^\nmissing field `history`\n"
)),
},
];

for case in cases {
match case.expect {
Ok(e) => assert_eq!(
e,
parse_history(case.content).unwrap(),
"\nFailed: 🚨{:?}🚨\n",
case.title,
),
Ok(e) => assert_eq!(e, parse_history(case.content).unwrap(), "\nFailed: 🚨{:?}🚨\n", case.title,),
Err(err) => assert_eq!(
err.to_string(),
parse_history(case.content).unwrap_err().to_string(),
Expand Down
7 changes: 1 addition & 6 deletions src/model/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ pub struct Command {
}

impl Command {
pub fn new(
runner_type: runner_type::RunnerType,
args: String,
file_path: PathBuf,
line_number: u32,
) -> Self {
pub fn new(runner_type: runner_type::RunnerType, args: String, file_path: PathBuf, line_number: u32) -> Self {
Self {
runner_type,
args,
Expand Down
15 changes: 3 additions & 12 deletions src/model/histories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ impl Histories {

// Update the whole histories.
let mut new_histories = self.histories.clone();
match new_histories
.iter()
.position(|h| h.path == new_history.path)
{
match new_histories.iter().position(|h| h.path == new_history.path) {
Some(index) => {
new_histories[index] = new_history;
}
Expand Down Expand Up @@ -199,8 +196,7 @@ mod test {
for case in cases {
assert_eq!(
case.after,
case.before
.append(path_to_append.clone(), case.command_to_append),
case.before.append(path_to_append.clone(), case.command_to_append),
"\nFailed: 🚨{:?}🚨\n",
case.title,
)
Expand Down Expand Up @@ -421,12 +417,7 @@ mod test {
];

for case in cases {
assert_eq!(
case.after,
case.before.append(case.command_to_append),
"\nFailed: 🚨{:?}🚨\n",
case.title,
)
assert_eq!(case.after, case.before.append(case.command_to_append), "\nFailed: 🚨{:?}🚨\n", case.title,)
}
}
}
7 changes: 2 additions & 5 deletions src/model/js_package_manager/js_package_manager_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ impl JsPackageManager {
if let Some(object) = v.as_object() {
for (k, v) in object {
let args = k.to_string();
let line_number =
files.line_index(file, k.start() as u32).number().to_usize() as u32;
let line_number = files.line_index(file, k.start() as u32).number().to_usize() as u32;
if let Some(v) = v.as_string() {
result.push((args, v.to_string(), line_number));
}
Expand All @@ -97,9 +96,7 @@ impl JsPackageManager {

pub fn get_js_package_manager_runner(current_dir: PathBuf) -> Option<JsPackageManager> {
let entries = fs::read_dir(current_dir.clone()).unwrap();
let file_names = entries
.map(|e| e.unwrap().file_name().into_string().unwrap())
.collect();
let file_names = entries.map(|e| e.unwrap().file_name().into_string().unwrap()).collect();

JsPackageManager::new(current_dir, file_names)
}
Expand Down
12 changes: 3 additions & 9 deletions src/model/js_package_manager/yarn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,10 @@ impl Yarn {
if let Ok(workspace_package_json_paths) = package_json_in_workspace {
for path in workspace_package_json_paths {
if let Ok(c) = path_to_content::path_to_content(&path) {
if let Some((name, parsing_result)) =
js::JsPackageManager::parse_package_json(&c)
{
if let Some((name, parsing_result)) = js::JsPackageManager::parse_package_json(&c) {
for (key, _, line_number) in parsing_result {
result.push(command::Command::new(
runner_type::RunnerType::JsPackageManager(
runner_type::JsPackageManager::Yarn,
),
runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Yarn),
// yarn executes workspace script following format: `yarn workspace {package_name} {script_name}`
// e.g. `yarn workspace app4 build`
format!("workspace {} {}", name.clone(), key.as_str()),
Expand Down Expand Up @@ -168,9 +164,7 @@ impl Yarn {
.iter()
.map(|(key, _value, line_number)| {
command::Command::new(
runner_type::RunnerType::JsPackageManager(
runner_type::JsPackageManager::Yarn,
),
runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Yarn),
key.to_string(),
current_dir.clone().join(js::METADATA_FILE_NAME),
*line_number,
Expand Down
22 changes: 8 additions & 14 deletions src/model/just/just_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ impl Just {
}

fn get_command(&self, command: command::Command) -> Option<command::Command> {
self.to_commands()
.iter()
.find(|c| **c == command)
.map(|_| command)
self.to_commands().iter().find(|c| **c == command).map(|_| command)
}

fn find_justfile(current_dir: PathBuf) -> Option<PathBuf> {
Expand Down Expand Up @@ -108,14 +105,12 @@ impl Just {
'recipe: for recipes_and_its_siblings in tree.root_node().named_children(&mut tree.walk()) {
if recipes_and_its_siblings.kind() == "recipe" {
let mut should_skip = false;
recipes_and_its_siblings
.children(&mut tree.walk())
.for_each(|attr| {
let attr_name = &source_code[attr.byte_range()];
if attr_name.contains("private") {
should_skip = true;
}
});
recipes_and_its_siblings.children(&mut tree.walk()).for_each(|attr| {
let attr_name = &source_code[attr.byte_range()];
if attr_name.contains("private") {
should_skip = true;
}
});
if should_skip {
continue;
}
Expand Down Expand Up @@ -283,8 +278,7 @@ clippy:
];

for case in cases {
let commands =
Just::parse_justfile(PathBuf::from("justfile"), case.source_code.to_string());
let commands = Just::parse_justfile(PathBuf::from("justfile"), case.source_code.to_string());
assert_eq!(commands, case.expected, "{}", case.name);
}
}
Expand Down
Loading

0 comments on commit 63c0caa

Please sign in to comment.