Skip to content

Commit

Permalink
fix: Improve syntax highlighting for enums and annotations. (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sneeringer authored Jun 3, 2021
1 parent be71b1f commit 9f6be8c
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions aip_site/support/assets/js/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,52 @@ $.when($.ready).then(() => {
.prev()
.addClass('nk');

// The option keyword is always followed by the annotation name.
$('.language-proto .k:contains(option)').each((_, el) => {
$(el).nextAll('.n').eq(0).addClass('protobuf-annotation');
});
// Split up the beginning punctuation `[(` for field annotations, putting
// them in separate <span>s so we can recolor only the latter.
$('.language-proto .p')
.filter((_, el) => $(el).text() === '[(')
.each((_, el) => {
$(el).text('[');
$('<span class="p">(</span>').insertAfter($(el));
});

// Designate protobuf annotations, which follow a consistent pattern with
// paerentheses.
$('.language-proto .p')
.filter((_, el) => $(el).text() === '(')
.each((_, el) => {
let open = $(el); // (
let ann = open.next(); // google.api.foo
let close = ann.next(); // )

// Sanity check: Does this really look like an annotation?
//
// This checks the existing classes that Pygments uses for proto
// annotations, to ensure we do not get false positive matches
// (since the starting match is just a span with `(` in it).
let conditions = [
ann.hasClass('n'),
!ann.hasClass('nc'),
close.hasClass('p'),
close.text() === ')',
close.next('.o').text() === '=',
];
if (!conditions.every((v) => v === true)) {
return;
}

// This is an annotation, color it accordingly.
for (let e of [open, ann, close]) {
e.addClass('protobuf-annotation');
}
});

// Highlight constants.
$('.language-proto .o + .n:not(.nc)')
.filter((_, el) => /^[A-Z0-9_]+$/.test($(el).text()))
.each((_, el) => {
$(el).addClass('mi').removeClass('n');
});

// ----------------
// -- TypeScript --
Expand Down

0 comments on commit 9f6be8c

Please sign in to comment.