Skip to content

Commit

Permalink
Capture stderr when building
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Jun 2, 2019
1 parent 05352f2 commit a5e92ae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
44 changes: 26 additions & 18 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::client::LspClient;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::executor::block_on;
use futures_boxed::boxed;
use lsp_types::*;
use serde::{Deserialize, Serialize};
Expand All @@ -10,6 +11,7 @@ use std::io::BufRead;
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::thread;

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -61,7 +63,7 @@ pub struct BuildProvider<C> {

impl<C> BuildProvider<C>
where
C: LspClient + Send + Sync,
C: LspClient + Send + Sync + 'static,
{
pub fn new(client: Arc<C>, options: BuildOptions) -> Self {
Self { client, options }
Expand All @@ -76,32 +78,38 @@ where
let mut process = Command::new(options.executable)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.stderr(Stdio::piped())
.current_dir(path.parent().unwrap())
.spawn()?;

let stdout = process.stdout.as_mut().unwrap();
let mut reader = io::BufReader::new(stdout);
loop {
let mut line = String::new();
let count = reader.read_line(&mut line)?;
if count == 0 {
break;
}
let params = LogMessageParams {
typ: MessageType::Log,
message: Cow::from(line.trim().to_owned()),
};
self.client.log_message(params).await;
}

Self::read(Arc::clone(&self.client), process.stdout.take().unwrap());
Self::read(Arc::clone(&self.client), process.stderr.take().unwrap());
Ok(process.wait()?.success())
}

fn read<R>(client: Arc<C>, output: R)
where
R: io::Read + Send + 'static,
{
thread::spawn(move || {
let client = Arc::clone(&client);
let reader = io::BufReader::new(output);
reader.lines().for_each(|line| {
if let Ok(line) = line {
let params = LogMessageParams {
typ: MessageType::Log,
message: Cow::from(line),
};
block_on(client.log_message(params));
}
});
});
}
}

impl<C> FeatureProvider for BuildProvider<C>
where
C: LspClient + Send + Sync,
C: LspClient + Send + Sync + 'static,
{
type Params = BuildParams;
type Output = BuildResult;
Expand Down
4 changes: 2 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct LatexLspServer<C> {
}

#[jsonrpc_server]
impl<C: LspClient + Send + Sync> LatexLspServer<C> {
impl<C: LspClient + Send + Sync + 'static> LatexLspServer<C> {
pub fn new(client: Arc<C>) -> Self {
LatexLspServer {
client,
Expand Down Expand Up @@ -353,7 +353,7 @@ impl<C: LspClient + Send + Sync> LatexLspServer<C> {
}
}

impl<C: LspClient + Send + Sync> jsonrpc::ActionHandler for LatexLspServer<C> {
impl<C: LspClient + Send + Sync + 'static> jsonrpc::ActionHandler for LatexLspServer<C> {
#[boxed]
async fn execute_actions(&self) {
for action in self.action_manager.take() {
Expand Down
14 changes: 10 additions & 4 deletions tests/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#![feature(async_await)]

use futures::executor::block_on;
use jsonrpc::server::ActionHandler;
use lsp_types::*;
use texlab::build::*;
use texlab::scenario::Scenario;
use jsonrpc::server::ActionHandler;

async fn run(executable: &'static str, on_save: bool, name: &'static str) -> (Scenario, BuildResult) {
async fn run(
executable: &'static str,
on_save: bool,
name: &'static str,
) -> (Scenario, BuildResult) {
let scenario = Scenario::new("build").await;
scenario.open(name).await;
let mut build_options = BuildOptions::default();
Expand All @@ -18,7 +22,7 @@ async fn run(executable: &'static str, on_save: bool, name: &'static str) -> (Sc
options.latex_build = Some(build_options);
}
let text_document = TextDocumentIdentifier::new(scenario.uri(name));
let params = BuildParams { text_document};
let params = BuildParams { text_document };
let result = scenario.server.build(params).await.unwrap();
scenario.server.execute_actions().await;
(scenario, result)
Expand Down Expand Up @@ -63,7 +67,9 @@ fn test_on_save() {
options.latex_build = Some(build_options);
}
let text_document = TextDocumentIdentifier::new(scenario.uri("foo.tex"));
scenario.server.did_save(DidSaveTextDocumentParams { text_document });
scenario
.server
.did_save(DidSaveTextDocumentParams { text_document });
scenario.server.execute_actions().await;
let path = scenario.directory.path().join("foo.pdf");
assert!(path.exists());
Expand Down

0 comments on commit a5e92ae

Please sign in to comment.