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

...
Автор SHA1 Повідомлення Дата
l0ng-ai bbdf0052b2 fix(editor): offer to make a read-only file writable when editing it
A file with the read-only attribute set (often kept when copying files in
Explorer) opened read-only, and typing into it did nothing beyond the
banner. Typing, pasting or dropping into such a file now asks to make it
writable (the same question as Toggle Read-only Attribute) and clears the
attribute on confirm. Binary, too-large and library documents still refuse
edits without asking.

Fixes #3479
2026-11-15 17:15:32 +08:00
3 змінених файлів з 57 додано та 2 видалено

No files matched your search

+25 -1
Переглянути файл
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { DocModel } from "./model/document";
import { readOnlyQuestion, toggleReadOnly } from "./fileAttributes";
import { offerMakeWritable, readOnlyQuestion, toggleReadOnly } from "./fileAttributes";
function model(readOnly: boolean, extra: Partial<{ virtual: boolean; banner: string | null }> = {}) {
const meta = { readOnly, banner: extra.banner ?? (readOnly ? "readOnly" : null) };
@@ -43,3 +43,27 @@ describe("Toggle Read-only Attribute", () => {
expect(readOnlyQuestion("a.txt", false).confirm).toBe("Make Read-only");
});
});
describe("editing a read-only file", () => {
it("offers to make it writable once, then lets edits through", async () => {
const m = model(true);
answer = true;
expect(offerMakeWritable(m)).toBe(true);
expect(offerMakeWritable(m)).toBe(true);
expect(confirm).toHaveBeenCalledTimes(1);
expect(confirm).toHaveBeenCalledWith(expect.objectContaining({ title: "Make File Writable" }));
await vi.waitFor(() => expect(m.setReadOnly).toHaveBeenCalledWith(false));
expect(offerMakeWritable(m)).toBe(false);
});
it("refuses edits without asking for library, binary and too-large documents", () => {
for (const m of [
model(true, { virtual: true }),
model(true, { banner: "binary" }),
model(true, { banner: "tooLarge" }),
])
expect(offerMakeWritable(m)).toBe(true);
expect(confirm).not.toHaveBeenCalled();
expect(offerMakeWritable(model(false))).toBe(false);
});
});
+21 -1
Переглянути файл
@@ -1,7 +1,8 @@
/**
* File › File Properties › Make File Read-only / Writable (IDEA's "Toggle
* Read-only Attribute", also the status bar lock): asks, then flips the
* file's writable bit and the open document's editability.
* file's writable bit and the open document's editability. Typing into a file
* whose read-only attribute is set asks the same (IDEA's Clear Read-Only Status).
*/
import { call, commands, isTauri } from "@/platform/ipc";
import { basename } from "@/platform/project";
@@ -41,3 +42,22 @@ export function toggleReadOnly(model: DocModel) {
(ok) => ok && void setFileWritable(model, meta.readOnly),
);
}
let asking: DocModel | null = null;
/**
* An edit was attempted in `model`. When only the file's read-only attribute is in the
* way (not a binary, too large or library file), offers to make it writable, once at a
* time. Returns whether the edit was refused: the caller drops it either way.
*/
export function offerMakeWritable(model: DocModel): boolean {
const meta = model.getMeta();
if (!meta.readOnly) return false;
if (model.virtual || meta.banner !== "readOnly" || asking) return true;
asking = model;
const q = readOnlyQuestion(basename(model.path), true);
void confirm({ title: q.title, message: q.message, ok: q.confirm })
.then((ok) => ok && model.getMeta().readOnly && setFileWritable(model, true))
.finally(() => (asking = null));
return true;
}
+11
Переглянути файл
@@ -34,6 +34,7 @@ import {
import { showContextMenu } from "@/ui";
import { baseExtensions, compartments, contributionExtensions } from "../cm/setup";
import { gutterMenuItems, inGutterMenuArea } from "../gutterMenu";
import { offerMakeWritable } from "../fileAttributes";
import type { DocModel } from "./document";
import type { WorkspaceHistoryTarget } from "./workspaceHistory";
import { isNavigation } from "./navHistory";
@@ -147,6 +148,12 @@ export class EditorSlot {
return this.state;
}
private refuseReadOnlyEdit(e: Event): boolean {
if (!offerMakeWritable(this.model)) return false;
e.preventDefault();
return true;
}
private slotExtensions() {
return [
this.model.manager.workspaceHistory.extension(this.historyTarget),
@@ -169,6 +176,10 @@ export class EditorSlot {
this.host.onContextMenu(this, e);
return true;
},
// Typing, pasting or dropping into a read-only file offers to make it writable.
beforeinput: (e) => this.refuseReadOnlyEdit(e),
paste: (e) => this.refuseReadOnlyEdit(e),
drop: (e) => this.refuseReadOnlyEdit(e),
}),
ViewPlugin.define((view) => {
// Gutters sit outside the content DOM. Gutter extensions with their own menu (breakpoints,