From f96a0d922ed95949eb67b530d95ce808409086f5 Mon Sep 17 00:00:00 2001 From: Ford Hurley Date: Wed, 27 Dec 2017 13:39:50 -0500 Subject: [PATCH 1/4] Ensure that new editors get unique ids This restores the behavior from when TextEditor was written in coffeescript, and extended the Model class. --- src/text-editor.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/text-editor.js b/src/text-editor.js index 05e510b75..6f9993eed 100644 --- a/src/text-editor.js +++ b/src/text-editor.js @@ -119,6 +119,10 @@ class TextEditor { } this.id = params.id != null ? params.id : nextId++ + if (this.id >= nextId) { + // Ensure that new editors get unique ids: + nextId = this.id + 1; + } this.initialScrollTopRow = params.initialScrollTopRow this.initialScrollLeftColumn = params.initialScrollLeftColumn this.decorationManager = params.decorationManager From b5189e4e4ab90cc72ba1767d2462496a2b57fa21 Mon Sep 17 00:00:00 2001 From: Ford Hurley Date: Wed, 27 Dec 2017 15:16:13 -0500 Subject: [PATCH 2/4] Delint --- src/text-editor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/text-editor.js b/src/text-editor.js index 6f9993eed..18c767f81 100644 --- a/src/text-editor.js +++ b/src/text-editor.js @@ -121,7 +121,7 @@ class TextEditor { this.id = params.id != null ? params.id : nextId++ if (this.id >= nextId) { // Ensure that new editors get unique ids: - nextId = this.id + 1; + nextId = this.id + 1 } this.initialScrollTopRow = params.initialScrollTopRow this.initialScrollLeftColumn = params.initialScrollLeftColumn From 3ad3852dd6a2fbdc5d64f21d4838fdd81ff1dbc4 Mon Sep 17 00:00:00 2001 From: Ford Hurley Date: Wed, 27 Dec 2017 15:16:22 -0500 Subject: [PATCH 3/4] Add a test for generated TextEditor ids --- spec/text-editor-spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/text-editor-spec.js b/spec/text-editor-spec.js index 89af72137..afd0d9068 100644 --- a/spec/text-editor-spec.js +++ b/spec/text-editor-spec.js @@ -20,6 +20,16 @@ describe('TextEditor', () => { await atom.packages.activatePackage('language-javascript') }) + it('generates unique ids for each editor', () => { + // Deserialized editors are initialized with an id: + new TextEditor({id: 0}) + new TextEditor({id: 1}) + new TextEditor({id: 2}) + // Initializing an editor without an id causes a new id to be generated: + const generatedId = new TextEditor().id + expect(generatedId).toBe(3) + }) + describe('when the editor is deserialized', () => { it('restores selections and folds based on markers in the buffer', async () => { editor.setSelectedBufferRange([[1, 2], [3, 4]]) From 065f4c48ec66654a604af5408fb5efb6ddb461fa Mon Sep 17 00:00:00 2001 From: Ford Hurley Date: Wed, 27 Dec 2017 16:37:12 -0500 Subject: [PATCH 4/4] Avoid dependency on shared state The test was passing only when run in isolation. --- spec/text-editor-spec.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/spec/text-editor-spec.js b/spec/text-editor-spec.js index afd0d9068..ab84d88c8 100644 --- a/spec/text-editor-spec.js +++ b/spec/text-editor-spec.js @@ -20,14 +20,15 @@ describe('TextEditor', () => { await atom.packages.activatePackage('language-javascript') }) - it('generates unique ids for each editor', () => { - // Deserialized editors are initialized with an id: - new TextEditor({id: 0}) - new TextEditor({id: 1}) - new TextEditor({id: 2}) - // Initializing an editor without an id causes a new id to be generated: - const generatedId = new TextEditor().id - expect(generatedId).toBe(3) + it('generates unique ids for each editor', async () => { + // Deserialized editors are initialized with the serialized id. We can + // initialize an editor with what we expect to be the next id: + const deserialized = new TextEditor({id: editor.id+1}) + expect(deserialized.id).toEqual(editor.id+1) + + // The id generator should skip the id used up by the deserialized one: + const fresh = new TextEditor() + expect(fresh.id).toNotEqual(deserialized.id) }) describe('when the editor is deserialized', () => {