Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c65688e

Browse files
author
Antonio Scandurra
committed
Rename to EditorReviewComments
1 parent 922cec6 commit c65688e

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

spec/github/review-comment-tracker-spec.js renamed to spec/github/editor-review-comments-spec.js

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,19 @@ import {diffLines} from 'diff'
55
import MarkerIndex from 'marker-index'
66
import temp from 'temp'
77

8-
class ReviewCommentTracker {
8+
class EditorReviewComments {
99
constructor (editor) {
1010
this.editor = editor
11-
this.invalidCommentsByCommitId = new Map()
12-
this.contentsByCommitId = new Map()
11+
this.outdatedCommentsByCommitId = new Map()
12+
this.bufferContentsByCommitId = new Map()
1313
this.subscriptions = new CompositeDisposable()
14-
15-
this.subscriptions.add(this.editor.onDidStopChanging(() => {
16-
let invalidCommentsByCommitId = this.invalidCommentsByCommitId
17-
this.invalidCommentsByCommitId = new Map()
18-
for (let [commitId, rowsByCommentId] of invalidCommentsByCommitId) {
19-
this.trackCommentsForCommitId(commitId, this.contentsByCommitId.get(commitId), rowsByCommentId)
20-
}
21-
}))
2214
}
2315

2416
destroy () {
2517
this.subscriptions.dispose()
2618
}
2719

28-
trackCommentsForCommitId (commitId, bufferContents, rowsByCommentId) {
20+
add (commitId, bufferContents, rowsByCommentId) {
2921
let index = new MarkerIndex()
3022
for (let [id, row] of rowsByCommentId) {
3123
index.insert(id, {row: row, column: 0}, {row: row + 1, column: 0})
@@ -37,15 +29,15 @@ class ReviewCommentTracker {
3729
if (change.added) {
3830
let {inside} = index.splice({row: currentRow, column: 0}, {row: 0, column: 0}, {row: change.count, column: 0})
3931
for (let id of inside) {
40-
this.invalidate(commitId, bufferContents, id, rowsByCommentId.get(id))
32+
this.markAsOutdated(commitId, bufferContents, id, rowsByCommentId.get(id))
4133
index.delete(id)
4234
}
4335

4436
currentRow += change.count
4537
} else if (change.removed) {
4638
let {inside} = index.splice({row: currentRow, column: 0}, {row: change.count, column: 0}, {row: 0, column: 0})
4739
for (let id of inside) {
48-
this.invalidate(commitId, bufferContents, id, rowsByCommentId.get(id))
40+
this.markAsOutdated(commitId, bufferContents, id, rowsByCommentId.get(id))
4941
index.delete(id)
5042
}
5143
} else {
@@ -60,7 +52,7 @@ class ReviewCommentTracker {
6052
let marker = this.editor.markBufferRange(range, {reversed: true, invalidate: 'inside'})
6153
let subscription = marker.onDidChange(({isValid}) => {
6254
if (!isValid) {
63-
this.invalidate(commitId, bufferContents, id, rowsByCommentId.get(id))
55+
this.markAsOutdated(commitId, bufferContents, id, rowsByCommentId.get(id))
6456
marker.destroy()
6557
subscription.dispose()
6658
}
@@ -69,38 +61,47 @@ class ReviewCommentTracker {
6961
}
7062
}
7163

72-
track (id, fileContents, rowInFileContents) {
73-
// throw new Error("Don't use this interface. Use trackCommentsForCommitId()")
64+
// TODO: delete this.
65+
addComment (id, fileContents, rowInFileContents) {
66+
// throw new Error("Don't use this interface. Use add()")
7467
let map = new Map()
7568
map.set(id, rowInFileContents)
76-
this.trackCommentsForCommitId(1234, fileContents, map)
69+
this.add(1234, fileContents, map)
7770
}
7871

79-
invalidate (commitId, bufferContents, id, row) {
80-
if (!this.contentsByCommitId.has(commitId)) {
81-
this.contentsByCommitId.set(commitId, bufferContents)
72+
markAsOutdated (commitId, bufferContents, id, row) {
73+
if (!this.bufferContentsByCommitId.has(commitId)) {
74+
this.bufferContentsByCommitId.set(commitId, bufferContents)
8275
}
8376

84-
if (!this.invalidCommentsByCommitId.has(commitId)) {
85-
this.invalidCommentsByCommitId.set(commitId, new Map())
77+
if (!this.outdatedCommentsByCommitId.has(commitId)) {
78+
this.outdatedCommentsByCommitId.set(commitId, new Map())
8679
}
8780

88-
this.invalidCommentsByCommitId.get(commitId).set(id, row)
81+
this.outdatedCommentsByCommitId.get(commitId).set(id, row)
82+
}
83+
84+
refreshOutdated () {
85+
let outdatedCommentsByCommitId = this.outdatedCommentsByCommitId
86+
this.outdatedCommentsByCommitId = new Map()
87+
for (let [commitId, rowsByCommentId] of outdatedCommentsByCommitId) {
88+
this.add(commitId, this.bufferContentsByCommitId.get(commitId), rowsByCommentId)
89+
}
8990
}
9091
}
9192

92-
describe('ReviewCommentTracker', () => {
93-
let editor, foo
93+
describe('EditorReviewComments', () => {
94+
let editor, editorReviewComments
9495

9596
beforeEach(() => {
9697
editor = atom.workspace.buildTextEditor()
97-
foo = new ReviewCommentTracker(editor)
98+
editorReviewComments = new EditorReviewComments(editor)
9899
})
99100

100101
it("adds a decoration on the same row when the buffers' contents match", () => {
101102
editor.setText('abc\ndef\nghi')
102103

103-
foo.track(1, 'abc\ndef\nghi', 1)
104+
editorReviewComments.addComment(1, 'abc\ndef\nghi', 1)
104105

105106
let decorations = editor.getDecorations({type: 'block'})
106107
expect(decorations.length).toBe(1)
@@ -110,7 +111,7 @@ describe('ReviewCommentTracker', () => {
110111
it("adds a decoration on a translated row in the current buffer corresponding to row in the original buffer", () => {
111112
editor.setText('def\nghi\nABC\nDEF\nlmn\nopq\nrst')
112113

113-
foo.track(1, 'abc\ndef\nghi\nlmn\nopq\nrst', 3)
114+
editorReviewComments.addComment(1, 'abc\ndef\nghi\nlmn\nopq\nrst', 3)
114115

115116
let decorations = editor.getDecorations({type: 'block'})
116117
expect(decorations.length).toBe(1)
@@ -124,35 +125,35 @@ describe('ReviewCommentTracker', () => {
124125
expect(decorations[0].getMarker().getHeadBufferPosition()).toEqual([5, 0])
125126
})
126127

127-
it("doesn't add a decoration when the comment position doesn't exist anymore, and adds it back on save if it becomes valid again", () => {
128+
it("doesn't add a decoration when the comment is already outdated, but adds it on `refreshOutdated()` if it becomes valid again", () => {
128129
editor.setText('def\nABC\nDEF\nghi\nGHI\nJKL\nMNO\nopq\nrst')
129130

130-
foo.track(1, 'abc\ndef\nghi\nlmn\nopq\nrst', 3)
131+
editorReviewComments.addComment(1, 'abc\ndef\nghi\nlmn\nopq\nrst', 3)
131132

132133
let decorations = editor.getDecorations({type: 'block'})
133134
expect(decorations.length).toBe(0)
134135

135136
editor.setSelectedBufferRange([[6, 0], [6, 3]])
136137
editor.insertText('lmn')
137-
advanceClock(editor.buffer.stoppedChangingDelay)
138+
editorReviewComments.refreshOutdated()
138139

139140
decorations = editor.getDecorations({type: 'block'})
140141
expect(decorations.length).toBe(1)
141142
expect(decorations[0].getMarker().getHeadBufferPosition()).toEqual([6, 0])
142143
})
143144

144-
it("removes decorations as soon as they become invalid and adds them back on save if they become valid again", () => {
145+
it("removes decorations if they become outdated after a buffer change and adds them back on `refreshOutdated()` if they become valid again", () => {
145146
editor.setText('def\nghi\nABC\nDEF\nlmn\nopq\nrst')
146147

147-
foo.track(1, 'abc\ndef\nghi\nlmn\nopq\nrst', 3)
148+
editorReviewComments.addComment(1, 'abc\ndef\nghi\nlmn\nopq\nrst', 3)
148149
editor.setCursorBufferPosition([4, 0])
149150
editor.deleteLine()
150151

151152
let decorations = editor.getDecorations({type: 'block'})
152153
expect(decorations.length).toBe(0)
153154

154155
editor.undo()
155-
advanceClock(editor.buffer.stoppedChangingDelay)
156+
editorReviewComments.refreshOutdated()
156157

157158
decorations = editor.getDecorations({type: 'block'})
158159
expect(decorations.length).toBe(1)
@@ -163,13 +164,13 @@ describe('ReviewCommentTracker', () => {
163164
decorations = editor.getDecorations({type: 'block'})
164165
expect(decorations.length).toBe(0)
165166

166-
advanceClock(editor.buffer.stoppedChangingDelay)
167+
editorReviewComments.refreshOutdated()
167168

168169
decorations = editor.getDecorations({type: 'block'})
169170
expect(decorations.length).toBe(0)
170171

171172
editor.undo()
172-
advanceClock(editor.buffer.stoppedChangingDelay)
173+
editorReviewComments.refreshOutdated()
173174

174175
decorations = editor.getDecorations({type: 'block'})
175176
expect(decorations.length).toBe(1)

0 commit comments

Comments
 (0)