Skip to content

Commit fa7d08f

Browse files
committed
Cleanup
1 parent 1f38ca5 commit fa7d08f

File tree

4 files changed

+59
-75
lines changed

4 files changed

+59
-75
lines changed

crates/ty_project/src/metadata/options.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ impl OptionDiagnostic {
14761476
/// This is a wrapper for options that actually get loaded from configuration files
14771477
/// and the CLI, which also includes a `config_file_override` option that overrides
14781478
/// default configuration discovery with an explicitly-provided path to a configuration file
1479-
#[derive(Debug, Default)]
1479+
#[derive(Debug, Default, PartialEq, Eq, Clone)]
14801480
pub struct ProjectOptionsOverrides {
14811481
pub config_file_override: Option<SystemPathBuf>,
14821482
pub fallback_python_version: Option<RangedValue<PythonVersion>>,
@@ -1496,41 +1496,12 @@ impl ProjectOptionsOverrides {
14961496
pub fn apply_to(&self, options: Options) -> Options {
14971497
let mut combined = self.options.clone().combine(options);
14981498

1499-
if let Some(python_version) = &self.fallback_python_version {
1500-
let configured_python_version = combined
1501-
.environment
1502-
.as_ref()
1503-
.and_then(|environment| environment.python_version.as_ref());
1504-
1505-
if configured_python_version.is_none() {
1506-
tracing::debug!(
1507-
"Using the Python version of the selected Python interpreter in the VS Code Python extension: {python_version}"
1508-
);
1509-
1510-
combined.environment = Some(EnvironmentOptions {
1511-
python_version: Some(python_version.clone()),
1512-
..combined.environment.unwrap_or_default()
1513-
});
1514-
}
1515-
}
1516-
1517-
if let Some(python) = &self.fallback_python {
1518-
let configured_python = combined
1519-
.environment
1520-
.as_ref()
1521-
.and_then(|environment| environment.python.as_ref());
1522-
1523-
if configured_python.is_none() {
1524-
tracing::debug!(
1525-
"Using the environment selected in the VS Code Python extension as python path: {python}"
1526-
);
1527-
1528-
combined.environment = Some(EnvironmentOptions {
1529-
python: Some(python.clone()),
1530-
..combined.environment.unwrap_or_default()
1531-
});
1532-
}
1533-
}
1499+
// Set the fallback python version and path if set
1500+
combined.environment.combine_with(Some(EnvironmentOptions {
1501+
python_version: self.fallback_python_version.clone(),
1502+
python: self.fallback_python.clone(),
1503+
..EnvironmentOptions::default()
1504+
}));
15341505

15351506
combined
15361507
}

crates/ty_server/src/session.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ impl Session {
200200
self.workspaces()
201201
.for_path(root)?
202202
.settings()
203-
.to_project_overrides()
203+
.project_options_overrides()
204+
.cloned()
204205
});
205206

206207
let db = match path {
@@ -261,7 +262,7 @@ impl Session {
261262
.apply_configuration_files(&system)
262263
.context("Failed to apply configuration files")?;
263264

264-
if let Some(overrides) = workspace.settings.to_project_overrides() {
265+
if let Some(overrides) = workspace.settings.project_options_overrides() {
265266
metadata.apply_overrides(&overrides);
266267
}
267268

crates/ty_server/src/session/options.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use lsp_types::Url;
22
use ruff_db::system::SystemPathBuf;
3+
use ruff_python_ast::PythonVersion;
34
use rustc_hash::FxHashMap;
45
use serde::Deserialize;
6+
use ty_project::metadata::Options;
7+
use ty_project::metadata::options::ProjectOptionsOverrides;
8+
use ty_project::metadata::value::{RangedValue, RelativePathBuf};
59

610
use crate::logging::LogLevel;
711
use crate::session::ClientSettings;
@@ -73,16 +77,55 @@ impl DiagnosticMode {
7377
impl ClientOptions {
7478
/// Returns the client settings that are relevant to the language server.
7579
pub(crate) fn into_settings(self) -> ClientSettings {
80+
let overrides = self.python_extension.and_then(|extension| {
81+
let active_environment = extension.active_environment?;
82+
83+
let mut overrides = ProjectOptionsOverrides::new(None, Options::default());
84+
85+
overrides.fallback_python = if let Some(environment) = &active_environment.environment {
86+
environment.folder_uri.to_file_path().ok().and_then(|path| {
87+
Some(RelativePathBuf::python_extension(
88+
SystemPathBuf::from_path_buf(path).ok()?,
89+
))
90+
})
91+
} else {
92+
Some(RelativePathBuf::python_extension(
93+
active_environment.executable.sys_prefix.clone(),
94+
))
95+
};
96+
97+
overrides.fallback_python_version =
98+
active_environment.version.as_ref().and_then(|version| {
99+
Some(RangedValue::python_extension(PythonVersion::from((
100+
u8::try_from(version.major).ok()?,
101+
u8::try_from(version.minor).ok()?,
102+
))))
103+
});
104+
105+
if let Some(python) = &overrides.fallback_python {
106+
tracing::debug!(
107+
"Using the Python environment selected in the VS Code Python extension in case the configuration doesn't specify a Python environment: {python}",
108+
python = python.path()
109+
);
110+
}
111+
112+
if let Some(version) = &overrides.fallback_python_version {
113+
tracing::debug!(
114+
"Using the Python version selected in the VS Code Python extension: {version} in case the configuration doesn't specify a Python version",
115+
);
116+
}
117+
118+
Some(overrides)
119+
});
120+
76121
ClientSettings {
77122
disable_language_services: self
78123
.python
79124
.and_then(|python| python.ty)
80125
.and_then(|ty| ty.disable_language_services)
81126
.unwrap_or_default(),
82127
diagnostic_mode: self.diagnostic_mode.unwrap_or_default(),
83-
active_python_environment: self
84-
.python_extension
85-
.and_then(|extension| extension.active_environment),
128+
overrides,
86129
}
87130
}
88131
}
Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
use super::options::DiagnosticMode;
22

3-
use crate::session::options::ActiveEnvironment;
4-
use ruff_db::system::SystemPathBuf;
5-
use ruff_python_ast::PythonVersion;
6-
use ty_project::metadata::Options;
73
use ty_project::metadata::options::ProjectOptionsOverrides;
8-
use ty_project::metadata::value::{RangedValue, RelativePathBuf};
94

105
/// Resolved client settings for a specific document. These settings are meant to be
116
/// used directly by the server, and are *not* a 1:1 representation with how the client
@@ -15,7 +10,7 @@ use ty_project::metadata::value::{RangedValue, RelativePathBuf};
1510
pub(crate) struct ClientSettings {
1611
pub(super) disable_language_services: bool,
1712
pub(super) diagnostic_mode: DiagnosticMode,
18-
pub(super) active_python_environment: Option<ActiveEnvironment>,
13+
pub(super) overrides: Option<ProjectOptionsOverrides>,
1914
}
2015

2116
impl ClientSettings {
@@ -27,33 +22,7 @@ impl ClientSettings {
2722
self.diagnostic_mode
2823
}
2924

30-
pub(crate) fn to_project_overrides(&self) -> Option<ProjectOptionsOverrides> {
31-
let Some(active_environment) = &self.active_python_environment else {
32-
return None;
33-
};
34-
35-
let mut overrides = ProjectOptionsOverrides::new(None, Options::default());
36-
37-
overrides.fallback_python = if let Some(environment) = &active_environment.environment {
38-
environment.folder_uri.to_file_path().ok().and_then(|path| {
39-
Some(RelativePathBuf::python_extension(
40-
SystemPathBuf::from_path_buf(path).ok()?,
41-
))
42-
})
43-
} else {
44-
Some(RelativePathBuf::python_extension(
45-
active_environment.executable.sys_prefix.clone(),
46-
))
47-
};
48-
49-
overrides.fallback_python_version =
50-
active_environment.version.as_ref().and_then(|version| {
51-
Some(RangedValue::python_extension(PythonVersion::from((
52-
u8::try_from(version.major).ok()?,
53-
u8::try_from(version.minor).ok()?,
54-
))))
55-
});
56-
57-
Some(overrides)
25+
pub(crate) fn project_options_overrides(&self) -> Option<&ProjectOptionsOverrides> {
26+
self.overrides.as_ref()
5827
}
5928
}

0 commit comments

Comments
 (0)