Skip to content

Commit

Permalink
Pass configuration to LaTeX parser
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Feb 26, 2020
1 parent c65d74a commit 67ed52d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
3 changes: 2 additions & 1 deletion crates/texlab_syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use self::lsp_kind::*;
pub use self::text::*;

use texlab_distro::{Language, Resolver};
use texlab_protocol::Uri;
use texlab_protocol::{Options, Uri};

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum SyntaxTree {
Expand All @@ -21,6 +21,7 @@ pub enum SyntaxTree {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct SyntaxTreeInput<'a> {
pub options: &'a Options,
pub resolver: &'a Resolver,
pub uri: &'a Uri,
pub text: &'a str,
Expand Down
9 changes: 8 additions & 1 deletion crates/texlab_workspace/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ pub struct Document {
}

impl Document {
pub fn parse(uri: Uri, text: String, language: Language, resolver: &Resolver) -> Self {
pub fn parse(
uri: Uri,
text: String,
language: Language,
options: &Options,
resolver: &Resolver,
) -> Self {
let input = SyntaxTreeInput {
options,
resolver,
uri: &uri,
text: &text,
Expand Down
3 changes: 2 additions & 1 deletion crates/texlab_workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ impl TestWorkspaceBuilder {

pub fn add_document(&mut self, name: &str, text: &str) -> Uri {
let resolver = Resolver::default();
let options = Options::default();
let path = env::temp_dir().join(name);
let language = Language::by_extension(path.extension().unwrap().to_str().unwrap()).unwrap();
let uri = Uri::from_file_path(path).unwrap();
let document = Document::parse(uri.clone(), text.to_owned(), language, &resolver);
let document = Document::parse(uri.clone(), text.to_owned(), language, &options, &resolver);
self.workspace.documents.push(Arc::new(document));
uri
}
Expand Down
22 changes: 15 additions & 7 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
#[jsonrpc_method("textDocument/didOpen", kind = "notification")]
pub async fn did_open(&self, params: DidOpenTextDocumentParams) {
let uri = params.text_document.uri.clone();
self.workspace_manager.add(params.text_document);
let options = self.configuration(false).await;
self.workspace_manager.add(params.text_document, &options);
self.action_manager
.push(Action::DetectRoot(uri.clone().into()));
self.action_manager
Expand All @@ -181,9 +182,11 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {

#[jsonrpc_method("textDocument/didChange", kind = "notification")]
pub async fn did_change(&self, params: DidChangeTextDocumentParams) {
let options = self.configuration(false).await;
for change in params.content_changes {
let uri = params.text_document.uri.clone();
self.workspace_manager.update(uri.into(), change.text);
self.workspace_manager
.update(uri.into(), change.text, &options);
}
self.action_manager.push(Action::RunLinter(
params.text_document.uri.into(),
Expand Down Expand Up @@ -496,7 +499,7 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
let workspace = self.workspace_manager.get();
for path in workspace.unresolved_includes(&options) {
if path.exists() {
changed |= self.workspace_manager.load(&path).is_ok();
changed |= self.workspace_manager.load(&path, &options).is_ok();
}
}

Expand All @@ -506,15 +509,19 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
}
}

fn update_document(&self, document: &Document) -> std::result::Result<(), WorkspaceLoadError> {
fn update_document(
&self,
document: &Document,
options: &Options,
) -> std::result::Result<(), WorkspaceLoadError> {
if document.uri.scheme() != "file" {
return Ok(());
}

let path = document.uri.to_file_path().unwrap();
let data = fs::metadata(&path).map_err(WorkspaceLoadError::IO)?;
if data.modified().map_err(WorkspaceLoadError::IO)? > document.modified {
self.workspace_manager.load(&path)
self.workspace_manager.load(&path, &options)
} else {
Ok(())
}
Expand Down Expand Up @@ -573,7 +580,7 @@ impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
{
if let Ok(parent_uri) = Uri::from_file_path(entry.path()) {
if workspace.find(&parent_uri).is_none() {
let _ = self.workspace_manager.load(entry.path());
let _ = self.workspace_manager.load(entry.path(), &options);
}
}
}
Expand All @@ -587,9 +594,10 @@ impl<C: LspClient + Send + Sync + 'static> Middleware for LatexLspServer<C> {
async fn before_message(&self) {
self.detect_children().await;

let options = self.configuration(false).await;
let workspace = self.workspace_manager.get();
for document in &workspace.documents {
let _ = self.update_document(document);
let _ = self.update_document(document, &options);
}
}

Expand Down
23 changes: 15 additions & 8 deletions src/workspace_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;
use texlab_distro::{Distribution, Language};
use texlab_protocol::{TextDocumentItem, Uri};
use texlab_protocol::{Options, TextDocumentItem, Uri};
use texlab_syntax::SyntaxTree;
use texlab_workspace::{Document, Workspace};

Expand Down Expand Up @@ -35,7 +35,7 @@ impl WorkspaceManager {
Arc::clone(&workspace)
}

pub fn add(&self, document: TextDocumentItem) {
pub fn add(&self, document: TextDocumentItem, options: &Options) {
let language = match Language::by_language_id(&document.language_id) {
Some(language) => language,
None => {
Expand All @@ -45,10 +45,16 @@ impl WorkspaceManager {
};

let mut workspace = self.workspace.lock().unwrap();
*workspace = self.add_or_update(&workspace, document.uri.into(), document.text, language);
*workspace = self.add_or_update(
&workspace,
document.uri.into(),
document.text,
language,
options,
);
}

pub fn load(&self, path: &Path) -> Result<(), WorkspaceLoadError> {
pub fn load(&self, path: &Path, options: &Options) -> Result<(), WorkspaceLoadError> {
let language = match path
.extension()
.and_then(OsStr::to_str)
Expand Down Expand Up @@ -78,11 +84,11 @@ impl WorkspaceManager {
};

let mut workspace = self.workspace.lock().unwrap();
*workspace = self.add_or_update(&workspace, uri, text, language);
*workspace = self.add_or_update(&workspace, uri, text, language, options);
Ok(())
}

pub fn update(&self, uri: Uri, text: String) {
pub fn update(&self, uri: Uri, text: String, options: &Options) {
let mut workspace = self.workspace.lock().unwrap();

let old_document = match workspace.documents.iter().find(|x| x.uri == uri) {
Expand All @@ -98,7 +104,7 @@ impl WorkspaceManager {
SyntaxTree::Bibtex(_) => Language::Bibtex,
};

*workspace = self.add_or_update(&workspace, uri, text, language);
*workspace = self.add_or_update(&workspace, uri, text, language, options);
}

fn add_or_update(
Expand All @@ -107,9 +113,10 @@ impl WorkspaceManager {
uri: Uri,
text: String,
language: Language,
options: &Options,
) -> Arc<Workspace> {
let resolver = block_on(self.distribution.resolver());
let document = Document::parse(uri, text, language, &resolver);
let document = Document::parse(uri, text, language, &options, &resolver);
let mut documents: Vec<Arc<Document>> = workspace
.documents
.iter()
Expand Down

0 comments on commit 67ed52d

Please sign in to comment.