Update mruby api.
This commit is contained in:
3
TODO.md
3
TODO.md
@@ -4,6 +4,7 @@ Copyright 2025 Syed Daanish
|
||||
|
||||
##### BTW Check each lsp with each of the features implemented
|
||||
|
||||
* Add ruby config for file detection (with `file` command by default)
|
||||
* [ ] Add mgems for most common things and a ruby library to allow combining true ruby with mruby
|
||||
- Or revert to cruby and retry with manual linking . maybe it might work?
|
||||
* [ ] color alpha in ini files
|
||||
@@ -30,7 +31,7 @@ Copyright 2025 Syed Daanish
|
||||
* Try to make all functions better now that folds have been purged
|
||||
* Cleanup syntax and renderer files
|
||||
|
||||
* Add a thing called view which is a rect with speacial type editor . but otherwise a ruby or c++ view
|
||||
* **RN** Add a thing called view which is a rect with speacial type editor . but otherwise a ruby or c++ view
|
||||
* can be used for stuff like file manager/git manager/theme picker.
|
||||
* allow flushing functions in ruby to tell c++ to refresh keybinds/themes etc.
|
||||
* allow keybinds to be set in ruby
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
# Files can be insluded using Kernel#require_relative
|
||||
# but it can be called with binding as the second argument
|
||||
# skipping it will call it with global binding which is usually fine
|
||||
# Kernel#load can also be used
|
||||
|
||||
require_relative "theme"
|
||||
|
||||
# basic configuration
|
||||
# This can also be used to do speacail configs for different projects.
|
||||
# its ruby guys script whatever you want.
|
||||
@@ -12,43 +19,6 @@ C.shutdown do
|
||||
puts "Exiting crib..."
|
||||
end
|
||||
|
||||
# this can be modified by the user during runtime through keybindings
|
||||
# But i need to know how to ever read this value only when needed.
|
||||
# maybe i can write a function that notifies if theme maybe changed then reload
|
||||
# It can also be scripted to load different theme formats into a hash usable by crib
|
||||
C.theme = {
|
||||
:default => { fg: 0xEEEEEE },
|
||||
:shebang => { fg: 0x7DCFFF },
|
||||
:error => { fg: 0xEF5168 },
|
||||
:comment => { fg: 0xAAAAAA, italic: true },
|
||||
:string => { fg: 0xAAD94C },
|
||||
:escape => { fg: 0x7DCFFF },
|
||||
:interpolation => { fg: 0x7DCFFF },
|
||||
:regexp => { fg: 0xD2A6FF },
|
||||
:number => { fg: 0xE6C08A },
|
||||
# rubocop:disable Lint/BooleanSymbol
|
||||
:true => { fg: 0x7AE93C },
|
||||
:false => { fg: 0xEF5168 },
|
||||
# rubocop:enable Lint/BooleanSymbol
|
||||
:char => { fg: 0xFFAF70 },
|
||||
:keyword => { fg: 0xFF8F40 },
|
||||
:keywordoperator => { fg: 0xF07178 },
|
||||
:operator => { fg: 0xFFFFFF, italic: true },
|
||||
:function => { fg: 0xFFAF70 },
|
||||
:type => { fg: 0xF07178 },
|
||||
:constant => { fg: 0x7DCFFF },
|
||||
:variableinstance => { fg: 0x95E6CB },
|
||||
:variableglobal => { fg: 0xF07178 },
|
||||
:annotation => { fg: 0x7DCFFF },
|
||||
:directive => { fg: 0xFF8F40 },
|
||||
:label => { fg: 0xD2A6FF },
|
||||
:brace1 => { fg: 0xD2A6FF },
|
||||
:brace2 => { fg: 0xFFAFAF },
|
||||
:brace3 => { fg: 0xFFFF00 },
|
||||
:brace4 => { fg: 0x0FFF0F },
|
||||
:brace5 => { fg: 0xFF0F0F }
|
||||
}
|
||||
|
||||
# TODO: to be done once a proper api for binding and window drawing is made
|
||||
# The binds will be connected to either `editor` or windows where editor can
|
||||
# only use a preset set of stuff to bind while teh windows are purely custom
|
||||
@@ -99,7 +69,7 @@ C.extra_highlights do |_line, _idx|
|
||||
end
|
||||
|
||||
# The highlighter will be aplied to the language as long as the langauge is defined in C.languages
|
||||
C.highlighters[:ruby_n] = {
|
||||
C.highlighters[:string] = {
|
||||
parser: ->(line, state, line_idx) {
|
||||
# the return value is a hash
|
||||
# it contains the state and the highlights
|
||||
|
||||
36
config/theme.rb
Normal file
36
config/theme.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
# this can be modified by the user during runtime through keybindings
|
||||
# But i need to know how to ever read this value only when needed.
|
||||
# maybe i can write a function that notifies if theme maybe changed then reload
|
||||
# It can also be scripted to load different theme formats into a hash usable by crib
|
||||
C.theme = {
|
||||
:default => { fg: 0xEEEEEE },
|
||||
:shebang => { fg: 0x7DCFFF },
|
||||
:error => { fg: 0xEF5168 },
|
||||
:comment => { fg: 0xAAAAAA, italic: true },
|
||||
:string => { fg: 0xAAD94C },
|
||||
:escape => { fg: 0x7DCFFF },
|
||||
:interpolation => { fg: 0x7DCFFF },
|
||||
:regexp => { fg: 0xD2A6FF },
|
||||
:number => { fg: 0xE6C08A },
|
||||
# rubocop:disable Lint/BooleanSymbol
|
||||
:true => { fg: 0x7AE93C },
|
||||
:false => { fg: 0xEF5168 },
|
||||
# rubocop:enable Lint/BooleanSymbol
|
||||
:char => { fg: 0xFFAF70 },
|
||||
:keyword => { fg: 0xFF8F40 },
|
||||
:keywordoperator => { fg: 0xF07178 },
|
||||
:operator => { fg: 0xFFFFFF, italic: true },
|
||||
:function => { fg: 0xFFAF70 },
|
||||
:type => { fg: 0xF07178 },
|
||||
:constant => { fg: 0x7DCFFF },
|
||||
:variableinstance => { fg: 0x95E6CB },
|
||||
:variableglobal => { fg: 0xF07178 },
|
||||
:annotation => { fg: 0x7DCFFF },
|
||||
:directive => { fg: 0xFF8F40 },
|
||||
:label => { fg: 0xD2A6FF },
|
||||
:brace1 => { fg: 0xD2A6FF },
|
||||
:brace2 => { fg: 0xFFAFAF },
|
||||
:brace3 => { fg: 0xFFFF00 },
|
||||
:brace4 => { fg: 0x0FFF0F },
|
||||
:brace5 => { fg: 0xFF0F0F }
|
||||
}
|
||||
@@ -4,9 +4,12 @@
|
||||
#include "syntax/decl.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
extern std::unordered_map<std::string, std::pair<mrb_value, mrb_value>>
|
||||
custom_highlighters;
|
||||
extern mrb_state *mrb;
|
||||
extern fs::path ruby_config_path;
|
||||
|
||||
struct BarLight {
|
||||
uint32_t start;
|
||||
@@ -32,14 +35,16 @@ void ruby_start();
|
||||
void ruby_shutdown();
|
||||
void ruby_copy(const char *text, size_t len);
|
||||
std::string ruby_paste();
|
||||
std::string ruby_file_detect(std::string filename);
|
||||
void load_theme();
|
||||
void load_languages_info();
|
||||
uint8_t read_line_endings();
|
||||
void load_custom_highlighters();
|
||||
mrb_value parse_custom(std::vector<Token> *tokens, mrb_value parser_block,
|
||||
const char *line, uint32_t len, mrb_value state,
|
||||
bool custom_compare(mrb_value match_block, std::string state1,
|
||||
std::string state2);
|
||||
std::string parse_custom(std::vector<Token> *tokens, mrb_value parser_block,
|
||||
const char *line, uint32_t len, std::string state,
|
||||
uint32_t c_line);
|
||||
bool custom_compare(mrb_value match_block, mrb_value state1, mrb_value state2);
|
||||
BarLine bar_contents(uint8_t mode, std::string lang_name, uint32_t warnings,
|
||||
std::string lsp_name, std::string filename,
|
||||
std::string foldername, uint32_t line, uint32_t max_line,
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
def command_exists?(cmd)
|
||||
system("command -v #{cmd} > /dev/null 2>&1")
|
||||
end
|
||||
|
||||
module Clipboard
|
||||
@clip = ""
|
||||
@os = :os_name_placed_here
|
||||
|
||||
class << self
|
||||
def command_exists?(cmd)
|
||||
system("command -v #{cmd} > /dev/null 2>&1")
|
||||
end
|
||||
def copy(text)
|
||||
if @os == :windows
|
||||
IO.popen("clip", "w") { |f| f.write(text) }
|
||||
@@ -375,12 +376,44 @@ module C
|
||||
@b_paste = proc do
|
||||
next Clipboard.paste
|
||||
end
|
||||
@b_file_detect = proc do |filename|
|
||||
type = :default
|
||||
next type unless File.exist?(filename)
|
||||
first_line = File.open(filename, &:readline).chomp
|
||||
if first_line.start_with?("#!")
|
||||
shebang = first_line[2..].downcase
|
||||
type = case shebang
|
||||
when /bash/, /sh/ then :bash
|
||||
when /fish/ then :fish
|
||||
when /python/ then :python
|
||||
when /ruby/ then :ruby
|
||||
when /lua/ then :lua
|
||||
else :default
|
||||
end
|
||||
next type
|
||||
end
|
||||
next type if :os_name_placed_here != :linux || :os_name_placed_here != :mac
|
||||
next type if !command_exists?("file")
|
||||
mimetype = `file --mime-type -b #{filename}`.chomp
|
||||
type = case mimetype
|
||||
when /shellscript/ then :bash
|
||||
when /ruby/ then :ruby
|
||||
when /diff/ then :diff
|
||||
when /html/ then :html
|
||||
when /python/ then :python
|
||||
when /javascript/ then :javascript
|
||||
when /makefile/ then :makefile
|
||||
when /-c$/ then :c
|
||||
else :default
|
||||
end
|
||||
next type
|
||||
end
|
||||
|
||||
class << self
|
||||
attr_accessor :theme, :lsp_config, :languages,
|
||||
:line_endings, :highlighters
|
||||
attr_reader :b_startup, :b_shutdown, :b_extra_highlights,
|
||||
:b_bar, :b_copy, :b_paste
|
||||
:b_bar, :b_copy, :b_paste, :b_file_detect
|
||||
|
||||
def bar=(&block)
|
||||
@b_bar = block
|
||||
@@ -402,6 +435,10 @@ module C
|
||||
@b_paste = block
|
||||
end
|
||||
|
||||
def file_detect(&block)
|
||||
@b_file_detect = block
|
||||
end
|
||||
|
||||
def extra_highlights(&block)
|
||||
@b_extra_highlights = block
|
||||
end
|
||||
@@ -437,3 +474,22 @@ module C
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
$LOADED ||= []
|
||||
|
||||
module Kernel
|
||||
def require_relative(path, bind = nil)
|
||||
path += ".rb" unless path.end_with?(".rb")
|
||||
path = File.expand_path(path, File.dirname(C.config_file))
|
||||
return if $LOADED.include?(path)
|
||||
$LOADED << path
|
||||
code = File.read(path)
|
||||
eval(code, bind || binding, path)
|
||||
end
|
||||
def load(path, bind = nil)
|
||||
path += ".rb" unless path.end_with?(".rb")
|
||||
path = File.expand_path(path, File.dirname(C.config_file))
|
||||
$LOADED.delete(path)
|
||||
require_relative(path, bind)
|
||||
end
|
||||
end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,12 +16,6 @@
|
||||
#name, { name##_parse, name##_state_match } \
|
||||
}
|
||||
|
||||
struct CustomState {
|
||||
mrb_value state;
|
||||
CustomState(mrb_value s) : state(s) { mrb_gc_register(mrb, state); }
|
||||
~CustomState() { mrb_gc_unregister(mrb, state); }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline std::shared_ptr<T> ensure_state(std::shared_ptr<T> state) {
|
||||
using U = typename T::full_state_type;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#include "main.h"
|
||||
#include "scripting/decl.h"
|
||||
|
||||
mrb_value get_mode(mrb_state *mrb, mrb_value self) {
|
||||
return mrb_fixnum_value(mode);
|
||||
mrb_value get_config_file(mrb_state *mrb, mrb_value self) {
|
||||
return mrb_str_new_cstr(mrb, ruby_config_path.string().c_str());
|
||||
}
|
||||
|
||||
void setup_ruby_bindings(mrb_state *mrb, RClass *C_module) {
|
||||
mrb_define_module_function(mrb, C_module, "mode", get_mode, MRB_ARGS_NONE());
|
||||
mrb_define_module_function(mrb, C_module, "config_file", get_config_file,
|
||||
MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
@@ -28,8 +28,7 @@ struct R_Language {
|
||||
|
||||
mrb_state *mrb = nullptr;
|
||||
RClass *C_module;
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
fs::path ruby_config_path;
|
||||
|
||||
void ruby_start() {
|
||||
mrb = mrb_open();
|
||||
@@ -61,6 +60,7 @@ void ruby_start() {
|
||||
if (fs::exists(p)) {
|
||||
FILE *f = fopen(p.string().c_str(), "r");
|
||||
if (f) {
|
||||
ruby_config_path = p;
|
||||
mrb_load_file(mrb, f);
|
||||
if (mrb->exc)
|
||||
exit(1);
|
||||
@@ -204,6 +204,38 @@ BarLine bar_contents(uint8_t mode, std::string lang_name, uint32_t warnings,
|
||||
return bar_line;
|
||||
}
|
||||
|
||||
std::string serialize_value(mrb_state *mrb, mrb_value val) {
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
mrb_value marshal_module = mrb_obj_value(mrb_module_get(mrb, "Marshal"));
|
||||
mrb_value dumped = mrb_funcall(mrb, marshal_module, "dump", 1, val);
|
||||
if (mrb->exc) {
|
||||
end_screen();
|
||||
fputs("Error when executing Ruby code:\n", stderr);
|
||||
mrb_print_error(mrb);
|
||||
mrb_close(mrb);
|
||||
exit(1);
|
||||
}
|
||||
std::string bytes(RSTRING_PTR(dumped), RSTRING_LEN(dumped));
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
mrb_value deserialize_value(mrb_state *mrb, std::string bytes) {
|
||||
if (bytes.empty())
|
||||
return mrb_nil_value();
|
||||
mrb_value marshal_module = mrb_obj_value(mrb_module_get(mrb, "Marshal"));
|
||||
mrb_value val = mrb_funcall(mrb, marshal_module, "load", 1,
|
||||
mrb_str_new(mrb, bytes.c_str(), bytes.length()));
|
||||
if (mrb->exc) {
|
||||
end_screen();
|
||||
fputs("Error when executing Ruby code:\n", stderr);
|
||||
mrb_print_error(mrb);
|
||||
mrb_close(mrb);
|
||||
exit(1);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
void ruby_copy(const char *text, size_t len) {
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
if (C_module == nullptr)
|
||||
@@ -222,6 +254,48 @@ void ruby_copy(const char *text, size_t len) {
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
}
|
||||
|
||||
std::string ruby_file_detect(std::string filename) {
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
if (C_module == nullptr)
|
||||
return "";
|
||||
mrb_value mod_val = mrb_obj_value(C_module);
|
||||
mrb_value block = mrb_funcall(mrb, mod_val, "b_file_detect", 0);
|
||||
if (mrb->exc) {
|
||||
end_screen();
|
||||
fputs("Error when executing Ruby code:\n", stderr);
|
||||
mrb_print_error(mrb);
|
||||
mrb_close(mrb);
|
||||
exit(1);
|
||||
}
|
||||
if (!mrb_nil_p(block)) {
|
||||
mrb_value val =
|
||||
mrb_funcall(mrb, block, "call", 1,
|
||||
mrb_str_new(mrb, filename.c_str(), filename.length()));
|
||||
if (mrb->exc) {
|
||||
end_screen();
|
||||
fputs("Error when executing Ruby code:\n", stderr);
|
||||
mrb_print_error(mrb);
|
||||
mrb_close(mrb);
|
||||
exit(1);
|
||||
}
|
||||
mrb_value s_val = mrb_funcall(mrb, val, "to_s", 0);
|
||||
if (mrb->exc) {
|
||||
end_screen();
|
||||
fputs("Error when executing Ruby code:\n", stderr);
|
||||
mrb_print_error(mrb);
|
||||
mrb_close(mrb);
|
||||
exit(1);
|
||||
}
|
||||
if (mrb_string_p(s_val)) {
|
||||
std::string result = std::string(RSTRING_PTR(s_val), RSTRING_LEN(s_val));
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string ruby_paste() {
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
if (C_module == nullptr)
|
||||
@@ -302,23 +376,33 @@ void load_custom_highlighters() {
|
||||
mrb_garbage_collect(mrb);
|
||||
}
|
||||
|
||||
bool custom_compare(mrb_value match_block, mrb_value state1, mrb_value state2) {
|
||||
bool custom_compare(mrb_value match_block, std::string state1,
|
||||
std::string state2) {
|
||||
if (state1.empty() || state2.empty())
|
||||
return false;
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
if (mrb_type(match_block) != MRB_TT_PROC)
|
||||
return false;
|
||||
mrb_value ret = mrb_funcall(mrb, match_block, "call", 2, state1, state2);
|
||||
return mrb_test(ret);
|
||||
mrb_value ret =
|
||||
mrb_funcall(mrb, match_block, "call", 2, deserialize_value(mrb, state1),
|
||||
deserialize_value(mrb, state2));
|
||||
bool result = mrb_test(ret);
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
return result;
|
||||
}
|
||||
|
||||
mrb_value parse_custom(std::vector<Token> *tokens, mrb_value parser_block,
|
||||
const char *line, uint32_t len, mrb_value state,
|
||||
std::string parse_custom(std::vector<Token> *tokens, mrb_value parser_block,
|
||||
const char *line, uint32_t len, std::string state,
|
||||
uint32_t c_line) {
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
tokens->clear();
|
||||
if (mrb_nil_p(parser_block))
|
||||
return mrb_nil_value();
|
||||
return "";
|
||||
mrb_value ruby_line = mrb_str_new(mrb, line, len);
|
||||
mrb_value line_idx = mrb_fixnum_value(c_line);
|
||||
mrb_value tokens_and_state_hash =
|
||||
mrb_funcall(mrb, parser_block, "call", 3, ruby_line, state, line_idx);
|
||||
mrb_funcall(mrb, parser_block, "call", 3, ruby_line,
|
||||
deserialize_value(mrb, state), line_idx);
|
||||
mrb_sym tokens_sym = mrb_intern_lit(mrb, "tokens");
|
||||
mrb_value tokens_rb =
|
||||
mrb_hash_get(mrb, tokens_and_state_hash, mrb_symbol_value(tokens_sym));
|
||||
@@ -338,7 +422,13 @@ mrb_value parse_custom(std::vector<Token> *tokens, mrb_value parser_block,
|
||||
}
|
||||
}
|
||||
mrb_sym state_sym = mrb_intern_lit(mrb, "state");
|
||||
return mrb_hash_get(mrb, tokens_and_state_hash, mrb_symbol_value(state_sym));
|
||||
mrb_value state_rb =
|
||||
mrb_hash_get(mrb, tokens_and_state_hash, mrb_symbol_value(state_sym));
|
||||
std::string result;
|
||||
if (mrb_type(state_rb) == MRB_TT_STRING)
|
||||
result = std::string(RSTRING_PTR(state_rb), RSTRING_LEN(state_rb));
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::vector<R_ThemeEntry> read_theme() {
|
||||
|
||||
@@ -88,12 +88,12 @@ void Parser::work() {
|
||||
}
|
||||
std::shared_ptr<void> new_state;
|
||||
if (is_custom) {
|
||||
mrb_value state = mrb_nil_value();
|
||||
std::string state = "";
|
||||
if (prev_state)
|
||||
state = std::static_pointer_cast<CustomState>(prev_state)->state;
|
||||
mrb_value out_state = parse_custom(&line_data->tokens, parser_block,
|
||||
state = std::static_pointer_cast<std::string>(prev_state)->c_str();
|
||||
std::string out_state = parse_custom(&line_data->tokens, parser_block,
|
||||
line, len, state, cur_line);
|
||||
new_state = std::make_shared<CustomState>(out_state);
|
||||
new_state = std::make_shared<std::string>(out_state);
|
||||
} else {
|
||||
new_state =
|
||||
parse_func(&line_data->tokens, prev_state, line, len, cur_line);
|
||||
@@ -105,15 +105,15 @@ void Parser::work() {
|
||||
LineData *next_line_data = line_tree.at(cur_line + 1);
|
||||
if (next_line_data) {
|
||||
if (is_custom) {
|
||||
mrb_value a =
|
||||
std::string a =
|
||||
prev_state
|
||||
? std::static_pointer_cast<CustomState>(new_state)->state
|
||||
: mrb_nil_value();
|
||||
mrb_value b = next_line_data->in_state
|
||||
? std::static_pointer_cast<CustomState>(
|
||||
? std::static_pointer_cast<std::string>(new_state)->c_str()
|
||||
: "";
|
||||
std::string b = next_line_data->in_state
|
||||
? std::static_pointer_cast<std::string>(
|
||||
next_line_data->in_state)
|
||||
->state
|
||||
: mrb_nil_value();
|
||||
->c_str()
|
||||
: "";
|
||||
done = custom_compare(match_block, a, b);
|
||||
} else {
|
||||
done = state_match_func(new_state, next_line_data->in_state);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "scripting/decl.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
std::unordered_map<std::string, Language> languages;
|
||||
@@ -107,11 +108,16 @@ static std::string file_extension(const char *filename) {
|
||||
|
||||
Language language_for_file(const char *filename) {
|
||||
std::string ext = file_extension(filename);
|
||||
std::string lang_name;
|
||||
if (!ext.empty()) {
|
||||
auto it = language_extensions.find(ext);
|
||||
if (it != language_extensions.end())
|
||||
return languages.find(it->second)->second;
|
||||
}
|
||||
std::string lang_name = ruby_file_detect(filename);
|
||||
if (!lang_name.empty()) {
|
||||
auto it = languages.find(lang_name);
|
||||
if (it != languages.end())
|
||||
return it->second;
|
||||
}
|
||||
return Language{};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user