Skip to content

Commit

Permalink
Add syntax highlighting support for C# (#55)
Browse files Browse the repository at this point in the history
* feat: add syntax highlighting support for C#

* update README - add C# sysntac highlighting support

* restore original indentation / formatting

* Fix syntax error in Lezer grammar

* Fix language auto detection of C#

---------

Co-authored-by: Jonatan Heyman <[email protected]>
  • Loading branch information
ahmedatia and heyman authored Dec 24, 2023
1 parent 5d7af50 commit 96440b2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Available for Mac, Windows, and Linux.
- Block-based
- Syntax highlighting
- C++
- C#
- CSS
- HTML
- Java
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@electron/asar": "^3.2.2",
"@lezer/generator": "^1.5.1",
"@lezer/markdown": "^1.1.2",
"@replit/codemirror-lang-csharp": "^6.2.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@vitejs/plugin-vue": "^4.0.0",
"debounce": "^1.2.1",
Expand Down
11 changes: 6 additions & 5 deletions public/langdetect-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ GUESSLANG_LANGUAGES = [
"xml",
"rs",
"md",
"cs",
]

const guessLang = new self.GuessLang()
Expand All @@ -26,12 +27,12 @@ onmessage = (event) => {
// we first check some custom heuristic rules to determine if the language is JSON
const trimmedContent = content.trim()
if ((
trimmedContent.startsWith("{") &&
trimmedContent.startsWith("{") &&
trimmedContent.endsWith("}")
) || (
trimmedContent.startsWith("[") &&
trimmedContent.endsWith("]")
)) {
trimmedContent.startsWith("[") &&
trimmedContent.endsWith("]")
)) {
try {
if (typeof JSON.parse(trimmedContent) === "object") {
postMessage({
Expand All @@ -52,7 +53,7 @@ onmessage = (event) => {
//let startTime = performance.now()
guessLang.runModel(content).then((result) => {
//const duration = performance.now() - startTime
//console.log("Guessing language done:", result, result[0]?.languageId, result[0]?.confidence)
console.log("Guessing language done:", result, result[0]?.languageId, result[0]?.confidence)
//console.log("Guessing language took", duration, "ms")

if (result.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/lang-heynote/heynote.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ NoteDelimiter {

@tokens {
noteDelimiterMark { "∞∞∞" }
NoteLanguage { "text" | "math" | "javascript" | "json" | "python" | "html" | "sql" | "markdown" | "java" | "php" | "css" | "xml" | "cpp" | "rust" }
NoteLanguage { "text" | "math" | "javascript" | "json" | "python" | "html" | "sql" | "markdown" | "java" | "php" | "css" | "xml" | "cpp" | "rust" | "csharp" }
Auto { "-a" }
noteDelimiterEnter { "\n" }
//NoteContent { String }
Expand Down
2 changes: 1 addition & 1 deletion src/editor/lang-heynote/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const parser = LRParser.deserialize({
maxTerm: 10,
skippedNodes: [0],
repeatNodeCount: 1,
tokenData: "'V~R[YZw}!O|#V#W!X#[#]!s#^#_#V#a#b$p#d#e%o#f#g&U#g#h&b#h#i&h#l#m!y%&x%&y&t~|OX~~!PP#T#U!S~!XOU~~![Q#d#e!b#g#h!m~!eP#d#e!h~!mOT~~!pP#g#h!h~!vP#h#i!y~!|P#a#b#P~#SP#`#a!h~#YQ#T#U#`#g#h$d~#cP#j#k#f~#iP#T#U#l~#qPT~#g#h#t~#wP#V#W#z~#}P#f#g$Q~$TP#]#^$W~$ZP#d#e$^~$aP#h#i!h~$gP#c#d$j~$mP#b#c!h~$sP#T#U$v~$yQ#f#g%P#h#i%i~%SP#_#`%V~%YP#W#X%]~%`P#c#d%c~%fP#k#l$j~%lP#[#]!h~%rQ#[#]!b#m#n%x~%{P#h#i&O~&RP#[#]$d~&XP#i#j&[~&_P#g#h$^~&eP#e#f#P~&kP#X#Y&n~&qP#l#m$^~&wP%&x%&y&z~&}P%&x%&y'Q~'VOY~",
tokenData: "'f~R[YZw}!O|#V#W!X#[#]#S#^#_#f#a#b%P#d#e&O#f#g&e#g#h&q#h#i&w#l#m#Y%&x%&y'T~|OX~~!PP#T#U!S~!XOU~~![Q#d#e!b#g#h!m~!eP#d#e!h~!mOT~~!pQ#[#]!v#g#h!h~!yP#T#U!|~#PP#f#g!b~#VP#h#i#Y~#]P#a#b#`~#cP#`#a!h~#iQ#T#U#o#g#h$s~#rP#j#k#u~#xP#T#U#{~$QPT~#g#h$T~$WP#V#W$Z~$^P#f#g$a~$dP#]#^$g~$jP#d#e$m~$pP#h#i!h~$vP#c#d$y~$|P#b#c!h~%SP#T#U%V~%YQ#f#g%`#h#i%x~%cP#_#`%f~%iP#W#X%l~%oP#c#d%r~%uP#k#l$y~%{P#[#]!h~&RQ#[#]!b#m#n&X~&[P#h#i&_~&bP#[#]$s~&hP#i#j&k~&nP#g#h$m~&tP#e#f#`~&zP#X#Y&}~'QP#l#m$m~'WP%&x%&y'Z~'^P%&x%&y'a~'fOY~",
tokenizers: [0, noteContent],
topRules: {"Document":[0,2]},
tokenPrec: 0
Expand Down
6 changes: 3 additions & 3 deletions src/editor/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { cssLanguage } from "@codemirror/lang-css"
import { cppLanguage } from "@codemirror/lang-cpp"
import { xmlLanguage } from "@codemirror/lang-xml"
import { rustLanguage } from "@codemirror/lang-rust"

import { csharpLanguage } from "@replit/codemirror-lang-csharp"

class Language {
constructor(token, name, parser, guesslang, supportsFormat=false) {
constructor(token, name, parser, guesslang, supportsFormat = false) {
this.token = token
this.name = name
this.parser = parser
Expand All @@ -39,5 +39,5 @@ export const LANGUAGES = [
new Language("xml", "XML", xmlLanguage.parser, "xml"),
new Language("cpp", "C++", cppLanguage.parser, "cpp"),
new Language("rust", "Rust", rustLanguage.parser, "rust"),
new Language("csharp", "C#", csharpLanguage.parser, "cs"),
]

0 comments on commit 96440b2

Please sign in to comment.