Fix dependancies and precompile ruby module
This commit is contained in:
10
src/main.cc
10
src/main.cc
@@ -72,8 +72,6 @@ void input_listener(Bar bar) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
ruby_start();
|
||||
load_theme();
|
||||
load_languages_info();
|
||||
@@ -81,19 +79,11 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
Coord screen = start_screen();
|
||||
const char *filename = (argc > 1) ? argv[1] : "";
|
||||
|
||||
uint8_t eol = read_line_endings();
|
||||
|
||||
Editor *editor =
|
||||
new_editor(filename, {0, 0}, {screen.row - 2, screen.col}, eol);
|
||||
Bar bar(screen);
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
|
||||
.count();
|
||||
ruby_log("[LOG] STARTUP_TIME: " + std::to_string(static_cast<long long>(ms)) +
|
||||
"ms");
|
||||
|
||||
if (!editor) {
|
||||
end_screen();
|
||||
fprintf(stderr, "Failed to load editor\n");
|
||||
|
||||
38
src/ruby_compile.sh
Executable file
38
src/ruby_compile.sh
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||
INPUT="$SCRIPT_DIR/../include/syntax/tokens.def"
|
||||
TMP="/tmp/__crib_precompiled.rb"
|
||||
OUTPUT="/tmp/__crib_precompiled.mrb"
|
||||
|
||||
echo "module Tokens" >"$TMP"
|
||||
|
||||
counter=0
|
||||
|
||||
while read -r line; do
|
||||
if [[ $line =~ ADD\(([^\)]+)\) ]]; then
|
||||
name="${BASH_REMATCH[1]}"
|
||||
echo " $name = $counter" >>"$TMP"
|
||||
counter=$((counter + 1))
|
||||
fi
|
||||
done <"$INPUT"
|
||||
|
||||
{
|
||||
echo " freeze"
|
||||
echo "end"
|
||||
echo
|
||||
cat "$SCRIPT_DIR/../include/scripting/libcrib.rb"
|
||||
} >>"$TMP"
|
||||
|
||||
mrbc -o$OUTPUT $TMP
|
||||
|
||||
{
|
||||
echo "#pragma once"
|
||||
xxd -i $OUTPUT | sed 's/^unsigned char /constexpr unsigned char /' |
|
||||
sed 's/^unsigned int /constexpr unsigned int /'
|
||||
} >"$SCRIPT_DIR/../include/scripting/ruby_compiled.h"
|
||||
|
||||
rm $TMP
|
||||
rm $OUTPUT
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "scripting/decl.h"
|
||||
#include "scripting/ruby_compiled.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
std::unordered_map<std::string, std::pair<mrb_value, mrb_value>>
|
||||
@@ -20,7 +21,6 @@ struct R_Language {
|
||||
std::string symbol;
|
||||
std::vector<std::string> extensions;
|
||||
std::vector<std::string> filenames;
|
||||
std::vector<std::string> mimetypes;
|
||||
std::string lsp_command; // link to LSP by name
|
||||
};
|
||||
|
||||
@@ -54,13 +54,14 @@ void ruby_start() {
|
||||
}
|
||||
candidates.emplace_back(exe_dir / "../config/main.rb");
|
||||
candidates.emplace_back(exe_dir / "../config/crib.rb");
|
||||
mrb_load_string(mrb, crib_module);
|
||||
mrb_load_string(mrb, tokens_def);
|
||||
mrb_load_irep(mrb, _tmp___crib_precompiled_mrb);
|
||||
for (const auto &p : candidates) {
|
||||
if (fs::exists(p)) {
|
||||
FILE *f = fopen(p.string().c_str(), "r");
|
||||
if (f) {
|
||||
mrb_load_file(mrb, f);
|
||||
if (mrb->exc)
|
||||
exit(1);
|
||||
fclose(f);
|
||||
}
|
||||
break;
|
||||
@@ -76,7 +77,6 @@ void ruby_shutdown() {
|
||||
std::lock_guard lock(ruby_mutex);
|
||||
if (C_module == nullptr)
|
||||
return;
|
||||
C_module = mrb_module_get(mrb, "C");
|
||||
mrb_value mod_val = mrb_obj_value(C_module);
|
||||
mrb_value block = mrb_funcall(mrb, mod_val, "b_shutdown", 0);
|
||||
mrb_funcall(mrb, block, "call", 0);
|
||||
@@ -98,13 +98,6 @@ std::vector<std::string> array_to_vector(mrb_value ary) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void ruby_log(std::string msg) {
|
||||
std::lock_guard lock(ruby_mutex);
|
||||
mrb_value str = mrb_str_new(mrb, msg.c_str(), msg.size());
|
||||
mrb_value mod_val = mrb_obj_value(C_module);
|
||||
mrb_funcall(mrb, mod_val, "queue_log", 1, str);
|
||||
}
|
||||
|
||||
void load_custom_highlighters() {
|
||||
std::lock_guard<std::mutex> lock(ruby_mutex);
|
||||
if (!C_module)
|
||||
@@ -320,8 +313,6 @@ std::vector<R_Language> read_languages() {
|
||||
mrb, val_hash, mrb_symbol_value(mrb_intern_lit(mrb, "extensions")));
|
||||
mrb_value filenames = mrb_hash_get(
|
||||
mrb, val_hash, mrb_symbol_value(mrb_intern_lit(mrb, "filenames")));
|
||||
mrb_value mimetypes = mrb_hash_get(
|
||||
mrb, val_hash, mrb_symbol_value(mrb_intern_lit(mrb, "mimetypes")));
|
||||
mrb_value lsp = mrb_hash_get(mrb, val_hash,
|
||||
mrb_symbol_value(mrb_intern_lit(mrb, "lsp")));
|
||||
if (!mrb_nil_p(fg))
|
||||
@@ -329,8 +320,8 @@ std::vector<R_Language> read_languages() {
|
||||
if (!mrb_nil_p(symbol))
|
||||
lang.symbol = std::string(RSTRING_PTR(symbol), RSTRING_LEN(symbol));
|
||||
lang.extensions = array_to_vector(extensions);
|
||||
lang.filenames = array_to_vector(filenames);
|
||||
lang.mimetypes = array_to_vector(mimetypes);
|
||||
if (!mrb_nil_p(filenames))
|
||||
lang.filenames = array_to_vector(filenames);
|
||||
if (!mrb_nil_p(lsp))
|
||||
lang.lsp_command = std::string(RSTRING_PTR(lsp), RSTRING_LEN(lsp));
|
||||
result.push_back(lang);
|
||||
@@ -355,8 +346,6 @@ void load_languages_info() {
|
||||
// TODO: seperate extensions and filenames
|
||||
for (auto &filename : lang.filenames)
|
||||
language_extensions[filename] = lang.name;
|
||||
for (auto &mimetype : lang.mimetypes)
|
||||
language_mimetypes[mimetype] = lang.name;
|
||||
}
|
||||
for (auto &lsp : lsps_t)
|
||||
lsps[lsp.command] = lsp;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
std::unordered_map<std::string, Language> languages;
|
||||
std::unordered_map<std::string, std::string> language_extensions;
|
||||
std::unordered_map<std::string, std::string> language_mimetypes;
|
||||
std::unordered_map<std::string, LSP> lsps;
|
||||
|
||||
void log(const char *fmt, ...) {
|
||||
@@ -106,24 +105,6 @@ static std::string file_extension(const char *filename) {
|
||||
return ext;
|
||||
}
|
||||
|
||||
char *detect_file_type(const char *filename) {
|
||||
magic_t magic = magic_open(MAGIC_MIME_TYPE);
|
||||
if (!magic)
|
||||
return nullptr;
|
||||
if (magic_load(magic, nullptr) != 0) {
|
||||
magic_close(magic);
|
||||
return nullptr;
|
||||
}
|
||||
const char *type = magic_file(magic, filename);
|
||||
if (!type) {
|
||||
magic_close(magic);
|
||||
return nullptr;
|
||||
}
|
||||
char *result = strdup(type);
|
||||
magic_close(magic);
|
||||
return result;
|
||||
}
|
||||
|
||||
Language language_for_file(const char *filename) {
|
||||
std::string ext = file_extension(filename);
|
||||
std::string lang_name;
|
||||
@@ -132,14 +113,6 @@ Language language_for_file(const char *filename) {
|
||||
if (it != language_extensions.end())
|
||||
return languages.find(it->second)->second;
|
||||
}
|
||||
char *mime = detect_file_type(filename);
|
||||
if (mime) {
|
||||
std::string mime_type(mime);
|
||||
free(mime);
|
||||
auto it = language_mimetypes.find(mime_type);
|
||||
if (it != language_mimetypes.end())
|
||||
return languages.find(it->second)->second;
|
||||
}
|
||||
return Language{};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user