mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
Reduce extensive use of snapshot.query
(#11596)
This commit is contained in:
parent
204c59e353
commit
163c374242
8 changed files with 38 additions and 74 deletions
|
@ -10,7 +10,6 @@ use ruff_linter::{
|
||||||
use ruff_notebook::SourceValue;
|
use ruff_notebook::SourceValue;
|
||||||
use ruff_source_file::LineIndex;
|
use ruff_source_file::LineIndex;
|
||||||
use ruff_workspace::resolver::match_any_exclusion;
|
use ruff_workspace::resolver::match_any_exclusion;
|
||||||
use ruff_workspace::FileResolverSettings;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
edit::{Replacement, ToRangeExt},
|
edit::{Replacement, ToRangeExt},
|
||||||
|
@ -24,13 +23,14 @@ pub(crate) type Fixes = FxHashMap<lsp_types::Url, Vec<lsp_types::TextEdit>>;
|
||||||
|
|
||||||
pub(crate) fn fix_all(
|
pub(crate) fn fix_all(
|
||||||
query: &DocumentQuery,
|
query: &DocumentQuery,
|
||||||
file_resolver_settings: &FileResolverSettings,
|
|
||||||
linter_settings: &LinterSettings,
|
linter_settings: &LinterSettings,
|
||||||
encoding: PositionEncoding,
|
encoding: PositionEncoding,
|
||||||
) -> crate::Result<Fixes> {
|
) -> crate::Result<Fixes> {
|
||||||
let document_path = query.file_path();
|
let document_path = query.file_path();
|
||||||
let source_kind = query.make_source_kind();
|
let source_kind = query.make_source_kind();
|
||||||
|
|
||||||
|
let file_resolver_settings = query.settings().file_resolver();
|
||||||
|
|
||||||
// If the document is excluded, return an empty list of fixes.
|
// If the document is excluded, return an empty list of fixes.
|
||||||
if let Some(exclusion) = match_any_exclusion(
|
if let Some(exclusion) = match_any_exclusion(
|
||||||
document_path,
|
document_path,
|
||||||
|
|
|
@ -7,7 +7,7 @@ use ruff_linter::{
|
||||||
linter::{check_path, LinterResult, TokenSource},
|
linter::{check_path, LinterResult, TokenSource},
|
||||||
packaging::detect_package_root,
|
packaging::detect_package_root,
|
||||||
registry::AsRule,
|
registry::AsRule,
|
||||||
settings::{flags, LinterSettings},
|
settings::flags,
|
||||||
source_kind::SourceKind,
|
source_kind::SourceKind,
|
||||||
};
|
};
|
||||||
use ruff_notebook::Notebook;
|
use ruff_notebook::Notebook;
|
||||||
|
@ -17,7 +17,6 @@ use ruff_python_parser::AsMode;
|
||||||
use ruff_source_file::{LineIndex, Locator};
|
use ruff_source_file::{LineIndex, Locator};
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
use ruff_workspace::resolver::match_any_exclusion;
|
use ruff_workspace::resolver::match_any_exclusion;
|
||||||
use ruff_workspace::FileResolverSettings;
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
@ -60,14 +59,11 @@ pub(crate) struct DiagnosticFix {
|
||||||
/// A series of diagnostics across a single text document or an arbitrary number of notebook cells.
|
/// A series of diagnostics across a single text document or an arbitrary number of notebook cells.
|
||||||
pub(crate) type Diagnostics = FxHashMap<lsp_types::Url, Vec<lsp_types::Diagnostic>>;
|
pub(crate) type Diagnostics = FxHashMap<lsp_types::Url, Vec<lsp_types::Diagnostic>>;
|
||||||
|
|
||||||
pub(crate) fn check(
|
pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> Diagnostics {
|
||||||
query: &DocumentQuery,
|
|
||||||
file_resolver_settings: &FileResolverSettings,
|
|
||||||
linter_settings: &LinterSettings,
|
|
||||||
encoding: PositionEncoding,
|
|
||||||
) -> Diagnostics {
|
|
||||||
let document_path = query.file_path();
|
let document_path = query.file_path();
|
||||||
let source_kind = query.make_source_kind();
|
let source_kind = query.make_source_kind();
|
||||||
|
let file_resolver_settings = query.settings().file_resolver();
|
||||||
|
let linter_settings = query.settings().linter();
|
||||||
|
|
||||||
// If the document is excluded, return an empty list of diagnostics.
|
// If the document is excluded, return an empty list of diagnostics.
|
||||||
if let Some(exclusion) = match_any_exclusion(
|
if let Some(exclusion) = match_any_exclusion(
|
||||||
|
|
|
@ -8,12 +8,8 @@ use super::LSPResult;
|
||||||
|
|
||||||
pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> Diagnostics {
|
pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> Diagnostics {
|
||||||
if snapshot.client_settings().lint() {
|
if snapshot.client_settings().lint() {
|
||||||
crate::lint::check(
|
let document = snapshot.query();
|
||||||
snapshot.query(),
|
crate::lint::check(document, snapshot.encoding())
|
||||||
snapshot.query().settings().file_resolver(),
|
|
||||||
snapshot.query().settings().linter(),
|
|
||||||
snapshot.encoding(),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
Diagnostics::default()
|
Diagnostics::default()
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,8 +85,10 @@ fn quick_fix(
|
||||||
.map(|fix| {
|
.map(|fix| {
|
||||||
let mut tracker = WorkspaceEditTracker::new(snapshot.resolved_client_capabilities());
|
let mut tracker = WorkspaceEditTracker::new(snapshot.resolved_client_capabilities());
|
||||||
|
|
||||||
|
let document_url = snapshot.query().make_key().into_url();
|
||||||
|
|
||||||
tracker.set_edits_for_document(
|
tracker.set_edits_for_document(
|
||||||
snapshot.query().make_key().into_url(),
|
document_url.clone(),
|
||||||
document.version(),
|
document.version(),
|
||||||
fix.edits.clone(),
|
fix.edits.clone(),
|
||||||
)?;
|
)?;
|
||||||
|
@ -97,8 +99,7 @@ fn quick_fix(
|
||||||
edit: Some(tracker.into_workspace_edit()),
|
edit: Some(tracker.into_workspace_edit()),
|
||||||
diagnostics: Some(vec![fix.fixed_diagnostic.clone()]),
|
diagnostics: Some(vec![fix.fixed_diagnostic.clone()]),
|
||||||
data: Some(
|
data: Some(
|
||||||
serde_json::to_value(snapshot.query().make_key().into_url())
|
serde_json::to_value(document_url).expect("document url should serialize"),
|
||||||
.expect("document url should serialize"),
|
|
||||||
),
|
),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}))
|
}))
|
||||||
|
@ -157,8 +158,6 @@ fn fix_all(snapshot: &DocumentSnapshot) -> crate::Result<CodeActionOrCommand> {
|
||||||
Some(resolve_edit_for_fix_all(
|
Some(resolve_edit_for_fix_all(
|
||||||
document,
|
document,
|
||||||
snapshot.resolved_client_capabilities(),
|
snapshot.resolved_client_capabilities(),
|
||||||
snapshot.query().settings().file_resolver(),
|
|
||||||
snapshot.query().settings().linter(),
|
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)?),
|
)?),
|
||||||
None,
|
None,
|
||||||
|
@ -194,8 +193,6 @@ fn notebook_fix_all(snapshot: &DocumentSnapshot) -> crate::Result<CodeActionOrCo
|
||||||
Some(resolve_edit_for_fix_all(
|
Some(resolve_edit_for_fix_all(
|
||||||
document,
|
document,
|
||||||
snapshot.resolved_client_capabilities(),
|
snapshot.resolved_client_capabilities(),
|
||||||
snapshot.query().settings().file_resolver(),
|
|
||||||
snapshot.query().settings().linter(),
|
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)?),
|
)?),
|
||||||
None,
|
None,
|
||||||
|
@ -231,8 +228,6 @@ fn organize_imports(snapshot: &DocumentSnapshot) -> crate::Result<CodeActionOrCo
|
||||||
Some(resolve_edit_for_organize_imports(
|
Some(resolve_edit_for_organize_imports(
|
||||||
document,
|
document,
|
||||||
snapshot.resolved_client_capabilities(),
|
snapshot.resolved_client_capabilities(),
|
||||||
snapshot.query().settings().file_resolver(),
|
|
||||||
snapshot.query().settings().linter(),
|
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)?),
|
)?),
|
||||||
None,
|
None,
|
||||||
|
@ -268,8 +263,6 @@ fn notebook_organize_imports(snapshot: &DocumentSnapshot) -> crate::Result<CodeA
|
||||||
Some(resolve_edit_for_organize_imports(
|
Some(resolve_edit_for_organize_imports(
|
||||||
document,
|
document,
|
||||||
snapshot.resolved_client_capabilities(),
|
snapshot.resolved_client_capabilities(),
|
||||||
snapshot.query().settings().file_resolver(),
|
|
||||||
snapshot.query().settings().linter(),
|
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)?),
|
)?),
|
||||||
None,
|
None,
|
||||||
|
|
|
@ -4,8 +4,6 @@ use lsp_server::ErrorCode;
|
||||||
use lsp_types::{self as types, request as req};
|
use lsp_types::{self as types, request as req};
|
||||||
|
|
||||||
use ruff_linter::codes::Rule;
|
use ruff_linter::codes::Rule;
|
||||||
use ruff_linter::settings::LinterSettings;
|
|
||||||
use ruff_workspace::FileResolverSettings;
|
|
||||||
|
|
||||||
use crate::edit::WorkspaceEditTracker;
|
use crate::edit::WorkspaceEditTracker;
|
||||||
use crate::fix::Fixes;
|
use crate::fix::Fixes;
|
||||||
|
@ -57,8 +55,6 @@ impl super::BackgroundDocumentRequestHandler for CodeActionResolve {
|
||||||
resolve_edit_for_fix_all(
|
resolve_edit_for_fix_all(
|
||||||
query,
|
query,
|
||||||
snapshot.resolved_client_capabilities(),
|
snapshot.resolved_client_capabilities(),
|
||||||
query.settings().file_resolver(),
|
|
||||||
query.settings().linter(),
|
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)
|
)
|
||||||
.with_failure_code(ErrorCode::InternalError)?,
|
.with_failure_code(ErrorCode::InternalError)?,
|
||||||
|
@ -68,8 +64,6 @@ impl super::BackgroundDocumentRequestHandler for CodeActionResolve {
|
||||||
resolve_edit_for_organize_imports(
|
resolve_edit_for_organize_imports(
|
||||||
query,
|
query,
|
||||||
snapshot.resolved_client_capabilities(),
|
snapshot.resolved_client_capabilities(),
|
||||||
query.settings().file_resolver(),
|
|
||||||
query.settings().linter(),
|
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)
|
)
|
||||||
.with_failure_code(ErrorCode::InternalError)?,
|
.with_failure_code(ErrorCode::InternalError)?,
|
||||||
|
@ -89,49 +83,35 @@ impl super::BackgroundDocumentRequestHandler for CodeActionResolve {
|
||||||
pub(super) fn resolve_edit_for_fix_all(
|
pub(super) fn resolve_edit_for_fix_all(
|
||||||
query: &DocumentQuery,
|
query: &DocumentQuery,
|
||||||
client_capabilities: &ResolvedClientCapabilities,
|
client_capabilities: &ResolvedClientCapabilities,
|
||||||
file_resolver_settings: &FileResolverSettings,
|
|
||||||
linter_settings: &LinterSettings,
|
|
||||||
encoding: PositionEncoding,
|
encoding: PositionEncoding,
|
||||||
) -> crate::Result<types::WorkspaceEdit> {
|
) -> crate::Result<types::WorkspaceEdit> {
|
||||||
let mut tracker = WorkspaceEditTracker::new(client_capabilities);
|
let mut tracker = WorkspaceEditTracker::new(client_capabilities);
|
||||||
tracker.set_fixes_for_document(
|
tracker.set_fixes_for_document(fix_all_edit(query, encoding)?, query.version())?;
|
||||||
fix_all_edit(query, file_resolver_settings, linter_settings, encoding)?,
|
|
||||||
query.version(),
|
|
||||||
)?;
|
|
||||||
Ok(tracker.into_workspace_edit())
|
Ok(tracker.into_workspace_edit())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn fix_all_edit(
|
pub(super) fn fix_all_edit(
|
||||||
query: &DocumentQuery,
|
query: &DocumentQuery,
|
||||||
file_resolver_settings: &FileResolverSettings,
|
|
||||||
linter_settings: &LinterSettings,
|
|
||||||
encoding: PositionEncoding,
|
encoding: PositionEncoding,
|
||||||
) -> crate::Result<Fixes> {
|
) -> crate::Result<Fixes> {
|
||||||
crate::fix::fix_all(query, file_resolver_settings, linter_settings, encoding)
|
crate::fix::fix_all(query, query.settings().linter(), encoding)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn resolve_edit_for_organize_imports(
|
pub(super) fn resolve_edit_for_organize_imports(
|
||||||
query: &DocumentQuery,
|
query: &DocumentQuery,
|
||||||
client_capabilities: &ResolvedClientCapabilities,
|
client_capabilities: &ResolvedClientCapabilities,
|
||||||
file_resolver_settings: &FileResolverSettings,
|
|
||||||
linter_settings: &LinterSettings,
|
|
||||||
encoding: PositionEncoding,
|
encoding: PositionEncoding,
|
||||||
) -> crate::Result<types::WorkspaceEdit> {
|
) -> crate::Result<types::WorkspaceEdit> {
|
||||||
let mut tracker = WorkspaceEditTracker::new(client_capabilities);
|
let mut tracker = WorkspaceEditTracker::new(client_capabilities);
|
||||||
tracker.set_fixes_for_document(
|
tracker.set_fixes_for_document(organize_imports_edit(query, encoding)?, query.version())?;
|
||||||
organize_imports_edit(query, file_resolver_settings, linter_settings, encoding)?,
|
|
||||||
query.version(),
|
|
||||||
)?;
|
|
||||||
Ok(tracker.into_workspace_edit())
|
Ok(tracker.into_workspace_edit())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn organize_imports_edit(
|
pub(super) fn organize_imports_edit(
|
||||||
query: &DocumentQuery,
|
query: &DocumentQuery,
|
||||||
file_resolver_settings: &FileResolverSettings,
|
|
||||||
linter_settings: &LinterSettings,
|
|
||||||
encoding: PositionEncoding,
|
encoding: PositionEncoding,
|
||||||
) -> crate::Result<Fixes> {
|
) -> crate::Result<Fixes> {
|
||||||
let mut linter_settings = linter_settings.clone();
|
let mut linter_settings = query.settings().linter().clone();
|
||||||
linter_settings.rules = [
|
linter_settings.rules = [
|
||||||
Rule::UnsortedImports, // I001
|
Rule::UnsortedImports, // I001
|
||||||
Rule::MissingRequiredImport, // I002
|
Rule::MissingRequiredImport, // I002
|
||||||
|
@ -139,5 +119,5 @@ pub(super) fn organize_imports_edit(
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
crate::fix::fix_all(query, file_resolver_settings, &linter_settings, encoding)
|
crate::fix::fix_all(query, &linter_settings, encoding)
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,8 +64,6 @@ impl super::SyncRequestHandler for ExecuteCommand {
|
||||||
Command::FixAll => {
|
Command::FixAll => {
|
||||||
let fixes = super::code_action_resolve::fix_all_edit(
|
let fixes = super::code_action_resolve::fix_all_edit(
|
||||||
snapshot.query(),
|
snapshot.query(),
|
||||||
snapshot.query().settings().file_resolver(),
|
|
||||||
snapshot.query().settings().linter(),
|
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)
|
)
|
||||||
.with_failure_code(ErrorCode::InternalError)?;
|
.with_failure_code(ErrorCode::InternalError)?;
|
||||||
|
@ -82,8 +80,6 @@ impl super::SyncRequestHandler for ExecuteCommand {
|
||||||
Command::OrganizeImports => {
|
Command::OrganizeImports => {
|
||||||
let fixes = super::code_action_resolve::organize_imports_edit(
|
let fixes = super::code_action_resolve::organize_imports_edit(
|
||||||
snapshot.query(),
|
snapshot.query(),
|
||||||
snapshot.query().settings().file_resolver(),
|
|
||||||
snapshot.query().settings().linter(),
|
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)
|
)
|
||||||
.with_failure_code(ErrorCode::InternalError)?;
|
.with_failure_code(ErrorCode::InternalError)?;
|
||||||
|
|
|
@ -35,6 +35,7 @@ impl super::BackgroundDocumentRequestHandler for Format {
|
||||||
/// Formats either a full text document or each individual cell in a single notebook document.
|
/// Formats either a full text document or each individual cell in a single notebook document.
|
||||||
pub(super) fn format_full_document(snapshot: &DocumentSnapshot) -> Result<Fixes> {
|
pub(super) fn format_full_document(snapshot: &DocumentSnapshot) -> Result<Fixes> {
|
||||||
let mut fixes = Fixes::default();
|
let mut fixes = Fixes::default();
|
||||||
|
let query = snapshot.query();
|
||||||
|
|
||||||
if let Some(notebook) = snapshot.query().as_notebook() {
|
if let Some(notebook) = snapshot.query().as_notebook() {
|
||||||
for (url, text_document) in notebook
|
for (url, text_document) in notebook
|
||||||
|
@ -43,10 +44,10 @@ pub(super) fn format_full_document(snapshot: &DocumentSnapshot) -> Result<Fixes>
|
||||||
{
|
{
|
||||||
if let Some(changes) = format_text_document(
|
if let Some(changes) = format_text_document(
|
||||||
text_document,
|
text_document,
|
||||||
snapshot.query().source_type(),
|
query.source_type(),
|
||||||
snapshot.query().file_path(),
|
query.file_path(),
|
||||||
snapshot.query().settings().file_resolver(),
|
query.settings().file_resolver(),
|
||||||
snapshot.query().settings().formatter(),
|
query.settings().formatter(),
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
true,
|
true,
|
||||||
)? {
|
)? {
|
||||||
|
@ -55,11 +56,11 @@ pub(super) fn format_full_document(snapshot: &DocumentSnapshot) -> Result<Fixes>
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if let Some(changes) = format_text_document(
|
if let Some(changes) = format_text_document(
|
||||||
snapshot.query().as_single_document().unwrap(),
|
query.as_single_document().unwrap(),
|
||||||
snapshot.query().source_type(),
|
query.source_type(),
|
||||||
snapshot.query().file_path(),
|
query.file_path(),
|
||||||
snapshot.query().settings().file_resolver(),
|
query.settings().file_resolver(),
|
||||||
snapshot.query().settings().formatter(),
|
query.settings().formatter(),
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
false,
|
false,
|
||||||
)? {
|
)? {
|
||||||
|
@ -77,14 +78,15 @@ pub(super) fn format_document(snapshot: &DocumentSnapshot) -> Result<super::Form
|
||||||
.query()
|
.query()
|
||||||
.as_single_document()
|
.as_single_document()
|
||||||
.expect("format should only be called on text documents or notebook cells");
|
.expect("format should only be called on text documents or notebook cells");
|
||||||
|
let query = snapshot.query();
|
||||||
format_text_document(
|
format_text_document(
|
||||||
text_document,
|
text_document,
|
||||||
snapshot.query().source_type(),
|
query.source_type(),
|
||||||
snapshot.query().file_path(),
|
query.file_path(),
|
||||||
snapshot.query().settings().file_resolver(),
|
query.settings().file_resolver(),
|
||||||
snapshot.query().settings().formatter(),
|
query.settings().formatter(),
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
snapshot.query().as_notebook().is_some(),
|
query.as_notebook().is_some(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,13 +38,14 @@ fn format_document_range(
|
||||||
.query()
|
.query()
|
||||||
.as_single_document()
|
.as_single_document()
|
||||||
.expect("format should only be called on text documents or notebook cells");
|
.expect("format should only be called on text documents or notebook cells");
|
||||||
|
let query = snapshot.query();
|
||||||
format_text_document_range(
|
format_text_document_range(
|
||||||
text_document,
|
text_document,
|
||||||
range,
|
range,
|
||||||
snapshot.query().source_type(),
|
query.source_type(),
|
||||||
snapshot.query().file_path(),
|
query.file_path(),
|
||||||
snapshot.query().settings().file_resolver(),
|
query.settings().file_resolver(),
|
||||||
snapshot.query().settings().formatter(),
|
query.settings().formatter(),
|
||||||
snapshot.encoding(),
|
snapshot.encoding(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue