Skip to content

Commit 7d27135

Browse files
authored
fix(lynxtron-go): preserve loaded content on undo (#64)
1 parent 50f2570 commit 7d27135

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

.changeset/calm-tools-undo.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lynxtron-go": patch
3+
---
4+
5+
Prevent Cmd+Z from clearing an untouched editor after Lynxtron Go loads a file.

lynxtron-go/scintilla-extension/module/scintilla_view.mm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,15 @@ static bool ScxVerbose() {
480480
if (current == text) return;
481481
}
482482
[container.scintillaView message:SCI_SETTEXT wParam:0 lParam:(sptr_t)text.c_str()];
483+
// Host-driven replacement is a document load, not a user edit.
484+
// SCI_SETTEXT records the inserted document in Scintilla's undo
485+
// history, so the first Cmd+Z on an untouched editor otherwise
486+
// removes the entire file. Also discard history from the previously
487+
// displayed IDE tab: applying that history to this document would be
488+
// equally incorrect. User edits made after this point are collected
489+
// normally and remain undoable.
490+
[container.scintillaView message:SCI_EMPTYUNDOBUFFER wParam:0 lParam:0];
491+
[container.scintillaView message:SCI_SETSAVEPOINT wParam:0 lParam:0];
483492
[container.scintillaView setNeedsDisplay:YES];
484493
};
485494
if ([NSThread isMainThread]) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
import { describe, expect, it } from 'vitest';
5+
6+
const TEST_DIR = path.dirname(fileURLToPath(import.meta.url));
7+
const SCINTILLA_VIEW_SOURCE = path.resolve(
8+
TEST_DIR,
9+
'../../../scintilla-extension/module/scintilla_view.mm',
10+
);
11+
12+
describe('Scintilla host content replacement', () => {
13+
it('starts loaded documents with an empty native undo history', () => {
14+
const source = fs.readFileSync(SCINTILLA_VIEW_SOURCE, 'utf8');
15+
const setContent = source.slice(
16+
source.indexOf('void ScintillaView::SetContent'),
17+
source.indexOf('std::string ScintillaView::GetContent'),
18+
);
19+
20+
const setText = setContent.indexOf('message:SCI_SETTEXT');
21+
const emptyUndo = setContent.indexOf('message:SCI_EMPTYUNDOBUFFER', setText);
22+
const savePoint = setContent.indexOf('message:SCI_SETSAVEPOINT', emptyUndo);
23+
24+
expect(setText).toBeGreaterThanOrEqual(0);
25+
expect(emptyUndo).toBeGreaterThan(setText);
26+
expect(savePoint).toBeGreaterThan(emptyUndo);
27+
});
28+
});

0 commit comments

Comments
 (0)