Skip to content

Commit

Permalink
feat: show git commit hash
Browse files Browse the repository at this point in the history
  • Loading branch information
washanhanzi committed Nov 14, 2024
1 parent fa751bd commit 8eae59b
Show file tree
Hide file tree
Showing 8 changed files with 18,637 additions and 40 deletions.
18,572 changes: 18,540 additions & 32 deletions editor/code/dist/extension.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion editor/code/dist/extension.js.map

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/controller/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub struct CargoResolveOutput {

#[tracing::instrument(name = "cargo_resolve", level = "trace")]
pub async fn cargo_resolve(ctx: &Ctx) -> Result<CargoResolveOutput, CargoError> {
info!("start resolve {}", ctx.rev);
let path = Path::new(ctx.uri.path().as_str());
let gctx = cargo::util::context::GlobalContext::default().map_err(CargoError::resolve_error)?;
let workspace =
Expand Down Expand Up @@ -114,7 +113,6 @@ pub async fn cargo_resolve(ctx: &Ctx) -> Result<CargoResolveOutput, CargoError>
);
}
}
info!("finished resolve inside {}", ctx.rev);
Ok(CargoResolveOutput {
ctx: ctx.clone(),
dependencies: res,
Expand Down
22 changes: 20 additions & 2 deletions src/controller/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::HashMap;
use tower_lsp::lsp_types::{Hover, HoverContents, MarkedString};

use crate::entity::{
Dependency, DependencyEntryKind, DependencyKeyKind, EntryKind, KeyKind, NodeKind, TomlNode,
WorkspaceKeyKind,
commit_str, git_ref_str, Dependency, DependencyEntryKind, DependencyKeyKind, EntryKind,
KeyKind, NodeKind, TomlNode, WorkspaceKeyKind,
};

pub fn hover(
Expand Down Expand Up @@ -71,6 +71,24 @@ pub fn hover(
range: Some(node.range),
})
}
NodeKind::Entry(EntryKind::Dependency(_, DependencyEntryKind::TableDependencyGit)) => {
let source_id = dep?.resolved.as_ref()?.package_id().source_id();
let git_ref = git_ref_str(&source_id);
let commit = commit_str(&source_id);
//make a new string of markdown list "- <git_ref>\n - <commit>\n"
//if git_ref is some and commit is some
let mut s = String::new();
if let Some(git_ref) = git_ref {
s.push_str(&format!("- {}\n", git_ref));
}
if let Some(commit) = commit {
s.push_str(&format!("- {}\n", commit));
}
Some(Hover {
contents: HoverContents::Scalar(MarkedString::String(s)),
range: Some(node.range),
})
}
_ => None,
}
}
37 changes: 35 additions & 2 deletions src/decoration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use tower_lsp::{
lsp_types::{InlayHint, Range, Uri},
Client,
};
use tracing::info;

use crate::entity::Dependency;
use crate::entity::{commit_str_short, git_ref_str, Dependency};

pub mod inlay_hint;

Expand Down Expand Up @@ -84,11 +85,25 @@ pub struct DecorationPayload {
installed: String,
latest_matched: String,
latest: String,
//(ref,commit)
git: Option<(String, String)>,
}

pub fn decoration_payload(dep: &Dependency) -> DecorationPayload {
let mut git = None;
let installed = match dep.resolved.as_ref() {
Some(resolved) => resolved.version().to_string(),
Some(resolved) => {
if resolved.package_id().source_id().is_git() {
git = Some((
git_ref_str(&resolved.package_id().source_id()).unwrap_or_default(),
commit_str_short(&resolved.package_id().source_id())
.map_or(String::new(), |c| c.to_string()),
));
String::new()
} else {
resolved.version().to_string()
}
}
None => "".to_string(),
};
let latest_matched = match dep.latest_matched_summary.as_ref() {
Expand All @@ -103,12 +118,21 @@ pub fn decoration_payload(dep: &Dependency) -> DecorationPayload {
installed,
latest_matched,
latest,
git,
}
}

pub fn formatted_string(dep: &Dependency, formatter: &DecorationFormatter) -> Option<String> {
let version = version_decoration(dep);
let payload = decoration_payload(dep);
if let Some((r, commit)) = payload.git {
return Some(
formatter
.git
.replace("{{ref}}", &r)
.replace("{{commit}}", &commit),
);
}
match version {
VersionDecoration::Latest => Some(
formatter
Expand Down Expand Up @@ -211,6 +235,7 @@ pub fn version_decoration(dep: &Dependency) -> VersionDecoration {
/// - installed: the installed version
/// - latest_matched: the latest compatible version
/// - latest: the latest version, the latest version may or may not be compatilbe with the version requirement
/// - git: if the dependency source is git
///
/// the formatter has 7 fields:
/// latest: the dependency has the latest version installed
Expand All @@ -221,6 +246,7 @@ pub fn version_decoration(dep: &Dependency) -> VersionDecoration {
/// compatible_latest: the installed version can update to latest version
/// noncompatible_latest: the installed version can't upate to latest version
/// yanked: the installed version is yanked
/// git: support {{ref}}, {{commit}}
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DecorationFormatter {
Expand All @@ -240,6 +266,8 @@ pub struct DecorationFormatter {
pub noncompatible_latest: String,
#[serde(default = "default_yanked")]
pub yanked: String,
#[serde(default = "default_git")]
pub git: String,
}

impl Default for DecorationFormatter {
Expand All @@ -253,6 +281,7 @@ impl Default for DecorationFormatter {
waiting: default_waiting(),
mixed_upgradeable: default_mixed_upgradeable(),
yanked: default_yanked(),
git: default_git(),
}
}
}
Expand Down Expand Up @@ -288,3 +317,7 @@ fn default_local() -> String {
fn default_yanked() -> String {
"❌ yanked {{installed}}, {{latest_matched}}".to_string()
}

fn default_git() -> String {
"🐙 {{commit}}".to_string()
}
1 change: 1 addition & 0 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use entry::*;
pub use key::*;
pub use manifest::*;
pub use node::*;
pub use package::*;
pub use table::*;
pub use toml_error::*;
pub use tree::*;
Expand Down
39 changes: 39 additions & 0 deletions src/entity/package.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use cargo::core::SourceId;

use super::Value;

//Package is a semantic representation of cargo.toml's package table
Expand All @@ -15,3 +17,40 @@ pub struct Package {
readme: Option<Value<String>>,
workspace: Option<Value<String>>,
}

pub fn git_ref_str(source_id: &SourceId) -> Option<String> {
if source_id.is_git() {
let Some(r) = source_id.git_reference() else {
return None;
};
match r.pretty_ref(false) {
Some(r) => return Some(r.to_string()),
None => return None,
};
}
None
}

pub fn commit_str(source_id: &SourceId) -> Option<&str> {
if source_id.is_git() {
let Some(c) = source_id.precise_git_fragment() else {
return None;
};
return Some(c);
}
None
}

pub fn commit_str_short(source_id: &SourceId) -> Option<&str> {
// Get the full commit hash
let Some(commit) = commit_str(source_id) else {
return None;
};

// Handle case where commit hash is shorter than 7 chars
if commit.len() < 7 {
Some(commit)
} else {
Some(&commit[..7])
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::sync::{mpsc::Sender, oneshot};
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};
use tracing::{error, info, warn};
use tracing::{error, info};

mod config;
mod controller;
Expand Down

0 comments on commit 8eae59b

Please sign in to comment.