This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path4coder_fleury_divider_comments.cpp
163 lines (145 loc) · 6.39 KB
/
4coder_fleury_divider_comments.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//~ NOTE(rjf): Divider Comments
String_Const_u8 strong_divider_comment_signifier = string_u8_litexpr("//~");
String_Const_u8 weak_divider_comment_signifier = string_u8_litexpr("//-");
function i64
_F4_Boundary_DividerComment(Application_Links *app, Buffer_ID buffer,
Side side, Scan_Direction direction, i64 pos,
String_Const_u8 signifier)
{
i64 result = pos;
Scratch_Block scratch(app);
Token_Array tokens = get_token_array_from_buffer(app, buffer);
if(tokens.tokens != 0)
{
Token_Iterator_Array it = token_iterator_pos(0, &tokens, pos);
switch(direction)
{
case Scan_Forward:
{
for(;token_it_inc_non_whitespace(&it);)
{
Token *token = token_it_read(&it);
if(token == 0)
{
break;
}
if(token->kind == TokenBaseKind_Comment)
{
String_Const_u8 str = push_buffer_range(app, scratch, buffer, Ii64(token));
if(str.size >= signifier.size &&
string_match(string_substring(str, Ii64(0, signifier.size)), signifier))
{
result = token->pos;
break;
}
}
}
}break;
case Scan_Backward:
{
for(;token_it_dec_non_whitespace(&it);)
{
Token *token = token_it_read(&it);
if(token == 0)
{
break;
}
if(token->kind == TokenBaseKind_Comment)
{
String_Const_u8 str = push_buffer_range(app, scratch, buffer, Ii64(token));
if(str.size >= signifier.size &&
string_match(string_substring(str, Ii64(0, signifier.size)), signifier))
{
result = token->pos;
break;
}
}
}
}break;
}
}
return(result);
}
function i64
F4_Boundary_DividerComment(Application_Links *app, Buffer_ID buffer,
Side side, Scan_Direction direction, i64 pos,
String_Const_u8 signifier)
{
return _F4_Boundary_DividerComment(app, buffer, side, direction, pos, strong_divider_comment_signifier);
}
CUSTOM_COMMAND_SIG(f4_move_to_next_divider_comment)
CUSTOM_DOC("Seek right for next divider comment in the buffer.")
{
Scratch_Block scratch(app);
current_view_scan_move(app, Scan_Forward, push_boundary_list(scratch, F4_Boundary_DividerComment));
}
CUSTOM_COMMAND_SIG(f4_move_to_prev_divider_comment)
CUSTOM_DOC("Seek left for previous divider comment in the buffer.")
{
Scratch_Block scratch(app);
current_view_scan_move(app, Scan_Backward, push_boundary_list(scratch, F4_Boundary_DividerComment));
}
function void
F4_RenderDividerComments(Application_Links *app, Buffer_ID buffer, View_ID view,
Text_Layout_ID text_layout_id)
{
if(!def_get_config_b32(vars_save_string_lit("f4_disable_divider_comments")))
{
ProfileScope(app, "[F4] Divider Comments");
Token_Array token_array = get_token_array_from_buffer(app, buffer);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
Scratch_Block scratch(app);
if(token_array.tokens != 0)
{
i64 first_index = token_index_from_pos(&token_array, visible_range.first);
Token_Iterator_Array it = token_iterator_index(0, &token_array, first_index);
Token *token = 0;
for(;;)
{
token = token_it_read(&it);
if(token->pos >= visible_range.one_past_last || !token || !token_it_inc_non_whitespace(&it))
{
break;
}
if(token->kind == TokenBaseKind_Comment)
{
Rect_f32 comment_first_char_rect = text_layout_character_on_screen(app, text_layout_id, token->pos);
Rect_f32 comment_last_char_rect = text_layout_character_on_screen(app, text_layout_id, token->pos+token->size-1);
String_Const_u8 token_string = push_buffer_range(app, scratch, buffer, Ii64(token));
String_Const_u8 signifier_substring = string_substring(token_string, Ii64(0, 3));
f32 roundness = 4.f;
// NOTE(rjf): Strong dividers.
if(string_match(signifier_substring, strong_divider_comment_signifier))
{
Rect_f32 rect =
{
comment_first_char_rect.x0,
comment_first_char_rect.y0-2,
10000,
comment_first_char_rect.y0,
};
draw_rectangle(app, rect, roundness, fcolor_resolve(fcolor_id(defcolor_comment)));
}
// NOTE(rjf): Weak dividers.
else if(string_match(signifier_substring, weak_divider_comment_signifier))
{
f32 dash_size = 8;
Rect_f32 rect =
{
comment_last_char_rect.x1,
(comment_last_char_rect.y0 + comment_last_char_rect.y1)/2 - 1,
comment_last_char_rect.x1 + dash_size,
(comment_last_char_rect.y0 + comment_last_char_rect.y1)/2 + 1,
};
for(int i = 0; i < 1000; i += 1)
{
draw_rectangle(app, rect, roundness, fcolor_resolve(fcolor_id(defcolor_comment)));
rect.x0 += dash_size*1.5f;
rect.x1 += dash_size*1.5f;
}
}
}
}
}
}
}