Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 18, 2019
1 parent 8922c39 commit ed2a3bf
Show file tree
Hide file tree
Showing 32 changed files with 422 additions and 622 deletions.
33 changes: 15 additions & 18 deletions src/completion/latex/combinators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ use crate::feature::FeatureRequest;
use crate::syntax::latex::*;
use crate::syntax::SyntaxTree;
use lsp_types::{CompletionItem, CompletionParams};
use std::sync::Arc;

pub struct LatexCombinators;

impl LatexCombinators {
pub async fn command<'a, E, F>(
request: &'a FeatureRequest<CompletionParams>,
pub async fn command<E, F>(
request: &FeatureRequest<CompletionParams>,
execute: E,
) -> Vec<CompletionItem>
where
E: Fn(&'a LatexCommand) -> F,
E: Fn(Arc<LatexCommand>) -> F,
F: std::future::Future<Output = Vec<CompletionItem>>,
{
if let SyntaxTree::Latex(tree) = &request.document.tree {
let mut finder = LatexCommandFinder::new(request.params.position);
finder.visit_root(&tree.root);
if let Some(command) = finder.result {
if let Some(command) = tree.find_command(request.params.position) {
return await!(execute(command));
}
}
Expand All @@ -31,15 +30,15 @@ impl LatexCombinators {
execute: E,
) -> Vec<CompletionItem>
where
E: Fn(&'a LatexCommand) -> F,
E: Fn(Arc<LatexCommand>) -> F,
F: std::future::Future<Output = Vec<CompletionItem>>,
{
let find_command = |nodes: &[LatexNode<'a>], node_index: usize| {
if let LatexNode::Group(group) = nodes[node_index] {
if let LatexNode::Command(command) = nodes[node_index + 1] {
let find_command = |nodes: &[LatexNode], node_index: usize| {
if let LatexNode::Group(group) = &nodes[node_index] {
if let LatexNode::Command(command) = nodes[node_index + 1].clone() {
if command_names.contains(&command.name.text())
&& command.args.len() > argument_index
&& command.args[argument_index] == *group
&& &command.args[argument_index] == group
{
return Some(command);
}
Expand All @@ -48,7 +47,7 @@ impl LatexCombinators {
None
};

let find_non_empty_command = |nodes: &[LatexNode<'a>]| {
let find_non_empty_command = |nodes: &[LatexNode]| {
if nodes.len() >= 3 {
if let LatexNode::Text(_) = nodes[0] {
return find_command(nodes, 1);
Expand All @@ -57,7 +56,7 @@ impl LatexCombinators {
None
};

let find_empty_command = |nodes: &[LatexNode<'a>]| {
let find_empty_command = |nodes: &[LatexNode]| {
if nodes.len() >= 2 {
find_command(nodes, 0)
} else {
Expand All @@ -66,9 +65,7 @@ impl LatexCombinators {
};

if let SyntaxTree::Latex(tree) = &request.document.tree {
let mut finder = LatexFinder::new(request.params.position);
finder.visit_root(&tree.root);
let mut nodes = finder.results;
let mut nodes = tree.find(request.params.position);
nodes.reverse();

let command = find_non_empty_command(&nodes).or_else(|| find_empty_command(&nodes));
Expand All @@ -78,7 +75,7 @@ impl LatexCombinators {
.range
.contains_exclusive(request.params.position)
{
return await!(execute(command));
return await!(execute(Arc::clone(&command)));
}
}
}
Expand All @@ -90,7 +87,7 @@ impl LatexCombinators {
execute: E,
) -> Vec<CompletionItem>
where
E: Fn(&LatexCommand) -> F,
E: Fn(Arc<LatexCommand>) -> F,
F: std::future::Future<Output = Vec<CompletionItem>>,
{
await!(Self::argument(&request, &ENVIRONMENT_COMMANDS, 0, execute))
Expand Down
6 changes: 1 addition & 5 deletions src/completion/latex/data/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::syntax::latex::{LatexIncludeAnalyzer, LatexVisitor};
use crate::syntax::SyntaxTree;
use crate::workspace::Document;
use itertools::Itertools;
Expand Down Expand Up @@ -29,10 +28,7 @@ impl LatexComponentDatabase {
let mut start_components = Vec::new();
for document in documents {
if let SyntaxTree::Latex(tree) = &document.tree {
let mut analyzer = LatexIncludeAnalyzer::new();
analyzer.visit_root(&tree.root);
analyzer
.included_components
tree.components
.iter()
.flat_map(|file| self.find(&file))
.for_each(|component| start_components.push(component))
Expand Down
2 changes: 1 addition & 1 deletion src/completion/latex/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl LatexIncludeCompletionProvider {
.into_iter()
{
if let Ok(entry) = entry {
if entry.file_type().is_file() && is_included(command, &entry.path()) {
if entry.file_type().is_file() && is_included(&command, &entry.path()) {
let mut path = entry.into_path();
if NO_EXTENSION_COMMANDS.contains(&command.name.text()) {
remove_extension(&mut path);
Expand Down
8 changes: 3 additions & 5 deletions src/completion/latex/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ impl LatexLabelCompletionProvider {
let mut items = Vec::new();
for document in &request.related_documents {
if let SyntaxTree::Latex(tree) = &document.tree {
let mut analyzer = LatexLabelAnalyzer::new();
analyzer.visit_root(&tree.root);
analyzer
tree
.labels
.iter()
.filter(|label| label.kind == LatexLabelKind::Definition)
.map(|label| Cow::from(label.name.text().to_owned()))
.filter(|label| label.kind() == LatexLabelKind::Definition)
.map(|label| Cow::from(label.name().text().to_owned()))
.map(factory::create_label)
.for_each(|item| items.push(item))
}
Expand Down
5 changes: 1 addition & 4 deletions src/completion/latex/user_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::completion::factory;
use crate::completion::factory::LatexComponentId;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::FeatureRequest;
use crate::syntax::latex::{LatexCommandAnalyzer, LatexVisitor};
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use itertools::Itertools;
Expand All @@ -19,9 +18,7 @@ impl LatexUserCommandCompletionProvider {
let mut items = Vec::new();
for document in &request.related_documents {
if let SyntaxTree::Latex(tree) = &document.tree {
let mut analyzer = LatexCommandAnalyzer::new();
analyzer.visit_root(&tree.root);
analyzer
tree
.commands
.iter()
.filter(|command| command.range() != current_command.range())
Expand Down
52 changes: 27 additions & 25 deletions src/completion/quality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use crate::workspace::Document;
use lsp_types::{CompletionItem, CompletionParams, Position};
use std::borrow::Cow;

pub struct OrderByQualityCompletionProvider;

Expand All @@ -19,61 +20,62 @@ impl OrderByQualityCompletionProvider {
{
let query = Self::get_query(&request.document, request.params.position);
let mut items = await!(execute(&request));
items.sort_by_key(|item| -Self::get_quality(query, &item.label));
items.sort_by_key(|item| -Self::get_quality(&query, &item.label));
items
}

fn get_query(document: &Document, position: Position) -> Option<&str> {
fn get_query(document: &Document, position: Position) -> Option<Cow<str>> {
match &document.tree {
SyntaxTree::Latex(tree) => {
let mut command_finder = LatexCommandFinder::new(position);
command_finder.visit_root(&tree.root);
let mut node = command_finder.result.map(LatexNode::Command).or_else(|| {
let mut finder = LatexFinder::new(position);
finder.visit_root(&tree.root);
finder.results.into_iter().last()
})?;
let node = tree
.find_command(position)
.map(LatexNode::Command)
.or_else(|| tree.find(position).into_iter().last())?;

match node {
LatexNode::Root(_) | LatexNode::Group(_) => Some(""),
LatexNode::Command(command) => Some(&command.name.text()[1..]),
LatexNode::Text(text) => text.words.last().map(LatexToken::text),
LatexNode::Root(_) | LatexNode::Group(_) => Some(Cow::from("")),
LatexNode::Command(command) => {
Some(Cow::from(command.name.text()[1..].to_owned()))
}
LatexNode::Text(text) => {
text.words.last().map(|w| Cow::from(w.text().to_owned()))
}
}
}
SyntaxTree::Bibtex(tree) => {
fn get_type_query(ty: &BibtexToken, position: Position) -> Option<&str> {
fn get_type_query(ty: &BibtexToken, position: Position) -> Option<Cow<str>> {
if ty.range().contains(position) {
Some(&ty.text()[1..])
Some(Cow::from(&ty.text()[1..]))
} else {
Some("")
Some(Cow::from(""))
}
}
let mut finder = BibtexFinder::new(position);
finder.visit_root(&tree.root);
match finder.results.pop()? {
BibtexNode::Root(_) => Some(""),
BibtexNode::Root(_) => Some(Cow::from("")),
BibtexNode::Preamble(preamble) => get_type_query(&preamble.ty, position),
BibtexNode::String(string) => get_type_query(&string.ty, position),
BibtexNode::Entry(entry) => get_type_query(&entry.ty, position),
BibtexNode::Comment(comment) => Some(comment.token.text()),
BibtexNode::Comment(comment) => Some(Cow::from(comment.token.text())),
BibtexNode::Field(field) => {
if field.name.range().contains(position) {
Some(field.name.text())
Some(Cow::from(field.name.text()))
} else {
Some("")
Some(Cow::from(""))
}
}
BibtexNode::Word(word) => Some(word.token.text()),
BibtexNode::Command(command) => Some(&command.token.text()[1..]),
BibtexNode::Word(word) => Some(Cow::from(word.token.text())),
BibtexNode::Command(command) => Some(Cow::from(&command.token.text()[1..])),
BibtexNode::QuotedContent(_)
| BibtexNode::BracedContent(_)
| BibtexNode::Concat(_) => Some(""),
| BibtexNode::Concat(_) => Some(Cow::from("")),
}
}
}
}

fn get_quality(query: Option<&str>, label: &str) -> i32 {
fn get_quality(query: &Option<Cow<str>>, label: &str) -> i32 {
if let Some(query) = query {
if label == query {
return 7;
Expand All @@ -83,15 +85,15 @@ impl OrderByQualityCompletionProvider {
return 6;
}

if label.starts_with(query) {
if label.starts_with(query.as_ref()) {
return 5;
}

if label.to_lowercase().starts_with(&query.to_lowercase()) {
return 4;
}

if label.contains(query) {
if label.contains(query.as_ref()) {
return 3;
}

Expand Down
11 changes: 4 additions & 7 deletions src/definition/latex_citation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::feature::FeatureRequest;
use crate::syntax::bibtex::BibtexDeclaration;
use crate::syntax::latex::{LatexCitationAnalyzer, LatexVisitor};
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use crate::workspace::Document;
Expand Down Expand Up @@ -37,13 +36,11 @@ impl LatexCitationDefinitionProvider {

fn find_reference(request: &FeatureRequest<TextDocumentPositionParams>) -> Option<&str> {
if let SyntaxTree::Latex(tree) = &request.document.tree {
let mut analyzer = LatexCitationAnalyzer::new();
analyzer.visit_root(&tree.root);
analyzer
.citations
tree.citations
.iter()
.find(|citation| citation.key.range().contains(request.params.position))
.map(|citation| citation.key.text())
.map(|citation| citation.key())
.find(|key| key.range().contains(request.params.position))
.map(|key| key.text())
} else {
None
}
Expand Down
20 changes: 7 additions & 13 deletions src/definition/latex_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,22 @@ impl LatexLabelDefinitionProvider {

fn find_definition(document: &Document, reference: &str) -> Option<Location> {
if let SyntaxTree::Latex(tree) = &document.tree {
let mut analyzer = LatexLabelAnalyzer::new();
analyzer.visit_root(&tree.root);
analyzer
.labels
tree.labels
.iter()
.filter(|label| label.kind == LatexLabelKind::Definition)
.find(|label| label.name.text() == reference)
.map(|label| Location::new(document.uri.clone(), label.name.range()))
.filter(|label| label.kind() == LatexLabelKind::Definition)
.find(|label| label.name().text() == reference)
.map(|label| Location::new(document.uri.clone(), label.name().range()))
} else {
None
}
}

fn find_reference(request: &FeatureRequest<TextDocumentPositionParams>) -> Option<&str> {
if let SyntaxTree::Latex(tree) = &request.document.tree {
let mut analyzer = LatexLabelAnalyzer::new();
analyzer.visit_root(&tree.root);
analyzer
.labels
tree.labels
.iter()
.find(|label| label.name.range().contains(request.params.position))
.map(|label| label.name.text())
.find(|label| label.name().range().contains(request.params.position))
.map(|label| label.name().text())
} else {
None
}
Expand Down
5 changes: 1 addition & 4 deletions src/folding/latex_environment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::feature::FeatureRequest;
use crate::syntax::latex::{LatexEnvironmentAnalyzer, LatexVisitor};
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use lsp_types::{FoldingRange, FoldingRangeKind, FoldingRangeParams};
Expand All @@ -10,9 +9,7 @@ impl LatexEnvironmentFoldingProvider {
pub async fn execute(request: &FeatureRequest<FoldingRangeParams>) -> Vec<FoldingRange> {
let mut foldings = Vec::new();
if let SyntaxTree::Latex(tree) = &request.document.tree {
let mut analyzer = LatexEnvironmentAnalyzer::new();
analyzer.visit_root(&tree.root);
for environment in &analyzer.environments {
for environment in &tree.environments {
let start = environment.left.command.end();
let end = environment.right.command.start();
foldings.push(FoldingRange {
Expand Down
23 changes: 11 additions & 12 deletions src/folding/latex_section.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::feature::FeatureRequest;
use crate::syntax::latex::{LatexSectionAnalyzer, LatexVisitor};
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use lsp_types::{FoldingRange, FoldingRangeKind, FoldingRangeParams};
Expand All @@ -10,9 +9,7 @@ impl LatexSectionFoldingProvider {
pub async fn execute(request: &FeatureRequest<FoldingRangeParams>) -> Vec<FoldingRange> {
let mut foldings = Vec::new();
if let SyntaxTree::Latex(tree) = &request.document.tree {
let mut analyzer = LatexSectionAnalyzer::new();
analyzer.visit_root(&tree.root);
let sections = analyzer.sections;
let sections = &tree.sections;
for i in 0..sections.len() {
let current = &sections[i];
let mut next = None;
Expand All @@ -24,14 +21,16 @@ impl LatexSectionFoldingProvider {
}

if let Some(next) = next {
let folding = FoldingRange {
start_line: current.command.end().line,
start_character: Some(current.command.end().character),
end_line: next.command.start().line - 1,
end_character: Some(0),
kind: Some(FoldingRangeKind::Region),
};
foldings.push(folding);
if next.command.start().line > 0 {
let folding = FoldingRange {
start_line: current.command.end().line,
start_character: Some(current.command.end().character),
end_line: next.command.start().line - 1,
end_character: Some(0),
kind: Some(FoldingRangeKind::Region),
};
foldings.push(folding);
}
}
}
}
Expand Down
Loading

0 comments on commit ed2a3bf

Please sign in to comment.