From e38966d8843becc7234fa7d46009c16af4ba41e9 Mon Sep 17 00:00:00 2001 From: Jane Lewis Date: Sat, 20 Apr 2024 17:22:59 -0700 Subject: [PATCH] Rework settings and workspaces to support editor setting resolution --- .../src/server/api/requests/code_action.rs | 4 +- .../api/requests/code_action_resolve.rs | 4 +- .../src/server/api/requests/diagnostic.rs | 2 +- .../server/api/requests/execute_command.rs | 4 +- .../src/server/api/requests/format.rs | 2 +- .../src/server/api/requests/format_range.rs | 2 +- crates/ruff_server/src/session.rs | 5 +- crates/ruff_server/src/session/settings.rs | 11 ++-- crates/ruff_server/src/session/workspace.rs | 53 ++++++++++++------- .../src/session/workspace/ruff_settings.rs | 36 ++++++++++--- 10 files changed, 81 insertions(+), 42 deletions(-) diff --git a/crates/ruff_server/src/server/api/requests/code_action.rs b/crates/ruff_server/src/server/api/requests/code_action.rs index 3896ec9427a2c9..7f5c0b97709cee 100644 --- a/crates/ruff_server/src/server/api/requests/code_action.rs +++ b/crates/ruff_server/src/server/api/requests/code_action.rs @@ -105,7 +105,7 @@ fn fix_all(snapshot: &DocumentSnapshot) -> crate::Result { document, snapshot.resolved_client_capabilities(), snapshot.url(), - &snapshot.settings().linter, + snapshot.settings().linter(), snapshot.encoding(), document.version(), )?), @@ -140,7 +140,7 @@ fn organize_imports(snapshot: &DocumentSnapshot) -> crate::Result { let edits = super::code_action_resolve::fix_all_edit( snapshot.document(), - &snapshot.settings().linter, + snapshot.settings().linter(), snapshot.encoding(), ) .with_failure_code(ErrorCode::InternalError)?; @@ -83,7 +83,7 @@ impl super::SyncRequestHandler for ExecuteCommand { Command::OrganizeImports => { let edits = super::code_action_resolve::organize_imports_edit( snapshot.document(), - &snapshot.settings().linter, + snapshot.settings().linter(), snapshot.encoding(), ) .with_failure_code(ErrorCode::InternalError)?; diff --git a/crates/ruff_server/src/server/api/requests/format.rs b/crates/ruff_server/src/server/api/requests/format.rs index c8a9260324f825..dbfbc1af4e0987 100644 --- a/crates/ruff_server/src/server/api/requests/format.rs +++ b/crates/ruff_server/src/server/api/requests/format.rs @@ -26,7 +26,7 @@ impl super::BackgroundDocumentRequestHandler for Format { pub(super) fn format_document(snapshot: &DocumentSnapshot) -> Result { let doc = snapshot.document(); let source = doc.contents(); - let formatted = crate::format::format(doc, &snapshot.settings().formatter) + let formatted = crate::format::format(doc, snapshot.settings().formatter()) .with_failure_code(lsp_server::ErrorCode::InternalError)?; // fast path - if the code is the same, return early if formatted == source { diff --git a/crates/ruff_server/src/server/api/requests/format_range.rs b/crates/ruff_server/src/server/api/requests/format_range.rs index 904be4914e8b5f..e01b4b042a1ebd 100644 --- a/crates/ruff_server/src/server/api/requests/format_range.rs +++ b/crates/ruff_server/src/server/api/requests/format_range.rs @@ -22,7 +22,7 @@ impl super::BackgroundDocumentRequestHandler for FormatRange { let index = document.index(); let range = params.range.to_text_range(text, index, snapshot.encoding()); let formatted_range = - crate::format::format_range(document, &snapshot.settings().formatter, range) + crate::format::format_range(document, snapshot.settings().formatter(), range) .with_failure_code(lsp_server::ErrorCode::InternalError)?; Ok(Some(vec![types::TextEdit { range: formatted_range diff --git a/crates/ruff_server/src/session.rs b/crates/ruff_server/src/session.rs index d1d7b80f2e4742..51a8909c033d60 100644 --- a/crates/ruff_server/src/session.rs +++ b/crates/ruff_server/src/session.rs @@ -46,11 +46,11 @@ impl Session { ) -> crate::Result { Ok(Self { position_encoding, + workspaces: workspace::Workspaces::new(workspaces, &global_settings)?, global_settings, resolved_client_capabilities: Arc::new(ResolvedClientCapabilities::new( client_capabilities, )), - workspaces: workspace::Workspaces::new(workspaces)?, }) } @@ -87,7 +87,8 @@ impl Session { } pub(crate) fn open_workspace_folder(&mut self, url: &Url) -> crate::Result<()> { - self.workspaces.open_workspace_folder(url)?; + self.workspaces + .open_workspace_folder(url, &self.global_settings)?; Ok(()) } diff --git a/crates/ruff_server/src/session/settings.rs b/crates/ruff_server/src/session/settings.rs index 84e841ad72ab6c..924ae195c7ac54 100644 --- a/crates/ruff_server/src/session/settings.rs +++ b/crates/ruff_server/src/session/settings.rs @@ -11,7 +11,7 @@ pub(crate) type WorkspaceSettingsMap = FxHashMap; /// Resolved client settings for a specific document. These settings are meant to be /// used directly by the server, and are *not* a 1:1 representation with how the client /// sends them. -#[derive(Debug)] +#[derive(Clone, Debug)] #[cfg_attr(test, derive(PartialEq, Eq))] #[allow(clippy::struct_excessive_bools)] pub(crate) struct ResolvedClientSettings { @@ -22,17 +22,14 @@ pub(crate) struct ResolvedClientSettings { #[allow(dead_code)] disable_rule_comment_enable: bool, fix_violation_enable: bool, - // TODO(jane): Remove once editor settings resolution is implemented - #[allow(dead_code)] editor_settings: ResolvedEditorSettings, } /// Contains the resolved values of 'editor settings' - Ruff configuration for the linter/formatter that was passed in via /// LSP client settings. These fields are optional because we don't want to override file-based linter/formatting settings /// if these were un-set. -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] #[cfg_attr(test, derive(PartialEq, Eq))] -#[allow(dead_code)] // TODO(jane): Remove once editor settings resolution is implemented pub(crate) struct ResolvedEditorSettings { lint_preview: Option, format_preview: Option, @@ -306,6 +303,10 @@ impl ResolvedClientSettings { pub(crate) fn fix_violation(&self) -> bool { self.fix_violation_enable } + + pub(crate) fn editor_settings(&self) -> &ResolvedEditorSettings { + &self.editor_settings + } } impl Default for InitializationOptions { diff --git a/crates/ruff_server/src/session/workspace.rs b/crates/ruff_server/src/session/workspace.rs index a508a360c8cd2d..cb8332c7306514 100644 --- a/crates/ruff_server/src/session/workspace.rs +++ b/crates/ruff_server/src/session/workspace.rs @@ -12,7 +12,10 @@ use crate::{edit::DocumentVersion, Document}; use self::ruff_settings::RuffSettingsIndex; -use super::{settings, ClientSettings}; +use super::{ + settings::{self, ResolvedClientSettings, ResolvedEditorSettings}, + ClientSettings, +}; mod ruff_settings; @@ -23,7 +26,7 @@ pub(crate) struct Workspaces(BTreeMap); pub(crate) struct Workspace { open_documents: OpenDocuments, - settings: ClientSettings, + settings: ResolvedClientSettings, } pub(crate) struct OpenDocuments { @@ -46,18 +49,28 @@ pub(crate) struct DocumentRef { } impl Workspaces { - pub(super) fn new(workspaces: Vec<(Url, ClientSettings)>) -> crate::Result { + pub(super) fn new( + workspaces: Vec<(Url, ClientSettings)>, + global_settings: &ClientSettings, + ) -> crate::Result { Ok(Self( workspaces .into_iter() - .map(|(url, settings)| Workspace::new(&url, settings)) + .map(|(url, workspace_settings)| { + Workspace::new(&url, &workspace_settings, global_settings) + }) .collect::>()?, )) } - pub(super) fn open_workspace_folder(&mut self, folder_url: &Url) -> crate::Result<()> { + pub(super) fn open_workspace_folder( + &mut self, + folder_url: &Url, + global_settings: &ClientSettings, + ) -> crate::Result<()> { // TODO(jane): find a way to allow for workspace settings to be updated dynamically - let (path, workspace) = Workspace::new(folder_url, ClientSettings::default())?; + let (path, workspace) = + Workspace::new(folder_url, &ClientSettings::default(), global_settings)?; self.0.insert(path, workspace); Ok(()) } @@ -117,12 +130,7 @@ impl Workspaces { ); settings::ResolvedClientSettings::global(global_settings) }, - |workspace| { - settings::ResolvedClientSettings::with_workspace( - &workspace.settings, - global_settings, - ) - }, + |workspace| workspace.settings.clone(), ) } @@ -152,13 +160,19 @@ impl Workspaces { } impl Workspace { - pub(crate) fn new(root: &Url, settings: ClientSettings) -> crate::Result<(PathBuf, Self)> { + pub(crate) fn new( + root: &Url, + workspace_settings: &ClientSettings, + global_settings: &ClientSettings, + ) -> crate::Result<(PathBuf, Self)> { let path = root .to_file_path() .map_err(|()| anyhow!("workspace URL was not a file path!"))?; + let settings = ResolvedClientSettings::with_workspace(workspace_settings, global_settings); + let workspace = Self { - open_documents: OpenDocuments::new(&path), + open_documents: OpenDocuments::new(&path, settings.editor_settings()), settings, }; @@ -166,15 +180,16 @@ impl Workspace { } fn reload_settings(&mut self, root: &Path) { - self.open_documents.reload_settings(root); + self.open_documents + .reload_settings(root, self.settings.editor_settings()); } } impl OpenDocuments { - fn new(path: &Path) -> Self { + fn new(path: &Path, editor_settings: &ResolvedEditorSettings) -> Self { Self { documents: FxHashMap::default(), - settings_index: RuffSettingsIndex::new(path), + settings_index: RuffSettingsIndex::new(path, editor_settings), } } @@ -209,8 +224,8 @@ impl OpenDocuments { Ok(()) } - fn reload_settings(&mut self, root: &Path) { - self.settings_index = RuffSettingsIndex::new(root); + fn reload_settings(&mut self, root: &Path, editor_settings: &ResolvedEditorSettings) { + self.settings_index = RuffSettingsIndex::new(root, editor_settings); } } diff --git a/crates/ruff_server/src/session/workspace/ruff_settings.rs b/crates/ruff_server/src/session/workspace/ruff_settings.rs index d1e13d6370f2f9..70c56aafa95136 100644 --- a/crates/ruff_server/src/session/workspace/ruff_settings.rs +++ b/crates/ruff_server/src/session/workspace/ruff_settings.rs @@ -10,12 +10,14 @@ use std::{ }; use walkdir::{DirEntry, WalkDir}; +use crate::session::settings::ResolvedEditorSettings; + #[derive(Default)] pub(crate) struct RuffSettings { // settings to pass into the ruff linter - pub(crate) linter: ruff_linter::settings::LinterSettings, + linter: ruff_linter::settings::LinterSettings, // settings to pass into the ruff formatter - pub(crate) formatter: ruff_workspace::FormatterSettings, + formatter: ruff_workspace::FormatterSettings, } pub(super) struct RuffSettingsIndex { @@ -36,8 +38,27 @@ impl std::fmt::Display for RuffSettings { } } +impl RuffSettings { + pub(crate) fn resolve( + linter: ruff_linter::settings::LinterSettings, + formatter: ruff_workspace::FormatterSettings, + editor_settings: &ResolvedEditorSettings, + ) -> Self { + // TODO(jane): impl resolution + Self { linter, formatter } + } + + pub(crate) fn linter(&self) -> &ruff_linter::settings::LinterSettings { + &self.linter + } + + pub(crate) fn formatter(&self) -> &ruff_workspace::FormatterSettings { + &self.formatter + } +} + impl RuffSettingsIndex { - pub(super) fn new(root: &Path) -> Self { + pub(super) fn new(root: &Path, editor_settings: &ResolvedEditorSettings) -> Self { let mut index = BTreeMap::default(); for directory in WalkDir::new(root) @@ -56,10 +77,11 @@ impl RuffSettingsIndex { }; index.insert( directory, - Arc::new(RuffSettings { - linter: settings.linter, - formatter: settings.formatter, - }), + Arc::new(RuffSettings::resolve( + settings.linter, + settings.formatter, + editor_settings, + )), ); } }