mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
Implement and test format hook
This commit is contained in:
parent
904438e993
commit
e823db0698
4 changed files with 41 additions and 8 deletions
|
@ -4,6 +4,7 @@ gen-syntax = "run --package tools -- gen-syntax"
|
||||||
gen-tests = "run --package tools -- gen-tests"
|
gen-tests = "run --package tools -- gen-tests"
|
||||||
install-code = "run --package tools -- install-code"
|
install-code = "run --package tools -- install-code"
|
||||||
format = "run --package tools -- format"
|
format = "run --package tools -- format"
|
||||||
|
format-hook = "run --package tools -- format-hook"
|
||||||
|
|
||||||
render-test = "run --package ra_cli -- render-test"
|
render-test = "run --package ra_cli -- render-test"
|
||||||
parse = "run --package ra_cli -- parse"
|
parse = "run --package ra_cli -- parse"
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
|
||||||
return LocalEdit {
|
return LocalEdit {
|
||||||
edit: EditBuilder::new().finish(),
|
edit: EditBuilder::new().finish(),
|
||||||
cursor_position: None,
|
cursor_position: None,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
Some(pos) => pos,
|
Some(pos) => pos,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
use std::{
|
use std::{
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
|
fs::OpenOptions,
|
||||||
|
io::{Write, Error, ErrorKind}
|
||||||
};
|
};
|
||||||
|
#[cfg(unix)]
|
||||||
|
use std::os::unix::fs::OpenOptionsExt;
|
||||||
|
|
||||||
use failure::bail;
|
use failure::bail;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
@ -39,7 +43,7 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
|
||||||
let (start_line, name) = loop {
|
let (start_line, name) = loop {
|
||||||
match block.next() {
|
match block.next() {
|
||||||
Some((idx, line)) if line.starts_with("test ") => {
|
Some((idx, line)) if line.starts_with("test ") => {
|
||||||
break (idx, line["test ".len()..].to_string())
|
break (idx, line["test ".len()..].to_string());
|
||||||
}
|
}
|
||||||
Some(_) => (),
|
Some(_) => (),
|
||||||
None => continue 'outer,
|
None => continue 'outer,
|
||||||
|
@ -116,3 +120,26 @@ fn install_rustfmt() -> Result<()> {
|
||||||
".",
|
".",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn install_format_hook() -> Result<()> {
|
||||||
|
let path = Path::new("./.git/hooks/pre-commit");
|
||||||
|
if !path.exists() {
|
||||||
|
let mut open_options = OpenOptions::new();
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
// Set as executable
|
||||||
|
open_options.mode(0o770);
|
||||||
|
}
|
||||||
|
let mut file = open_options.write(true).create(true).open(path)?;
|
||||||
|
write!(
|
||||||
|
file,
|
||||||
|
r#"#!/bin/sh
|
||||||
|
|
||||||
|
cargo format
|
||||||
|
git update-index --add ."#
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ use std::{
|
||||||
use clap::{App, Arg, SubCommand};
|
use clap::{App, Arg, SubCommand};
|
||||||
use failure::bail;
|
use failure::bail;
|
||||||
|
|
||||||
use tools::{collect_tests, generate, run, run_rustfmt, Mode, Overwrite, Result, Test, Verify};
|
use tools::{collect_tests, generate, install_format_hook, run, run_rustfmt, Mode, Overwrite, Result, Test, Verify};
|
||||||
|
|
||||||
const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
|
const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
|
||||||
const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
|
const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
|
||||||
|
@ -25,17 +25,22 @@ fn main() -> Result<()> {
|
||||||
.subcommand(SubCommand::with_name("gen-tests"))
|
.subcommand(SubCommand::with_name("gen-tests"))
|
||||||
.subcommand(SubCommand::with_name("install-code"))
|
.subcommand(SubCommand::with_name("install-code"))
|
||||||
.subcommand(SubCommand::with_name("format"))
|
.subcommand(SubCommand::with_name("format"))
|
||||||
|
.subcommand(SubCommand::with_name("format-hook"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
let mode = if matches.is_present("verify") {
|
let mode = if matches.is_present("verify") {
|
||||||
Verify
|
Verify
|
||||||
} else {
|
} else {
|
||||||
Overwrite
|
Overwrite
|
||||||
};
|
};
|
||||||
match matches.subcommand() {
|
match matches
|
||||||
("install-code", _) => install_code_extension()?,
|
.subcommand_name()
|
||||||
("gen-tests", _) => gen_tests(mode)?,
|
.expect("Subcommand must be specified")
|
||||||
("gen-syntax", _) => generate(Overwrite)?,
|
{
|
||||||
("format", _) => run_rustfmt(Overwrite)?,
|
"install-code" => install_code_extension()?,
|
||||||
|
"gen-tests" => gen_tests(mode)?,
|
||||||
|
"gen-syntax" => generate(Overwrite)?,
|
||||||
|
"format" => run_rustfmt(mode)?,
|
||||||
|
"format-hook" => install_format_hook()?,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue