Skip to content

Commit 4ab6b4e

Browse files
authored
refactor: extract constants and improve code quality (#20)
1 parent b572504 commit 4ab6b4e

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

crates/deps-core/src/cache.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ use std::time::Instant;
77
/// Maximum number of cached entries to prevent unbounded memory growth.
88
const MAX_CACHE_ENTRIES: usize = 1000;
99

10+
/// HTTP request timeout in seconds.
11+
const HTTP_TIMEOUT_SECS: u64 = 30;
12+
13+
/// Percentage of cache entries to evict when capacity is reached.
14+
const CACHE_EVICTION_PERCENTAGE: usize = 10;
15+
1016
/// Validates that a URL uses HTTPS protocol.
1117
///
1218
/// Returns an error if the URL doesn't start with "https://".
@@ -96,12 +102,12 @@ pub struct HttpCache {
96102
impl HttpCache {
97103
/// Creates a new HTTP cache with default configuration.
98104
///
99-
/// The cache uses a 30-second timeout for all requests and identifies
100-
/// itself with a `deps-lsp/0.1.0` user agent.
105+
/// The cache uses a configurable timeout for all requests and identifies
106+
/// itself with an auto-versioned user agent.
101107
pub fn new() -> Self {
102108
let client = Client::builder()
103-
.user_agent("deps-lsp/0.1.0")
104-
.timeout(std::time::Duration::from_secs(30))
109+
.user_agent(format!("deps-lsp/{}", env!("CARGO_PKG_VERSION")))
110+
.timeout(std::time::Duration::from_secs(HTTP_TIMEOUT_SECS))
105111
.build()
106112
.expect("failed to create HTTP client");
107113

@@ -330,12 +336,12 @@ impl HttpCache {
330336
self.entries.is_empty()
331337
}
332338

333-
/// Evicts approximately 10% of cache entries when capacity is reached.
339+
/// Evicts approximately `CACHE_EVICTION_PERCENTAGE`% of cache entries when capacity is reached.
334340
///
335341
/// Uses a simple random eviction strategy. In a production system,
336342
/// this could be replaced with LRU or TTL-based eviction.
337343
fn evict_entries(&self) {
338-
let target_removals = MAX_CACHE_ENTRIES / 10;
344+
let target_removals = MAX_CACHE_ENTRIES / CACHE_EVICTION_PERCENTAGE;
339345
let mut removed = 0;
340346

341347
// Simple eviction: remove oldest entries by fetched_at timestamp

crates/deps-core/src/handler.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ use tower_lsp::lsp_types::{
1414
InlayHint, InlayHintKind, InlayHintLabel, InlayHintLabelPart, MarkupContent, MarkupKind, Range,
1515
};
1616

17+
/// Maximum number of versions to display in hover tooltips.
18+
const MAX_VERSIONS_IN_HOVER: usize = 8;
19+
20+
/// Maximum number of features to display in hover tooltips.
21+
const MAX_FEATURES_IN_HOVER: usize = 10;
22+
23+
/// Maximum number of versions to offer in code action suggestions.
24+
const MAX_CODE_ACTION_VERSIONS: usize = 5;
25+
1726
/// Generic handler for LSP operations across ecosystems.
1827
///
1928
/// This trait uses Generic Associated Types (GATs) to provide
@@ -431,26 +440,32 @@ where
431440
}
432441

433442
markdown.push_str("**Versions** *(use Cmd+. to update)*:\n");
434-
for (i, version) in versions.iter().take(8).enumerate() {
443+
for (i, version) in versions.iter().take(MAX_VERSIONS_IN_HOVER).enumerate() {
435444
if i == 0 {
436445
markdown.push_str(&format!("- {} *(latest)*\n", version.version_string()));
437446
} else {
438447
markdown.push_str(&format!("- {}\n", version.version_string()));
439448
}
440449
}
441-
if versions.len() > 8 {
442-
markdown.push_str(&format!("- *...and {} more*\n", versions.len() - 8));
450+
if versions.len() > MAX_VERSIONS_IN_HOVER {
451+
markdown.push_str(&format!(
452+
"- *...and {} more*\n",
453+
versions.len() - MAX_VERSIONS_IN_HOVER
454+
));
443455
}
444456

445457
// Features (if supported by ecosystem)
446458
let features = latest.features();
447459
if !features.is_empty() {
448460
markdown.push_str("\n**Features**:\n");
449-
for feature in features.iter().take(10) {
461+
for feature in features.iter().take(MAX_FEATURES_IN_HOVER) {
450462
markdown.push_str(&format!("- `{}`\n", feature));
451463
}
452-
if features.len() > 10 {
453-
markdown.push_str(&format!("- *...and {} more*\n", features.len() - 10));
464+
if features.len() > MAX_FEATURES_IN_HOVER {
465+
markdown.push_str(&format!(
466+
"- *...and {} more*\n",
467+
features.len() - MAX_FEATURES_IN_HOVER
468+
));
454469
}
455470
}
456471

@@ -562,11 +577,11 @@ where
562577
continue;
563578
};
564579

565-
// Offer up to 5 non-deprecated versions
580+
// Offer up to MAX_CODE_ACTION_VERSIONS non-deprecated versions
566581
for (i, version) in versions
567582
.iter()
568583
.filter(|v| !H::is_deprecated(v))
569-
.take(5)
584+
.take(MAX_CODE_ACTION_VERSIONS)
570585
.enumerate()
571586
{
572587
let new_text = H::format_version_for_edit(dep, version.version_string());

crates/deps-lsp/src/server.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ use tower_lsp::lsp_types::{
1818
};
1919
use tower_lsp::{Client, LanguageServer, jsonrpc::Result};
2020

21+
/// LSP command identifiers.
22+
mod commands {
23+
/// Command to update a dependency version.
24+
pub const UPDATE_VERSION: &str = "deps-lsp.updateVersion";
25+
}
26+
2127
pub struct Backend {
2228
client: Client,
2329
state: Arc<ServerState>,
@@ -191,7 +197,7 @@ impl Backend {
191197
..Default::default()
192198
})),
193199
execute_command_provider: Some(ExecuteCommandOptions {
194-
commands: vec!["deps-lsp.updateVersion".into()],
200+
commands: vec![commands::UPDATE_VERSION.into()],
195201
..Default::default()
196202
}),
197203
..Default::default()
@@ -341,7 +347,7 @@ impl LanguageServer for Backend {
341347
) -> Result<Option<serde_json::Value>> {
342348
tracing::info!("execute_command: {:?}", params.command);
343349

344-
if params.command == "deps-lsp.updateVersion"
350+
if params.command == commands::UPDATE_VERSION
345351
&& let Some(args) = params.arguments.first()
346352
&& let Ok(update_args) = serde_json::from_value::<UpdateVersionArgs>(args.clone())
347353
{

0 commit comments

Comments
 (0)