4 Commits

Author SHA1 Message Date
c683754d49 Cleanup / optimizations 2026-02-12 01:01:52 +00:00
e9d164d769 Rewrite diagnostics popup with new api 2026-02-11 19:51:45 +00:00
5b66f503e4 Improve highlighting structure
- switched to a sparse delta based map
- true lazy-loading to avoid any unneccessary allocations
- fixed windows management api
2026-02-11 18:18:28 +00:00
d79ea4e75a Fix parser bug on newlines 2026-02-05 14:23:52 +00:00
34 changed files with 831 additions and 835 deletions

View File

@@ -134,6 +134,7 @@ crib ./filename.ext
``` ```
*If `filename.ext` does not exist, it will be created*<br> *If `filename.ext` does not exist, it will be created*<br>
*memory usage is average case 10MB + 2x size of file*<br>
## Keybindings ## Keybindings

10
TODO.md
View File

@@ -2,19 +2,12 @@ Copyright 2025 Syed Daanish
# TODO # TODO
# memory usage for debug build (release build will be smaller by about 25%)
```
8K -> 13.2M
128K -> 13.2M (expected worst case 16.6M)
128M -> 412.0M (expected worst case 2.3G)
```
## Next few super long boring things to do:: ## Next few super long boring things to do::
* redo lsp threads such that no mutex needed for any rope stuff (done) * redo lsp threads such that no mutex needed for any rope stuff (done)
- Also make the classes own the methods in lsp (done) - Also make the classes own the methods in lsp (done)
- This will mean that parsers/renderers and keystrokes will not need to be individually locked (done) - This will mean that parsers/renderers and keystrokes will not need to be individually locked (done)
- And so it will be much faster (done) - And so it will be much faster (done)
- At which point the main thread can also be blocked on user input or lsp responses and still be fast - At which point the main thread can also be blocked on user input or lsp responses and still be faster
* Add a superclass for editor called Window (which can be popup or tiled) (done) * Add a superclass for editor called Window (which can be popup or tiled) (done)
* Add a recursive tiling class for windows (done) * Add a recursive tiling class for windows (done)
* Handled by a single renderer that calls and renders each window (done) * Handled by a single renderer that calls and renders each window (done)
@@ -44,6 +37,7 @@ Copyright 2025 Syed Daanish
* also move default bar and clipboard back into cpp * also move default bar and clipboard back into cpp
* all lsp popups are no longer their own classes but instead windows (extention like) in popup mode * all lsp popups are no longer their own classes but instead windows (extention like) in popup mode
* also **fix click handling to send mouse unclick to the same focus window as the click And also for drag to be same as the starter**
* skip opening binary files * skip opening binary files
* apply themeing in bg log bar lsp popus etc. to keep visual consistency * apply themeing in bg log bar lsp popus etc. to keep visual consistency
* searching/replace/Multi-Cursor (for just one lsp command for all) with pcre2 with regex (started by a slash) (disabled for large files) * searching/replace/Multi-Cursor (for just one lsp command for all) with pcre2 with regex (started by a slash) (disabled for large files)

View File

@@ -2,11 +2,12 @@
#define EDITOR_H #define EDITOR_H
#include "editor/indents.h" #include "editor/indents.h"
#include "extentions/diagnostics.h"
#include "extentions/hover.h"
#include "io/knot.h" #include "io/knot.h"
#include "io/sysio.h" #include "io/sysio.h"
#include "syntax/extras.h" #include "syntax/extras.h"
#include "syntax/parser.h" #include "syntax/parser.h"
#include "ui/diagnostics.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "windows/decl.h" #include "windows/decl.h"
@@ -37,8 +38,8 @@ struct Editor : Window {
VAI ai = {}; VAI ai = {};
std::shared_mutex lsp_mtx; std::shared_mutex lsp_mtx;
std::atomic<struct LSPInstance *> lsp = nullptr; std::atomic<struct LSPInstance *> lsp = nullptr;
bool hover_active = false; HoverBox *hover_popup = init_hover();
bool diagnostics_active = false; DiagnosticBox *diagnostic_popup = init_diagnostic();
std::atomic<int> lsp_version = 1; std::atomic<int> lsp_version = 1;
IndentationEngine indents = {}; IndentationEngine indents = {};
Parser *parser = nullptr; Parser *parser = nullptr;

View File

@@ -0,0 +1,22 @@
#ifndef EXTENTION_DIAGNOSTICS_H
#define EXTENTION_DIAGNOSTICS_H
#include "editor/decl.h"
#include "io/sysio.h"
#include "pch.h"
#include "utils/utils.h"
#include "windows/decl.h"
struct DiagnosticBox : Popup {
std::vector<VWarn> warnings;
DiagnosticBox() { this->hidden = true; }
void clear() { this->warnings.clear(); }
void render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) override;
void handle_click(KeyEvent, Coord) override { this->hidden = true; };
~DiagnosticBox() {};
};
DiagnosticBox *init_diagnostic();
#endif

View File

@@ -6,9 +6,7 @@
#include "utils/utils.h" #include "utils/utils.h"
#include "windows/decl.h" #include "windows/decl.h"
TileRoot *init_hover(); struct HoverBox : Popup {
struct HoverBox : Window {
std::string text; std::string text;
std::atomic<bool> is_markup; std::atomic<bool> is_markup;
uint32_t scroll_; uint32_t scroll_;
@@ -24,8 +22,16 @@ struct HoverBox : Window {
} }
void scroll(int32_t number); void scroll(int32_t number);
void render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) override; void render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) override;
void handle_click(KeyEvent, Coord) override { this->hidden = true; }; void handle_click(KeyEvent ev, Coord) override {
if (ev.mouse_button == SCROLL_BTN && ev.mouse_state == SCROLL) {
this->scroll(ev.mouse_direction == SCROLL_UP ? -1 : 1);
return;
}
this->hidden = true;
};
~HoverBox() {}; ~HoverBox() {};
}; };
HoverBox *init_hover();
#endif #endif

View File

@@ -96,14 +96,19 @@ inline bool is_empty_cell(const ScreenCell &c) {
return c.utf8.empty() || c.utf8 == " " || c.utf8 == "\x1b"; return c.utf8.empty() || c.utf8 == " " || c.utf8 == "\x1b";
} }
namespace io {
extern std::vector<ScreenCell> new_screen; extern std::vector<ScreenCell> new_screen;
extern uint32_t rows, cols;
extern bool show_cursor;
extern std::vector<ScreenCell> old_screen;
extern termios orig_termios;
} // namespace io
Coord start_screen(); Coord start_screen();
void end_screen(); void end_screen();
void set_cursor(uint8_t row, uint8_t col, uint32_t type, void set_cursor(uint8_t row, uint8_t col, uint32_t type,
bool show_cursor_param); bool show_cursor_param);
void io_render(); void io_render();
Coord get_size();
KeyEvent read_key(); KeyEvent read_key();

View File

