Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 823c013

Browse files
authored
Merge pull request #14 from Jack-Punter/CursorTokenHighlight
Added cursor token occurrence highlighting.
2 parents 3ae0a8e + aa44648 commit 823c013

6 files changed

+86
-5
lines changed

4coder_fleury.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@
139139
// - fleury_color_token_highlight: The color used to underline/highlight
140140
// tokens that the cursor is over.
141141
//
142+
// - fleury_color_token_minor_highlight: The Color That is used for minor
143+
// highlights. i.e. cursor token occurance underlines.
144+
//
142145
// - fleury_color_comment_user_name: The color used to highlight the
143146
// username in comments.
144147

@@ -174,6 +177,13 @@
174177
// *compilation* window, it will display what that error is to the right of
175178
// the line. Turn it off with "f4_disable_error_annotations" in your config
176179
// file.
180+
//
181+
// - Cursor Identifier Highlight: The Identifier under the cursor is highlighted
182+
// with an underline (using the fleury_color_token_highlight color). It also
183+
// highlights all other occurances of the identifier (by string) that is
184+
// visible and syntax highlighted (occurances are highlighted using
185+
// "fleury_color_token_minor_highlight". Turn the highlight of other occurances
186+
// off with "f4_disable_cursor_token_occurance" in your config file.
177187

178188
//- @f4_calc_intro Built-In Calculator/Plotting
179189
//

4coder_fleury_colors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CUSTOM_ID(colors, fleury_color_cursor_power_mode);
2222
CUSTOM_ID(colors, fleury_color_cursor_inactive);
2323
CUSTOM_ID(colors, fleury_color_plot_cycle);
2424
CUSTOM_ID(colors, fleury_color_token_highlight);
25+
CUSTOM_ID(colors, fleury_color_token_minor_highlight);
2526
CUSTOM_ID(colors, fleury_color_comment_user_name);
2627
CUSTOM_ID(colors, fleury_color_lego_grab);
2728
CUSTOM_ID(colors, fleury_color_lego_splat);

4coder_fleury_hooks.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,65 @@ F4_RenderBuffer(Application_Links *app, View_ID view_id, Face_ID face_id,
102102
F4_RenderErrorAnnotations(app, buffer, text_layout_id, compilation_buffer);
103103
}
104104

105-
// NOTE(rjf): Token highlight
105+
// NOTE(jack): Token Occurance Highlight
106+
if (!def_get_config_b32(vars_save_string_lit("f4_disable_cursor_token_occurance")))
107+
{
108+
ProfileScope(app, "[Fleury] Token Occurance Highlight");
109+
110+
// NOTE(jack): Get the active cursor's token string
111+
Buffer_ID active_cursor_buffer = view_get_buffer(app, active_view, Access_Always);
112+
i64 active_cursor_pos = view_get_cursor_pos(app, active_view);
113+
Token_Array active_cursor_buffer_tokens = get_token_array_from_buffer(app, active_cursor_buffer);
114+
Token_Iterator_Array active_cursor_it = token_iterator_pos(0, &active_cursor_buffer_tokens, active_cursor_pos);
115+
Token *active_cursor_token = token_it_read(&active_cursor_it);
116+
117+
String_Const_u8 active_cursor_string = string_u8_litexpr("");
118+
if(active_cursor_token)
119+
{
120+
active_cursor_string = push_buffer_range(app, scratch, active_cursor_buffer, Ii64(active_cursor_token));
121+
122+
// Loop the visible tokens
123+
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
124+
i64 first_index = token_index_from_pos(&token_array, visible_range.first);
125+
Token_Iterator_Array it = token_iterator_index(0, &token_array, first_index);
126+
for (;;)
127+
{
128+
Token *token = token_it_read(&it);
129+
if(!token || token->pos >= visible_range.one_past_last)
130+
{
131+
break;
132+
}
133+
134+
if (token->kind == TokenBaseKind_Identifier)
135+
{
136+
Range_i64 token_range = Ii64(token);
137+
String_Const_u8 token_string = push_buffer_range(app, scratch, buffer, token_range);
138+
139+
// NOTE(jack) If this is the buffers cursor token, highlight it with an Underline
140+
if (range_contains(token_range, view_get_cursor_pos(app, view_id)))
141+
{
142+
F4_RenderRangeHighlight(app, view_id, text_layout_id,
143+
token_range, F4_RangeHighlightKind_Underline);
144+
}
145+
// NOTE(jack): If the token matches the active buffer token. highlight it with a Minor Underline
146+
else if(active_cursor_token->kind == TokenBaseKind_Identifier &&
147+
string_match(token_string, active_cursor_string))
148+
{
149+
F4_RenderRangeHighlight(app, view_id, text_layout_id,
150+
token_range, F4_RangeHighlightKind_MinorUnderline);
151+
152+
}
153+
}
154+
155+
if(!token_it_inc_non_whitespace(&it))
156+
{
157+
break;
158+
}
159+
}
160+
}
161+
}
162+
// NOTE(jack): if "f4_disable_cursor_token_occurance" is set, just highlight the cusror
163+
else
106164
{
107165
ProfileScope(app, "[Fleury] Token Highlight");
108166

4coder_fleury_render_helpers.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ F4_RenderRangeHighlight(Application_Links *app, View_ID view_id, Text_Layout_ID
2727
total_range_rect.y0 = MinimumF32(range_start_rect.y0, range_end_rect.y0);
2828
total_range_rect.x1 = MaximumF32(range_start_rect.x1, range_end_rect.x1);
2929
total_range_rect.y1 = MaximumF32(range_start_rect.y1, range_end_rect.y1);
30-
if(kind == F4_RangeHighlightKind_Underline)
31-
{
32-
total_range_rect.y0 = total_range_rect.y1 - 1.f;
33-
total_range_rect.y1 += 1.f;
30+
31+
ARGB_Color color = fcolor_resolve(fcolor_id(fleury_color_token_highlight));
32+
33+
switch (kind) {
34+
case F4_RangeHighlightKind_Underline: {
35+
total_range_rect.y0 = total_range_rect.y1 - 1.f;
36+
total_range_rect.y1 += 1.f;
37+
} break;
38+
39+
case F4_RangeHighlightKind_MinorUnderline: {
40+
total_range_rect.y0 = total_range_rect.y1 - 1.f;
41+
total_range_rect.y1 += 1.f;
42+
color = fcolor_resolve(fcolor_id(fleury_color_token_minor_highlight));
43+
}
3444
}
3545
draw_rectangle(app, total_range_rect, 4.f, color);
3646
}

4coder_fleury_render_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum F4_RangeHighlightKind
77
{
88
F4_RangeHighlightKind_Whole,
99
F4_RangeHighlightKind_Underline,
10+
F4_RangeHighlightKind_MinorUnderline,
1011
};
1112

1213
struct F4_Flash

config.4coder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ default_flags_sh = "-g";
9696
// f4_disable_calc_comments = true;
9797
// f4_poscontext_draw_at_bottom_of_buffer = true;
9898
// f4_disable_poscontext = true;
99+
// f4_disable_cursor_token_occurance = true;

0 commit comments

Comments
 (0)