Skip to content

Commit 254a0a4

Browse files
author
Olivier D'Ancona
committed
Merge branch 'main' of github.com:mufeedvh/code2prompt
2 parents db16515 + b1cb9b8 commit 254a0a4

19 files changed

Lines changed: 174 additions & 57 deletions

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ jobs:
125125

126126
- name: Create GitHub Release and upload assets
127127
if: startsWith(github.ref, 'refs/tags/')
128-
uses: softprops/action-gh-release@v2
128+
uses: softprops/action-gh-release@v3
129129
with:
130130
tag_name: ${{ github.ref }}
131131
name: Release ${{ github.ref }}

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ codegen-units = 1
1616
anyhow = "1.0.98"
1717
ansi_term = "0.12.1"
1818
arboard = { version = "3.6.0" }
19-
bracoxide = "0.1.6"
19+
bracoxide = "0.1.8"
2020
colored = "3.0.0"
2121
csv = "1.4.0"
2222
chrono = { version = "0.4", features = ["serde"] }
@@ -39,7 +39,7 @@ git2 = { version = "0.21.0", default-features = false, features = [
3939
"vendored-openssl",
4040
] }
4141
globset = "0.4.15"
42-
handlebars = "6.3.2"
42+
handlebars = "6.4.0"
4343
once_cell = "1.19.0"
4444
pyo3 = { version = "0.28.3", features = ["extension-module", "abi3-py312"] }
4545
ratatui = "0.30.1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ code2prompt .
7474
**Save to file**:
7575

7676
```sh
77-
code2prompt path/to/project --output prompt.txt
77+
code2prompt path/to/project --output-file prompt.txt
7878
```
7979

8080
## 🌐 Ecosystem

crates/code2prompt-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ section = "utility"
5454
assets = [["target/release/code2prompt_core", "/usr/bin/", "755"]]
5555

5656
[dev-dependencies]
57-
tempfile = "3.20"
57+
tempfile = "3.24"
5858
assert_cmd = "2.1.1"
5959
predicates = "3.1"
6060
env_logger = "0.11.3"

crates/code2prompt-core/src/configuration.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub struct Code2PromptConfig {
8585
/// (parallelized during file I/O). This flag only controls whether the breakdown
8686
/// is shown to users in the final output.
8787
pub token_map_enabled: bool,
88+
89+
/// If true, starts with all files deselected.
90+
pub deselected: bool,
8891
}
8992

9093
impl Code2PromptConfig {
@@ -148,6 +151,9 @@ pub struct TomlConfig {
148151

149152
/// Token map
150153
pub token_map_enabled: bool,
154+
155+
/// Initial selection state
156+
pub deselected: bool,
151157
}
152158

153159
impl TomlConfig {
@@ -208,7 +214,8 @@ impl TomlConfig {
208214

209215
builder
210216
.user_variables(self.user_variables.clone())
211-
.token_map_enabled(self.token_map_enabled);
217+
.token_map_enabled(self.token_map_enabled)
218+
.deselected(self.deselected);
212219

213220
builder.build().unwrap_or_default()
214221
}
@@ -249,6 +256,7 @@ pub fn export_config_to_toml(config: &Code2PromptConfig) -> Result<String, toml:
249256
},
250257
user_variables: config.user_variables.clone(),
251258
token_map_enabled: config.token_map_enabled,
259+
deselected: config.deselected,
252260
};
253261

254262
toml_config.to_string()

