Skip to content

Commit 3725276

Browse files
bors[bot]DJMcNab
andcommitted
Merge #271
271: Implement format hook r=matklad a=DJMcNab Tentatively: fixes #155. However, this does add all changes in staged files, which might not be desirable. However, I think we can't solve that without explicit support in rustfmt for it, so it should be fine. Co-authored-by: DJMcNab <[email protected]>
2 parents b9c17a6 + cbce28a commit 3725276

File tree

5 files changed

+75
-15
lines changed

5 files changed

+75
-15
lines changed

.cargo/config

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
[alias]
22
# Automatically generates the ast and syntax kinds files
3-
gen-syntax = "run --package tools -- gen-syntax"
4-
gen-tests = "run --package tools -- gen-tests"
5-
install-code = "run --package tools -- install-code"
6-
format = "run --package tools -- format"
3+
gen-syntax = "run --package tools --bin tools -- gen-syntax"
4+
gen-tests = "run --package tools --bin tools -- gen-tests"
5+
install-code = "run --package tools --bin tools -- install-code"
6+
format = "run --package tools --bin tools -- format"
7+
format-hook = "run --package tools --bin tools -- format-hook"
78

8-
render-test = "run --package ra_cli -- render-test"
9-
parse = "run --package ra_cli -- parse"
9+
render-test = "run --package ra_cli -- render-test"
10+
parse = "run --package ra_cli -- parse"

crates/ra_editor/src/typing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
2020
return LocalEdit {
2121
edit: EditBuilder::new().finish(),
2222
cursor_position: None,
23-
}
23+
};
2424
}
2525
Some(pos) => pos,
2626
};

crates/tools/src/bin/pre-commit.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::{
2+
process::{Command},
3+
};
4+
5+
use tools::{Result, run_rustfmt, run, project_root};
6+
use failure::bail;
7+
8+
fn main() -> tools::Result<()> {
9+
run_rustfmt(tools::Overwrite)?;
10+
update_staged()
11+
}
12+
13+
fn update_staged() -> Result<()> {
14+
let root = project_root();
15+
let output = Command::new("git")
16+
.arg("diff")
17+
.arg("--name-only")
18+
.arg("--cached")
19+
.current_dir(&root)
20+
.output()?;
21+
if !output.status.success() {
22+
bail!(
23+
"`git diff --name-only --cached` exited with {}",
24+
output.status
25+
);
26+
}
27+
for line in String::from_utf8(output.stdout)?.lines() {
28+
run(
29+
&format!(
30+
"git update-index --add {}",
31+
root.join(line).to_string_lossy()
32+
),
33+
".",
34+
)?;
35+
}
36+
Ok(())
37+
}

crates/tools/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{
22
path::{Path, PathBuf},
33
process::{Command, Stdio},
4+
fs::copy,
5+
io::{Error, ErrorKind}
46
};
57

68
use failure::bail;
@@ -39,7 +41,7 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
3941
let (start_line, name) = loop {
4042
match block.next() {
4143
Some((idx, line)) if line.starts_with("test ") => {
42-
break (idx, line["test ".len()..].to_string())
44+
break (idx, line["test ".len()..].to_string());
4345
}
4446
Some(_) => (),
4547
None => continue 'outer,
@@ -65,7 +67,7 @@ pub fn generate(mode: Mode) -> Result<()> {
6567
}
6668

6769
pub fn project_root() -> PathBuf {
68-
Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
70+
Path::new(&env!("CARGO_MANIFEST_DIR"))
6971
.ancestors()
7072
.nth(2)
7173
.unwrap()
@@ -116,3 +118,18 @@ fn install_rustfmt() -> Result<()> {
116118
".",
117119
)
118120
}
121+
122+
pub fn install_format_hook() -> Result<()> {
123+
let result_path = Path::new("./.git/hooks/pre-commit");
124+
if !result_path.exists() {
125+
run("cargo build --package tools --bin pre-commit", ".")?;
126+
if cfg!(windows) {
127+
copy("./target/debug/pre-commit.exe", result_path)?;
128+
} else {
129+
copy("./target/debug/pre-commit", result_path)?;
130+
}
131+
} else {
132+
return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
133+
}
134+
Ok(())
135+
}

crates/tools/src/main.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
use clap::{App, Arg, SubCommand};
88
use failure::bail;
99

10-
use tools::{collect_tests, generate, run, run_rustfmt, Mode, Overwrite, Result, Test, Verify};
10+
use tools::{collect_tests, generate, install_format_hook, run, run_rustfmt, Mode, Overwrite, Result, Test, Verify};
1111

1212
const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
1313
const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
@@ -25,17 +25,22 @@ fn main() -> Result<()> {
2525
.subcommand(SubCommand::with_name("gen-tests"))
2626
.subcommand(SubCommand::with_name("install-code"))
2727
.subcommand(SubCommand::with_name("format"))
28+
.subcommand(SubCommand::with_name("format-hook"))
2829
.get_matches();
2930
let mode = if matches.is_present("verify") {
3031
Verify
3132
} else {
3233
Overwrite
3334
};
34-
match matches.subcommand() {
35-
("install-code", _) => install_code_extension()?,
36-
("gen-tests", _) => gen_tests(mode)?,
37-
("gen-syntax", _) => generate(Overwrite)?,
38-
("format", _) => run_rustfmt(Overwrite)?,
35+
match matches
36+
.subcommand_name()
37+
.expect("Subcommand must be specified")
38+
{
39+
"install-code" => install_code_extension()?,
40+
"gen-tests" => gen_tests(mode)?,
41+
"gen-syntax" => generate(Overwrite)?,
42+
"format" => run_rustfmt(mode)?,
43+
"format-hook" => install_format_hook()?,
3944
_ => unreachable!(),
4045
}
4146
Ok(())

0 commit comments

Comments
 (0)