-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlanguage_detection_demo.rs
More file actions
289 lines (233 loc) · 6.08 KB
/
language_detection_demo.rs
File metadata and controls
289 lines (233 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//! Demonstration of the enhanced language detection capabilities
use smart_diff_parser::language::LanguageDetector;
fn main() {
println!("Smart Code Diff - Language Detection Demo");
println!("=========================================");
// Test file extension detection
demo_file_extension_detection();
// Test content-based detection
demo_content_detection();
// Test combined detection
demo_combined_detection();
// Test edge cases
demo_edge_cases();
}
fn demo_file_extension_detection() {
println!("\n--- File Extension Detection ---");
let test_files = vec![
"Calculator.java",
"script.py",
"app.js",
"main.cpp",
"utils.c",
"data.h",
"component.jsx",
"module.pyw",
"unknown.xyz",
];
for file in test_files {
let detected = LanguageDetector::detect_from_path(file);
println!("{:<15} -> {:?}", file, detected);
}
}
fn demo_content_detection() {
println!("\n--- Content-Based Detection ---");
// Java example
let java_code = r#"
public class Calculator {
private int value;
public Calculator() {
this.value = 0;
}
public int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Result: " + calc.add(5, 3));
}
}
"#;
let detected = LanguageDetector::detect_from_content(java_code);
println!("Java code detected as: {:?}", detected);
// Python example
let python_code = r#"
class Calculator:
def __init__(self):
self.value = 0
def add(self, a, b):
return a + b
def main():
calc = Calculator()
result = calc.add(5, 3)
print(f"Result: {result}")
if __name__ == "__main__":
main()
"#;
let detected = LanguageDetector::detect_from_content(python_code);
println!("Python code detected as: {:?}", detected);
// JavaScript example
let js_code = r#"
class Calculator {
constructor() {
this.value = 0;
}
add(a, b) {
return a + b;
}
}
const calc = new Calculator();
const result = calc.add(5, 3);
console.log(`Result: ${result}`);
// Arrow function example
const multiply = (a, b) => a * b;
console.log(`Multiply: ${multiply(4, 6)}`);
"#;
let detected = LanguageDetector::detect_from_content(js_code);
println!("JavaScript code detected as: {:?}", detected);
// C++ example
let cpp_code = r#"
#include <iostream>
#include <vector>
class Calculator {
private:
int value;
public:
Calculator() : value(0) {}
int add(int a, int b) {
return a + b;
}
};
int main() {
Calculator calc;
int result = calc.add(5, 3);
std::cout << "Result: " << result << std::endl;
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (const auto& num : numbers) {
std::cout << num << " ";
}
return 0;
}
"#;
let detected = LanguageDetector::detect_from_content(cpp_code);
println!("C++ code detected as: {:?}", detected);
// C example
let c_code = r#"
#include <stdio.h>
#include <stdlib.h>
struct Calculator {
int value;
};
int add(int a, int b) {
return a + b;
}
int main() {
struct Calculator calc = {0};
int result = add(5, 3);
printf("Result: %d\n", result);
int* numbers = malloc(5 * sizeof(int));
for (int i = 0; i < 5; i++) {
numbers[i] = i + 1;
printf("%d ", numbers[i]);
}
printf("\n");
free(numbers);
return 0;
}
"#;
let detected = LanguageDetector::detect_from_content(c_code);
println!("C code detected as: {:?}", detected);
}
fn demo_combined_detection() {
println!("\n--- Combined Detection (Path + Content) ---");
// Test cases where extension and content might conflict
let test_cases = vec![
(
"script.py",
r#"
def hello():
print("Hello from Python!")
if __name__ == "__main__":
hello()
"#,
"Clear Python content with .py extension",
),
(
"script.txt",
r#"
def hello():
print("Hello from Python!")
if __name__ == "__main__":
hello()
"#,
"Clear Python content with .txt extension",
),
(
"main.cpp",
r#"
int main() {
return 0;
}
"#,
"Ambiguous C/C++ content with .cpp extension",
),
(
"main.c",
r#"
int main() {
return 0;
}
"#,
"Ambiguous C/C++ content with .c extension",
),
(
"app.js",
r#"
function greet(name) {
console.log("Hello, " + name + "!");
}
const person = "World";
greet(person);
"#,
"Clear JavaScript content with .js extension",
),
];
for (filename, content, description) in test_cases {
let detected = LanguageDetector::detect(filename, content);
println!("{:<50} -> {:?}", description, detected);
}
}
fn demo_edge_cases() {
println!("\n--- Edge Cases ---");
// Empty content
let detected = LanguageDetector::detect_from_content("");
println!("Empty content detected as: {:?}", detected);
// Very short content
let detected = LanguageDetector::detect_from_content("int x;");
println!("Short C-like content detected as: {:?}", detected);
// Mixed language content (should pick the strongest signal)
let mixed_content = r#"
// This looks like C++
#include <iostream>
// But also has Python-like comments
def some_function():
pass
// And JavaScript
console.log("Hello");
// But the C++ is strongest
int main() {
std::cout << "Hello World" << std::endl;
return 0;
}
"#;
let detected = LanguageDetector::detect_from_content(mixed_content);
println!("Mixed language content detected as: {:?}", detected);
// Comments only
let comments_only = r#"
// This is a comment
/* This is also a comment */
# This is a Python comment
"#;
let detected = LanguageDetector::detect_from_content(comments_only);
println!("Comments-only content detected as: {:?}", detected);
}