@@ -5,27 +5,19 @@ import {diffLines} from 'diff'
5
5
import MarkerIndex from 'marker-index'
6
6
import temp from 'temp'
7
7
8
- class ReviewCommentTracker {
8
+ class EditorReviewComments {
9
9
constructor ( editor ) {
10
10
this . editor = editor
11
- this . invalidCommentsByCommitId = new Map ( )
12
- this . contentsByCommitId = new Map ( )
11
+ this . outdatedCommentsByCommitId = new Map ( )
12
+ this . bufferContentsByCommitId = new Map ( )
13
13
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
- } ) )
22
14
}
23
15
24
16
destroy ( ) {
25
17
this . subscriptions . dispose ( )
26
18
}
27
19
28
- trackCommentsForCommitId ( commitId , bufferContents , rowsByCommentId ) {
20
+ add ( commitId , bufferContents , rowsByCommentId ) {
29
21
let index = new MarkerIndex ( )
30
22
for ( let [ id , row ] of rowsByCommentId ) {
31
23
index . insert ( id , { row : row , column : 0 } , { row : row + 1 , column : 0 } )
@@ -37,15 +29,15 @@ class ReviewCommentTracker {
37
29
if ( change . added ) {
38
30
let { inside} = index . splice ( { row : currentRow , column : 0 } , { row : 0 , column : 0 } , { row : change . count , column : 0 } )
39
31
for ( let id of inside ) {
40
- this . invalidate ( commitId , bufferContents , id , rowsByCommentId . get ( id ) )
32
+ this . markAsOutdated ( commitId , bufferContents , id , rowsByCommentId . get ( id ) )
41
33
index . delete ( id )
42
34
}
43
35
44
36
currentRow += change . count
45
37
} else if ( change . removed ) {
46
38
let { inside} = index . splice ( { row : currentRow , column : 0 } , { row : change . count , column : 0 } , { row : 0 , column : 0 } )
47
39
for ( let id of inside ) {
48
- this . invalidate ( commitId , bufferContents , id , rowsByCommentId . get ( id ) )
40
+ this . markAsOutdated ( commitId , bufferContents , id , rowsByCommentId . get ( id ) )
49
41
index . delete ( id )
50
42
}
51
43
} else {
@@ -60,7 +52,7 @@ class ReviewCommentTracker {
60
52
let marker = this . editor . markBufferRange ( range , { reversed : true , invalidate : 'inside' } )
61
53
let subscription = marker . onDidChange ( ( { isValid} ) => {
62
54
if ( ! isValid ) {
63
- this . invalidate ( commitId , bufferContents , id , rowsByCommentId . get ( id ) )
55
+ this . markAsOutdated ( commitId , bufferContents , id , rowsByCommentId . get ( id ) )
64
56
marker . destroy ( )
65
57
subscription . dispose ( )
66
58
}
@@ -69,38 +61,47 @@ class ReviewCommentTracker {
69
61
}
70
62
}
71
63
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()")
74
67
let map = new Map ( )
75
68
map . set ( id , rowInFileContents )
76
- this . trackCommentsForCommitId ( 1234 , fileContents , map )
69
+ this . add ( 1234 , fileContents , map )
77
70
}
78
71
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 )
82
75
}
83
76
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 ( ) )
86
79
}
87
80
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
+ }
89
90
}
90
91
}
91
92
92
- describe ( 'ReviewCommentTracker ' , ( ) => {
93
- let editor , foo
93
+ describe ( 'EditorReviewComments ' , ( ) => {
94
+ let editor , editorReviewComments
94
95
95
96
beforeEach ( ( ) => {
96
97
editor = atom . workspace . buildTextEditor ( )
97
- foo = new ReviewCommentTracker ( editor )
98
+ editorReviewComments = new EditorReviewComments ( editor )
98
99
} )
99
100
100
101
it ( "adds a decoration on the same row when the buffers' contents match" , ( ) => {
101
102
editor . setText ( 'abc\ndef\nghi' )
102
103
103
- foo . track ( 1 , 'abc\ndef\nghi' , 1 )
104
+ editorReviewComments . addComment ( 1 , 'abc\ndef\nghi' , 1 )
104
105
105
106
let decorations = editor . getDecorations ( { type : 'block' } )
106
107
expect ( decorations . length ) . toBe ( 1 )
@@ -110,7 +111,7 @@ describe('ReviewCommentTracker', () => {
110
111
it ( "adds a decoration on a translated row in the current buffer corresponding to row in the original buffer" , ( ) => {
111
112
editor . setText ( 'def\nghi\nABC\nDEF\nlmn\nopq\nrst' )
112
113
113
- foo . track ( 1 , 'abc\ndef\nghi\nlmn\nopq\nrst' , 3 )
114
+ editorReviewComments . addComment ( 1 , 'abc\ndef\nghi\nlmn\nopq\nrst' , 3 )
114
115
115
116
let decorations = editor . getDecorations ( { type : 'block' } )
116
117
expect ( decorations . length ) . toBe ( 1 )
@@ -124,35 +125,35 @@ describe('ReviewCommentTracker', () => {
124
125
expect ( decorations [ 0 ] . getMarker ( ) . getHeadBufferPosition ( ) ) . toEqual ( [ 5 , 0 ] )
125
126
} )
126
127
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" , ( ) => {
128
129
editor . setText ( 'def\nABC\nDEF\nghi\nGHI\nJKL\nMNO\nopq\nrst' )
129
130
130
- foo . track ( 1 , 'abc\ndef\nghi\nlmn\nopq\nrst' , 3 )
131
+ editorReviewComments . addComment ( 1 , 'abc\ndef\nghi\nlmn\nopq\nrst' , 3 )
131
132
132
133
let decorations = editor . getDecorations ( { type : 'block' } )
133
134
expect ( decorations . length ) . toBe ( 0 )
134
135
135
136
editor . setSelectedBufferRange ( [ [ 6 , 0 ] , [ 6 , 3 ] ] )
136
137
editor . insertText ( 'lmn' )
137
- advanceClock ( editor . buffer . stoppedChangingDelay )
138
+ editorReviewComments . refreshOutdated ( )
138
139
139
140
decorations = editor . getDecorations ( { type : 'block' } )
140
141
expect ( decorations . length ) . toBe ( 1 )
141
142
expect ( decorations [ 0 ] . getMarker ( ) . getHeadBufferPosition ( ) ) . toEqual ( [ 6 , 0 ] )
142
143
} )
143
144
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" , ( ) => {
145
146
editor . setText ( 'def\nghi\nABC\nDEF\nlmn\nopq\nrst' )
146
147
147
- foo . track ( 1 , 'abc\ndef\nghi\nlmn\nopq\nrst' , 3 )
148
+ editorReviewComments . addComment ( 1 , 'abc\ndef\nghi\nlmn\nopq\nrst' , 3 )
148
149
editor . setCursorBufferPosition ( [ 4 , 0 ] )
149
150
editor . deleteLine ( )
150
151
151
152
let decorations = editor . getDecorations ( { type : 'block' } )
152
153
expect ( decorations . length ) . toBe ( 0 )
153
154
154
155
editor . undo ( )
155
- advanceClock ( editor . buffer . stoppedChangingDelay )
156
+ editorReviewComments . refreshOutdated ( )
156
157
157
158
decorations = editor . getDecorations ( { type : 'block' } )
158
159
expect ( decorations . length ) . toBe ( 1 )
@@ -163,13 +164,13 @@ describe('ReviewCommentTracker', () => {
163
164
decorations = editor . getDecorations ( { type : 'block' } )
164
165
expect ( decorations . length ) . toBe ( 0 )
165
166
166
- advanceClock ( editor . buffer . stoppedChangingDelay )
167
+ editorReviewComments . refreshOutdated ( )
167
168
168
169
decorations = editor . getDecorations ( { type : 'block' } )
169
170
expect ( decorations . length ) . toBe ( 0 )
170
171
171
172
editor . undo ( )
172
- advanceClock ( editor . buffer . stoppedChangingDelay )
173
+ editorReviewComments . refreshOutdated ( )
173
174
174
175
decorations = editor . getDecorations ( { type : 'block' } )
175
176
expect ( decorations . length ) . toBe ( 1 )
0 commit comments