aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2021-04-21 13:21:46 +0200
committerMarvin Borner2021-04-21 13:21:46 +0200
commitf663aed8e0650234dd7b9450cc09e4f8215da41b (patch)
tree7b77399b12ff9ebf5bc1bd02f3188d24c8d09324
parentf6968de6417a5489dc94d2079768f54b3b1163b0 (diff)
"Fixed" syntax highlighting
-rw-r--r--src/gui.c3
-rw-r--r--src/syntax.c29
2 files changed, 21 insertions, 11 deletions
diff --git a/src/gui.c b/src/gui.c
index eadcb91..3288827 100644
--- a/src/gui.c
+++ b/src/gui.c
@@ -141,7 +141,7 @@ static void gui_show_save_dialog(void)
int res = gtk_dialog_run(GTK_DIALOG(dialog));
if (res == GTK_RESPONSE_ACCEPT) {
char *savefile = gtk_file_chooser_get_filename(chooser);
- strncpy(filename, savefile, sizeof(filename));
+ strcpy(filename, savefile);
g_free(savefile);
text = gui_text_buffer();
gtk_widget_destroy(dialog);
@@ -267,6 +267,7 @@ static void gui_activate(GtkApplication *app, gpointer data)
gui_fill_text_view_from_file("test.asm");
gui_init_highlighter();
+ gui_call_syntax_highlighter();
}
int gui_init(int argc, char *argv[])
diff --git a/src/syntax.c b/src/syntax.c
index d01496e..400f6e1 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -11,8 +11,13 @@ struct pos {
static void syntax_highlight_line(struct pos *pos, char *str, u32 size)
{
+ /* printf("Got size %d '", size); */
+ /* for (u32 i = 0; i < size; i++) { */
+ /* printf("%c", str[i]); */
+ /* } */
+ /* printf("'\n"); */
+
while (1) {
- /* printf("%d:%d\n", pos->y, pos->x); */
struct token tok = token_resolve(str + pos->x, size - pos->x);
if (tok.type == NEWLINE) {
@@ -29,13 +34,13 @@ static void syntax_highlight_line(struct pos *pos, char *str, u32 size)
continue;
}
- token_print(&tok);
+ /* token_print(&tok); */
- /* if (tok.type > INSTR_START && tok.type < INSTR_END) */
- /* gui_highlight(pos->x, pos->y, tok.length, "instr"); */
- /* else if (tok.type > REGS_START && tok.type < REGS_END) { */
- /* gui_highlight(pos->x, pos->y, tok.length, "regs"); */
- /* } */
+ if (tok.type > INSTR_START && tok.type < INSTR_END)
+ gui_highlight(pos->x, pos->y, tok.length, "instr");
+ else if (tok.type > REGS_START && tok.type < REGS_END) {
+ gui_highlight(pos->x, pos->y, tok.length, "regs");
+ }
pos->x += tok.length;
}
@@ -43,18 +48,22 @@ static void syntax_highlight_line(struct pos *pos, char *str, u32 size)
void syntax_highlight(char *buf, u32 size)
{
- return; // TODO: Fix some stuff
struct pos pos = { 0 };
+ u32 diff = 0;
char *start = buf;
for (u32 i = 0; i < size; i++) {
+ // TODO: Fix highlighting of last line without \n
if (buf[i] == '\0')
break;
if (buf[i] == '\n') {
pos.x = 0;
- syntax_highlight_line(&pos, start, i);
- start += i;
+ syntax_highlight_line(&pos, start, diff);
+ start += diff + 1;
+ diff = 0;
+ } else {
+ diff++;
}
}
}