@@ -2,19 +2,15 @@
#define LSP_H #define LSP_H
#include "editor/editor.h" #include "editor/editor.h"
#include "main.h"
#include "pch.h" #include "pch.h"
#include "utils/utils.h" #include "utils/utils.h"
#include <cstdint>
#include <sys/types.h>
#define LSP_TIMEOUT 3000 #define LSP_TIMEOUT_START 3000
#define LSP_TIMEOUT_QUIT_NORMAL 300
namespace lsp { #define LSP_TIMEOUT_QUIT_FORCE 80
extern std::mutex lsp_mutex;
extern std::unordered_map<std::string, std::unique_ptr<LSPInstance>>
active_lsps;
extern Queue<std::string> need_opening;
extern std::unordered_set<std::string> opened;
extern std::vector<Editor *> new_editors;
} // namespace lsp
void lsp_worker(); void lsp_worker();
@@ -48,6 +44,16 @@ struct LSPMessage {
std::function<void(const LSPMessage &)> callback; std::function<void(const LSPMessage &)> callback;
}; };
namespace lsp {
extern std::mutex lsp_mutex;
extern std::unordered_map<std::string, std::unique_ptr<LSPInstance>>
active_lsps;
extern Queue<std::string> need_opening;
extern std::unordered_set<std::string> opened;
extern std::vector<Editor *> new_editors;
extern Queue<std::unique_ptr<LSPMessage>> response_queue;
} // namespace lsp
struct LSPInstance { struct LSPInstance {
const LSP *lsp_info; const LSP *lsp_info;
std::string root_dir; std::string root_dir;
@@ -70,7 +76,6 @@ struct LSPInstance {
Queue<json> inbox; Queue<json> inbox;
Queue<json> outbox; Queue<json> outbox;
std::unordered_map<uint32_t, std::unique_ptr<LSPMessage>> pending; std::unordered_map<uint32_t, std::unique_ptr<LSPMessage>> pending;
std::vector<std::unique_ptr<LSPMessage>> lsp_response_queue;
std::vector<Editor *> editors; std::vector<Editor *> editors;
LSPInstance(std::string lsp_id) { LSPInstance(std::string lsp_id) {
@@ -89,7 +94,15 @@ struct LSPInstance {
{"capabilities", client_capabilities}}}}; {"capabilities", client_capabilities}}}};
send_raw(initialize_message); send_raw(initialize_message);
pollfd pfd{stdout_fd, POLLIN, 0}; pollfd pfd{stdout_fd, POLLIN, 0};
poll(&pfd, 1, LSP_TIMEOUT); uint32_t waited = 0;
while (waited < LSP_TIMEOUT_START) {
poll(&pfd, 1, 50);
if (pfd.revents & POLLIN)
break;
if (!running)
return;
waited += 50;
}
if (!(pfd.revents & POLLIN)) { if (!(pfd.revents & POLLIN)) {
exited = true; exited = true;
return; return;
@@ -116,9 +129,7 @@ struct LSPInstance {
} }
} }
allow_formatting = caps.value("documentFormattingProvider", false); allow_formatting = caps.value("documentFormattingProvider", false);
if (lsp_id != "lua-language-server" /* Lua ls gives terrible ontype if (caps.contains("documentOnTypeFormattingProvider")) {
formatting so disable */
&& caps.contains("documentOnTypeFormattingProvider")) {
auto &fmt = caps["documentOnTypeFormattingProvider"]; auto &fmt = caps["documentOnTypeFormattingProvider"];
if (fmt.is_object()) { if (fmt.is_object()) {
if (fmt.contains("firstTriggerCharacter")) { if (fmt.contains("firstTriggerCharacter")) {
@@ -181,34 +192,39 @@ struct LSPInstance {
send_raw(initialized_message); send_raw(initialized_message);
} }
~LSPInstance() { ~LSPInstance() {
uint32_t timeout =
running ? LSP_TIMEOUT_QUIT_NORMAL : LSP_TIMEOUT_QUIT_FORCE;
for (auto &ed : editors) for (auto &ed : editors)
ed->lsp.store(nullptr); ed->lsp.store(nullptr);
initialized = false; initialized = false;
exited = true; exited = true;
if (pid == -1) if (pid == -1)
return; return;
json shutdown = {{"id", ++last_id}, {"method", "shutdown"}}; json shutdown = {
{"jsonrpc", "2.0"}, {"id", ++last_id}, {"method", "shutdown"}};
send_raw(shutdown); send_raw(shutdown);
pollfd pfd{stdout_fd, POLLIN, 0}; pollfd pfd{stdout_fd, POLLIN, 0};
poll(&pfd, 1, 500); poll(&pfd, 1, timeout);
json exit_msg = {{"method", "exit"}}; if (pfd.revents & POLLIN) {
json exit_msg = {{"jsonrpc", "2.0"}, {"method", "exit"}};
send_raw(exit_msg); send_raw(exit_msg);
int waited = 0; int waited = 0;
while (waited < 100) { while (waited < timeout) {
int status; int status;
pid_t res = waitpid(pid, &status, WNOHANG); pid_t res = waitpid(pid, &status, WNOHANG);
if (res == pid) if (res == pid)
break; break;
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(10ms);
waited += 10; waited += 10;
} }
}
close(stdin_fd);
close(stdout_fd);
if (kill(pid, 0) == 0) { if (kill(pid, 0) == 0) {
kill(pid, SIGKILL); kill(pid, SIGKILL);
waitpid(pid, nullptr, 0); waitpid(pid, nullptr, 0);
} }
pid = -1; pid = -1;
close(stdin_fd);
close(stdout_fd);
} }
bool init_process() { bool init_process() {
int in_pipe[2]; int in_pipe[2];
@@ -314,7 +330,7 @@ struct LSPInstance {
if (it != pending.end()) { if (it != pending.end()) {
if (it->second->editor) { if (it->second->editor) {
it->second->message = *msg; it->second->message = *msg;
lsp_response_queue.push_back(std::move(it->second)); lsp::response_queue.push(std::move(it->second));
} else { } else {
auto message = *std::move(it->second); auto message = *std::move(it->second);
message.message = *msg; message.message = *msg;
@@ -337,7 +353,7 @@ struct LSPInstance {
response->message = *msg; response->message = *msg;
response->callback = editor_handle_wrapper; response->callback = editor_handle_wrapper;
if (ed) if (ed)
lsp_response_queue.push_back(std::move(response)); lsp::response_queue.push(std::move(response));
else else
lsp_handle(*msg); lsp_handle(*msg);
} }
@@ -346,11 +362,6 @@ struct LSPInstance {
inline static void editor_handle_wrapper(const LSPMessage &message) { inline static void editor_handle_wrapper(const LSPMessage &message) {
message.editor->lsp_handle(message.message); message.editor->lsp_handle(message.message);
} }
void callbacks() {
for (auto &message : lsp_response_queue)
message->callback(*message);
lsp_response_queue.clear();
}
inline void send_raw(const json &msg) { inline void send_raw(const json &msg) {
std::string payload = msg.dump(); std::string payload = msg.dump();
std::string header = std::string header =

View File

@@ -16,7 +16,6 @@ extern fs::path pwd;
namespace ui { namespace ui {
extern Bar bar; extern Bar bar;
extern TileRoot *hover_popup;
} // namespace ui } // namespace ui
#endif #endif

View File

@@ -6,6 +6,8 @@
#include "pch.h" #include "pch.h"
#include "syntax/trie.h" #include "syntax/trie.h"
#define MAX_LINES_LOOKAROUND 512
struct Highlight { struct Highlight {
uint32_t fg{0xFFFFFF}; uint32_t fg{0xFFFFFF};
uint32_t bg{0x000000}; uint32_t bg{0x000000};
@@ -35,10 +37,23 @@ struct Token {
TokenKind type; TokenKind type;
}; };
struct StateBase {
virtual ~StateBase() = default;
virtual std::unique_ptr<StateBase> clone() const = 0;
};
struct CustomState : StateBase {
std::string value;
explicit CustomState(std::string v) : value(std::move(v)) {}
std::unique_ptr<StateBase> clone() const override {
return std::make_unique<CustomState>(value);
}
};
struct LineData { struct LineData {
std::shared_ptr<void> in_state{nullptr}; std::unique_ptr<StateBase> out_state;
std::vector<Token> tokens; std::vector<Token> tokens;
std::shared_ptr<void> out_state{nullptr}; std::unique_ptr<StateBase> in_state;
}; };
#endif #endif

View File

@@ -4,29 +4,19 @@
#include "syntax/decl.h" #include "syntax/decl.h"
#define DEF_LANG(name) \ #define DEF_LANG(name) \
std::shared_ptr<void> name##_parse( \ std::unique_ptr<StateBase> name##_parse( \
std::vector<Token> *tokens, std::shared_ptr<void> in_state, \ std::vector<Token> *tokens, StateBase *in_state, const char *text, \
const char *text, uint32_t len, uint32_t line_num); \ uint32_t len, uint32_t line_num); \
bool name##_state_match(std::shared_ptr<void> state_1, \ bool name##_state_match(StateBase *state_1, StateBase *state_2);
std::shared_ptr<void> state_2);
#define LANG_A(name) \ #define LANG_A(name) \
{ \ { \
#name, { name##_parse, name##_state_match } \ #name, { name##_parse, name##_state_match } \
} }
template <typename T> template <typename B, typename A>
inline std::shared_ptr<T> ensure_state(std::shared_ptr<T> state) { std::unique_ptr<B> static_unique_ptr_cast(std::unique_ptr<A> &&p) {
using U = typename T::full_state_type; return std::unique_ptr<B>(static_cast<B *>(p.release()));
if (!state)
state = std::make_shared<T>();
if (!state.unique())
state = std::make_shared<T>(*state);
if (!state->full_state)
state->full_state = std::make_shared<U>();
else if (!state->full_state.unique())
state->full_state = std::make_shared<U>(*state->full_state);
return state;
} }
DEF_LANG(ruby); DEF_LANG(ruby);
@@ -34,11 +24,10 @@ DEF_LANG(bash);
inline static const std::unordered_map< inline static const std::unordered_map<
std::string, std::string,
std::tuple<std::shared_ptr<void> (*)( std::tuple<std::unique_ptr<StateBase> (*)(
std::vector<Token> *tokens, std::shared_ptr<void> in_state, std::vector<Token> *tokens, StateBase *in_state,
const char *text, uint32_t len, uint32_t line_num), const char *text, uint32_t len, uint32_t line_num),
bool (*)(std::shared_ptr<void> state_1, bool (*)(StateBase *state_1, StateBase *state_2)>>
std::shared_ptr<void> state_2)>>
parsers = { parsers = {
LANG_A(ruby), LANG_A(ruby),
LANG_A(bash), LANG_A(bash),

136
include/syntax/line_map.h Normal file
View File

@@ -0,0 +1,136 @@
#ifndef LINE_MAP_H
#define LINE_MAP_H
#include "syntax/decl.h"
struct LineKey {
uint32_t line;
uint32_t version;
bool operator==(const LineKey &other) const {
return line == other.line && version == other.version;
}
};
struct LineKeyHash {
size_t operator()(const LineKey &key) const {
uint64_t combined = (uint64_t(key.line) << 32) | key.version;
return std::hash<uint64_t>()(combined);
}
};
struct EditDelta {
uint32_t start_line;
int64_t delta;
};
struct LineMap {
LineMap() : current_version(0) { edit_log.push_back({0, 0}); }
LineData *at(uint32_t line) {
auto key_opt = resolve_line(line);
if (!key_opt)
return nullptr;
LineKey key = *key_opt;
if (key.version == current_version)
return lines[key].get();
auto data_ptr = std::move(lines[key]);
lines.erase(key);
key = {line, current_version};
lines[key] = std::move(data_ptr);
return lines[key].get();
}
LineData *create_at(uint32_t line) {
auto key_opt = resolve_line(line);
LineKey key;
std::unique_ptr<LineData> data_ptr;
if (key_opt) {
key = *key_opt;
if (key.version == current_version)
return lines[key].get();
data_ptr = std::move(lines[key]);
lines.erase(key);
} else {
data_ptr = std::make_unique<LineData>();
}
key = {line, current_version};
lines[key] = std::move(data_ptr);
return lines[key].get();
}
void apply_edit(uint32_t start, int64_t delta) {
if (delta < 0)
batch_remove(start, -delta);
current_version++;
edit_log.push_back({start, delta});
}
private:
std::unordered_map<LineKey, std::unique_ptr<LineData>, LineKeyHash> lines;
std::vector<EditDelta> edit_log;
uint32_t current_version;
void batch_remove(uint32_t line, uint32_t count) {
std::vector<uint32_t> lines_t;
lines_t.reserve(count);
for (uint32_t i = 0; i < count; i++)
lines_t.push_back(line + i);
for (int64_t v = current_version; v >= 0; v--) {
for (auto &l : lines_t) {
if (l == UINT32_MAX)
continue;
LineKey key = {l, (uint32_t)v};
if (lines.find(key) != lines.end()) {
lines.erase(key);
l = UINT32_MAX;
}
}
bool all_removed = true;
const auto &edit = edit_log[v];
for (auto &l : lines_t) {
if (l == UINT32_MAX)
continue;
all_removed = false;
if (edit.delta > 0) {
if (l >= edit.start_line) {
if (l < edit.start_line + edit.delta)
l = UINT32_MAX;
else
l -= edit.delta;
}
} else {
if (l >= edit.start_line)
l -= edit.delta;
}
}
if (all_removed)
break;
}
return;
}
std::optional<LineKey> resolve_line(uint32_t line) {
uint32_t current_line = line;
for (int64_t v = current_version; v >= 0; v--) {
LineKey key = {current_line, (uint32_t)v};
if (lines.find(key) != lines.end())
return key;
const auto &edit = edit_log[v];
if (edit.delta > 0) {
if (current_line >= edit.start_line) {
if (current_line < edit.start_line + edit.delta)
return std::nullopt;
current_line -= edit.delta;
}
} else {
if (current_line >= edit.start_line)
current_line -= edit.delta;
}
}
return std::nullopt;
}
};
#endif

View File

@@ -1,232 +0,0 @@
#ifndef LINE_TREE_H
#define LINE_TREE_H
#include "syntax/decl.h"
struct LineTree {
void clear() {
clear_node(root);
root = nullptr;
stack_size = 0;
}
void build(uint32_t x) { root = build_node(x); }
LineData *at(uint32_t x) {
LineNode *n = root;
while (n) {
uint32_t left_size = n->left ? n->left->size : 0;
if (x < left_size) {
n = n->left;
} else if (x < left_size + n->data.size()) {
return &n->data[x - left_size];
} else {
x -= left_size + n->data.size();
n = n->right;
}
}
return nullptr;
}
LineData *start_iter(uint32_t x) {
stack_size = 0;
LineNode *n = root;
while (n) {
uint32_t left_size = n->left ? n->left->size : 0;
if (x < left_size) {
push(n, 0);
n = n->left;
} else if (x < left_size + n->data.size()) {
push(n, x - left_size + 1);
return &n->data[x - left_size];
} else {
x -= left_size + n->data.size();
push(n, UINT32_MAX);
n = n->right;
}
}
return nullptr;
}
void end_iter() { stack_size = 0; }
LineData *next() {
while (stack_size) {
auto &f = stack[stack_size - 1];
LineNode *n = f.node;
if (f.index < n->data.size())
return &n->data[f.index++];
stack_size--;
if (n->right) {
n = n->right;
while (n) {
push(n, 0);
if (!n->left)
break;
n = n->left;
}
return &stack[stack_size - 1].node->data[0];
}
}
return nullptr;
}
void insert(uint32_t x, uint32_t y) {
if (x > subtree_size(root))
x = subtree_size(root);
root = insert_node(root, x, y);
}
void erase(uint32_t x, uint32_t y) {
if (x + y > subtree_size(root))
x = subtree_size(root) - y;
root = erase_node(root, x, y);
}
uint32_t count() { return subtree_size(root); }
~LineTree() { clear(); }
private:
struct LineNode {
LineNode *left = nullptr;
LineNode *right = nullptr;
uint8_t depth = 1;
uint32_t size = 0;
std::vector<LineData> data;
};
struct Frame {
LineNode *node;
uint32_t index;
};
void push(LineNode *n, uint32_t x) {
stack[stack_size].node = n;
stack[stack_size].index = x;
stack_size++;
}
static void clear_node(LineNode *n) {
if (!n)
return;
clear_node(n->left);
clear_node(n->right);
delete n;
}
LineNode *root = nullptr;
Frame stack[32];
std::atomic<uint8_t> stack_size = 0;
static constexpr uint32_t LEAF_TARGET = 256;
LineTree::LineNode *erase_node(LineNode *n, uint32_t x, uint32_t y) {
if (!n || y == 0)
return n;
uint32_t left_sz = subtree_size(n->left);
uint32_t mid_sz = n->data.size();
if (x < left_sz) {
uint32_t len = std::min(y, left_sz - x);
n->left = erase_node(n->left, x, len);
y -= len;
x = left_sz;
}
if (y > 0 && x < left_sz + mid_sz) {
uint32_t mid_x = x - left_sz;
uint32_t len = std::min(y, mid_sz - mid_x);
n->data.erase(n->data.begin() + mid_x, n->data.begin() + mid_x + len);
y -= len;
x += len;
}
if (y > 0) {
n->right = erase_node(n->right, x - left_sz - n->data.size(), y);
}
if (n->left && n->right &&
subtree_size(n->left) + subtree_size(n->right) < 256) {
return merge(n->left, n->right);
}
return rebalance(n);
}
LineTree::LineNode *insert_node(LineNode *n, uint32_t x, uint32_t y) {
if (!n) {
auto *leaf = new LineNode();
leaf->data.resize(y);
leaf->size = y;
return leaf;
}
if (!n->left && !n->right) {
n->data.insert(n->data.begin() + x, y, LineData());
fix(n);
if (n->data.size() > 512)
return split_leaf(n);
return n;
}
uint32_t left_size = subtree_size(n->left);
if (x <= left_size)
n->left = insert_node(n->left, x, y);
else
n->right = insert_node(n->right, x - left_size - n->data.size(), y);
return rebalance(n);
}
LineNode *build_node(uint32_t count) {
if (count <= LEAF_TARGET) {
auto *n = new LineNode();
n->data.resize(count);
n->size = count;
return n;
}
uint32_t left_count = count / 2;
uint32_t right_count = count - left_count;
auto *n = new LineNode();
n->left = build_node(left_count);
n->right = build_node(right_count);
fix(n);
return n;
}
static LineNode *split_leaf(LineNode *n) {
auto *right = new LineNode();
size_t mid = n->data.size() / 2;
right->data.assign(n->data.begin() + mid, n->data.end());
n->data.resize(mid);
fix(n);
fix(right);
auto *parent = new LineNode();
parent->left = n;
parent->right = right;
fix(parent);
return parent;
}
static LineNode *merge(LineNode *a, LineNode *b) {
a->data.insert(a->data.end(), b->data.begin(), b->data.end());
delete b;
fix(a);
return a;
}
static void fix(LineNode *n) {
n->depth = 1 + MAX(height(n->left), height(n->right));
n->size = subtree_size(n->left) + n->data.size() + subtree_size(n->right);
}
static LineNode *rotate_right(LineNode *y) {
LineNode *x = y->left;
LineNode *T2 = x->right;
x->right = y;
y->left = T2;
fix(y);
fix(x);
return x;
}
static LineNode *rotate_left(LineNode *x) {
LineNode *y = x->right;
LineNode *T2 = y->left;
y->left = x;
x->right = T2;
fix(x);
fix(y);
return y;
}
static LineNode *rebalance(LineNode *n) {
fix(n);
int balance = int(height(n->left)) - int(height(n->right));
if (balance > 1) {
if (height(n->left->left) < height(n->left->right))
n->left = rotate_left(n->left);
return rotate_right(n);
}
if (balance < -1) {
if (height(n->right->right) < height(n->right->left))
n->right = rotate_right(n->right);
return rotate_left(n);
}
return n;
}
static uint8_t height(LineNode *n) { return n ? n->depth : 0; }
static uint32_t subtree_size(LineNode *n) { return n ? n->size : 0; }
};
#endif

View File

@@ -3,24 +3,22 @@
#include "ruby/decl.h" #include "ruby/decl.h"
#include "syntax/decl.h" #include "syntax/decl.h"
#include "syntax/line_tree.h" #include "syntax/line_map.h"
struct Parser { struct Parser {
struct Editor *editor = nullptr; struct Editor *editor = nullptr;
std::string lang; std::string lang;
std::shared_ptr<void> (*parse_func)(std::vector<Token> *tokens, std::unique_ptr<StateBase> (*parse_func)(std::vector<Token> *tokens,
std::shared_ptr<void> in_state, StateBase *in_state,
const char *text, uint32_t len, const char *text, uint32_t len,
uint32_t line_num); uint32_t line_num);
bool (*state_match_func)(std::shared_ptr<void> state_1, bool (*state_match_func)(StateBase *state_1, StateBase *state_2);
std::shared_ptr<void> state_2);
mrb_value parser_block = mrb_nil_value(); mrb_value parser_block = mrb_nil_value();
mrb_value match_block = mrb_nil_value(); mrb_value match_block = mrb_nil_value();
bool is_custom{false}; bool is_custom{false};
std::atomic<uint32_t> scroll_max{0}; std::atomic<uint32_t> scroll_max{0};
std::atomic<bool> scroll_dirty{false}; std::atomic<bool> scroll_dirty{false};
LineTree line_tree; LineMap line_map = LineMap();
UniqueQueue<uint32_t> dirty_lines;
Parser(Editor *editor, std::string n_lang, uint32_t n_scroll_max); Parser(Editor *editor, std::string n_lang, uint32_t n_scroll_max);
void edit(uint32_t start_line, uint32_t removed_rows, uint32_t inserted_rows); void edit(uint32_t start_line, uint32_t removed_rows, uint32_t inserted_rows);

View File

@@ -1,19 +0,0 @@
#ifndef UI_DIAGNOSTICS_H
#define UI_DIAGNOSTICS_H
#include "editor/decl.h"
#include "io/sysio.h"
#include "pch.h"
#include "utils/utils.h"
struct DiagnosticBox {
std::vector<VWarn> warnings;
std::vector<ScreenCell> cells;
Coord size;
void clear();
void render_first();
void render(Coord pos);
};
#endif

View File

@@ -6,18 +6,19 @@
template <typename T> struct Queue { template <typename T> struct Queue {
void push(T val) { void push(T val) {
std::lock_guard<std::mutex> lock(m); std::lock_guard<std::mutex> lock(m);
q.push(val); q.push(std::move(val));
} }
std::optional<T> front() { std::optional<T> front() {
std::lock_guard<std::mutex> lock(m);
if (q.empty()) if (q.empty())
return std::nullopt; return std::nullopt;
return q.front(); return std::move(q.front());
} }
bool pop(T &val) { bool pop(T &val) {
std::lock_guard<std::mutex> lock(m); std::lock_guard<std::mutex> lock(m);
if (q.empty()) if (q.empty())
return false; return false;
val = q.front(); val = std::move(q.front());
q.pop(); q.pop();
return true; return true;
} }
@@ -92,6 +93,11 @@ struct Coord {
bool operator>=(const Coord &other) const { return !(*this < other); } bool operator>=(const Coord &other) const { return !(*this < other); }
}; };
static inline bool inside(Coord c, Coord pos, Coord size) {
return c.row >= pos.row && c.col >= pos.col && c.row - pos.row < size.row &&
c.col - pos.col < size.col;
}
struct Match { struct Match {
size_t start; size_t start;
size_t end; size_t end;

View File

@@ -22,6 +22,12 @@ struct Window : Tile {
virtual std::array<std::string, 5> bar_info() { return {}; }; virtual std::array<std::string, 5> bar_info() { return {}; };
}; };
struct Popup : Window {
Coord pos;
Coord size;
virtual ~Popup() = default;
};
struct TileBlock : Tile { struct TileBlock : Tile {
bool vertical; bool vertical;
std::vector<std::unique_ptr<Tile>> tiles; std::vector<std::unique_ptr<Tile>> tiles;
@@ -82,11 +88,12 @@ struct TileBlock : Tile {
} }
}; };
struct TileRoot { struct TileBase {
std::unique_ptr<Tile> tile; std::unique_ptr<Tile> tile;
Coord pos; Coord pos;
Coord size; Coord size;
inline bool hidden() { return this->tile->hidden; }
void render(std::vector<ScreenCell> &buffer) { void render(std::vector<ScreenCell> &buffer) {
if (this->tile->hidden) if (this->tile->hidden)
return; return;
@@ -97,22 +104,14 @@ struct TileRoot {
event.mouse_y -= this->pos.row; event.mouse_y -= this->pos.row;
this->tile->handle_click(event, size); this->tile->handle_click(event, size);
} }
bool inside(uint32_t x, uint32_t y) {
return x >= pos.col && x < pos.col + size.col && y >= pos.row &&
y < pos.row + size.row;
}
}; };
extern TileRoot root_tile; namespace layout {
extern std::vector<std::unique_ptr<TileRoot>> popups; extern TileBase root_tile;
extern Window *focused_window; extern Window *focused_window;
extern std::vector<std::unique_ptr<Popup>> popups;
inline void close_popup(TileRoot *handle) { extern std::vector<std::unique_ptr<TileBase>> floating_tiles;
auto it = std::find_if(popups.begin(), popups.end(), } // namespace layout
[handle](const auto &p) { return p.get() == handle; });
if (it != popups.end())
popups.erase(it);
}
void render(); void render();
void handle_click(KeyEvent event); void handle_click(KeyEvent event);

View File

@@ -339,6 +339,5 @@ puts 'Ruby syntax highlighting test complete.'
__END__ __END__
Anything here should be ignored >><< Anything here should be ignored >><<
{{{}}}[[[]]](((000))) {{{}}}[[[]]](((000)))

View File

@@ -1,13 +1,10 @@
#include "editor/editor.h" #include "editor/editor.h"
#include "main.h"
Coord Editor::click_coord(uint32_t x, uint32_t y) { Coord Editor::click_coord(uint32_t x, uint32_t y) {
if (mode == INSERT)
x++;
uint32_t numlen = uint32_t numlen =
EXTRA_META + static_cast<int>(std::log10(this->root->line_count + 1)); EXTRA_META + static_cast<int>(std::log10(this->root->line_count + 1));
uint32_t render_width = this->size.col - numlen; uint32_t render_width = this->size.col - numlen;
x = MAX(x, numlen) - numlen + 1; x = MAX(x, numlen) - numlen - 1;
uint32_t target_visual_row = y; uint32_t target_visual_row = y;
uint32_t visual_row = 0; uint32_t visual_row = 0;
uint32_t line_index = this->scroll.row; uint32_t line_index = this->scroll.row;

View File

@@ -232,8 +232,6 @@ void Editor::edit_replace(Coord start, Coord end, const char *text,
for (uint32_t i = 0; i < len; i++) for (uint32_t i = 0; i < len; i++)
if (text[i] == '\n') if (text[i] == '\n')
rows++; rows++;
if (rows > 0)
rows--;
if (this->parser) if (this->parser)
this->parser->edit(start.row, end.row - start.row, rows); this->parser->edit(start.row, end.row - start.row, rows);
auto lsp = this->lsp.load(); auto lsp = this->lsp.load();

View File

@@ -6,8 +6,8 @@
void Editor::handle_event(KeyEvent event) { void Editor::handle_event(KeyEvent event) {
uint8_t old_mode = mode; uint8_t old_mode = mode;
if (this->hover_active) if (!this->hover_popup->hidden)
this->hover_active = false; this->hover_popup->hidden = true;
if (event.key_type == KEY_SPECIAL) { if (event.key_type == KEY_SPECIAL) {
switch (event.special_modifier) { switch (event.special_modifier) {
case 0: case 0:
@@ -67,12 +67,12 @@ void Editor::handle_event(KeyEvent event) {
this->select_all(); this->select_all();
break; break;
case CTRL('h'): case CTRL('h'):
static_cast<HoverBox *>(ui::hover_popup->tile.get())->scroll(-1); this->hover_popup->scroll(-1);
this->hover_active = true; this->hover_popup->hidden = false;
break; break;
case CTRL('l'): case CTRL('l'):
static_cast<HoverBox *>(ui::hover_popup->tile.get())->scroll(1); this->hover_popup->scroll(1);
this->hover_active = true; this->hover_popup->hidden = false;
break; break;
case 'h': case 'h':
this->fetch_lsp_hover(); this->fetch_lsp_hover();

View File

@@ -410,11 +410,10 @@ void Editor::fetch_lsp_hover() {
hover_text += contents.get<std::string>(); hover_text += contents.get<std::string>();
} }
if (!hover_text.empty()) { if (!hover_text.empty()) {
auto hover_box = static_cast<HoverBox *>(ui::hover_popup->tile.get()); message.editor->hover_popup->clear();
hover_box->clear(); message.editor->hover_popup->text = clean_text(hover_text);
hover_box->text = clean_text(hover_text); message.editor->hover_popup->is_markup = is_markup;
hover_box->is_markup = is_markup; message.editor->hover_popup->hidden = false;
message.editor->hover_active = true;
} }
} }
}; };
@@ -423,12 +422,14 @@ void Editor::fetch_lsp_hover() {
} }
void Editor::handle_click(KeyEvent event, Coord size) { void Editor::handle_click(KeyEvent event, Coord size) {
focused_window = this; layout::focused_window = this;
this->size = size; this->size = size;
static std::chrono::steady_clock::time_point last_click_time = static std::chrono::steady_clock::time_point last_click_time =
std::chrono::steady_clock::now(); std::chrono::steady_clock::now();
static uint32_t click_count = 0; static uint32_t click_count = 0;
static Coord last_click_pos = {UINT32_MAX, UINT32_MAX}; static Coord last_click_pos = {UINT32_MAX, UINT32_MAX};
if (!this->hover_popup->hidden)
this->hover_popup->hidden = true;
if (event.key_type == KEY_MOUSE) { if (event.key_type == KEY_MOUSE) {
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>( auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(

View File

@@ -31,7 +31,7 @@ void Editor::render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) {
return (int)token.type; return (int)token.type;
return 0; return 0;
}; };
Coord screen = get_size(); Coord screen = {io::rows, io::cols};
auto update = [&](uint32_t row, uint32_t col, std::string text, uint32_t fg, auto update = [&](uint32_t row, uint32_t col, std::string text, uint32_t fg,
uint32_t bg, uint8_t flags, uint32_t u_color, uint32_t bg, uint8_t flags, uint32_t u_color,
uint32_t width) { uint32_t width) {
@@ -115,12 +115,8 @@ void Editor::render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) {
while (rendered_rows < this->size.row) { while (rendered_rows < this->size.row) {
uint32_t line_len; uint32_t line_len;
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
if (this->parser) { if (this->parser)
if (line_data) line_data = this->parser->line_map.at(line_index);
line_data = this->parser->line_tree.next();
else
line_data = this->parser->line_tree.start_iter(line_index);
}
if (!line) if (!line)
break; break;
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
@@ -471,14 +467,10 @@ void Editor::render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) {
break; break;
} }
set_cursor(cursor.row, cursor.col, type, true); set_cursor(cursor.row, cursor.col, type, true);
// if (this->completion.active && !this->completion.box.hidden) if (!this->hover_popup->hidden)
// this->completion.box.render(cursor); this->hover_popup->pos = cursor;
// else if (this->hover_active) if (!this->diagnostic_popup->hidden)
// this->hover.render(cursor); this->diagnostic_popup->pos = cursor;
// else if (this->diagnostics_active)
// this->diagnostics.render(cursor);
if (this->hover_active)
ui::hover_popup->pos = cursor;
} }
free(it->buffer); free(it->buffer);
free(it); free(it);

View File

@@ -1,36 +1,33 @@
#include "editor/editor.h" #include "editor/editor.h"
// void hover_diagnostic(Editor *editor) { void hover_diagnostic(Editor *editor) {
// static uint32_t last_line = UINT32_MAX; static uint32_t last_line = UINT32_MAX;
// if (last_line == editor->cursor.row && !editor->warnings_dirty) if (last_line == editor->cursor.row && !editor->warnings_dirty)
// return; return;
// VWarn dummy; VWarn dummy;
// dummy.line = editor->cursor.row; dummy.line = editor->cursor.row;
// editor->warnings_dirty = false; editor->warnings_dirty = false;
// last_line = editor->cursor.row; last_line = editor->cursor.row;
// auto first = auto first =
// std::lower_bound(editor->warnings.begin(), editor->warnings.end(), std::lower_bound(editor->warnings.begin(), editor->warnings.end(), dummy);
// dummy); auto last =
// auto last = std::upper_bound(editor->warnings.begin(), editor->warnings.end(), dummy);
// std::upper_bound(editor->warnings.begin(), editor->warnings.end(), std::vector<VWarn> warnings_at_line(first, last);
// dummy); if (warnings_at_line.size() == 0) {
// std::vector<VWarn> warnings_at_line(first, last); editor->diagnostic_popup->hidden = true;
// if (warnings_at_line.size() == 0) { return;
// editor->diagnostics_active = false; }
// return; editor->diagnostic_popup->clear();
// } editor->diagnostic_popup->warnings.swap(warnings_at_line);
// editor->diagnostics.clear(); editor->diagnostic_popup->hidden = false;
// editor->diagnostics.warnings.swap(warnings_at_line); }
// editor->diagnostics.render_first();
// editor->diagnostics_active = true;
// }
void Editor::work() { void Editor::work() {
if (!this->root) if (!this->root)
return; return;
if (this->parser) if (this->parser)
this->parser->work(); this->parser->work();
// hover_diagnostic(this); hover_diagnostic(this);
// if (this->completion.active && this->completion.hover_dirty) { // if (this->completion.active && this->completion.hover_dirty) {
// this->completion.hover.render_first(); // this->completion.hover.render_first();
// this->completion.hover_dirty = false; // this->completion.hover_dirty = false;

View File

@@ -0,0 +1,187 @@
#include "extentions/diagnostics.h"
#include <cstdint>
DiagnosticBox *init_diagnostic() {
auto diagnostic = std::make_unique<DiagnosticBox>();
diagnostic->pos = {0, 0};
diagnostic->size = {1, 1};
diagnostic->hidden = true;
DiagnosticBox *ptr = diagnostic.get();
layout::popups.push_back(std::move(diagnostic));
return ptr;
}
void DiagnosticBox::render(std::vector<ScreenCell> &buffer, Coord n_size,
Coord n_pos) {
pos = n_pos;
size = n_size;
int32_t start_row = (int32_t)pos.row - (int32_t)size.row;
if (start_row < 0)
start_row = pos.row + 1;
int32_t start_col = pos.col;
Coord screen_size = {io::rows, io::cols};
if (start_col + size.col > screen_size.col) {
start_col = screen_size.col - size.col;
if (start_col < 0)
start_col = 0;
}
pos.col = start_col;
pos.row = start_row;
if (warnings.empty())
return;
uint32_t longest_line = 8 + warnings[0].source.length();
for (auto &warn : warnings) {
uint32_t longest = 0;
uint32_t cur = 0;
for (char ch : warn.text_full)
if (ch == '\n') {
longest = MAX(longest, cur);
cur = 0;
} else {
if (ch == '\t')
cur += 3;
++cur;
}
longest = MAX(longest, cur);
longest_line = MAX(longest_line, longest + 7);
longest_line = MAX(longest_line, (uint32_t)warn.code.length() + 4);
for (auto &see_also : warn.see_also)
longest_line = MAX(longest_line, (uint32_t)see_also.length() + 4);
}
uint32_t content_width = MIN(longest_line, 150u);
size.col = content_width + 2;
auto set = [&](uint32_t r, uint32_t c, std::string text, uint32_t fg,
uint32_t bg, uint8_t flags, uint32_t width) {
if (r < 0 || r >= size.row || c < 0 || c >= size.col)
return;
r += pos.row;
c += pos.col;
if (r < 0 || r >= screen_size.row || c < 0 || c >= screen_size.col)
return;
ScreenCell &cell = buffer[r * screen_size.col + c];
cell.utf8 = std::move(text);
cell.width = width;
cell.fg = fg;
cell.bg = bg;
cell.flags = flags;
cell.ul_color = 0;
};
uint32_t base_bg = 0;
uint32_t border_fg = 0x82AAFF;
uint32_t r = 0;
if (warnings[0].source != "") {
std::string src_txt = "Source: ";
uint32_t i = 0;
for (; i < 8 && i < content_width; i++)
set(1, i + 1, (char[2]){src_txt[i], 0}, 0x3EAAFF, base_bg, 0, 1);
for (; i < 8 + warnings[0].source.length() && i < content_width; i++)
set(1, i + 1, (char[2]){warnings[0].source[i - 8], 0}, 0xffffff, base_bg,
0, 1);
while (i < content_width)
set(1, ++i, " ", 0xffffff, base_bg, 0, 1);
r++;
}
int idx = 1;
for (auto &warn : warnings) {
char buf[4];
std::snprintf(buf, sizeof(buf), "%2d", idx % 100);
std::string line_txt = std::string(buf) + ". ";
for (uint32_t i = 0; i < line_txt.length(); i++)
set(r + 1, i + 1, (char[2]){line_txt[i], 0}, 0xffffff, base_bg, 0, 1);
if (r >= 23)
break;
const char *err_sym = "";
uint32_t c_sym = 0xAAAAAA;
switch (warn.type) {
case 1:
err_sym = "";
c_sym = 0xFF0000;
break;
case 2:
err_sym = "";
c_sym = 0xFFFF00;
break;
case 3:
err_sym = "";
c_sym = 0xFF00FF;
break;
case 4:
err_sym = "";
c_sym = 0xAAAAAA;
break;
}
std::string text = warn.text_full + " " + err_sym;
uint32_t i = 0;
while (i < text.length() && r < 23) {
uint32_t c = 4;
while (c < content_width && i < text.length()) {
if (text[i] == '\n') {
while (i < text.length() && text[i] == '\n')
i++;
break;
}
uint32_t cluster_len = grapheme_next_character_break_utf8(
text.c_str() + i, text.length() - i);
std::string cluster = text.substr(i, cluster_len);
int width = display_width(cluster.c_str(), cluster_len);
if (c + width > content_width)
break;
set(r + 1, c + 1, cluster.c_str(), c_sym, base_bg, 0, width);
c += width;
i += cluster_len;
for (int w = 1; w < width; w++)
set(r + 1, c - w + 1, "\x1b", c_sym, base_bg, 0, 0);
}
while (c < content_width)
set(r + 1, ++c, " ", 0xffffff, base_bg, 0, 1);
r++;
}
if (r >= 23)
break;
if (warn.code != "") {
uint32_t i = 0;
for (; i < 5 && i < content_width; i++)
set(r + 1, i, " ", 0x81cdc6, base_bg, 0, 1);
for (; i < warn.code.length() + 5 && i < content_width; i++)
set(r + 1, i, (char[2]){warn.code[i - 5], 0}, 0x81cdc6, base_bg, 0, 1);
while (i <= content_width)
set(r + 1, i++, " ", 0x81cdc6, base_bg, 0, 1);
r++;
}
if (r >= 23)
break;
for (std::string &see_also : warn.see_also) {
uint32_t fg = 0xB55EFF;
uint8_t colon_count = 0;
uint32_t i = 0;
for (; i < 5 && i < content_width; i++)
set(r + 1, i, " ", 0x81cdc6, base_bg, 0, 1);
for (; i < see_also.length() + 5 && i < content_width; i++) {
set(r + 1, i, (char[2]){see_also[i - 5], 0}, fg, base_bg, 0, 1);
if (see_also[i] == ':')
colon_count++;
if (colon_count == 2)
fg = 0xFFFFFF;
}
while (i <= content_width)
set(r + 1, i++, " ", fg, base_bg, 0, 1);
r++;
if (r >= 23)
break;
};
idx++;
}
size.row = 2 + r;
set(0, 0, "", border_fg, base_bg, 0, 1);
for (uint32_t i = 1; i < size.col - 1; i++)
set(0, i, "", border_fg, base_bg, 0, 1);
set(0, size.col - 1, "", border_fg, base_bg, 0, 1);
for (uint32_t r = 1; r < size.row - 1; r++) {
set(r, 0, "", border_fg, base_bg, 0, 1);
set(r, size.col - 1, "", border_fg, base_bg, 0, 1);
}
set(size.row - 1, 0, "", border_fg, base_bg, 0, 1);
for (uint32_t i = 1; i < size.col - 1; i++)
set(size.row - 1, i, "", border_fg, base_bg, 0, 1);
set(size.row - 1, size.col - 1, "", border_fg, base_bg, 0, 1);
}

View File

@@ -1,15 +1,16 @@
#include "extentions/hover.h" #include "extentions/hover.h"
#include "io/sysio.h"
#include "syntax/decl.h" #include "syntax/decl.h"
#include "windows/decl.h" #include "windows/decl.h"
TileRoot *init_hover() { HoverBox *init_hover() {
auto root = std::make_unique<TileRoot>(); auto hover = std::make_unique<HoverBox>();
root->tile = std::make_unique<HoverBox>(); hover->pos = {0, 0};
root->pos = {0, 0}; hover->size = {1, 1};
root->size = {1, 1}; hover->hidden = true;
root->tile->hidden = true; HoverBox *ptr = hover.get();
popups.push_back(std::move(root)); layout::popups.push_back(std::move(hover));
return popups.back().get(); return ptr;
} }
void HoverBox::scroll(int32_t number) { void HoverBox::scroll(int32_t number) {
@@ -25,20 +26,25 @@ void HoverBox::scroll(int32_t number) {
scroll_dirty = true; scroll_dirty = true;
} }
void HoverBox::render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) { void HoverBox::render(std::vector<ScreenCell> &buffer, Coord n_size,
Coord n_pos) {
pos = n_pos;
size = n_size;
if (scroll_dirty) { if (scroll_dirty) {
// TODO: call syntax highlighter here // TODO: call syntax highlighter here
} }
// int32_t start_row = (int32_t)pos.row - (int32_t)size.row; int32_t start_row = (int32_t)pos.row - (int32_t)size.row;
// if (start_row < 0) if (start_row < 0)
// start_row = pos.row + 1; start_row = pos.row + 1;
// int32_t start_col = pos.col; // pos here is the cursor pos int32_t start_col = pos.col;
Coord screen_size = get_size(); Coord screen_size = {io::rows, io::cols};
// if (start_col + size.col > screen_size.col) { if (start_col + size.col > screen_size.col) {
// start_col = screen_size.col - size.col; start_col = screen_size.col - size.col;
// if (start_col < 0) if (start_col < 0)
// start_col = 0; start_col = 0;
// } }
pos.col = start_col;
pos.row = start_row;
uint32_t longest_line = 0; uint32_t longest_line = 0;
uint32_t current_width = 0; uint32_t current_width = 0;
for (size_t j = 0; j < text.length(); j++) { for (size_t j = 0; j < text.length(); j++) {
@@ -69,6 +75,8 @@ void HoverBox::render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) {
return; return;
r += pos.row; r += pos.row;
c += pos.col; c += pos.col;
if (r < 0 || r >= screen_size.row || c < 0 || c >= screen_size.col)
return;
ScreenCell &cell = buffer[r * screen_size.col + c]; ScreenCell &cell = buffer[r * screen_size.col + c];
cell.utf8 = text; cell.utf8 = text;
cell.width = width; cell.width = width;
@@ -103,6 +111,8 @@ void HoverBox::render(std::vector<ScreenCell> &buffer, Coord size, Coord pos) {
for (int w = 1; w < width; w++) for (int w = 1; w < width; w++)
set(r + 1, c - w + 1, "\x1b", 0xFFFFFF, base_bg, 0, 0); set(r + 1, c - w + 1, "\x1b", 0xFFFFFF, base_bg, 0, 0);
} }
while (c < content_width)
set(r + 1, ++c, " ", 0xFFFFFF, base_bg, 0, 0);
r++; r++;
} }
if (scroll_dirty) if (scroll_dirty)

View File

@@ -1,15 +1,19 @@
#include "io/sysio.h" #include "io/sysio.h"
namespace io {
std::vector<ScreenCell> new_screen; std::vector<ScreenCell> new_screen;
static uint32_t rows, cols; uint32_t rows, cols;
static bool show_cursor = 0; bool show_cursor = 0;
static std::vector<ScreenCell> old_screen; std::vector<ScreenCell> old_screen;
static termios orig_termios; termios orig_termios;
} // namespace io
using namespace io;
void disable_raw_mode() { void disable_raw_mode() {
std::string os = "\x1b[?1049l\x1b[2 q\x1b[?1002l\x1b[?25h\x1b[?2004l"; std::string os = "\x1b[?1049l\x1b[2 q\x1b[?1002l\x1b[?25h\x1b[?2004l";
write(STDOUT_FILENO, os.c_str(), os.size()); write(STDOUT_FILENO, os.c_str(), os.size());
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &io::orig_termios) == -1) {
perror("tcsetattr"); perror("tcsetattr");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@@ -45,8 +49,6 @@ Coord start_screen() {
void end_screen() { disable_raw_mode(); } void end_screen() { disable_raw_mode(); }
Coord get_size() { return {rows, cols}; }
void io_render() { void io_render() {
static bool first_render = true; static bool first_render = true;
uint32_t current_fg = 0; uint32_t current_fg = 0;

View File

@@ -6,6 +6,7 @@ std::unordered_map<std::string, std::unique_ptr<LSPInstance>> active_lsps;
std::unordered_set<std::string> opened; std::unordered_set<std::string> opened;
Queue<std::string> need_opening; Queue<std::string> need_opening;
std::vector<Editor *> new_editors; std::vector<Editor *> new_editors;
Queue<std::unique_ptr<LSPMessage>> response_queue;
} // namespace lsp } // namespace lsp
void lsp_worker() { void lsp_worker() {

View File

@@ -1,6 +1,5 @@
#include "main.h" #include "main.h"
#include "editor/editor.h" #include "editor/editor.h"
#include "extentions/hover.h"
#include "io/sysio.h" #include "io/sysio.h"
#include "lsp/lsp.h" #include "lsp/lsp.h"
#include "ruby/decl.h" #include "ruby/decl.h"
@@ -15,7 +14,6 @@ std::atomic<uint8_t> mode = NORMAL;
namespace ui { namespace ui {
Bar bar; Bar bar;
TileRoot *hover_popup = nullptr;
} // namespace ui } // namespace ui
void background_lsp() { void background_lsp() {
@@ -33,31 +31,34 @@ int main(int argc, char *argv[]) {
const char *filename = (argc > 1) ? argv[1] : ""; const char *filename = (argc > 1) ? argv[1] : "";
uint8_t eol = read_line_endings(); uint8_t eol = read_line_endings();
ui::hover_popup = init_hover(); layout::root_tile.pos = {0, 0};
layout::root_tile.size = {screen.row - 2, screen.col - 1};
auto editor = std::make_unique<Editor>(filename, eol);
layout::focused_window = editor.get();
auto block = std::make_unique<TileBlock>();
block->tiles.push_back(std::move(editor));
layout::root_tile.tile = std::move(block);
root_tile.size = {screen.row - 2, screen.col - 1};
root_tile.pos = {0, 0};
root_tile.tile = std::make_unique<Editor>(filename, eol);
focused_window = static_cast<Editor *>(root_tile.tile.get());
ui::bar.init(screen); ui::bar.init(screen);
std::thread lsp_thread(background_lsp); std::thread lsp_thread(background_lsp);
while (running) { while (running) {
KeyEvent event = throttle(1ms, read_key); uint8_t consumed = 0;
KeyEvent event;
while (++consumed < 32 && (event = read_key()).key_type != KEY_NONE) {
if (event.key_type != KEY_NONE) { if (event.key_type != KEY_NONE) {
if (event.key_type == KEY_CHAR && event.len == 1 && if (event.key_type == KEY_CHAR && event.len == 1 &&
event.c[0] == CTRL('q')) { event.c[0] == CTRL('q')) {
free(event.c); free(event.c);
running = false; running = false;
break; goto quit;
} }
if (mode != RUNNER) { if (mode != RUNNER) {
if (event.key_type == KEY_MOUSE) { if (event.key_type == KEY_MOUSE) {
handle_click(event); handle_click(event);
} else { } else {
focused_window->handle_event(event); layout::focused_window->handle_event(event);
} }
} else { } else {
ui::bar.handle_event(event); ui::bar.handle_event(event);
@@ -66,12 +67,17 @@ int main(int argc, char *argv[]) {
event.c) event.c)
free(event.c); free(event.c);
} }
for (auto &lsp_inst : lsp::active_lsps)
lsp_inst.second->callbacks();
focused_window->work();
throttle(4ms, render);
throttle(4ms, io_render);
} }
std::unique_ptr<LSPMessage> msg;
while (lsp::response_queue.pop(msg)) {
msg->callback(*msg);
};
layout::focused_window->work();
render();
throttle(8ms, io_render);
}
quit:
if (lsp_thread.joinable()) if (lsp_thread.joinable())
lsp_thread.join(); lsp_thread.join();

View File

@@ -10,7 +10,7 @@ struct BashFullState {
bool line_cont = false; bool line_cont = false;
struct Lit { struct Lit {
std::string delim = ""; // Only 1 wide for strings std::string delim = "";
bool allow_interp = false; bool allow_interp = false;
bool operator==(const BashFullState::Lit &other) const { bool operator==(const BashFullState::Lit &other) const {
@@ -24,7 +24,7 @@ struct BashFullState {
} }
}; };
struct BashState { struct BashState : StateBase {
using full_state_type = BashFullState; using full_state_type = BashFullState;
int interp_level = 0; int interp_level = 0;
@@ -37,26 +37,32 @@ struct BashState {
((full_state && other.full_state && ((full_state && other.full_state &&
*full_state == *other.full_state)); *full_state == *other.full_state));
} }
std::unique_ptr<StateBase> clone() const override {
return std::make_unique<BashState>(*this);
}
}; };
bool bash_state_match(std::shared_ptr<void> state_1, bool bash_state_match(StateBase *state_1, StateBase *state_2) {
std::shared_ptr<void> state_2) {
if (!state_1 || !state_2) if (!state_1 || !state_2)
return false; return false;
return *std::static_pointer_cast<BashState>(state_1) == return *static_cast<BashState *>(state_1) ==
*std::static_pointer_cast<BashState>(state_2); *static_cast<BashState *>(state_2);
} }
std::shared_ptr<void> bash_parse(std::vector<Token> *tokens, std::unique_ptr<StateBase> bash_parse(std::vector<Token> *tokens,
std::shared_ptr<void> in_state, StateBase *in_state, const char *text,
const char *text, uint32_t len, uint32_t len, uint32_t line_num) {
uint32_t line_num) {
static bool keywords_trie_init = false; static bool keywords_trie_init = false;
if (!keywords_trie_init) { if (!keywords_trie_init) {
keywords_trie_init = true; keywords_trie_init = true;
} }
tokens->clear(); tokens->clear();
auto state = ensure_state(std::static_pointer_cast<BashState>(in_state)); std::unique_ptr<BashState> state;
if (in_state)
state = static_unique_ptr_cast<BashState>(in_state->clone());
else
state = std::make_unique<BashState>();
uint32_t i = 0; uint32_t i = 0;
while (len > 0 && (text[len - 1] == '\n' || text[len - 1] == '\r' || while (len > 0 && (text[len - 1] == '\n' || text[len - 1] == '\r' ||
text[len - 1] == '\t' || text[len - 1] == ' ')) text[len - 1] == '\t' || text[len - 1] == ' '))

View File

@@ -26,54 +26,55 @@ Parser::Parser(Editor *n_editor, std::string n_lang, uint32_t n_scroll_max) {
assert("unknown lang should be checked by caller" && 0); assert("unknown lang should be checked by caller" && 0);
} }
} }
edit(0, 0, editor->root->line_count + 1); line_map.apply_edit(0, editor->root->line_count + 1);
} }
void Parser::edit(uint32_t start_line, uint32_t removed_rows, void Parser::edit(uint32_t start_line, uint32_t removed_rows,
uint32_t inserted_rows) { uint32_t inserted_rows) {
int64_t delta = (int64_t)inserted_rows - (int64_t)removed_rows; int64_t delta = (int64_t)inserted_rows - (int64_t)removed_rows;
if (delta < 0) line_map.apply_edit(start_line, delta);
line_tree.erase(start_line, (uint32_t)(-delta));
else if (delta > 0)
line_tree.insert(start_line, (uint32_t)delta);
uint32_t span = MAX(removed_rows, inserted_rows); uint32_t span = MAX(removed_rows, inserted_rows);
uint32_t begin = (start_line > 0) ? start_line - 1 : 0; uint32_t begin = (start_line > 0) ? start_line - 1 : 0;
uint32_t end = start_line + span; uint32_t end = start_line + span;
for (uint32_t line = begin; line <= end + 1; ++line) for (uint32_t line = begin; line <= end + 1; ++line)
dirty_lines.push(line); if (LineData *ld = line_map.at(line))
ld->out_state = nullptr;
} }
void Parser::work() { void Parser::work() {
if (!editor || !editor->root) if (!editor || !editor->root)
return; return;
std::vector<uint32_t> batch; std::vector<uint32_t> batch;
uint32_t c_line; uint32_t line_count = editor->root->line_count + 1;
while (dirty_lines.pop(c_line)) uint32_t min_line =
batch.push_back(c_line); scroll_max > MAX_LINES_LOOKAROUND ? scroll_max - MAX_LINES_LOOKAROUND : 0;
uint32_t i = MAX(0, (int64_t)scroll_max - 60); uint32_t max_line = MIN(scroll_max + MAX_LINES_LOOKAROUND, line_count - 1);
LineData *l_iter = line_tree.start_iter(i); bool sequential = false;
while (l_iter && i < scroll_max + 10) { for (uint32_t i = min_line; i <= max_line; ++i) {
if (!l_iter->out_state) LineData *ld = line_map.at(i);
if ((!ld || !ld->in_state || !ld->out_state) && !sequential) {
batch.push_back(i); batch.push_back(i);
i++; sequential = true;
l_iter = line_tree.next(); continue;
}
sequential = false;
} }
line_tree.end_iter();
for (uint32_t c_line : batch) { for (uint32_t c_line : batch) {
if (!running.load(std::memory_order_relaxed)) if (!running.load(std::memory_order_relaxed))
break; break;
uint32_t min_line = scroll_max > 60 ? scroll_max - 60 : 0; uint32_t min_line = scroll_max > MAX_LINES_LOOKAROUND
uint32_t max_line = scroll_max + 10; ? scroll_max - MAX_LINES_LOOKAROUND
if (c_line < min_line || c_line > max_line) { : 0;
dirty_lines.push(c_line); uint32_t max_line = scroll_max + MAX_LINES_LOOKAROUND;
if (c_line < min_line || c_line > max_line)
continue; continue;
}
uint32_t scroll_snapshot = scroll_max; uint32_t scroll_snapshot = scroll_max;
std::shared_ptr<void> prev_state = nullptr; std::unique_ptr<StateBase> prev_state = nullptr;
uint32_t line_count; if (c_line > 0 && c_line < line_count) {
line_count = line_tree.count(); auto lm = line_map.at(c_line - 1);
if (c_line > 0 && c_line < line_count) if (lm && lm->out_state)
prev_state = line_tree.at(c_line - 1)->out_state; prev_state = lm->out_state ? lm->out_state->clone() : nullptr;
}
LineIterator *it = begin_l_iter(editor->root, c_line); LineIterator *it = begin_l_iter(editor->root, c_line);
if (!it) if (!it)
continue; continue;
@@ -81,60 +82,61 @@ void Parser::work() {
while (cur_line < line_count) { while (cur_line < line_count) {
if (!running.load(std::memory_order_relaxed)) if (!running.load(std::memory_order_relaxed))
break; break;
if (scroll_snapshot != scroll_max) { if (scroll_snapshot != scroll_max)
LineData *line_data = line_tree.at(cur_line);
if (line_data && !line_data->out_state)
dirty_lines.push(cur_line);
break; break;
}
if (cur_line < min_line || cur_line > max_line) { if (cur_line < min_line || cur_line > max_line) {
dirty_lines.push(cur_line); LineData *line_data = line_map.at(cur_line);
if (line_data)
line_data->out_state = nullptr;
break; break;
} }
uint32_t len; uint32_t len;
char *line = next_line(it, &len); char *line = next_line(it, &len);
if (!line) if (!line)
break; break;
LineData *line_data = line_tree.at(cur_line); LineData *line_data = line_map.create_at(cur_line);
if (!line_data) { std::unique_ptr<StateBase> new_state;
cur_line++;
continue;
}
std::shared_ptr<void> new_state;
if (is_custom) { if (is_custom) {
std::string state = ""; std::string prev_value = "";
if (prev_state) if (prev_state) {
state = std::static_pointer_cast<std::string>(prev_state)->c_str(); CustomState *prev_custom =
std::string out_state = parse_custom(&line_data->tokens, parser_block, static_cast<CustomState *>(prev_state.get());
line, len, state, cur_line); prev_value = prev_custom->value;
new_state = std::make_shared<std::string>(out_state);
} else {
new_state =
parse_func(&line_data->tokens, prev_state, line, len, cur_line);
} }
line_data->in_state = prev_state; std::string out_value = parse_custom(&line_data->tokens, parser_block,
line_data->out_state = new_state; line, len, prev_value, cur_line);
new_state = std::make_unique<CustomState>(out_value);
} else {
new_state = parse_func(&line_data->tokens, prev_state.get(), line, len,
cur_line);
}
line_data->in_state = std::move(prev_state);
line_data->out_state = std::move(new_state);
bool done = false; bool done = false;
if (cur_line + 1 < line_count) { if (cur_line + 1 < line_count) {
LineData *next_line_data = line_tree.at(cur_line + 1); LineData *next_line_data = line_map.at(cur_line + 1);
if (next_line_data) { if (next_line_data) {
if (is_custom) { if (is_custom) {
std::string a = std::string a = "";
prev_state if (new_state) {
? std::static_pointer_cast<std::string>(new_state)->c_str() CustomState *cs = static_cast<CustomState *>(new_state.get());
: ""; a = cs->value;
std::string b = next_line_data->in_state }
? std::static_pointer_cast<std::string>( std::string b = "";
next_line_data->in_state) if (next_line_data->in_state) {
->c_str() CustomState *cs =
: ""; static_cast<CustomState *>(next_line_data->in_state.get());
b = cs->value;
}
done = custom_compare(match_block, a, b); done = custom_compare(match_block, a, b);
} else { } else {
done = state_match_func(new_state, next_line_data->in_state); done = state_match_func(new_state.get(),
next_line_data->in_state.get());
} }
} }
} }
prev_state = new_state; prev_state =
line_data->out_state ? line_data->out_state->clone() : nullptr;
cur_line++; cur_line++;
if (done) if (done)
break; break;

View File

@@ -241,20 +241,22 @@ struct RubyFullState {
} }
}; };
struct RubyState { struct RubyState : StateBase {
using full_state_type = RubyFullState; using full_state_type = RubyFullState;
int interp_level = 0; int interp_level = 0;
std::stack<std::shared_ptr<RubyFullState>> interp_stack; std::stack<RubyFullState> interp_stack;
std::shared_ptr<RubyFullState> full_state; RubyFullState full_state;
std::deque<HeredocInfo> heredocs; std::deque<HeredocInfo> heredocs;
bool operator==(const RubyState &other) const { bool operator==(const RubyState &other) const {
return interp_level == other.interp_level && return interp_level == other.interp_level &&
interp_stack == other.interp_stack && interp_stack == other.interp_stack &&
((full_state && other.full_state && full_state == other.full_state && heredocs == other.heredocs;
*full_state == *other.full_state)) && }
heredocs == other.heredocs;
std::unique_ptr<StateBase> clone() const override {
return std::make_unique<RubyState>(*this);
} }
}; };
@@ -278,18 +280,16 @@ inline static uint32_t get_next_word(const char *text, uint32_t i,
return width; return width;
} }
bool ruby_state_match(std::shared_ptr<void> state_1, bool ruby_state_match(StateBase *state_1, StateBase *state_2) {
std::shared_ptr<void> state_2) {
if (!state_1 || !state_2) if (!state_1 || !state_2)
return false; return false;
return *std::static_pointer_cast<RubyState>(state_1) == return *static_cast<RubyState *>(state_1) ==
*std::static_pointer_cast<RubyState>(state_2); *static_cast<RubyState *>(state_2);
} }
std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens, std::unique_ptr<StateBase> ruby_parse(std::vector<Token> *tokens,
std::shared_ptr<void> in_state, StateBase *in_state, const char *text,
const char *text, uint32_t len, uint32_t len, uint32_t line_num) {
uint32_t line_num) {
static bool keywords_trie_init = false; static bool keywords_trie_init = false;
static Trie<void> base_keywords_trie; static Trie<void> base_keywords_trie;
static Trie<void> expecting_keywords_trie; static Trie<void> expecting_keywords_trie;
@@ -313,7 +313,11 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
keywords_trie_init = true; keywords_trie_init = true;
} }
tokens->clear(); tokens->clear();
auto state = ensure_state(std::static_pointer_cast<RubyState>(in_state)); std::unique_ptr<RubyState> state;
if (in_state)
state = static_unique_ptr_cast<RubyState>(in_state->clone());
else
state = std::make_unique<RubyState>();
uint32_t i = 0; uint32_t i = 0;
while (len > 0 && (text[len - 1] == '\n' || text[len - 1] == '\r' || while (len > 0 && (text[len - 1] == '\n' || text[len - 1] == '\r' ||
text[len - 1] == '\t' || text[len - 1] == ' ')) text[len - 1] == '\t' || text[len - 1] == ' '))
@@ -322,18 +326,18 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
return state; return state;
bool heredoc_first = false; bool heredoc_first = false;
while (i < len) { while (i < len) {
if (state->full_state->in_state == RubyFullState::END) if (state->full_state.in_state == RubyFullState::END)
return state; return state;
if (state->full_state->in_state == RubyFullState::COMMENT) { if (state->full_state.in_state == RubyFullState::COMMENT) {
tokens->push_back({i, len, TokenKind::K_COMMENT}); tokens->push_back({i, len, TokenKind::K_COMMENT});
if (i == 0 && len == 4 && text[i] == '=' && text[i + 1] == 'e' && if (i == 0 && len == 4 && text[i] == '=' && text[i + 1] == 'e' &&
text[i + 2] == 'n' && text[i + 3] == 'd') { text[i + 2] == 'n' && text[i + 3] == 'd') {
state->full_state->in_state = RubyFullState::NONE; state->full_state.in_state = RubyFullState::NONE;
} }
return state; return state;
} }
if (!heredoc_first && if (!heredoc_first &&
state->full_state->in_state == RubyFullState::HEREDOC) { state->full_state.in_state == RubyFullState::HEREDOC) {
if (i == 0) { if (i == 0) {
uint32_t start = 0; uint32_t start = 0;
if (state->heredocs.front().allow_indentation) if (state->heredocs.front().allow_indentation)
@@ -344,7 +348,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
state->heredocs.front().delim.length())) { state->heredocs.front().delim.length())) {
state->heredocs.pop_front(); state->heredocs.pop_front();
if (state->heredocs.empty()) if (state->heredocs.empty())
state->full_state->in_state = RubyFullState::NONE; state->full_state.in_state = RubyFullState::NONE;
tokens->push_back({i, len, TokenKind::K_ANNOTATION}); tokens->push_back({i, len, TokenKind::K_ANNOTATION});
return state; return state;
} }
@@ -421,7 +425,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({i, i + 2, TokenKind::K_INTERPOLATION}); tokens->push_back({i, i + 2, TokenKind::K_INTERPOLATION});
i += 2; i += 2;
state->interp_stack.push(state->full_state); state->interp_stack.push(state->full_state);
state->full_state = std::make_shared<RubyFullState>(); state->full_state = RubyFullState();
state->interp_level = 1; state->interp_level = 1;
break; break;
} }
@@ -432,7 +436,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
continue; continue;
} }
} }
if (state->full_state->in_state == RubyFullState::STRING) { if (state->full_state.in_state == RubyFullState::STRING) {
uint32_t start = i; uint32_t start = i;
while (i < len) { while (i < len) {
if (text[i] == '\\') { if (text[i] == '\\') {
@@ -496,36 +500,36 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({start, i, TokenKind::K_ESCAPE}); tokens->push_back({start, i, TokenKind::K_ESCAPE});
continue; continue;
} }
if (state->full_state->lit.allow_interp && text[i] == '#' && if (state->full_state.lit.allow_interp && text[i] == '#' &&
i + 1 < len && text[i + 1] == '{') { i + 1 < len && text[i + 1] == '{') {
tokens->push_back({start, i, TokenKind::K_STRING}); tokens->push_back({start, i, TokenKind::K_STRING});
tokens->push_back({i, i + 2, TokenKind::K_INTERPOLATION}); tokens->push_back({i, i + 2, TokenKind::K_INTERPOLATION});
i += 2; i += 2;
state->interp_stack.push(state->full_state); state->interp_stack.push(state->full_state);
state->full_state = std::make_shared<RubyFullState>(); state->full_state = RubyFullState();
state->interp_level = 1; state->interp_level = 1;
break; break;
} }
if (text[i] == state->full_state->lit.delim_start && if (text[i] == state->full_state.lit.delim_start &&
state->full_state->lit.delim_start != state->full_state.lit.delim_start !=
state->full_state->lit.delim_end) { state->full_state.lit.delim_end) {
state->full_state->lit.brace_level++; state->full_state.lit.brace_level++;
} }
if (text[i] == state->full_state->lit.delim_end) { if (text[i] == state->full_state.lit.delim_end) {
if (state->full_state->lit.delim_start == if (state->full_state.lit.delim_start ==
state->full_state->lit.delim_end) { state->full_state.lit.delim_end) {
i++; i++;
tokens->push_back({start, i, TokenKind::K_STRING}); tokens->push_back({start, i, TokenKind::K_STRING});
state->full_state->in_state = RubyFullState::NONE; state->full_state.in_state = RubyFullState::NONE;
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
break; break;
} else { } else {
state->full_state->lit.brace_level--; state->full_state.lit.brace_level--;
if (state->full_state->lit.brace_level == 0) { if (state->full_state.lit.brace_level == 0) {
i++; i++;
tokens->push_back({start, i, TokenKind::K_STRING}); tokens->push_back({start, i, TokenKind::K_STRING});
state->full_state->in_state = RubyFullState::NONE; state->full_state.in_state = RubyFullState::NONE;
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
break; break;
} }
} }
@@ -536,7 +540,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({start, len, TokenKind::K_STRING}); tokens->push_back({start, len, TokenKind::K_STRING});
continue; continue;
} }
if (state->full_state->in_state == RubyFullState::REGEXP) { if (state->full_state.in_state == RubyFullState::REGEXP) {
uint32_t start = i; uint32_t start = i;
while (i < len) { while (i < len) {
if (text[i] == '\\') { if (text[i] == '\\') {
@@ -605,30 +609,30 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({i, i + 2, TokenKind::K_INTERPOLATION}); tokens->push_back({i, i + 2, TokenKind::K_INTERPOLATION});
i += 2; i += 2;
state->interp_stack.push(state->full_state); state->interp_stack.push(state->full_state);
state->full_state = std::make_shared<RubyFullState>(); state->full_state = RubyFullState();
state->interp_level = 1; state->interp_level = 1;
break; break;
} }
if (text[i] == state->full_state->lit.delim_start && if (text[i] == state->full_state.lit.delim_start &&
state->full_state->lit.delim_start != state->full_state.lit.delim_start !=
state->full_state->lit.delim_end) { state->full_state.lit.delim_end) {
state->full_state->lit.brace_level++; state->full_state.lit.brace_level++;
} }
if (text[i] == state->full_state->lit.delim_end) { if (text[i] == state->full_state.lit.delim_end) {
if (state->full_state->lit.delim_start == if (state->full_state.lit.delim_start ==
state->full_state->lit.delim_end) { state->full_state.lit.delim_end) {
i += 1; i += 1;
tokens->push_back({start, i, TokenKind::K_REGEXP}); tokens->push_back({start, i, TokenKind::K_REGEXP});
state->full_state->in_state = RubyFullState::NONE; state->full_state.in_state = RubyFullState::NONE;
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
break; break;
} else { } else {
state->full_state->lit.brace_level--; state->full_state.lit.brace_level--;
if (state->full_state->lit.brace_level == 0) { if (state->full_state.lit.brace_level == 0) {
i += 1; i += 1;
tokens->push_back({start, i, TokenKind::K_REGEXP}); tokens->push_back({start, i, TokenKind::K_REGEXP});
state->full_state->in_state = RubyFullState::NONE; state->full_state.in_state = RubyFullState::NONE;
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
break; break;
} }
} }
@@ -642,8 +646,8 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
if (i == 0 && len == 6) { if (i == 0 && len == 6) {
if (text[i] == '=' && text[i + 1] == 'b' && text[i + 2] == 'e' && if (text[i] == '=' && text[i + 1] == 'b' && text[i + 2] == 'e' &&
text[i + 3] == 'g' && text[i + 4] == 'i' && text[i + 5] == 'n') { text[i + 3] == 'g' && text[i + 4] == 'i' && text[i + 5] == 'n') {
state->full_state->in_state = RubyFullState::COMMENT; state->full_state.in_state = RubyFullState::COMMENT;
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
tokens->push_back({0, len, TokenKind::K_COMMENT}); tokens->push_back({0, len, TokenKind::K_COMMENT});
return state; return state;
} }
@@ -653,8 +657,8 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
text[i + 3] == 'N' && text[i + 4] == 'D' && text[i + 5] == '_' && text[i + 3] == 'N' && text[i + 4] == 'D' && text[i + 5] == '_' &&
text[i + 6] == '_') { text[i + 6] == '_') {
tokens->clear(); tokens->clear();
state->full_state->in_state = RubyFullState::END; state->full_state.in_state = RubyFullState::END;
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
return state; return state;
} }
} }
@@ -684,33 +688,33 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
delim += text[j++]; delim += text[j++];
} }
} }
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
if (!delim.empty()) { if (!delim.empty()) {
tokens->push_back({s, j, TokenKind::K_ANNOTATION}); tokens->push_back({s, j, TokenKind::K_ANNOTATION});
state->heredocs.push_back({delim, interpolation, indented}); state->heredocs.push_back({delim, interpolation, indented});
state->full_state->in_state = RubyFullState::HEREDOC; state->full_state.in_state = RubyFullState::HEREDOC;
heredoc_first = true; heredoc_first = true;
} }
i = j; i = j;
continue; continue;
} }
if (text[i] == '/' && state->full_state->expecting_expr) { if (text[i] == '/' && state->full_state.expecting_expr) {
tokens->push_back({i, i + 1, TokenKind::K_REGEXP}); tokens->push_back({i, i + 1, TokenKind::K_REGEXP});
state->full_state->in_state = RubyFullState::REGEXP; state->full_state.in_state = RubyFullState::REGEXP;
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
state->full_state->lit.delim_start = '/'; state->full_state.lit.delim_start = '/';
state->full_state->lit.delim_end = '/'; state->full_state.lit.delim_end = '/';
state->full_state->lit.allow_interp = true; state->full_state.lit.allow_interp = true;
i++; i++;
continue; continue;
} else if (text[i] == '#') { } else if (text[i] == '#') {
if (line_num == 0 && i == 0 && len > 4 && text[i + 1] == '!') { if (line_num == 0 && i == 0 && len > 4 && text[i + 1] == '!') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
tokens->push_back({0, len, TokenKind::K_SHEBANG}); tokens->push_back({0, len, TokenKind::K_SHEBANG});
return state; return state;
} }
tokens->push_back({i, len, TokenKind::K_COMMENT}); tokens->push_back({i, len, TokenKind::K_COMMENT});
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
return state; return state;
} else if (text[i] == '.') { } else if (text[i] == '.') {
uint32_t start = i; uint32_t start = i;
@@ -722,15 +726,15 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
} }
} }
tokens->push_back({start, i, TokenKind::K_OPERATOR}); tokens->push_back({start, i, TokenKind::K_OPERATOR});
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
continue; continue;
} else if (text[i] == ':') { } else if (text[i] == ':') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
uint32_t start = i; uint32_t start = i;
i++; i++;
if (i >= len) { if (i >= len) {
tokens->push_back({start, i, TokenKind::K_OPERATOR}); tokens->push_back({start, i, TokenKind::K_OPERATOR});
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
continue; continue;
} }
if (text[i] == ':') { if (text[i] == ':') {
@@ -739,7 +743,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
} }
if (text[i] == '\'' || text[i] == '"') { if (text[i] == '\'' || text[i] == '"') {
tokens->push_back({start, i, TokenKind::K_LABEL}); tokens->push_back({start, i, TokenKind::K_LABEL});
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
continue; continue;
} }
if (text[i] == '$' || text[i] == '@') { if (text[i] == '$' || text[i] == '@') {
@@ -767,7 +771,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({start, i, TokenKind::K_OPERATOR}); tokens->push_back({start, i, TokenKind::K_OPERATOR});
continue; continue;
} else if (text[i] == '@') { } else if (text[i] == '@') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
uint32_t start = i; uint32_t start = i;
i++; i++;
if (i >= len) if (i >= len)
@@ -783,7 +787,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({start, i, TokenKind::K_VARIABLEINSTANCE}); tokens->push_back({start, i, TokenKind::K_VARIABLEINSTANCE});
continue; continue;
} else if (text[i] == '$') { } else if (text[i] == '$') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
uint32_t start = i; uint32_t start = i;
i++; i++;
if (i >= len) if (i >= len)
@@ -806,7 +810,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({start, i, TokenKind::K_VARIABLEGLOBAL}); tokens->push_back({start, i, TokenKind::K_VARIABLEGLOBAL});
continue; continue;
} else if (text[i] == '?') { } else if (text[i] == '?') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
uint32_t start = i; uint32_t start = i;
i++; i++;
if (i < len && text[i] == '\\') { if (i < len && text[i] == '\\') {
@@ -851,95 +855,95 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({start, i, TokenKind::K_CHAR}); tokens->push_back({start, i, TokenKind::K_CHAR});
continue; continue;
} else { } else {
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
tokens->push_back({start, i, TokenKind::K_OPERATOR}); tokens->push_back({start, i, TokenKind::K_OPERATOR});
continue; continue;
} }
} else if (text[i] == '{') { } else if (text[i] == '{') {
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
uint8_t brace_color = uint8_t brace_color =
(uint8_t)TokenKind::K_BRACE1 + (state->full_state->brace_level % 5); (uint8_t)TokenKind::K_BRACE1 + (state->full_state.brace_level % 5);
tokens->push_back({i, i + 1, (TokenKind)brace_color}); tokens->push_back({i, i + 1, (TokenKind)brace_color});
state->interp_level++; state->interp_level++;
state->full_state->brace_level++; state->full_state.brace_level++;
i++; i++;
continue; continue;
} else if (text[i] == '}') { } else if (text[i] == '}') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
state->interp_level--; state->interp_level--;
if (state->interp_level == 0 && !state->interp_stack.empty()) { if (state->interp_level == 0 && !state->interp_stack.empty()) {
state->full_state = state->interp_stack.top(); state->full_state = state->interp_stack.top();
state->interp_stack.pop(); state->interp_stack.pop();
tokens->push_back({i, i + 1, TokenKind::K_INTERPOLATION}); tokens->push_back({i, i + 1, TokenKind::K_INTERPOLATION});
} else { } else {
state->full_state->brace_level--; state->full_state.brace_level--;
uint8_t brace_color = uint8_t brace_color =
(uint8_t)TokenKind::K_BRACE1 + (state->full_state->brace_level % 5); (uint8_t)TokenKind::K_BRACE1 + (state->full_state.brace_level % 5);
tokens->push_back({i, i + 1, (TokenKind)brace_color}); tokens->push_back({i, i + 1, (TokenKind)brace_color});
} }
i++; i++;
continue; continue;
} else if (text[i] == '(') { } else if (text[i] == '(') {
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
uint8_t brace_color = uint8_t brace_color =
(uint8_t)TokenKind::K_BRACE1 + (state->full_state->brace_level % 5); (uint8_t)TokenKind::K_BRACE1 + (state->full_state.brace_level % 5);
tokens->push_back({i, i + 1, (TokenKind)brace_color}); tokens->push_back({i, i + 1, (TokenKind)brace_color});
state->full_state->brace_level++; state->full_state.brace_level++;
i++; i++;
continue; continue;
} else if (text[i] == ')') { } else if (text[i] == ')') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
state->full_state->brace_level--; state->full_state.brace_level--;
uint8_t brace_color = uint8_t brace_color =
(uint8_t)TokenKind::K_BRACE1 + (state->full_state->brace_level % 5); (uint8_t)TokenKind::K_BRACE1 + (state->full_state.brace_level % 5);
tokens->push_back({i, i + 1, (TokenKind)brace_color}); tokens->push_back({i, i + 1, (TokenKind)brace_color});
i++; i++;
continue; continue;
} else if (text[i] == '[') { } else if (text[i] == '[') {
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
uint8_t brace_color = uint8_t brace_color =
(uint8_t)TokenKind::K_BRACE1 + (state->full_state->brace_level % 5); (uint8_t)TokenKind::K_BRACE1 + (state->full_state.brace_level % 5);
tokens->push_back({i, i + 1, (TokenKind)brace_color}); tokens->push_back({i, i + 1, (TokenKind)brace_color});
state->full_state->brace_level++; state->full_state.brace_level++;
i++; i++;
continue; continue;
} else if (text[i] == ']') { } else if (text[i] == ']') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
state->full_state->brace_level--; state->full_state.brace_level--;
uint8_t brace_color = uint8_t brace_color =
(uint8_t)TokenKind::K_BRACE1 + (state->full_state->brace_level % 5); (uint8_t)TokenKind::K_BRACE1 + (state->full_state.brace_level % 5);
tokens->push_back({i, i + 1, (TokenKind)brace_color}); tokens->push_back({i, i + 1, (TokenKind)brace_color});
i++; i++;
continue; continue;
} else if (text[i] == '\'') { } else if (text[i] == '\'') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
tokens->push_back({i, i + 1, TokenKind::K_STRING}); tokens->push_back({i, i + 1, TokenKind::K_STRING});
state->full_state->in_state = RubyFullState::STRING; state->full_state.in_state = RubyFullState::STRING;
state->full_state->lit.delim_start = '\''; state->full_state.lit.delim_start = '\'';
state->full_state->lit.delim_end = '\''; state->full_state.lit.delim_end = '\'';
state->full_state->lit.allow_interp = false; state->full_state.lit.allow_interp = false;
i++; i++;
continue; continue;
} else if (text[i] == '"') { } else if (text[i] == '"') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
tokens->push_back({i, i + 1, TokenKind::K_STRING}); tokens->push_back({i, i + 1, TokenKind::K_STRING});
state->full_state->in_state = RubyFullState::STRING; state->full_state.in_state = RubyFullState::STRING;
state->full_state->lit.delim_start = '"'; state->full_state.lit.delim_start = '"';
state->full_state->lit.delim_end = '"'; state->full_state.lit.delim_end = '"';
state->full_state->lit.allow_interp = true; state->full_state.lit.allow_interp = true;
i++; i++;
continue; continue;
} else if (text[i] == '`') { } else if (text[i] == '`') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
tokens->push_back({i, i + 1, TokenKind::K_STRING}); tokens->push_back({i, i + 1, TokenKind::K_STRING});
state->full_state->in_state = RubyFullState::STRING; state->full_state.in_state = RubyFullState::STRING;
state->full_state->lit.delim_start = '`'; state->full_state.lit.delim_start = '`';
state->full_state->lit.delim_end = '`'; state->full_state.lit.delim_end = '`';
state->full_state->lit.allow_interp = true; state->full_state.lit.allow_interp = true;
i++; i++;
continue; continue;
} else if (text[i] == '%') { } else if (text[i] == '%') {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
if (i + 1 >= len) { if (i + 1 >= len) {
i++; i++;
continue; continue;
@@ -1005,16 +1009,16 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back( tokens->push_back(
{i, i + prefix_len + 1, {i, i + prefix_len + 1,
(is_regexp ? TokenKind::K_REGEXP : TokenKind::K_STRING)}); (is_regexp ? TokenKind::K_REGEXP : TokenKind::K_STRING)});
state->full_state->in_state = state->full_state.in_state =
is_regexp ? RubyFullState::REGEXP : RubyFullState::STRING; is_regexp ? RubyFullState::REGEXP : RubyFullState::STRING;
state->full_state->lit.delim_start = delim_start; state->full_state.lit.delim_start = delim_start;
state->full_state->lit.delim_end = delim_end; state->full_state.lit.delim_end = delim_end;
state->full_state->lit.allow_interp = allow_interp; state->full_state.lit.allow_interp = allow_interp;
state->full_state->lit.brace_level = 1; state->full_state.lit.brace_level = 1;
i += prefix_len + 1; i += prefix_len + 1;
continue; continue;
} else if (isdigit(text[i])) { } else if (isdigit(text[i])) {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
uint32_t start = i; uint32_t start = i;
if (text[i] == '0') { if (text[i] == '0') {
i++; i++;
@@ -1115,7 +1119,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
tokens->push_back({start, i, TokenKind::K_NUMBER}); tokens->push_back({start, i, TokenKind::K_NUMBER});
continue; continue;
} else if (identifier_start_char(text[i])) { } else if (identifier_start_char(text[i])) {
state->full_state->expecting_expr = false; state->full_state.expecting_expr = false;
uint32_t length; uint32_t length;
if ((length = base_keywords_trie.match(text, i, len, identifier_char))) { if ((length = base_keywords_trie.match(text, i, len, identifier_char))) {
tokens->push_back({i, i + length, TokenKind::K_KEYWORD}); tokens->push_back({i, i + length, TokenKind::K_KEYWORD});
@@ -1123,7 +1127,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
continue; continue;
} else if ((length = expecting_keywords_trie.match(text, i, len, } else if ((length = expecting_keywords_trie.match(text, i, len,
identifier_char))) { identifier_char))) {
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
tokens->push_back({i, i + length, TokenKind::K_KEYWORD}); tokens->push_back({i, i + length, TokenKind::K_KEYWORD});
i += length; i += length;
continue; continue;
@@ -1134,7 +1138,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
continue; continue;
} else if ((length = expecting_operators_trie.match( } else if ((length = expecting_operators_trie.match(
text, i, len, identifier_char)) > 0) { text, i, len, identifier_char)) > 0) {
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
tokens->push_back({i, i + length, TokenKind::K_KEYWORDOPERATOR}); tokens->push_back({i, i + length, TokenKind::K_KEYWORDOPERATOR});
i += length; i += length;
continue; continue;
@@ -1256,7 +1260,7 @@ std::shared_ptr<void> ruby_parse(std::vector<Token> *tokens,
operator_trie.match(text, i, len, [](char) { return false; }))) { operator_trie.match(text, i, len, [](char) { return false; }))) {
tokens->push_back({i, i + op_len, TokenKind::K_OPERATOR}); tokens->push_back({i, i + op_len, TokenKind::K_OPERATOR});
i += op_len; i += op_len;
state->full_state->expecting_expr = true; state->full_state.expecting_expr = true;
continue; continue;
} else { } else {
i += utf8_codepoint_width(text[i]); i += utf8_codepoint_width(text[i]);

View File

@@ -10,7 +10,8 @@ void Bar::log(std::string message) { log_line = message; }
void Bar::render(std::vector<ScreenCell> &buffer) { void Bar::render(std::vector<ScreenCell> &buffer) {
USING(LSPInstance); USING(LSPInstance);
BarLine bar_line; BarLine bar_line;
bar_line = bar_contents(mode, screen.col, pwd.string(), focused_window); bar_line =
bar_contents(mode, screen.col, pwd.string(), layout::focused_window);
auto update = [&](uint32_t row, uint32_t col, std::string text, uint32_t fg, auto update = [&](uint32_t row, uint32_t col, std::string text, uint32_t fg,
uint32_t bg, uint8_t flags, uint32_t width) { uint32_t bg, uint8_t flags, uint32_t width) {
ScreenCell &c = buffer[row * screen.col + col]; ScreenCell &c = buffer[row * screen.col + col];
@@ -61,8 +62,8 @@ void Bar::handle_command(std::string &command) {
running = false; running = false;
return; return;
} }
if (focused_window) if (layout::focused_window)
focused_window->handle_command(command); layout::focused_window->handle_command(command);
} }
void Bar::handle_event(KeyEvent event) { void Bar::handle_event(KeyEvent event) {

View File

@@ -1,164 +0,0 @@
// #include "ui/diagnostics.h"
//
// void DiagnosticBox::clear() {
// warnings.clear();
// cells.clear();
// size = {0, 0};
// };
//
// void DiagnosticBox::render_first() {
// if (warnings.empty())
// return;
// uint32_t longest_line = 8 + warnings[0].source.length();
// for (auto &warn : warnings) {
// uint32_t longest = 0;
// uint32_t cur = 0;
// for (char ch : warn.text_full)
// if (ch == '\n') {
// longest = MAX(longest, cur);
// cur = 0;
// } else {
// if (ch == '\t')
// cur += 3;
// ++cur;
// }
// longest = MAX(longest, cur);
// longest_line = MAX(longest_line, longest + 7);
// longest_line = MAX(longest_line, (uint32_t)warn.code.length() + 4);
// for (auto &see_also : warn.see_also)
// longest_line = MAX(longest_line, (uint32_t)see_also.length() + 4);
// }
// uint32_t content_width = MIN(longest_line, 150u);
// size.col = content_width + 2;
// cells.assign(size.col * 25, {" ", 0, 0, 0, 0, 0});
// auto set = [&](uint32_t r, uint32_t c, const char *text, uint32_t fg,
// uint32_t bg, uint8_t flags) {
// cells[r * size.col + c] = {std::string(text), 0, fg, bg, flags, 0};
// };
// uint32_t base_bg = 0;
// uint32_t border_fg = 0x82AAFF;
// uint32_t r = 0;
// if (warnings[0].source != "") {
// std::string src_txt = "Source: ";
// for (uint32_t i = 0; i < src_txt.length() && i < content_width; i++)
// set(1, i + 1, (char[2]){src_txt[i], 0}, 0x3EAAFF, base_bg, 0);
// for (uint32_t i = 0; i < warnings[0].source.length() && i <
// content_width;
// i++)
// set(1, i + 1 + src_txt.length(), (char[2]){warnings[0].source[i], 0},
// 0xffffff, base_bg, 0);
// r++;
// }
// int idx = 1;
// for (auto &warn : warnings) {
// char buf[4];
// std::snprintf(buf, sizeof(buf), "%2d", idx % 100);
// std::string line_txt = std::string(buf) + ". ";
// for (uint32_t i = 0; i < line_txt.length(); i++)
// set(r + 1, i + 1, (char[2]){line_txt[i], 0}, 0xffffff, base_bg, 0);
// if (r >= 23)
// break;
// const char *err_sym = "";
// uint32_t c_sym = 0xAAAAAA;
// switch (warn.type) {
// case 1:
// err_sym = "";
// c_sym = 0xFF0000;
// break;
// case 2:
// err_sym = "";
// c_sym = 0xFFFF00;
// break;
// case 3:
// err_sym = "";
// c_sym = 0xFF00FF;
// break;
// case 4:
// err_sym = "";
// c_sym = 0xAAAAAA;
// break;
// }
// std::string text = warn.text_full + " " + err_sym;
// uint32_t i = 0;
// while (i < text.length() && r < 23) {
// uint32_t c = 4;
// while (c < content_width && i < text.length()) {
// if (text[i] == '\n') {
// while (i < text.length() && text[i] == '\n')
// i++;
// break;
// }
// uint32_t cluster_len = grapheme_next_character_break_utf8(
// text.c_str() + i, text.length() - i);
// std::string cluster = text.substr(i, cluster_len);
// int width = display_width(cluster.c_str(), cluster_len);
// if (c + width > content_width)
// break;
// set(r + 1, c + 1, cluster.c_str(), c_sym, base_bg, 0);
// c += width;
// i += cluster_len;
// for (int w = 1; w < width; w++)
// set(r + 1, c - w + 1, "\x1b", c_sym, base_bg, 0);
// }
// r++;
// }
// if (r >= 23)
// break;
// if (warn.code != "") {
// for (uint32_t i = 0; i < warn.code.length() && i + 5 < content_width;
// i++)
// set(r + 1, i + 5, (char[2]){warn.code[i], 0}, 0x81cdc6, base_bg, 0);
// r++;
// }
// if (r >= 23)
// break;
// for (std::string &see_also : warn.see_also) {
// uint32_t fg = 0xB55EFF;
// uint8_t colon_count = 0;
// for (uint32_t i = 0; i < see_also.length() && i + 5 < content_width;
// i++) {
// set(r + 1, i + 5, (char[2]){see_also[i], 0}, fg, base_bg, 0);
// if (see_also[i] == ':')
// colon_count++;
// if (colon_count == 2)
// fg = 0xFFFFFF;
// }
// r++;
// if (r >= 23)
// break;
// };
// idx++;
// }
// size.row = 2 + r;
// set(0, 0, "┌", border_fg, base_bg, 0);
// for (uint32_t i = 1; i < size.col - 1; i++)
// set(0, i, "─", border_fg, base_bg, 0);
// set(0, size.col - 1, "┐", border_fg, base_bg, 0);
// for (uint32_t r = 1; r < size.row - 1; r++) {
// set(r, 0, "│", border_fg, base_bg, 0);
// set(r, size.col - 1, "│", border_fg, base_bg, 0);
// }
// set(size.row - 1, 0, "└", border_fg, base_bg, 0);
// for (uint32_t i = 1; i < size.col - 1; i++)
// set(size.row - 1, i, "─", border_fg, base_bg, 0);
// set(size.row - 1, size.col - 1, "┘", border_fg, base_bg, 0);
// cells.resize(size.col * size.row);
// }
//
// void DiagnosticBox::render(Coord pos) {
// int32_t start_row = (int32_t)pos.row - (int32_t)size.row;
// if (start_row < 0)
// start_row = pos.row + 1;
// int32_t start_col = pos.col;
// Coord screen_size = get_size();
// if (start_col + size.col > screen_size.col) {
// start_col = screen_size.col - size.col;
// if (start_col < 0)
// start_col = 0;
// }
// for (uint32_t r = 0; r < size.row; r++)
// for (uint32_t c = 0; c < size.col; c++)
// update(start_row + r, start_col + c, cells[r * size.col + c].utf8,
// cells[r * size.col + c].fg, cells[r * size.col + c].bg,
// cells[r * size.col + c].flags);
// }

View File

@@ -1,23 +1,49 @@
#include "io/sysio.h"
#include "main.h" #include "main.h"
#include "windows/decl.h" #include "windows/decl.h"
TileRoot root_tile; namespace layout {
std::vector<std::unique_ptr<TileRoot>> popups; TileBase root_tile;
Window *focused_window; Window *focused_window;
std::vector<std::unique_ptr<Popup>> popups;
std::vector<std::unique_ptr<TileBase>> floating_tiles;
} // namespace layout
void render() { void render() {
ui::bar.render(new_screen); ui::bar.render(io::new_screen);
root_tile.render(new_screen); layout::root_tile.render(io::new_screen);
for (auto &popup : popups) for (auto &tile : layout::floating_tiles) {
popup->render(new_screen); if (tile->hidden())
continue;
tile->render(io::new_screen);
}
for (auto &popup : layout::popups) {
if (popup->hidden)
continue;
popup->render(io::new_screen, popup->size, popup->pos);
}
} }
void handle_click(KeyEvent event) { void handle_click(KeyEvent event) {
for (auto &popup : popups) { for (auto &popup : layout::popups) {
if (popup->inside(event.mouse_x, event.mouse_y)) { if (!popup->hidden &&
popup->handle_click(event); inside({event.mouse_y, event.mouse_x}, popup->pos, popup->size)) {
event.mouse_x -= popup->pos.col;
event.mouse_y -= popup->pos.row;
popup->handle_click(event, popup->size);
return; return;
} }
} }
root_tile.handle_click(event); for (auto &tile : layout::floating_tiles) {
if (!tile->hidden() &&
inside({event.mouse_y, event.mouse_x}, tile->pos, tile->size)) {
event.mouse_x -= tile->pos.col;
event.mouse_y -= tile->pos.row;
tile->handle_click(event);
return;
}
}
if (inside({event.mouse_y, event.mouse_x}, layout::root_tile.pos,
layout::root_tile.size))
layout::root_tile.handle_click(event);
} }