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

Use XDG dirs per spec #2135

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
22 changes: 21 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion helix-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage = "https://helix-editor.com"
anyhow = "1"
serde = { version = "1.0", features = ["derive"] }
toml = "0.5"
etcetera = "0.3"
directories = "4"
Copy link
Member

Choose a reason for hiding this comment

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

❌ etcetera was specifically chosen because it uses XDG on macOS instead of system directories. For a command line tool it makes more sense to use XDG.

Copy link
Member

Choose a reason for hiding this comment

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

directories/dirs is also deprecated in favour of dirs-next (used by etcetera) https://www.reddit.com/r/rust/comments/pps4f0/directories_crate_version_400_broken_on_macos/

Copy link
Author

Choose a reason for hiding this comment

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

What do you mean by system directories? Going by directories' README and etcetera code, all paths used on macOS begin with $HOME/Library. Both crates behave the same way.

directories crate isn't deprecated. It has more recent release than the *-next forks or etcetera and it's the only cross-platform crate I found that bothered to incorporate XDG_STATE_HOME. The Reddit link says nothing about deprecation either, just random people recommending directories-next, one without explanation and other saying "might be good alternative". It may have been at the time but now it's *-next that's outdated.

I'm open to using whatever, but etcetera and directories-next aren't up-to-date and don't do what's required.

Copy link
Member

Choose a reason for hiding this comment

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

This is one of the defining features of etcetera: https://docs.rs/etcetera/latest/etcetera/base_strategy/fn.choose_base_strategy.html

Meanwhile it's not implemented in directories-next: xdg-rs/dirs#45

directories had a minor release last year but it's unmaintained. Most crates switched over to directories-next after original author stopped responding: https://www.reddit.com/r/rust/comments/gli4pc/directoriesnext_dirsnext_the_new_home_for/

Copy link
Author

@mmatous mmatous Apr 21, 2022

Choose a reason for hiding this comment

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

Got it, I only looked at the Apple strategy. I created a PR lunacookies/etcetera#1 to support state. Etcetera dev seems fairly active so hopefully it'll be merged soon.

tree-sitter = "0.20"
libloading = "0.7"
once_cell = "1.9"
Expand Down
15 changes: 5 additions & 10 deletions helix-loader/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const REMOTE_NAME: &str = "origin";

pub fn get_language(name: &str) -> Result<Language> {
let name = name.to_ascii_lowercase();
let mut library_path = crate::runtime_dir().join("grammars").join(&name);
let mut library_path = crate::grammar_dir().join(&name);
library_path.set_extension(DYLIB_EXTENSION);

let library = unsafe { Library::new(&library_path) }
Expand Down Expand Up @@ -142,8 +142,7 @@ fn fetch_grammar(grammar: GrammarConfiguration) -> Result<()> {
remote, revision, ..
} = grammar.source
{
let grammar_dir = crate::runtime_dir()
.join("grammars")
let grammar_dir = crate::grammar_dir()
.join("sources")
.join(&grammar.grammar_id);

Expand Down Expand Up @@ -233,8 +232,7 @@ fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
let grammar_dir = if let GrammarSource::Local { path } = &grammar.source {
PathBuf::from(&path)
} else {
crate::runtime_dir()
.join("grammars")
crate::grammar_dir()
.join("sources")
.join(&grammar.grammar_id)
};
Expand Down Expand Up @@ -280,7 +278,7 @@ fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) ->
None
}
};
let parser_lib_path = crate::runtime_dir().join("grammars");
let parser_lib_path = crate::grammar_dir();
let mut library_path = parser_lib_path.join(&grammar.grammar_id);
library_path.set_extension(DYLIB_EXTENSION);

Expand Down Expand Up @@ -385,9 +383,6 @@ fn mtime(path: &Path) -> Result<SystemTime> {
/// Gives the contents of a file from a language's `runtime/queries/<lang>`
/// directory
pub fn load_runtime_file(language: &str, filename: &str) -> Result<String, std::io::Error> {
let path = crate::RUNTIME_DIR
.join("queries")
.join(language)
.join(filename);
let path = crate::query_dir().join(language).join(filename);
std::fs::read_to_string(&path)
}
41 changes: 26 additions & 15 deletions helix-loader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
pub mod grammar;

use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};

pub static RUNTIME_DIR: once_cell::sync::Lazy<std::path::PathBuf> =
once_cell::sync::Lazy::new(runtime_dir);

fn project_dirs() -> directories::ProjectDirs {
directories::ProjectDirs::from("com", "helix-editor", "helix")
.expect("Unable to continue. User has no home")
}

