Skip to content

Commit 1d80ccc

Browse files
committed
Bump version to 1.823.0 and improve diff processor to handle trimmed search lines in generic test case 5
1 parent a6f7920 commit 1d80ccc

6 files changed

Lines changed: 430 additions & 38 deletions

File tree

‎apps/editor/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": "Free AI coding in chatbots (CWC)",
5-
"version": "1.822.0",
5+
"version": "1.823.0",
66
"scripts": {
77
"build": "npx vsce package --no-dependencies",
88
"vscode:prepublish": "rimraf out && npm run compile",

‎apps/editor/src/commands/apply-chat-response-command/utils/diff-processor/diff-processor.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,19 @@ describe('diff-processor', () => {
112112

113113
expect(result).toBe(expected)
114114
})
115+
116+
it('applies diff with generic test case 5 correctly', async () => {
117+
const test_case = 'generic-5'
118+
const original = load_test_case_file('', test_case, 'original.txt')
119+
const diff = load_test_case_file('', test_case, 'diff.txt')
120+
const expected = load_test_case_file('', test_case, 'expected.txt')
121+
122+
const result = apply_diff({
123+
original_code: original,
124+
diff_patch: diff
125+
})
126+
127+
expect(result).toBe(expected)
128+
})
115129
})
116130
})

‎apps/editor/src/commands/apply-chat-response-command/utils/diff-processor/diff-processor.ts‎

