Skip to content

Commit 4208ee8

Browse files
authored
feat:follows linguist better now for source detection (#6)
* Follows linguist better now for source detection * Update version --------- Co-authored-by: ro <[email protected]>
1 parent f45eb39 commit 4208ee8

File tree

6 files changed

+8977
-1413
lines changed

6 files changed

+8977
-1413
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
[package]
22
name = "glimpse"
3-
version = "0.6.8"
3+
version = "0.6.9"
44
edition = "2021"
55
description = "A blazingly fast tool for peeking at codebases. Perfect for loading your codebase into an LLM's context."
66
license = "MIT"
7+
build = "build.rs"
78

89
[dependencies]
910
anyhow = "1.0.95"
@@ -24,3 +25,7 @@ tiktoken-rs = "0.6.0"
2425
tokenizers = { version = "0.21.0", features = ["http"] }
2526
toml = "0.8.19"
2627
walkdir = "2.5.0"
28+
29+
[build-dependencies]
30+
serde = { version = "1.0.217", features = ["derive"] }
31+
serde_yaml = "0.9.34"

build.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use serde::Deserialize;
2+
use std::collections::HashMap;
3+
use std::fs::File;
4+
use std::io::Write;
5+
use std::path::Path;
6+
7+
#[derive(Debug, Deserialize)]
8+
struct Language {
9+
#[serde(default)]
10+
r#type: Option<String>,
11+
#[serde(default)]
12+
extensions: Vec<String>,
13+
#[serde(default)]
14+
filenames: Vec<String>,
15+
#[serde(default)]
16+
interpreters: Vec<String>,
17+
#[serde(default)]
18+
language_id: Option<i32>,
19+
}
20+
21+
fn main() {
22+
println!("cargo:rerun-if-changed=languages.yml");
23+
24+
// Read and parse languages.yml
25+
let yaml_content =
26+
std::fs::read_to_string("languages.yml").expect("Failed to read languages.yml");
27+
let languages: HashMap<String, Language> =
28+
serde_yaml::from_str(&yaml_content).expect("Failed to parse languages.yml");
29+
30+
// Generate the rust code
31+
let mut code = String::new();
32+
33+
// Add the use statements
34+
code.push_str("use once_cell::sync::Lazy;\n");
35+
code.push_str("use std::collections::HashSet;\n\n");
36+
37+
// Generate source extensions set
38+
code.push_str("pub static SOURCE_EXTENSIONS: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n");
39+
code.push_str(" let mut set = HashSet::new();\n\n");
40+
41+
for (_, lang) in &languages {
42+
for ext in &lang.extensions {
43+
let ext = ext.trim_start_matches('.');
44+
code.push_str(&format!(" set.insert(\"{}\");\n", ext));
45+
}
46+
}
47+
48+
code.push_str(" set\n");
49+
code.push_str("});\n\n");
50+
51+
// Generate filename mappings
52+
code.push_str("pub static KNOWN_FILENAMES: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n");
53+
code.push_str(" let mut set = HashSet::new();\n\n");
54+
55+
for (_, lang) in &languages {
56+
for filename in &lang.filenames {
57+
code.push_str(&format!(" set.insert(\"{}\");\n", filename));
58+
}
59+
}
60+
61+
code.push_str(" set\n");
62+
code.push_str("});\n\n");
63+
64+
// Generate interpreter mappings
65+
code.push_str("pub static INTERPRETER_NAMES: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n");
66+
code.push_str(" let mut set = HashSet::new();\n\n");
67+
68+
for (_, lang) in &languages {
69+
for interpreter in &lang.interpreters {
70+
code.push_str(&format!(" set.insert(\"{}\");\n", interpreter));
71+
}
72+
}
73+
74+
code.push_str(" set\n");
75+
code.push_str("});\n");
76+
77+
// Write the generated code to file
78+
let out_dir = std::env::var_os("OUT_DIR").unwrap();
79+
let dest_path = Path::new(&out_dir).join("languages.rs");
80+
let mut f = File::create(dest_path).unwrap();
81+
f.write_all(code.as_bytes()).unwrap();
82+
}

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{
1919
packages.default = pkgs.rustPlatform.buildRustPackage {
2020
pname = "glimpse";
21-
version = "0.6.8";
21+
version = "0.6.9";
2222

2323
src = ./.;
2424

0 commit comments

Comments
 (0)