Skip to content
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

Remove extra newlines between -doc attributes and function definitions #110

Merged
merged 1 commit into from
Sep 25, 2024
Merged
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
7 changes: 5 additions & 2 deletions efmt_core/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ impl Form {
&self.0
}

pub(crate) fn is_func_spec(&self) -> bool {
matches!(self.0, self::forms::Form::FunSpec(_))
pub(crate) fn is_func_spec_or_doc(&self) -> bool {
matches!(
self.0,
self::forms::Form::FunSpec(_) | self::forms::Form::Doc(_)
)
}

pub(crate) fn is_func_decl(&self) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions efmt_core/src/items/atoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ impl_parse!(ExportTypeAtom, "export_type");
#[derive(Debug, Clone, Span, Format, Element)]
pub struct ModuleAtom(AtomToken);
impl_parse!(ModuleAtom, "module");

#[derive(Debug, Clone, Span, Format, Element)]
pub struct DocAtom(AtomToken);
impl_parse!(DocAtom, "doc");
23 changes: 23 additions & 0 deletions efmt_core/src/items/forms.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Erlang top-level components such as attributes, directives or declarations.
use super::atoms::DocAtom;
use super::components::Guard;
use super::symbols::RightArrowSymbol;
use crate::format::{Format, Formatter};
Expand Down Expand Up @@ -33,6 +34,7 @@ pub enum Form {
RecordDecl(RecordDecl),
Export(ExportAttr),
Module(ModuleAttr),
Doc(DocAttr),
Attr(Attr),
}

Expand All @@ -47,6 +49,7 @@ impl Format for Form {
Form::RecordDecl(x) => x.format(fmt),
Form::Export(x) => x.format(fmt),
Form::Module(x) => x.format(fmt),
Form::Doc(x) => x.format(fmt),
Form::Attr(x) => x.format(fmt),
}
fmt.write_subsequent_comments();
Expand Down Expand Up @@ -463,6 +466,12 @@ impl ExportItem {
}
}

/// `-` `doc` `$ARGS` `.`
/// - $ARGS: `(` (`$ARG` `,`?)* `)`
/// - $ARG: [Expr]
#[derive(Debug, Clone, Span, Parse, Format)]
pub struct DocAttr(AttrLike<DocAtom, AttrValue>);

/// `-` `$NAME` `$ARGS`? `.`
///
/// - $NAME: [AtomToken] | `if`
Expand Down Expand Up @@ -847,4 +856,18 @@ mod tests {
crate::assert_format!(text, Form);
}
}

#[test]
fn doc_works() {
let texts = [
r#"-doc "Adds two number together"."#,
r#"-doc(#{since => "1.0"})."#,
indoc::indoc! {"
-doc({file,
\"../doc/add.md\"})."},
];
for text in texts {
crate::assert_format!(text, Form);
}
}
}
10 changes: 5 additions & 5 deletions efmt_core/src/items/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<const ALLOW_PARTIAL_FAILURE: bool> Parse for Module<ALLOW_PARTIAL_FAILURE>
impl<const ALLOW_PARTIAL_FAILURE: bool> Format for Module<ALLOW_PARTIAL_FAILURE> {
fn format(&self, fmt: &mut Formatter) {
let mut state = FormatState {
is_last_spec: false,
is_last_spec_or_doc: false,
pending_constants: Vec::new(),
};
let mut is_last_fun_decl = false;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<const ALLOW_PARTIAL_FAILURE: bool> Format for Module<ALLOW_PARTIAL_FAILURE>
}

struct FormatState<'a> {
is_last_spec: bool,
is_last_spec_or_doc: bool,
pending_constants: Vec<&'a DefineDirective>,
}

Expand Down Expand Up @@ -168,16 +168,16 @@ impl<'a> FormatState<'a> {
}

fn insert_two_empty_newlines_if_need(&mut self, fmt: &mut Formatter, form: &'a Form) {
if form.is_func_decl() && !self.is_last_spec {
if form.is_func_decl() && !self.is_last_spec_or_doc {
fmt.flush_non_preceding_comments(form);
fmt.write_newlines(3);
}

self.is_last_spec = form.is_func_spec();
if form.is_func_spec() {
if form.is_func_spec_or_doc() && !self.is_last_spec_or_doc {
fmt.flush_non_preceding_comments(form);
fmt.write_newlines(3);
}
self.is_last_spec_or_doc = form.is_func_spec_or_doc();
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/testdata/doc.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%%---10--|----20---|----30---|----40---|----50---|
-doc "Adds two number together".
-doc #{since => "1.0", author => "Joe"}.
-doc #{since => "2.0"}.
add(One, Two) -> One + Two.


-doc #{equiv => add(One, Two, [])}.
-spec add(One :: number(), Two :: number()) -> number().
add(One, Two) -> add(One, Two, []).
2 changes: 0 additions & 2 deletions tests/testdata/long_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
-spec foo(#bar{}, #baz{}) ->
{ok, #quz{}} | {error, term()}.


-spec bar(#bar{}, #baz{}) ->
{ok, #quz{}} |
{error, term()} |
undefined.


-spec baz() ->
{ok, term()} | {error, Reason} | timeout
when Reason :: term().
3 changes: 1 addition & 2 deletions tests/testdata/otp27_strings.erl
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
-module(otp27_strings).


-doc """
First line
Second line with "\*not emphasized\* Markdown"
Third line
""".


triple_quoted_strings() ->
X = <<"""
Line 1
Expand Down
Loading