Major code cleanup.

This commit is contained in:
2025-12-10 22:51:19 +00:00
parent 6c57521833
commit 24c5b70af1
13 changed files with 103 additions and 106 deletions

View File

@@ -2,7 +2,7 @@
#define EDITOR_H
#include "../libs/tree-sitter/lib/include/tree_sitter/api.h"
#include "./rope.h"
#include "./knot.h"
#include "./ui.h"
#include "./utils.h"
#include <algorithm>
@@ -82,26 +82,25 @@ struct SpanCursor {
};
struct Editor {
const char *filename; // Filename of the editor
Knot *root; // A rope
std::shared_mutex knot_mtx; // A mutex
Coord cursor; // position of the cursor
uint32_t cursor_preffered; // preffered visual column
Coord selection; // position of the selection
bool selection_active; // true if there is a selection
Coord position; // Position of the editor
Coord size; // Size of the editor
Coord scroll; // Position of the scroll
TSTree *tree; // Tree-sitter tree
TSParser *parser; // Tree-sitter parser
TSQuery *query; // Tree-sitter query
const TSLanguage *language; // Tree-sitter language
Queue<TSInputEdit> edit_queue; // Tree-sitter edit queue
std::vector<Highlight> query_map; // Tree-sitter query map
std::vector<int8_t> folded; // folded lines indexed by line number
Spans spans; // Highlighted spans
std::map<uint32_t, bool> folded_node; // maps content hash to fold state
// - built by tree-sitter helpers
const char *filename;
Knot *root;
std::shared_mutex knot_mtx;
Coord cursor;
uint32_t cursor_preffered;
Coord selection;
bool selection_active;
Coord position;
Coord size;
Coord scroll;
TSTree *tree;
TSParser *parser;
TSQuery *query;
const TSLanguage *language;
Queue<TSInputEdit> edit_queue;
std::vector<Highlight> query_map;
std::vector<int8_t> folded;
Spans spans;
std::map<uint32_t, bool> folded_node;
};
Editor *new_editor(const char *filename, Coord position, Coord size);

View File

@@ -1,6 +1,7 @@
#ifndef ROPE_H
#define ROPE_H
#include "./utils.h"
#include <cstdint>
#include <vector>
@@ -10,9 +11,6 @@
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define DEPTH(n) ((n) ? (n)->depth : 0)
#define PCRE2_CODE_UNIT_WIDTH 8
#define PCRE_WORKSPACE_SIZE 512
// Rope node definition
typedef struct Knot {
Knot *left;

View File

@@ -2,6 +2,7 @@
#define TS_H
#include "./editor.h"
#include "./utils.h"
#include <pcre2.h>
#define HEX(s) (static_cast<uint32_t>(std::stoul(s, nullptr, 16)))

View File

@@ -7,18 +7,18 @@ struct Language {
};
extern "C" {
const TSLanguage *tree_sitter_bash(void);
const TSLanguage *tree_sitter_c(void);
const TSLanguage *tree_sitter_cpp(void);
const TSLanguage *tree_sitter_css(void);
const TSLanguage *tree_sitter_fish(void);
const TSLanguage *tree_sitter_go(void);
const TSLanguage *tree_sitter_haskell(void);
const TSLanguage *tree_sitter_html(void);
const TSLanguage *tree_sitter_javascript(void);
const TSLanguage *tree_sitter_json(void);
const TSLanguage *tree_sitter_lua(void);
const TSLanguage *tree_sitter_make(void);
const TSLanguage *tree_sitter_python(void);
const TSLanguage *tree_sitter_ruby(void);
const TSLanguage *tree_sitter_bash();
const TSLanguage *tree_sitter_c();
const TSLanguage *tree_sitter_cpp();
const TSLanguage *tree_sitter_css();
const TSLanguage *tree_sitter_fish();
const TSLanguage *tree_sitter_go();
const TSLanguage *tree_sitter_haskell();
const TSLanguage *tree_sitter_html();
const TSLanguage *tree_sitter_javascript();
const TSLanguage *tree_sitter_json();
const TSLanguage *tree_sitter_lua();
const TSLanguage *tree_sitter_make();
const TSLanguage *tree_sitter_python();
const TSLanguage *tree_sitter_ruby();
}

View File

@@ -1,6 +1,7 @@
#ifndef UI_H
#define UI_H
#include "./utils.h"
#include <atomic>
#include <cstdint>
#include <mutex>
@@ -63,11 +64,6 @@ struct ScreenCell {
uint8_t flags = CF_NONE;
};
struct Coord {
uint32_t row;
uint32_t col;
};
struct KeyEvent {
uint8_t key_type;
@@ -91,7 +87,6 @@ extern std::mutex screen_mutex;
extern std::atomic<bool> running;
void get_terminal_size();
void die(const char *s);
void enable_raw_mode();
void disable_raw_mode();
Coord start_screen();

View File

@@ -6,6 +6,9 @@
#include <queue>
#include <string>
#define PCRE2_CODE_UNIT_WIDTH 8
#define PCRE_WORKSPACE_SIZE 512
template <typename T> struct Queue {
std::queue<T> q;
std::mutex m;
@@ -28,6 +31,11 @@ template <typename T> struct Queue {
}
};
struct Coord {
uint32_t row;
uint32_t col;
};
uint32_t grapheme_strlen(const char *s);
uint32_t get_visual_col_from_bytes(const char *line, uint32_t byte_limit);
uint32_t get_bytes_from_visual_col(const char *line,