Skip to main content

build_version_completion

Function build_version_completion 

Source
pub fn build_version_completion(
    display_item: &VersionDisplayItem,
    insert_range: Option<Range>,
    now: PublishTime,
    freshness_enabled: bool,
) -> CompletionItem
Expand description

Builds a completion item for a version string.

Creates a properly formatted LSP CompletionItem with version metadata in a simplified format matching Code Actions (Cmd+.) style.

§Arguments

  • display_item - Version display metadata with label, description, and flags
  • insert_range - Optional LSP range where the completion should replace text. If None, the completion will insert at cursor position without replacing.
  • now - Current instant, injected explicitly rather than read internally, so every item in the same completion response has its age computed against one consistent instant instead of drifting mid-request.

§Returns

A complete CompletionItem with simple index-based sorting and preselect.

§Format

  • Label: "version" or "version (latest)" for the latest version
  • Detail: "Update package_name to version"
  • Label details: a greyed-out relative age (e.g. "2 hours ago") when display_item.published_at is known and freshness_enabled is true; omitted entirely otherwise
  • Preselect: true for latest version, false otherwise
  • Sort: Index-based (00000, 00001, etc.)

§Examples

use deps_core::completion::{build_version_completion, VersionDisplayItem};
use deps_core::PackageName;
use tower_lsp_server::ls_types::Range;

let now = deps_core::PublishTime::now();

// Without range - insert at cursor
let display_item = VersionDisplayItem::new(version, &PackageName::new("serde"), 0, true);
let item = build_version_completion(&display_item, None, now, true);
assert_eq!(item.label, display_item.label);

// With range - replace existing text
let range = Range::default();
let item = build_version_completion(&display_item, Some(range), now, true);