Skip to content

Implement proof of concept for formatting specific line ranges #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ syntex_syntax = "0.30"
log = "0.3"
env_logger = "0.3"
getopts = "0.2"
itertools = "0.4"
nom = "1.2.1"
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ the command line. For example `rustfmt --write-mode=display src/filename.rs`

`cargo fmt` uses `--write-mode=replace` by default.

If you want to restrict reformatting to specific sets of lines, you can use the
`--file-lines` option. It can be specified multiple times, and takes
`FILE:RANGE,RANGE,...` as an argument. `FILE` is a file name, and each `RANGE`
is an inclusive range of lines like `7-13`. For example,

```
rustfmt src/lib.rs --file-lines src/lib.rs:7-13,21-29 --file-lines src/foo.rs:10-11,15-15
```

would format lines `7-13` and `21-29` for `src/lib.rs`, and lines `10-11`, and
`15` of `src/foo.rs`. No other files would be formatted, even if they are
included as out of line modules from `src/lib.rs`.

If `rustfmt` successfully reformatted the code it will exit with `0` exit
status. Exit status `1` signals some unexpected error, like an unknown option or
a failure to read a file. Exit status `2` is returned if there are syntax errors
Expand Down
28 changes: 23 additions & 5 deletions src/bin/rustfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ extern crate env_logger;
extern crate getopts;

use rustfmt::{run, Input, Summary};
use rustfmt::config::{Config, WriteMode};
use rustfmt::config::{self, Config, ConfigType, FileLinesMap, WriteMode};

