Skip to content

Commit 737bb95

Browse files
committed
Scroll diff editors to the first changed line when opening previews
- once diff between two files is shown, trigger 'next change' to focus on the first change - if position after running [Fragment] is 0:0, don't run [Fragment], if it isn't, run [Fragment] - should not run this 'if' block [Fragment] if the first change is on the first line - wrap [Changes] in a function └── apps └── editor └── src ├── commands │ └── apply-response-command │ ├── apply-response-command.ts │ ├── types │ │ └── original-file-state.ts │ └── utils │ ├── preview │ │ ├── build-history-files.ts │ │ ├── diff-utils.ts │ │ ├── file-preparer.ts │ │ ├── index.ts │ │ ├── preview.ts │ │ ├── temp-file-manager.ts │ │ ├── types.ts │ │ ├── virtual-document-provider.ts │ │ ├── vscode-ui.ts │ │ └── workspace-listener.ts │ └── preview-handler.ts └── views └── prompt └── backend └── message-handlers └── handle-show-diff.ts
1 parent 6f395f7 commit 737bb95

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

  • apps/editor/src/commands/apply-response-command/utils/preview

‎apps/editor/src/commands/apply-response-command/utils/preview/vscode-ui.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,49 @@ export const close_preview_diff_editors = async (
3737
await Promise.all(promises)
3838
}
3939

40+
const get_first_changed_line = (
41+
original_content: string,
42+
new_content: string
43+
): number => {
44+
const original_lines = original_content.split(/\r?\n/)
45+
const new_lines = new_content.split(/\r?\n/)
46+
const max_common_lines = Math.min(original_lines.length, new_lines.length)
47+
48+
let line = 0
49+
while (line < max_common_lines && original_lines[line] === new_lines[line]) {
50+
line++
51+
}
52+
return line
53+
}
54+
55+
const scroll_to_first_changed_line = (params: {
56+
right_doc_uri: vscode.Uri
57+
original_content: string
58+
new_content: string
59+
}) => {
60+
const first_changed_line = get_first_changed_line(
61+
params.original_content,
62+
params.new_content
63+
)
64+
65+
const active_editor = vscode.window.activeTextEditor
66+
if (
67+
active_editor &&
68+
active_editor.document.uri.toString() == params.right_doc_uri.toString()
69+
) {
70+
const line = Math.min(
71+
first_changed_line,
72+
active_editor.document.lineCount - 1
73+
)
74+
const position = new vscode.Position(line, 0)
75+
active_editor.selection = new vscode.Selection(position, position)
76+
active_editor.revealRange(
77+
new vscode.Range(position, position),
78+
vscode.TextEditorRevealType.InCenter
79+
)
80+
}
81+
}
82+
4083
export const show_diff_with_actions = async (
4184
prepared_file: PreparedFile
4285
): Promise<PreviewResult> => {
@@ -55,6 +98,12 @@ export const show_diff_with_actions = async (
5598
preview: false
5699
}
57100
)
101+
102+
scroll_to_first_changed_line({
103+
right_doc_uri,
104+
original_content: prepared_file.original_content,
105+
new_content: prepared_file.previewable_file.content
106+
})
58107
}
59108

60109
return new Promise<PreviewResult>((resolve) => {

0 commit comments

Comments
 (0)