-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add completion database to FeatureRequest
- Loading branch information
Showing
9 changed files
with
218 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use crate::completion::latex::data::types::{LatexComponent, LatexComponentDatabase}; | ||
use crate::workspace::{Document, SyntaxTree}; | ||
use futures::channel::{mpsc, oneshot}; | ||
use futures::compat::*; | ||
use futures::executor::ThreadPool; | ||
use futures::lock::Mutex; | ||
use futures::prelude::*; | ||
use futures::task::*; | ||
use std::io::{Read, Write}; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::Arc; | ||
|
||
enum Message { | ||
Get(oneshot::Sender<Arc<LatexComponentDatabase>>), | ||
Add(LatexComponent), | ||
} | ||
|
||
pub struct LatexComponentDatabaseActor { | ||
sender: Mutex<mpsc::Sender<Message>>, | ||
receiver: Mutex<mpsc::Receiver<Message>>, | ||
} | ||
|
||
impl LatexComponentDatabaseActor { | ||
pub fn new() -> Self { | ||
let (sender, receiver) = mpsc::channel(0); | ||
LatexComponentDatabaseActor { | ||
sender: Mutex::new(sender), | ||
receiver: Mutex::new(receiver), | ||
} | ||
} | ||
|
||
pub async fn spawn(mut pool: ThreadPool, path: PathBuf) -> Arc<Self> { | ||
let actor = Arc::new(Self::new()); | ||
let task = |actor: Arc<LatexComponentDatabaseActor>| { | ||
async move { | ||
let mut database = Arc::new(Self::load_database(&path).unwrap_or_default()); | ||
let mut receiver = await!(actor.receiver.lock()); | ||
while let Some(message) = await!(receiver.next()) { | ||
match message { | ||
Message::Get(sender) => { | ||
let database = Arc::clone(&database); | ||
sender.send(database).unwrap(); | ||
} | ||
Message::Add(component) => { | ||
let mut components = Vec::new(); | ||
for component in &database.components { | ||
components.push(Arc::clone(&component)); | ||
} | ||
components.push(Arc::new(component)); | ||
database = Arc::new(LatexComponentDatabase::new(components)); | ||
Self::save_database(&path, &database); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
pool.spawn(task(Arc::clone(&actor))) | ||
.expect("Failed to intitialize completion database"); | ||
actor | ||
} | ||
|
||
fn load_database(path: &Path) -> Option<LatexComponentDatabase> { | ||
let mut file = std::fs::File::open(path).ok()?; | ||
let mut text = String::new(); | ||
file.read_to_string(&mut text).ok()?; | ||
serde_json::from_str(&text).ok() | ||
} | ||
|
||
fn save_database(path: &Path, database: &LatexComponentDatabase) { | ||
let mut file = std::fs::File::create(path).expect("Failed to create completion database"); | ||
let text = serde_json::to_string_pretty(database) | ||
.expect("Failed to serialize completion database"); | ||
file.write_all(&mut text.into_bytes()) | ||
.expect("Failed to save completion database"); | ||
} | ||
|
||
pub async fn get(&self) -> Arc<LatexComponentDatabase> { | ||
let (sender, receiver) = oneshot::channel(); | ||
let message = Message::Get(sender); | ||
await!(self.send(message)); | ||
await!(receiver).unwrap() | ||
} | ||
|
||
pub async fn add(&self, component: LatexComponent) { | ||
let message = Message::Add(component); | ||
await!(self.send(message)); | ||
} | ||
|
||
async fn send(&self, message: Message) { | ||
let mut sender = await!(self.sender.lock()); | ||
await!(sender.send(message)).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod actor; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::syntax::latex::analysis::include::LatexIncludeAnalyzer; | ||
use crate::syntax::latex::ast::LatexVisitor; | ||
use crate::workspace::{Document, SyntaxTree}; | ||
use itertools::Itertools; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct LatexComponent { | ||
pub files: Vec<String>, | ||
pub references: Vec<String>, | ||
pub commands: Vec<String>, | ||
pub environments: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone, Default, Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct LatexComponentDatabase { | ||
pub components: Vec<Arc<LatexComponent>>, | ||
} | ||
|
||
impl LatexComponentDatabase { | ||
pub fn new(components: Vec<Arc<LatexComponent>>) -> Self { | ||
LatexComponentDatabase { components } | ||
} | ||
|
||
pub fn related_components(&self, documents: &[&Document]) -> Vec<Arc<LatexComponent>> { | ||
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 | ||
.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(Arc::clone(&component)); | ||
component | ||
.references | ||
.iter() | ||
.flat_map(|file| self.find(&file)) | ||
.for_each(|component| all_components.push(component)) | ||
} | ||
|
||
all_components | ||
.iter() | ||
.unique_by(|component| &component.files) | ||
.map(Arc::clone) | ||
.collect() | ||
} | ||
|
||
fn find(&self, name: &String) -> Option<Arc<LatexComponent>> { | ||
self.components | ||
.iter() | ||
.find(|component| component.files.contains(name)) | ||
.map(Arc::clone) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters