Skip to content
Open
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
27 changes: 24 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2465,10 +2465,31 @@ fn rewrite_fn_base(
let where_clause = &fn_sig.generics.where_clause;

let mut result = String::with_capacity(1024);
result.push_str(&fn_sig.to_str(context));

// fn foo
result.push_str("fn ");
// Everything before `fn`
let before_ident_span = mk_sp(span.lo(), ident.span.lo());
let fn_lo = context
.snippet_provider
.span_before_last(before_ident_span, "fn");
let span_before_fn = mk_sp(span.lo(), fn_lo);

if file_lines_contains!(context, span_before_fn) {
result.push_str(&fn_sig.to_str(context));
} else {
result.push_str(context.snippet(mk_sp(span_before_fn.lo(), fn_lo)));
}

result.push_str("fn");

// If both `fn` and the ident are selected, put them both on the same line.
// Otherwise, preserve the snippet between `fn` and the ident.
let fn_ident_span = mk_sp(fn_lo, ident.span.hi());
if file_lines_contains!(context, fn_ident_span) {
result.push(' ');
} else {
let fn_hi = fn_lo + BytePos(2);
result.push_str(context.snippet(mk_sp(fn_hi, ident.span.lo())));
}

// Generics.
let overhead = if let FnBraceStyle::SameLine = fn_brace_style {
Expand Down
2 changes: 1 addition & 1 deletion src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl SpanUtils for SnippetProvider {
offset += additional_offset + needle.len();
}

original.lo() + BytePos(offset as u32 - 1)
original.lo() + BytePos(offset as u32 - needle.len() as u32)
}

fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
Expand Down
10 changes: 10 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ macro_rules! out_of_file_lines_range {
};
}

/// Returns `true` if the given span is fully contained by the selected file lines.
macro_rules! file_lines_contains {
($self:ident, $span:expr) => {
$self
.config
.file_lines()
.contains(&$self.psess.lookup_line_range($span))
};
}

macro_rules! skip_out_of_file_lines_range_err {
($self:ident, $span:expr) => {
if out_of_file_lines_range!($self, $span) {
Expand Down
16 changes: 16 additions & 0 deletions tests/source/issue-6868-before-fn/fn_and_ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fn_and_ident.rs","range":[11,12]}]

pub
(
crate
)
const
unsafe
extern
"C"
fn
foo
(
)
{
}
16 changes: 16 additions & 0 deletions tests/source/issue-6868-before-fn/fully_selected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,11]}]

pub
(
crate
)
const
unsafe
extern
"C"
fn
foo
(
)
{
}
16 changes: 16 additions & 0 deletions tests/source/issue-6868-before-fn/partial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/partial.rs","range":[7,8]}]

pub
(
crate
)
const
unsafe
extern
"C"
fn
foo
(
)
{
}
16 changes: 16 additions & 0 deletions tests/source/issue-6868-before-fn/unselected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/unselected.rs","range":[11,14]}]

pub
(
crate
)
const
unsafe
extern
"C"
fn
foo
(
)
{
}
12 changes: 12 additions & 0 deletions tests/target/issue-6868-before-fn/fn_and_ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fn_and_ident.rs","range":[11,12]}]

pub
(
crate
)
const
unsafe
extern
"C"
fn foo() {
}
5 changes: 5 additions & 0 deletions tests/target/issue-6868-before-fn/fully_selected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/fully_selected.rs","range":[3,11]}]

pub(crate) const unsafe extern "C" fn
foo() {
}
13 changes: 13 additions & 0 deletions tests/target/issue-6868-before-fn/partial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/partial.rs","range":[7,8]}]

pub
(
crate
)
const
unsafe
extern
"C"
fn
foo() {
}
12 changes: 12 additions & 0 deletions tests/target/issue-6868-before-fn/unselected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-6868-before-fn/unselected.rs","range":[11,14]}]

pub
(
crate
)
const
unsafe
extern
"C"
fn foo() {
}
Loading