feat: add LSP stderr output to log

add LSP stderr output to log and sort imports
This commit is contained in:
Jakub Panek 2022-07-16 22:05:06 +00:00 committed by GitHub
parent 47adca6991
commit f12ab9840d
1 changed files with 28 additions and 9 deletions

View File

@ -1,9 +1,10 @@
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
use std::{ use std::{
collections::HashMap, collections::HashMap,
io::BufRead, io::{BufRead, BufReader, BufWriter, Write},
io::{BufReader, BufWriter, Write},
path::Path, path::Path,
process::{self, Child, ChildStdout, Command, Stdio}, process::{self, Child, ChildStderr, ChildStdout, Command, Stdio},
sync::{ sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
mpsc::channel, mpsc::channel,
@ -13,9 +14,6 @@
time::Duration, time::Duration,
}; };
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use jsonrpc_lite::{Id, JsonRpc, Params}; use jsonrpc_lite::{Id, JsonRpc, Params};
use lapce_rpc::{ use lapce_rpc::{
@ -23,12 +21,12 @@
style::{LineStyle, SemanticStyles, Style}, style::{LineStyle, SemanticStyles, Style},
RequestId, RequestId,
}; };
use log::error;
use lsp_types::*; use lsp_types::*;
use parking_lot::Mutex; use parking_lot::Mutex;
use serde_json::{json, to_value, Value}; use serde_json::{json, to_value, Value};
use crate::buffer::Buffer; use crate::{buffer::Buffer, dispatch::Dispatcher};
use crate::dispatch::Dispatcher;
pub type Callback = Box<dyn Callable>; pub type Callback = Box<dyn Callable>;
const HEADER_CONTENT_LENGTH: &str = "content-length"; const HEADER_CONTENT_LENGTH: &str = "content-length";
@ -504,7 +502,7 @@ fn default() -> Self {
impl LspClient { impl LspClient {
pub fn new( pub fn new(
_language_id: String, language_id: String,
exec_path: &str, exec_path: &str,
options: Option<Value>, options: Option<Value>,
args: Vec<String>, args: Vec<String>,
@ -514,6 +512,7 @@ pub fn new(
let mut process = Self::process(exec_path, args.clone()); let mut process = Self::process(exec_path, args.clone());
let writer = Box::new(BufWriter::new(process.stdin.take().unwrap())); let writer = Box::new(BufWriter::new(process.stdin.take().unwrap()));
let stdout = process.stdout.take().unwrap(); let stdout = process.stdout.take().unwrap();
let stderr = process.stderr.take().unwrap();
let lsp_client = Arc::new(LspClient { let lsp_client = Arc::new(LspClient {
dispatcher, dispatcher,
@ -534,6 +533,7 @@ pub fn new(
}); });
lsp_client.handle_stdout(stdout); lsp_client.handle_stdout(stdout);
lsp_client.handle_stderr(stderr, language_id);
lsp_client.initialize(); lsp_client.initialize();
lsp_client lsp_client
@ -561,6 +561,24 @@ fn handle_stdout(&self, stdout: ChildStdout) {
}); });
} }
fn handle_stderr(&self, stderr: ChildStderr, language_id: String) {
thread::spawn(move || {
let mut reader = Box::new(BufReader::new(stderr));
loop {
let mut buffer = String::new();
loop {
buffer.clear();
let _result = reader.read_line(&mut buffer);
if buffer.trim().is_empty() {
continue;
}
error!("[LSP::{}] {}", language_id, buffer.trim())
}
}
});
}
fn process(exec_path: &str, args: Vec<String>) -> Child { fn process(exec_path: &str, args: Vec<String>) -> Child {
let mut process = Command::new(exec_path); let mut process = Command::new(exec_path);
@ -571,6 +589,7 @@ fn process(exec_path: &str, args: Vec<String>) -> Child {
process process
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn() .spawn()
.expect("Error Occurred") .expect("Error Occurred")
} }