Skip to content

Commit 3482f8f

Browse files
author
Max Brunsfeld
authored
Merge pull request atom#16455 from fordhurley/fix-unique-editor-ids
Ensure that new editors get unique ids
2 parents e51d2b0 + 065f4c4 commit 3482f8f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

spec/text-editor-spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ describe('TextEditor', () => {
2020
await atom.packages.activatePackage('language-javascript')
2121
})
2222

23+
it('generates unique ids for each editor', async () => {
24+
// Deserialized editors are initialized with the serialized id. We can
25+
// initialize an editor with what we expect to be the next id:
26+
const deserialized = new TextEditor({id: editor.id+1})
27+
expect(deserialized.id).toEqual(editor.id+1)
28+
29+
// The id generator should skip the id used up by the deserialized one:
30+
const fresh = new TextEditor()
31+
expect(fresh.id).toNotEqual(deserialized.id)
32+
})
33+
2334
describe('when the editor is deserialized', () => {
2435
it('restores selections and folds based on markers in the buffer', async () => {
2536
editor.setSelectedBufferRange([[1, 2], [3, 4]])

src/text-editor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ class TextEditor {
119119
}
120120

121121
this.id = params.id != null ? params.id : nextId++
122+
if (this.id >= nextId) {
123+
// Ensure that new editors get unique ids:
124+
nextId = this.id + 1
125+
}
122126
this.initialScrollTopRow = params.initialScrollTopRow
123127
this.initialScrollLeftColumn = params.initialScrollLeftColumn
124128
this.decorationManager = params.decorationManager

0 commit comments

Comments
 (0)