Skip to content

Commit 599dd21

Browse files
author
UnboundVariable
committed
Code review feedback.
1 parent 05d0daf commit 599dd21

File tree

6 files changed

+17
-84
lines changed

6 files changed

+17
-84
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ty_ide/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ruff_db = { workspace = true }
1616
ruff_python_ast = { workspace = true }
1717
ruff_python_parser = { workspace = true }
1818
ruff_python_trivia = { workspace = true }
19+
ruff_source_file = { workspace = true }
1920
ruff_text_size = { workspace = true }
2021
ty_python_semantic = { workspace = true }
2122

crates/ty_ide/src/docstring.rs

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
use regex::Regex;
1010
use ruff_python_trivia::leading_indentation;
11+
use ruff_source_file::UniversalNewlines;
1112
use std::collections::HashMap;
1213
use std::sync::LazyLock;
1314

@@ -57,67 +58,6 @@ pub fn get_parameter_documentation(docstring: &str) -> HashMap<String, String> {
5758
param_docs
5859
}
5960

60-
/// Iterator that splits text on universal newlines (`\r\n`, `\r`, `\n`) similar to Python's `str.splitlines()`.
61-
/// This ensures consistent behavior across different platforms and line ending styles.
62-
struct UniversalLinesIterator<'a> {
63-
text: &'a str,
64-
position: usize,
65-
}
66-
67-
impl<'a> UniversalLinesIterator<'a> {
68-
fn new(text: &'a str) -> Self {
69-
Self { text, position: 0 }
70-
}
71-
}
72-
73-
impl<'a> Iterator for UniversalLinesIterator<'a> {
74-
type Item = &'a str;
75-
76-
fn next(&mut self) -> Option<Self::Item> {
77-
if self.position >= self.text.len() {
78-
return None;
79-
}
80-
81-
let start = self.position;
82-
let remaining = &self.text[start..];
83-
84-
// Find the next line ending
85-
if let Some(pos) = remaining.find('\n') {
86-
// Check if it's \r\n
87-
let end = if pos > 0 && remaining.as_bytes()[pos - 1] == b'\r' {
88-
start + pos - 1 // Don't include the \r
89-
} else {
90-
start + pos // Don't include the \n
91-
};
92-
self.position = start + pos + 1; // Move past the \n
93-
Some(&self.text[start..end])
94-
} else if let Some(pos) = remaining.find('\r') {
95-
// Just \r (old Mac style)
96-
let end = start + pos;
97-
self.position = start + pos + 1; // Move past the \r
98-
Some(&self.text[start..end])
99-
} else {
100-
// No more line endings, return the rest
101-
self.position = self.text.len();
102-
if start < self.text.len() {
103-
Some(&self.text[start..])
104-
} else {
105-
None
106-
}
107-
}
108-
}
109-
}
110-
111-
trait UniversalLines {
112-
fn universal_lines(&self) -> UniversalLinesIterator<'_>;
113-
}
114-
115-
impl UniversalLines for str {
116-
fn universal_lines(&self) -> UniversalLinesIterator<'_> {
117-
UniversalLinesIterator::new(self)
118-
}
119-
}
120-
12161
/// Extract parameter documentation from Google-style docstrings.
12262
fn extract_google_style_params(docstring: &str) -> Option<HashMap<String, String>> {
12363
let mut param_docs = HashMap::new();
@@ -126,7 +66,8 @@ fn extract_google_style_params(docstring: &str) -> Option<HashMap<String, String
12666
let mut current_param: Option<String> = None;
12767
let mut current_doc = String::new();
12868

129-
for line in docstring.universal_lines() {
69+
for line_obj in docstring.universal_newlines() {
70+
let line = line_obj.as_str();
13071
if GOOGLE_SECTION_REGEX.is_match(line) {
13172
in_args_section = true;
13273
continue;
@@ -213,7 +154,10 @@ fn get_indentation_level(line: &str) -> usize {
213154
fn extract_numpy_style_params(docstring: &str) -> Option<HashMap<String, String>> {
214155
let mut param_docs = HashMap::new();
215156

216-
let mut lines = docstring.universal_lines().peekable();
157+
let mut lines = docstring
158+
.universal_newlines()
159+
.map(|line| line.as_str())
160+
.peekable();
217161
let mut in_params_section = false;
218162
let mut found_underline = false;
219163
let mut current_param: Option<String> = None;
@@ -384,7 +328,8 @@ fn extract_rest_style_params(docstring: &str) -> Option<HashMap<String, String>>
384328
let mut current_param: Option<String> = None;
385329
let mut current_doc = String::new();
386330

387-
for line in docstring.universal_lines() {
331+
for line_obj in docstring.universal_newlines() {
332+
let line = line_obj.as_str();
388333
if let Some(captures) = REST_PARAM_REGEX.captures(line) {
389334
// Save previous parameter if exists
390335
if let Some(param_name) = current_param.take() {

crates/ty_python_semantic/src/types/call/bind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,8 +2270,8 @@ impl<'db> Binding<'db> {
22702270

22712271
/// Returns a vector where each index corresponds to an argument position,
22722272
/// and the value is the parameter index that argument maps to (if any).
2273-
pub(crate) fn argument_to_parameter_mapping(&self) -> Vec<Option<usize>> {
2274-
self.argument_parameters.to_vec()
2273+
pub(crate) fn argument_to_parameter_mapping(&self) -> Box<[Option<usize>]> {
2274+
self.argument_parameters.clone()
22752275
}
22762276
}
22772277

crates/ty_python_semantic/src/types/ide_support.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ pub struct CallSignatureDetails<'db> {
394394

395395
/// Mapping from argument indices to parameter indices. This helps
396396
/// determine which parameter corresponds to which argument position.
397-
pub argument_to_parameter_mapping: Vec<Option<usize>>,
397+
pub argument_to_parameter_mapping: Box<[Option<usize>]>,
398398
}
399399

400400
/// Extract signature details from a callable type.
@@ -476,7 +476,7 @@ fn create_argument_mapping(
476476
callable: crate::types::CallableType<'_>,
477477
signature: &Signature<'_>,
478478
arguments: &ast::Arguments,
479-
) -> Vec<Option<usize>> {
479+
) -> Box<[Option<usize>]> {
480480
let call_arguments = CallArguments::from_arguments(arguments);
481481

482482
let mut argument_forms = vec![None; call_arguments.len()];

crates/ty_server/src/server/api/requests/signature_help.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ use ruff_db::source::{line_index, source_text};
1515
use ty_ide::signature_help;
1616
use ty_project::ProjectDatabase;
1717

18-
/// Client capabilities for signature help feature.
19-
#[derive(Debug, Clone, Default)]
20-
struct SignatureHelpClientCapabilities {
21-
/// Does the client support signature label offsets?
22-
signature_label_offset_support: bool,
23-
24-
/// Does the client support per-signature active parameter?
25-
active_parameter_support: bool,
26-
}
27-
2818
pub(crate) struct SignatureHelpRequestHandler;
2919

3020
impl RequestHandler for SignatureHelpRequestHandler {
@@ -61,10 +51,6 @@ impl BackgroundDocumentRequestHandler for SignatureHelpRequestHandler {
6151

6252
// Extract signature help capabilities from the client
6353
let resolved_capabilities = snapshot.resolved_client_capabilities();
64-
let client_capabilities = SignatureHelpClientCapabilities {
65-
signature_label_offset_support: resolved_capabilities.signature_label_offset_support,
66-
active_parameter_support: resolved_capabilities.signature_active_parameter_support,
67-
};
6854

6955
let Some(signature_help_info) = signature_help(db, file, offset) else {
7056
return Ok(None);
@@ -86,7 +72,7 @@ impl BackgroundDocumentRequestHandler for SignatureHelpRequestHandler {
8672
.parameters
8773
.into_iter()
8874
.map(|param| {
89-
let label = if client_capabilities.signature_label_offset_support {
75+
let label = if resolved_capabilities.signature_label_offset_support {
9076
// Find the parameter's offset in the signature label
9177
if let Some(start) = sig.label.find(&param.label) {
9278
let start_u32 = u32::try_from(start).unwrap_or(u32::MAX);
@@ -107,7 +93,7 @@ impl BackgroundDocumentRequestHandler for SignatureHelpRequestHandler {
10793
})
10894
.collect();
10995

110-
let active_parameter = if client_capabilities.active_parameter_support {
96+
let active_parameter = if resolved_capabilities.signature_active_parameter_support {
11197
sig.active_parameter.and_then(|p| u32::try_from(p).ok())
11298
} else {
11399
None

0 commit comments

Comments
 (0)