From 4ff4d669ccb658b8b48785d37946378a5b29688c Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 2 Feb 2021 11:26:50 -0500 Subject: big fix with buffer_pos_to_pixels also started rust highlighting --- buffer.c | 65 ++++++++++++++++++++++++++++++++-------------------------------- syntax.c | 47 ++++++++++++++++++++++++++++++++++------------ ted.c | 2 +- ted.h | 20 ++++++++++---------- test.rs | 23 +++++++++++++++++++++++ ui.c | 2 +- 6 files changed, 102 insertions(+), 57 deletions(-) create mode 100644 test.rs diff --git a/buffer.c b/buffer.c index 71e0f1e..53d8201 100644 --- a/buffer.c +++ b/buffer.c @@ -820,14 +820,14 @@ void buffer_text_dimensions(TextBuffer *buffer, u32 *lines, u32 *columns) { } -// returns the number of rows of text that can fit in the buffer, rounded down. -int buffer_display_lines(TextBuffer *buffer) { - return (int)((buffer->y2 - buffer->y1) / text_font_char_height(buffer_font(buffer))); +// returns the number of rows of text that can fit in the buffer +float buffer_display_lines(TextBuffer *buffer) { + return (buffer->y2 - buffer->y1) / text_font_char_height(buffer_font(buffer)); } -// returns the number of columns of text that can fit in the buffer, rounded down. -int buffer_display_cols(TextBuffer *buffer) { - return (int)((buffer->x2 - buffer->x1) / text_font_char_width(buffer_font(buffer))); +// returns the number of columns of text that can fit in the buffer +float buffer_display_cols(TextBuffer *buffer) { + return (buffer->x2 - buffer->x1) / text_font_char_width(buffer_font(buffer)); } // make sure we don't scroll too far @@ -860,11 +860,11 @@ void buffer_scroll(TextBuffer *buffer, double dx, double dy) { } void buffer_page_up(TextBuffer *buffer, i64 npages) { - buffer_scroll(buffer, 0, (double)(-npages * buffer_display_lines(buffer))); + buffer_scroll(buffer, 0, (double)-npages * buffer_display_lines(buffer)); } void buffer_page_down(TextBuffer *buffer, i64 npages) { - buffer_scroll(buffer, 0, (double)(+npages * buffer_display_lines(buffer))); + buffer_scroll(buffer, 0, (double)+npages * buffer_display_lines(buffer)); } // returns the position of the character at the given position in the buffer. @@ -874,7 +874,7 @@ v2 buffer_pos_to_pixels(TextBuffer *buffer, BufferPos pos) { u32 col = buffer_index_to_column(buffer, line, index); Font *font = buffer_font(buffer); float x = (float)((double)col - buffer->scroll_x) * text_font_char_width(font) + buffer->x1; - float y = (float)((double)line - buffer->scroll_y + 0.2f /* nudge */) * text_font_char_height(font) + buffer->y1; + float y = (float)((double)line - buffer->scroll_y) * text_font_char_height(font) + buffer->y1; return V2(x, y); } @@ -895,7 +895,7 @@ bool buffer_pixels_to_pos(TextBuffer *buffer, v2 pixel_coords, BufferPos *pos) { display_col = 0; ret = false; } - int display_cols = buffer_display_cols(buffer), display_lines = buffer_display_lines(buffer); + double display_cols = buffer_display_cols(buffer), display_lines = buffer_display_lines(buffer); if (display_col >= display_cols) { display_col = display_cols - 1; ret = false; @@ -940,24 +940,24 @@ static bool buffer_clip_rect(TextBuffer *buffer, Rect *r) { // if the cursor is offscreen, this will scroll to make it onscreen. static void buffer_scroll_to_cursor(TextBuffer *buffer) { Settings const *settings = buffer_settings(buffer); - i64 cursor_line = buffer->cursor_pos.line; - i64 cursor_col = buffer_index_to_column(buffer, (u32)cursor_line, buffer->cursor_pos.index); - i64 display_lines = buffer_display_lines(buffer); - i64 display_cols = buffer_display_cols(buffer); + double cursor_line = buffer->cursor_pos.line; + double cursor_col = buffer_index_to_column(buffer, (u32)cursor_line, buffer->cursor_pos.index); + double display_lines = buffer_display_lines(buffer); + double display_cols = buffer_display_cols(buffer); double scroll_x = buffer->scroll_x, scroll_y = buffer->scroll_y; - i64 scrolloff = settings->scrolloff; + double scrolloff = settings->scrolloff; // scroll left if cursor is off screen in that direction - double max_scroll_x = (double)(cursor_col - scrolloff); + double max_scroll_x = cursor_col - scrolloff; scroll_x = mind(scroll_x, max_scroll_x); // scroll right - double min_scroll_x = (double)(cursor_col - display_cols + scrolloff); + double min_scroll_x = cursor_col - display_cols + scrolloff; scroll_x = maxd(scroll_x, min_scroll_x); // scroll up - double max_scroll_y = (double)(cursor_line - scrolloff); + double max_scroll_y = cursor_line - scrolloff; scroll_y = mind(scroll_y, max_scroll_y); // scroll down - double min_scroll_y = (double)(cursor_line - display_lines + scrolloff); + double min_scroll_y = cursor_line - display_lines + scrolloff; scroll_y = maxd(scroll_y, min_scroll_y); buffer->scroll_x = scroll_x; @@ -967,13 +967,13 @@ static void buffer_scroll_to_cursor(TextBuffer *buffer) { // scroll so that the cursor is in the center of the screen void buffer_center_cursor(TextBuffer *buffer) { - i64 cursor_line = buffer->cursor_pos.line; - i64 cursor_col = buffer_index_to_column(buffer, (u32)cursor_line, buffer->cursor_pos.index); - i64 display_lines = buffer_display_lines(buffer); - i64 display_cols = buffer_display_cols(buffer); + double cursor_line = buffer->cursor_pos.line; + double cursor_col = buffer_index_to_column(buffer, (u32)cursor_line, buffer->cursor_pos.index); + double display_lines = buffer_display_lines(buffer); + double display_cols = buffer_display_cols(buffer); - buffer->scroll_x = (double)(cursor_col - display_cols / 2); - buffer->scroll_y = (double)(cursor_line - display_lines / 2); + buffer->scroll_x = cursor_col - display_cols * 0.5; + buffer->scroll_y = cursor_line - display_lines * 0.5; buffer_correct_scroll(buffer); } @@ -1901,10 +1901,6 @@ void buffer_render(TextBuffer *buffer, Rect r) { float border_thickness = settings->border_thickness; - // get screen coordinates of cursor - v2 cursor_display_pos = buffer_pos_to_pixels(buffer, buffer->cursor_pos); - // the rectangle that the cursor is rendered as - Rect cursor_rect = rect(cursor_display_pos, V2(settings->cursor_width, char_height)); u32 border_color = colors[COLOR_BORDER]; // color of border around buffer @@ -1918,6 +1914,12 @@ void buffer_render(TextBuffer *buffer, Rect r) { x2 -= border_thickness * 0.5f; y2 -= border_thickness * 0.5f; + + // get screen coordinates of cursor + v2 cursor_display_pos = buffer_pos_to_pixels(buffer, buffer->cursor_pos); + // the rectangle that the cursor is rendered as + Rect cursor_rect = rect(cursor_display_pos, V2(settings->cursor_width, char_height)); + TextRenderState text_state = { .x = 0, .y = 0, .min_x = x1, .min_y = y1, @@ -1993,15 +1995,12 @@ void buffer_render(TextBuffer *buffer, Rect r) { text_chars_begin(font); text_state = (TextRenderState){ - .x = render_start_x, .y = y1 + 0.25f * text_font_char_height(font), + .x = render_start_x, .y = y1, .min_x = x1, .min_y = y1, .max_x = x2, .max_y = y2, .render = true }; - // sel_pos >= scrolloff - // sel - scroll >= scrolloff - // scroll <= sel - scrolloff text_state.y -= (float)(buffer->scroll_y - start_line) * char_height; Language language = buffer_language(buffer); diff --git a/syntax.c b/syntax.c index b7ed327..4f3864c 100644 --- a/syntax.c +++ b/syntax.c @@ -42,18 +42,17 @@ static inline bool keyword_matches(char32_t *text, size_t len, char const *keywo static void syntax_highlight_c_cpp(SyntaxState *state_ptr, bool cpp, char32_t *line, u32 line_len, SyntaxCharType *char_types) { SyntaxState state = *state_ptr; - bool in_preprocessor = (state & SYNTAX_STATE_PREPROCESSOR) != 0; - bool in_string = (state & SYNTAX_STATE_STRING) != 0; - bool in_single_line_comment = (state & SYNTAX_STATE_SINGLE_LINE_COMMENT) != 0; - bool in_multi_line_comment = (state & SYNTAX_STATE_MULTI_LINE_COMMENT) != 0; - bool in_raw_string = (state & SYNTAX_STATE_RAW_STRING); + bool in_preprocessor = (state & SYNTAX_STATE_CPP_PREPROCESSOR) != 0; + bool in_string = (state & SYNTAX_STATE_CPP_STRING) != 0; + bool in_single_line_comment = (state & SYNTAX_STATE_CPP_SINGLE_LINE_COMMENT) != 0; + bool in_multi_line_comment = (state & SYNTAX_STATE_CPP_MULTI_LINE_COMMENT) != 0; + bool in_raw_string = (state & SYNTAX_STATE_CPP_RAW_STRING); bool in_char = false; bool in_number = false; bool raw_string_ending = false; int backslashes = 0; for (u32 i = 0; i < line_len; ++i) { - SyntaxCharType type = SYNTAX_NORMAL; // necessary for the final " of a string to be highlighted bool in_string_now = in_string; bool in_char_now = in_char; @@ -160,7 +159,7 @@ static void syntax_highlight_c_cpp(SyntaxState *state_ptr, bool cpp, char32_t *l if (keyword) { // it's a keyword // let's highlight all of it now - type = SYNTAX_KEYWORD; + SyntaxCharType type = SYNTAX_KEYWORD; if (isupper(keyword[0]) || (keyword_len == 4 && streq(keyword, "true")) || (keyword_len == 5 && streq(keyword, "false")) || @@ -186,6 +185,7 @@ static void syntax_highlight_c_cpp(SyntaxState *state_ptr, bool cpp, char32_t *l } if (char_types && !dealt_with) { + SyntaxCharType type = SYNTAX_NORMAL; if (in_single_line_comment || in_multi_line_comment_now) type = SYNTAX_COMMENT; else if (in_string_now) @@ -201,11 +201,34 @@ static void syntax_highlight_c_cpp(SyntaxState *state_ptr, bool cpp, char32_t *l } } *state_ptr = (SyntaxState)( - (backslashes && in_single_line_comment) << SYNTAX_STATE_SINGLE_LINE_COMMENT_SHIFT - | (backslashes && in_preprocessor) << SYNTAX_STATE_PREPROCESSOR_SHIFT - | (backslashes && in_string) << SYNTAX_STATE_STRING_SHIFT - | in_multi_line_comment << SYNTAX_STATE_MULTI_LINE_COMMENT_SHIFT) - | in_raw_string << SYNTAX_STATE_RAW_STRING_SHIFT; + ((backslashes && in_single_line_comment) * SYNTAX_STATE_CPP_SINGLE_LINE_COMMENT) + | ((backslashes && in_preprocessor) * SYNTAX_STATE_CPP_PREPROCESSOR) + | ((backslashes && in_string) * SYNTAX_STATE_CPP_STRING) + | (in_multi_line_comment * SYNTAX_STATE_CPP_MULTI_LINE_COMMENT) + | (in_raw_string * SYNTAX_STATE_CPP_RAW_STRING) + ); +} + +static void syntax_highlight_rust(SyntaxState *state, char32_t *line, u32 line_len, SyntaxCharType *char_types) { + u8 comment_depth = (u8)((*state & SYNTAX_STATE_RUST_COMMENT_DEPTH_MASK) / SYNTAX_STATE_RUST_COMMENT_DEPTH_MUL); + for (u32 i = 0; i < line_len; ++i) { + char32_t c = line[i]; + bool dealt_with = false; + switch (c) { + + } + if (char_types && !dealt_with) { + SyntaxCharType type = SYNTAX_NORMAL; + char_types[i] = type; + } + } + + uint max_comment_depth = (1u<= max_comment_depth) + comment_depth = (u8)max_comment_depth; + *state = (SyntaxState)( + (comment_depth * SYNTAX_STATE_RUST_COMMENT_DEPTH_MUL) + ); } // This is the main syntax highlighting function. It will determine which colors to use for each character. diff --git a/ted.c b/ted.c index 65f4dc5..73e0965 100644 --- a/ted.c +++ b/ted.c @@ -168,7 +168,7 @@ static Status ted_open_buffer(Ted *ted, u16 *buffer_idx, u16 *tab) { // Returns true on success static bool ted_open_file(Ted *ted, char const *filename) { u16 buffer_idx, tab_idx; - if (buffer_is_untitled(ted->active_buffer) && buffer_empty(ted->active_buffer)) { + if (ted->active_buffer && buffer_is_untitled(ted->active_buffer) && buffer_empty(ted->active_buffer)) { // the active buffer is just an empty untitled buffer. open it here. return buffer_load_file(ted->active_buffer, filename); } else if (ted_open_buffer(ted, &buffer_idx, &tab_idx)) { diff --git a/ted.h b/ted.h index 6558842..5018879 100644 --- a/ted.h +++ b/ted.h @@ -4,20 +4,20 @@ #define TEXT_SIZE_MIN 6 #define TEXT_SIZE_MAX 70 +// these all say "CPP" but really they're C/C++ enum { - SYNTAX_STATE_SINGLE_LINE_COMMENT_SHIFT, - SYNTAX_STATE_MULTI_LINE_COMMENT_SHIFT, - SYNTAX_STATE_PREPROCESSOR_SHIFT, - SYNTAX_STATE_STRING_SHIFT, - SYNTAX_STATE_RAW_STRING_SHIFT, + SYNTAX_STATE_CPP_MULTI_LINE_COMMENT = 0x1u, // are we in a multi-line comment? (delineated by /* */) + SYNTAX_STATE_CPP_SINGLE_LINE_COMMENT = 0x2u, // if you add a \ to the end of a single-line comment, it is continued to the next line. + SYNTAX_STATE_CPP_PREPROCESSOR = 0x4u, // similar to above + SYNTAX_STATE_CPP_STRING = 0x8u, + SYNTAX_STATE_CPP_RAW_STRING = 0x10u, }; enum { - SYNTAX_STATE_MULTI_LINE_COMMENT = 1< Result<()> { + let file = File::open("test.rs")?; + let mut reader = BufReader::new(file); + loop { + let mut line = String::new(); + if reader.read_line(&mut line)? == 0 { + // reached end of file + break; + } + print!("{}", line); + } + print!(" + string + "); + Ok(()) +} diff --git a/ui.c b/ui.c index 3c16be5..310f725 100644 --- a/ui.c +++ b/ui.c @@ -428,7 +428,7 @@ static void file_selector_render(Ted *ted, FileSelector *fs) { y1 += char_height + padding; // search buffer - float line_buffer_height = char_height * 1.5f; + float line_buffer_height = char_height; buffer_render(&ted->line_buffer, rect4(x1, y1, x2, y1 + line_buffer_height)); y1 += line_buffer_height; -- cgit v1.2.3