mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mirb: share is_word_char() and COLOR_RESET across modules
Move is_word_char() to mirb_buffer.h as mirb_is_word_char() static inline, removing duplicate definitions from mirb_buffer.c and mirb_highlight.c. Move COLOR_RESET to mirb_highlight.h, removing the duplicate from mirb_editor.c. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#ifdef MRB_UTF8_STRING
|
||||
/*
|
||||
@@ -782,15 +781,6 @@ mirb_buffer_cursor_finish(mirb_buffer *buf)
|
||||
buf->cursor_col = buf->lines[buf->cursor_line].len;
|
||||
}
|
||||
|
||||
/*
|
||||
* Helper: Check if character is word character
|
||||
*/
|
||||
static mrb_bool
|
||||
is_word_char(char c)
|
||||
{
|
||||
return isalnum((unsigned char)c) || c == '_';
|
||||
}
|
||||
|
||||
/*
|
||||
* Move cursor back one word
|
||||
*/
|
||||
@@ -810,7 +800,7 @@ mirb_buffer_cursor_word_back(mirb_buffer *buf)
|
||||
}
|
||||
|
||||
char c = buf->lines[buf->cursor_line].data[buf->cursor_col - 1];
|
||||
if (is_word_char(c)) break;
|
||||
if (mirb_is_word_char(c)) break;
|
||||
buf->cursor_col--;
|
||||
moved = TRUE;
|
||||
}
|
||||
@@ -818,7 +808,7 @@ mirb_buffer_cursor_word_back(mirb_buffer *buf)
|
||||
/* Move through word chars */
|
||||
while (buf->cursor_col > 0) {
|
||||
char c = buf->lines[buf->cursor_line].data[buf->cursor_col - 1];
|
||||
if (!is_word_char(c)) break;
|
||||
if (!mirb_is_word_char(c)) break;
|
||||
buf->cursor_col--;
|
||||
moved = TRUE;
|
||||
}
|
||||
@@ -837,7 +827,7 @@ mirb_buffer_cursor_word_forward(mirb_buffer *buf)
|
||||
|
||||
/* Move through current word chars */
|
||||
while (buf->cursor_col < line->len) {
|
||||
if (!is_word_char(line->data[buf->cursor_col])) break;
|
||||
if (!mirb_is_word_char(line->data[buf->cursor_col])) break;
|
||||
buf->cursor_col++;
|
||||
moved = TRUE;
|
||||
}
|
||||
@@ -853,7 +843,7 @@ mirb_buffer_cursor_word_forward(mirb_buffer *buf)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_word_char(line->data[buf->cursor_col])) break;
|
||||
if (mirb_is_word_char(line->data[buf->cursor_col])) break;
|
||||
buf->cursor_col++;
|
||||
moved = TRUE;
|
||||
}
|
||||
@@ -960,12 +950,12 @@ mirb_buffer_kill_word_forward(mirb_buffer *buf)
|
||||
size_t end_col = start_col;
|
||||
|
||||
/* Skip word chars */
|
||||
while (end_col < line->len && is_word_char(line->data[end_col])) {
|
||||
while (end_col < line->len && mirb_is_word_char(line->data[end_col])) {
|
||||
end_col++;
|
||||
}
|
||||
|
||||
/* Skip non-word chars */
|
||||
while (end_col < line->len && !is_word_char(line->data[end_col])) {
|
||||
while (end_col < line->len && !mirb_is_word_char(line->data[end_col])) {
|
||||
end_col++;
|
||||
}
|
||||
|
||||
|
||||
@@ -182,4 +182,14 @@ size_t mirb_buffer_line_len(mirb_buffer *buf, size_t index);
|
||||
*/
|
||||
size_t mirb_buffer_cursor_display_col(mirb_buffer *buf);
|
||||
|
||||
/*
|
||||
* Check if character is a word character (alphanumeric or underscore)
|
||||
*/
|
||||
static inline mrb_bool
|
||||
mirb_is_word_char(char c)
|
||||
{
|
||||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9') || c == '_';
|
||||
}
|
||||
|
||||
#endif /* MIRB_BUFFER_H */
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
/* ANSI color codes */
|
||||
#define COLOR_GREEN "\033[32m"
|
||||
#define COLOR_RESET "\033[0m"
|
||||
|
||||
/*
|
||||
* Check if line contains only whitespace before given column
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
*/
|
||||
|
||||
#include "mirb_highlight.h"
|
||||
#include "mirb_buffer.h"
|
||||
#include "mirb_term.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/* ANSI color codes - using standard 16-color palette for compatibility */
|
||||
|
||||
@@ -41,8 +41,6 @@
|
||||
#define LIGHT_ERROR "\033[31m" /* red */
|
||||
#define LIGHT_ARROW "\033[90m" /* gray */
|
||||
|
||||
#define COLOR_RESET "\033[0m"
|
||||
|
||||
/* Keyword list - sorted alphabetically for bsearch, NULL-terminated */
|
||||
const char *mirb_keywords[] = {
|
||||
"BEGIN", "END", "__ENCODING__", "__FILE__", "__LINE__",
|
||||
@@ -72,13 +70,6 @@ is_keyword(const char *word, size_t len)
|
||||
return bsearch(buf, mirb_keywords, mirb_num_keywords, sizeof(mirb_keywords[0]), keyword_cmp) != NULL;
|
||||
}
|
||||
|
||||
static mrb_bool
|
||||
is_word_char(char c)
|
||||
{
|
||||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9') || c == '_';
|
||||
}
|
||||
|
||||
static mrb_bool
|
||||
is_word_start(char c)
|
||||
{
|
||||
@@ -365,7 +356,7 @@ mirb_highlight_print_line(mirb_highlighter *hl, const char *line)
|
||||
}
|
||||
else {
|
||||
/* Regular symbol */
|
||||
while (*p && (is_word_char(*p) || *p == '?' || *p == '!')) p++;
|
||||
while (*p && (mirb_is_word_char(*p) || *p == '?' || *p == '!')) p++;
|
||||
}
|
||||
print_colored(hl, token_start, (size_t)(p - token_start), MIRB_TOK_SYMBOL);
|
||||
continue;
|
||||
@@ -375,7 +366,7 @@ mirb_highlight_print_line(mirb_highlighter *hl, const char *line)
|
||||
if (*p == '@') {
|
||||
token_start = p++;
|
||||
if (*p == '@') p++; /* @@class_var */
|
||||
while (*p && is_word_char(*p)) p++;
|
||||
while (*p && mirb_is_word_char(*p)) p++;
|
||||
print_colored(hl, token_start, (size_t)(p - token_start), MIRB_TOK_IVAR);
|
||||
continue;
|
||||
}
|
||||
@@ -384,11 +375,11 @@ mirb_highlight_print_line(mirb_highlighter *hl, const char *line)
|
||||
if (*p == '$') {
|
||||
token_start = p++;
|
||||
/* Special globals like $!, $?, $1, etc. */
|
||||
if (*p && !is_word_char(*p) && *p != ' ') {
|
||||
if (*p && !mirb_is_word_char(*p) && *p != ' ') {
|
||||
p++;
|
||||
}
|
||||
else {
|
||||
while (*p && is_word_char(*p)) p++;
|
||||
while (*p && mirb_is_word_char(*p)) p++;
|
||||
}
|
||||
print_colored(hl, token_start, (size_t)(p - token_start), MIRB_TOK_GVAR);
|
||||
continue;
|
||||
@@ -396,7 +387,7 @@ mirb_highlight_print_line(mirb_highlighter *hl, const char *line)
|
||||
|
||||
/* Numbers */
|
||||
if ((*p >= '0' && *p <= '9') ||
|
||||
(*p == '-' && p[1] >= '0' && p[1] <= '9' && (p == line || !is_word_char(p[-1])))) {
|
||||
(*p == '-' && p[1] >= '0' && p[1] <= '9' && (p == line || !mirb_is_word_char(p[-1])))) {
|
||||
token_start = p;
|
||||
if (*p == '-') p++;
|
||||
if (*p == '0' && (p[1] == 'x' || p[1] == 'X')) {
|
||||
@@ -440,7 +431,7 @@ mirb_highlight_print_line(mirb_highlighter *hl, const char *line)
|
||||
mrb_bool is_const = is_upper(*p);
|
||||
/* Check if preceded by dot (method call like obj.class) */
|
||||
mrb_bool after_dot = (token_start > line && token_start[-1] == '.');
|
||||
while (*p && (is_word_char(*p) || *p == '?' || *p == '!')) p++;
|
||||
while (*p && (mirb_is_word_char(*p) || *p == '?' || *p == '!')) p++;
|
||||
|
||||
size_t len = (size_t)(p - token_start);
|
||||
|
||||
|
||||
@@ -98,4 +98,7 @@ void mirb_highlight_print_result(mirb_highlighter *hl, const char *result);
|
||||
*/
|
||||
void mirb_highlight_print_error(mirb_highlighter *hl, const char *error);
|
||||
|
||||
/* Common ANSI reset code shared across modules */
|
||||
#define COLOR_RESET "\033[0m"
|
||||
|
||||
#endif /* MIRB_HIGHLIGHT_H */
|
||||
|
||||
Reference in New Issue
Block a user