diff --git a/src/diagnostics/mod.rs b/src/diagnostics/mod.rs index 487a17a..fdc5590 100644 --- a/src/diagnostics/mod.rs +++ b/src/diagnostics/mod.rs @@ -30,17 +30,17 @@ pub use modelsim::*; /// - uri: 当前正在诊断的文件的 uri /// - rope: 当前正在诊断的文件在后端增量更新的文本内容 /// - files: 所有 hdl 文件,方便后续进行联合诊断使用 -/// - configuration: 前端关于 lsp 相关的配置 /// - server: 服务实例 pub fn provide_diagnostics( uri: Url, rope: &Rope, - configuration: &LspConfiguration, server: &LspServer ) -> PublishDiagnosticsParams { let mut diagnostics = Vec::::new(); let language_id = get_language_id_by_uri(&uri); + let configuration = server.configuration.read().unwrap(); + // 选择对应语言的 lsp let linter_configuration = match language_id.as_str() { "vhdl" => Some(&configuration.vhdl_linter_configuration), diff --git a/src/main.rs b/src/main.rs index 6de28de..6160bf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,8 @@ use request::{ fast::SyncFastApi, config::UpdateConfigurationApi, primitives::DoPrimitivesJudgeApi, - linter::LinterStatusApi + linter::LinterStatusApi, + linter::DoLintApi }; use log::info; @@ -59,6 +60,7 @@ async fn main() { .custom_method("api/update-configuration", UpdateConfigurationApi) .custom_method("api/sync-fast", SyncFastApi) .custom_method("api/linter-status", LinterStatusApi) + .custom_method("api/do-lint", DoLintApi) .finish(); Server::new(stdin, stdout, socket) diff --git a/src/request/linter.rs b/src/request/linter.rs index fd8d887..7fb84b0 100644 --- a/src/request/linter.rs +++ b/src/request/linter.rs @@ -4,9 +4,11 @@ use std::future; use log::info; use serde::{Deserialize, Serialize}; use tower_lsp::jsonrpc::Result; +use tower_lsp::lsp_types::{Diagnostic, Url}; -use crate::diagnostics::{AbstractLinterConfiguration, LinterStatus}; +use crate::diagnostics::{provide_diagnostics, AbstractLinterConfiguration, LinterStatus}; use crate::server::Backend; +use crate::utils::from_uri_to_escape_path_string; #[derive(Clone)] pub struct LinterStatusApi; @@ -91,4 +93,42 @@ fn get_linter_status( }); } } -} \ No newline at end of file +} + + + + +#[derive(Clone)] +pub struct DoLintApi; + +#[derive(Deserialize, Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct DoLintParams { + path: String, +} + + +impl <'a>tower_lsp::jsonrpc::Method<&'a Arc, (DoLintParams, ), Result>> for DoLintApi { + type Future = future::Ready>>; + + fn invoke(&self, _server: &'a Arc, _params: (DoLintParams, )) -> Self::Future { + let path = _params.0.path; + let uri = Url::from_file_path(&path).unwrap(); + let path_string = from_uri_to_escape_path_string(&uri).unwrap(); + let server = &_server.server; + + let result = if let Some(source) = server.srcs.get_source(&path_string) { + let source = source.read().unwrap(); + let diags_param = provide_diagnostics(uri, &source.text, server); + Ok(diags_param.diagnostics) + } else { + Err(tower_lsp::jsonrpc::Error { + code: tower_lsp::jsonrpc::ErrorCode::InvalidRequest, + message: Cow::Owned(format!("文件尚未初始化 {}", path)), + data: None + }) + }; + + future::ready(result) + } +} diff --git a/src/sources.rs b/src/sources.rs index b301e57..3a319c0 100644 --- a/src/sources.rs +++ b/src/sources.rs @@ -62,7 +62,6 @@ impl LspServer { let diagnostics = provide_diagnostics( uri, &source.text, - &self.configuration.read().unwrap(), &self ); diagnostics @@ -113,7 +112,6 @@ impl LspServer { provide_diagnostics( uri, &source.text, - &self.configuration.read().unwrap(), &self ) } else {