Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve BibTeX completion performance #917

Merged
merged 7 commits into from
Aug 11, 2023
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Improve performance when completing BibTeX entries ([#493](https://github.com/latex-lsp/texlab/issues/493))
- Don't report unused entries for very large bibliographies
- Avoid redundant reparses after saving documents

## [5.9.0] - 2023-08-06

### Added
Expand Down
44 changes: 42 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/base-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rustc-hash = "1.1.0"
syntax = { path = "../syntax" }
text-size = "1.1.1"
url = "=2.3.1"
bibtex-utils = { path = "../bibtex-utils" }

[lib]
doctest = false
23 changes: 22 additions & 1 deletion crates/base-db/src/semantics/bib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use bibtex_utils::field::text::TextFieldData;
use itertools::Itertools;
use rowan::ast::AstNode;
use syntax::bibtex::{self, HasName};
use syntax::bibtex::{self, HasName, HasType, HasValue};
use text_size::TextRange;

use crate::data::{BibtexEntryType, BibtexEntryTypeCategory};

use super::Span;

#[derive(Debug, Clone, Default)]
Expand All @@ -23,12 +27,27 @@ impl Semantics {

fn process_entry(&mut self, entry: bibtex::Entry) {
if let Some(name) = entry.name_token() {
let type_token = entry.type_token().unwrap();
let category = BibtexEntryType::find(&type_token.text()[1..])
.map_or(BibtexEntryTypeCategory::Misc, |ty| ty.category);

let field_values = entry
.fields()
.filter_map(|field| Some(TextFieldData::parse(&field.value()?)?.text));

let keywords = [name.text().into(), type_token.text().into()]
.into_iter()
.chain(field_values)
.join(" ");

self.entries.push(Entry {
name: Span {
range: name.text_range(),
text: name.text().into(),
},
full_range: entry.syntax().text_range(),
category,
keywords,
});
}
}
Expand All @@ -50,6 +69,8 @@ impl Semantics {
pub struct Entry {
pub name: Span,
pub full_range: TextRange,
pub keywords: String,
pub category: BibtexEntryTypeCategory,
}

#[derive(Debug, Clone)]
Expand Down
6 changes: 6 additions & 0 deletions crates/base-db/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ impl Workspace {
Cow::Owned(text) => text,
};

if let Some(document) = self.lookup_path(path) {
if document.text == text {
return Ok(());
}
}

self.open(uri, text, language, owner, LineCol { line: 0, col: 0 });
Ok(())
}
Expand Down
23 changes: 23 additions & 0 deletions crates/bibtex-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "bibtex-utils"
version = "0.0.0"
license.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true

[dependencies]
chrono = { version = "0.4.26", default-features = false, features = ["std"] }
human_name = "2.0.2"
itertools = "0.11.0"
rowan = "0.15.11"
rustc-hash = "1.1.0"
syntax = { path = "../syntax" }
unicode-normalization = "0.1.22"

[lib]
doctest = false

[dev-dependencies]
expect-test = "1.4.1"
parser = { path = "../parser" }
File renamed without changes.
1 change: 1 addition & 0 deletions crates/bibtex-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod field;
3 changes: 1 addition & 2 deletions crates/citeproc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ edition.workspace = true
rust-version.workspace = true

[dependencies]
chrono = { version = "0.4.26", default-features = false, features = ["std"] }
human_name = "2.0.2"
bibtex-utils = { path = "../bibtex-utils" }
isocountry = "0.3.2"
itertools = "0.11.0"
rowan = "0.15.11"
Expand Down
12 changes: 6 additions & 6 deletions crates/citeproc/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use bibtex_utils::field::{
author::AuthorField,
date::DateField,
number::{NumberField, NumberFieldData},
text::TextField,
};
use isocountry::CountryCode;
use itertools::Itertools;
use syntax::bibtex;
Expand All @@ -6,12 +12,6 @@ use url::Url;

use super::{
entry::{EntryData, EntryKind},
field::{
author::AuthorField,
date::DateField,
number::{NumberField, NumberFieldData},
text::TextField,
},
output::{Inline, InlineBuilder, Punct},
};

Expand Down
7 changes: 3 additions & 4 deletions crates/citeproc/src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use rustc_hash::FxHashMap;
use syntax::bibtex::{Entry, Field, HasName, HasType, HasValue, Value};

use super::field::{
use bibtex_utils::field::{
author::{AuthorField, AuthorFieldData},
date::{DateField, DateFieldData},
number::{NumberField, NumberFieldData},
text::{TextField, TextFieldData},
};
use rustc_hash::FxHashMap;
use syntax::bibtex::{Entry, Field, HasName, HasType, HasValue, Value};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum EntryKind {
Expand Down
1 change: 0 additions & 1 deletion crates/citeproc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod driver;
mod entry;
pub mod field;
mod output;

use syntax::bibtex;
Expand Down
2 changes: 1 addition & 1 deletion crates/completion-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const JSON_GZ: &[u8] = include_bytes!("../data/completion.json.gz");

pub static DATABASE: Lazy<Database<'static>> = Lazy::new(|| {
let mut decoder = GzDecoder::new(JSON_GZ);
let json = Box::leak(Box::new(String::new()));
let json = Box::leak(Box::default());
decoder.read_to_string(json).unwrap();
let mut db: Database = serde_json::from_str(json).unwrap();
db.lookup_packages = db
Expand Down
2 changes: 1 addition & 1 deletion crates/definition/src/citation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::DefinitionContext;

use super::DefinitionResult;

pub(super) fn goto_definition<'db>(context: &mut DefinitionContext<'db>) -> Option<()> {
pub(super) fn goto_definition(context: &mut DefinitionContext) -> Option<()> {
let data = context.params.document.data.as_tex()?;

let citation = queries::object_at_cursor(
Expand Down
2 changes: 1 addition & 1 deletion crates/definition/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::DefinitionContext;

use super::DefinitionResult;

pub(super) fn goto_definition<'db>(context: &mut DefinitionContext<'db>) -> Option<()> {
pub(super) fn goto_definition(context: &mut DefinitionContext) -> Option<()> {
let data = context.params.document.data.as_tex()?;
let root = data.root_node();
let name = root
Expand Down
2 changes: 1 addition & 1 deletion crates/definition/src/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::DefinitionContext;

use super::DefinitionResult;

pub(super) fn goto_definition<'db>(context: &mut DefinitionContext<'db>) -> Option<()> {
pub(super) fn goto_definition(context: &mut DefinitionContext) -> Option<()> {
let start = context.params.document;
let parents = context.params.workspace.parents(start);
let results = parents
Expand Down
2 changes: 1 addition & 1 deletion crates/definition/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::DefinitionContext;

use super::DefinitionResult;

pub(super) fn goto_definition<'db>(context: &mut DefinitionContext<'db>) -> Option<()> {
pub(super) fn goto_definition(context: &mut DefinitionContext) -> Option<()> {
let data = context.params.document.data.as_tex()?;
let reference = queries::object_at_cursor(
&data.semantics.labels,
Expand Down
2 changes: 1 addition & 1 deletion crates/definition/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct DefinitionContext<'db> {
results: Vec<DefinitionResult<'db>>,
}

pub fn goto_definition<'db>(params: DefinitionParams<'db>) -> Vec<DefinitionResult<'db>> {
pub fn goto_definition(params: DefinitionParams) -> Vec<DefinitionResult> {
let project = params.workspace.project(params.document);
let mut context = DefinitionContext {
params,
Expand Down
2 changes: 1 addition & 1 deletion crates/definition/src/string_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::DefinitionContext;

use super::DefinitionResult;

pub(super) fn goto_definition<'db>(context: &mut DefinitionContext<'db>) -> Option<()> {
pub(super) fn goto_definition(context: &mut DefinitionContext) -> Option<()> {
let data = context.params.document.data.as_bib()?;
let root = data.root_node();
let name = root
Expand Down
26 changes: 17 additions & 9 deletions crates/diagnostics/src/build_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@ impl DiagnosticSource for BuildErrors {
fn update(&mut self, workspace: &Workspace, log_document: &Document) {
let mut errors: FxHashMap<Url, Vec<Diagnostic>> = FxHashMap::default();

let Some(data) = log_document.data.as_log() else { return };
let Some(data) = log_document.data.as_log() else {
return;
};

let parents = workspace.parents(log_document);
let Some(root_document) = parents.iter().next() else { return };
let Some(root_document) = parents.iter().next() else {
return;
};

let Some(base_path) = root_document
.path
.as_deref()
.and_then(|path| path.parent()) else { return };
let Some(base_path) = root_document.path.as_deref().and_then(|path| path.parent()) else {
return;
};

for error in &data.errors {
let full_path = base_path.join(&error.relative_path);
let Ok(full_path_uri) = Url::from_file_path(&full_path) else { continue };
let Ok(full_path_uri) = Url::from_file_path(&full_path) else {
continue;
};
let tex_document = workspace.lookup(&full_path_uri).unwrap_or(root_document);

let range = find_range_of_hint(tex_document, error).unwrap_or_else(|| {
Expand Down Expand Up @@ -74,9 +79,12 @@ impl DiagnosticSource for BuildErrors {
self.logs.retain(|uri, _| workspace.lookup(uri).is_some());

for document in workspace.iter() {
let Some(log) = self.logs.get(&document.uri) else { continue };
let Some(log) = self.logs.get(&document.uri) else {
continue;
};

for (uri, errors) in &log.errors {
builder.push_many(&uri, errors.iter().map(Cow::Borrowed));
builder.push_many(uri, errors.iter().map(Cow::Borrowed));
}
}
}
Expand Down
Loading