Skip to content

Commit

Permalink
Consider --preview flag for server subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jul 5, 2024
1 parent f3ccd15 commit 0ad5123
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
17 changes: 14 additions & 3 deletions crates/ruff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,20 @@ pub struct FormatCommand {

#[derive(Copy, Clone, Debug, clap::Parser)]
pub struct ServerCommand {
/// Enable preview mode; required for regular operation
#[arg(long)]
pub(crate) preview: bool,
/// Enable preview mode. Use `--no-preview` to disable.
///
/// This enables unstable server features and turns on the preview mode for the linter
/// and the formatter.
#[arg(long, overrides_with("no_preview"))]
preview: bool,
#[clap(long, overrides_with("preview"), hide = true)]
no_preview: bool,
}

impl ServerCommand {
pub(crate) fn resolve_preview(&self) -> Option<bool> {
resolve_bool_arg(self.preview, self.no_preview)
}
}

#[derive(Debug, Clone, Copy, clap::ValueEnum)]
Expand Down
12 changes: 5 additions & 7 deletions crates/ruff/src/commands/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use crate::ExitStatus;
use anyhow::Result;
use ruff_server::Server;

pub(crate) fn run_server(preview: bool, worker_threads: NonZeroUsize) -> Result<ExitStatus> {
if !preview {
tracing::error!("--preview needs to be provided as a command line argument while the server is still unstable.\nFor example: `ruff server --preview`");
return Ok(ExitStatus::Error);
}

let server = Server::new(worker_threads)?;
pub(crate) fn run_server(
worker_threads: NonZeroUsize,
preview: Option<bool>,
) -> Result<ExitStatus> {
let server = Server::new(worker_threads, preview)?;

server.run().map(|()| ExitStatus::Success)
}
4 changes: 1 addition & 3 deletions crates/ruff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,13 @@ fn format(args: FormatCommand, global_options: GlobalConfigArgs) -> Result<ExitS
}

fn server(args: ServerCommand) -> Result<ExitStatus> {
let ServerCommand { preview } = args;

let four = NonZeroUsize::new(4).unwrap();

// by default, we set the number of worker threads to `num_cpus`, with a maximum of 4.
let worker_threads = std::thread::available_parallelism()
.unwrap_or(four)
.max(four);
commands::server::run_server(preview, worker_threads)
commands::server::run_server(worker_threads, args.resolve_preview())
}

pub fn check(args: CheckCommand, global_options: GlobalConfigArgs) -> Result<ExitStatus> {
Expand Down
14 changes: 9 additions & 5 deletions crates/ruff_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Server {
}

impl Server {
pub fn new(worker_threads: NonZeroUsize) -> crate::Result<Self> {
pub fn new(worker_threads: NonZeroUsize, preview: Option<bool>) -> crate::Result<Self> {
let connection = ConnectionInitializer::stdio();

let (id, init_params) = connection.initialize_start()?;
Expand All @@ -70,14 +70,18 @@ impl Server {

crate::message::init_messenger(connection.make_sender());

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

crate::trace::init_tracing(
connection.make_sender(),
Expand Down
23 changes: 22 additions & 1 deletion crates/ruff_server/src/session/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ pub struct ClientSettings {
pub(crate) tracing: TracingSettings,
}

impl ClientSettings {
pub(crate) fn set_preview(&mut self, preview: bool) {
if let Some(lint) = self.lint.as_mut() {
lint.preview = Some(preview);
}
if let Some(format) = self.format.as_mut() {
format.preview = Some(preview);
}
}
}

/// Settings needed to initialize tracing. These will only be
/// read from the global configuration.
#[derive(Debug, Deserialize, Default)]
Expand All @@ -107,7 +118,7 @@ struct WorkspaceSettings {
workspace: Url,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Default, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(rename_all = "camelCase")]
struct LintOptions {
Expand Down Expand Up @@ -179,6 +190,16 @@ impl AllSettings {
)
}

/// Set the preview flag for both the global and all workspace settings.
pub(crate) fn set_preview(&mut self, preview: bool) {
self.global_settings.set_preview(preview);
if let Some(workspace_settings) = self.workspace_settings.as_mut() {
for settings in workspace_settings.values_mut() {
settings.set_preview(preview);
}
}
}

fn from_init_options(options: InitializationOptions) -> Self {
let (global_settings, workspace_settings) = match options {
InitializationOptions::GlobalOnly { settings } => (settings, None),
Expand Down

0 comments on commit 0ad5123

Please sign in to comment.