use std::{env, error};
use std::fs::{self, File};
use std::io::{self, ErrorKind, Read, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;

use getopts::{Matches, Options};

Expand Down Expand Up @@ -57,6 +56,7 @@ struct CliOptions {
skip_children: bool,
verbose: bool,
write_mode: Option<WriteMode>,
file_lines_map: Option<FileLinesMap>,
}

impl CliOptions {
Expand All @@ -66,19 +66,32 @@ impl CliOptions {
options.verbose = matches.opt_present("verbose");

if let Some(ref write_mode) = matches.opt_str("write-mode") {
if let Ok(write_mode) = WriteMode::from_str(write_mode) {
if let Ok(write_mode) = WriteMode::parse(write_mode) {
options.write_mode = Some(write_mode);
} else {
return Err(FmtError::from(format!("Invalid write-mode: {}", write_mode)));
}
}

if matches.opt_present("file-lines") {
let specs = matches.opt_strs("file-lines");
let file_lines_map = try!(specs.iter()
.map(|ref s| {
config::parse_file_lines_spec(s)
.map_err(FmtError::from)
})
.collect());

options.file_lines_map = Some(file_lines_map);
}

Ok(options)
}

fn apply_to(&self, config: &mut Config) {
fn apply_to(self, config: &mut Config) {
config.skip_children = self.skip_children;
config.verbose = self.verbose;
config.file_lines_map = self.file_lines_map;
if let Some(write_mode) = self.write_mode {
config.write_mode = write_mode;
}
Expand Down Expand Up @@ -168,6 +181,11 @@ fn make_opts() -> Options {
"Recursively searches the given path for the rustfmt.toml config file. If not \
found reverts to the input file path",
"[Path for the configuration file]");
opts.optmulti("",
"file-lines",
"Format specified line RANGEs in FILE. RANGEs are inclusive of both endpoints. \
May be specified multiple times.",
"FILE:RANGE,RANGE,...");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document this more thoroughly somewhere please (maybe in the README), needs good explanations and a concrete example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


opts
}
Expand Down Expand Up @@ -230,7 +248,7 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
config = config_tmp;
}

options.apply_to(&mut config);
options.clone().apply_to(&mut config);
error_summary.add(run(Input::File(file), &config));
}
Ok(error_summary)
Expand Down
227 changes: 227 additions & 0 deletions src/codemap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
use std::cmp;
use std::iter::FromIterator;

use itertools::Itertools;

use syntax::codemap::{BytePos, CodeMap, Span};

use comment::FindUncommented;

/// A range of lines, inclusive of both ends.
#[derive(Clone, Copy, Debug, Eq, PartialEq, RustcDecodable)]
pub struct LineRange {
pub lo: usize,
pub hi: usize,
}

impl LineRange {
#[inline]
fn is_valid(self) -> bool {
self.lo <= self.hi
}

#[inline]
pub fn contains(self, other: LineRange) -> bool {
debug_assert!(self.is_valid());
debug_assert!(other.is_valid());

self.lo <= other.lo && self.hi >= other.hi
}

#[inline]
pub fn intersects(self, other: LineRange) -> bool {
debug_assert!(self.is_valid());
debug_assert!(other.is_valid());

self.lo <= other.hi || other.lo <= self.hi
}

#[inline]
/// Returns a new `LineRange` with lines from `self` and `other` if they were adjacent or
/// intersect; returns `None` otherwise.
pub fn merge(self, other: LineRange) -> Option<LineRange> {
debug_assert!(self.is_valid());
debug_assert!(other.is_valid());

// We can't merge non-adjacent ranges.
if self.hi + 1 < other.lo || other.hi + 1 < self.lo {
None
} else {
Some(LineRange {
lo: cmp::min(self.lo, other.lo),
hi: cmp::max(self.hi, other.hi),
})
}
}
}

/// A set of lines.
///
/// The set is represented as a list of disjoint, non-adjacent ranges sorted by lower endpoint.
/// This allows efficient querying for containment of a `LineRange`.
#[derive(Clone, Debug, Eq, PartialEq, RustcDecodable)]
pub struct LineSet(Vec<LineRange>);

impl LineSet {
/// Creates an empty `LineSet`.
pub fn new() -> LineSet {
LineSet(Vec::new())
}

/// Returns `true` if the lines in `range` are all contained in `self`.
pub fn contains(&self, range: LineRange) -> bool {
// This coule be a binary search, but it's unlikely this is a performance bottleneck.
self.0.iter().any(|r| r.contains(range))
}

/// Normalizes the line ranges so they are sorted by `lo` and are disjoint: any adjacent
/// contiguous ranges are merged.
fn normalize(&mut self) {
let mut v = Vec::with_capacity(self.0.len());
{
let ranges = &mut self.0;
ranges.sort_by_key(|x| x.lo);
let merged = ranges.drain(..).coalesce(|x, y| x.merge(y).ok_or((x, y)));
v.extend(merged);
}
v.shrink_to_fit();

self.0 = v;
}
}

impl FromIterator<LineRange> for LineSet {
/// Produce a `LineSet` from `LineRange`s in `iter`.
fn from_iter<I: IntoIterator<Item = LineRange>>(iter: I) -> LineSet {
let mut ret = LineSet::new();
ret.extend(iter);

ret
}
}

impl Extend<LineRange> for LineSet {
/// Add `LineRanges` from `iter` to `self`.
fn extend<T>(&mut self, iter: T)
where T: IntoIterator<Item = LineRange>
{
self.0.extend(iter);
self.normalize();
}
}

impl IntoIterator for LineSet {
type Item = LineRange;
type IntoIter = ::std::vec::IntoIter<LineRange>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

pub trait SpanUtils {
fn span_after(&self, original: Span, needle: &str) -> BytePos;
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
fn span_before(&self, original: Span, needle: &str) -> BytePos;
}

pub trait LineRangeUtils {
/// Returns the `LineRange` that corresponds to `span` in `self`.
fn lookup_line_range(&self, span: Span) -> LineRange;
}

impl SpanUtils for CodeMap {
#[inline]
fn span_after(&self, original: Span, needle: &str) -> BytePos {
let snippet = self.span_to_snippet(original).unwrap();
let offset = snippet.find_uncommented(needle).unwrap() + needle.len();

original.lo + BytePos(offset as u32)
}

#[inline]
fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
let snippet = self.span_to_snippet(original).unwrap();
let mut offset = 0;

while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
offset += additional_offset + needle.len();
}

original.lo + BytePos(offset as u32)
}

#[inline]
fn span_before(&self, original: Span, needle: &str) -> BytePos {
let snippet = self.span_to_snippet(original).unwrap();
let offset = snippet.find_uncommented(needle).unwrap();

original.lo + BytePos(offset as u32)
}
}

impl LineRangeUtils for CodeMap {
/// Returns the `LineRange` that corresponds to `span` in `self`.
///
/// # Panics
///
/// Panics if `span` crosses a file boundary, which shouldn't happen.
fn lookup_line_range(&self, span: Span) -> LineRange {
let lo = self.lookup_char_pos(span.lo);
let hi = self.lookup_char_pos(span.hi);

assert!(lo.file.name == hi.file.name,
"span crossed file boundary: lo: {:?}, hi: {:?}",
lo,
hi);

LineRange {
lo: lo.line,
hi: hi.line,
}
}
}

#[cfg(test)]
mod test {
use super::*;

fn mk_range(lo: usize, hi: usize) -> LineRange {
assert!(lo <= hi);
LineRange { lo: lo, hi: hi }
}

#[test]
fn test_line_range_contains() {
assert_eq!(true, mk_range(1, 2).contains(mk_range(1, 1)));
assert_eq!(true, mk_range(1, 2).contains(mk_range(2, 2)));
assert_eq!(false, mk_range(1, 2).contains(mk_range(0, 0)));
assert_eq!(false, mk_range(1, 2).contains(mk_range(3, 10)));
}

#[test]
fn test_line_range_merge() {
assert_eq!(None, mk_range(1, 3).merge(mk_range(5, 5)));
assert_eq!(None, mk_range(4, 7).merge(mk_range(0, 1)));
assert_eq!(Some(mk_range(3, 7)), mk_range(3, 5).merge(mk_range(4, 7)));
assert_eq!(Some(mk_range(3, 7)), mk_range(3, 5).merge(mk_range(5, 7)));
assert_eq!(Some(mk_range(3, 7)), mk_range(3, 5).merge(mk_range(6, 7)));
assert_eq!(Some(mk_range(3, 7)), mk_range(3, 7).merge(mk_range(4, 5)));
}

#[test]
fn test_line_set_extend() {
let mut line_set = LineSet(vec![LineRange { lo: 3, hi: 4 },
LineRange { lo: 7, hi: 8 },
LineRange { lo: 10, hi: 13 }]);

// Fill in the gaps, and add one disjoint range at the end.
line_set.extend(vec![LineRange { lo: 5, hi: 6 },
LineRange { lo: 9, hi: 9 },
LineRange { lo: 14, hi: 17 },
LineRange { lo: 19, hi: 21 }]);

assert_eq!(line_set.0,
vec![LineRange { lo: 3, hi: 17 }, LineRange { lo: 19, hi: 21 }]);
}
}
Loading