summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-02-01 15:19:47 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-02-01 15:19:47 -0500
commitb376a87775d10dc7a693c0e1ecbe59e867e4634a (patch)
tree492abc20a91ae41bd089410b7082dc9cea88628a /buffer.c
parentfe4d14a5bb135a925bb47fe88b1a78df0d03cd49 (diff)
different syntax highlighting depending on the file extension
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c32
1 files changed, 30 insertions, 2 deletions
diff --git a/buffer.c b/buffer.c
index 2b9957c..fa16c20 100644
--- a/buffer.c
+++ b/buffer.c
@@ -177,6 +177,33 @@ static inline Settings const *buffer_settings(TextBuffer *buffer) {
return &buffer->ted->settings;
}
+// what programming language is this?
+Language buffer_language(TextBuffer *buffer) {
+ Settings const *settings = buffer_settings(buffer);
+ char const *filename = buffer->filename;
+ if (!filename)
+ return LANG_NONE;
+ size_t filename_len = strlen(filename);
+
+ for (u16 l = 0; l < LANG_COUNT; ++l) {
+ char const *extensions = settings->language_extensions[l];
+ if (extensions) {
+ // extensions is a string with commas separating each extension.
+ size_t len = 0;
+ for (char const *p = extensions; *p; p += len) {
+ if (*p == ',') ++p; // move past comma
+ len = strcspn(p, ",");
+ if (filename_len >= len && strncmp(&filename[filename_len - len], p, len) == 0) {
+ // found a match!
+ return (Language)l;
+ }
+ }
+ }
+ }
+ // no extensions matched
+ return LANG_NONE;
+}
+
// NOTE: this string will be invalidated when the line is edited!!!
// only use it briefly!!
static String32 buffer_get_line(TextBuffer *buffer, u32 line_number) {
@@ -1976,11 +2003,12 @@ void buffer_render(TextBuffer *buffer, Rect r) {
text_state.y -= (float)(buffer->scroll_y - start_line) * char_height;
SyntaxState syntax_state = {0};
+ Language language = buffer_language(buffer);
// dynamic array of character types, to be filled by syntax_highlight
SyntaxCharType *char_types = NULL;
for (u32 line_idx = 0; line_idx < start_line; ++line_idx) {
Line *line = &lines[line_idx];
- syntax_highlight(&syntax_state, buffer->language, line->str, line->len, NULL);
+ syntax_highlight(&syntax_state, language, line->str, line->len, NULL);
}
for (u32 line_idx = start_line; line_idx < nlines; ++line_idx) {
@@ -1988,7 +2016,7 @@ void buffer_render(TextBuffer *buffer, Rect r) {
if (arr_len(char_types) < line->len) {
arr_set_len(char_types, line->len);
}
- syntax_highlight(&syntax_state, buffer->language, line->str, line->len, char_types);
+ syntax_highlight(&syntax_state, language, line->str, line->len, char_types);
for (u32 i = 0; i < line->len; ++i) {
char32_t c = line->str[i];
SyntaxCharType type = char_types[i];