Add tree-sitter grammars for common languages I use.

This commit is contained in:
2025-12-25 22:01:58 +00:00
parent f3c87431a3
commit b6e40ae1a6
26 changed files with 259 additions and 54 deletions

View File

@@ -6,12 +6,13 @@ extern "C" {
#include "../include/main.h"
#include "../include/utils.h"
Editor *new_editor(const char *filename, Coord position, Coord size) {
Editor *new_editor(const char *filename_arg, Coord position, Coord size) {
Editor *editor = new Editor();
if (!editor)
return nullptr;
uint32_t len = 0;
char *str = load_file(filename, &len);
std::string filename = path_abs(filename_arg);
char *str = load_file(filename.c_str(), &len);
if (!str) {
free_editor(editor);
return nullptr;
@@ -23,7 +24,7 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
editor->cursor_preffered = UINT32_MAX;
editor->root = load(str, len, optimal_chunk_size(len));
free(str);
Language language = language_for_file(filename);
Language language = language_for_file(filename.c_str());
if (language.name != "unknown" && len <= (1024 * 128)) {
editor->ts.parser = ts_parser_new();
editor->ts.language = language.fn();
@@ -57,6 +58,17 @@ void free_editor(Editor *editor) {
delete editor;
}
void save_file(Editor *editor) {
if (!editor || !editor->root)
return;
char *str = read(editor->root, 0, editor->root->char_count);
if (!str)
return;
std::ofstream out(editor->filename);
out.write(str, editor->root->char_count);
free(str);
}
void render_editor(Editor *editor) {
uint32_t sel_start = 0, sel_end = 0;
uint32_t numlen =

View File

@@ -2,6 +2,7 @@
#include "../include/main.h"
#include "../include/ts.h"
#include <cstdint>
#include <sys/ioctl.h>
void handle_editor_event(Editor *editor, KeyEvent event) {
static std::chrono::steady_clock::time_point last_click_time =
@@ -294,6 +295,9 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
case ',':
dedent_line(editor, editor->cursor.row);
break;
case CTRL('s'):
save_file(editor);
break;
case 'p':
uint32_t len;
char *text = get_from_clipboard(&len);

View File

@@ -21,11 +21,14 @@ static std::string percent_encode(const std::string &s) {
return out;
}
std::string path_to_file_uri(const std::string &path_str) {
std::string path_abs(const std::string &path_str) {
namespace fs = std::filesystem;
fs::path p = fs::weakly_canonical(fs::absolute(fs::path(path_str)));
std::string generic = p.generic_string();
return "file://" + percent_encode(generic);
return p.generic_string();
}
std::string path_to_file_uri(const std::string &path_str) {
return "file://" + percent_encode(path_abs(path_str));
}
uint64_t fnv1a_64(const char *s, size_t len) {