Random stuff to do with scripting

This commit is contained in:
2026-01-21 15:05:37 +00:00
parent 81da75dc15
commit 6dc0813b49
44 changed files with 1207 additions and 745 deletions

View File

@@ -76,6 +76,7 @@ void edit_replace(Editor *editor, Coord start, Coord end, const char *text,
uint32_t len);
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y);
char *get_selection(Editor *editor, uint32_t *out_len, Coord *out_start);
void selection_bounds(Editor *editor, Coord *out_start, Coord *out_end);
void editor_worker(Editor *editor);
void move_line_down(Editor *editor);
void move_line_up(Editor *editor);
@@ -127,12 +128,12 @@ inline static void utf8_normalize_edit(Editor *editor, TextEdit *edit) {
return;
}
if (edit->start.col < len)
edit->start.col = utf16_offset_to_utf8(line, edit->start.col);
edit->start.col = utf16_offset_to_utf8(line, len, edit->start.col);
else
edit->start.col = len;
if (edit->end.row == edit->start.row) {
if (edit->end.col < len)
edit->end.col = utf16_offset_to_utf8(line, edit->end.col);
edit->end.col = utf16_offset_to_utf8(line, len, edit->end.col);
else
edit->end.col = len;
free(it->buffer);
@@ -151,7 +152,7 @@ inline static void utf8_normalize_edit(Editor *editor, TextEdit *edit) {
return;
}
if (edit->end.col < len)
edit->end.col = utf16_offset_to_utf8(line, edit->end.col);
edit->end.col = utf16_offset_to_utf8(line, len, edit->end.col);
else
edit->end.col = len;
free(it->buffer);

24
include/editor/helpers.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef EDITOR_HELPERS_H
#define EDITOR_HELPERS_H
#include "editor/editor.h"
void insert_str(Editor *editor, char *c, uint32_t len);
void insert_char(Editor *editor, char c);
void normal_mode(Editor *editor);
void backspace_edit(Editor *editor);
void delete_prev_word(Editor *editor);
void delete_next_word(Editor *editor);
void clear_hooks_at_line(Editor *editor, uint32_t line);
void cursor_prev_word(Editor *editor);
void cursor_next_word(Editor *editor);
void select_all(Editor *editor);
void fetch_lsp_hover(Editor *editor);
void handle_mouse(Editor *editor, KeyEvent event);
void indent_current_line(Editor *editor);
void dedent_current_line(Editor *editor);
void paste(Editor *editor);
void copy(Editor *editor);
void cut(Editor *editor);
#endif

View File

@@ -37,6 +37,7 @@ struct LSPInstance {
bool allow_resolve = false;
bool allow_formatting = false;
bool allow_formatting_on_type = false;
bool is_utf8 = false;
std::vector<char> format_chars;
std::vector<char> trigger_chars;
std::vector<char> end_chars;
@@ -53,6 +54,7 @@ extern std::unordered_map<uint8_t, std::shared_ptr<LSPInstance>> active_lsps;
extern Queue<LSPOpenRequest> lsp_open_queue;
static json client_capabilities = {
{"general", {{"positionEncodings", {"utf-16"}}}},
{"textDocument",
{{"publishDiagnostics", {{"relatedInformation", true}}},
{"hover", {{"contentFormat", {"markdown", "plaintext"}}}},

View File

@@ -4,6 +4,7 @@
#define PCRE2_CODE_UNIT_WIDTH 8
#define PCRE_WORKSPACE_SIZE 512
#include "ruby.h"
#include <magic.h>
#include <nlohmann/json.hpp>
#include <pcre2.h>

12
include/scripting/decl.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef SCRIPTING_DECL_H
#define SCRIPTING_DECL_H
#include "utils/utils.h"
void ruby_start(const char *main_file);
void ruby_shutdown();
void load_theme();
std::string read_line_endings();
std::string read_utf_mode();
#endif

View File

@@ -6,9 +6,9 @@
#include "syntax/trie.h"
struct Highlight {
uint32_t fg;
uint32_t bg;
uint8_t flags;
uint32_t fg{0xFFFFFF};
uint32_t bg{0x000000};
uint8_t flags{0};
};
enum struct TokenKind : uint8_t {
@@ -28,55 +28,6 @@ const std::unordered_map<std::string, TokenKind> kind_map = {
extern std::array<Highlight, TOKEN_KIND_COUNT> highlights;
inline void load_theme(std::string filename) {
uint32_t len = 0;
char *raw = load_file(filename.c_str(), &len);
if (!raw)
return;
std::string data(raw, len);
free(raw);
json j = json::parse(data);
Highlight default_hl = {0xFFFFFF, 0, 0};
if (j.contains("Default")) {
auto def = j["Default"];
if (def.contains("fg") && def["fg"].is_string())
default_hl.fg = HEX(def["fg"]);
if (def.contains("bg") && def["bg"].is_string())
default_hl.bg = HEX(def["bg"]);
if (def.contains("italic") && def["italic"].get<bool>())
default_hl.flags |= CF_ITALIC;
if (def.contains("bold") && def["bold"].get<bool>())
default_hl.flags |= CF_BOLD;
if (def.contains("underline") && def["underline"].get<bool>())
default_hl.flags |= CF_UNDERLINE;
if (def.contains("strikethrough") && def["strikethrough"].get<bool>())
default_hl.flags |= CF_STRIKETHROUGH;
}
for (auto &hl : highlights)
hl = default_hl;
for (auto &[key, value] : j.items()) {
if (key == "Default")
continue;
auto it = kind_map.find(key);
if (it == kind_map.end())
continue;
Highlight hl = {0xFFFFFF, 0, 0};
if (value.contains("fg") && value["fg"].is_string())
hl.fg = HEX(value["fg"]);
if (value.contains("bg") && value["bg"].is_string())
hl.bg = HEX(value["bg"]);
if (value.contains("italic") && value["italic"].get<bool>())
hl.flags |= CF_ITALIC;
if (value.contains("bold") && value["bold"].get<bool>())
hl.flags |= CF_BOLD;
if (value.contains("underline") && value["underline"].get<bool>())
hl.flags |= CF_UNDERLINE;
if (value.contains("strikethrough") && value["strikethrough"].get<bool>())
hl.flags |= CF_STRIKETHROUGH;
highlights[static_cast<uint8_t>(it->second)] = hl;
}
}
struct Token {
uint32_t start;
uint32_t end;

View File

@@ -453,7 +453,7 @@ private:
uint8_t g = (color >> 8) & 0xFF;
uint8_t b = color & 0xFF;
double luminance = 0.299 * r + 0.587 * g + 0.114 * b;
return (luminance > 128) ? 0x000000 : 0xFFFFFF;
return (luminance > 128) ? 0x010101 : 0xFFFFFF;
}
uint32_t hslToRgb(double h, double s, double l) {

View File

@@ -1,53 +1,53 @@
ADD(Data)
ADD(Shebang)
ADD(Comment)
ADD(Error)
ADD(String)
ADD(Escape)
ADD(Interpolation)
ADD(Regexp)
ADD(Number)
ADD(True)
ADD(False)
ADD(Char)
ADD(Keyword)
ADD(KeywordOperator)
ADD(Operator)
ADD(Function)
ADD(Type)
ADD(Constant)
ADD(VariableInstance)
ADD(VariableGlobal)
ADD(Annotation)
ADD(Directive)
ADD(Label)
ADD(Brace1)
ADD(Brace2)
ADD(Brace3)
ADD(Brace4)
ADD(Brace5)
ADD(Heading1)
ADD(Heading2)
ADD(Heading3)
ADD(Heading4)
ADD(Heading5)
ADD(Heading6)
ADD(Blockquote)
ADD(List)
ADD(ListItem)
ADD(Code)
ADD(LanguageName)
ADD(LinkLabel)
ADD(ImageLabel)
ADD(Link)
ADD(Table)
ADD(TableHeader)
ADD(Italic)
ADD(Bold)
ADD(Underline)
ADD(Strikethrough)
ADD(HorixontalRule)
ADD(Tag)
ADD(Attribute)
ADD(CheckDone)
ADD(CheckNotDone)
ADD(K_DATA)
ADD(K_SHEBANG)
ADD(K_COMMENT)
ADD(K_ERROR)
ADD(K_STRING)
ADD(K_ESCAPE)
ADD(K_INTERPOLATION)
ADD(K_REGEXP)
ADD(K_NUMBER)
ADD(K_TRUE)
ADD(K_FALSE)
ADD(K_CHAR)
ADD(K_KEYWORD)
ADD(K_KEYWORDOPERATOR)
ADD(K_OPERATOR)
ADD(K_FUNCTION)
ADD(K_TYPE)
ADD(K_CONSTANT)
ADD(K_VARIABLEINSTANCE)
ADD(K_VARIABLEGLOBAL)
ADD(K_ANNOTATION)
ADD(K_DIRECTIVE)
ADD(K_LABEL)
ADD(K_BRACE1)
ADD(K_BRACE2)
ADD(K_BRACE3)
ADD(K_BRACE4)
ADD(K_BRACE5)
ADD(K_HEADING1)
ADD(K_HEADING2)
ADD(K_HEADING3)
ADD(K_HEADING4)
ADD(K_HEADING5)
ADD(K_HEADING6)
ADD(K_BLOCKQUOTE)
ADD(K_LIST)
ADD(K_LISTITEM)
ADD(K_CODE)
ADD(K_LANGUAGENAME)
ADD(K_LINKLABEL)
ADD(K_IMAGELABEL)
ADD(K_LINK)
ADD(K_TABLE)
ADD(K_TABLEHEADER)
ADD(K_ITALIC)
ADD(K_BOLD)
ADD(K_UNDERLINE)
ADD(K_STRIKETHROUGH)
ADD(K_HORIXONTALRULE)
ADD(K_TAG)
ADD(K_ATTRIBUTE)
ADD(K_CHECKDONE)
ADD(K_CHECKNOTDONE)

View File

@@ -92,8 +92,10 @@ uint32_t get_visual_col_from_bytes(const char *line, uint32_t len,
uint32_t byte_limit);
uint32_t get_bytes_from_visual_col(const char *line, uint32_t len,
uint32_t target_visual_col);
uint32_t utf8_byte_offset_to_utf16(const char *s, uint32_t byte_pos);
uint32_t utf16_offset_to_utf8(const char *s, uint32_t utf16_pos);
size_t utf8_offset_to_utf16(const char *utf8, size_t utf8_len,
size_t byte_offset);
size_t utf16_offset_to_utf8(const char *utf8, size_t utf8_len,
size_t utf16_offset);
uint8_t utf8_codepoint_width(unsigned char c);
void log(const char *fmt, ...);