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

Added code highlight for ftd in ide #113

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
.idea
.packages
node_modules
components/editor/command-k/parser.js
components/editor/command-k/parser.terms.js
components/editor/ftd/ftd-parser.js
components/editor/ftd/ftd-parser.terms.js
2 changes: 1 addition & 1 deletion components/editor/editor-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/editor/editor-bundle.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components/editor/editor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "./panels/package/package-content";
import {indentationMarkers} from '@replit/codemirror-indentation-markers';
import {CommandEditor} from "./command-k/command-editor";
import {ftd} from "./ftd/language";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call it ./fastn/language etc. FTD is no longer the name, we want to use fastn language terminology everywhere.



const SAVE_MODIFICATION_WAIT_TIME = 1000;
Expand Down Expand Up @@ -68,6 +69,9 @@ class CMEditor extends HTMLElement {
EditorView.updateListener.of(update),
];
switch (language) {
case 'ftd':
extensions.push(ftd());
break;
case 'JavaScript':
extensions.push(javascript());
break;
Expand Down
73 changes: 73 additions & 0 deletions components/editor/ftd/ftd.grammar
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
@top Section {
SectionDefinition HeaderDefinition*
Comment*
}




HeaderDefinition {
HeaderType? HeaderName ":" HeaderValue?
}


HeaderType {
identifier
}

HeaderName {
identifier
}

HeaderValue {
noNewLine+
}



SectionDefinition {
"--" SectionType? SectionName ":" SectionCaption?
}

SectionType {
KeywordComponent | KeywordRecord | identifier
}

SectionName {
identifier
}

SectionCaption {
noNewLine+
}

Comment {
InlineComment | BlockComment | HeaderComment
}

InlineComment {
";;" noNewLine*
}

BlockComment {
"/--" identifier "/--"
}

HeaderComment {
"/" identifier ":" noNewLine*
}

@tokens {
identifierChar { @asciiLetter | $[_$\u{a1}-\u{10ffff}] }

word { identifierChar (identifierChar | @digit)* }

identifier { word }

noNewLine { $[^\n] }

KeywordComponent { "component" }
KeywordRecord { "record" }

@precedence { KeywordComponent, KeywordRecord, identifier }
}
50 changes: 50 additions & 0 deletions components/editor/ftd/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be ideally a standalone npm package.

// Define a basic FTD grammar with "-- import:" keyword
import {LanguageSupport, LRLanguage, syntaxTree} from "@codemirror/language";
import {styleTags, tags as t} from "@lezer/highlight";
import {parser} from "./ftd.grammar";

const ftdLanguage = LRLanguage.define({
parser: parser.configure({
// Basic tokenizer for FTD import grammar
props: [
styleTags({
KeywordComponent: t.literal,
KeywordRecord: t.literal,
SectionType: t.typeName,
SectionName: t.keyword,
SectionCaption: t.string,
Comment: t.comment,
HeaderType: t.meta,
HeaderName: t.number,
HeaderValue: t.string,
})
]
}),
});

// Autocomplete support for FTD packages
const ftdAutocomplete = ftdLanguage.data.of({
autocomplete: (context) => {
let nodeBefore = syntaxTree(context.state).resolveInner(context.pos, -1);
if (nodeBefore.name == "Import") {
return {
from: nodeBefore.from,
options: [
{label: "ftd_core", type: "variable"},
{label: "ftd_ui", type: "variable"},
{label: "ftd_utils", type: "variable"},
{label: "ftd_data", type: "variable"}
]
};
}
return null;
}
});

// Create a language support extension
function ftd() {
return new LanguageSupport(ftdLanguage, [ftdAutocomplete]);
}

export {ftd}
Loading