Skip to content

Commit a6e905f

Browse files
authored
Merge pull request #97 from mufeedvh/stateful
Code2prompt should handle stateful session
2 parents f5a63c4 + 717e35f commit a6e905f

File tree

22 files changed

+1412
-637
lines changed

22 files changed

+1412
-637
lines changed

Cargo.lock

Lines changed: 211 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ pyo3 = { version = "0.23", features = ["extension-module", "abi3-py312"] }
3333
clap = { version = "4.0", features = ["derive"] }
3434
env_logger = { version = "0.11.3" }
3535
arboard = { version = "3.4.0" }
36+
derive_builder = { version = "0.20.2" }

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<i>Report Bug</i> 🐛<br>
3030
</p>
3131

32-
3332
## Quick Install ⚡
3433

3534
CLI with cargo 🦀
@@ -327,7 +326,6 @@ print(result["prompt"])
327326

328327
[![Star History Chart](https://api.star-history.com/svg?repos=mufeedvh/code2prompt&type=Date)](https://star-history.com/#mufeedvh/code2prompt&Date)
329328

330-
331329
## License
332330

333331
Licensed under the MIT License, see <a href="https://github.com/mufeedvh/code2prompt/blob/master/LICENSE">LICENSE</a> for more information.
@@ -345,4 +343,3 @@ Ways to contribute:
345343
- Fix something and open a pull request
346344
- Help me document the code
347345
- Spread the word
348-

crates/code2prompt-core/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "code2prompt_core"
3-
version = "3.0.0"
3+
version = "3.1.0"
44
authors = [
55
"Mufeed VH <[email protected]>",
66
"Olivier D'Ancona <[email protected]>",
@@ -33,11 +33,11 @@ jwalk = { workspace = true }
3333
termtree = { workspace = true }
3434
tiktoken-rs = { workspace = true }
3535
ignore = { workspace = true }
36-
inquire = { workspace = true }
3736
regex = { workspace = true }
3837
git2 = { workspace = true }
3938
once_cell = { workspace = true }
4039
globset = { workspace = true }
40+
derive_builder = { workspace = true }
4141

4242
[lib]
4343
name = "code2prompt_core"
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//! This module defines the `Code2PromptConfig` struct and its Builder for configuring the behavior
2+
//! of code2prompt in a stateless manner. It includes all parameters needed for file traversal,
3+
//! code filtering, token counting, and more.
4+
5+
use crate::template::OutputFormat;
6+
use crate::tokenizer::TokenizerType;
7+
use crate::{sort::FileSortMethod, tokenizer::TokenFormat};
8+
use derive_builder::Builder;
9+
use std::path::PathBuf;
10+
11+
/// A stateless configuration object describing all the preferences and filters
12+
/// applied when generating a code prompt. It does not store any mutable data,
13+
/// so it can be cloned freely or shared across multiple sessions.
14+
#[derive(Debug, Clone, Default, Builder)]
15+
#[builder(setter(into), default)]
16+
pub struct Code2PromptConfig {
17+
/// Path to the root directory of the codebase.
18+
pub path: PathBuf,
19+
20+
/// List of glob-like patterns to include.
21+
#[builder(default)]
22+
pub include_patterns: Vec<String>,
23+
24+
/// List of glob-like patterns to exclude.
25+
#[builder(default)]
26+
pub exclude_patterns: Vec<String>,
27+
28+
/// If true, include patterns have priority over exclude patterns in case of conflicts.
29+
#[builder(default)]
30+
pub include_priority: bool,
31+
32+
/// If true, code lines will be numbered in the output.
33+
#[builder(default)]
34+
pub line_numbers: bool,
35+
36+
/// If true, paths in the output will be relative instead of absolute.
37+
#[builder(default)]
38+
pub relative_paths: bool,
39+
40+
/// If true, code2prompt will generate a full directory tree, ignoring include/exclude rules.
41+
#[builder(default)]
42+
pub full_directory_tree: bool,
43+
44+
/// If true, code blocks will not be wrapped in Markdown fences (```).
45+
#[builder(default)]
46+
pub no_codeblock: bool,
47+
48+
/// If true, symbolic links will be followed during traversal.
49+
#[builder(default)]
50+
pub follow_symlinks: bool,
51+
52+
/// If true, hidden files and directories will be included.
53+
#[builder(default)]
54+
pub hidden: bool,
55+
56+
/// If true, .gitignore rules will be ignored.
57+
#[builder(default)]
58+
pub no_ignore: bool,
59+
60+
/// Defines the sorting method for files.
61+
#[builder(default)]
62+
pub sort_method: Option<FileSortMethod>,
63+
64+
/// Determines the output format of the final prompt.
65+
#[builder(default)]
66+
pub output_format: OutputFormat,
67+
68+
/// An optional custom Handlebars template string.
69+
#[builder(default)]
70+
pub custom_template: Option<String>,
71+
72+
/// The tokenizer encoding to use for counting tokens.
73+
#[builder(default)]
74+
pub encoding: TokenizerType,
75+
76+
/// The counting format to use for token counting.
77+
#[builder(default)]
78+
pub token_format: TokenFormat,
79+
80+
/// If true, the git diff between HEAD and index will be included.
81+
#[builder(default)]
82+
pub diff_enabled: bool,
83+
84+
/// If set, contains two branch names for which code2prompt will generate a git diff.
85+
#[builder(default)]
86+
pub diff_branches: Option<(String, String)>,
87+
88+
/// If set, contains two branch names for which code2prompt will retrieve the git log.
89+
#[builder(default)]
90+
pub log_branches: Option<(String, String)>,
91+
92+
/// The name of the template used.
93+
#[builder(default)]
94+
pub template_name: String,
95+
96+
/// The template string itself.
97+
#[builder(default)]
98+
pub template_str: String,
99+
}
100+
101+
impl Code2PromptConfig {
102+
pub fn builder() -> Code2PromptConfigBuilder {
103+
Code2PromptConfigBuilder::default()
104+
}
105+
}

crates/code2prompt-core/src/filter.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::path::Path;
1818
/// # Returns
1919
///
2020
/// * A `globset::GlobSet` containing all valid glob patterns from the input.
21-
fn build_globset(patterns: &[String]) -> GlobSet {
21+
pub fn build_globset(patterns: &[String]) -> GlobSet {
2222
let mut builder = GlobSetBuilder::new();
2323

2424
for pattern in patterns {
@@ -63,14 +63,10 @@ fn build_globset(patterns: &[String]) -> GlobSet {
6363
/// * `bool` - Returns `true` if the file should be included; otherwise, returns `false`.
6464
pub fn should_include_file(
6565
path: &Path,
66-
include_patterns: &[String],
67-
exclude_patterns: &[String],
66+
include_globset: &GlobSet,
67+
exclude_globset: &GlobSet,
6868
include_priority: bool,
6969
) -> bool {
70-
// ~~~ Initialization ~~~
71-
let include_globset = build_globset(include_patterns);
72-
let exclude_globset = build_globset(exclude_patterns);
73-
7470
// ~~~ Matching ~~~
7571
let included = include_globset.is_match(path);
7672
let excluded = exclude_globset.is_match(path);
@@ -80,7 +76,7 @@ pub fn should_include_file(
8076
(true, true) => include_priority, // If both include and exclude patterns match, use the include_priority flag
8177
(true, false) => true, // If the path is included and not excluded, include it
8278
(false, true) => false, // If the path is excluded, exclude it
83-
(false, false) => include_patterns.is_empty(), // If no include patterns are provided, include everything
79+
(false, false) => include_globset.is_empty(), // If no include patterns are provided, include everything
8480
};
8581

8682
debug!(

crates/code2prompt-core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//! Core library for code2prompt.
2+
pub mod configuration;
13
pub mod filter;
24
pub mod git;
35
pub mod path;
6+
pub mod session;
47
pub mod sort;
58
pub mod template;
69
pub mod tokenizer;

0 commit comments

Comments
 (0)