Compare commits

...
Author SHA1 Message Date
waozi e329b2c616 ki: command palette (Ctrl+Shift+P) and fuzzy quick-open (Ctrl+P)
Add a centered overlay for two VS Code-style pickers:

- Ctrl+P opens Quick Open: a fuzzy filename finder over the project
  tree. Ctrl+Shift+P opens the Command Palette: a fuzzy-filtered list of
  editor commands (save, undo/redo, find, view modes, run, build, zoom).
  Both rank results with a subsequence matcher that rewards consecutive
  matches and word-boundary hits.

- The overlay dims the workspace and captures input (PushUIInputCapture)
  so nothing behind reacts. Arrow keys navigate, Enter activates, Esc
  closes. It is drawn last in the frame so it sits on top of all panes.

- Ctrl+P previously reused the project-grep field (a mislabel). It now
  opens the dedicated quick-open picker instead.

Replaces the earlier buggy DrawUIModalFrame-based draft with a plain
DrawRectangleRec panel matching the IDE's existing inline-panel style.

Also drop a dead 'growth' local from editor_replace_all_source (the
rewrite bounds-checks against EDITOR_SOURCE_MAX_BYTES directly).
2026-07-30 08:16:21 -03:00
waozi f02733761b IDE editor-feel polish: line-number gutter, cursor-preserving undo, C highlighting, replace-all
Library (src/ui/ui.c, include/ui_controls.h):
- Optional line-number gutter on DrawUITextArea via show_line_numbers;
  the Kryon IDE source pane turns it on. Numbers are right-aligned in the
  established muted gray, sized to the widest line number.
