Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 9658fadd4f fix(format): keep the caret when a language server replaces the whole file
Vue's language server (Volar) answers Reformat Code with a single edit that
replaces the whole document. Applied as is, the caret and the scroll position
jump to the top of the file. Multi-line LSP formatting edits are now reduced to
one edit per changed region (via Document::diff) before they reach the editor,
so the caret and view stay where they were for every server that does this.
2026-11-19 19:05:05 +08:00
l0ng-ai a0a51a63cb feat(vcs): open a push dialog file's diff in its own window (#4490)
Double-click or Enter on a file in the push dialog opens its diff in a separate diff window (Show Diff in New Window), as IDEA's push dialog does; closing it returns to the dialog. Before, the diff was only shown in a split inside the small modal dialog. Selecting a file still shows the diff in the dialog.
2026-11-19 18:28:32 +08:00
4 змінених файлів з 79 додано та 5 видалено

No files matched your search

+47 -1
Переглянути файл
@@ -119,6 +119,29 @@ fn php_cs_fixer_edits(
.collect())
}
/// Some language servers (Vue's, for one) answer Reformat Code with one edit replacing the
/// whole file; applied as is, it moves the caret and the view to the top of the file. The same
/// result as one edit per changed region keeps them where they were.
fn minimal_edits(text: &str, edits: Vec<TextEdit>) -> Vec<TextEdit> {
if !edits.iter().any(|e| e.range.end.line > e.range.start.line) {
return edits;
}
let original = ide_text::Document::new(text);
let mut formatted = original.clone();
if formatted.apply_edits(&edits).is_err() {
return edits;
}
original
.diff(&formatted.text())
.changes
.into_iter()
.map(|c| TextEdit {
range: Range::new(original.offset_to_pos(c.from), original.offset_to_pos(c.to)),
new_text: c.insert,
})
.collect()
}
pub async fn format(
host: &Host,
window: &str,
@@ -292,7 +315,11 @@ async fn provider_edits(
if options.on_save && !settings.editor.format_on_save {
return Ok(None);
}
return project.lsp.format(doc, range, options).await.map(Some);
let edits = project.lsp.format(doc, range, options).await?;
let text = snap.text;
return tokio::task::spawn_blocking(move || Some(minimal_edits(&text, edits)))
.await
.map_err(|e| IdeError::internal(e.to_string()));
};
let tool_settings = settings.languages.formatter(selected);
if options.on_save && !tool_settings.run_on_save {
@@ -523,6 +550,25 @@ mod tests {
const ALIGN: PhpAlignment = PhpAlignment { key_value_pairs: true, assignments: true };
#[test]
fn a_whole_file_edit_becomes_one_edit_per_changed_region() {
let text = "a\n b\nc\nd\n e\n";
let formatted = "a\nb\nc\nd\ne\n";
let whole = vec![TextEdit { range: Range::new(Pos::new(0, 0), Pos::new(5, 0)), new_text: formatted.into() }];
let edits = minimal_edits(text, whole);
assert_eq!(
edits,
[
TextEdit { range: Range::new(Pos::new(1, 0), Pos::new(1, 2)), new_text: String::new() },
TextEdit { range: Range::new(Pos::new(4, 0), Pos::new(4, 2)), new_text: String::new() },
]
);
assert_eq!(after(text, &edits), formatted);
// Single-line edits are already minimal and pass through.
let small = vec![TextEdit { range: Range::new(Pos::new(1, 0), Pos::new(1, 2)), new_text: String::new() }];
assert_eq!(minimal_edits(text, small.clone()), small);
}
#[test]
fn php_alignment_is_off_unless_the_php_code_style_turns_it_on() {
let php = LanguageId::new("php");
+4 -1
Переглянути файл
@@ -19,6 +19,7 @@ export function ComparisonTree<F>({
getColor,
onOpen,
inlineDiff = true,
openDiff = (diff) => void openVcsDiff(diff),
}: {
tree: FileTreeNode<F>[];
current: F | null;
@@ -32,6 +33,8 @@ export function ComparisonTree<F>({
onOpen?: () => void;
/** Show the selected file's diff beside the tree; off where there is no room (a sidebar). */
inlineDiff?: boolean;
/** With `openOnActivate`: where a double-clicked file's diff opens (default: an editor tab). */
openDiff?: (diff: DiffData) => void;
}) {
const [selected, setSelected] = useState<ReadonlySet<string>>(new Set());
const [expanded, setExpanded] = useState<ReadonlySet<string> | null>(null);
@@ -58,7 +61,7 @@ export function ComparisonTree<F>({
onSelect(entry);
const diff = diffFor(entry);
if (openOnActivate && diff) {
void openVcsDiff(diff);
openDiff(diff);
onOpen?.();
}
}}
+6 -2
Переглянути файл
@@ -1,10 +1,12 @@
/**
* Push dialog: the files changed by the outgoing commits (the selected commit's, or all of
* them), with the selected file's diff beside the tree. The diff stays inside the dialog,
* which is modal. A single commit also shows its message, author and time.
* them), with the selected file's diff beside the tree. Double-click or Enter opens the diff in
* its own window, as IDEA does, since the dialog is modal and small. A single commit also shows
* its message, author and time.
*/
import { useEffect, useMemo, useState } from "react";
import type { ChangedFile, Commit, RevisionChange } from "@/bindings";
import { executeAction } from "@/platform/actions";
import { call, commands } from "@/platform/ipc";
import { tx } from "@/platform/i18n";
import { Button, IconButton, Spinner, SplitGroup, SplitHandle, SplitPanel, T } from "@/ui";
@@ -94,6 +96,8 @@ export function PushChanges({
}}
diffFor={(c) => c.diff}
inlineDiff={!onSelectFile}
openOnActivate
openDiff={(diff) => void executeAction("vcs.showDiffInNewWindow", diff)}
getColor={(c) => KIND_COLOR[c.kind === "untracked" ? "added" : c.kind]}
ariaLabel="Changed files"
renderDetails={(n) =>
+22 -1
Переглянути файл
@@ -2,7 +2,16 @@ import { describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen } from "@testing-library/react";
import type { Commit } from "@/bindings";
const { files, combined } = vi.hoisted(() => ({ files: vi.fn(), combined: vi.fn() }));
const { files, combined, executeAction } = vi.hoisted(() => ({
files: vi.fn(),
combined: vi.fn(),
executeAction: vi.fn(async () => true),
}));
vi.mock("@/platform/actions", async (importOriginal) => ({
...(await importOriginal<typeof import("@/platform/actions")>()),
executeAction,
}));
vi.mock("@/platform/ipc", async (importOriginal) => {
const real = await importOriginal<typeof import("@/platform/ipc")>();
@@ -76,6 +85,18 @@ describe("Push dialog changes", () => {
expect(localStorage.getItem("openidea.vcs.push.groupByDirectory")).toBe("false");
});
it("opens a double-clicked file's diff in its own window and keeps the selection's diff in the dialog", async () => {
files.mockResolvedValue({ status: "ok", data: [{ path: "a.txt", oldPath: null, kind: "modified" }] });
const select = vi.fn();
render(<PushChanges repo="/r" commits={[older]} onSelectFile={select} />);
fireEvent.doubleClick(await screen.findByText("a.txt"));
expect(select).toHaveBeenLastCalledWith(expect.objectContaining({ path: "a.txt" }));
expect(executeAction).toHaveBeenCalledWith(
"vcs.showDiffInNewWindow",
expect.objectContaining({ path: expect.stringContaining("a.txt") }),
);
});
it("shows no commit details for several commits", async () => {
combined.mockResolvedValue({ status: "ok", data: [] });
render(<PushChanges repo="/r" commits={[newer, older]} />);