Skip to content

Commit

Permalink
aa
Browse files Browse the repository at this point in the history
  • Loading branch information
bestgopher committed Aug 28, 2024
1 parent 278f833 commit fc272e6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
.DS_Store
.idea/
*.wasm
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
[package]
name = "wakatime-zed"
version = "0.1.0"
edition = "2021"

[lib]
path = "wakatime/src/lib.rs"

[workspace]
members = ["wakatime-ls", "wakatime"]
default-members = ["wakatime-ls"]
default-members = ["wakatime"]

[profile.dist]
inherits = "release"
Expand Down
1 change: 1 addition & 0 deletions wakatime-ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ chrono = "0.4.38"
clap = "4.5.16"
tokio = { version = "1.39.3", features = ["full"] }
tower-lsp = "0.20.0"
url = "2.5.2"
42 changes: 21 additions & 21 deletions wakatime-ls/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{any::Any, sync::Arc, time::Duration};
use std::sync::Arc;

use chrono::{DateTime, Local, TimeDelta};
use clap::{Arg, Command};
Expand All @@ -23,7 +23,7 @@ struct CurrentFile {
struct WakatimeLanguageServer {
client: Client,
wakatime_path: String,
api_key: String,
api_key: Option<String>,
current_file: Mutex<CurrentFile>,
}

Expand All @@ -36,13 +36,6 @@ impl WakatimeLanguageServer {
let mut current_file = self.current_file.lock().await;
let now = Local::now();

self.client
.log_message(
MessageType::ERROR,
format!("current_file: {current_file:?}, event: {event:?}"),
)
.await;

if event.uri == current_file.uri
&& now - current_file.timestamp < INTERVAL
&& event.is_write
Expand All @@ -53,23 +46,31 @@ impl WakatimeLanguageServer {
let mut command = TokioCommand::new(self.wakatime_path.as_str());

command
.arg("--log-file")
.arg("/Users/xsky/.wakatime/wakatime.log")
.arg("--verbose")
.arg("--time")
.arg((now.timestamp() as f64).to_string())
.arg("--key")
.arg(self.api_key.as_str())
.arg("--write")
.arg(event.is_write.to_string())
.arg("--entity")
.arg(event.uri.as_str());

if let Some(ref key) = self.api_key {
command.arg("--key").arg(key);
}

if let Some(ref language) = event.language {
command.arg("--language").arg(language);
}

self.client
.log_message(MessageType::LOG, format!("command: {:?}", command.as_std()))
.await;

match command.output().await {
Err(e) => {
self.client
.log_message(
MessageType::ERROR,
MessageType::LOG,
format!("Wakatime language server send msg faild: {e:?}"),
)
.await
Expand Down Expand Up @@ -119,7 +120,7 @@ impl LanguageServer for WakatimeLanguageServer {

async fn did_open(&self, params: DidOpenTextDocumentParams) {
let event = Event {
uri: params.text_document.uri.to_string(),
uri: params.text_document.uri[url::Position::BeforeUsername..].to_string(),
is_write: false,
lineno: None,
language: Some(params.text_document.language_id.clone()),
Expand All @@ -131,7 +132,7 @@ impl LanguageServer for WakatimeLanguageServer {

async fn did_change(&self, params: DidChangeTextDocumentParams) {
let event = Event {
uri: params.text_document.uri.to_string(),
uri: params.text_document.uri[url::Position::BeforeUsername..].to_string(),
is_write: false,
lineno: None, // todo
language: None,
Expand All @@ -143,7 +144,7 @@ impl LanguageServer for WakatimeLanguageServer {

async fn did_save(&self, params: DidSaveTextDocumentParams) {
let event = Event {
uri: params.text_document.uri.to_string(),
uri: params.text_document.uri[url::Position::BeforeUsername..].to_string(),
is_write: true,
lineno: None,
language: None,
Expand Down Expand Up @@ -171,13 +172,12 @@ async fn main() {
Arg::new("api-key")
.short('k')
.long("api-key")
.help("the api key of wakatime-cli")
.required(true),
.help("the api key of wakatime-cli"),
)
.get_matches();

let wakatime_cli = matches.get_one::<String>("wakatime-cli").unwrap();
let api_key = matches.get_one::<String>("api-key").unwrap();
let api_key = matches.get_one::<String>("api-key").map(|x| x.to_string());

let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
Expand All @@ -186,7 +186,7 @@ async fn main() {
Arc::new(WakatimeLanguageServer {
client,
wakatime_path: wakatime_cli.to_string(),
api_key: api_key.to_string(),
api_key,
current_file: Mutex::new(CurrentFile {
uri: String::new(),
timestamp: Local::now(),
Expand Down
1 change: 1 addition & 0 deletions wakatime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[lib]
name = "wakatime_zed"
crate-type = ["cdylib"]

[dependencies]
Expand Down
45 changes: 35 additions & 10 deletions wakatime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fs;
use std::{
env, fs,
path::{Path, PathBuf},
};

use zed_extension_api::{
self as zed, settings::LspSettings, Command, LanguageServerId, Result, Worktree,
Expand All @@ -25,25 +28,33 @@ impl WakatimeExtension {
)?;

let (platform, arch) = zed::current_platform();
let target_triple = format!(
"{binary}-{arch}-{os}",
arch = match arch {
let (arch, os) = {
let arch = match arch {
zed::Architecture::Aarch64 if binary == "wakatime-cli" => "arm64",
zed::Architecture::Aarch64 if binary == "wakatime-ls" => "aarch64",
zed::Architecture::X8664 if binary == "wakatime-cli" => "amd64",
zed::Architecture::X8664 if binary == "wakatime-ls" => "x86_64",
_ => return Err(format!("unsupported architecture: {arch:?}")),
},
os = match platform {
};

let os = match platform {
zed::Os::Mac if binary == "wakatime-cli" => "darwin",
zed::Os::Mac if binary == "wakatime-ls" => "apple-darwin",
zed::Os::Linux if binary == "wakatime-cli" => "linux",
zed::Os::Linux if binary == "wakatime-ls" => "unknown-linux-gnu",
zed::Os::Windows if binary == "wakatime-cli" => "windows",
zed::Os::Windows if binary == "wakatime-ls" => "pc-windows-msvc",
_ => return Err(format!("unsupported platform")),
},
);
};

(arch, os)
};

let target_triple = if binary == "wakatime-cli" {
format!("{binary}-{os}-{arch}")
} else {
format!("{binary}-{arch}-{os}")
};

let asset_name = format!("{target_triple}.zip");
let asset = release
Expand All @@ -53,7 +64,11 @@ impl WakatimeExtension {
.ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;

let version_dir = format!("{binary}-{}", release.version);
let binary_path = format!("{version_dir}/{target_triple}/{binary}");
let binary_path = if binary == "wakatime-cli" {
format!("{version_dir}/{target_triple}")
} else {
format!("{version_dir}/{target_triple}/{binary}")
};

if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
zed::set_language_server_installation_status(
Expand All @@ -70,6 +85,7 @@ impl WakatimeExtension {

let entries = fs::read_dir(".")
.map_err(|err| format!("failed to list working directory {err}"))?;

for entry in entries {
let entry = entry.map_err(|err| format!("failed to load directory entry {err}"))?;
if entry.file_name().to_str() != Some(&version_dir) {
Expand Down Expand Up @@ -153,11 +169,20 @@ impl zed::Extension for WakatimeExtension {
) -> Result<Command> {
let wakatime_cli_binary_path =
self.wakatime_cli_binary_path(language_server_id, worktree)?;

let ls_binary_path = self.language_server_binary_path(language_server_id, worktree)?;

let setting = LspSettings::for_worktree("wakatime", worktree)?;

let mut args = vec!["--wakatime-cli".to_string(), wakatime_cli_binary_path];
let mut args = vec!["--wakatime-cli".to_string(), {
use std::env;
let current = env::current_dir().unwrap();
current
.join(wakatime_cli_binary_path)
.to_str()
.unwrap()
.to_string()
}];

if let Some(ref value) = setting.settings {
if let Some(y) = value.get("api-key") {
Expand Down

0 comments on commit fc272e6

Please sign in to comment.