Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Astro mode syntax highlighting #5269

Merged
merged 7 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions demo/kitchen-sink/docs/astro.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
const visible = true;
const name = "Legend Sabbir"
---
{visible && <p>Show me!</p>}

{visible ? <p>Show me!</p> : <p>Else show me!</p>}

<h1>{name}</h1>
1 change: 1 addition & 0 deletions src/ext/modelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var supportedModes = {
AsciiDoc: ["asciidoc|adoc"],
ASL: ["dsl|asl|asl.json"],
Assembly_x86:["asm|a"],
Astro: ["astro"],
AutoHotKey: ["ahk"],
BatchFile: ["bat|cmd"],
BibTeX: ["bib"],
Expand Down
20 changes: 20 additions & 0 deletions src/mode/astro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

var oop = require("../lib/oop");
var HtmlMode = require("./html").Mode;
var AstroHighlightRules = require("./astro_highlight_rules").AstroHighlightRules;
var HtmlBehaviour = require("./behaviour/html").HtmlBehaviour;

var Mode = function() {
HtmlMode.call(this);
this.HighlightRules = AstroHighlightRules;
this.$behaviour = new HtmlBehaviour();
};

oop.inherits(Mode, HtmlMode);

(function() {
this.$id = "ace/mode/astro";
}).call(Mode.prototype);

exports.Mode = Mode;
100 changes: 100 additions & 0 deletions src/mode/astro_highlight_rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"use strict";

var oop = require("../lib/oop");
var HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
var JavascriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules;

var AstroHighlightRules = function () {
HtmlHighlightRules.call(this);

var astro = {
token: "paren.quasi.start.astro.level3",
regex: /{/,
next: "inline-js-start"
};

for (var key in this.$rules) {
if (key.startsWith("js") || key.startsWith("css") || key.startsWith("comment")) continue;
this.$rules[key].unshift(astro);
}

this.$rules.start.unshift({
token: "comment",
regex: "---",
onMatch: function (value, state, stack) {
stack.splice(0);
return this.token;

Check warning on line 26 in src/mode/astro_highlight_rules.js

View check run for this annotation

Codecov / codecov/patch

src/mode/astro_highlight_rules.js#L24-L26

Added lines #L24 - L26 were not covered by tests
},
next: "js-start"
});

this.embedRules(JavascriptHighlightRules, "js-", [{
regex: "---",
token: "comment",
next: "start",
onMatch: function (value, state, stack) {
stack.splice(0);
return this.token;

Check warning on line 37 in src/mode/astro_highlight_rules.js

View check run for this annotation

Codecov / codecov/patch

src/mode/astro_highlight_rules.js#L35-L37

Added lines #L35 - L37 were not covered by tests
}
}]);

this.embedRules(JavascriptHighlightRules, "inline-js-", [{
regex: /}/,
token: "paren.quasi.end.astro.level3",
onMatch: function (value, state, stack) {
if (stack.length) {
if (stack.includes("inline-js-start")) {
stack.shift();
this.next = stack.shift();
if (this.next.indexOf("string") !== -1) return "paren.quasi.end";
return "paren.rparen";

Check warning on line 50 in src/mode/astro_highlight_rules.js

View check run for this annotation

Codecov / codecov/patch

src/mode/astro_highlight_rules.js#L46-L50

Added lines #L46 - L50 were not covered by tests
} else {
if (stack.includes("string.attribute-value.xml0")) {
this.next = "string.attribute-value.xml0";

Check warning on line 53 in src/mode/astro_highlight_rules.js

View check run for this annotation

Codecov / codecov/patch

src/mode/astro_highlight_rules.js#L52-L53

Added lines #L52 - L53 were not covered by tests
}
else if (stack.includes("tag_stuff")) {
this.next = "tag_stuff";

Check warning on line 56 in src/mode/astro_highlight_rules.js

View check run for this annotation

Codecov / codecov/patch

src/mode/astro_highlight_rules.js#L55-L56

Added lines #L55 - L56 were not covered by tests
}
return this.token;

Check warning on line 58 in src/mode/astro_highlight_rules.js

View check run for this annotation

Codecov / codecov/patch

src/mode/astro_highlight_rules.js#L58

Added line #L58 was not covered by tests
}
} else {
this.next = this.nextState;
return this.token;
}
},
nextState: "start"
}, {
regex: /{/,
token: "paren.lparen",
push: "inline-js-start"
}]);

var overwriteJSXendRule = function (prefix) {
for (var index in this.$rules[prefix + "jsxAttributes"]) {
if (this.$rules[prefix + "jsxAttributes"][index].token === "meta.tag.punctuation.tag-close.xml") {
this.$rules[prefix + "jsxAttributes"][index].onMatch = function (value, currentState, stack) {
if (currentState == stack[0])
stack.shift();
if (value.length == 2) {
if (stack[0] == this.nextState)
stack[1]--;
if (!stack[1] || stack[1] < 0) {
stack.splice(0, 2);

Check warning on line 82 in src/mode/astro_highlight_rules.js

View check run for this annotation

Codecov / codecov/patch

src/mode/astro_highlight_rules.js#L76-L82

Added lines #L76 - L82 were not covered by tests
}
}
this.next = stack[0] || prefix + "start";
return [{ type: this.token, value: value }];

Check warning on line 86 in src/mode/astro_highlight_rules.js

View check run for this annotation

Codecov / codecov/patch

src/mode/astro_highlight_rules.js#L85-L86

Added lines #L85 - L86 were not covered by tests
};
break;
}
}
};

overwriteJSXendRule.call(this, "js-");
overwriteJSXendRule.call(this, "inline-js-");

this.normalizeRules();
};

oop.inherits(AstroHighlightRules, HtmlHighlightRules);
exports.AstroHighlightRules = AstroHighlightRules;
Loading