Basic lsp and precompiler header support

This commit is contained in:
2025-12-24 11:08:25 +00:00
parent 85d4039a5e
commit a10dd92249
20 changed files with 1499 additions and 389 deletions

View File

@@ -1,23 +1,16 @@
#ifndef EDITOR_H
#define EDITOR_H
#include "../libs/tree-sitter/lib/include/tree_sitter/api.h"
#include "./knot.h"
#include "./pch.h"
#include "./ui.h"
#include "./utils.h"
#include <algorithm>
#include <cstdint>
#include <map>
#include <shared_mutex>
#include <unordered_map>
#include <vector>
#define CHAR 0
#define WORD 1
#define LINE 2
#define EXTRA_META 4
#define INDENT_WIDTH 2
struct Highlight {
@@ -125,7 +118,8 @@ struct VAI {
};
struct Editor {
const char *filename;
std::string filename;
std::string uri;
Knot *root;
std::shared_mutex knot_mtx;
Coord cursor;
@@ -138,6 +132,7 @@ struct Editor {
Coord scroll;
TSTree *tree;
TSParser *parser;
std::string query_file;
TSQuery *query;
const TSLanguage *language;
Queue<TSInputEdit> edit_queue;
@@ -150,6 +145,8 @@ struct Editor {
std::vector<VHint> hints;
std::vector<VWarn> warnings;
VAI ai;
std::shared_mutex lsp_mtx;
struct LSPInstance *lsp;
};
inline const Fold *fold_for_line(const std::vector<Fold> &folds,
@@ -217,6 +214,8 @@ void cursor_up(Editor *editor, uint32_t number);
void cursor_down(Editor *editor, uint32_t number);
Coord move_left(Editor *editor, Coord cursor, uint32_t number);
Coord move_right(Editor *editor, Coord cursor, uint32_t number);
Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number);
Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number);
void cursor_left(Editor *editor, uint32_t number);
void cursor_right(Editor *editor, uint32_t number);
void scroll_up(Editor *editor, int32_t number);
@@ -247,5 +246,6 @@ void apply_line_deletion(Editor *editor, uint32_t removal_start,
uint32_t leading_indent(const char *line, uint32_t len);
uint32_t get_indent(Editor *editor, Coord cursor);
bool closing_after_cursor(const char *line, uint32_t len, uint32_t col);
// void editor_lsp_handle(Editor *editor, json msg);
#endif

View File

@@ -1,9 +1,8 @@
#ifndef ROPE_H
#define ROPE_H
#include "./pch.h"
#include "./utils.h"
#include <cstdint>
#include <vector>
#define MIN_CHUNK_SIZE 64 // 64 Bytes
#define MAX_CHUNK_SIZE 1024 * 8 // 8192 Bytes (8 KiB)

56
include/lsp.h Normal file
View File

@@ -0,0 +1,56 @@
#ifndef LSP_H
#define LSP_H
#include "./editor.h"
#include "./pch.h"
#include "utils.h"
struct LSP {
const char *command;
std::vector<const char *> args;
};
struct LSPPending {
std::string method;
Editor *editor = nullptr;
std::function<void(Editor *, std::string, json)> callback;
};
struct LSPOpenRequest {
Language language;
Editor *editor;
};
struct LSPInstance {
std::shared_mutex mtx;
LSP *lsp;
std::string root_dir;
int pid{-1};
int stdin_fd{-1};
int stdout_fd{-1};
bool initialized = false;
uint32_t last_id = 0;
Queue<json> inbox;
Queue<json> outbox;
std::unordered_map<uint32_t, LSPPending *> pending;
std::vector<Editor *> editors;
};
extern std::shared_mutex active_lsps_mtx;
extern std::unordered_map<uint8_t, LSPInstance *> active_lsps;
extern std::unordered_map<uint8_t, LSP> lsp_map;
void lsp_worker();
void lsp_handle(LSPInstance *lsp, json message);
LSPInstance *get_or_init_lsp(uint8_t lsp_id);
void close_lsp(uint8_t lsp_id);
void request_add_to_lsp(Language language, Editor *editor);
void add_to_lsp(Language language, Editor *editor);
void remove_from_lsp(Editor *editor);
void lsp_send(LSPInstance *lsp, json message, LSPPending *pending);
#endif

View File

@@ -1,8 +1,7 @@
#ifndef MAIN_H
#define MAIN_H
#include <atomic>
#include <vector>
#include "./pch.h"
#define NORMAL 0
#define INSERT 1

33
include/pch.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef PCH_H
#define PCH_H
#define PCRE2_CODE_UNIT_WIDTH 8
#define PCRE_WORKSPACE_SIZE 512
#include "../libs/tree-sitter/lib/include/tree_sitter/api.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <deque>
#include <functional>
#include <map>
#include <mutex>
#include <nlohmann/json.hpp>
#include <optional>
#include <pcre2.h>
#include <queue>
#include <shared_mutex>
#include <string.h>
#include <string>
#include <sys/ioctl.h>
#include <termios.h>
#include <thread>
#include <unistd.h>
#include <unordered_map>
#include <vector>
using json = nlohmann::json;
using namespace std::chrono_literals;
#endif

View File

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

View File

@@ -1,9 +1,12 @@
#include "../libs/tree-sitter/lib/include/tree_sitter/api.h"
#include <string>
#ifndef TS_DEF_H
#define TS_DEF_H
#include "./pch.h"
struct Language {
std::string name;
const TSLanguage *(*fn)();
uint8_t lsp_id = 0;
};
extern "C" {
@@ -22,3 +25,5 @@ const TSLanguage *tree_sitter_make();
const TSLanguage *tree_sitter_python();
const TSLanguage *tree_sitter_ruby();
}
#endif

View File

@@ -1,16 +1,8 @@
#ifndef UI_H
#define UI_H
#include "./pch.h"
#include "./utils.h"
#include <atomic>
#include <cstdint>
#include <mutex>
#include <string.h>
#include <string>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <vector>
#define KEY_CHAR 0
#define KEY_SPECIAL 1

View File

@@ -1,16 +1,8 @@
#ifndef UTILS_H
#define UTILS_H
#include "./pch.h"
#include "./ts_def.h"
#include <chrono>
#include <functional>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#define PCRE2_CODE_UNIT_WIDTH 8
#define PCRE_WORKSPACE_SIZE 512
template <typename T> struct Queue {
std::queue<T> q;
@@ -20,6 +12,10 @@ template <typename T> struct Queue {
std::lock_guard<std::mutex> lock(m);
q.push(val);
}
T front() {
std::lock_guard<std::mutex> lock(m);
return q.front();
}
bool pop(T &val) {
std::lock_guard<std::mutex> lock(m);
if (q.empty())
@@ -28,6 +24,10 @@ template <typename T> struct Queue {
q.pop();
return true;
}
void pop() {
std::lock_guard<std::mutex> lock(m);
q.pop();
}
bool empty() {
std::lock_guard<std::mutex> lock(m);
return q.empty();
@@ -52,6 +52,7 @@ struct Coord {
bool operator>=(const Coord &other) const { return !(*this < other); }
};
std::string path_to_file_uri(const std::string &path_str);
int display_width(const char *str, size_t len);
uint32_t get_visual_col_from_bytes(const char *line, uint32_t len,
uint32_t byte_limit);