pub fn runtime_dir() -> std::path::PathBuf {
if let Ok(dir) = std::env::var("HELIX_RUNTIME") {
return dir.into();
Expand All @@ -28,26 +31,22 @@ pub fn runtime_dir() -> std::path::PathBuf {
.unwrap()
}

pub fn config_dir() -> std::path::PathBuf {
// TODO: allow env var override
let strategy = choose_base_strategy().expect("Unable to find the config directory!");
let mut path = strategy.config_dir();
path.push("helix");
path
fn config_dir() -> std::path::PathBuf {
project_dirs().config_dir().to_path_buf()
}

pub fn cache_dir() -> std::path::PathBuf {
// TODO: allow env var override
let strategy = choose_base_strategy().expect("Unable to find the config directory!");
let mut path = strategy.cache_dir();
path.push("helix");
path
fn cache_dir() -> std::path::PathBuf {
project_dirs().cache_dir().to_path_buf()
}

pub fn config_file() -> std::path::PathBuf {
config_dir().join("config.toml")
}

pub fn grammar_dir() -> std::path::PathBuf {
config_dir().join("grammars")
}

pub fn lang_config_file() -> std::path::PathBuf {
config_dir().join("languages.toml")
}
Expand All @@ -56,6 +55,18 @@ pub fn log_file() -> std::path::PathBuf {
cache_dir().join("helix.log")
}

pub fn theme_dir() -> std::path::PathBuf {
config_dir().join("themes")
}

pub fn tutor_file() -> std::path::PathBuf {
config_dir().join("tutor.txt")
}

pub fn query_dir() -> std::path::PathBuf {
config_dir().join("queries")
}

/// Default bultin-in languages.toml.
pub fn default_lang_config() -> toml::Value {
toml::from_slice(include_bytes!("../../languages.toml"))
Expand All @@ -65,7 +76,7 @@ pub fn default_lang_config() -> toml::Value {
/// User configured languages.toml file, merged with the default config.
pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
let def_lang_conf = default_lang_config();
let data = std::fs::read(crate::config_dir().join("languages.toml"));
let data = std::fs::read(lang_config_file());
let user_lang_conf = match data {
Ok(raw) => {
let value = toml::from_slice(&raw)?;
Expand Down
10 changes: 5 additions & 5 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl Application {
let mut compositor = Compositor::new()?;
let size = compositor.size();

let conf_dir = helix_loader::config_dir();

let theme_loader =
std::sync::Arc::new(theme::Loader::new(&conf_dir, &helix_loader::runtime_dir()));
let theme_loader = std::sync::Arc::new(theme::Loader::new(
&helix_loader::theme_dir(),
&helix_loader::runtime_dir(),
));

let true_color = config.editor.true_color || crate::true_color();
let theme = config
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Application {
compositor.push(editor_view);

if args.load_tutor {
let path = helix_loader::runtime_dir().join("tutor.txt");
let path = helix_loader::tutor_file();
editor.open(path, Action::VerticalSplit)?;
// Unset path to prevent accidentally saving to the original tutor file.
doc_mut!(editor).set_path(None)?;
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ fn tutor(
_args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
let path = helix_loader::runtime_dir().join("tutor.txt");
let path = helix_loader::tutor_file();
cx.editor.open(path, Action::Replace)?;
// Unset path to prevent accidentally saving to the original tutor file.
doc_mut!(cx.editor).set_path(None)?;
Expand Down
7 changes: 4 additions & 3 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ FLAGS:
return Ok(0);
}

let conf_dir = helix_loader::config_dir();
if !conf_dir.exists() {
std::fs::create_dir_all(&conf_dir).ok();
if let Some(conf_dir) = helix_loader::config_file().parent() {
if !conf_dir.exists() {
std::fs::create_dir_all(&conf_dir).ok();
}
}

let config = match Config::load_default() {
Expand Down
6 changes: 2 additions & 4 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,8 @@ pub mod completers {
}

pub fn theme(_editor: &Editor, input: &str) -> Vec<Completion> {
let mut names = theme::Loader::read_names(&helix_loader::runtime_dir().join("themes"));
names.extend(theme::Loader::read_names(
&helix_loader::config_dir().join("themes"),
));
let mut names = theme::Loader::read_names(&helix_loader::theme_dir());
names.extend(theme::Loader::read_names(&helix_loader::theme_dir()));
names.push("default".into());
names.push("base16_default".into());

Expand Down
4 changes: 2 additions & 2 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl Loader {
/// Creates a new loader that can load themes from two directories.
pub fn new<P: AsRef<Path>>(user_dir: P, default_dir: P) -> Self {
Self {
user_dir: user_dir.as_ref().join("themes"),
default_dir: default_dir.as_ref().join("themes"),
user_dir: PathBuf::from(user_dir.as_ref()),
default_dir: PathBuf::from(default_dir.as_ref()),
}
}

Expand Down