Skip to content

Commit 1e0154c

Browse files
committed
Refactor file compaction to strip C-style function bodies while preserving control flow and update supported file extensions
1 parent 451d0de commit 1e0154c

9 files changed

Lines changed: 44 additions & 25 deletions

File tree

‎packages/vscode/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "gemini-coder",
33
"displayName": "Code Web Chat",
44
"description": "Fast AI coding with chatbots/APIs (CWC)",
5-
"version": "1.738.0",
5+
"version": "1.739.0",
66
"scripts": {
77
"build": "npx vsce package --no-dependencies",
88
"vscode:prepublish": "rimraf out && npm run compile",

‎packages/vscode/src/context/utils/compact-file/compact-file.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,24 @@ export const compact_file = (content: string, extension: string): string => {
1919
return compact_sql(content)
2020
} else if (['.html', '.htm'].includes(extension)) {
2121
return compact_html(content)
22+
} else if (
23+
[
24+
'.ts',
25+
'.js',
26+
'.c',
27+
'.cpp',
28+
'.cs',
29+
'.java',
30+
'.php',
31+
'.go',
32+
'.rs',
33+
'.swift',
34+
'.kt',
35+
'.dart'
36+
].includes(extension)
37+
) {
38+
return compact_c_style(content)
2239
} else {
23-
return compact_c_style(content, true)
40+
return content
2441
}
2542
}

‎packages/vscode/src/context/utils/compact-file/languages/c-style.ts‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
export const compact_c_style = (
2-
content: string,
3-
should_strip_bodies: boolean
4-
): string => {
1+
export const compact_c_style = (content: string): string => {
52
// Split by newline to process line by line, handling both LF and CRLF
63
const lines = content.split(/\r?\n/)
74
const result: string[] = []
@@ -76,7 +73,7 @@ export const compact_c_style = (
7673
}
7774

7875
// Handle Function Body Stripping
79-
if (should_strip_bodies && char == '{') {
76+
if (char == '{') {
8077
if (skip_body_depth > 0) {
8178
// Already skipping, just deepen the level
8279
skip_body_depth++
@@ -86,8 +83,9 @@ export const compact_c_style = (
8683
} else {
8784
// Check if we should preserve this block (class, interface, etc.)
8885
// We look at last_code_buffer for keywords
86+
// We also want to preserve control flow (if, for, while, etc.)
8987
const is_keeper =
90-
/(^|\s)(class|interface|enum|namespace|type)(\s|$)/.test(
88+
/(^|\s)(class|interface|enum|namespace|type|if|else|for|while|do|switch|try|catch|finally)(\s|$)/.test(
9189
last_code_buffer
9290
)
9391
// Also preserve object literals (preceded by = or :)
@@ -96,8 +94,7 @@ export const compact_c_style = (
9694
if (!is_keeper && !is_object_literal) {
9795
// Start skipping
9896
skip_body_depth = 1
99-
const indent = processed_line.match(/^\s*/)?.[0] || ''
100-
processed_line += `{\n${indent} // ...\n${indent}}`
97+
processed_line += `{\n// ...\n}`
10198
last_code_buffer += '{}'
10299
i++
103100
continue
@@ -106,7 +103,7 @@ export const compact_c_style = (
106103
last_code_buffer = ''
107104
}
108105
}
109-
} else if (should_strip_bodies && char == '}') {
106+
} else if (char == '}') {
110107
if (skip_body_depth > 0) {
111108
skip_body_depth--
112109
last_code_buffer += char

‎packages/vscode/src/context/utils/compact-file/languages/html.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const compact_html = (content: string): string => {
4343
const { open, content } = scripts[parseInt(idx)]
4444
// Treat as C-style but do NOT strip bodies (preserve logic, just strip comments)
4545
// as scripts in HTML are often minimal or structural.
46-
const compacted = compact_c_style(content, false).trim()
46+
const compacted = compact_c_style(content).trim()
4747
return `${open}\n${compacted}\n</script>`
4848
})
4949

‎packages/vscode/src/context/utils/compact-file/languages/jsx.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ export const compact_jsx = (content: string): string => {
9595
if (!is_keeper && !is_object_literal) {
9696
// Start skipping
9797
skip_body_depth = 1
98-
const indent = processed_line.match(/^\s*/)?.[0] || ''
99-
processed_line += `{\n${indent} // ...\n${indent}}`
98+
processed_line += `{\n// ...\n}`
10099
last_code_buffer += '{}'
101100
i++
102101
continue

‎packages/vscode/src/context/utils/compact-file/test-cases/c-style/output.txt‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
function test() {
2-
// ...
2+
// ...
33
}
44
const str = "String with \"// quote";
55
function add(a, b) {
6-
// ...
6+
// ...
77
}
88
class Calculator {
99
constructor() {
10-
// ...
11-
}
10+
// ...
11+
}
1212
add(n) {
13-
// ...
14-
}
13+
// ...
14+
}
1515
}
1616
interface Item {
1717
id: string;
1818
name: string;
1919
}
2020
const double = (x) => {
21-
// ...
21+
// ...
2222
}
2323
const config = {
2424
env: "production",

‎packages/vscode/src/context/utils/compact-file/test-cases/html/input.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
if (a < b) {
88
console.log("<div>");
99
}
10+
function test() {
11+
console.log("test");
12+
}
1013
</script>
1114
<style>
1215
/* CSS Comment */

‎packages/vscode/src/context/utils/compact-file/test-cases/html/output.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
if (a < b) {
77
console.log("<div>");
88
}
9+
function test() {
10+
// ...
11+
}
912
</script>
1013
<style>
1114
body {}

‎packages/vscode/src/context/utils/compact-file/test-cases/jsx/output.txt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ interface Props {
33
title: string;
44
}
55
export const MyComponent: React.FC<Props> = ({ title }) => {
6-
// ...
6+
// ...
77
}
88
function Header() {
9-
// ...
9+
// ...
1010
}
1111
class OldComponent extends React.Component {
1212
render() {
13-
// ...
14-
}
13+
// ...
14+
}
1515
}

0 commit comments

Comments
 (0)