Skip to content

Commit

Permalink
chore: apply prettier to grammar code
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-cristino committed Sep 13, 2024
1 parent 9077754 commit 35b03bc
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 131 deletions.
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ pkg/*/node_modules/
!/pkg/**/*.test.ts

!docs/.vitepress/config.mts

# grammar
!/grammar/src/index.ts
!/grammar/utils/prepare_complete.mjs
30 changes: 15 additions & 15 deletions grammar/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import typescript from "@rollup/plugin-typescript"
import {lezer} from "@lezer/generator/rollup"
import typescript from '@rollup/plugin-typescript';
import { lezer } from '@lezer/generator/rollup';

export default {
input: "src/index.ts",
external: id => id != "tslib" && !/^(\.?\/|\w:)/.test(id),
output: [
{file: "dist/index.cjs", format: "cjs"},
{dir: "./dist", format: "es"}
],
plugins: [
typescript({
exclude: ["./utils/*"]
}),
lezer(),
]
}
input: 'src/index.ts',
external: (id) => id != 'tslib' && !/^(\.?\/|\w:)/.test(id),
output: [
{ file: 'dist/index.cjs', format: 'cjs' },
{ dir: './dist', format: 'es' },
],
plugins: [
typescript({
exclude: ['./utils/*'],
}),
lezer(),
],
};
84 changes: 44 additions & 40 deletions grammar/src/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,58 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import { CompletionContext, snippetCompletion } from "@codemirror/autocomplete";
import { fullStatementTemplates } from "./complete_statement";
import { CompletionContext, snippetCompletion } from '@codemirror/autocomplete';
import { fullStatementTemplates } from './complete_statement';

// Helper function to strip quotes from matched strings
function stripQuotes(s: string) {
return s.replace(/^'|'$/g, "");
return s.replace(/^'|'$/g, '');
}

const fullStatementSnippets = fullStatementTemplates.map((x) => {
let n = 1;
return snippetCompletion(x.label.replace(/''/g, () => `'\${${n++}:}'`), x);
return snippetCompletion(
x.label.replace(/''/g, () => `'\${${n++}:}'`),
x,
);
});

export function completeStatement(context: CompletionContext) {
const line = context.state.doc.lineAt(context.pos);
let textBefore = context.state.sliceDoc(line.from, context.pos);
const triggerMatch = /^[GT].*$/i.exec(textBefore);

if (triggerMatch) {
const strings = textBefore.match(/'([^']*)'/g);
textBefore = textBefore.toLowerCase()
if (!strings) {
return {
from: context.pos - triggerMatch[0].length,
options: fullStatementSnippets,
validFor: /^.*$/,
};
}

const strippedStrings = strings.map(stripQuotes);

const templateOption = fullStatementTemplates.map((x) => {
let n = 1;
let m = 0;
return snippetCompletion(
x.label.replace(/''/g, () => `'\${${n}:${strippedStrings[n++-1] || ""}}'`),
{
label: x.label.replace(/''/g, () => `${strings[m++] || "''"}`),
type: x.type
});
})

return {
from: context.pos - textBefore.length,
options: templateOption,
validFor: /^.*$/,
};
}

return null;
const line = context.state.doc.lineAt(context.pos);
let textBefore = context.state.sliceDoc(line.from, context.pos);
const triggerMatch = /^[GT].*$/i.exec(textBefore);

if (triggerMatch) {
const strings = textBefore.match(/'([^']*)'/g);
textBefore = textBefore.toLowerCase();
if (!strings) {
return {
from: context.pos - triggerMatch[0].length,
options: fullStatementSnippets,
validFor: /^.*$/,
};
}

const strippedStrings = strings.map(stripQuotes);

const templateOption = fullStatementTemplates.map((x) => {
let n = 1;
let m = 0;
return snippetCompletion(
x.label.replace(/''/g, () => `'\${${n}:${strippedStrings[n++ - 1] || ''}}'`),
{
label: x.label.replace(/''/g, () => `${strings[m++] || "''"}`),
type: x.type,
},
);
});

return {
from: context.pos - textBefore.length,
options: templateOption,
validFor: /^.*$/,
};
}

return null;
}
67 changes: 37 additions & 30 deletions grammar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,55 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import { parser } from "./syntax.grammar"
import { LRLanguage, LanguageSupport, HighlightStyle, syntaxHighlighting,} from "@codemirror/language"
import { styleTags, tags as t } from "@lezer/highlight"
import { completeStatement } from "./complete"
import { parser } from './syntax.grammar';
import {
LRLanguage,
LanguageSupport,
HighlightStyle,
syntaxHighlighting,
} from '@codemirror/language';
import { styleTags, tags as t } from '@lezer/highlight';
import { completeStatement } from './complete';

const syntax_colors = syntaxHighlighting(
HighlightStyle.define(
[
{ tag: t.heading, color: "purple" },
{ tag: t.heading1, color: "gray" },
{tag: t.variableName, color: "red"},
{ tag: t.keyword, color: "green" },
{tag: t.string, color: "blue"},
{tag: t.lineComment, color: "gray"},
{tag: t.heading2, color: "black"}
],
{ all: { color: "black" } }
)
);
export const SlangroomLanguage = LRLanguage.define({
[
{ tag: t.heading, color: 'purple' },
{ tag: t.heading1, color: 'gray' },
{ tag: t.variableName, color: 'red' },
{ tag: t.keyword, color: 'green' },
{ tag: t.string, color: 'blue' },
{ tag: t.lineComment, color: 'gray' },
{ tag: t.heading2, color: 'black' },
],
{ all: { color: 'black' } },
),
);

export const SlangroomLanguage = LRLanguage.define({
parser: parser.configure({
props: [
styleTags({
"given then when and in inside if endif foreach endforeach" : t.variableName,
"have send open connect print output" : t.keyword,
"rule VersionRule! GenericRule!": t.heading,
"scenario ScenarioType/... ScenarioComment!": t.heading1,
"DbAction! EthereumAction! FsAction! GitAction! HelpersAction! HttpAction! JsonSchemaAction! OAuthAction! PocketbaseAction! QrCodeAction! RedisAction! ShellAction! TimestampAction! WalletAction! ZencodeAction!": t.heading2,
'given then when and in inside if endif foreach endforeach': t.variableName,
'have send open connect print output': t.keyword,
'rule VersionRule! GenericRule!': t.heading,
'scenario ScenarioType/... ScenarioComment!': t.heading1,
'DbAction! EthereumAction! FsAction! GitAction! HelpersAction! HttpAction! JsonSchemaAction! OAuthAction! PocketbaseAction! QrCodeAction! RedisAction! ShellAction! TimestampAction! WalletAction! ZencodeAction!':
t.heading2,
StringLiteral: t.string,
Comment: t.lineComment,
})
]
}),
],
}),
languageData: {
commentTokens: { line: "#" }
}
})
commentTokens: { line: '#' },
},
});

const ac = SlangroomLanguage.data.of({
autocomplete: completeStatement
})
autocomplete: completeStatement,
});

export function Slangroom() {
return new LanguageSupport(SlangroomLanguage, [syntax_colors, ac])
return new LanguageSupport(SlangroomLanguage, [syntax_colors, ac]);
}
4 changes: 2 additions & 2 deletions grammar/src/syntax.grammar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import {LRParser} from "@lezer/lr"
import { LRParser } from '@lezer/lr';

export declare const parser: LRParser
export declare const parser: LRParser;
2 changes: 1 addition & 1 deletion grammar/src/syntax.grammar.terms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

export const eof: number
export const eof: number;
Loading

0 comments on commit 35b03bc

Please sign in to comment.