Skip to content

Commit 6c31a99

Browse files
committed
Add the line numbers to the SVG diagram to enable real time tracking
1 parent 64e78ef commit 6c31a99

File tree

5 files changed

+46
-33
lines changed

5 files changed

+46
-33
lines changed

src/diagram.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Diagram() {
1313
/*
1414
* Return an existing actor with this alias, or creates a new one with alias and name.
1515
*/
16-
Diagram.prototype.getActor = function(alias, name) {
16+
Diagram.prototype.getActor = function(alias, name, lineno) {
1717
alias = alias.trim();
1818

1919
var i;
@@ -23,14 +23,14 @@ Diagram.prototype.getActor = function(alias, name) {
2323
return actors[i];
2424
}
2525
}
26-
i = actors.push(new Diagram.Actor(alias, (name || alias), actors.length));
26+
i = actors.push(new Diagram.Actor(alias, (name || alias), actors.length, lineno));
2727
return actors[ i - 1 ];
2828
};
2929

3030
/*
3131
* Parses the input as either a alias, or a "name as alias", and returns the corresponding actor.
3232
*/
33-
Diagram.prototype.getActorWithAlias = function(input) {
33+
Diagram.prototype.getActorWithAlias = function(input, lineno) {
3434
input = input.trim();
3535

3636
// We are lazy and do some of the parsing in javascript :(. TODO move into the .jison file.
@@ -43,41 +43,47 @@ Diagram.prototype.getActorWithAlias = function(input) {
4343
} else {
4444
name = alias = input;
4545
}
46-
return this.getActor(alias, name);
46+
return this.getActor(alias, name, lineno);
4747
};
4848

49-
Diagram.prototype.setTitle = function(title) {
50-
this.title = title;
49+
Diagram.prototype.setTitle = function(title, lineno) {
50+
this.title = {
51+
message: title,
52+
lineno: lineno
53+
};
5154
};
5255

5356
Diagram.prototype.addSignal = function(signal) {
5457
this.signals.push(signal);
5558
};
5659

57-
Diagram.Actor = function(alias, name, index) {
58-
this.alias = alias;
59-
this.name = name;
60-
this.index = index;
60+
Diagram.Actor = function(alias, name, index, lineno) {
61+
this.alias = alias;
62+
this.name = name;
63+
this.index = index;
64+
this.lineno = lineno;
6165
};
6266

63-
Diagram.Signal = function(actorA, signaltype, actorB, message) {
67+
Diagram.Signal = function(actorA, signaltype, actorB, message, lineno) {
6468
this.type = 'Signal';
6569
this.actorA = actorA;
6670
this.actorB = actorB;
6771
this.linetype = signaltype & 3;
6872
this.arrowtype = (signaltype >> 2) & 3;
6973
this.message = message;
74+
this.lineno = lineno;
7075
};
7176

7277
Diagram.Signal.prototype.isSelf = function() {
7378
return this.actorA.index == this.actorB.index;
7479
};
7580

76-
Diagram.Note = function(actor, placement, message) {
81+
Diagram.Note = function(actor, placement, message, lineno) {
7782
this.type = 'Note';
7883
this.actor = actor;
7984
this.placement = placement;
8085
this.message = message;
86+
this.lineno = lineno;
8187

8288
if (this.hasManyActors() && actor[0] == actor[1]) {
8389
throw new Error('Note should be over two different actors');

src/grammar.jison

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ statement
5757
: 'participant' actor_alias { $2; }
5858
| signal { yy.parser.yy.addSignal($1); }
5959
| note_statement { yy.parser.yy.addSignal($1); }
60-
| 'title' message { yy.parser.yy.setTitle($2); }
60+
| 'title' message { yy.parser.yy.setTitle($2, yylineno); }
6161
;
6262

6363
note_statement
64-
: 'note' placement actor message { $$ = new Diagram.Note($3, $2, $4); }
65-
| 'note' 'over' actor_pair message { $$ = new Diagram.Note($3, Diagram.PLACEMENT.OVER, $4); }
64+
: 'note' placement actor message { $$ = new Diagram.Note($3, $2, $4, yylineno); }
65+
| 'note' 'over' actor_pair message { $$ = new Diagram.Note($3, Diagram.PLACEMENT.OVER, $4, yylineno); }
6666
;
6767

6868
actor_pair
@@ -77,15 +77,15 @@ placement
7777

7878
signal
7979
: actor signaltype actor message
80-
{ $$ = new Diagram.Signal($1, $2, $3, $4); }
80+
{ $$ = new Diagram.Signal($1, $2, $3, $4, yylineno); }
8181
;
8282

8383
actor
8484
: ACTOR { $$ = yy.parser.yy.getActor(Diagram.unescape($1)); }
8585
;
8686

8787
actor_alias
88-
: ACTOR { $$ = yy.parser.yy.getActorWithAlias(Diagram.unescape($1)); }
88+
: ACTOR { $$ = yy.parser.yy.getActorWithAlias(Diagram.unescape($1), yylineno); }
8989
;
9090

9191
signaltype

src/theme-snap.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,17 @@ if (typeof Snap != 'undefined') {
155155
},
156156

157157
// Finishes the group, and returns the <group> element
158-
finishGroup: function() {
159-
var g = this.paper_.group.apply(this.paper_, this._stack);
160-
this.beginGroup(); // Reset the group
161-
return g;
162-
},
158+
finishGroup: function(groupName, lineno) {
159+
var g = this.paper_.group.apply(this.paper_, this._stack);
160+
this.beginGroup();
161+
if (groupName) {
162+
g.addClass(groupName);
163+
}
164+
if (lineno !== undefined) {
165+
g.attr({'data-lineno': lineno + 1}); // +1 to correct jison line numbering
166+
}
167+
return g;
168+
},
163169

164170
createText: function(text, font) {
165171
text = _.invoke(text.split('\n'), 'trim');
@@ -220,31 +226,31 @@ if (typeof Snap != 'undefined') {
220226
drawTitle: function() {
221227
this.beginGroup();
222228
BaseTheme.prototype.drawTitle.call(this);
223-
return this.finishGroup().addClass('title');
229+
return this.finishGroup('title', this.title_ ? this.title_.lineno : undefined);
224230
},
225231

226232
drawActor: function(actor, offsetY, height) {
227233
this.beginGroup();
228234
BaseTheme.prototype.drawActor.call(this, actor, offsetY, height);
229-
return this.finishGroup().addClass('actor');
235+
return this.finishGroup('actor', actor.lineno);
230236
},
231237

232238
drawSignal: function(signal, offsetY) {
233239
this.beginGroup();
234240
BaseTheme.prototype.drawSignal.call(this, signal, offsetY);
235-
return this.finishGroup().addClass('signal');
241+
return this.finishGroup('signal', signal.lineno);
236242
},
237243

238244
drawSelfSignal: function(signal, offsetY) {
239245
this.beginGroup();
240246
BaseTheme.prototype.drawSelfSignal.call(this, signal, offsetY);
241-
return this.finishGroup().addClass('signal');
247+
return this.finishGroup('signal', signal.lineno);
242248
},
243249

244250
drawNote: function(note, offsetY) {
245251
this.beginGroup();
246252
BaseTheme.prototype.drawNote.call(this, note, offsetY);
247-
return this.finishGroup().addClass('note');
253+
return this.finishGroup('note', note.lineno);
248254
},
249255
});
250256

src/theme.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ _.extend(BaseTheme.prototype, {
185185
// Setup some layout stuff
186186
if (diagram.title) {
187187
var title = this.title_ = {};
188-
var bb = this.textBBox(diagram.title, font);
188+
title.message = diagram.title.message;
189+
title.lineno = diagram.title.lineno;
190+
var bb = this.textBBox(title.message, font);
189191
title.textBB = bb;
190-
title.message = diagram.title;
191192

192193
title.width = bb.width + (TITLE_PADDING + TITLE_MARGIN) * 2;
193194
title.height = bb.height + (TITLE_PADDING + TITLE_MARGIN) * 2;

test/grammar-tests.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ test('Dashed Open Arrow', function() {
111111
});
112112

113113
test('Titles', function() {
114-
equal(Diagram.parse('Title: title').title, 'title', 'Title');
115-
equal(Diagram.parse('Title: line1\\nline2').title, 'line1\nline2', 'Multiline Title');
114+
deepEqual(Diagram.parse('Title: title').title, {message: 'title', lineno: 0}, 'Title');
115+
deepEqual(Diagram.parse('Title: line1\\nline2').title, {message: 'line1\nline2', lineno: 0}, 'Multiline Title');
116116
});
117117

118118
test('Unicode', function() {
119-
equal(Diagram.parse('Title: 中国').title, '中国', 'Unicode Title');
119+
deepEqual(Diagram.parse('Title: 中国').title, {message: '中国', lineno: 0}, 'Unicode Title');
120120
assertEmptyDocument(Diagram.parse('# 中国'));
121121
assertSingleActor(Diagram.parse('Participant 中国'), '中国');
122122
assertSingleActor(Diagram.parse('Participant 中国 as alias'), 'alias', '中国');
@@ -149,7 +149,7 @@ test('Comments', function() {
149149
assertSingleArrow(Diagram.parse('A->B: Message # not a comment'), ARROWTYPE.FILLED,
150150
LINETYPE.SOLID, 'A', 'B', 'Message # not a comment');
151151

152-
equal(Diagram.parse('Title: title # not a comment').title, 'title # not a comment');
152+
deepEqual(Diagram.parse('Title: title # not a comment').title, {message: 'title # not a comment', lineno: 0});
153153
assertSingleNote(Diagram.parse('note left of A: Message # not a comment'), PLACEMENT.LEFTOF, 'A',
154154
'Message # not a comment');
155155
});

0 commit comments

Comments
 (0)