Skip to content
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
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions crates/semath-core/src/candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ pub(crate) fn structural_candidate_options(
.collect()
}

pub(crate) fn application_end_offset(
document: &ProjectDocument,
structural_path: &[u32],
occurrence_range: &crate::SourceRange,
) -> Option<u32> {
structural_path.iter().find_map(|node_id| {
let node = document.nodes.get(*node_id as usize)?;
if !matches!(
node.kind,
NotationNodeKind::NamedOperator | NotationNodeKind::Token
) || node.ranges.full.start_offset < occurrence_range.start_offset
|| node.ranges.full.end_offset > occurrence_range.end_offset
{
return None;
}
let argument = next_meaningful_sibling(document, *node_id)?;
(matches!(
argument.kind,
NotationNodeKind::Delimiter | NotationNodeKind::Group
) && argument.state == crate::SyntaxState::Complete
&& argument.ranges.full.start_offset < argument.ranges.full.end_offset)
.then_some(argument.ranges.full.end_offset)
})
}

pub(crate) fn append_semantic_candidates(
document: &ProjectDocument,
occurrence: &SourceOccurrence,
Expand Down Expand Up @@ -402,6 +427,17 @@ mod tests {
assert!(candidates.iter().all(|candidate| {
candidate.supporting_claims.is_empty() && candidate.rejecting_claims.is_empty()
}));
assert_eq!(
application_end_offset(
&document,
&[2, 0],
&SourceRange {
start_offset: 0,
end_offset: 18,
},
),
Some(21)
);
}

#[test]
Expand Down
60 changes: 54 additions & 6 deletions crates/semath-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use thiserror::Error;

use crate::binder::{binder_at, binders, bound_occurrences, rename_rejection};
use crate::candidate::{
StructuralCandidateOption, append_semantic_candidates, structural_candidate_options,
StructuralCandidateOption, append_semantic_candidates, application_end_offset,
structural_candidate_options,
};
use crate::canonical::{SemanticExpr, lower_document_region};
use crate::cross_modal::{BindingPredicate, CrossModalBinding, extract_cross_modal_bindings};
Expand Down Expand Up @@ -82,6 +83,7 @@ struct SemanticOccurrenceSeed {
source_text: String,
notation: Vec<NotationComponent>,
candidate_options: Vec<StructuralCandidateOption>,
application_end_offset: Option<u32>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -137,16 +139,18 @@ impl AnalyzedDocument {
.map(|(surface, selection_range)| {
let range = notation_occurrence_range(&document, selection_range);
let structural_path = notation_path(&document, selection_range);
let candidate_options =
structural_candidate_options(&document, &structural_path, &range, surface);
SemanticOccurrenceSeed {
kind: OccurrenceKind::Notation,
surface: surface.clone(),
selection_range: selection_range.clone(),
candidate_options: structural_candidate_options(
application_end_offset: application_end_offset(
&document,
&structural_path,
&range,
surface,
),
candidate_options,
structural_path,
source_text: source_text(&document, &range),
notation: notation_components(&document, selection_range, surface),
Expand All @@ -171,6 +175,7 @@ impl AnalyzedDocument {
value: binding.short.clone(),
}],
candidate_options: Vec::new(),
application_end_offset: None,
});
if binding.long_range != binding.short_range {
semantic_occurrences.push(SemanticOccurrenceSeed {
Expand All @@ -182,6 +187,7 @@ impl AnalyzedDocument {
source_text: source_text(&document, &binding.long_range),
notation: Vec::new(),
candidate_options: Vec::new(),
application_end_offset: None,
});
}
}
Expand Down Expand Up @@ -240,6 +246,11 @@ fn structural_command_occurrences(
surface: surface.clone(),
selection_range,
range: node.ranges.full.clone(),
application_end_offset: application_end_offset(
document,
&structural_path,
&node.ranges.full,
),
structural_path,
source_text: source_text(document, &node.ranges.full),
notation: vec![NotationComponent::NamedSurface { value: surface }],
Expand Down Expand Up @@ -799,9 +810,19 @@ fn notation_occurrence_range(document: &ProjectDocument, selection: &SourceRange
.filter(|node| {
let identity_range = match node.kind {
crate::NotationNodeKind::NamedOperator => node.ranges.name.as_ref(),
crate::NotationNodeKind::Modifier
| crate::NotationNodeKind::Style
| crate::NotationNodeKind::Script => node.ranges.nucleus.as_ref(),
crate::NotationNodeKind::Modifier | crate::NotationNodeKind::Script => {
node.ranges.nucleus.as_ref().or_else(|| {
node.arguments
.iter()
.find(|argument| argument.role == "nucleus")
.map(|argument| &argument.range)
})
}
crate::NotationNodeKind::Style => node
.arguments
.iter()
.find(|argument| argument.role == "body")
.map(|argument| &argument.range),
_ => None,
};
identity_range.is_some_and(|identity| {
Expand Down Expand Up @@ -1800,6 +1821,23 @@ fn semantic_symbol_at_cursor(
&& seed.range.end_offset == offset
}));
}
if candidates.is_empty() {
candidates.extend(document.semantic_occurrences.iter().filter(|seed| {
math.region.full_range.start_offset <= seed.selection_range.start_offset
&& seed.selection_range.end_offset <= math.region.full_range.end_offset
&& (seed.selection_range.contains(offset)
|| (seed.selection_range.start_offset < seed.selection_range.end_offset
&& seed.selection_range.end_offset == offset))
}));
}
if candidates.is_empty() {
candidates.extend(
document
.semantic_occurrences
.iter()
.filter(|seed| completes_application_at(seed, &math.region.full_range, offset)),
);
}
candidates.sort_by_key(|seed| {
(
seed.range.end_offset - seed.range.start_offset,
Expand All @@ -1816,6 +1854,16 @@ fn semantic_symbol_at_cursor(
Some((selected.surface.clone(), selected.selection_range.clone()))
}

fn completes_application_at(
seed: &SemanticOccurrenceSeed,
math_range: &SourceRange,
offset: u32,
) -> bool {
math_range.start_offset <= seed.selection_range.start_offset
&& seed.selection_range.end_offset <= math_range.end_offset
&& seed.application_end_offset == Some(offset)
}

fn symbol_range_at_cursor(
symbols: &[(String, SourceRange)],
offset: u32,
Expand Down
75 changes: 70 additions & 5 deletions crates/semath-core/src/engine_tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use super::SemathEngine;
use super::{
SemanticOccurrenceSeed, SemathEngine, completes_application_at, notation_occurrence_range,
};
use crate::candidate::StructuralCandidateOption;
use crate::parser::test_math_regions;
use crate::semantic_index::{CandidateFamily, OccurrenceKind};
use crate::{
ChangeEnvelope, DocumentLanguage, MeaningDecision, PROTOCOL_VERSION, ProjectChange,
ProjectDocument, ProjectInclude, ProjectMacro, ProjectMacroExpansion,
ProjectMacroExpansionStatus, ProjectMacroKind, ProjectSnapshot, ProjectSourceRef, Query,
QueryEnvelope, QueryValue, SourceRange,
ChangeEnvelope, DocumentLanguage, MeaningDecision, NotationArgument, NotationNode,
NotationNodeKind, NotationNodeRanges, PROTOCOL_VERSION, ProjectChange, ProjectDocument,
ProjectInclude, ProjectMacro, ProjectMacroExpansion, ProjectMacroExpansionStatus,
ProjectMacroKind, ProjectSnapshot, ProjectSourceRef, Query, QueryEnvelope, QueryValue,
SourceRange, SyntaxState,
};

fn document(file_id: &str, path: &str, content: &str, version: u64) -> ProjectDocument {
Expand Down Expand Up @@ -48,6 +53,66 @@ fn query(query: Query, inventory_version: u64, document_version: u64) -> QueryEn
}
}

fn range(start_offset: u32, end_offset: u32) -> SourceRange {
SourceRange {
start_offset,
end_offset,
}
}

#[test]
fn expands_a_style_body_to_its_exact_source_notation() {
let mut input = document("main", "main.tex", "$\\mathbf{y}$", 1);
input.nodes.push(NotationNode {
kind: NotationNodeKind::Style,
parent: None,
children: Vec::new(),
ranges: NotationNodeRanges {
full: range(1, 11),
command: Some(range(1, 8)),
name: None,
nucleus: None,
editable: Some(range(9, 10)),
},
state: SyntaxState::Complete,
name: Some("mathbf".into()),
text: None,
arguments: vec![NotationArgument {
node: 0,
role: "body".into(),
syntax: "required".into(),
range: range(9, 10),
}],
math_class: None,
provenance: None,
});
assert_eq!(
notation_occurrence_range(&input, &range(9, 10)),
range(1, 11)
);
}

#[test]
fn application_boundary_requires_a_complete_ancestor_in_the_same_math_region() {
let seed = SemanticOccurrenceSeed {
kind: OccurrenceKind::Notation,
surface: "ECE".into(),
selection_range: range(1, 5),
range: range(1, 5),
structural_path: vec![0],
source_text: "\\ECE".into(),
notation: Vec::new(),
application_end_offset: Some(8),
candidate_options: vec![StructuralCandidateOption {
family: CandidateFamily::Application,
interpretation: "application".into(),
}],
};
assert!(completes_application_at(&seed, &range(0, 9), 8));
assert!(!completes_application_at(&seed, &range(0, 9), 9));
assert!(!completes_application_at(&seed, &range(8, 12), 8));
}

#[test]
fn resolves_definition_on_both_edges_of_a_symbol() {
let content = "Let $A$ denote an event. Let $B$ denote an event. $p=\\frac{\\mathbb{P}(A \\cap B)}{\\mathbb{P}(B)}$";
Expand Down
Loading
Loading