Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lsp workspace commands to open daily notes #85

Merged
merged 7 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::File;
use std::path::Path;

use crate::config::Settings;
use crate::vault::{self, Vault};
use chrono::offset::Local;
use chrono::NaiveDateTime;
use fuzzydate::parse;
Expand All @@ -14,25 +15,29 @@ fn file_to_datetime(filename: &str, format: &str) -> Option<NaiveDateTime> {
todo!()
}

fn datetime_to_file(datetime: NaiveDateTime, settings: &Settings) -> Option<Url> {
let filename = datetime.format(&settings.dailynote).to_string();
let path = Path::new(&filename);
fn datetime_to_file(datetime: NaiveDateTime, dailynote_format: &str, root_dir: &Path) -> Option<Url> {
let filename = datetime.format(dailynote_format).to_string();
let path = root_dir.join(&filename);

println!("path: {:?}", path);

Url::from_file_path(path.with_extension("md")).ok()
}

pub async fn jump(
client: &tower_lsp::Client,
root_dir: &Path,
settings: &Settings,
jump_to: Option<&str>,
) -> Result<Option<Value>> {
// if jump_to is None, use the current time.

let daily_note_format = &settings.dailynote;
let note_file = match jump_to {
Some(jmp_str) => parse(jmp_str)
.ok()
.and_then(|dt| datetime_to_file(dt, &settings)),
None => datetime_to_file(Local::now().naive_local(), &settings),
.and_then(|dt| datetime_to_file(dt, &daily_note_format, root_dir)),
None => datetime_to_file(Local::now().naive_local(), &daily_note_format, root_dir),
};

if let Some(uri) = note_file {
Expand All @@ -41,7 +46,9 @@ pub async fn jump(
// TODO: log failure to create file
let _ = uri.to_file_path().map(|path| {
path.parent().map(|parent| std::fs::create_dir_all(parent));
File::create_new(path.as_path().to_owned())
if !path.exists() {
Feel-ix-343 marked this conversation as resolved.
Show resolved Hide resolved
let _ = File::create(path.as_path().to_owned());
}
});

client
Expand All @@ -61,7 +68,7 @@ pub async fn jump(
)
.await;
Err(Error::invalid_params(
"Could not parse journal format as a valid uri.".to_string(),
format!("Could not parse journal format ({jump_to:?}) as a valid uri: {:?}.", jump_to.map(parse)),
))
}
}
Expand All @@ -73,3 +80,25 @@ pub fn jump_relative(
) -> Result<Option<Value>> {
todo!("pending PR in fuzzydate to specify base time")
}

// tests
#[cfg(test)]
mod tests {
use fuzzydate::parse;

use crate::config::Settings;

use super::datetime_to_file;


#[test]
fn test_string_to_file() {

let input = "today";

let parsed_datetime = parse(input).unwrap();

let file = datetime_to_file(parsed_datetime, "%Y-%m-%d", &std::fs::canonicalize("./").unwrap()).unwrap();

}
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ impl LanguageServer for Backend {
let settings = self
.bind_settings(|settings| Ok(settings.to_owned()))
.await?;
commands::jump(&self.client, &settings, jump_to).await
let root_dir = self.bind_vault(|vault| Ok(vault.root_dir().to_owned())).await?;
commands::jump(&self.client, &root_dir, &settings, jump_to).await
}
_ => Ok(None),
}
Expand Down
Loading