- C syntax highlighting: preprocessor lines (#include/#define/...), block
  comments (single-line /* ... */ and multi-line, with state threaded across
  lines), char literals ('x', '\n'), and an expanded keyword/type set
  (storage qualifiers, signed/unsigned/inline/extern, stdint types, NULL).
- Block-comment state is threaded through the per-line tokenizer so lines
  inside an open comment render correctly.

IDE (cmd/ki/main.c):
- Undo/redo now restores the caret to where the edit happened instead of
  jumping to end-of-buffer. History entries carry a cursor snapshot alongside
  the text.
- Replace All in the find/replace bar: rebuilds the buffer in one pass with a
  single undo entry, reports the count replaced.

Also ignore .kryon-ide-state (local editor state).
2026-07-29 19:58:15 -03:00
4 changed files with 562 additions and 42 deletions

No files matched your search

+1
View File
@@ -5,6 +5,7 @@
*~
*.tmp
.zcode
.kryon-ide-state
# Build output
build/
+456 -26
View File
@@ -5,6 +5,7 @@
#include "ui_internal.h"
#if !defined(_WIN32)
#include <ctype.h>
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
@@ -104,6 +105,14 @@ typedef struct EditorKryScreen {
int takes_viewport;
} EditorKryScreen;
/* One undo/redo checkpoint: the full source snapshot plus the cursor position
at that snapshot, so undo restores the caret where the edit happened instead
of jumping to end-of-buffer. */
typedef struct EditorHistoryEntry {
char *text;
int cursor;
} EditorHistoryEntry;
typedef struct EditorProject {
char path[EDITOR_PATH_CAP];
char name[96];
@@ -131,8 +140,15 @@ typedef struct EditorProject {
int replace_cursor;
int replace_focused;
int find_replace_visible;
char *undo_items[EDITOR_HISTORY_MAX];
char *redo_items[EDITOR_HISTORY_MAX];
int palette_visible;
int palette_mode; /* 0 = quick open (files), 1 = commands */
char palette_text[128];
int palette_cursor;
int palette_focused;
int palette_selected; /* highlighted result index */
int palette_scroll_y;
EditorHistoryEntry undo_items[EDITOR_HISTORY_MAX];
EditorHistoryEntry redo_items[EDITOR_HISTORY_MAX];
int undo_count;
int redo_count;
char image_file[EDITOR_PATH_CAP];
@@ -606,48 +622,55 @@ editor_strdup(const char *text)
}
static void
editor_free_history(char **items, int *count)
editor_free_history(EditorHistoryEntry *items, int *count)
{
if(items == NULL || count == NULL)
return;
for(int i = 0; i < *count; i++) {
free(items[i]);
items[i] = NULL;
free(items[i].text);
items[i].text = NULL;
items[i].cursor = 0;
}
*count = 0;
}
static void
editor_push_history(char **items, int *count, const char *text)
editor_push_history(EditorHistoryEntry *items, int *count, const char *text,
int cursor)
{
char *copy;
if(items == NULL || count == NULL)
return;
if(*count > 0 && strcmp(items[*count - 1], text != NULL ? text : "") == 0)
if(*count > 0 && strcmp(items[*count - 1].text, text != NULL ? text : "") == 0)
return;
copy = editor_strdup(text);
if(copy == NULL)
return;
if(*count >= EDITOR_HISTORY_MAX) {
free(items[0]);
free(items[0].text);
memmove(items, items + 1, sizeof(items[0]) * (EDITOR_HISTORY_MAX - 1));
*count = EDITOR_HISTORY_MAX - 1;
}
items[*count] = copy;
items[*count].text = copy;
items[*count].cursor = cursor;
(*count)++;
}
static int
editor_pop_history(char **items, int *count, char *dst, size_t dst_size)
editor_pop_history(EditorHistoryEntry *items, int *count, char *dst,
size_t dst_size, int *cursor_out)
{
char *text;
if(items == NULL || count == NULL || dst == NULL || dst_size == 0 ||
*count <= 0)
return 0;
text = items[*count - 1];
items[*count - 1] = NULL;
text = items[*count - 1].text;
if(cursor_out != NULL)
*cursor_out = items[*count - 1].cursor;
items[*count - 1].text = NULL;
items[*count - 1].cursor = 0;
(*count)--;
snprintf(dst, dst_size, "%s", text != NULL ? text : "");
free(text);
@@ -2899,6 +2922,107 @@ editor_build_project_tree(EditorProject *project, const char *dir_path,
}
}
/* --- Command palette / quick-open ---------------------------------------- */
enum {
EDITOR_PALETTE_QUICK_OPEN,
EDITOR_PALETTE_COMMANDS
};
/* Command palette entry. command_id is interpreted by draw_palette. */
typedef struct {
const char *label;
int command_id;
} EditorPaletteCommand;
enum {
EDITOR_CMD_SAVE,
EDITOR_CMD_UNDO,
EDITOR_CMD_REDO,
EDITOR_CMD_FIND,
EDITOR_CMD_TOGGLE_PREVIEW,
EDITOR_CMD_SOURCE_ONLY,
EDITOR_CMD_SPLIT,
EDITOR_CMD_RUN,
EDITOR_CMD_BUILD,
EDITOR_CMD_ZOOM_IN,
EDITOR_CMD_ZOOM_OUT,
EDITOR_CMD_COUNT
};
static const EditorPaletteCommand editor_palette_commands[] = {
{"Save", EDITOR_CMD_SAVE},
{"Undo", EDITOR_CMD_UNDO},
{"Redo", EDITOR_CMD_REDO},
{"Find / Replace", EDITOR_CMD_FIND},
{"View: Preview Only", EDITOR_CMD_TOGGLE_PREVIEW},
{"View: Source Only", EDITOR_CMD_SOURCE_ONLY},
{"View: Split", EDITOR_CMD_SPLIT},
{"Run", EDITOR_CMD_RUN},
{"Build", EDITOR_CMD_BUILD},
{"Zoom In", EDITOR_CMD_ZOOM_IN},
{"Zoom Out", EDITOR_CMD_ZOOM_OUT}
};
/*
* Subsequence fuzzy match: every query char must appear in target in order
* (case-insensitive). Returns a score > 0 on match (higher = tighter), 0
* otherwise. Rewards consecutive matches and matches at word boundaries so
* "sma" ranks "session_main" above "scroll_max_anchor".
*/
static int
editor_fuzzy_score(const char *query, const char *target)
{
int score = 0;
int consecutive = 0;
char prev_target = '_';
if(query == NULL || target == NULL)
return 0;
while(*query != '\0' && *target != '\0') {
char q = (char)tolower((unsigned char)*query);
char t = (char)tolower((unsigned char)*target);
if(q == t) {
consecutive++;
score += consecutive * 2;
if(prev_target == '_' || prev_target == '/' ||
prev_target == '.' || prev_target == '\\' ||
prev_target == ' ' || prev_target == '-')
score += 4;
query++;
} else {
consecutive = 0;
}
prev_target = *target;
target++;
}
return *query == '\0' ? (score > 0 ? score : 1) : 0;
}
static void
editor_open_palette(EditorProject *project, int mode)
{
if(project == NULL)
return;
project->palette_visible = 1;
project->palette_mode = mode;
project->palette_text[0] = '\0';
project->palette_cursor = 0;
project->palette_focused = 1;
project->palette_selected = 0;
project->palette_scroll_y = 0;
project->source_focused = 0;
}
static void
editor_close_palette(EditorProject *project)
{
if(project == NULL)
return;
project->palette_visible = 0;
project->palette_focused = 0;
}
static const char *
editor_tree_path_for_id(const EditorTreeItem *items, int count, int id)
{
@@ -3947,15 +4071,19 @@ static int
editor_undo_source(EditorProject *project, char *status, size_t status_size)
{
char current[EDITOR_SOURCE_MAX_BYTES];
int restored_cursor = 0;
if(project == NULL || project->undo_count <= 0)
return 0;
snprintf(current, sizeof(current), "%s", project->source);
editor_push_history(project->redo_items, &project->redo_count, current);
editor_push_history(project->redo_items, &project->redo_count, current,
project->source_cursor);
if(!editor_pop_history(project->undo_items, &project->undo_count,
project->source, sizeof(project->source)))
project->source, sizeof(project->source),
&restored_cursor))
return 0;
project->source_cursor = (int)strlen(project->source);
project->source_cursor =
restored_cursor > 0 ? restored_cursor : (int)strlen(project->source);
project->source_dirty = 1;
project->source_last_edit_time = GetTime();
snprintf(status, status_size, "Undo");
@@ -3966,15 +4094,19 @@ static int
editor_redo_source(EditorProject *project, char *status, size_t status_size)
{
char current[EDITOR_SOURCE_MAX_BYTES];
int restored_cursor = 0;
if(project == NULL || project->redo_count <= 0)
return 0;
snprintf(current, sizeof(current), "%s", project->source);
editor_push_history(project->undo_items, &project->undo_count, current);
editor_push_history(project->undo_items, &project->undo_count, current,
project->source_cursor);
if(!editor_pop_history(project->redo_items, &project->redo_count,
project->source, sizeof(project->source)))
project->source, sizeof(project->source),
&restored_cursor))
return 0;
project->source_cursor = (int)strlen(project->source);
project->source_cursor =
restored_cursor > 0 ? restored_cursor : (int)strlen(project->source);
project->source_dirty = 1;
project->source_last_edit_time = GetTime();
snprintf(status, status_size, "Redo");
@@ -4072,7 +4204,8 @@ editor_replace_next_source(EditorProject *project, char *status,
}
before = editor_strdup(project->source);
if(before != NULL)
editor_push_history(project->undo_items, &project->undo_count, before);
editor_push_history(project->undo_items, &project->undo_count, before,
(int)(hit - project->source));
free(before);
memmove(hit + replace_len, hit + find_len,
(size_t)(source_len - (int)(hit - project->source) - find_len + 1));
@@ -4086,6 +4219,73 @@ editor_replace_next_source(EditorProject *project, char *status,
return 1;
}
static int
editor_replace_all_source(EditorProject *project, char *status,
size_t status_size)
{
char *before;
int source_len;
int find_len;
int replace_len;
int replaced = 0;
int write;
char *read;
char tmp[EDITOR_SOURCE_MAX_BYTES];
if(project == NULL || project->find_text[0] == '\0' ||
!project->source_loaded)
return 0;
source_len = (int)strlen(project->source);
find_len = (int)strlen(project->find_text);
replace_len = (int)strlen(project->replace_text);
before = editor_strdup(project->source);
if(before != NULL)
editor_push_history(project->undo_items, &project->undo_count, before,
project->source_cursor);
free(before);
/* Build the result into tmp, counting matches, then copy back. This avoids
O(n^2) repeated memmove and keeps one undo entry for the whole batch. */
write = 0;
read = project->source;
while(read < project->source + source_len) {
char *hit = strstr(read, project->find_text);
int copy_len;
if(hit == NULL) {
copy_len = (int)((project->source + source_len) - read);
if(write + copy_len + 1 > EDITOR_SOURCE_MAX_BYTES)
goto too_large;
memcpy(tmp + write, read, (size_t)copy_len);
write += copy_len;
break;
}
copy_len = (int)(hit - read);
if(write + copy_len + replace_len + 1 > EDITOR_SOURCE_MAX_BYTES)
goto too_large;
memcpy(tmp + write, read, (size_t)copy_len);
write += copy_len;
memcpy(tmp + write, project->replace_text, (size_t)replace_len);
write += replace_len;
read = hit + find_len;
replaced++;
}
tmp[write] = '\0';
/* Honor the pre-edit cursor if it still falls within the new text. */
project->source_cursor =
project->source_cursor <= write ? project->source_cursor : write;
memcpy(project->source, tmp, (size_t)write + 1);
project->source_dirty = 1;
project->source_last_edit_time = GetTime();
if(replaced == 0)
snprintf(status, status_size, "No matches: %s", project->find_text);
else
snprintf(status, status_size, "Replaced %d", replaced);
return 1;
too_large:
snprintf(status, status_size, "Replace-all result too large");
return 0;
}
static void
editor_fill_find_from_selection(EditorProject *project)
{
@@ -4327,6 +4527,7 @@ draw_source_code(Rectangle content, EditorProject *project,
{
char path[EDITOR_PATH_CAP];
char *before_edit = NULL;
int before_cursor = 0;
int source_changed = 0;
int textarea_changed = 0;
int pad = ScaleUIPx(16);
@@ -4489,6 +4690,12 @@ draw_source_code(Rectangle content, EditorProject *project,
project->find_text[0] == '\0', NULL))
editor_replace_next_source(project, status, status_size);
if(DrawUIGenericButton((int)content.x + pad + ScaleUIPx(512),
find_y,
ScaleUIPx(90), toolbar_h, "Replace All",
UI_BUTTON_STYLE_SECONDARY,
project->find_text[0] == '\0', NULL))
editor_replace_all_source(project, status, status_size);
if(DrawUIGenericButton((int)content.x + pad + ScaleUIPx(610),
find_y,
ScaleUIPx(56), toolbar_h, "Close",
UI_BUTTON_STYLE_SECONDARY, 0, NULL)) {
@@ -4515,6 +4722,7 @@ draw_source_code(Rectangle content, EditorProject *project,
if(bounds.height < ScaleUIPx(80))
bounds.height = ScaleUIPx(80);
before_edit = editor_strdup(project->source);
before_cursor = project->source_cursor;
if(project->source_focused && editor_mod_key_down()) {
if(IsKeyPressed(KEY_S)) {
editor_save_source_now(project, status, status_size);
@@ -4547,7 +4755,8 @@ draw_source_code(Rectangle content, EditorProject *project,
.focus_id = 1501,
.placeholder = "",
.syntax = editor_source_syntax(project->selected_file),
.style = editor_input_style()
.style = editor_input_style(),
.show_line_numbers = 1
});
source_changed = textarea_changed;
if(editor_handle_source_clipboard(project, textarea_changed, status,
@@ -4569,7 +4778,7 @@ draw_source_code(Rectangle content, EditorProject *project,
if(source_changed) {
if(before_edit != NULL)
editor_push_history(project->undo_items, &project->undo_count,
before_edit);
before_edit, before_cursor);
editor_free_history(project->redo_items, &project->redo_count);
project->source_dirty = 1;
project->source_last_edit_time = GetTime();
@@ -5076,6 +5285,221 @@ draw_preview_pane(Rectangle content, EditorProject *project, char *status,
draw_preview_context_menu(project, stage, status, status_size);
}
static int
editor_run_palette_command(EditorProject *project, int command_id, char *status,
size_t status_size)
{
switch(command_id) {
case EDITOR_CMD_SAVE:
editor_save_source_now(project, status, status_size);
return 1;
case EDITOR_CMD_UNDO:
editor_undo_source(project, status, status_size);
return 1;
case EDITOR_CMD_REDO:
editor_redo_source(project, status, status_size);
return 1;
case EDITOR_CMD_FIND:
project->find_replace_visible = 1;
project->source_focused = 1;
return 1;
case EDITOR_CMD_TOGGLE_PREVIEW:
project->layout_mode = EDITOR_LAYOUT_PREVIEW;
return 1;
case EDITOR_CMD_SOURCE_ONLY:
project->layout_mode = EDITOR_LAYOUT_SOURCE;
return 1;
case EDITOR_CMD_SPLIT:
project->layout_mode = EDITOR_LAYOUT_SPLIT;
return 1;
case EDITOR_CMD_RUN:
editor_run_project(project, status, status_size);
return 1;
case EDITOR_CMD_BUILD:
if(project->selected_run_target >= 0 &&
project->selected_run_target < project->run_target_count)
editor_build_capture(project,
project->run_targets[project->selected_run_target].command,
"Built project", "Could not build project",
status, status_size);
else
snprintf(status, status_size, "No run target selected");
return 1;
case EDITOR_CMD_ZOOM_IN:
editor_adjust_source_zoom(project, 2, status, status_size);
return 1;
case EDITOR_CMD_ZOOM_OUT:
editor_adjust_source_zoom(project, -2, status, status_size);
return 1;
default:
return 0;
}
}
/*
* Command palette (Ctrl+Shift+P) and quick-open (Ctrl+P). A centered overlay
* with a query field and a filtered, fuzzy-ranked result list. Drawn as a
* plain panel (no modal-frame widget) with its own input capture, matching the
* IDE's existing inline-panel style.
*/
static void
draw_palette(EditorProject *project, char *status, size_t status_size)
{
Rectangle panel;
Rectangle input_bounds;
int input_changed = 0;
int view_w;
int view_h;
int panel_w;
int panel_h;
int list_x;
int list_y;
int list_w;
int list_h;
int item_h = ScaleUIPx(28);
int font = UI_TEXT_16;
int max_results = 10;
int visible_results = 0;
int selected_command = -1;
const char *selected_path = NULL;
EditorTreeItem files[EDITOR_MAX_TREE_ITEMS];
int file_count = 0;
int title_font = UI_TEXT_16;
if(project == NULL || !project->palette_visible)
return;
view_w = GetScreenWidth();
view_h = GetScreenHeight();
panel_w = ScaleUIPx(520);
if(panel_w > view_w - ScaleUIPx(32))
panel_w = view_w - ScaleUIPx(32);
panel_h = ScaleUIPx(64) + item_h * max_results;
/* Dim the workspace and capture all input so nothing behind reacts. */
PushUIInputCapture((Rectangle){0, 0, (float)view_w, (float)view_h}, 0);
DrawRectangle(0, 0, view_w, view_h, (Color){0, 0, 0, 170});
panel = (Rectangle){(float)((view_w - panel_w) / 2),
(float)((view_h - panel_h) / 2),
(float)panel_w, (float)panel_h};
DrawRectangleRec(panel, GetThemeSurface());
DrawRectangleLinesEx(panel, 1, DarkenUIColor(GetThemeSurface(), 40));
DrawUIText(project->palette_mode == EDITOR_PALETTE_COMMANDS
? "Command Palette" : "Quick Open",
(int)panel.x + ScaleUIPx(16), (int)panel.y + ScaleUIPx(10),
title_font, GetThemeText());
input_bounds = (Rectangle){(int)panel.x + ScaleUIPx(16),
(int)panel.y + ScaleUIPx(36),
panel_w - ScaleUIPx(32), ScaleUIPx(30)};
input_changed = DrawUITextField((UITextField){
.bounds = input_bounds,
.text = project->palette_text,
.text_size = sizeof(project->palette_text),
.cursor_position = &project->palette_cursor,
.focused = &project->palette_focused,
.max_codepoints = (int)sizeof(project->palette_text) - 1,
.font = font,
.focus_id = 1701,
.style = editor_input_style()
});
list_x = (int)panel.x + ScaleUIPx(16);
list_y = (int)panel.y + ScaleUIPx(72);
list_w = panel_w - ScaleUIPx(32);
list_h = item_h * max_results;
/* Keyboard navigation. */
if(UIKeyPressed(KEY_ESCAPE)) {
editor_close_palette(project);
return;
}
if(UIKeyPressed(KEY_DOWN))
project->palette_selected++;
if(UIKeyPressed(KEY_UP))
project->palette_selected--;
/* Build the filtered result list and draw it. */
BeginUIClip(list_x, list_y, list_w, list_h);
{
int draw_y = list_y - project->palette_scroll_y;
int result_index = 0;
if(project->palette_mode == EDITOR_PALETTE_COMMANDS) {
for(int i = 0; i < EDITOR_CMD_COUNT; i++) {
if(project->palette_text[0] != '\0' &&
editor_fuzzy_score(project->palette_text,
editor_palette_commands[i].label) == 0)
continue;
if(result_index == project->palette_selected)
selected_command = editor_palette_commands[i].command_id;
if(result_index >= project->palette_selected &&
result_index < project->palette_selected + max_results) {
Color bg = (result_index == project->palette_selected)
? GetThemeButtonHover() : GetThemeSurface();
DrawRectangle(list_x, draw_y, list_w, item_h, bg);
DrawUIText(editor_palette_commands[i].label,
list_x + ScaleUIPx(10), draw_y + ScaleUIPx(5),
font, GetThemeText());
draw_y += item_h;
}
result_index++;
visible_results++;
}
} else {
editor_build_project_tree(project, project->path, 0, files,
&file_count);
for(int i = 0; i < file_count; i++) {
if(project->palette_text[0] != '\0' &&
editor_fuzzy_score(project->palette_text,
files[i].path) == 0)
continue;
if(result_index == project->palette_selected)
selected_path = files[i].path;
if(result_index >= project->palette_selected &&
result_index < project->palette_selected + max_results) {
Color bg = (result_index == project->palette_selected)
? GetThemeButtonHover() : GetThemeSurface();
DrawRectangle(list_x, draw_y, list_w, item_h, bg);
DrawUIText(files[i].path, list_x + ScaleUIPx(10),
draw_y + ScaleUIPx(5), font, GetThemeText());
draw_y += item_h;
}
result_index++;
visible_results++;
}
}
}
EndUIClip();
/* Clamp selection into range. */
if(project->palette_selected < 0)
project->palette_selected = 0;
if(visible_results > 0 && project->palette_selected > visible_results - 1)
project->palette_selected = visible_results - 1;
/* Execute on Enter. */
if(UIKeyPressed(KEY_ENTER) || UIKeyPressed(KEY_KP_ENTER)) {
if(project->palette_mode == EDITOR_PALETTE_COMMANDS &&
selected_command >= 0) {
editor_run_palette_command(project, selected_command, status,
status_size);
editor_close_palette(project);
} else if(project->palette_mode == EDITOR_PALETTE_QUICK_OPEN &&
selected_path != NULL) {
editor_select_file(project, selected_path);
editor_select_source_path(project, selected_path);
editor_close_palette(project);
snprintf(status, status_size, "Opened %s", selected_path);
}
}
/* Any text edit re-ranks and resets the selection. */
if(input_changed)
project->palette_selected = 0;
}
static void
draw_source_pane(Rectangle content, EditorProject *project,
char *status, size_t status_size)
@@ -6124,11 +6548,15 @@ main(int argc, char **argv)
else
snprintf(status_text, sizeof(status_text), "Search project");
}
if(project.loaded && editor_mod_key_down() && IsKeyPressed(KEY_P)) {
project.search_focused = 1;
project.source_focused = 0;
SetUIFocus(1211);
snprintf(status_text, sizeof(status_text), "Quick open/search");
if(project.loaded && editor_mod_key_down() &&
(IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT)) &&
IsKeyPressed(KEY_P)) {
editor_open_palette(&project, EDITOR_PALETTE_COMMANDS);
snprintf(status_text, sizeof(status_text), "Command palette");
} else if(project.loaded && editor_mod_key_down() &&
IsKeyPressed(KEY_P)) {
editor_open_palette(&project, EDITOR_PALETTE_QUICK_OPEN);
snprintf(status_text, sizeof(status_text), "Quick open");
}
editor_maybe_autosave_source(&project, status_text,
sizeof(status_text));
@@ -6179,6 +6607,8 @@ main(int argc, char **argv)
&project_menu_open, &project_dialog, play_icon,
&new_project_requested, status_text, sizeof(status_text));
draw_palette(&project, status_text, sizeof(status_text));
EndUIFocus();
EndDrawing();
}
+1
View File
@@ -131,6 +131,7 @@ typedef struct {
UITextInputStyle style;
UITextInputFilter filter;
void *filter_user_data;
int show_line_numbers; /* when nonzero, draw a line-number gutter */
} UITextArea;
typedef struct {
+104 -16
View File
@@ -1854,10 +1854,15 @@ static int
ui_syntax_c_keyword(const char *text, int len)
{
static const char *keywords[] = {
"break", "case", "char", "const", "continue", "default", "do",
"double", "else", "enum", "float", "for", "goto", "if", "int", "long",
"return", "short", "sizeof", "static", "struct", "switch",
"typedef", "void", "while"
"auto", "break", "case", "char", "const", "continue", "default",
"do", "double", "else", "enum", "extern", "float", "for", "goto",
"if", "inline", "int", "long", "register", "restrict", "return",
"short", "signed", "sizeof", "static", "struct", "switch", "typedef",
"union", "unsigned", "void", "volatile", "while",
/* Common C99/C11 standard-library types highlighted as keywords. */
"bool", "true", "false", "size_t", "ssize_t", "ptrdiff_t",
"int8_t", "int16_t", "int32_t", "int64_t",
"uint8_t", "uint16_t", "uint32_t", "uint64_t", "NULL"
};
for(size_t i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
@@ -1898,11 +1903,17 @@ ui_syntax_token_color(UISyntaxMode syntax, const char *text, int len,
if((syntax == UI_SYNTAX_KRY || syntax == UI_SYNTAX_MAKE) &&
first_token && text[0] == '#')
return comment;
if(syntax == UI_SYNTAX_C && first_token && text[0] == '#')
return path;
if(text[0] == '"')
return string;
if(syntax == UI_SYNTAX_C && text[0] == '\'')
return string;
if((text[0] >= '0' && text[0] <= '9') ||
(text[0] == '-' && len > 1 && text[1] >= '0' && text[1] <= '9'))
return number;
if(syntax == UI_SYNTAX_C && len >= 2 && text[0] == '/' && text[1] == '*')
return comment;
if(syntax == UI_SYNTAX_KRY &&
(text[0] == '/' ||
(text[0] == '.' && len > 1 && (text[1] == '/' || text[1] == '.')) ||
@@ -1921,12 +1932,26 @@ ui_syntax_token_color(UISyntaxMode syntax, const char *text, int len,
static int
ui_syntax_token_len(const char *line, int len, int index, UISyntaxMode syntax,
int first_token)
int first_token, int *in_block_comment)
{
int i = index;
if(i >= len)
return 0;
/* Continuing a C block comment from a previous line: consume to the
closing star-slash (or to end of line, leaving the flag set). */
if(in_block_comment != NULL && *in_block_comment) {
while(i < len) {
if(line[i] == '*' && i + 1 < len && line[i + 1] == '/') {
i += 2;
*in_block_comment = 0;
break;
}
i++;
}
if(i > index)
return i - index;
}
if(line[i] == ' ' || line[i] == '\t') {
while(i < len && (line[i] == ' ' || line[i] == '\t'))
i++;
@@ -1935,6 +1960,8 @@ ui_syntax_token_len(const char *line, int len, int index, UISyntaxMode syntax,
if((syntax == UI_SYNTAX_KRY || syntax == UI_SYNTAX_MAKE) &&
first_token && line[i] == '#')
return len - index;
if(syntax == UI_SYNTAX_C && first_token && line[i] == '#')
return len - index;
if(syntax == UI_SYNTAX_C && line[i] == '/' && i + 1 < len &&
line[i + 1] == '/')
return len - index;
@@ -1950,6 +1977,36 @@ ui_syntax_token_len(const char *line, int len, int index, UISyntaxMode syntax,
}
return i - index;
}
if(syntax == UI_SYNTAX_C && line[i] == '\'') {
i++;
while(i < len) {
if(line[i] == '\\' && i + 1 < len) {
i += 2;
continue;
}
if(line[i++] == '\'')
break;
}
return i - index;
}
if(syntax == UI_SYNTAX_C && line[i] == '/' && i + 1 < len &&
line[i + 1] == '*') {
/* C block comment open-slash-star: closes on this line, or runs to
EOL and leaves in_block_comment set for the following lines. */
i += 2;
while(i < len) {
if(line[i] == '*' && i + 1 < len && line[i + 1] == '/') {
i += 2;
if(in_block_comment != NULL)
*in_block_comment = 0;
break;
}
i++;
}
if(i >= len && in_block_comment != NULL)
*in_block_comment = 1;
return i - index;
}
if(line[i] == '/' || line[i] == '%' ||
(syntax == UI_SYNTAX_MAKE && line[i] == '$') ||
(line[i] == '.' && i + 1 < len && (line[i + 1] == '/' || line[i + 1] == '.'))) {
@@ -2012,15 +2069,18 @@ ui_draw_text_area_selection(const char *text, int line_start, int line_end,
static void
ui_draw_syntax_line(const char *line, int len, int x, int y, int font,
UISyntaxMode syntax, UITextInputStyle style)
UISyntaxMode syntax, UITextInputStyle style,
int *in_block_comment)
{
char token[1024];
int offset = 0;
int first_token = 1;
while(offset < len) {
int was_in_block_comment = in_block_comment != NULL &&
*in_block_comment;
int token_len = ui_syntax_token_len(line, len, offset, syntax,
first_token);
first_token, in_block_comment);
Color color;
int token_is_first;
@@ -2031,8 +2091,11 @@ ui_draw_syntax_line(const char *line, int len, int x, int y, int font,
memcpy(token, line + offset, (size_t)token_len);
token[token_len] = '\0';
token_is_first = first_token;
color = ui_syntax_token_color(syntax, token, token_len, token_is_first,
style);
if(was_in_block_comment)
color = (Color){88, 88, 88, 255};
else
color = ui_syntax_token_color(syntax, token, token_len,
token_is_first, style);
if(token[0] != ' ' && token[0] != '\t')
first_token = 0;
DrawUIText(token, x, y, font, color);
@@ -2046,16 +2109,20 @@ ui_draw_text_area_text(const char *text, int cursor, int focused,
Rectangle bounds, int font, int line_gap,
int scroll_y, UISyntaxMode syntax,
UITextInputStyle style, int selection_start,
int selection_end)
int selection_end, int text_x_offset)
{
char line[1024];
char num[16];
int len;
int line_start = 0;
int line_no = 1;
int in_block_comment = 0;
int padding_x = style.padding_x > 0 ? style.padding_x : ScaleUIPx(10);
int padding_y = style.padding_y > 0 ? style.padding_y : ScaleUIPx(8);
int text_x = (int)bounds.x + padding_x;
int text_x = (int)bounds.x + padding_x + text_x_offset;
int text_y = (int)bounds.y + padding_y - scroll_y;
int draw_y = text_y;
Color gutter_color = (Color){88, 88, 88, 255};
if(text == NULL)
text = "";
@@ -2077,13 +2144,22 @@ ui_draw_text_area_text(const char *text, int cursor, int focused,
DrawUIText(line, text_x, draw_y, line_font, style.text);
else
ui_draw_syntax_line(line, line_len, text_x, draw_y,
line_font, syntax, style);
line_font, syntax, style,
&in_block_comment);
if(focused && cursor >= line_start && cursor <= i && (((int)(GetTime() * 2.0)) % 2 == 0)) {
int cursor_x = text_x + ui_text_column_x(text, line_start, cursor, line_font);
DrawRectangle(cursor_x, draw_y, ScaleUIPx(2), GetUITextLineHeight(line_font), style.cursor);
}
if(text_x_offset > 0) {
snprintf(num, sizeof(num), "%d", line_no);
DrawUIText(num,
(int)bounds.x + padding_x + text_x_offset -
MeasureUIText(num, line_font) - ScaleUIPx(6),
draw_y, line_font, gutter_color);
}
draw_y += line_h;
line_start = i + 1;
line_no++;
}
}
}
@@ -2133,6 +2209,18 @@ DrawUITextArea(UITextArea area)
line_h = GetUITextLineHeight(font) + line_gap;
padding_x = area.style.padding_x > 0 ? area.style.padding_x : ScaleUIPx(10);
padding_y = area.style.padding_y > 0 ? area.style.padding_y : ScaleUIPx(8);
int gutter_w = 0;
if(area.show_line_numbers) {
int total_lines = 1;
const char *p;
for(p = area.text; *p; p++)
if(*p == '\n')
total_lines++;
char digit_str[16];
snprintf(digit_str, sizeof(digit_str), "%d", total_lines);
/* Room for the widest line number plus a right gap. */
gutter_w = MeasureUIText(digit_str, font) + ScaleUIPx(12);
}
first_line_y = GetUIControlTextY("Hg", (int)area.bounds.y + padding_y,
line_h, font);
focused = *area.focused != 0;
@@ -2163,7 +2251,7 @@ DrawUITextArea(UITextArea area)
focused = 1;
ClaimUITextAreaFocus(area.focused);
clicked_cursor = ui_text_area_cursor_from_point(area.text, font, line_gap,
(int)area.bounds.x + padding_x, (int)area.bounds.y + padding_y,
(int)area.bounds.x + padding_x + gutter_w, (int)area.bounds.y + padding_y,
(int)mouse_world.x, (int)mouse_world.y, scroll_y);
*area.cursor_position = clicked_cursor;
if(g_ui_text_area_last_click_owner == area.focused &&
@@ -2215,7 +2303,7 @@ DrawUITextArea(UITextArea area)
focused = 1;
ClaimUITextAreaFocus(area.focused);
*area.cursor_position = ui_text_area_cursor_from_point(area.text, font, line_gap,
(int)area.bounds.x + padding_x, (int)area.bounds.y + padding_y,
(int)area.bounds.x + padding_x + gutter_w, (int)area.bounds.y + padding_y,
(int)mouse_world.x, (int)mouse_world.y, scroll_y);
if(ui_text_selection_matches(g_ui_text_area_selection, drag_id,
area.focused)) {
@@ -2465,13 +2553,13 @@ DrawUITextArea(UITextArea area)
ui_begin_world_clip((Rectangle){area.bounds.x + padding_x, area.bounds.y + padding_y,
area.bounds.width - padding_x * 2, area.bounds.height - padding_y * 2});
if(area.text[0] == '\0' && !focused && area.placeholder != NULL)
DrawUIText(area.placeholder, (int)area.bounds.x + padding_x,
DrawUIText(area.placeholder, (int)area.bounds.x + padding_x + gutter_w,
first_line_y, font, area.style.border);
else
ui_draw_text_area_text(area.text, *area.cursor_position, focused,
area.bounds, font, line_gap, scroll_y,
area.syntax, area.style, selection_start,
selection_end);
selection_end, gutter_w);
EndUIClip();
EndUIWidget(&widget);
return changed;