Skip to content

Commit

Permalink
feat(highlight.js): New stuff (PRQL#3550)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
vanillajonathan and pre-commit-ci[bot] authored Sep 20, 2023
1 parent b0f1f00 commit 5fbc983
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 22 deletions.
89 changes: 78 additions & 11 deletions web/book/highlight-prql.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
Language: PRQL
Description: PRQL is a modern language for transforming data — a simple, powerful, pipelined SQL replacement.
Category: common, database
Website: https://prql-lang.org/
*/

// Syntax highlighting for PRQL.

// Keep consistent with
Expand All @@ -20,6 +27,30 @@
// - Can we represent the inner s & f string items?

formatting = function (hljs) {
const BUILTIN_FUNCTIONS = [
"any",
"average",
"concat_array",
"count",
"every",
"max",
"min",
"stddev",
"sum",
];

const DATATYPES = [
"bool",
"float",
"int",
"int8",
"int16",
"int32",
"int64",
"text",
"timestamp",
];

const TRANSFORMS = [
"aggregate",
"append",
Expand All @@ -35,16 +66,31 @@ formatting = function (hljs) {
"union",
"window",
];
const BUILTIN_FUNCTIONS = ["case", "in", "as"];
const KEYWORDS = ["let", "prql", "into"];

const KEYWORDS = ["let", "prql", "into", "case", "in", "as", "module"];

const CHAR_ESCAPE = {
scope: "char.escape",
match: /\\\\|\\([bfnrt]|u\d{4})/,
};

return {
name: "PRQL",
case_insensitive: true,
keywords: {
built_in: BUILTIN_FUNCTIONS,
keyword: [...TRANSFORMS, ...BUILTIN_FUNCTIONS, ...KEYWORDS],
literal: "false true null ",
literal: "false true null",
type: DATATYPES,
},
contains: [
{
// docblock
begin: "#!",
end: "$",
subLanguage: "markdown",
relevance: 0,
},
hljs.COMMENT("#", "$"),
{
// named arg
Expand All @@ -53,6 +99,11 @@ formatting = function (hljs) {
end: "",
relevance: 10,
},
{
// meta prql for target and version
scope: "meta",
match: /^prql/,
},
// This seems much too strong at the moment, so disabling. I think ideally
// we'd have it for aliases but not assigns.
// {
Expand All @@ -64,16 +115,31 @@ formatting = function (hljs) {
{
// date
scope: "string",
match: /@(\d*|-|\.\d|:)+/,
match: /@(\d*|-|\.\d|:|T)+Z?/,
relevance: 10,
},
{
// interval
scope: "string",
// Add more as needed
match: /\d+(days|hours|minutes|seconds|milliseconds)/,
match:
/\d+(years|months|weeks|days|hours|minutes|seconds|milliseconds|microseconds)/,
relevance: 10,
},
{
scope: "string",
relevance: 10,
variants: [
{
begin: 'r"""',
end: '"""',
},
{
begin: 'r"',
end: '"',
},
],
},
{
// interpolation strings: s-strings are variables and f-strings are
// strings? (Though possibly that's too cute, open to adjusting)
Expand All @@ -82,11 +148,11 @@ formatting = function (hljs) {
relevance: 10,
variants: [
{
begin: '(s)"""',
begin: 's"""',
end: '"""',
},
{
begin: '(s)"',
begin: 's"',
end: '"',
},
],
Expand All @@ -107,15 +173,16 @@ formatting = function (hljs) {
relevance: 10,
variants: [
{
begin: '(f)"""',
begin: 'f"""',
end: '"""',
},
{
begin: '(f)"',
begin: 'f"',
end: '"',
},
],
contains: [
CHAR_ESCAPE,
{
scope: "variable",
begin: "f",
Expand Down Expand Up @@ -159,12 +226,12 @@ formatting = function (hljs) {
end: "'",
},
],
contains: [CHAR_ESCAPE],
},
{ scope: "punctuation", match: /[\[\]{}(),]/ },
{
scope: "operator",
match:
/(>)|(<)|(==)|(\+)|(\-)|(\/)|(\*)|(!=)|(->)|(=>)|(<=)|(>=)|(&&)|(\|\|)/,
match: /==|~=|\+|\-|\/|\*|!=|->|=>|<=|>=|&&|\|\||<|>/,
relevance: 10,
},
{
Expand Down
89 changes: 78 additions & 11 deletions web/website/themes/prql-theme/static/plugins/highlight/prql.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
Language: PRQL
Description: PRQL is a modern language for transforming data — a simple, powerful, pipelined SQL replacement.
Category: common, database
Website: https://prql-lang.org/
*/

// !!keep consistent with
// https://github.com/PRQL/prql/blob/main/reference/highlight-prql.js
//
Expand All @@ -8,6 +15,30 @@
// Possibly we can even import parts at runtime, simplifying this file?

formatting = function (hljs) {
const BUILTIN_FUNCTIONS = [
"any",
"average",
"concat_array",
"count",
"every",
"max",
"min",
"stddev",
"sum",
];

const DATATYPES = [
"bool",
"float",
"int",
"int8",
"int16",
"int32",
"int64",
"text",
"timestamp",
];

const TRANSFORMS = [
"aggregate",
"append",
Expand All @@ -23,16 +54,31 @@ formatting = function (hljs) {
"union",
"window",
];
const BUILTIN_FUNCTIONS = ["case", "in", "as"];
const KEYWORDS = ["let", "prql", "into"];

const KEYWORDS = ["let", "prql", "into", "case", "in", "as", "module"];

const CHAR_ESCAPE = {
scope: "char.escape",
match: /\\\\|\\([bfnrt]|u\d{4})/,
};

return {
name: "PRQL",
case_insensitive: true,
keywords: {
built_in: BUILTIN_FUNCTIONS,
keyword: [...TRANSFORMS, ...BUILTIN_FUNCTIONS, ...KEYWORDS],
literal: "false true null ",
literal: "false true null",
type: DATATYPES,
},
contains: [
{
// docblock
begin: "#!",
end: "$",
subLanguage: "markdown",
relevance: 0,
},
hljs.COMMENT("#", "$"),
{
// named arg
Expand All @@ -41,6 +87,11 @@ formatting = function (hljs) {
end: "",
relevance: 10,
},
{
// meta prql for target and version
scope: "meta",
match: /^prql/,
},
// This seems much too strong at the moment, so disabling. I think ideally
// we'd have it for aliases but not assigns.
// {
Expand All @@ -52,16 +103,31 @@ formatting = function (hljs) {
{
// date
scope: "string",
match: /@(\d*|-|\.\d|:)+/,
match: /@(\d*|-|\.\d|:|T)+Z?/,
relevance: 10,
},
{
// interval
scope: "string",
// Add more as needed
match: /\d+(days|hours|minutes|seconds|milliseconds)/,
match:
/\d+(years|months|weeks|days|hours|minutes|seconds|milliseconds|microseconds)/,
relevance: 10,
},
{
scope: "string",
relevance: 10,
variants: [
{
begin: 'r"""',
end: '"""',
},
{
begin: 'r"',
end: '"',
},
],
},
{
// interpolation strings: s-strings are variables and f-strings are
// strings? (Though possibly that's too cute, open to adjusting)
Expand All @@ -70,11 +136,11 @@ formatting = function (hljs) {
relevance: 10,
variants: [
{
begin: '(s)"""',
begin: 's"""',
end: '"""',
},
{
begin: '(s)"',
begin: 's"',
end: '"',
},
],
Expand All @@ -95,15 +161,16 @@ formatting = function (hljs) {
relevance: 10,
variants: [
{
begin: '(f)"""',
begin: 'f"""',
end: '"""',
},
{
begin: '(f)"',
begin: 'f"',
end: '"',
},
],
contains: [
CHAR_ESCAPE,
{
scope: "variable",
begin: "f",
Expand Down Expand Up @@ -147,12 +214,12 @@ formatting = function (hljs) {
end: "'",
},
],
contains: [CHAR_ESCAPE],
},
{ scope: "punctuation", match: /[\[\]{}(),]/ },
{
scope: "operator",
match:
/(>)|(<)|(==)|(\+)|(\-)|(\/)|(\*)|(!=)|(->)|(=>)|(<=)|(>=)|(&&)|(\|\|)/,
match: /==|~=|\+|\-|\/|\*|!=|->|=>|<=|>=|&&|\|\||<|>/,
relevance: 10,
},
{
Expand Down

0 comments on commit 5fbc983

Please sign in to comment.