2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-12-07 05:46:39 -05:00
|
|
|
|
|
|
|
use super::analysis::get_lint_references;
|
|
|
|
use super::analysis::references_to_diagnostics;
|
2020-12-24 05:53:03 -05:00
|
|
|
use super::analysis::ResolvedDependency;
|
2021-03-09 21:41:35 -05:00
|
|
|
use super::language_server;
|
2020-12-07 05:46:39 -05:00
|
|
|
use super::tsc;
|
|
|
|
|
|
|
|
use crate::diagnostics;
|
|
|
|
use crate::media_type::MediaType;
|
2021-03-09 21:41:35 -05:00
|
|
|
use crate::tokio_util::create_basic_runtime;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-03-09 21:41:35 -05:00
|
|
|
use deno_core::error::anyhow;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::serde_json;
|
2021-02-11 23:17:48 -05:00
|
|
|
use deno_core::serde_json::json;
|
2021-01-22 05:03:16 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::error;
|
2021-01-29 14:34:33 -05:00
|
|
|
use lspower::lsp;
|
2021-03-09 21:41:35 -05:00
|
|
|
use lspower::Client;
|
2020-12-07 05:46:39 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::mem;
|
2021-03-09 21:41:35 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
use tokio::sync::oneshot;
|
2021-03-18 16:26:41 -04:00
|
|
|
use tokio::time::sleep;
|
|
|
|
use tokio::time::Duration;
|
|
|
|
use tokio::time::Instant;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
|
|
|
|
pub enum DiagnosticSource {
|
2020-12-24 05:53:03 -05:00
|
|
|
Deno,
|
2020-12-07 05:46:39 -05:00
|
|
|
Lint,
|
|
|
|
TypeScript,
|
|
|
|
}
|
|
|
|
|
2021-03-09 21:41:35 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum DiagnosticRequest {
|
|
|
|
Get(
|
|
|
|
ModuleSpecifier,
|
|
|
|
DiagnosticSource,
|
|
|
|
oneshot::Sender<Vec<lsp::Diagnostic>>,
|
|
|
|
),
|
|
|
|
Invalidate(ModuleSpecifier),
|
|
|
|
Update,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a client and a diagnostics collection, publish the appropriate changes
|
|
|
|
/// to the client.
|
|
|
|
async fn publish_diagnostics(
|
|
|
|
client: &Client,
|
|
|
|
collection: &mut DiagnosticCollection,
|
|
|
|
snapshot: &language_server::StateSnapshot,
|
|
|
|
) {
|
|
|
|
let mark = snapshot.performance.mark("publish_diagnostics");
|
|
|
|
let maybe_changes = collection.take_changes();
|
|
|
|
if let Some(diagnostic_changes) = maybe_changes {
|
|
|
|
for specifier in diagnostic_changes {
|
|
|
|
// TODO(@kitsonk) not totally happy with the way we collect and store
|
|
|
|
// different types of diagnostics and offer them up to the client, we
|
|
|
|
// do need to send "empty" vectors though when a particular feature is
|
|
|
|
// disabled, otherwise the client will not clear down previous
|
|
|
|
// diagnostics
|
|
|
|
let mut diagnostics: Vec<lsp::Diagnostic> =
|
|
|
|
if snapshot.config.settings.lint {
|
|
|
|
collection
|
|
|
|
.diagnostics_for(&specifier, &DiagnosticSource::Lint)
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
if snapshot.config.settings.enable {
|
|
|
|
diagnostics.extend(
|
|
|
|
collection
|
|
|
|
.diagnostics_for(&specifier, &DiagnosticSource::TypeScript)
|
|
|
|
.cloned(),
|
|
|
|
);
|
|
|
|
diagnostics.extend(
|
|
|
|
collection
|
|
|
|
.diagnostics_for(&specifier, &DiagnosticSource::Deno)
|
|
|
|
.cloned(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let uri = specifier.clone();
|
|
|
|
let version = snapshot.documents.version(&specifier);
|
|
|
|
client.publish_diagnostics(uri, diagnostics, version).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.performance.measure(mark);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn update_diagnostics(
|
|
|
|
client: &Client,
|
|
|
|
collection: &mut DiagnosticCollection,
|
|
|
|
snapshot: &language_server::StateSnapshot,
|
|
|
|
ts_server: &tsc::TsServer,
|
|
|
|
) {
|
|
|
|
let (enabled, lint_enabled) = {
|
|
|
|
let config = &snapshot.config;
|
|
|
|
(config.settings.enable, config.settings.lint)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mark = snapshot.performance.mark("update_diagnostics");
|
|
|
|
let lint = async {
|
|
|
|
let mut diagnostics = None;
|
|
|
|
if lint_enabled {
|
|
|
|
let mark = snapshot.performance.mark("prepare_diagnostics_lint");
|
|
|
|
diagnostics = Some(
|
|
|
|
generate_lint_diagnostics(snapshot.clone(), collection.clone()).await,
|
|
|
|
);
|
|
|
|
snapshot.performance.measure(mark);
|
|
|
|
};
|
|
|
|
Ok::<_, AnyError>(diagnostics)
|
|
|
|
};
|
|
|
|
|
|
|
|
let ts = async {
|
|
|
|
let mut diagnostics = None;
|
|
|
|
if enabled {
|
|
|
|
let mark = snapshot.performance.mark("prepare_diagnostics_ts");
|
|
|
|
diagnostics = Some(
|
|
|
|
generate_ts_diagnostics(
|
|
|
|
snapshot.clone(),
|
|
|
|
collection.clone(),
|
|
|
|
ts_server,
|
|
|
|
)
|
|
|
|
.await?,
|
|
|
|
);
|
|
|
|
snapshot.performance.measure(mark);
|
|
|
|
};
|
|
|
|
Ok::<_, AnyError>(diagnostics)
|
|
|
|
};
|
|
|
|
|
|
|
|
let deps = async {
|
|
|
|
let mut diagnostics = None;
|
|
|
|
if enabled {
|
|
|
|
let mark = snapshot.performance.mark("prepare_diagnostics_deps");
|
|
|
|
diagnostics = Some(
|
|
|
|
generate_dependency_diagnostics(snapshot.clone(), collection.clone())
|
|
|
|
.await?,
|
|
|
|
);
|
|
|
|
snapshot.performance.measure(mark);
|
|
|
|
};
|
|
|
|
Ok::<_, AnyError>(diagnostics)
|
|
|
|
};
|
|
|
|
|
|
|
|
let (lint_res, ts_res, deps_res) = tokio::join!(lint, ts, deps);
|
|
|
|
let mut disturbed = false;
|
|
|
|
|
|
|
|
match lint_res {
|
|
|
|
Ok(Some(diagnostics)) => {
|
|
|
|
for (specifier, version, diagnostics) in diagnostics {
|
|
|
|
collection.set(specifier, DiagnosticSource::Lint, version, diagnostics);
|
|
|
|
disturbed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!("Internal error: {}", err);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match ts_res {
|
|
|
|
Ok(Some(diagnostics)) => {
|
|
|
|
for (specifier, version, diagnostics) in diagnostics {
|
|
|
|
collection.set(
|
|
|
|
specifier,
|
|
|
|
DiagnosticSource::TypeScript,
|
|
|
|
version,
|
|
|
|
diagnostics,
|
|
|
|
);
|
|
|
|
disturbed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!("Internal error: {}", err);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match deps_res {
|
|
|
|
Ok(Some(diagnostics)) => {
|
|
|
|
for (specifier, version, diagnostics) in diagnostics {
|
|
|
|
collection.set(specifier, DiagnosticSource::Deno, version, diagnostics);
|
|
|
|
disturbed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!("Internal error: {}", err);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
snapshot.performance.measure(mark);
|
|
|
|
|
|
|
|
if disturbed {
|
|
|
|
publish_diagnostics(client, collection, snapshot).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A server which calculates diagnostics in its own thread and publishes them
|
|
|
|
/// to an LSP client.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct DiagnosticsServer(
|
|
|
|
Option<mpsc::UnboundedSender<DiagnosticRequest>>,
|
|
|
|
);
|
|
|
|
|
|
|
|
impl DiagnosticsServer {
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
Self(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn start(
|
|
|
|
&mut self,
|
|
|
|
language_server: Arc<tokio::sync::Mutex<language_server::Inner>>,
|
|
|
|
client: Client,
|
|
|
|
ts_server: Arc<tsc::TsServer>,
|
|
|
|
) {
|
|
|
|
let (tx, mut rx) = mpsc::unbounded_channel::<DiagnosticRequest>();
|
|
|
|
self.0 = Some(tx);
|
|
|
|
|
|
|
|
let _join_handle = thread::spawn(move || {
|
|
|
|
let runtime = create_basic_runtime();
|
|
|
|
let mut collection = DiagnosticCollection::default();
|
|
|
|
|
|
|
|
runtime.block_on(async {
|
2021-03-18 16:26:41 -04:00
|
|
|
// Debounce timer delay. 150ms between keystrokes is about 45 WPM, so we
|
|
|
|
// want something that is longer than that, but not too long to
|
|
|
|
// introduce detectable UI delay; 200ms is a decent compromise.
|
|
|
|
const DELAY: Duration = Duration::from_millis(200);
|
|
|
|
// If the debounce timer isn't active, it will be set to expire "never",
|
|
|
|
// which is actually just 1 year in the future.
|
|
|
|
const NEVER: Duration = Duration::from_secs(365 * 24 * 60 * 60);
|
2021-03-09 21:41:35 -05:00
|
|
|
|
2021-03-18 16:26:41 -04:00
|
|
|
// A flag that is set whenever something has changed that requires the
|
|
|
|
// diagnostics collection to be updated.
|
|
|
|
let mut dirty = false;
|
2021-03-09 21:41:35 -05:00
|
|
|
|
2021-03-18 16:26:41 -04:00
|
|
|
let debounce_timer = sleep(NEVER);
|
|
|
|
tokio::pin!(debounce_timer);
|
2021-03-09 21:41:35 -05:00
|
|
|
|
2021-03-18 16:26:41 -04:00
|
|
|
loop {
|
2021-03-09 21:41:35 -05:00
|
|
|
// "race" the next message off the rx queue or the debounce timer.
|
2021-03-18 16:26:41 -04:00
|
|
|
// The debounce timer gets reset every time a message comes off the
|
|
|
|
// queue. When the debounce timer expires, a snapshot of the most
|
|
|
|
// up-to-date state is used to produce diagnostics.
|
2021-03-09 21:41:35 -05:00
|
|
|
tokio::select! {
|
2021-03-18 16:26:41 -04:00
|
|
|
maybe_request = rx.recv() => {
|
|
|
|
use DiagnosticRequest::*;
|
|
|
|
match maybe_request {
|
|
|
|
None => break, // Request channel closed.
|
|
|
|
Some(Get(specifier, source, tx)) => {
|
|
|
|
let diagnostics = collection
|
|
|
|
.diagnostics_for(&specifier, &source)
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
// If this fails, the requestor disappeared; not a problem.
|
|
|
|
let _ = tx.send(diagnostics);
|
|
|
|
}
|
|
|
|
Some(Invalidate(specifier)) => {
|
|
|
|
collection.invalidate(&specifier);
|
|
|
|
}
|
|
|
|
Some(Update) => {
|
|
|
|
dirty = true;
|
|
|
|
debounce_timer.as_mut().reset(Instant::now() + DELAY);
|
|
|
|
}
|
2021-03-09 21:41:35 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 16:26:41 -04:00
|
|
|
_ = debounce_timer.as_mut(), if dirty => {
|
|
|
|
dirty = false;
|
|
|
|
debounce_timer.as_mut().reset(Instant::now() + NEVER);
|
|
|
|
|
|
|
|
let snapshot = language_server.lock().await.snapshot();
|
|
|
|
update_diagnostics(
|
|
|
|
&client,
|
|
|
|
&mut collection,
|
|
|
|
&snapshot,
|
|
|
|
&ts_server
|
|
|
|
).await;
|
2021-03-09 21:41:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get(
|
|
|
|
&self,
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
source: DiagnosticSource,
|
|
|
|
) -> Result<Vec<lsp::Diagnostic>, AnyError> {
|
|
|
|
let (tx, rx) = oneshot::channel::<Vec<lsp::Diagnostic>>();
|
|
|
|
if let Some(self_tx) = &self.0 {
|
|
|
|
self_tx.send(DiagnosticRequest::Get(specifier, source, tx))?;
|
|
|
|
rx.await.map_err(|err| err.into())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("diagnostic server not started"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn invalidate(&self, specifier: ModuleSpecifier) -> Result<(), AnyError> {
|
|
|
|
if let Some(tx) = &self.0 {
|
|
|
|
tx.send(DiagnosticRequest::Invalidate(specifier))
|
|
|
|
.map_err(|err| err.into())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("diagnostic server not started"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(&self) -> Result<(), AnyError> {
|
|
|
|
if let Some(tx) = &self.0 {
|
|
|
|
tx.send(DiagnosticRequest::Update).map_err(|err| err.into())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("diagnostic server not started"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2021-03-09 21:41:35 -05:00
|
|
|
struct DiagnosticCollection {
|
2021-01-29 14:34:33 -05:00
|
|
|
map: HashMap<(ModuleSpecifier, DiagnosticSource), Vec<lsp::Diagnostic>>,
|
2021-01-22 05:03:16 -05:00
|
|
|
versions: HashMap<ModuleSpecifier, i32>,
|
|
|
|
changes: HashSet<ModuleSpecifier>,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticCollection {
|
|
|
|
pub fn set(
|
|
|
|
&mut self,
|
2021-01-22 05:03:16 -05:00
|
|
|
specifier: ModuleSpecifier,
|
2020-12-07 05:46:39 -05:00
|
|
|
source: DiagnosticSource,
|
|
|
|
version: Option<i32>,
|
2021-01-29 14:34:33 -05:00
|
|
|
diagnostics: Vec<lsp::Diagnostic>,
|
2020-12-07 05:46:39 -05:00
|
|
|
) {
|
2021-01-22 05:03:16 -05:00
|
|
|
self.map.insert((specifier.clone(), source), diagnostics);
|
2020-12-07 05:46:39 -05:00
|
|
|
if let Some(version) = version {
|
2021-01-22 05:03:16 -05:00
|
|
|
self.versions.insert(specifier.clone(), version);
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
self.changes.insert(specifier);
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn diagnostics_for(
|
|
|
|
&self,
|
2021-01-22 05:03:16 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: &DiagnosticSource,
|
2021-01-29 14:34:33 -05:00
|
|
|
) -> impl Iterator<Item = &lsp::Diagnostic> {
|
2021-01-22 05:03:16 -05:00
|
|
|
self
|
|
|
|
.map
|
|
|
|
.get(&(specifier.clone(), source.clone()))
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
pub fn get_version(&self, specifier: &ModuleSpecifier) -> Option<i32> {
|
|
|
|
self.versions.get(specifier).cloned()
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
pub fn invalidate(&mut self, specifier: &ModuleSpecifier) {
|
|
|
|
self.versions.remove(specifier);
|
2020-12-29 23:17:17 -05:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
pub fn take_changes(&mut self) -> Option<HashSet<ModuleSpecifier>> {
|
2020-12-07 05:46:39 -05:00
|
|
|
if self.changes.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(mem::take(&mut self.changes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
pub type DiagnosticVec =
|
2021-01-29 14:34:33 -05:00
|
|
|
Vec<(ModuleSpecifier, Option<i32>, Vec<lsp::Diagnostic>)>;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-03-09 21:41:35 -05:00
|
|
|
async fn generate_lint_diagnostics(
|
|
|
|
state_snapshot: language_server::StateSnapshot,
|
|
|
|
collection: DiagnosticCollection,
|
2020-12-07 05:46:39 -05:00
|
|
|
) -> DiagnosticVec {
|
2020-12-21 08:44:26 -05:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
let mut diagnostic_list = Vec::new();
|
|
|
|
|
2021-01-25 18:47:12 -05:00
|
|
|
for specifier in state_snapshot.documents.open_specifiers() {
|
|
|
|
let version = state_snapshot.documents.version(specifier);
|
2021-03-09 21:41:35 -05:00
|
|
|
let current_version = collection.get_version(specifier);
|
2020-12-21 08:44:26 -05:00
|
|
|
if version != current_version {
|
|
|
|
let media_type = MediaType::from(specifier);
|
2021-01-25 18:47:12 -05:00
|
|
|
if let Ok(Some(source_code)) =
|
|
|
|
state_snapshot.documents.content(specifier)
|
|
|
|
{
|
2020-12-21 08:44:26 -05:00
|
|
|
if let Ok(references) =
|
|
|
|
get_lint_references(specifier, &media_type, &source_code)
|
|
|
|
{
|
|
|
|
if !references.is_empty() {
|
|
|
|
diagnostic_list.push((
|
2021-01-22 05:03:16 -05:00
|
|
|
specifier.clone(),
|
2020-12-21 08:44:26 -05:00
|
|
|
version,
|
|
|
|
references_to_diagnostics(references),
|
|
|
|
));
|
|
|
|
} else {
|
2021-01-22 05:03:16 -05:00
|
|
|
diagnostic_list.push((specifier.clone(), version, Vec::new()));
|
2020-12-21 08:44:26 -05:00
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2020-12-21 08:44:26 -05:00
|
|
|
} else {
|
|
|
|
error!("Missing file contents for: {}", specifier);
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 08:44:26 -05:00
|
|
|
|
|
|
|
diagnostic_list
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:34:33 -05:00
|
|
|
impl<'a> From<&'a diagnostics::DiagnosticCategory> for lsp::DiagnosticSeverity {
|
2020-12-21 08:44:26 -05:00
|
|
|
fn from(category: &'a diagnostics::DiagnosticCategory) -> Self {
|
|
|
|
match category {
|
2021-01-29 14:34:33 -05:00
|
|
|
diagnostics::DiagnosticCategory::Error => lsp::DiagnosticSeverity::Error,
|
2020-12-21 08:44:26 -05:00
|
|
|
diagnostics::DiagnosticCategory::Warning => {
|
2021-01-29 14:34:33 -05:00
|
|
|
lsp::DiagnosticSeverity::Warning
|
2020-12-21 08:44:26 -05:00
|
|
|
}
|
|
|
|
diagnostics::DiagnosticCategory::Suggestion => {
|
2021-01-29 14:34:33 -05:00
|
|
|
lsp::DiagnosticSeverity::Hint
|
2020-12-21 08:44:26 -05:00
|
|
|
}
|
|
|
|
diagnostics::DiagnosticCategory::Message => {
|
2021-01-29 14:34:33 -05:00
|
|
|
lsp::DiagnosticSeverity::Information
|
2020-12-21 08:44:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:34:33 -05:00
|
|
|
impl<'a> From<&'a diagnostics::Position> for lsp::Position {
|
2020-12-21 08:44:26 -05:00
|
|
|
fn from(pos: &'a diagnostics::Position) -> Self {
|
|
|
|
Self {
|
|
|
|
line: pos.line as u32,
|
|
|
|
character: pos.character as u32,
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2020-12-21 08:44:26 -05:00
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2020-12-21 08:44:26 -05:00
|
|
|
fn to_lsp_range(
|
|
|
|
start: &diagnostics::Position,
|
|
|
|
end: &diagnostics::Position,
|
2021-01-29 14:34:33 -05:00
|
|
|
) -> lsp::Range {
|
|
|
|
lsp::Range {
|
2020-12-21 08:44:26 -05:00
|
|
|
start: start.into(),
|
|
|
|
end: end.into(),
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
type TsDiagnostics = HashMap<String, Vec<diagnostics::Diagnostic>>;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
|
|
|
fn get_diagnostic_message(diagnostic: &diagnostics::Diagnostic) -> String {
|
|
|
|
if let Some(message) = diagnostic.message_text.clone() {
|
|
|
|
message
|
|
|
|
} else if let Some(message_chain) = diagnostic.message_chain.clone() {
|
|
|
|
message_chain.format_message(0)
|
|
|
|
} else {
|
|
|
|
"[missing message]".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_lsp_related_information(
|
|
|
|
related_information: &Option<Vec<diagnostics::Diagnostic>>,
|
2021-01-29 14:34:33 -05:00
|
|
|
) -> Option<Vec<lsp::DiagnosticRelatedInformation>> {
|
2021-03-25 14:17:37 -04:00
|
|
|
related_information.as_ref().map(|related| {
|
|
|
|
related
|
|
|
|
.iter()
|
|
|
|
.filter_map(|ri| {
|
|
|
|
if let (Some(source), Some(start), Some(end)) =
|
|
|
|
(&ri.source, &ri.start, &ri.end)
|
|
|
|
{
|
|
|
|
let uri = lsp::Url::parse(&source).unwrap();
|
|
|
|
Some(lsp::DiagnosticRelatedInformation {
|
|
|
|
location: lsp::Location {
|
|
|
|
uri,
|
|
|
|
range: to_lsp_range(start, end),
|
|
|
|
},
|
|
|
|
message: get_diagnostic_message(&ri),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ts_json_to_diagnostics(
|
2021-01-22 05:03:16 -05:00
|
|
|
diagnostics: &[diagnostics::Diagnostic],
|
2021-01-29 14:34:33 -05:00
|
|
|
) -> Vec<lsp::Diagnostic> {
|
2021-01-22 05:03:16 -05:00
|
|
|
diagnostics
|
|
|
|
.iter()
|
|
|
|
.filter_map(|d| {
|
|
|
|
if let (Some(start), Some(end)) = (&d.start, &d.end) {
|
2021-01-29 14:34:33 -05:00
|
|
|
Some(lsp::Diagnostic {
|
2021-01-22 05:03:16 -05:00
|
|
|
range: to_lsp_range(start, end),
|
|
|
|
severity: Some((&d.category).into()),
|
2021-01-29 14:34:33 -05:00
|
|
|
code: Some(lsp::NumberOrString::Number(d.code as i32)),
|
2021-01-22 05:03:16 -05:00
|
|
|
code_description: None,
|
|
|
|
source: Some("deno-ts".to_string()),
|
|
|
|
message: get_diagnostic_message(d),
|
|
|
|
related_information: to_lsp_related_information(
|
|
|
|
&d.related_information,
|
|
|
|
),
|
|
|
|
tags: match d.code {
|
|
|
|
// These are codes that indicate the variable is unused.
|
2021-01-26 05:55:59 -05:00
|
|
|
2695 | 6133 | 6138 | 6192 | 6196 | 6198 | 6199 | 7027 | 7028 => {
|
2021-01-29 14:34:33 -05:00
|
|
|
Some(vec![lsp::DiagnosticTag::Unnecessary])
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-03-09 21:41:35 -05:00
|
|
|
async fn generate_ts_diagnostics(
|
|
|
|
state_snapshot: language_server::StateSnapshot,
|
|
|
|
collection: DiagnosticCollection,
|
2021-01-22 05:03:16 -05:00
|
|
|
ts_server: &tsc::TsServer,
|
2020-12-07 05:46:39 -05:00
|
|
|
) -> Result<DiagnosticVec, AnyError> {
|
|
|
|
let mut diagnostics = Vec::new();
|
2021-01-22 05:03:16 -05:00
|
|
|
let mut specifiers = Vec::new();
|
2021-01-25 18:47:12 -05:00
|
|
|
for specifier in state_snapshot.documents.open_specifiers() {
|
|
|
|
let version = state_snapshot.documents.version(specifier);
|
2021-03-09 21:41:35 -05:00
|
|
|
let current_version = collection.get_version(specifier);
|
2021-01-25 18:47:12 -05:00
|
|
|
if version != current_version {
|
|
|
|
specifiers.push(specifier.clone());
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !specifiers.is_empty() {
|
|
|
|
let req = tsc::RequestMethod::GetDiagnostics(specifiers);
|
|
|
|
let res = ts_server.request(state_snapshot.clone(), req).await?;
|
|
|
|
let ts_diagnostic_map: TsDiagnostics = serde_json::from_value(res)?;
|
|
|
|
for (specifier_str, ts_diagnostics) in ts_diagnostic_map.iter() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = deno_core::resolve_url(specifier_str)?;
|
2021-01-25 18:47:12 -05:00
|
|
|
let version = state_snapshot.documents.version(&specifier);
|
2021-01-22 05:03:16 -05:00
|
|
|
diagnostics.push((
|
|
|
|
specifier,
|
|
|
|
version,
|
|
|
|
ts_json_to_diagnostics(ts_diagnostics),
|
|
|
|
));
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(diagnostics)
|
|
|
|
}
|
2020-12-24 05:53:03 -05:00
|
|
|
|
2021-03-09 21:41:35 -05:00
|
|
|
async fn generate_dependency_diagnostics(
|
|
|
|
mut state_snapshot: language_server::StateSnapshot,
|
|
|
|
collection: DiagnosticCollection,
|
2020-12-24 05:53:03 -05:00
|
|
|
) -> Result<DiagnosticVec, AnyError> {
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
let mut diagnostics = Vec::new();
|
|
|
|
|
2021-01-26 04:55:04 -05:00
|
|
|
let sources = &mut state_snapshot.sources;
|
2021-01-25 18:47:12 -05:00
|
|
|
for specifier in state_snapshot.documents.open_specifiers() {
|
|
|
|
let version = state_snapshot.documents.version(specifier);
|
2021-03-09 21:41:35 -05:00
|
|
|
let current_version = collection.get_version(specifier);
|
2020-12-24 05:53:03 -05:00
|
|
|
if version != current_version {
|
|
|
|
let mut diagnostic_list = Vec::new();
|
2021-01-25 18:47:12 -05:00
|
|
|
if let Some(dependencies) = state_snapshot.documents.dependencies(specifier) {
|
2020-12-24 05:53:03 -05:00
|
|
|
for (_, dependency) in dependencies.iter() {
|
|
|
|
if let (Some(code), Some(range)) = (
|
|
|
|
&dependency.maybe_code,
|
|
|
|
&dependency.maybe_code_specifier_range,
|
|
|
|
) {
|
|
|
|
match code.clone() {
|
2021-02-11 23:17:48 -05:00
|
|
|
ResolvedDependency::Err(dependency_err) => {
|
2021-01-29 14:34:33 -05:00
|
|
|
diagnostic_list.push(lsp::Diagnostic {
|
2020-12-24 05:53:03 -05:00
|
|
|
range: *range,
|
2021-01-29 14:34:33 -05:00
|
|
|
severity: Some(lsp::DiagnosticSeverity::Error),
|
2021-02-11 23:17:48 -05:00
|
|
|
code: Some(dependency_err.as_code()),
|
2020-12-24 05:53:03 -05:00
|
|
|
code_description: None,
|
|
|
|
source: Some("deno".to_string()),
|
2021-02-11 23:17:48 -05:00
|
|
|
message: format!("{}", dependency_err),
|
2020-12-24 05:53:03 -05:00
|
|
|
related_information: None,
|
|
|
|
tags: None,
|
|
|
|
data: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ResolvedDependency::Resolved(specifier) => {
|
2021-02-12 06:49:42 -05:00
|
|
|
if !(state_snapshot.documents.contains_key(&specifier) || sources.contains_key(&specifier)) {
|
2021-02-17 23:37:05 -05:00
|
|
|
let scheme = specifier.scheme();
|
|
|
|
let (code, message) = if scheme == "file" {
|
2021-02-11 23:17:48 -05:00
|
|
|
(Some(lsp::NumberOrString::String("no-local".to_string())), format!("Unable to load a local module: \"{}\".\n Please check the file path.", specifier))
|
2021-02-17 23:37:05 -05:00
|
|
|
} else if scheme == "data" {
|
|
|
|
(Some(lsp::NumberOrString::String("no-cache-data".to_string())), "Uncached data URL.".to_string())
|
2021-02-11 23:17:48 -05:00
|
|
|
} else {
|
|
|
|
(Some(lsp::NumberOrString::String("no-cache".to_string())), format!("Unable to load the remote module: \"{}\".", specifier))
|
|
|
|
};
|
2021-01-29 14:34:33 -05:00
|
|
|
diagnostic_list.push(lsp::Diagnostic {
|
2020-12-24 05:53:03 -05:00
|
|
|
range: *range,
|
2021-01-29 14:34:33 -05:00
|
|
|
severity: Some(lsp::DiagnosticSeverity::Error),
|
2021-02-11 23:17:48 -05:00
|
|
|
code,
|
2020-12-24 05:53:03 -05:00
|
|
|
code_description: None,
|
|
|
|
source: Some("deno".to_string()),
|
2021-02-11 23:17:48 -05:00
|
|
|
message,
|
2020-12-24 05:53:03 -05:00
|
|
|
related_information: None,
|
|
|
|
tags: None,
|
2021-02-11 23:17:48 -05:00
|
|
|
data: Some(json!({
|
|
|
|
"specifier": specifier
|
|
|
|
})),
|
2020-12-24 05:53:03 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 05:03:16 -05:00
|
|
|
diagnostics.push((specifier.clone(), version, diagnostic_list))
|
2020-12-24 05:53:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(diagnostics)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
}
|