Include document specific debug info (#16215)

## Summary

Related https://github.com/astral-sh/ruff-vscode/pull/692.

## Test Plan

**When there's no active text document:**

```
[Info  - 10:57:03 PM] Global:
executable = /Users/dhruv/work/astral/ruff/target/debug/ruff
version = 0.9.6
position_encoding = UTF16
workspace_root_folders = [
    "/Users/dhruv/playground/ruff",
]
indexed_configuration_files = [
    "/Users/dhruv/playground/ruff/pyproject.toml",
    "/Users/dhruv/playground/ruff/formatter/ruff.toml",
]
open_documents = 0
client_capabilities = ResolvedClientCapabilities {
    code_action_deferred_edit_resolution: true,
    apply_edit: true,
    document_changes: true,
    workspace_refresh: true,
    pull_diagnostics: true,
}

global_client_settings = ResolvedClientSettings {
    fix_all: true,
    organize_imports: true,
    lint_enable: true,
    disable_rule_comment_enable: true,
    fix_violation_enable: true,
    show_syntax_errors: true,
    editor_settings: ResolvedEditorSettings {
        configuration: None,
        lint_preview: None,
        format_preview: None,
        select: None,
        extend_select: None,
        ignore: None,
        exclude: None,
        line_length: None,
        configuration_preference: EditorFirst,
    },
}
```

**When there's an active text document that's been passed as param:**

```
[Info  - 10:53:33 PM] Global:
executable = /Users/dhruv/work/astral/ruff/target/debug/ruff
version = 0.9.6
position_encoding = UTF16
workspace_root_folders = [
    "/Users/dhruv/playground/ruff",
]
indexed_configuration_files = [
    "/Users/dhruv/playground/ruff/pyproject.toml",
    "/Users/dhruv/playground/ruff/formatter/ruff.toml",
]
open_documents = 1
client_capabilities = ResolvedClientCapabilities {
    code_action_deferred_edit_resolution: true,
    apply_edit: true,
    document_changes: true,
    workspace_refresh: true,
    pull_diagnostics: true,
}

Document:
uri = file:///Users/dhruv/playground/ruff/lsp/play.py
kind = Text
version = 1
client_settings = ResolvedClientSettings {
    fix_all: true,
    organize_imports: true,
    lint_enable: true,
    disable_rule_comment_enable: true,
    fix_violation_enable: true,
    show_syntax_errors: true,
    editor_settings: ResolvedEditorSettings {
        configuration: None,
        lint_preview: None,
        format_preview: None,
        select: None,
        extend_select: None,
        ignore: None,
        exclude: None,
        line_length: None,
        configuration_preference: EditorFirst,
    },
}
config_path = Some("/Users/dhruv/playground/ruff/pyproject.toml")

...
```

Replace `...` at the end with the output of `ruff check --show-settings
path.py`
This commit is contained in:
Dhruv Manilawala 2025-02-18 15:38:30 +05:30 committed by GitHub
parent bb2a712f6a
commit ed9c18d9b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 132 additions and 47 deletions

View file

@ -177,21 +177,6 @@ impl Index {
.register_workspace(&Workspace::new(url), global_settings)
}
pub(super) fn num_documents(&self) -> usize {
self.documents.len()
}
pub(super) fn num_workspaces(&self) -> usize {
self.settings.len()
}
pub(super) fn list_config_files(&self) -> Vec<&Path> {
self.settings
.values()
.flat_map(|WorkspaceSettings { ruff_settings, .. }| ruff_settings.list_files())
.collect()
}
pub(super) fn close_workspace_folder(&mut self, workspace_url: &Url) -> crate::Result<()> {
let workspace_path = workspace_url.to_file_path().map_err(|()| {
anyhow!("Failed to convert workspace URL to file path: {workspace_url}")
@ -404,6 +389,23 @@ impl Index {
.next_back()
.map(|(_, settings)| settings)
}
/// Returns an iterator over the workspace root folders contained in this index.
pub(super) fn workspace_root_folders(&self) -> impl Iterator<Item = &Path> {
self.settings.keys().map(PathBuf::as_path)
}
/// Returns the number of open documents.
pub(super) fn open_documents_len(&self) -> usize {
self.documents.len()
}
/// Returns an iterator over the paths to the configuration files in the index.
pub(super) fn config_file_paths(&self) -> impl Iterator<Item = &Path> {
self.settings
.values()
.flat_map(|WorkspaceSettings { ruff_settings, .. }| ruff_settings.config_file_paths())
}
}
/// Maps a workspace folder root to its settings.

View file

@ -20,6 +20,7 @@ use ruff_workspace::{
use crate::session::settings::{ConfigurationPreference, ResolvedEditorSettings};
#[derive(Debug)]
pub struct RuffSettings {
/// The path to this configuration file, used for debugging.
/// The default fallback configuration does not have a file path.
@ -28,6 +29,12 @@ pub struct RuffSettings {
settings: Settings,
}
impl RuffSettings {
pub(crate) fn path(&self) -> Option<&Path> {
self.path.as_deref()
}
}
impl Deref for RuffSettings {
type Target = Settings;
@ -298,15 +305,16 @@ impl RuffSettingsIndex {
.clone()
}
pub(crate) fn list_files(&self) -> impl Iterator<Item = &Path> {
pub(super) fn fallback(&self) -> Arc<RuffSettings> {
self.fallback.clone()
}
/// Returns an iterator over the paths to the configuration files in the index.
pub(crate) fn config_file_paths(&self) -> impl Iterator<Item = &Path> {
self.index
.values()
.filter_map(|settings| settings.path.as_deref())
}
pub(super) fn fallback(&self) -> Arc<RuffSettings> {
self.fallback.clone()
}
}
struct EditorConfigurationTransformer<'a>(&'a ResolvedEditorSettings, &'a Path);