Skip to content

Commit dc4173c

Browse files
committed
use a single query for injections
In the past we used two separate queries for combined and normal injections. There was no real reason for this (except historical/slightly easier implementation). Instead, we now use a single query and simply check if an injection corresponds to a combined injection or not.
1 parent 2813e2c commit dc4173c

File tree

1 file changed

+43
-59
lines changed

1 file changed

+43
-59
lines changed

helix-core/src/syntax.rs

+43-59
Original file line numberDiff line numberDiff line change
@@ -1135,13 +1135,38 @@ impl Syntax {
11351135
layer.tree().root_node(),
11361136
RopeProvider(source_slice),
11371137
);
1138+
let mut combined_injections = vec![
1139+
(None, Vec::new(), IncludedChildren::default());
1140+
layer.config.combined_injections_patterns.len()
1141+
];
11381142
let mut injections = Vec::new();
11391143
let mut last_injection_end = 0;
11401144
for mat in matches {
11411145
let (injection_capture, content_node, included_children) = layer
11421146
.config
11431147
.injection_for_match(&layer.config.injections_query, &mat, source_slice);
11441148

1149+
// in case this is a combined injection save it for more processing later
1150+
if let Some(combined_injection_idx) = layer
1151+
.config
1152+
.combined_injections_patterns
1153+
.iter()
1154+
.position(|&pattern| pattern == mat.pattern_index)
1155+
{
1156+
let entry = &mut combined_injections[combined_injection_idx];
1157+
if injection_capture.is_some() {
1158+
entry.0 = injection_capture;
1159+
}
1160+
if let Some(content_node) = content_node {
1161+
if content_node.start_byte() >= last_injection_end {
1162+
entry.1.push(content_node);
1163+
last_injection_end = content_node.end_byte();
1164+
}
1165+
}
1166+
entry.2 = included_children;
1167+
continue;
1168+
}
1169+
11451170
// Explicitly remove this match so that none of its other captures will remain
11461171
// in the stream of captures.
11471172
mat.remove();
@@ -1166,43 +1191,13 @@ impl Syntax {
11661191
}
11671192
}
11681193

1169-
// Process combined injections.
1170-
if let Some(combined_injections_query) = &layer.config.combined_injections_query {
1171-
let mut injections_by_pattern_index =
1172-
vec![
1173-
(None, Vec::new(), IncludedChildren::default());
1174-
combined_injections_query.pattern_count()
1175-
];
1176-
let matches = cursor.matches(
1177-
combined_injections_query,
1178-
layer.tree().root_node(),
1179-
RopeProvider(source_slice),
1180-
);
1181-
for mat in matches {
1182-
let entry = &mut injections_by_pattern_index[mat.pattern_index];
1183-
let (injection_capture, content_node, included_children) = layer
1184-
.config
1185-
.injection_for_match(combined_injections_query, &mat, source_slice);
1186-
if injection_capture.is_some() {
1187-
entry.0 = injection_capture;
1188-
}
1189-
if let Some(content_node) = content_node {
1190-
entry.1.push(content_node);
1191-
}
1192-
entry.2 = included_children;
1193-
}
1194-
for (lang_name, content_nodes, included_children) in injections_by_pattern_index
1195-
{
1196-
if let (Some(lang_name), false) = (lang_name, content_nodes.is_empty()) {
1197-
if let Some(config) = (injection_callback)(&lang_name) {
1198-
let ranges = intersect_ranges(
1199-
&layer.ranges,
1200-
&content_nodes,
1201-
included_children,
1202-
);
1203-
if !ranges.is_empty() {
1204-
injections.push((config, ranges));
1205-
}
1194+
for (lang_name, content_nodes, included_children) in combined_injections {
1195+
if let (Some(lang_name), false) = (lang_name, content_nodes.is_empty()) {
1196+
if let Some(config) = (injection_callback)(&lang_name) {
1197+
let ranges =
1198+
intersect_ranges(&layer.ranges, &content_nodes, included_children);
1199+
if !ranges.is_empty() {
1200+
injections.push((config, ranges));
12061201
}
12071202
}
12081203
}
@@ -1565,7 +1560,7 @@ pub struct HighlightConfiguration {
15651560
pub language: Grammar,
15661561
pub query: Query,
15671562
injections_query: Query,
1568-
combined_injections_query: Option<Query>,
1563+
combined_injections_patterns: Vec<usize>,
15691564
highlights_pattern_index: usize,
15701565
highlight_indices: ArcSwap<Vec<Option<Highlight>>>,
15711566
non_local_variable_patterns: Vec<bool>,
@@ -1681,26 +1676,15 @@ impl HighlightConfiguration {
16811676
}
16821677
}
16831678

1684-
let mut injections_query = Query::new(language, injection_query)?;
1685-
1686-
// Construct a separate query just for dealing with the 'combined injections'.
1687-
// Disable the combined injection patterns in the main query.
1688-
let mut combined_injections_query = Query::new(language, injection_query)?;
1689-
let mut has_combined_queries = false;
1690-
for pattern_index in 0..injections_query.pattern_count() {
1691-
let settings = injections_query.property_settings(pattern_index);
1692-
if settings.iter().any(|s| &*s.key == "injection.combined") {
1693-
has_combined_queries = true;
1694-
injections_query.disable_pattern(pattern_index);
1695-
} else {
1696-
combined_injections_query.disable_pattern(pattern_index);
1697-
}
1698-
}
1699-
let combined_injections_query = if has_combined_queries {
1700-
Some(combined_injections_query)
1701-
} else {
1702-
None
1703-
};
1679+
let injections_query = Query::new(language, injection_query)?;
1680+
let combined_injections_patterns = (0..injections_query.pattern_count())
1681+
.filter(|&i| {
1682+
injections_query
1683+
.property_settings(i)
1684+
.iter()
1685+
.any(|s| &*s.key == "injection.combined")
1686+
})
1687+
.collect();
17041688

17051689
// Find all of the highlighting patterns that are disabled for nodes that
17061690
// have been identified as local variables.
@@ -1749,7 +1733,7 @@ impl HighlightConfiguration {
17491733
language,
17501734
query,
17511735
injections_query,
1752-
combined_injections_query,
1736+
combined_injections_patterns,
17531737
highlights_pattern_index,
17541738
highlight_indices,
17551739
non_local_variable_patterns,

0 commit comments

Comments
 (0)