Skip to content

Commit

Permalink
Add suite/0 exports at beginning of export list
Browse files Browse the repository at this point in the history
Summary: This follows more closely the conventions we are currently using. In order to do this, we extend ExportBuilder so that one can optionally specify whether the function is to be added first or last (default)

Reviewed By: alanz

Differential Revision: D49916623

fbshipit-source-id: e68f31c8c20e6b17433a9a6dcef3ae50c2e43102
  • Loading branch information
jcpetruzza authored and facebook-github-bot committed Oct 5, 2023
1 parent d5de557 commit de33ecd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
57 changes: 42 additions & 15 deletions crates/ide_assists/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ fn export_suite_0(sema: &Semantic, file_id: FileId, builder: &mut SourceChangeBu
let name_arity = NameArity::new(known::suite, 0);
ExportBuilder::new(sema, file_id, &[name_arity.clone()], builder)
.group_with(NameArity::new(known::all, 0))
.export_list_pos(ExportListPosition::First)
.finish();
}

Expand Down Expand Up @@ -535,13 +536,20 @@ fn add_to_compile_attribute(

// ---------------------------------------------------------------------

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub(crate) enum ExportListPosition {
First,
Last,
}

pub(crate) struct ExportBuilder<'a> {
sema: &'a Semantic<'a>,
file_id: FileId,
funs: &'a [NameArity],
// `group_with`: Add `funs` to the same export as this, if found.
// If it is added to the existing export, the comment is not used.
group_with: Option<NameArity>,
export_list_pos: ExportListPosition,
insert_at: Option<TextSize>,
with_comment: Option<String>,
builder: &'a mut SourceChangeBuilder,
Expand All @@ -559,6 +567,7 @@ impl<'a> ExportBuilder<'a> {
file_id,
funs,
group_with: None,
export_list_pos: ExportListPosition::Last,
insert_at: None,
with_comment: None,
builder,
Expand All @@ -570,6 +579,11 @@ impl<'a> ExportBuilder<'a> {
self
}

pub(crate) fn export_list_pos(mut self, pos: ExportListPosition) -> ExportBuilder<'a> {
self.export_list_pos = pos;
self
}

pub(crate) fn insert_at(mut self, location: TextSize) -> ExportBuilder<'a> {
self.insert_at = Some(location);
self
Expand Down Expand Up @@ -601,7 +615,7 @@ impl<'a> ExportBuilder<'a> {
.clone()
.any(|fa| &form_list[fa].name == group_with)
})?;
add_to_export(export, &source, &export_text)
self.add_to_export(export, &source, &export_text)
}() {
(insert, text)
} else {
Expand All @@ -615,7 +629,7 @@ impl<'a> ExportBuilder<'a> {
// One existing export, add the function to it.

let (_, export) = form_list.exports().next()?;
add_to_export(export, &source, &export_text)
self.add_to_export(export, &source, &export_text)
} else {
// Multiple
None
Expand Down Expand Up @@ -654,19 +668,32 @@ impl<'a> ExportBuilder<'a> {
None => (insert, format!("\n-export([{export_text}]).\n")),
}
}
}

fn add_to_export(
export: &hir::Export,
source: &elp_syntax::SourceFile,
export_text: &String,
) -> Option<(TextSize, String)> {
let export_ast = export.form_id.get(source);
if let Some(fa) = export_ast.funs().last() {
Some((fa.syntax().text_range().end(), format!(", {export_text}")))
} else {
// Empty export list
let range = find_next_token(export_ast.syntax(), SyntaxKind::ANON_LBRACK)?;
Some((range.end(), export_text.clone()))
fn add_to_export(
&self,
export: &hir::Export,
source: &elp_syntax::SourceFile,
export_text: &String,
) -> Option<(TextSize, String)> {
let export_ast = export.form_id.get(source);

let maybe_added = match self.export_list_pos {
ExportListPosition::First => export_ast
.funs()
.next()
.map(|fa| (fa.syntax().text_range().start(), format!("{export_text}, "))),
ExportListPosition::Last => export_ast
.funs()
.last()
.map(|fa| (fa.syntax().text_range().end(), format!(", {export_text}"))),
};
match maybe_added {
Some(result) => Some(result),
None => {
// Empty export list
let range = find_next_token(export_ast.syntax(), SyntaxKind::ANON_LBRACK)?;
Some((range.end(), export_text.clone()))
}
}
}
}
2 changes: 1 addition & 1 deletion crates/ide_assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ fn add_to_suite_grouped_export() {
-export([other/0]).
-export([all/0, suite/0]).
-export([suite/0, all/0]).
suite() ->
[{timetrap, {seconds, 30}}].
Expand Down

0 comments on commit de33ecd

Please sign in to comment.