Skip to content
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
118 changes: 118 additions & 0 deletions assets/highlighting-tests/c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Comments
// Single-line comment

/*
* Multi-line
* comment
*/

// Preprocessor directives

#include <stdio.h>
#include "foo.h"
#ifndef HEADER_GUARD
#define HEADER_GUARD
#endif
#define MACRO(arg) (void)arg;

// Numbers
42;
3.14;
3.14f;
.5;
1e10;
-12L;
15lU;
0xff;
0xFF;
0b1010;
0o77;

// Constants
true;
false;
NULL;

// Strings
"double quotes with escape: \" \n \t \\";

// Control flow keywords
if (true) {
} else if (false) {
} else {
}

for (int i = 0; i < 10; i++) {
if (i == 5)
continue;
if (i == 8)
break;
}

while (false) {
}
do {
} while (false);

switch (42) {
case 1:
break;
default:
break;
}

goto cleanup;

cleanup :

int a = 1;
signed long long b = 2;
unsigned short int c = 3;
float pi = 3.14f;
double tau = 6.283F;
short foo = 42;
char c = 'c';
char newline = '\n';
const char *text = "Hello World!";

int add(int a, int b)
{
return a + b;
}

void greet(const char *name)
{
printf("Hello %s!\n", name);
}

static inline do_nothing()
{
static int a = 3;
volatile long long b = 4;
}

typedef struct {
char r;
char g;
char b;
char a;
} Color;

Color cornflower = { 100, 149, 237, 255 };
Color blue = { .b = 255, .a = 255 };

enum EntityKind {
None = 0,
Bear,
Bee,
Dog,
Cat,
};

(void)0;

int main(int argc, char *argv[])
{
greet("lhecker");
return 0;
}
9 changes: 9 additions & 0 deletions assets/highlighting-tests/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,12 @@ export function greet(name) {
def greet(name: str) -> str:
return f"hello {name}"
```

```c
#include <stdio.h>

int main() {
printf("Hello, world\n");
return 0;
}
```
2 changes: 2 additions & 0 deletions crates/edit/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,8 @@ impl TextBuffer {
HighlightKind::Method => Some(IndexedColor::BrightYellow),
HighlightKind::String => Some(IndexedColor::BrightRed),
HighlightKind::Variable => Some(IndexedColor::BrightCyan),
HighlightKind::Preprocessor => Some(IndexedColor::Cyan),
HighlightKind::TypeLanguage => Some(IndexedColor::Green),
HighlightKind::ConstantLanguage => Some(IndexedColor::BrightBlue),
HighlightKind::ConstantNumeric => Some(IndexedColor::BrightGreen),
HighlightKind::KeywordControl => Some(IndexedColor::BrightMagenta),
Expand Down
2 changes: 2 additions & 0 deletions crates/lsh-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ fn run_render(generator: lsh::compiler::Generator, path: &Path) -> anyhow::Resul
"method" => "\x1b[93m", // Bright Yellow
"string" => "\x1b[91m", // Bright Red
"variable" => "\x1b[96m", // Bright Cyan
"preprocessor" => "\x1b[36m", // Cyan

"type.language" => "\x1b[32m", // Green
"constant.language" => "\x1b[94m", // Bright Blue
"constant.numeric" => "\x1b[92m", // Bright Green
"keyword.control" => "\x1b[95m", // Bright Magenta
Expand Down
58 changes: 58 additions & 0 deletions crates/lsh/definitions/c.lsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#[display_name = "C"]
#[path = "**/*.c"]
#[path = "**/*.h"]
pub fn c() {

until /$/ {
yield other;

if /\/\/.*/ {
yield comment;
} else if /\/\*/ {
loop {
yield comment;
await input;
if /\*\// {
yield comment;
break;
}
}
} else if /#.*/ {
yield preprocessor;
} else if /"/ {
until /$/ {
yield string;
if /\\./ {}
else if /"/ { yield string; break; }
await input;
}
} else if /'/ { // technically not how '' works in C, but means we don't have to deal with stuff like '\n' manually
until /$/ {
yield string;
if /\\./ {}
else if /'/ { yield string; break; }
await input;
}
} else if /(?:break|case|continue|default|do|else|for|if|return|switch|while|goto)\>/ {
yield keyword.control;
} else if /(?:struct|enum|union|alignas|alignof|static_assert|thread_local|_Static_assert|_Thread_local|typedef|const|static|inline|volatile|sizeof|extern|register|restrict)\>/ {
yield keyword.other;
} else if /(?:void|int|long|char|float|double|signed|unsigned|short)\>/ {
yield type.language;
} else if /(?:true|false|NULL)\>/ {
yield constant.language;
} else if /(?i:-?(?:0x[\da-fA-F_]+|0b[01_]+|0o[0-7_]+|[\d_]+\.?[\d_]*|\.[\d_]+)(?:e[+-]?[\d_]+)?)(?:[fF]|[uU][lL]?|[lL][uU]?)?/ {
if /\w+/ {
// Invalid numeric literal
} else {
yield constant.numeric;
}
} else if /(\w+)\s*\(/ {
yield $1 as method;
} else if /\w+/ {
// Gobble word chars to align the next iteration on a word boundary.
}

yield other;
}
}
10 changes: 10 additions & 0 deletions crates/lsh/definitions/markdown.lsh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ pub fn markdown() {
if /.*/ {}
}
}
} else if /(?i:c)/ {
loop {
await input;
if /\s*```/ {
return;
} else {
c();
if /.*/ {}
}
}
} else if /(?i:json)/ {
loop {
await input;
Expand Down