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 1 commit
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
12 changes: 12 additions & 0 deletions components/editor/ftd/ftd.grammar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@top ImportCommand { "--" "import" ":" Name }
Copy link
Member

Choose a reason for hiding this comment

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

You can create p1 level grammar only, try to port our old grammar, instead of creating full parser.

Copy link
Member

Choose a reason for hiding this comment

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

I would love full parser, but it would be more work. Also the language support for fastn should be in fastn-stack org, so others can also use it.


Name {
identifier
}

@tokens {
identifier { $[a-zA-Z0-9_\-]+ }
// QuotedString { '"' (!["\\] | "\\" _)* '"' }
LineComment { ";;" ![\n]* }
// space { $[ \t\n\r]+ }
}
42 changes: 42 additions & 0 deletions components/editor/ftd/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

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({
Name: t.literal
})
]
}),
});

// 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