Skip to content

Commit

Permalink
feat: cargo update command on dependency workspace key and value
Browse files Browse the repository at this point in the history
  • Loading branch information
washanhanzi committed Dec 1, 2024
1 parent 2014a13 commit 91a5dec
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
- hover on git dependency will show the git reference and commit
- hover on `features` will show available features, hover on a feature name
will show its values
- code action on version
- `cargo update` command on version's code action
- code action on dependency's `version`
- `cargo update` code action on dependency's `version` and `workspace`
- goto definition on workspace dependency

# Config
Expand Down
6 changes: 2 additions & 4 deletions src/controller/appraiser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,13 @@ impl Appraiser {
let Some(doc) = state.document(&uri) else {
continue;
};
let Some(node) = doc.precise_match_entry(range.start) else {
let Some(node) = doc.precise_match(range.start) else {
continue;
};
let Some(id) = node.row_id() else {
continue;
};
let Some(dep) = doc.dependency(&id) else {
continue;
};
let dep = doc.dependency(&id);
let Some(action) = code_action(uri, node, dep) else {
continue;
};
Expand Down
51 changes: 39 additions & 12 deletions src/controller/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ use tower_lsp::lsp_types::{

use crate::{
decoration::{version_decoration, VersionDecorationKind},
entity::{strip_quotes, Dependency, DependencyEntryKind, EntryKind, NodeKind, TomlNode, CARGO},
entity::{
strip_quotes, Dependency, DependencyEntryKind, DependencyKeyKind, EntryKind, KeyKind,
NodeKind, TomlNode, CARGO,
},
};

pub fn code_action(uri: Uri, node: TomlNode, dep: &Dependency) -> Option<CodeActionResponse> {
if let NodeKind::Entry(EntryKind::Dependency(id, key)) = &node.kind {
code_action_dependency(uri, id, key, &node, dep)
} else {
None
}
pub fn code_action(
uri: Uri,
node: TomlNode,
dep: Option<&Dependency>,
) -> Option<CodeActionResponse> {
//only support dependency code action fro now
let dep = dep?;
code_action_dependency(uri, &node, dep)
}

pub fn code_action_dependency(
uri: Uri,
id: &str,
key: &DependencyEntryKind,
node: &TomlNode,
dep: &Dependency,
) -> Option<CodeActionResponse> {
match key {
DependencyEntryKind::SimpleDependency | DependencyEntryKind::TableDependencyVersion => {
match node.kind {
NodeKind::Entry(EntryKind::Dependency(_, DependencyEntryKind::SimpleDependency))
| NodeKind::Entry(EntryKind::Dependency(_, DependencyEntryKind::TableDependencyVersion)) => {
let version = version_decoration(dep);
let mut actions = VersionCodeAction::new(uri, node);
actions.check_unresolved(dep);
Expand Down Expand Up @@ -61,7 +65,6 @@ pub fn code_action_dependency(
VersionDecorationKind::NonCompatibleLatest => {
let v = version.latest.as_ref()?;
actions.add_quickfix(v);
actions.add_update_command(dep.package_name());
}
VersionDecorationKind::Yanked => {
if let Some(v) = version.latest.as_ref() {
Expand All @@ -85,6 +88,30 @@ pub fn code_action_dependency(

Some(actions.take())
}
NodeKind::Key(KeyKind::Dependency(_, DependencyKeyKind::Workspace))
| NodeKind::Entry(EntryKind::Dependency(
_,
DependencyEntryKind::TableDependencyWorkspace,
)) => {
let version = version_decoration(dep);
let mut actions = VersionCodeAction::new(uri, node);
match version.kind {
VersionDecorationKind::MixedUpgradeable => {
actions.add_update_command(dep.package_name());
}
VersionDecorationKind::CompatibleLatest => {
actions.add_update_command(dep.package_name());
}
VersionDecorationKind::Yanked => {
actions.add_update_command(dep.package_name());
}
VersionDecorationKind::Git => {
actions.add_update_command(dep.package_name());
}
_ => return None,
};
Some(actions.take())
}
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controller/gd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tower_lsp::lsp_types::{GotoDefinitionResponse, Location, Position};
use tower_lsp::lsp_types::{GotoDefinitionResponse, Location};

use crate::{
entity::{DependencyEntryKind, DependencyKeyKind, EntryKind, KeyKind, NodeKind, TomlNode},
Expand Down

0 comments on commit 91a5dec

Please sign in to comment.