mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 21:50:00 -05:00
refactor(lsp): unify config file data into ConfigTree (#23032)
This commit is contained in:
parent
d6452b3946
commit
3b61104e2a
10 changed files with 1090 additions and 967 deletions
|
@ -260,6 +260,12 @@ pub struct FmtOptions {
|
|||
pub files: FilePatterns,
|
||||
}
|
||||
|
||||
impl Default for FmtOptions {
|
||||
fn default() -> Self {
|
||||
Self::new_with_base(PathBuf::from("/"))
|
||||
}
|
||||
}
|
||||
|
||||
impl FmtOptions {
|
||||
pub fn new_with_base(base: PathBuf) -> Self {
|
||||
Self {
|
||||
|
@ -394,6 +400,12 @@ pub struct LintOptions {
|
|||
pub fix: bool,
|
||||
}
|
||||
|
||||
impl Default for LintOptions {
|
||||
fn default() -> Self {
|
||||
Self::new_with_base(PathBuf::from("/"))
|
||||
}
|
||||
}
|
||||
|
||||
impl LintOptions {
|
||||
pub fn new_with_base(base: PathBuf) -> Self {
|
||||
Self {
|
||||
|
|
|
@ -43,6 +43,7 @@ use std::cmp::Ordering;
|
|||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use tower_lsp::lsp_types as lsp;
|
||||
use tower_lsp::lsp_types::Position;
|
||||
use tower_lsp::lsp_types::Range;
|
||||
|
@ -216,7 +217,7 @@ fn code_as_string(code: &Option<lsp::NumberOrString>) -> String {
|
|||
/// Rewrites imports in quick fixes and code changes to be Deno specific.
|
||||
pub struct TsResponseImportMapper<'a> {
|
||||
documents: &'a Documents,
|
||||
maybe_import_map: Option<&'a ImportMap>,
|
||||
maybe_import_map: Option<Arc<ImportMap>>,
|
||||
node_resolver: Option<&'a CliNodeResolver>,
|
||||
npm_resolver: Option<&'a dyn CliNpmResolver>,
|
||||
}
|
||||
|
@ -224,7 +225,7 @@ pub struct TsResponseImportMapper<'a> {
|
|||
impl<'a> TsResponseImportMapper<'a> {
|
||||
pub fn new(
|
||||
documents: &'a Documents,
|
||||
maybe_import_map: Option<&'a ImportMap>,
|
||||
maybe_import_map: Option<Arc<ImportMap>>,
|
||||
node_resolver: Option<&'a CliNodeResolver>,
|
||||
npm_resolver: Option<&'a dyn CliNpmResolver>,
|
||||
) -> Self {
|
||||
|
@ -269,7 +270,7 @@ impl<'a> TsResponseImportMapper<'a> {
|
|||
let sub_path = (export != ".").then_some(export);
|
||||
let mut req = None;
|
||||
req = req.or_else(|| {
|
||||
let import_map = self.maybe_import_map?;
|
||||
let import_map = self.maybe_import_map.as_ref()?;
|
||||
for entry in import_map.entries_for_referrer(referrer) {
|
||||
let Some(value) = entry.raw_value else {
|
||||
continue;
|
||||
|
@ -296,7 +297,7 @@ impl<'a> TsResponseImportMapper<'a> {
|
|||
JsrPackageNvReference::new(nv_ref).to_string()
|
||||
};
|
||||
let specifier = ModuleSpecifier::parse(&spec_str).ok()?;
|
||||
if let Some(import_map) = self.maybe_import_map {
|
||||
if let Some(import_map) = &self.maybe_import_map {
|
||||
if let Some(result) = import_map.lookup(&specifier, referrer) {
|
||||
return Some(result);
|
||||
}
|
||||
|
@ -315,7 +316,7 @@ impl<'a> TsResponseImportMapper<'a> {
|
|||
// check if any pkg reqs match what is found in an import map
|
||||
if !pkg_reqs.is_empty() {
|
||||
let sub_path = self.resolve_package_path(specifier);
|
||||
if let Some(import_map) = self.maybe_import_map {
|
||||
if let Some(import_map) = &self.maybe_import_map {
|
||||
let pkg_reqs = pkg_reqs.iter().collect::<HashSet<_>>();
|
||||
let mut matches = Vec::new();
|
||||
for entry in import_map.entries_for_referrer(referrer) {
|
||||
|
@ -357,7 +358,7 @@ impl<'a> TsResponseImportMapper<'a> {
|
|||
}
|
||||
|
||||
// check if the import map has this specifier
|
||||
if let Some(import_map) = self.maybe_import_map {
|
||||
if let Some(import_map) = &self.maybe_import_map {
|
||||
if let Some(result) = import_map.lookup(specifier, referrer) {
|
||||
return Some(result);
|
||||
}
|
||||
|
@ -942,7 +943,7 @@ impl CodeActionCollection {
|
|||
let action = fix_ts_import_action(
|
||||
specifier,
|
||||
action,
|
||||
&language_server.get_ts_response_import_mapper(),
|
||||
&language_server.get_ts_response_import_mapper(specifier),
|
||||
)?;
|
||||
let edit = ts_changes_to_edit(&action.changes, language_server)?;
|
||||
let code_action = lsp::CodeAction {
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -21,7 +21,6 @@ use crate::graph_util::enhanced_resolution_error_message;
|
|||
use crate::lsp::lsp_custom::DiagnosticBatchNotificationParams;
|
||||
use crate::resolver::SloppyImportsResolution;
|
||||
use crate::resolver::SloppyImportsResolver;
|
||||
use crate::tools::lint::get_configured_rules;
|
||||
|
||||
use deno_ast::MediaType;
|
||||
use deno_core::anyhow::anyhow;
|
||||
|
@ -46,6 +45,7 @@ use deno_runtime::tokio_util::create_basic_runtime;
|
|||
use deno_semver::jsr::JsrPackageReqReference;
|
||||
use deno_semver::npm::NpmPackageReqReference;
|
||||
use deno_semver::package::PackageReq;
|
||||
use import_map::ImportMap;
|
||||
use log::error;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
@ -62,10 +62,10 @@ use tower_lsp::lsp_types as lsp;
|
|||
pub struct DiagnosticServerUpdateMessage {
|
||||
pub snapshot: Arc<StateSnapshot>,
|
||||
pub config: Arc<ConfigSnapshot>,
|
||||
pub lint_options: LintOptions,
|
||||
pub url_map: LspUrlMap,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct DiagnosticRecord {
|
||||
pub specifier: ModuleSpecifier,
|
||||
pub versioned: VersionedDiagnostics,
|
||||
|
@ -461,7 +461,6 @@ impl DiagnosticsServer {
|
|||
DiagnosticServerUpdateMessage {
|
||||
snapshot,
|
||||
config,
|
||||
lint_options,
|
||||
url_map,
|
||||
},
|
||||
batch_index,
|
||||
|
@ -612,14 +611,7 @@ impl DiagnosticsServer {
|
|||
let mark = performance.mark("lsp.update_diagnostics_lint");
|
||||
let diagnostics = spawn_blocking({
|
||||
let token = token.clone();
|
||||
move || {
|
||||
generate_lint_diagnostics(
|
||||
&snapshot,
|
||||
&config,
|
||||
&lint_options,
|
||||
token,
|
||||
)
|
||||
}
|
||||
move || generate_lint_diagnostics(&snapshot, &config, token)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -791,17 +783,12 @@ fn ts_json_to_diagnostics(
|
|||
fn generate_lint_diagnostics(
|
||||
snapshot: &language_server::StateSnapshot,
|
||||
config: &ConfigSnapshot,
|
||||
lint_options: &LintOptions,
|
||||
token: CancellationToken,
|
||||
) -> DiagnosticVec {
|
||||
let documents = snapshot
|
||||
.documents
|
||||
.documents(DocumentsFilter::OpenDiagnosable);
|
||||
let lint_rules = get_configured_rules(
|
||||
lint_options.rules.clone(),
|
||||
config.config_file.as_ref(),
|
||||
)
|
||||
.rules;
|
||||
let config_data_by_scope = config.tree.data_by_scope();
|
||||
let mut diagnostics_vec = Vec::new();
|
||||
for document in documents {
|
||||
let settings =
|
||||
|
@ -820,14 +807,20 @@ fn generate_lint_diagnostics(
|
|||
}
|
||||
}
|
||||
let version = document.maybe_lsp_version();
|
||||
let (lint_options, lint_rules) = config
|
||||
.tree
|
||||
.scope_for_specifier(document.specifier())
|
||||
.and_then(|s| config_data_by_scope.get(&s))
|
||||
.map(|d| (d.lint_options.clone(), d.lint_rules.clone()))
|
||||
.unwrap_or_default();
|
||||
diagnostics_vec.push(DiagnosticRecord {
|
||||
specifier: document.specifier().clone(),
|
||||
versioned: VersionedDiagnostics {
|
||||
version,
|
||||
diagnostics: generate_document_lint_diagnostics(
|
||||
config,
|
||||
lint_options,
|
||||
lint_rules.clone(),
|
||||
&lint_options,
|
||||
lint_rules.rules.clone(),
|
||||
&document,
|
||||
),
|
||||
},
|
||||
|
@ -1304,6 +1297,7 @@ fn diagnose_resolution(
|
|||
resolution: &Resolution,
|
||||
is_dynamic: bool,
|
||||
maybe_assert_type: Option<&str>,
|
||||
import_map: Option<&ImportMap>,
|
||||
) -> Vec<DenoDiagnostic> {
|
||||
fn check_redirect_diagnostic(
|
||||
specifier: &ModuleSpecifier,
|
||||
|
@ -1392,7 +1386,7 @@ fn diagnose_resolution(
|
|||
.push(DenoDiagnostic::InvalidNodeSpecifier(specifier.clone()));
|
||||
} else if module_name == dependency_key {
|
||||
let mut is_mapped = false;
|
||||
if let Some(import_map) = &snapshot.maybe_import_map {
|
||||
if let Some(import_map) = import_map {
|
||||
if let Resolution::Ok(resolved) = &resolution {
|
||||
if import_map.resolve(module_name, &resolved.specifier).is_ok() {
|
||||
is_mapped = true;
|
||||
|
@ -1455,7 +1449,8 @@ fn diagnose_dependency(
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(import_map) = &snapshot.maybe_import_map {
|
||||
let import_map = snapshot.config.tree.import_map_for_specifier(referrer);
|
||||
if let Some(import_map) = &import_map {
|
||||
if let Resolution::Ok(resolved) = &dependency.maybe_code {
|
||||
if let Some(to) = import_map.lookup(&resolved.specifier, referrer) {
|
||||
if dependency_key != to {
|
||||
|
@ -1504,6 +1499,7 @@ fn diagnose_dependency(
|
|||
},
|
||||
dependency.is_dynamic,
|
||||
dependency.maybe_attribute_type.as_deref(),
|
||||
import_map.as_deref(),
|
||||
)
|
||||
.iter()
|
||||
.flat_map(|diag| {
|
||||
|
@ -1526,6 +1522,7 @@ fn diagnose_dependency(
|
|||
&dependency.maybe_type,
|
||||
dependency.is_dynamic,
|
||||
dependency.maybe_attribute_type.as_deref(),
|
||||
import_map.as_deref(),
|
||||
)
|
||||
.iter()
|
||||
.map(|diag| diag.to_lsp_diagnostic(&range)),
|
||||
|
@ -1580,20 +1577,21 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::cache::GlobalHttpCache;
|
||||
use crate::cache::RealDenoCacheEnv;
|
||||
use crate::lsp::config::Config;
|
||||
use crate::lsp::config::ConfigSnapshot;
|
||||
use crate::lsp::config::Settings;
|
||||
use crate::lsp::config::WorkspaceSettings;
|
||||
use crate::lsp::documents::Documents;
|
||||
use crate::lsp::documents::LanguageId;
|
||||
use crate::lsp::language_server::StateSnapshot;
|
||||
use deno_config::glob::FilePatterns;
|
||||
use deno_config::ConfigFile;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use test_util::TempDir;
|
||||
|
||||
fn mock_state_snapshot(
|
||||
async fn mock_state_snapshot(
|
||||
fixtures: &[(&str, &str, i32, LanguageId)],
|
||||
location: &Path,
|
||||
maybe_import_map: Option<(&str, &str)>,
|
||||
|
@ -1613,22 +1611,19 @@ mod tests {
|
|||
(*source).into(),
|
||||
);
|
||||
}
|
||||
let maybe_import_map = maybe_import_map.map(|(base, json_string)| {
|
||||
let base_url = ModuleSpecifier::parse(base).unwrap();
|
||||
let result = import_map::parse_from_json(&base_url, json_string).unwrap();
|
||||
if !result.diagnostics.is_empty() {
|
||||
panic!("unexpected import map diagnostics");
|
||||
}
|
||||
Arc::new(result.import_map)
|
||||
});
|
||||
let config = Config::new_with_roots([resolve_url("file:///").unwrap()]);
|
||||
if let Some((base_url, json_string)) = maybe_import_map {
|
||||
let base_url = resolve_url(base_url).unwrap();
|
||||
let config_file = ConfigFile::new(json_string, base_url).unwrap();
|
||||
config.tree.inject_config_file(config_file).await;
|
||||
}
|
||||
StateSnapshot {
|
||||
documents,
|
||||
maybe_import_map,
|
||||
assets: Default::default(),
|
||||
cache_metadata: cache::CacheMetadata::new(Arc::new(
|
||||
GlobalHttpCache::new(location.to_path_buf(), RealDenoCacheEnv),
|
||||
)),
|
||||
config: Default::default(),
|
||||
config: config.snapshot(),
|
||||
npm: None,
|
||||
}
|
||||
}
|
||||
|
@ -1655,14 +1650,14 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
fn setup(
|
||||
async fn setup(
|
||||
temp_dir: &TempDir,
|
||||
sources: &[(&str, &str, i32, LanguageId)],
|
||||
maybe_import_map: Option<(&str, &str)>,
|
||||
) -> (StateSnapshot, PathBuf) {
|
||||
let location = temp_dir.path().join("deps").to_path_buf();
|
||||
let state_snapshot =
|
||||
mock_state_snapshot(sources, &location, maybe_import_map);
|
||||
mock_state_snapshot(sources, &location, maybe_import_map).await;
|
||||
(state_snapshot, location)
|
||||
}
|
||||
|
||||
|
@ -1681,18 +1676,14 @@ let c: number = "a";
|
|||
LanguageId::TypeScript,
|
||||
)],
|
||||
None,
|
||||
);
|
||||
)
|
||||
.await;
|
||||
let snapshot = Arc::new(snapshot);
|
||||
let cache =
|
||||
Arc::new(GlobalHttpCache::new(cache_location, RealDenoCacheEnv));
|
||||
let ts_server = TsServer::new(Default::default(), cache);
|
||||
let ts_server =
|
||||
TsServer::new(Default::default(), cache, Default::default());
|
||||
ts_server.start(None);
|
||||
let lint_options = LintOptions {
|
||||
rules: Default::default(),
|
||||
files: FilePatterns::new_with_base(temp_dir.path().to_path_buf()),
|
||||
reporter_kind: Default::default(),
|
||||
fix: false,
|
||||
};
|
||||
|
||||
// test enabled
|
||||
{
|
||||
|
@ -1700,7 +1691,6 @@ let c: number = "a";
|
|||
let diagnostics = generate_lint_diagnostics(
|
||||
&snapshot,
|
||||
&enabled_config,
|
||||
&lint_options,
|
||||
Default::default(),
|
||||
);
|
||||
assert_eq!(get_diagnostics_for_single(diagnostics).len(), 6);
|
||||
|
@ -1712,7 +1702,7 @@ let c: number = "a";
|
|||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(get_diagnostics_for_single(diagnostics).len(), 5);
|
||||
assert_eq!(get_diagnostics_for_single(diagnostics).len(), 4);
|
||||
let diagnostics = generate_deno_diagnostics(
|
||||
&snapshot,
|
||||
&enabled_config,
|
||||
|
@ -1732,7 +1722,6 @@ let c: number = "a";
|
|||
let diagnostics = generate_lint_diagnostics(
|
||||
&snapshot,
|
||||
&disabled_config,
|
||||
&lint_options,
|
||||
Default::default(),
|
||||
);
|
||||
assert_eq!(get_diagnostics_for_single(diagnostics).len(), 0);
|
||||
|
@ -1793,7 +1782,8 @@ let c: number = "a";
|
|||
}
|
||||
}"#,
|
||||
)),
|
||||
);
|
||||
)
|
||||
.await;
|
||||
let config = mock_config();
|
||||
let token = CancellationToken::new();
|
||||
let actual = generate_deno_diagnostics(&snapshot, &config, token);
|
||||
|
@ -1919,7 +1909,8 @@ let c: number = "a";
|
|||
LanguageId::TypeScript,
|
||||
)],
|
||||
None,
|
||||
);
|
||||
)
|
||||
.await;
|
||||
let config = mock_config();
|
||||
let token = CancellationToken::new();
|
||||
let actual = generate_deno_diagnostics(&snapshot, &config, token);
|
||||
|
|
|
@ -37,7 +37,6 @@ use deno_lockfile::Lockfile;
|
|||
use deno_runtime::deno_node;
|
||||
use deno_runtime::deno_node::NodeResolution;
|
||||
use deno_runtime::deno_node::NodeResolutionMode;
|
||||
use deno_runtime::deno_node::PackageJson;
|
||||
use deno_runtime::permissions::PermissionsContainer;
|
||||
use deno_semver::jsr::JsrPackageReqReference;
|
||||
use deno_semver::npm::NpmPackageReqReference;
|
||||
|
@ -817,15 +816,6 @@ impl FileSystemDocuments {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct UpdateDocumentConfigOptions<'a> {
|
||||
pub config: &'a Config,
|
||||
pub maybe_import_map: Option<Arc<import_map::ImportMap>>,
|
||||
pub maybe_package_json: Option<&'a PackageJson>,
|
||||
pub node_resolver: Option<Arc<CliNodeResolver>>,
|
||||
pub npm_resolver: Option<Arc<dyn CliNpmResolver>>,
|
||||
pub workspace_files: &'a BTreeSet<ModuleSpecifier>,
|
||||
}
|
||||
|
||||
/// Specify the documents to include on a `documents.documents(...)` call.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum DocumentsFilter {
|
||||
|
@ -1309,26 +1299,34 @@ impl Documents {
|
|||
Arc::new(JsrCacheResolver::new(self.cache.clone(), lockfile));
|
||||
}
|
||||
|
||||
pub fn update_config(&mut self, options: UpdateDocumentConfigOptions) {
|
||||
let maybe_config_file = options.config.maybe_config_file();
|
||||
let maybe_package_json_deps =
|
||||
options.maybe_package_json.map(|package_json| {
|
||||
package_json::get_local_package_json_version_reqs(package_json)
|
||||
});
|
||||
let maybe_jsx_config = maybe_config_file
|
||||
.and_then(|cf| cf.to_maybe_jsx_import_source_config().ok().flatten());
|
||||
let deps_provider =
|
||||
Arc::new(PackageJsonDepsProvider::new(maybe_package_json_deps));
|
||||
pub fn update_config(
|
||||
&mut self,
|
||||
config: &Config,
|
||||
node_resolver: Option<Arc<CliNodeResolver>>,
|
||||
npm_resolver: Option<Arc<dyn CliNpmResolver>>,
|
||||
workspace_files: &BTreeSet<ModuleSpecifier>,
|
||||
) {
|
||||
let config_data = config.tree.root_data();
|
||||
let config_file =
|
||||
config_data.as_ref().and_then(|d| d.config_file.as_deref());
|
||||
self.resolver = Arc::new(CliGraphResolver::new(CliGraphResolverOptions {
|
||||
node_resolver: options.node_resolver,
|
||||
npm_resolver: options.npm_resolver,
|
||||
package_json_deps_provider: deps_provider,
|
||||
maybe_jsx_import_source_config: maybe_jsx_config,
|
||||
maybe_import_map: options.maybe_import_map,
|
||||
maybe_vendor_dir: maybe_config_file
|
||||
.and_then(|c| c.vendor_dir_path())
|
||||
.as_ref(),
|
||||
bare_node_builtins_enabled: maybe_config_file
|
||||
node_resolver,
|
||||
npm_resolver,
|
||||
package_json_deps_provider: Arc::new(PackageJsonDepsProvider::new(
|
||||
config_data
|
||||
.as_ref()
|
||||
.and_then(|d| d.package_json.as_ref())
|
||||
.map(|package_json| {
|
||||
package_json::get_local_package_json_version_reqs(package_json)
|
||||
}),
|
||||
)),
|
||||
maybe_jsx_import_source_config: config_file
|
||||
.and_then(|cf| cf.to_maybe_jsx_import_source_config().ok().flatten()),
|
||||
maybe_import_map: config_data.as_ref().and_then(|d| d.import_map.clone()),
|
||||
maybe_vendor_dir: config_data
|
||||
.as_ref()
|
||||
.and_then(|d| d.vendor_dir.as_ref()),
|
||||
bare_node_builtins_enabled: config_file
|
||||
.map(|config| config.has_unstable("bare-node-builtins"))
|
||||
.unwrap_or(false),
|
||||
// Don't set this for the LSP because instead we'll use the OpenDocumentsLoader
|
||||
|
@ -1338,16 +1336,14 @@ impl Documents {
|
|||
}));
|
||||
self.jsr_resolver = Arc::new(JsrCacheResolver::new(
|
||||
self.cache.clone(),
|
||||
options.config.maybe_lockfile().cloned(),
|
||||
config.tree.root_lockfile(),
|
||||
));
|
||||
self.redirect_resolver =
|
||||
Arc::new(RedirectResolver::new(self.cache.clone()));
|
||||
let resolver = self.resolver.as_graph_resolver();
|
||||
let npm_resolver = self.resolver.as_graph_npm_resolver();
|
||||
self.imports = Arc::new(
|
||||
if let Some(Ok(imports)) =
|
||||
maybe_config_file.map(|cf| cf.to_maybe_imports())
|
||||
{
|
||||
if let Some(Ok(imports)) = config_file.map(|cf| cf.to_maybe_imports()) {
|
||||
imports
|
||||
.into_iter()
|
||||
.map(|(referrer, imports)| {
|
||||
|
@ -1364,7 +1360,7 @@ impl Documents {
|
|||
IndexMap::new()
|
||||
},
|
||||
);
|
||||
self.unstable_sloppy_imports = maybe_config_file
|
||||
self.unstable_sloppy_imports = config_file
|
||||
.map(|c| c.has_unstable("sloppy-imports"))
|
||||
.unwrap_or(false);
|
||||
{
|
||||
|
@ -1376,7 +1372,7 @@ impl Documents {
|
|||
// anymore after updating resolvers.
|
||||
return false;
|
||||
};
|
||||
if !options.config.specifier_enabled(specifier) {
|
||||
if !config.specifier_enabled(specifier) {
|
||||
return false;
|
||||
}
|
||||
path.is_file()
|
||||
|
@ -1384,7 +1380,7 @@ impl Documents {
|
|||
let mut open_docs = std::mem::take(&mut self.open_docs);
|
||||
for docs in [&mut open_docs, &mut fs_docs.docs] {
|
||||
for doc in docs.values_mut() {
|
||||
if !options.config.specifier_enabled(doc.specifier()) {
|
||||
if !config.specifier_enabled(doc.specifier()) {
|
||||
continue;
|
||||
}
|
||||
if let Some(new_doc) =
|
||||
|
@ -1395,8 +1391,8 @@ impl Documents {
|
|||
}
|
||||
}
|
||||
self.open_docs = open_docs;
|
||||
for specifier in options.workspace_files {
|
||||
if !options.config.specifier_enabled(specifier) {
|
||||
for specifier in workspace_files {
|
||||
if !config.specifier_enabled(specifier) {
|
||||
continue;
|
||||
}
|
||||
if !self.open_docs.contains_key(specifier)
|
||||
|
@ -1738,8 +1734,9 @@ mod tests {
|
|||
use crate::cache::RealDenoCacheEnv;
|
||||
|
||||
use super::*;
|
||||
use deno_config::ConfigFile;
|
||||
use deno_core::serde_json;
|
||||
use import_map::ImportMap;
|
||||
use deno_core::serde_json::json;
|
||||
use pretty_assertions::assert_eq;
|
||||
use test_util::PathRef;
|
||||
use test_util::TempDir;
|
||||
|
@ -1842,8 +1839,8 @@ console.log(b, "hello deno");
|
|||
assert_eq!(documents.documents(DocumentsFilter::All).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_documents_refresh_dependencies_config_change() {
|
||||
#[tokio::test]
|
||||
async fn test_documents_refresh_dependencies_config_change() {
|
||||
// it should never happen that a user of this API causes this to happen,
|
||||
// but we'll guard against it anyway
|
||||
let temp_dir = TempDir::new();
|
||||
|
@ -1878,23 +1875,23 @@ console.log(b, "hello deno");
|
|||
|
||||
// set the initial import map and point to file 2
|
||||
{
|
||||
let mut import_map = ImportMap::new(
|
||||
ModuleSpecifier::from_file_path(documents_path.join("import_map.json"))
|
||||
config
|
||||
.tree
|
||||
.inject_config_file(
|
||||
ConfigFile::new(
|
||||
&json!({
|
||||
"imports": {
|
||||
"test": "./file2.ts",
|
||||
},
|
||||
})
|
||||
.to_string(),
|
||||
config.root_uri().unwrap().join("deno.json").unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
import_map
|
||||
.imports_mut()
|
||||
.append("test".to_string(), "./file2.ts".to_string())
|
||||
.unwrap();
|
||||
)
|
||||
.await;
|
||||
|
||||
documents.update_config(UpdateDocumentConfigOptions {
|
||||
config: &config,
|
||||
maybe_import_map: Some(Arc::new(import_map)),
|
||||
maybe_package_json: None,
|
||||
node_resolver: None,
|
||||
npm_resolver: None,
|
||||
workspace_files: &workspace_files,
|
||||
});
|
||||
documents.update_config(&config, None, None, &workspace_files);
|
||||
|
||||
// open the document
|
||||
let document = documents.open(
|
||||
|
@ -1918,23 +1915,23 @@ console.log(b, "hello deno");
|
|||
|
||||
// now point at file 3
|
||||
{
|
||||
let mut import_map = ImportMap::new(
|
||||
ModuleSpecifier::from_file_path(documents_path.join("import_map.json"))
|
||||
config
|
||||
.tree
|
||||
.inject_config_file(
|
||||
ConfigFile::new(
|
||||
&json!({
|
||||
"imports": {
|
||||
"test": "./file3.ts",
|
||||
},
|
||||
})
|
||||
.to_string(),
|
||||
config.root_uri().unwrap().join("deno.json").unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
import_map
|
||||
.imports_mut()
|
||||
.append("test".to_string(), "./file3.ts".to_string())
|
||||
.unwrap();
|
||||
)
|
||||
.await;
|
||||
|
||||
documents.update_config(UpdateDocumentConfigOptions {
|
||||
config: &config,
|
||||
maybe_import_map: Some(Arc::new(import_map)),
|
||||
maybe_package_json: None,
|
||||
node_resolver: None,
|
||||
npm_resolver: None,
|
||||
workspace_files: &workspace_files,
|
||||
});
|
||||
documents.update_config(&config, None, None, &workspace_files);
|
||||
|
||||
// check the document's dependencies
|
||||
let document = documents.get(&file1_specifier).unwrap();
|
||||
|
|
File diff suppressed because it is too large
Load diff
121
cli/lsp/tsc.rs
121
cli/lsp/tsc.rs
|
@ -3,6 +3,7 @@
|
|||
use super::analysis::CodeActionData;
|
||||
use super::code_lens;
|
||||
use super::config;
|
||||
use super::config::ConfigTree;
|
||||
use super::documents::AssetOrDocument;
|
||||
use super::documents::DocumentsFilter;
|
||||
use super::language_server;
|
||||
|
@ -22,7 +23,6 @@ use super::urls::INVALID_SPECIFIER;
|
|||
|
||||
use crate::args::jsr_url;
|
||||
use crate::args::FmtOptionsConfig;
|
||||
use crate::args::TsConfig;
|
||||
use crate::cache::HttpCache;
|
||||
use crate::lsp::cache::CacheMetadata;
|
||||
use crate::lsp::documents::Documents;
|
||||
|
@ -221,6 +221,7 @@ pub struct TsServer {
|
|||
sender: mpsc::UnboundedSender<Request>,
|
||||
receiver: Mutex<Option<mpsc::UnboundedReceiver<Request>>>,
|
||||
specifier_map: Arc<TscSpecifierMap>,
|
||||
config_tree: Arc<ConfigTree>,
|
||||
inspector_server: Mutex<Option<Arc<InspectorServer>>>,
|
||||
}
|
||||
|
||||
|
@ -238,7 +239,11 @@ impl std::fmt::Debug for TsServer {
|
|||
}
|
||||
|
||||
impl TsServer {
|
||||
pub fn new(performance: Arc<Performance>, cache: Arc<dyn HttpCache>) -> Self {
|
||||
pub fn new(
|
||||
performance: Arc<Performance>,
|
||||
cache: Arc<dyn HttpCache>,
|
||||
config_tree: Arc<ConfigTree>,
|
||||
) -> Self {
|
||||
let (tx, request_rx) = mpsc::unbounded_channel::<Request>();
|
||||
Self {
|
||||
performance,
|
||||
|
@ -246,6 +251,7 @@ impl TsServer {
|
|||
sender: tx,
|
||||
receiver: Mutex::new(Some(request_rx)),
|
||||
specifier_map: Arc::new(TscSpecifierMap::new()),
|
||||
config_tree,
|
||||
inspector_server: Mutex::new(None),
|
||||
}
|
||||
}
|
||||
|
@ -268,6 +274,7 @@ impl TsServer {
|
|||
let performance = self.performance.clone();
|
||||
let cache = self.cache.clone();
|
||||
let specifier_map = self.specifier_map.clone();
|
||||
let config_tree = self.config_tree.clone();
|
||||
let _join_handle = thread::spawn(move || {
|
||||
run_tsc_thread(
|
||||
receiver,
|
||||
|
@ -275,6 +282,7 @@ impl TsServer {
|
|||
cache.clone(),
|
||||
specifier_map.clone(),
|
||||
maybe_inspector_server,
|
||||
config_tree,
|
||||
)
|
||||
});
|
||||
}
|
||||
|
@ -343,18 +351,6 @@ impl TsServer {
|
|||
self.request(snapshot, req).await
|
||||
}
|
||||
|
||||
pub async fn configure(
|
||||
&self,
|
||||
snapshot: Arc<StateSnapshot>,
|
||||
tsconfig: TsConfig,
|
||||
) -> Result<bool, AnyError> {
|
||||
let req = TscRequest {
|
||||
method: "$configure",
|
||||
args: json!([tsconfig]),
|
||||
};
|
||||
self.request(snapshot, req).await
|
||||
}
|
||||
|
||||
pub async fn get_supported_code_fixes(
|
||||
&self,
|
||||
snapshot: Arc<StateSnapshot>,
|
||||
|
@ -3482,7 +3478,7 @@ impl CompletionEntry {
|
|||
{
|
||||
if let Ok(import_specifier) = resolve_url(&import_data.file_name) {
|
||||
if let Some(new_module_specifier) = language_server
|
||||
.get_ts_response_import_mapper()
|
||||
.get_ts_response_import_mapper(specifier)
|
||||
.check_specifier(&import_specifier, specifier)
|
||||
.or_else(|| relative_specifier(specifier, &import_specifier))
|
||||
{
|
||||
|
@ -3849,6 +3845,7 @@ struct State {
|
|||
response: Option<Response>,
|
||||
state_snapshot: Arc<StateSnapshot>,
|
||||
specifier_map: Arc<TscSpecifierMap>,
|
||||
config_tree: Arc<ConfigTree>,
|
||||
token: CancellationToken,
|
||||
}
|
||||
|
||||
|
@ -3856,6 +3853,7 @@ impl State {
|
|||
fn new(
|
||||
state_snapshot: Arc<StateSnapshot>,
|
||||
specifier_map: Arc<TscSpecifierMap>,
|
||||
config_tree: Arc<ConfigTree>,
|
||||
performance: Arc<Performance>,
|
||||
) -> Self {
|
||||
Self {
|
||||
|
@ -3864,6 +3862,7 @@ impl State {
|
|||
response: None,
|
||||
state_snapshot,
|
||||
specifier_map,
|
||||
config_tree,
|
||||
token: Default::default(),
|
||||
}
|
||||
}
|
||||
|
@ -4077,10 +4076,20 @@ fn op_script_version(
|
|||
Ok(r)
|
||||
}
|
||||
|
||||
#[op2]
|
||||
#[serde]
|
||||
fn op_ts_config(state: &mut OpState) -> serde_json::Value {
|
||||
let state = state.borrow_mut::<State>();
|
||||
let mark = state.performance.mark("tsc.op.op_ts_config");
|
||||
let r = json!(state.config_tree.root_ts_config());
|
||||
state.performance.measure(mark);
|
||||
r
|
||||
}
|
||||
|
||||
#[op2]
|
||||
#[string]
|
||||
fn op_project_version(state: &mut OpState) -> String {
|
||||
let state = state.borrow_mut::<State>();
|
||||
let state: &mut State = state.borrow_mut::<State>();
|
||||
let mark = state.performance.mark("tsc.op.op_project_version");
|
||||
let r = state.state_snapshot.documents.project_version();
|
||||
state.performance.measure(mark);
|
||||
|
@ -4093,13 +4102,19 @@ fn run_tsc_thread(
|
|||
cache: Arc<dyn HttpCache>,
|
||||
specifier_map: Arc<TscSpecifierMap>,
|
||||
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
||||
config_tree: Arc<ConfigTree>,
|
||||
) {
|
||||
let has_inspector_server = maybe_inspector_server.is_some();
|
||||
// Create and setup a JsRuntime based on a snapshot. It is expected that the
|
||||
// supplied snapshot is an isolate that contains the TypeScript language
|
||||
// server.
|
||||
let mut tsc_runtime = JsRuntime::new(RuntimeOptions {
|
||||
extensions: vec![deno_tsc::init_ops(performance, cache, specifier_map)],
|
||||
extensions: vec![deno_tsc::init_ops(
|
||||
performance,
|
||||
cache,
|
||||
specifier_map,
|
||||
config_tree,
|
||||
)],
|
||||
startup_snapshot: Some(tsc::compiler_snapshot()),
|
||||
inspector: maybe_inspector_server.is_some(),
|
||||
..Default::default()
|
||||
|
@ -4166,12 +4181,14 @@ deno_core::extension!(deno_tsc,
|
|||
op_respond,
|
||||
op_script_names,
|
||||
op_script_version,
|
||||
op_ts_config,
|
||||
op_project_version,
|
||||
],
|
||||
options = {
|
||||
performance: Arc<Performance>,
|
||||
cache: Arc<dyn HttpCache>,
|
||||
specifier_map: Arc<TscSpecifierMap>,
|
||||
config_tree: Arc<ConfigTree>,
|
||||
},
|
||||
state = |state, options| {
|
||||
state.put(State::new(
|
||||
|
@ -4180,10 +4197,10 @@ deno_core::extension!(deno_tsc,
|
|||
cache_metadata: CacheMetadata::new(options.cache.clone()),
|
||||
config: Default::default(),
|
||||
documents: Documents::new(options.cache.clone()),
|
||||
maybe_import_map: None,
|
||||
npm: None,
|
||||
}),
|
||||
options.specifier_map,
|
||||
options.config_tree,
|
||||
options.performance,
|
||||
));
|
||||
},
|
||||
|
@ -4344,9 +4361,10 @@ pub struct UserPreferences {
|
|||
impl UserPreferences {
|
||||
pub fn from_config_for_specifier(
|
||||
config: &config::Config,
|
||||
fmt_config: &FmtOptionsConfig,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Self {
|
||||
let fmt_options = config.tree.fmt_options_for_specifier(specifier);
|
||||
let fmt_config = &fmt_options.options;
|
||||
let base_preferences = Self {
|
||||
allow_incomplete_completions: Some(true),
|
||||
allow_text_changes_in_new_files: Some(specifier.scheme() == "file"),
|
||||
|
@ -4450,7 +4468,8 @@ impl UserPreferences {
|
|||
language_settings.preferences.use_aliases_for_renames,
|
||||
),
|
||||
// Only use workspace settings for quote style if there's no `deno.json`.
|
||||
quote_preference: if config.has_config_file() {
|
||||
quote_preference: if config.tree.has_config_file_for_specifier(specifier)
|
||||
{
|
||||
base_preferences.quote_preference
|
||||
} else {
|
||||
Some(language_settings.preferences.quote_style)
|
||||
|
@ -4630,7 +4649,6 @@ mod tests {
|
|||
assets: Default::default(),
|
||||
cache_metadata: CacheMetadata::new(cache),
|
||||
config: Default::default(),
|
||||
maybe_import_map: None,
|
||||
npm: None,
|
||||
}
|
||||
}
|
||||
|
@ -4645,13 +4663,21 @@ mod tests {
|
|||
Arc::new(GlobalHttpCache::new(location.clone(), RealDenoCacheEnv));
|
||||
let snapshot = Arc::new(mock_state_snapshot(sources, &location));
|
||||
let performance = Arc::new(Performance::default());
|
||||
let ts_server = TsServer::new(performance, cache.clone());
|
||||
let config_tree = Arc::new(ConfigTree::default());
|
||||
config_tree
|
||||
.inject_config_file(
|
||||
deno_config::ConfigFile::new(
|
||||
&json!({
|
||||
"compilerOptions": config,
|
||||
})
|
||||
.to_string(),
|
||||
resolve_url("file:///deno.json").unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.await;
|
||||
let ts_server = TsServer::new(performance, cache.clone(), config_tree);
|
||||
ts_server.start(None);
|
||||
let ts_config = TsConfig::new(config);
|
||||
assert!(ts_server
|
||||
.configure(snapshot.clone(), ts_config,)
|
||||
.await
|
||||
.unwrap());
|
||||
(ts_server, snapshot, cache)
|
||||
}
|
||||
|
||||
|
@ -4670,43 +4696,6 @@ mod tests {
|
|||
assert_eq!(actual, r"test [`a link`](http://deno.land/x/mod.ts) test");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_project_configure() {
|
||||
let temp_dir = TempDir::new();
|
||||
setup(
|
||||
&temp_dir,
|
||||
json!({
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"noEmit": true,
|
||||
}),
|
||||
&[],
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_project_reconfigure() {
|
||||
let temp_dir = TempDir::new();
|
||||
let (ts_server, snapshot, _) = setup(
|
||||
&temp_dir,
|
||||
json!({
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"noEmit": true,
|
||||
}),
|
||||
&[],
|
||||
)
|
||||
.await;
|
||||
let ts_config = TsConfig::new(json!({
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"noEmit": true,
|
||||
"lib": ["deno.ns", "deno.worker"]
|
||||
}));
|
||||
assert!(ts_server.configure(snapshot, ts_config).await.unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_diagnostics() {
|
||||
let temp_dir = TempDir::new();
|
||||
|
@ -4716,6 +4705,7 @@ mod tests {
|
|||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"noEmit": true,
|
||||
"lib": [],
|
||||
}),
|
||||
&[(
|
||||
"file:///a.ts",
|
||||
|
@ -5468,11 +5458,10 @@ mod tests {
|
|||
.inlay_hints
|
||||
.variable_types
|
||||
.suppress_when_type_matches_name = true;
|
||||
let mut config = config::Config::new();
|
||||
let mut config = config::Config::default();
|
||||
config.set_workspace_settings(settings, vec![]);
|
||||
let user_preferences = UserPreferences::from_config_for_specifier(
|
||||
&config,
|
||||
&Default::default(),
|
||||
&ModuleSpecifier::parse("file:///foo.ts").unwrap(),
|
||||
);
|
||||
assert_eq!(
|
||||
|
|
|
@ -857,6 +857,12 @@ pub struct ConfiguredRules {
|
|||
pub no_slow_types: bool,
|
||||
}
|
||||
|
||||
impl Default for ConfiguredRules {
|
||||
fn default() -> Self {
|
||||
get_configured_rules(Default::default(), None)
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfiguredRules {
|
||||
fn incremental_cache_state(&self) -> Vec<&str> {
|
||||
// use a hash of the rule names in order to bust the cache
|
||||
|
|
|
@ -503,9 +503,6 @@ delete Object.prototype.__proto__;
|
|||
}
|
||||
}
|
||||
|
||||
/** @type {ts.CompilerOptions} */
|
||||
let compilationSettings = {};
|
||||
|
||||
/** @type {ts.LanguageService} */
|
||||
let languageService;
|
||||
|
||||
|
@ -720,7 +717,17 @@ delete Object.prototype.__proto__;
|
|||
if (logDebug) {
|
||||
debug("host.getCompilationSettings()");
|
||||
}
|
||||
return compilationSettings;
|
||||
const tsConfig = normalizeConfig(ops.op_ts_config());
|
||||
const { options, errors } = ts
|
||||
.convertCompilerOptionsFromJson(tsConfig, "");
|
||||
Object.assign(options, {
|
||||
allowNonTsExtensions: true,
|
||||
allowImportingTsExtensions: true,
|
||||
});
|
||||
if (errors.length > 0 && logDebug) {
|
||||
debug(ts.formatDiagnostics(errors, host));
|
||||
}
|
||||
return options;
|
||||
},
|
||||
getScriptFileNames() {
|
||||
if (logDebug) {
|
||||
|
@ -1010,21 +1017,6 @@ delete Object.prototype.__proto__;
|
|||
serverRestart();
|
||||
return respond(id, true);
|
||||
}
|
||||
case "$configure": {
|
||||
const config = normalizeConfig(args[0]);
|
||||
const { options, errors } = ts
|
||||
.convertCompilerOptionsFromJson(config, "");
|
||||
Object.assign(options, {
|
||||
allowNonTsExtensions: true,
|
||||
allowImportingTsExtensions: true,
|
||||
});
|
||||
if (errors.length > 0 && logDebug) {
|
||||
debug(ts.formatDiagnostics(errors, host));
|
||||
}
|
||||
compilationSettings = options;
|
||||
moduleSpecifierCache.clear();
|
||||
return respond(id, true);
|
||||
}
|
||||
case "$getSupportedCodeFixes": {
|
||||
return respond(
|
||||
id,
|
||||
|
|
|
@ -145,32 +145,6 @@ fn lsp_tsconfig_types_config_sub_dir() {
|
|||
client.shutdown();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lsp_tsconfig_bad_config_path() {
|
||||
let context = TestContextBuilder::new().use_temp_cwd().build();
|
||||
let mut client = context.new_lsp_command().build();
|
||||
client.initialize(|builder| {
|
||||
builder
|
||||
.set_config("bad_tsconfig.json")
|
||||
.set_maybe_root_uri(None);
|
||||
});
|
||||
let (method, maybe_params) = client.read_notification();
|
||||
assert_eq!(method, "window/showMessage");
|
||||
assert_eq!(maybe_params, Some(lsp::ShowMessageParams {
|
||||
typ: lsp::MessageType::WARNING,
|
||||
message: "The path to the configuration file (\"bad_tsconfig.json\") is not resolvable.".to_string()
|
||||
}));
|
||||
let diagnostics = client.did_open(json!({
|
||||
"textDocument": {
|
||||
"uri": "file:///a/file.ts",
|
||||
"languageId": "typescript",
|
||||
"version": 1,
|
||||
"text": "console.log(Deno.args);\n"
|
||||
}
|
||||
}));
|
||||
assert_eq!(diagnostics.all().len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lsp_triple_slash_types() {
|
||||
let context = TestContextBuilder::new().use_temp_cwd().build();
|
||||
|
@ -223,7 +197,7 @@ fn lsp_import_map() {
|
|||
}
|
||||
}));
|
||||
|
||||
assert_eq!(diagnostics.all().len(), 0);
|
||||
assert_eq!(json!(diagnostics.all()), json!([]));
|
||||
|
||||
let res = client.write_request(
|
||||
"textDocument/hover",
|
||||
|
@ -497,7 +471,7 @@ fn lsp_import_map_embedded_in_config_file_after_initialize() {
|
|||
}]
|
||||
}));
|
||||
|
||||
assert_eq!(client.read_diagnostics().all().len(), 0);
|
||||
assert_eq!(json!(client.read_diagnostics().all()), json!([]));
|
||||
|
||||
let res = client.write_request(
|
||||
"textDocument/hover",
|
||||
|
@ -546,7 +520,7 @@ fn lsp_import_map_config_file_auto_discovered() {
|
|||
}]
|
||||
}));
|
||||
client.wait_until_stderr_line(|line| {
|
||||
line.contains("Auto-resolved configuration file:")
|
||||
line.contains(" Resolved Deno configuration file:")
|
||||
});
|
||||
|
||||
let uri = temp_dir.uri().join("a.ts").unwrap();
|
||||
|
@ -607,7 +581,7 @@ fn lsp_import_map_config_file_auto_discovered() {
|
|||
}]
|
||||
}));
|
||||
client.wait_until_stderr_line(|line| {
|
||||
line.contains("Auto-resolved configuration file:")
|
||||
line.contains(" Resolved Deno configuration file:")
|
||||
});
|
||||
let res = client.write_request(
|
||||
"textDocument/hover",
|
||||
|
@ -675,7 +649,7 @@ fn lsp_import_map_config_file_auto_discovered_symlink() {
|
|||
|
||||
// this will discover the deno.json in the root
|
||||
let search_line = format!(
|
||||
"Auto-resolved configuration file: \"{}\"",
|
||||
" Resolved Deno configuration file: \"{}\"",
|
||||
temp_dir.uri().join("deno.json").unwrap().as_str()
|
||||
);
|
||||
client.wait_until_stderr_line(|line| line.contains(&search_line));
|
||||
|
@ -8946,10 +8920,7 @@ fn lsp_performance() {
|
|||
"lsp.update_diagnostics_deps",
|
||||
"lsp.update_diagnostics_lint",
|
||||
"lsp.update_diagnostics_ts",
|
||||
"lsp.update_import_map",
|
||||
"lsp.update_registries",
|
||||
"lsp.update_tsconfig",
|
||||
"tsc.host.$configure",
|
||||
"tsc.host.$getAssets",
|
||||
"tsc.host.$getDiagnostics",
|
||||
"tsc.host.$getSupportedCodeFixes",
|
||||
|
@ -8959,7 +8930,7 @@ fn lsp_performance() {
|
|||
"tsc.op.op_project_version",
|
||||
"tsc.op.op_script_names",
|
||||
"tsc.op.op_script_version",
|
||||
"tsc.request.$configure",
|
||||
"tsc.op.op_ts_config",
|
||||
"tsc.request.$getAssets",
|
||||
"tsc.request.$getSupportedCodeFixes",
|
||||
"tsc.request.getQuickInfoAtPosition",
|
||||
|
|
Loading…
Add table
Reference in a new issue