Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ty_ide/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ruff_text_size::TextSize;

use crate::Db;

#[derive(Debug, Clone)]
pub struct Completion {
pub label: String,
}
Expand Down
37 changes: 21 additions & 16 deletions crates/ty_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use lsp_types::{

use self::connection::{Connection, ConnectionInitializer};
use self::schedule::event_loop_thread;
use crate::session::{AllSettings, ClientSettings, Session};
use crate::session::{AllSettings, ClientSettings, Experimental, Session};
use crate::PositionEncoding;

mod api;
Expand All @@ -41,9 +41,19 @@ impl Server {

let (id, init_params) = connection.initialize_start()?;

let AllSettings {
global_settings,
mut workspace_settings,
} = AllSettings::from_value(
init_params
.initialization_options
.unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::default())),
);

let client_capabilities = init_params.capabilities;
let position_encoding = Self::find_best_position_encoding(&client_capabilities);
let server_capabilities = Self::server_capabilities(position_encoding);
let server_capabilities =
Self::server_capabilities(position_encoding, global_settings.experimental.as_ref());

let connection = connection.initialize_finish(
id,
Expand All @@ -53,16 +63,6 @@ impl Server {
)?;

crate::message::init_messenger(connection.make_sender());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's important that init_logging (line 53) happens after init_messenger

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think so too. The messenger needs to be initialized first because it's being used when serializing the initialization options. If there are invalid initialization options, it requires the messenger to provide a notification to the user.


let AllSettings {
global_settings,
mut workspace_settings,
} = AllSettings::from_value(
init_params
.initialization_options
.unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::default())),
);

crate::logging::init_logging(
global_settings.tracing.log_level.unwrap_or_default(),
global_settings.tracing.log_file.as_deref(),
Expand Down Expand Up @@ -206,7 +206,10 @@ impl Server {
.unwrap_or_default()
}

fn server_capabilities(position_encoding: PositionEncoding) -> ServerCapabilities {
fn server_capabilities(
position_encoding: PositionEncoding,
experimental: Option<&Experimental>,
) -> ServerCapabilities {
ServerCapabilities {
position_encoding: Some(position_encoding.into()),
diagnostic_provider: Some(DiagnosticServerCapabilities::Options(DiagnosticOptions {
Expand All @@ -226,9 +229,11 @@ impl Server {
inlay_hint_provider: Some(lsp_types::OneOf::Right(
InlayHintServerCapabilities::Options(InlayHintOptions::default()),
)),
completion_provider: Some(lsp_types::CompletionOptions {
..Default::default()
}),
completion_provider: experimental
.is_some_and(Experimental::is_completions_enabled)
.then_some(lsp_types::CompletionOptions {
..Default::default()
}),
..Default::default()
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/ty_server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) use self::capabilities::ResolvedClientCapabilities;
pub use self::index::DocumentQuery;
pub(crate) use self::settings::AllSettings;
pub use self::settings::ClientSettings;
pub(crate) use self::settings::Experimental;

mod capabilities;
pub(crate) mod index;
Expand Down
24 changes: 24 additions & 0 deletions crates/ty_server/src/session/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,35 @@ use serde::Deserialize;
/// Maps a workspace URI to its associated client settings. Used during server initialization.
pub(crate) type WorkspaceSettingsMap = FxHashMap<Url, ClientSettings>;

#[derive(Debug, Deserialize, Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(rename_all = "camelCase")]
struct Completions {
enable: Option<bool>,
}

#[derive(Debug, Deserialize, Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(rename_all = "camelCase")]
pub(crate) struct Experimental {
completions: Option<Completions>,
}

impl Experimental {
/// Returns `true` if completions are enabled in the settings.
pub(crate) fn is_completions_enabled(&self) -> bool {
self.completions
.as_ref()
.is_some_and(|completions| completions.enable.unwrap_or_default())
}
}

/// This is a direct representation of the settings schema sent by the client.
#[derive(Debug, Deserialize, Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(rename_all = "camelCase")]
pub struct ClientSettings {
pub(crate) experimental: Option<Experimental>,
// These settings are only needed for tracing, and are only read from the global configuration.
// These will not be in the resolved settings.
#[serde(flatten)]
Expand Down
Loading