crates/code2prompt-core/src/default_template_md.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ Source Tree:
1010
{{#if code}}
1111
`{{path}}`:
1212

13+
{{#unless ../no_codeblock}}```{{extension}}
1314
{{code}}
15+
```{{else}}
16+
{{code}}
17+
{{/unless}}
1418

1519
{{/if}}
1620
{{/each}}

crates/code2prompt-core/src/default_template_xml.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
{{#each files}}
99
{{#if code}}
1010
<file path="{{path}}">
11-
{{code}}
11+
{{#unless ../no_codeblock}}```{{extension}}
12+
{{/unless}}{{code}}{{#unless ../no_codeblock}}
13+
```{{/unless}}
1214
</file>
1315
{{/if}}
1416
{{/each}}

crates/code2prompt-core/src/path.rs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn process_single_file(file_info: &FileToProcess, config: &Code2PromptConfig) ->
247247
};
248248

249249
// Wrap code block
250-
let code_block = wrap_code_block(&code, extension, config.line_numbers, config.no_codeblock);
250+
let code_block = wrap_code_block(&code, config.line_numbers);
251251

252252
// Filter empty or invalid files
253253
if code.trim().is_empty() || code.contains(char::REPLACEMENT_CHARACTER) {
@@ -331,41 +331,23 @@ pub fn display_name<P: AsRef<Path>>(p: P) -> String {
331331
".".to_string()
332332
}
333333

334-
/// Wraps the code block with a delimiter and adds line numbers if required.
334+
/// Adds line numbers to a code block if required.
335335
///
336336
/// # Arguments
337337
///
338-
/// * `code` - The code block to wrap.
339-
/// * `extension` - The file extension of the code block.
340-
/// * `line_numbers` - Whether to add line numbers to the code.
341-
/// * `no_codeblock` - Whether to not wrap the code block with a delimiter.
338+
/// * `code` - The code to process.
339+
/// * `line_numbers` - Whether to add line numbers.
342340
///
343341
/// # Returns
344342
///
345-
/// * `String` - The wrapped code block.
346-
pub fn wrap_code_block(
347-
code: &str,
348-
extension: &str,
349-
line_numbers: bool,
350-
no_codeblock: bool,
351-
) -> String {
352-
let delimiter = "`".repeat(3);
353-
let mut code_with_line_numbers = String::new();
354-
343+
/// * `String` - The processed code.
344+
pub fn wrap_code_block(code: &str, line_numbers: bool) -> String {
355345
if line_numbers {
356-
for (line_number, line) in code.lines().enumerate() {
357-
code_with_line_numbers.push_str(&format!("{:4} | {}\n", line_number + 1, line));
358-
}
359-
} else {
360-
code_with_line_numbers = code.to_string();
361-
}
362-
363-
if no_codeblock {
364-
code_with_line_numbers
346+
code.lines()
347+
.enumerate()
348+
.map(|(i, line)| format!("{:4} | {}\n", i + 1, line))
349+
.collect()
365350
} else {
366-
format!(
367-
"{}{}\n{}\n{}",
368-
delimiter, extension, code_with_line_numbers, delimiter
369-
)
351+
code.to_string()
370352
}
371353
}

crates/code2prompt-core/src/selection.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,23 @@ pub struct SelectionEngine {
3737

3838
/// Cache for performance
3939
cache: HashMap<PathBuf, bool>,
40+
41+
/// Default behavior when no patterns or user actions match
42+
deselected_by_default: bool,
4043
}
4144

4245
impl SelectionEngine {
4346
/// Create a new SelectionEngine with base patterns
44-
pub fn new(include_patterns: Vec<String>, exclude_patterns: Vec<String>) -> Self {
47+
pub fn new(
48+
include_patterns: Vec<String>,
49+
exclude_patterns: Vec<String>,
50+
deselected_by_default: bool,
51+
) -> Self {
4552
Self {
4653
filter_engine: FilterEngine::new(&include_patterns, &exclude_patterns),
4754
user_actions: Vec::new(),
4855
cache: HashMap::new(),
56+
deselected_by_default,
4957
}
5058
}
5159

@@ -74,9 +82,12 @@ impl SelectionEngine {
7482
// If there are include patterns, use them
7583
self.filter_engine.matches_patterns(path)
7684
} else {
77-
// No include patterns: default behavior is to include all files
78-
// (unless excluded by exclude patterns)
79-
!self.filter_engine.is_excluded(path)
85+
// No include patterns: default behavior depends on deselected_by_default
86+
if self.deselected_by_default {
87+
false
88+
} else {
89+
!self.filter_engine.is_excluded(path)
90+
}
8091
}
8192
}
8293

@@ -241,6 +252,12 @@ impl SelectionEngine {
241252
pub fn filter_engine(&self) -> &FilterEngine {
242253
&self.filter_engine
243254
}
255+
256+
/// Set whether the engine should default to deselected
257+
pub fn set_deselected_by_default(&mut self, value: bool) {
258+
self.deselected_by_default = value;
259+
self.cache.clear();
260+
}
244261
}
245262

246263
impl std::fmt::Debug for SelectionEngine {
@@ -249,6 +266,7 @@ impl std::fmt::Debug for SelectionEngine {
249266
.field("filter_engine", &self.filter_engine)
250267
.field("user_actions", &self.user_actions)
251268
.field("cache_size", &self.cache.len())
269+
.field("deselected_by_default", &self.deselected_by_default)
252270
.finish()
253271
}
254272
}
@@ -259,7 +277,7 @@ mod tests {
259277

260278
#[test]
261279
fn test_specificity_calculation() {
262-
let engine = SelectionEngine::new(vec![], vec![]);
280+
let engine = SelectionEngine::new(vec![], vec![], false);
263281

264282
assert_eq!(engine.calculate_specificity(Path::new("file.rs")), 1);
265283
assert_eq!(engine.calculate_specificity(Path::new("src/main.rs")), 2);
@@ -271,7 +289,7 @@ mod tests {
271289

272290
#[test]
273291
fn test_precedence_rules() {
274-
let mut engine = SelectionEngine::new(vec![], vec![]);
292+
let mut engine = SelectionEngine::new(vec![], vec![], false);
275293

276294
// Add less specific action first
277295
engine.exclude_file(PathBuf::from("src"));
@@ -286,7 +304,7 @@ mod tests {
286304

287305
#[test]
288306
fn test_recent_wins_over_old() {
289-
let mut engine = SelectionEngine::new(vec![], vec![]);
307+
let mut engine = SelectionEngine::new(vec![], vec![], false);
290308

291309
// First action
292310
engine.exclude_file(PathBuf::from("main.rs"));
@@ -296,4 +314,18 @@ mod tests {
296314
engine.include_file(PathBuf::from("main.rs"));
297315
assert!(engine.is_selected(Path::new("main.rs")));
298316
}
317+
318+
#[test]
319+
fn test_deselected_by_default() {
320+
let mut engine = SelectionEngine::new(vec![], vec![], true);
321+
322+
// By default everything is deselected
323+
assert!(!engine.is_selected(Path::new("main.rs")));
324+
assert!(!engine.is_selected(Path::new("src/lib.rs")));
325+
326+
// User action should still work
327+
engine.include_file(PathBuf::from("main.rs"));
328+
assert!(engine.is_selected(Path::new("main.rs")));
329+
assert!(!engine.is_selected(Path::new("src/lib.rs")));
330+
}
299331
}

0 commit comments

Comments
 (0)