Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix automatic configuration reloading for text and notebook documents #11492

Merged
merged 2 commits into from
May 22, 2024
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
6 changes: 5 additions & 1 deletion crates/ruff_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,14 @@ impl Server {
watchers: vec![
FileSystemWatcher {
glob_pattern: types::GlobPattern::String(
"**/.?ruff.toml".into(),
"**/.ruff.toml".into(),
),
kind: None,
},
FileSystemWatcher {
glob_pattern: types::GlobPattern::String("**/ruff.toml".into()),
kind: None,
},
FileSystemWatcher {
glob_pattern: types::GlobPattern::String(
"**/pyproject.toml".into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::server::api::diagnostics::publish_diagnostics_for_document;
use crate::server::api::LSPResult;
use crate::server::client::{Notifier, Requester};
use crate::server::schedule::Task;
Expand All @@ -15,18 +16,36 @@ impl super::NotificationHandler for DidChangeWatchedFiles {
impl super::SyncNotificationHandler for DidChangeWatchedFiles {
fn run(
session: &mut Session,
_notifier: Notifier,
notifier: Notifier,
requester: &mut Requester,
params: types::DidChangeWatchedFilesParams,
) -> Result<()> {
for change in &params.changes {
session.reload_settings(&change.uri.to_file_path().unwrap());
}

if session.resolved_client_capabilities().workspace_refresh && !params.changes.is_empty() {
requester
.request::<types::request::WorkspaceDiagnosticRefresh>((), |()| Task::nothing())
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
if !params.changes.is_empty() {
if session.resolved_client_capabilities().workspace_refresh {
requester
.request::<types::request::WorkspaceDiagnosticRefresh>((), |()| Task::nothing())
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
} else {
// publish diagnostics for text documents
for url in session.text_document_urls() {
let snapshot = session
.take_snapshot(&url)
.expect("snapshot should be available");
publish_diagnostics_for_document(&snapshot, &notifier)?;
}
}

// always publish diagnostics for notebook files (since they don't use pull diagnostics)
for url in session.notebook_document_urls() {
let snapshot = session
.take_snapshot(&url)
.expect("snapshot should be available");
publish_diagnostics_for_document(&snapshot, &notifier)?;
}
}

Ok(())
Expand Down
10 changes: 10 additions & 0 deletions crates/ruff_server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ impl Session {
})
}

/// Iterates over the LSP URLs for all open text documents. These URLs are valid file paths.
pub(super) fn text_document_urls(&self) -> impl Iterator<Item = lsp_types::Url> + '_ {
self.index.text_document_urls()
}

/// Iterates over the LSP URLs for all open notebook documents. These URLs are valid file paths.
pub(super) fn notebook_document_urls(&self) -> impl Iterator<Item = lsp_types::Url> + '_ {
self.index.notebook_document_urls()
}

/// Updates a text document at the associated `key`.
///
/// The document key must point to a text document, or this will throw an error.
Expand Down
23 changes: 22 additions & 1 deletion crates/ruff_server/src/session/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ impl Index {
}
}

pub(super) fn text_document_urls(&self) -> impl Iterator<Item = lsp_types::Url> + '_ {
self.documents
.iter()
.filter(|(_, doc)| doc.as_text().is_some())
.map(|(path, _)| {
lsp_types::Url::from_file_path(path).expect("valid file path should convert to URL")
})
}

pub(super) fn notebook_document_urls(&self) -> impl Iterator<Item = lsp_types::Url> + '_ {
self.documents
.iter()
.filter(|(_, doc)| doc.as_notebook().is_some())
.map(|(path, _)| {
lsp_types::Url::from_file_path(path).expect("valid file path should convert to URL")
})
}

pub(super) fn update_text_document(
&mut self,
key: &DocumentKey,
Expand Down Expand Up @@ -234,11 +252,14 @@ impl Index {
Some(controller.make_ref(cell_uri, path, document_settings))
}

/// Reloads relevant existing settings files based on a changed settings file path.
/// This does not currently register new settings files.
pub(super) fn reload_settings(&mut self, changed_path: &PathBuf) {
let search_path = changed_path.parent().unwrap_or(changed_path);
for (root, settings) in self
.settings
.iter_mut()
.filter(|(path, _)| path.starts_with(changed_path))
.filter(|(path, _)| path.starts_with(search_path))
{
settings.workspace_settings_index = ruff_settings::RuffSettingsIndex::new(
root,
Expand Down
Loading