Move completion database into a separate crate

This commit is contained in:
Patrick Förster 2019-07-30 16:03:57 +02:00
parent 6ad870eccb
commit e201b39d85
14 changed files with 135 additions and 84 deletions

View file

@ -0,0 +1,19 @@
[package]
name = "texlab-completion-data"
version = "0.1.0"
authors = [
"Eric Förster <efoerster@users.noreply.github.com>",
"Patrick Förster <pfoerster@users.noreply.github.com>"]
edition = "2018"
[dependencies]
itertools = "0.8.0"
lsp-types = { git = "https://github.com/latex-lsp/lsp-types", rev = "9fcc5d9b9d3013ce84e20ef566267754d594b268", features = ["proposed"] }
once_cell = "0.2.2"
serde = { version = "1.0.97", features = ["derive", "rc"] }
serde_json = "1.0.40"
texlab-syntax = { path = "../texlab_syntax" }
texlab-workspace = { path = "../texlab_workspace" }
[build-dependencies]
reqwest = { version = "0.9", default-features = false, features = ["rustls-tls"] }

View file

@ -0,0 +1,9 @@
use std::fs;
fn main() {
let text = reqwest::get("https://github.com/latex-lsp/latex-completion-data/releases/download/v19.07.1/completion.json")
.expect("Failed to download completion database")
.text()
.unwrap();
fs::write("src/completion.json", text).expect("Failed to save completion database");
}

View file

@ -0,0 +1,119 @@
use itertools::Itertools;
use lsp_types::{MarkupContent, MarkupKind};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use texlab_syntax::*;
use texlab_workspace::Document;
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Database {
pub components: Vec<Component>,
pub metadata: Vec<Metadata>,
}
impl Database {
pub fn find(&self, name: &str) -> Option<&Component> {
self.components.iter().find(|component| {
component
.file_names
.iter()
.any(|file_name| file_name == name)
})
}
pub fn kernel(&self) -> &Component {
self.components
.iter()
.find(|component| component.file_names.is_empty())
.unwrap()
}
pub fn related_components(&self, documents: &[Arc<Document>]) -> Vec<&Component> {
let mut start_components = vec![self.kernel()];
for document in documents {
if let SyntaxTree::Latex(tree) = &document.tree {
tree.components
.iter()
.flat_map(|file| self.find(file))
.for_each(|component| start_components.push(component))
}
}
let mut all_components = Vec::new();
for component in start_components {
all_components.push(component);
component
.references
.iter()
.flat_map(|file| self.find(&file))
.for_each(|component| all_components.push(component))
}
all_components
.into_iter()
.unique_by(|component| &component.file_names)
.collect()
}
pub fn exists(&self, file_name: &str) -> bool {
return self
.components
.iter()
.any(|component| component.file_names.iter().any(|f| f == file_name));
}
pub fn documentation(&self, name: &str) -> Option<MarkupContent> {
let metadata = self
.metadata
.iter()
.find(|metadata| metadata.name == name)?;
let desc = metadata.description.to_owned()?;
Some(MarkupContent {
kind: MarkupKind::PlainText,
value: desc.into(),
})
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Component {
pub file_names: Vec<String>,
pub references: Vec<String>,
pub commands: Vec<Command>,
pub environments: Vec<String>,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Command {
pub name: String,
pub image: Option<String>,
pub parameters: Vec<Parameter>,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Parameter(pub Vec<Argument>);
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Argument {
pub name: String,
pub image: Option<String>,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
pub name: String,
pub caption: Option<String>,
pub description: Option<String>,
}
const JSON: &str = include_str!("completion.json");
pub static DATABASE: Lazy<Database> = Lazy::new(|| serde_json::from_str(JSON).unwrap());