forked from 4coder-archive/4coder_fleury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4coder_fleury_colors.cpp
320 lines (284 loc) · 11 KB
/
4coder_fleury_colors.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
typedef u32 F4_SyntaxFlags;
enum
{
F4_SyntaxFlag_Functions = (1<<0),
F4_SyntaxFlag_Macros = (1<<1),
F4_SyntaxFlag_Types = (1<<2),
F4_SyntaxFlag_Operators = (1<<3),
F4_SyntaxFlag_Constants = (1<<4),
F4_SyntaxFlag_Literals = (1<<5),
F4_SyntaxFlag_Preprocessor = (1<<6),
F4_SyntaxFlag_Keywords = (1<<7),
F4_SyntaxFlag_HighlightAll = (1<<15),
};
#define F4_SyntaxFlag_All 0xffffffff
struct F4_SyntaxOptions
{
String8 name;
F4_SyntaxFlags flags;
};
global f32 f4_syntax_flag_transitions[32] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,};
global F4_SyntaxOptions f4_syntax_opts[] =
{
{ S8Lit("All"), F4_SyntaxFlag_All },
{ S8Lit("None"), 0 },
{ S8Lit("Functions Only"), F4_SyntaxFlag_Functions },
{ S8Lit("Macros Only"), F4_SyntaxFlag_Macros },
{ S8Lit("Function-Likes Only"), F4_SyntaxFlag_Functions | F4_SyntaxFlag_Macros },
{ S8Lit("Types Only"), F4_SyntaxFlag_Types },
{ S8Lit("Externals Only"), F4_SyntaxFlag_Functions | F4_SyntaxFlag_Macros | F4_SyntaxFlag_Types | F4_SyntaxFlag_Constants },
};
global i32 f4_active_syntax_opt_idx = 0;
function b32
F4_ARGBIsValid(ARGB_Color color)
{
return color != 0xFF990099;
}
internal void
F4_TickColors(Application_Links *app, Frame_Info frame_info)
{
F4_SyntaxOptions opts = f4_syntax_opts[f4_active_syntax_opt_idx];
for(int i = 0; i < sizeof(F4_SyntaxFlags)*8; i += 1)
{
f32 delta = ((f32)!!(opts.flags & (1<<i)) - f4_syntax_flag_transitions[i]) * frame_info.animation_dt * 8.f;
f4_syntax_flag_transitions[i] += delta;
if(fabsf(delta) > 0.001f)
{
animate_in_n_milliseconds(app, 0);
}
}
}
CUSTOM_COMMAND_SIG(f4_switch_syntax_option)
CUSTOM_DOC("Switches the syntax highlighting mode.")
{
f4_active_syntax_opt_idx = (f4_active_syntax_opt_idx + 1) % ArrayCount(f4_syntax_opts);
}
internal String8
F4_SyntaxOptionString(void)
{
return f4_syntax_opts[f4_active_syntax_opt_idx].name;
}
typedef u32 ColorFlags;
enum
{
ColorFlag_Macro = (1<<0),
ColorFlag_PowerMode = (1<<1),
};
struct ColorCtx
{
Token token;
Buffer_ID buffer;
ColorFlags flags;
keybinding_mode mode;
};
internal ColorCtx
ColorCtx_Token(Token token, Buffer_ID buffer)
{
ColorCtx ctx = {0};
ctx.token = token;
ctx.buffer = buffer;
return ctx;
}
internal ColorCtx
ColorCtx_Cursor(ColorFlags flags, keybinding_mode mode)
{
ColorCtx ctx = {0};
ctx.flags = flags;
ctx.mode = mode;
return ctx;
}
static ARGB_Color
F4_ARGBFromID(Color_Table table, Managed_ID id, int subindex)
{
ARGB_Color result = 0;
FColor color = fcolor_id(id);
if (color.a_byte == 0){
if (color.id != 0){
result = finalize_color(table, color.id, subindex);
}
}
else{
result = color.argb;
}
return(result);
}
static ARGB_Color
F4_ARGBFromID(Color_Table table, Managed_ID id)
{
return F4_ARGBFromID(table, id, 0);
}
internal ARGB_Color
F4_GetColor(Application_Links *app, ColorCtx ctx)
{
Color_Table table = active_color_table;
ARGB_Color default_color = F4_ARGBFromID(table, defcolor_text_default);
ARGB_Color color = default_color;
f32 t = 1;
#define FillFromFlag(flag) do{ t = f4_syntax_flag_transitions[BitOffset(flag)]; }while(0)
//~ NOTE(rjf): Token Color
if(ctx.token.size != 0)
{
Scratch_Block scratch(app);
switch(ctx.token.kind)
{
case TokenBaseKind_Identifier:
{
String_Const_u8 string = push_buffer_range(app, scratch, ctx.buffer, Ii64(ctx.token.pos, ctx.token.pos + ctx.token.size));
F4_Index_Note *note = F4_Index_LookupNote(string);
if(note)
{
color = 0xffff0000;
switch(note->kind)
{
case F4_Index_NoteKind_Type:
{
FillFromFlag(F4_SyntaxFlag_Types);
color = F4_ARGBFromID(table,
note->flags & F4_Index_NoteFlag_SumType
? fleury_color_index_sum_type
: fleury_color_index_product_type);
}break;
case F4_Index_NoteKind_Macro:
{
FillFromFlag(F4_SyntaxFlag_Macros);
color = F4_ARGBFromID(table, fleury_color_index_macro);
}break;
case F4_Index_NoteKind_Function:
{
FillFromFlag(F4_SyntaxFlag_Functions);
color = F4_ARGBFromID(table, fleury_color_index_function);
}break;
case F4_Index_NoteKind_Constant:
{
FillFromFlag(F4_SyntaxFlag_Constants);
color = F4_ARGBFromID(table, fleury_color_index_constant);
}break;
case F4_Index_NoteKind_Decl:
{
FillFromFlag(F4_SyntaxFlag_Constants);
color = F4_ARGBFromID(table, fleury_color_index_decl);
}break;
default: color = 0xffff00ff; break;
}
if(!F4_ARGBIsValid(color)) { color = default_color; }
}
}break;
case TokenBaseKind_Preprocessor: { FillFromFlag(F4_SyntaxFlag_Preprocessor); color = F4_ARGBFromID(table, defcolor_preproc); } break;
case TokenBaseKind_Keyword: { FillFromFlag(F4_SyntaxFlag_Keywords); color = F4_ARGBFromID(table, defcolor_keyword); } break;
case TokenBaseKind_Comment: { color = F4_ARGBFromID(table, defcolor_comment); } break;
case TokenBaseKind_LiteralString: { FillFromFlag(F4_SyntaxFlag_Literals); color = F4_ARGBFromID(table, defcolor_str_constant); } break;
case TokenBaseKind_LiteralInteger: { FillFromFlag(F4_SyntaxFlag_Literals); color = F4_ARGBFromID(table, defcolor_int_constant); } break;
case TokenBaseKind_LiteralFloat: { FillFromFlag(F4_SyntaxFlag_Literals); color = F4_ARGBFromID(table, defcolor_float_constant); } break;
case TokenBaseKind_Operator: { FillFromFlag(F4_SyntaxFlag_Operators); color = F4_ARGBFromID(table, fleury_color_operators); if(!F4_ARGBIsValid(color)) { color = default_color; } } break;
case TokenBaseKind_ScopeOpen:
case TokenBaseKind_ScopeClose:
case TokenBaseKind_ParentheticalOpen:
case TokenBaseKind_ParentheticalClose:
case TokenBaseKind_StatementClose:
{
color = F4_ARGBFromID(table, fleury_color_syntax_crap);
if(!F4_ARGBIsValid(color)) { color = default_color; }
break;
}
default:
{
switch(ctx.token.sub_kind)
{
case TokenCppKind_LiteralTrue:
case TokenCppKind_LiteralFalse:
{
color = F4_ARGBFromID(table, defcolor_bool_constant);
FillFromFlag(F4_SyntaxFlag_Literals);
break;
}
case TokenCppKind_LiteralCharacter:
case TokenCppKind_LiteralCharacterWide:
case TokenCppKind_LiteralCharacterUTF8:
case TokenCppKind_LiteralCharacterUTF16:
case TokenCppKind_LiteralCharacterUTF32:
{
color = F4_ARGBFromID(table, defcolor_char_constant);
FillFromFlag(F4_SyntaxFlag_Literals);
break;
}
case TokenCppKind_PPIncludeFile:
{
color = F4_ARGBFromID(table, defcolor_include);
FillFromFlag(F4_SyntaxFlag_Literals);
break;
}
}
}break;
}
}
//~ NOTE(rjf): Cursor Color
else
{
if(ctx.flags & ColorFlag_Macro)
{
color = F4_ARGBFromID(table, fleury_color_cursor_macro);
}
else if(ctx.flags & ColorFlag_PowerMode)
{
color = F4_ARGBFromID(table, fleury_color_cursor_power_mode);
}
else
{
color = F4_ARGBFromID(table, defcolor_cursor, ctx.mode);
}
}
return color_blend(default_color, t, color);
}
static void
F4_SyntaxHighlight(Application_Links *app, Text_Layout_ID text_layout_id, Token_Array *array)
{
Color_Table table = active_color_table;
Buffer_ID buffer = text_layout_get_buffer(app, text_layout_id);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
i64 first_index = token_index_from_pos(array, visible_range.first);
Token_Iterator_Array it = token_iterator_index(0, array, first_index);
ARGB_Color comment_tag_color = F4_ARGBFromID(table, fleury_color_index_comment_tag, 0);
for(;;)
{
Token *token = token_it_read(&it);
if(!token || token->pos >= visible_range.one_past_last)
{
break;
}
ARGB_Color argb = F4_GetColor(app, ColorCtx_Token(*token, buffer));
paint_text_color(app, text_layout_id, Ii64_size(token->pos, token->size), argb);
// NOTE(rjf): Substrings from comments
if(F4_ARGBIsValid(comment_tag_color))
{
if(token->kind == TokenBaseKind_Comment)
{
Scratch_Block scratch(app);
String_Const_u8 string = push_buffer_range(app, scratch, buffer, Ii64(token->pos, token->pos + token->size));
for(u64 i = 0; i < string.size; i += 1)
{
if(string.str[i] == '@')
{
u64 j = i+1;
for(; j < string.size; j += 1)
{
if(character_is_whitespace(string.str[j]))
{
break;
}
}
paint_text_color(app, text_layout_id, Ii64(token->pos + (i64)i, token->pos + (i64)j), comment_tag_color);
}
}
}
}
if(!token_it_inc_all(&it))
{
break;
}
}
F4_Language *lang = F4_LanguageFromBuffer(app, buffer);
if(lang != 0 && lang->Highlight != 0)
{
lang->Highlight(app, text_layout_id, array, table);
}
}