aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorDJMcNab <[email protected]>2018-12-09 10:29:13 +0000
committerDJMcNab <[email protected]>2018-12-09 10:29:13 +0000
commite823db06985d6782ff3803d3a4dea67a3c18426c (patch)
treeb8377d95bdb62db3553d7bf3107c8c56052861a9 /crates
parent904438e993b4cc3c1d9269a44436c1b112de16c0 (diff)
Implement and test format hook
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_editor/src/typing.rs2
-rw-r--r--crates/tools/src/lib.rs29
-rw-r--r--crates/tools/src/main.rs17
3 files changed, 40 insertions, 8 deletions
diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs
index 9703e0371..01acdda7c 100644
--- a/crates/ra_editor/src/typing.rs
+++ b/crates/ra_editor/src/typing.rs
@@ -20,7 +20,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
20 return LocalEdit { 20 return LocalEdit {
21 edit: EditBuilder::new().finish(), 21 edit: EditBuilder::new().finish(),
22 cursor_position: None, 22 cursor_position: None,
23 } 23 };
24 } 24 }
25 Some(pos) => pos, 25 Some(pos) => pos,
26 }; 26 };
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 674b9d11f..d6c448f3b 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -1,7 +1,11 @@
1use std::{ 1use std::{
2 path::{Path, PathBuf}, 2 path::{Path, PathBuf},
3 process::{Command, Stdio}, 3 process::{Command, Stdio},
4 fs::OpenOptions,
5 io::{Write, Error, ErrorKind}
4}; 6};
7#[cfg(unix)]
8use std::os::unix::fs::OpenOptionsExt;
5 9
6use failure::bail; 10use failure::bail;
7use itertools::Itertools; 11use itertools::Itertools;
@@ -39,7 +43,7 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
39 let (start_line, name) = loop { 43 let (start_line, name) = loop {
40 match block.next() { 44 match block.next() {
41 Some((idx, line)) if line.starts_with("test ") => { 45 Some((idx, line)) if line.starts_with("test ") => {
42 break (idx, line["test ".len()..].to_string()) 46 break (idx, line["test ".len()..].to_string());
43 } 47 }
44 Some(_) => (), 48 Some(_) => (),
45 None => continue 'outer, 49 None => continue 'outer,
@@ -116,3 +120,26 @@ fn install_rustfmt() -> Result<()> {
116 ".", 120 ".",
117 ) 121 )
118} 122}
123
124pub fn install_format_hook() -> Result<()> {
125 let path = Path::new("./.git/hooks/pre-commit");
126 if !path.exists() {
127 let mut open_options = OpenOptions::new();
128 #[cfg(unix)]
129 {
130 // Set as executable
131 open_options.mode(0o770);
132 }
133 let mut file = open_options.write(true).create(true).open(path)?;
134 write!(
135 file,
136 r#"#!/bin/sh
137
138cargo format
139git update-index --add ."#
140 )?;
141 } else {
142 return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
143 }
144 Ok(())
145}
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index 36a7c83e2..9e90ac5c2 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -7,7 +7,7 @@ use std::{
7use clap::{App, Arg, SubCommand}; 7use clap::{App, Arg, SubCommand};
8use failure::bail; 8use failure::bail;
9 9
10use tools::{collect_tests, generate, run, run_rustfmt, Mode, Overwrite, Result, Test, Verify}; 10use tools::{collect_tests, generate, install_format_hook, run, run_rustfmt, Mode, Overwrite, Result, Test, Verify};
11 11
12const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; 12const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
13const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; 13const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
@@ -25,17 +25,22 @@ fn main() -> Result<()> {
25 .subcommand(SubCommand::with_name("gen-tests")) 25 .subcommand(SubCommand::with_name("gen-tests"))
26 .subcommand(SubCommand::with_name("install-code")) 26 .subcommand(SubCommand::with_name("install-code"))
27 .subcommand(SubCommand::with_name("format")) 27 .subcommand(SubCommand::with_name("format"))
28 .subcommand(SubCommand::with_name("format-hook"))
28 .get_matches(); 29 .get_matches();
29 let mode = if matches.is_present("verify") { 30 let mode = if matches.is_present("verify") {
30 Verify 31 Verify
31 } else { 32 } else {
32 Overwrite 33 Overwrite
33 }; 34 };
34 match matches.subcommand() { 35 match matches
35 ("install-code", _) => install_code_extension()?, 36 .subcommand_name()
36 ("gen-tests", _) => gen_tests(mode)?, 37 .expect("Subcommand must be specified")
37 ("gen-syntax", _) => generate(Overwrite)?, 38 {
38 ("format", _) => run_rustfmt(Overwrite)?, 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()?,
39 _ => unreachable!(), 44 _ => unreachable!(),
40 } 45 }
41 Ok(()) 46 Ok(())