use std::path::{Path, PathBuf};
use crate::core::rules_canonical::{self as rc, Wrapper};
pub(super) fn cursor_wrapper_for_mdc(mdc_path: &Path) -> Wrapper {
let covered = mdc_path
.ancestors()
.find(|path| {
path.file_name()
.is_some_and(|name| name == std::ffi::OsStr::new(".cursor"))
})
.is_some_and(|cursor_dir| {
crate::core::rules_channel::cursor_hooks_json_covers(&cursor_dir.join("hooks.json"))
});
if covered {
Wrapper::HookCovered
} else {
Wrapper::Dedicated
}
}
pub fn cursor_mdc_document(body: &str) -> String {
format!(
"---\n\
description: \"lean-ctx: context compression layer — \
tool guidance in rule body.\"\n\
globs: **/*\n\
alwaysApply: true\n\
---\n\n\
{body}"
)
}
pub(super) const ANTIGRAVITY_RULE_FRONTMATTER: &str = "---\n\
trigger: always_on\n\
description: \"lean-ctx: context compression layer, tool guidance in rule body.\"\n\
---\n\n";
const PI_TOOL_NAMES: &str = "Pi (pi-lean-ctx), for each tool it lists: \
Grep -> ctx_grep(pattern, path), Glob/find -> ctx_find(pattern, path), \
ls -> ctx_ls(path); ctx_search, ctx_glob and ctx_tree also only when listed.";
pub(super) fn with_host_tool_names(target_name: &str, block: String) -> String {
if target_name != "Pi Coding Agent" {
return block;
}
match block.rfind(rc::END_MARK) {
Some(end) => format!(
"{}\n{PI_TOOL_NAMES}\n{}",
block[..end].trim_end(),
&block[end..]
),
None => block,
}
}
pub(super) fn starts_with_frontmatter(content: &str) -> bool {
let content = content.strip_prefix('\u{feff}').unwrap_or(content);
content.starts_with("---\n") || content.starts_with("---\r\n")
}
pub(super) fn with_antigravity_frontmatter(content: String) -> String {
if starts_with_frontmatter(&content) {
content
} else {
format!("{ANTIGRAVITY_RULE_FRONTMATTER}{content}")
}
}
pub fn opencode_dedicated_rules_path(home: &std::path::Path) -> PathBuf {
home.join(".config/opencode/rules/lean-ctx.md")
}
pub fn antigravity_dedicated_rules_path(home: &std::path::Path) -> PathBuf {
home.join(".gemini/config/rules/lean-ctx.md")
}
pub fn gemini_dedicated_rules_path(home: &std::path::Path) -> PathBuf {
home.join(".gemini").join(GEMINI_DEDICATED_CONTEXT_FILENAME)
}
pub const GEMINI_DEDICATED_CONTEXT_FILENAME: &str = "LEANCTX.md";