Lines changed: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -229,53 +229,106 @@ export const apply_diff = (params: {
229229
continue
230230
}
231231

232+
const safe_trim_indices: number[] = []
233+
for (const replace_line of block.replace_lines) {
234+
if (replace_line.search_index === null) {
235+
break // Hit an insertion, cannot trim further
236+
}
237+
safe_trim_indices.push(replace_line.search_index)
238+
}
239+
240+
let max_trim_top = 0
241+
for (let k = 0; k < safe_trim_indices.length; k++) {
242+
if (safe_trim_indices[k] === k) {
243+
max_trim_top = k + 1
244+
} else {
245+
break
246+
}
247+
}
248+
232249
let found = false
233-
for (
234-
let j = previous_found_index;
235-
j < original_code_lines_normalized.length;
236-
j++
237-
) {
238-
let search_ptr = 0
239-
let original_ptr = j
240-
const matched_indices: number[] = []
241-
const current_search_to_original = new Map<number, number>()
242-
243-
while (
244-
search_ptr < block.search_lines.length &&
245-
original_ptr < original_code_lines_normalized.length
250+
let matched_info: {
251+
start_index: number
252+
actual_count: number
253+
map: Map<number, number>
254+
} | null = null
255+
let used_trim_top = 0
256+
257+
for (let trim_cnt = 0; trim_cnt <= max_trim_top; trim_cnt++) {
258+
const current_search_lines = block.search_lines.slice(trim_cnt)
259+
if (current_search_lines.length === 0) continue
260+
261+
for (
262+
let j = previous_found_index;
263+
j < original_code_lines_normalized.length;
264+
j++
246265
) {
247-
const s_val = block.search_lines[search_ptr]
248-
const o_val = original_code_lines_normalized[original_ptr].value
249-
250-
if (s_val == o_val) {
251-
current_search_to_original.set(
252-
search_ptr,
253-
original_code_lines_normalized[original_ptr].key
254-
)
255-
matched_indices.push(original_code_lines_normalized[original_ptr].key)
256-
search_ptr++
257-
original_ptr++
258-
} else if (o_val == '~nnn') {
259-
matched_indices.push(original_code_lines_normalized[original_ptr].key)
260-
original_ptr++
261-
} else if (s_val == '~nnn') {
262-
search_ptr++
263-
} else {
266+
let search_ptr = 0
267+
let original_ptr = j
268+
const matched_indices: number[] = []
269+
const current_search_to_original = new Map<number, number>()
270+
271+
while (
272+
search_ptr < current_search_lines.length &&
273+
original_ptr < original_code_lines_normalized.length
274+
) {
275+
const s_val = current_search_lines[search_ptr]
276+
const o_val = original_code_lines_normalized[original_ptr].value
277+
278+
if (s_val == o_val) {
279+
current_search_to_original.set(
280+
search_ptr + trim_cnt,
281+
original_code_lines_normalized[original_ptr].key
282+
)
283+
matched_indices.push(
284+
original_code_lines_normalized[original_ptr].key
285+
)
286+
search_ptr++
287+
original_ptr++
288+
} else if (o_val == '~nnn') {
289+
matched_indices.push(
290+
original_code_lines_normalized[original_ptr].key
291+
)
292+
original_ptr++
293+
} else if (s_val == '~nnn') {
294+
search_ptr++
295+
} else {
296+
break
297+
}
298+
}
299+
300+
if (search_ptr == current_search_lines.length) {
301+
matched_info = {
302+
start_index: matched_indices[0],
303+
actual_count: matched_indices.length,
304+
map: current_search_to_original
305+
}
306+
found = true
264307
break
265308
}
266309
}
267310

268-
if (search_ptr == block.search_lines.length) {
269-
block.search_block_start_index = matched_indices[0]
270-
block.actual_original_line_count = matched_indices.length
271-
block.search_to_original_map = current_search_to_original
272-
found = true
273-
previous_found_index = matched_indices[matched_indices.length - 1] + 1
311+
if (found) {
312+
used_trim_top = trim_cnt
274313
break
275314
}
276315
}
277316

278-
if (!found) {
317+
if (found && matched_info) {
318+
block.search_block_start_index = matched_info.start_index
319+
block.actual_original_line_count = matched_info.actual_count
320+
block.search_to_original_map = matched_info.map
321+
322+
if (used_trim_top > 0) {
323+
block.search_lines = block.search_lines.slice(used_trim_top)
324+
block.replace_lines = block.replace_lines.filter(
325+
(l) => l.search_index === null || l.search_index >= used_trim_top
326+
)
327+
}
328+
329+
const indices = Array.from(matched_info.map.values())
330+
previous_found_index = Math.max(...indices) + 1
331+
} else {
279332
block.search_block_start_index = -2
280333
}
281334
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--- a/apps/editor/src/context/providers/workspace/workspace-provider.ts
2+
+++ b/apps/editor/src/context/providers/workspace/workspace-provider.ts
3+
@@ -91,13 +91,17 @@
4+
private _workspace_view_collapsible_state: vscode.TreeItemCollapsibleState =
5+
vscode.TreeItemCollapsibleState.Collapsed
6+
7+
- constructor(
8+
- workspace_folders: vscode.WorkspaceFolder[],
9+
- context: vscode.ExtensionContext
10+
- ) {
11+
- this._context = context
12+
- this._workspace_roots = workspace_folders.map((folder) => folder.uri.fsPath)
13+
- this._workspace_names = workspace_folders.map((folder) => folder.name)
14+
+ constructor(params: {
15+
+ workspace_folders: vscode.WorkspaceFolder[]
16+
+ context: vscode.ExtensionContext
17+
+ }) {
18+
+ this._context = params.context
19+
+ this._workspace_roots = params.workspace_folders.map(
20+
+ (folder) => folder.uri.fsPath
21+
+ )
22+
+ this._workspace_names = params.workspace_folders.map(
23+
+ (folder) => folder.name
24+
+ )
25+
this.onDidChangeCheckedFiles(() => this._save_checked_files_state())
26+
this._token_calculator = new TokenCalculator(this, this._context)
27+
this._load_ignore_patterns()
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import * as vscode from 'vscode'
2+
import * as fs from 'fs'
3+
import * as path from 'path'
4+
import ignore, { Ignore } from 'ignore'
5+
import {
6+
CONTEXT_CHECKED_PATHS_STATE_KEY,
7+
CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY,
8+
RANGES_STATE_KEY
9+
} from '@/constants/state-keys'
10+
import { IGNORE_PATTERNS } from '@/constants/ignore-patterns'
11+
import { natural_sort } from '@/utils/natural-sort'
12+
import { dictionary } from '@shared/constants/dictionary'
13+
import { Logger } from '@shared/utils/logger'
14+
import { display_token_count } from '@/utils/display-token-count'
15+
import { TokenCalculator } from './modules/token-calculator'
16+
17+
export interface IWorkspaceProvider {
18+
get_workspace_roots(): string[]
19+
get_workspace_root_for_file(file_path: string): string | undefined
20+
get_workspace_name(root_path: string): string
21+
get_range(filePath: string): string | undefined
22+
apply_range_to_content(content: string, range_string: string): string
23+
is_excluded(relative_path: string): boolean
24+
is_ignored_by_patterns(file_path: string): boolean
25+
get_check_state(path: string): vscode.TreeItemCheckboxState
26+
is_partially_checked(path: string): boolean
27+
get_checked_files(): string[]
28+
get_selection_timestamp(path: string): number | undefined
29+
}
30+
31+
export class WorkspaceProvider
32+
implements
33+
vscode.TreeDataProvider<FileItem>,
34+
vscode.Disposable,
35+
IWorkspaceProvider
36+
{
37+
private _on_did_change_tree_data: vscode.EventEmitter<
38+
FileItem | undefined | null | void
39+
> = new vscode.EventEmitter<FileItem | undefined | null | void>()
40+
readonly onDidChangeTreeData: vscode.Event<
41+
FileItem | undefined | null | void
42+
> = this._on_did_change_tree_data.event
43+
private _context: vscode.ExtensionContext
44+
private _workspace_roots: string[] = []
45+
private _workspace_names: string[] = []
46+
private _checked_items: Map<string, vscode.TreeItemCheckboxState> = new Map()
47+
private _checked_timestamps: Map<string, number> = new Map()
48+
private _combined_gitignore = ignore()
49+
private _user_ignore_patterns: Ignore = ignore()
50+
private _user_allow_patterns: Ignore = ignore()
51+
private _watcher: vscode.FileSystemWatcher
52+
private _ranges_watcher: vscode.FileSystemWatcher
53+
private _gitignore_watcher: vscode.FileSystemWatcher
54+
private _file_ranges: Map<string, string> = new Map()
55+
private _token_calculator: TokenCalculator
56+
57+
private _config_change_handler: vscode.Disposable
58+
private _on_did_change_checked_files = new vscode.EventEmitter<void>()
59+
readonly onDidChangeCheckedFiles = this._on_did_change_checked_files.event
60+
private _refresh_timeout: NodeJS.Timeout | null = null
61+
// Track which files were opened from workspace view to prevent auto-checking
62+
private _opened_from_workspace_view: Set<string> = new Set()
63+
private _preview_tabs: Map<string, boolean> = new Map()
64+
private _tab_change_handler: vscode.Disposable
65+
private _partially_checked_dirs: Set<string> = new Set()
66+
private _file_workspace_map: Map<string, string> = new Map()
67+
public gitignore_initialization: Promise<void>
68+
public ranges_initialization: Promise<void>
69+
public use_compact_token_count: boolean = false
70+
private _context_view_collapsible_state: vscode.TreeItemCollapsibleState =
71+
vscode.TreeItemCollapsibleState.Expanded
72+
private _workspace_view_collapsible_state: vscode.TreeItemCollapsibleState =
73+
vscode.TreeItemCollapsibleState.Collapsed
74+
75+
public set_context_view_collapsible_state(
76+
state: vscode.TreeItemCollapsibleState
77+
) {
78+
this._context_view_collapsible_state = state
79+
}
80+
81+
public set_workspace_view_collapsible_state(
82+
state: vscode.TreeItemCollapsibleState
83+
) {
84+
this._workspace_view_collapsible_state = state
85+
}
86+
87+
public set_use_compact_token_count(use_compact: boolean) {
88+
if (this.use_compact_token_count != use_compact) {
89+
this.use_compact_token_count = use_compact
90+
this.refresh()
91+
}
92+
}
93+
94+
constructor(params: {
95+
workspace_folders: vscode.WorkspaceFolder[]
96+
context: vscode.ExtensionContext
97+
}) {
98+
this._context = params.context
99+
this._workspace_roots = params.workspace_folders.map(
100+
(folder) => folder.uri.fsPath
101+
)
102+
this._workspace_names = params.workspace_folders.map(
103+
(folder) => folder.name
104+
)
105+
this.onDidChangeCheckedFiles(() => this._save_checked_files_state())
106+
this._token_calculator = new TokenCalculator(this, this._context)
107+
this._load_ignore_patterns()
108+
109+
this._watcher = vscode.workspace.createFileSystemWatcher('**/*')
110+
this._watcher.onDidCreate((uri) => this._handle_file_create(uri.fsPath))
111+
this._watcher.onDidChange((uri) => this._on_file_system_changed(uri.fsPath))
112+
this._watcher.onDidDelete((uri) => this._on_file_system_changed(uri.fsPath))
113+
114+
this._gitignore_watcher = vscode.workspace.createFileSystemWatcher(
115+
'**/{.gitignore,.cursorignore,.codeiumignore}'
116+
)
117+
this._gitignore_watcher.onDidCreate(() => this._load_all_gitignore_files())
118+
this._gitignore_watcher.onDidChange(() => this._load_all_gitignore_files())
119+
this._gitignore_watcher.onDidDelete(() => this._load_all_gitignore_files())
120+
121+
this._config_change_handler = vscode.workspace.onDidChangeConfiguration(
122+
(event) => {
123+
if (
124+
event.affectsConfiguration('codeWebChat.ignorePatterns') ||
125+
event.affectsConfiguration('codeWebChat.allowPatterns')
126+
) {
127+
this._load_ignore_patterns()
128+
this._uncheck_ignored_files()
129+
this.refresh()
130+
}
131+
}
132+
)
133+
134+
this._update_preview_tabs_state()
135+
136+
this._tab_change_handler = vscode.window.tabGroups.onDidChangeTabs((e) => {
137+
this._handle_tab_changes(e)
138+
})
139+
140+
this.gitignore_initialization = this._load_all_gitignore_files()
141+
this._ranges_watcher = vscode.workspace.createFileSystemWatcher(
142+
'**/.vscode/ranges.json'
143+
)
144+
this._ranges_watcher.onDidCreate(() => this.load_all_ranges())
145+
this._ranges_watcher.onDidChange(() => this.load_all_ranges())
146+
this._ranges_watcher.onDidDelete(() => this.load_all_ranges())
147+
148+
this.ranges_initialization = this.load_all_ranges()
149+
150+
this.load_checked_files_state()
151+
}

0 commit comments

Comments
 (0)