Skip to content

Commit e7bc65a

Browse files
committed
Fixes microsoft#44805: Should not be able to undo in readonly editor
1 parent d3e6780 commit e7bc65a

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

src/vs/editor/common/controller/cursor.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,6 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
342342
return;
343343
}
344344

345-
if (this._configuration.editor.readOnly) {
346-
// Cannot execute when read only
347-
return;
348-
}
349-
350345
if (opResult.shouldPushStackElementBefore) {
351346
this._model.pushStackElement();
352347
}
@@ -458,6 +453,12 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
458453
return;
459454
}
460455

456+
if (this._configuration.editor.readOnly) {
457+
// All the remaining handlers will try to edit the model,
458+
// but we cannot edit when read only...
459+
return;
460+
}
461+
461462
const oldState = new CursorModelState(this._model, this);
462463
let cursorChangeReason = CursorChangeReason.NotSet;
463464

src/vs/editor/test/browser/controller/cursor.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,34 @@ suite('Editor Controller - Regression tests', () => {
19061906
assertCursor(cursor, new Selection(1, 6, 1, 6));
19071907
});
19081908
});
1909+
1910+
test('issue #44805: Should not be able to undo in readonly editor', () => {
1911+
let model = TextModel.createFromString(
1912+
[
1913+
''
1914+
].join('\n'),
1915+
{
1916+
defaultEOL: DefaultEndOfLine.LF,
1917+
detectIndentation: false,
1918+
insertSpaces: false,
1919+
tabSize: 4,
1920+
trimAutoWhitespace: true
1921+
}
1922+
);
1923+
1924+
withTestCodeEditor(null, { readOnly: true, model: model }, (editor, cursor) => {
1925+
model.pushEditOperations([new Selection(1, 1, 1, 1)], [{
1926+
range: new Range(1, 1, 1, 1),
1927+
text: 'Hello world!'
1928+
}], () => [new Selection(1, 1, 1, 1)]);
1929+
assert.equal(model.getValue(EndOfLinePreference.LF), 'Hello world!');
1930+
1931+
cursorCommand(cursor, H.Undo, {});
1932+
assert.equal(model.getValue(EndOfLinePreference.LF), 'Hello world!');
1933+
});
1934+
1935+
model.dispose();
1936+
});
19091937
});
19101938

19111939
suite('Editor Controller - Cursor Configuration', () => {

0 commit comments

Comments
 (0)