Skip to content

Commit

Permalink
Refactor LaTeX AST
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Oct 10, 2019
1 parent 949983e commit 791669b
Show file tree
Hide file tree
Showing 26 changed files with 657 additions and 570 deletions.
2 changes: 2 additions & 0 deletions src/completion/latex/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl FeatureProvider for LatexLabelCompletionProvider {

if let SyntaxTree::Latex(tree) = &document.tree {
for label in tree
.structure
.labels
.iter()
.filter(|label| label.kind == LatexLabelKind::Definition)
Expand Down Expand Up @@ -82,6 +83,7 @@ impl LatexLabelCompletionProvider {
match source {
LatexLabelReferenceSource::Everything => true,
LatexLabelReferenceSource::Math => tree
.env
.environments
.iter()
.filter(|env| env.left.is_math())
Expand Down
2 changes: 1 addition & 1 deletion src/completion/latex/theorem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl FeatureProvider for LatexTheoremEnvironmentCompletionProvider {
let mut items = Vec::new();
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
for theorem in &tree.theorem_definitions {
for theorem in &tree.math.theorem_definitions {
let name = theorem.name().text().to_owned();
let text_edit = TextEdit::new(context.range, name.clone());
let item = factory::environment(
Expand Down
2 changes: 1 addition & 1 deletion src/completion/latex/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl FeatureProvider for LatexUserEnvironmentCompletionProvider {
let mut items = Vec::new();
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
for environment in &tree.environments {
for environment in &tree.env.environments {
if environment.left.command == context.command
|| environment.right.command == context.command
{
Expand Down
2 changes: 1 addition & 1 deletion src/completion/preselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
let mut items = self.provider.execute(request).await;
if let SyntaxTree::Latex(tree) = &request.document().tree {
for environment in &tree.environments {
for environment in &tree.env.environments {
if let Some(name) = environment.left.name() {
let right_args = &environment.right.command.args[0];
let cond1 = right_args
Expand Down
3 changes: 2 additions & 1 deletion src/definition/latex_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl FeatureProvider for LatexCommandDefinitionProvider {
})
.for_each(|def| definitions.push(def));

tree.math_operators
tree.math
.operators
.iter()
.filter(|op| op.definition.name.text() == command.name.text())
.map(|op| LocationLink {
Expand Down
5 changes: 3 additions & 2 deletions src/definition/latex_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl LatexLabelDefinitionProvider {
if let SyntaxTree::Latex(tree) = &view.document.tree {
let outline = Outline::from(view);
let section_tree = build_section_tree(view, tree);
for label in &tree.labels {
for label in &tree.structure.labels {
if label.kind == LatexLabelKind::Definition {
let context = OutlineContext::parse(view, label, &outline);
for name in label.names() {
Expand Down Expand Up @@ -66,7 +66,8 @@ impl LatexLabelDefinitionProvider {

fn find_reference(request: &FeatureRequest<TextDocumentPositionParams>) -> Option<&LatexToken> {
if let SyntaxTree::Latex(tree) = &request.document().tree {
tree.labels
tree.structure
.labels
.iter()
.flat_map(LatexLabel::names)
.find(|label| label.range().contains(request.params.position))
Expand Down
2 changes: 1 addition & 1 deletion src/folding/latex_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl FeatureProvider for LatexEnvironmentFoldingProvider {
) -> Vec<FoldingRange> {
let mut foldings = Vec::new();
if let SyntaxTree::Latex(tree) = &request.document().tree {
for environment in &tree.environments {
for environment in &tree.env.environments {
let start = environment.left.command.end();
let end = environment.right.command.start();
foldings.push(FoldingRange {
Expand Down
2 changes: 1 addition & 1 deletion src/folding/latex_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl FeatureProvider for LatexSectionFoldingProvider {
) -> Vec<FoldingRange> {
let mut foldings = Vec::new();
if let SyntaxTree::Latex(tree) = &request.document().tree {
let sections = &tree.sections;
let sections = &tree.structure.sections;
for i in 0..sections.len() {
let current = &sections[i];
let next = sections
Expand Down
3 changes: 2 additions & 1 deletion src/highlight/latex_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ impl FeatureProvider for LatexLabelHighlightProvider {
let mut highlights = Vec::new();
if let SyntaxTree::Latex(tree) = &request.document().tree {
if let Some(name) = tree
.structure
.labels
.iter()
.flat_map(LatexLabel::names)
.find(|label| label.range().contains(request.params.position))
.map(|label| label.text())
{
for label_group in &tree.labels {
for label_group in &tree.structure.labels {
for label in label_group.names() {
if label.text() == name {
let highlight = DocumentHighlight {
Expand Down
4 changes: 2 additions & 2 deletions src/hover/latex_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl FeatureProvider for LatexLabelHoverProvider {

impl LatexLabelHoverProvider {
fn find_reference(tree: &LatexSyntaxTree, position: Position) -> Option<&LatexToken> {
for label in &tree.labels {
for label in &tree.structure.labels {
let names = label.names();
if names.len() == 1 && label.range().contains(position) {
return Some(&label.names()[0]);
Expand All @@ -55,7 +55,7 @@ impl LatexLabelHoverProvider {
) -> Option<(Arc<Document>, &'a LatexLabel)> {
for document in &view.related_documents {
if let SyntaxTree::Latex(tree) = &document.tree {
for label in &tree.labels {
for label in &tree.structure.labels {
if label.kind == LatexLabelKind::Definition {
for name in label.names() {
if name.text() == reference.text() {
Expand Down
18 changes: 12 additions & 6 deletions src/hover/latex_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ impl LatexPreviewHoverProvider {
let mut names = Vec::new();
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
tree.theorem_definitions
tree.math
.theorem_definitions
.iter()
.map(|thm| thm.name().text())
.for_each(|thm| names.push(thm));
Expand Down Expand Up @@ -206,7 +207,8 @@ impl LatexPreviewHoverProvider {
) {
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
tree.math_operators
tree.math
.operators
.iter()
.map(|op| CharStream::extract(&document.text, op.range()))
.for_each(|op| {
Expand All @@ -223,7 +225,8 @@ impl LatexPreviewHoverProvider {
) {
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
tree.theorem_definitions
tree.math
.theorem_definitions
.iter()
.map(|thm| CharStream::extract(&document.text, thm.range()))
.for_each(|thm| {
Expand Down Expand Up @@ -297,17 +300,20 @@ impl FeatureProvider for LatexPreviewHoverProvider {

if let SyntaxTree::Latex(tree) = &request.document().tree {
let mut elements = Vec::new();
tree.inlines
tree.math
.inlines
.iter()
.map(MathElement::Inline)
.for_each(|inline| elements.push(inline));

tree.equations
tree.math
.equations
.iter()
.map(MathElement::Equation)
.for_each(|eq| elements.push(eq));

tree.environments
tree.env
.environments
.iter()
.filter(|env| Self::is_math_environment(request, env))
.map(MathElement::Environment)
Expand Down
6 changes: 4 additions & 2 deletions src/reference/latex_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ impl FeatureProvider for LatexLabelReferenceProvider {
if let Some(definition) = Self::find_name(request) {
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
tree.labels
tree.structure
.labels
.iter()
.filter(|label| Self::is_included(request, label))
.flat_map(LatexLabel::names)
Expand All @@ -34,7 +35,8 @@ impl FeatureProvider for LatexLabelReferenceProvider {
impl LatexLabelReferenceProvider {
fn find_name(request: &FeatureRequest<ReferenceParams>) -> Option<&str> {
if let SyntaxTree::Latex(tree) = &request.document().tree {
tree.labels
tree.structure
.labels
.iter()
.flat_map(LatexLabel::names)
.find(|label| {
Expand Down
2 changes: 1 addition & 1 deletion src/rename/latex_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl FeatureProvider for LatexEnvironmentRenameProvider {

fn find_environment(tree: &SyntaxTree, position: Position) -> Option<&LatexEnvironment> {
if let SyntaxTree::Latex(tree) = &tree {
for environment in &tree.environments {
for environment in &tree.env.environments {
if let Some(left_name) = environment.left.name() {
if let Some(right_name) = environment.right.name() {
if left_name.range().contains(position) || right_name.range().contains(position)
Expand Down
4 changes: 3 additions & 1 deletion src/rename/latex_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl FeatureProvider for LatexLabelRenameProvider {
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
let edits = tree
.structure
.labels
.iter()
.flat_map(LatexLabel::names)
Expand All @@ -56,7 +57,8 @@ impl FeatureProvider for LatexLabelRenameProvider {

fn find_label(tree: &SyntaxTree, position: Position) -> Option<&Span> {
if let SyntaxTree::Latex(tree) = tree {
tree.labels
tree.structure
.labels
.iter()
.flat_map(LatexLabel::names)
.find(|label| label.range().contains(position))
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
}

if let SyntaxTree::Latex(tree) = &document.tree {
if tree.is_standalone {
if tree.env.is_standalone {
match diagnostics_manager.build.update(&document.uri) {
Ok(true) => self.action_manager.push(Action::PublishDiagnostics),
Ok(false) => (),
Expand Down
4 changes: 3 additions & 1 deletion src/symbol/latex_section/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use lsp_types::Range;

pub fn symbols(view: &DocumentView, tree: &LatexSyntaxTree) -> Vec<LatexSymbol> {
let mut symbols = Vec::new();
for environment in &tree.environments {
for environment in &tree.env.environments {
if environment.left.is_enum() {
symbols.push(make_symbol(view, tree, environment));
}
Expand All @@ -23,6 +23,7 @@ fn make_symbol(
let name = titlelize(enumeration.left.name().unwrap().text());

let items: Vec<_> = tree
.structure
.items
.iter()
.filter(|item| tree.is_enumeration_item(enumeration, item))
Expand Down Expand Up @@ -71,6 +72,7 @@ fn make_symbol(
fn find_item_label(tree: &LatexSyntaxTree, item_range: Range) -> Option<&LatexLabel> {
let label = tree.find_label_by_range(item_range)?;
if tree
.env
.environments
.iter()
.filter(|env| item_range.contains(env.start()))
Expand Down
4 changes: 2 additions & 2 deletions src/symbol/latex_section/equation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use lsp_types::Range;

pub fn symbols(view: &DocumentView, tree: &LatexSyntaxTree) -> Vec<LatexSymbol> {
let mut symbols = Vec::new();
for equation in &tree.equations {
for equation in &tree.math.equations {
symbols.push(make_symbol(view, tree, equation.range()));
}

for equation in &tree.environments {
for equation in &tree.env.environments {
if equation.left.is_math() {
symbols.push(make_symbol(view, tree, equation.range()));
}
Expand Down
4 changes: 3 additions & 1 deletion src/symbol/latex_section/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::syntax::*;
use crate::workspace::*;

pub fn symbols(view: &DocumentView, tree: &LatexSyntaxTree) -> Vec<LatexSymbol> {
tree.captions
tree.structure
.captions
.iter()
.filter_map(|caption| make_symbol(view, tree, caption))
.collect()
Expand All @@ -16,6 +17,7 @@ fn make_symbol(
caption: &LatexCaption,
) -> Option<LatexSymbol> {
let environment = tree
.env
.environments
.iter()
.find(|env| tree.is_direct_child(env, caption.start()))?;
Expand Down
6 changes: 4 additions & 2 deletions src/symbol/latex_section/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ pub fn build_section_tree<'a>(
fn compute_end_position(tree: &LatexSyntaxTree, text: &str) -> Position {
let mut stream = CharStream::new(text);
while stream.next().is_some() {}
tree.environments
tree.env
.environments
.iter()
.find(|env| env.left.name().map(LatexToken::text) == Some("document"))
.map(|env| env.right.start())
Expand Down Expand Up @@ -127,6 +128,7 @@ impl<'a> LatexSectionNode<'a> {

fn set_label(&mut self, tree: &LatexSyntaxTree, view: &DocumentView, outline: &Outline) {
if let Some(label) = tree
.structure
.labels
.iter()
.filter(|label| label.kind == LatexLabelKind::Definition)
Expand Down Expand Up @@ -270,7 +272,7 @@ impl<'a> LatexSectionTree<'a> {
impl<'a> From<&'a LatexSyntaxTree> for LatexSectionTree<'a> {
fn from(tree: &'a LatexSyntaxTree) -> Self {
let mut root = Self::new();
for section in &tree.sections {
for section in &tree.structure.sections {
LatexSectionNode::insert_section(&mut root.children, section);
}
root
Expand Down
5 changes: 3 additions & 2 deletions src/symbol/latex_section/theorem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::syntax::*;
use crate::workspace::*;

pub fn symbols(view: &DocumentView, tree: &LatexSyntaxTree) -> Vec<LatexSymbol> {
tree.environments
tree.env
.environments
.iter()
.filter_map(|env| make_symbol(view, tree, env))
.collect()
Expand All @@ -19,7 +20,7 @@ fn make_symbol(

for document in &view.related_documents {
if let SyntaxTree::Latex(tree) = &document.tree {
for definition in &tree.theorem_definitions {
for definition in &tree.math.theorem_definitions {
if environment_name == definition.name().text() {
let kind = definition
.command
Expand Down
Loading

0 comments on commit 791669b

Please sign in to comment.