lean_ctx/rules_inject/
content.rs1use std::path::{Path, PathBuf};
8
9use crate::core::rules_canonical::{self as rc, Wrapper};
10
11pub(super) fn cursor_wrapper_for_mdc(mdc_path: &Path) -> Wrapper {
17 let covered = mdc_path
18 .ancestors()
19 .find(|path| {
20 path.file_name()
21 .is_some_and(|name| name == std::ffi::OsStr::new(".cursor"))
22 })
23 .is_some_and(|cursor_dir| {
24 crate::core::rules_channel::cursor_hooks_json_covers(&cursor_dir.join("hooks.json"))
25 });
26 if covered {
27 Wrapper::HookCovered
28 } else {
29 Wrapper::Dedicated
30 }
31}
32
33pub fn cursor_mdc_document(body: &str) -> String {
37 format!(
38 "---\n\
39 description: \"lean-ctx: context compression layer — \
40 tool guidance in rule body.\"\n\
41 globs: **/*\n\
42 alwaysApply: true\n\
43 ---\n\n\
44 {body}"
45 )
46}
47
48pub(super) const ANTIGRAVITY_RULE_FRONTMATTER: &str = "---\n\
51 trigger: always_on\n\
52 description: \"lean-ctx: context compression layer, tool guidance in rule body.\"\n\
53 ---\n\n";
54
55const PI_TOOL_NAMES: &str = "Pi (pi-lean-ctx), for each tool it lists: \
61Grep -> ctx_grep(pattern, path), Glob/find -> ctx_find(pattern, path), \
62ls -> ctx_ls(path); ctx_search, ctx_glob and ctx_tree also only when listed.";
63
64pub(super) fn with_host_tool_names(target_name: &str, block: String) -> String {
68 if target_name != "Pi Coding Agent" {
69 return block;
70 }
71 match block.rfind(rc::END_MARK) {
72 Some(end) => format!(
73 "{}\n{PI_TOOL_NAMES}\n{}",
74 block[..end].trim_end(),
75 &block[end..]
76 ),
77 None => block,
78 }
79}
80
81pub(super) fn starts_with_frontmatter(content: &str) -> bool {
84 let content = content.strip_prefix('\u{feff}').unwrap_or(content);
85 content.starts_with("---\n") || content.starts_with("---\r\n")
86}
87
88pub(super) fn with_antigravity_frontmatter(content: String) -> String {
91 if starts_with_frontmatter(&content) {
92 content
93 } else {
94 format!("{ANTIGRAVITY_RULE_FRONTMATTER}{content}")
95 }
96}
97
98pub fn opencode_dedicated_rules_path(home: &std::path::Path) -> PathBuf {
99 home.join(".config/opencode/rules/lean-ctx.md")
100}
101
102pub fn antigravity_dedicated_rules_path(home: &std::path::Path) -> PathBuf {
103 home.join(".gemini/config/rules/lean-ctx.md")
104}
105
106pub fn gemini_dedicated_rules_path(home: &std::path::Path) -> PathBuf {
107 home.join(".gemini").join(GEMINI_DEDICATED_CONTEXT_FILENAME)
108}
109
110pub const GEMINI_DEDICATED_CONTEXT_FILENAME: &str = "LEANCTX.md";