Skip to content

Commit

Permalink
Merge pull request #425 from Enselic/find_id-helper
Browse files Browse the repository at this point in the history
SyntaxSetBuilder: De-duplicate `link_ref()` with `find_id()` helper
  • Loading branch information
trishume authored Mar 1, 2022
2 parents f51e41b + b168339 commit 4293ebf
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/parsing/syntax_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,22 +750,14 @@ impl SyntaxSetBuilder {
}
}
ByScope { scope, ref sub_context } => {
let context_name = sub_context.as_ref().map_or("main", |x| &**x);
syntaxes
.iter()
.enumerate()
.rev()
.find(|index_and_syntax| index_and_syntax.1.scope == scope)
.and_then(|index_and_syntax| all_context_ids[index_and_syntax.0].get(context_name))
Self::find_id(sub_context, all_context_ids, syntaxes, |index_and_syntax| {
index_and_syntax.1.scope == scope
})
}
File { ref name, ref sub_context } => {
let context_name = sub_context.as_ref().map_or("main", |x| &**x);
syntaxes
.iter()
.enumerate()
.rev()
.find(|index_and_syntax| &index_and_syntax.1.name == name)
.and_then(|index_and_syntax| all_context_ids[index_and_syntax.0].get(context_name))
Self::find_id(sub_context, all_context_ids, syntaxes, |index_and_syntax| {
&index_and_syntax.1.name == name
})
}
Direct(_) => None,
};
Expand All @@ -775,6 +767,21 @@ impl SyntaxSetBuilder {
}
}

fn find_id<'a>(
sub_context: &Option<String>,
all_context_ids: &'a [HashMap<String, ContextId>],
syntaxes: &'a [SyntaxReference],
predicate: impl FnMut(&(usize, &SyntaxReference)) -> bool,
) -> Option<&'a ContextId> {
let context_name = sub_context.as_ref().map_or("main", |x| &**x);
syntaxes
.iter()
.enumerate()
.rev()
.find(predicate)
.and_then(|index_and_syntax| all_context_ids[index_and_syntax.0].get(context_name))
}

fn link_match_pat(
match_pat: &mut MatchPattern,
syntax_index: usize,
Expand Down

0 comments on commit 4293ebf

Please sign in to comment.