Skip to content

feat(cargo): add auto-completion for dependency feature names in Cargo.toml#83

Merged
bug-ops merged 2 commits intomainfrom
cargo-features-completion
Mar 28, 2026
Merged

feat(cargo): add auto-completion for dependency feature names in Cargo.toml#83
bug-ops merged 2 commits intomainfrom
cargo-features-completion

Conversation

@bug-ops
Copy link
Copy Markdown
Owner

@bug-ops bug-ops commented Mar 28, 2026

Summary

  • Implements LSP completion for feature names inside features = [...] arrays in Cargo.toml (closes Add auto-completion for dependency features in Cargo.toml #82)
  • Adds features_range() to the Dependency trait so feature position is accessible generically; other ecosystems default to None and are unaffected
  • Adds extract_feature_prefix helper that extracts the partial feature string typed at cursor position, handling both inline and multi-line arrays
  • The existing complete_features and build_feature_completion infrastructure in deps-cargo/deps-core was already in place — this PR wires up the missing context detection step

Test plan

  • Unit tests for extract_feature_prefix (basic, empty, multi-line, no-quote cases)
  • Unit tests for detect_completion_context with Feature context (inline, empty prefix, second item, outside range)
  • All 1255 existing tests pass (cargo nextest run --workspace --all-features --lib --bins)
  • cargo clippy --all-targets --all-features --workspace -- -D warnings clean
  • cargo +nightly fmt --check clean
  • Manual test: open a Cargo.toml, add serde = { version = "1", features = [" and trigger completion — should show derive, std, etc.

…o.toml

Implement LSP completion for feature names inside `features = [...]` arrays
in Cargo.toml dependency entries. When the cursor is positioned inside a
features array, the server now detects this context and returns feature names
fetched from the crates.io sparse index.

Changes:
- Add `features_range() -> Option<Range>` to the `Dependency` trait in deps-core
  with a default `None` implementation so other ecosystems are unaffected
- Implement `features_range()` in `ParsedDependency` (deps-cargo) using the
  range already tracked by the TOML parser
- Replace the TODO in `detect_completion_context` (deps-core) with feature
  detection: if cursor is within `features_range`, return
  `CompletionContext::Feature` with the partial prefix extracted via the new
  `extract_feature_prefix` helper
- Add `extract_feature_prefix` that scans backwards from the cursor to the
  last unmatched `"` on the current line, handling both inline and multi-line
  feature arrays
- Add unit tests for `detect_completion_context`, `extract_feature_prefix`,
  and the `features_range` trait integration

Closes #82
@github-actions github-actions Bot added documentation Improvements or additions to documentation rust Rust code changes needs-review Needs review size: L 200-500 lines changed labels Mar 28, 2026
@bug-ops bug-ops requested a review from Copilot March 28, 2026 20:27
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Cargo-specific LSP completion for dependency feature names by exposing the features = [...] value range through the generic Dependency trait and using it during completion-context detection.

Changes:

  • Extend deps-core’s Dependency trait with features_range() (defaulting to None for non-supporting ecosystems).
  • Implement feature-context detection in detect_completion_context, including a new extract_feature_prefix helper and unit tests.
  • Wire Cargo’s parsed dependency type to expose features_range, and document the feature in the changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
crates/deps-core/src/ecosystem.rs Adds features_range() to the Dependency trait with a default None implementation.
crates/deps-core/src/completion.rs Detects feature completion context, adds extract_feature_prefix, and adds tests covering feature-context detection/prefix extraction.
crates/deps-cargo/src/types.rs Implements features_range() for Cargo parsed dependencies.
CHANGELOG.md Documents the new Cargo feature-name completion under Unreleased.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/deps-core/src/completion.rs
Comment thread crates/deps-core/src/completion.rs Outdated
Comment thread crates/deps-core/src/completion.rs Outdated
Comment thread crates/deps-cargo/src/types.rs Outdated
…traction

- extract_feature_prefix: count quotes after the last '[' to detect
  whether cursor is inside a string; return empty when it is not
  (prevents returning garbage from version quotes like '"1"' when
  cursor is between items, e.g. `["full", |]`)
- Mark doctest as no_run to avoid brittle hard-coded character offsets
- Fix comment: remove incorrect "unescaped" qualifier
- features_range(): use imported Range alias instead of qualified path
- Add tests: cursor between items, cursor after opening bracket
@bug-ops bug-ops merged commit 72e11ab into main Mar 28, 2026
20 checks passed
@bug-ops bug-ops deleted the cargo-features-completion branch March 28, 2026 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation needs-review Needs review rust Rust code changes size: L 200-500 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add auto-completion for dependency features in Cargo.toml

2 participants