Add tree-sitter injections support & cleanup

This commit is contained in:
2025-12-25 04:14:53 +00:00
parent a10dd92249
commit 659628835d
16 changed files with 302 additions and 323 deletions

View File

@@ -5,6 +5,8 @@
#include "./pch.h"
#include "./ui.h"
#include "./utils.h"
#include "ts_def.h"
#include <cstdint>
#define CHAR 0
#define WORD 1
@@ -117,6 +119,25 @@ struct VAI {
// after the first one
};
struct TSSetBase {
std::string lang;
TSTree *tree;
TSParser *parser;
std::string query_file;
TSQuery *query;
std::map<uint16_t, Highlight> query_map;
std::map<uint16_t, Language> injection_map;
const TSLanguage *language;
};
struct TSSet : TSSetBase {
std::vector<TSRange> ranges;
};
struct TSSetMain : TSSetBase {
std::vector<TSSet> injections;
};
struct Editor {
std::string filename;
std::string uri;
@@ -130,13 +151,8 @@ struct Editor {
Coord position;
Coord size;
Coord scroll;
TSTree *tree;
TSParser *parser;
std::string query_file;
TSQuery *query;
const TSLanguage *language;
TSSetMain ts;
Queue<TSInputEdit> edit_queue;
std::vector<Highlight> query_map;
std::vector<Fold> folds;
Spans spans;
Spans def_spans;

View File

@@ -24,7 +24,7 @@ struct LSPOpenRequest {
struct LSPInstance {
std::shared_mutex mtx;
LSP *lsp;
const LSP *lsp;
std::string root_dir;
int pid{-1};
int stdin_fd{-1};
@@ -39,7 +39,6 @@ struct LSPInstance {
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);

65
include/maps.h Normal file
View File

@@ -0,0 +1,65 @@
#ifndef MAPS_H
#define MAPS_H
#include "./lsp.h"
#include "./pch.h"
#include "./ts_def.h"
#include <unordered_map>
static const std::unordered_map<std::string, Language> kLanguages = {
{"bash", {"bash", tree_sitter_bash}},
{"c", {"c", tree_sitter_c, 1}},
{"cpp", {"cpp", tree_sitter_cpp, 1}},
{"h", {"h", tree_sitter_cpp, 1}},
{"css", {"css", tree_sitter_css}},
{"fish", {"fish", tree_sitter_fish}},
{"go", {"go", tree_sitter_go}},
{"haskell", {"haskell", tree_sitter_haskell}},
{"html", {"html", tree_sitter_html}},
{"javascript", {"javascript", tree_sitter_javascript}},
{"json", {"json", tree_sitter_json}},
{"lua", {"lua", tree_sitter_lua}},
{"make", {"make", tree_sitter_make}},
{"python", {"python", tree_sitter_python}},
{"ruby", {"ruby", tree_sitter_ruby}},
};
static const std::unordered_map<uint8_t, LSP> kLsps = {
{1,
{"clangd",
{
"clangd",
"--background-index",
"--clang-tidy",
"--completion-style=detailed",
"--header-insertion=iwyu",
"--log=error",
nullptr,
}}},
};
static const std::unordered_map<std::string, std::string> kExtToLang = {
{"sh", "bash"}, {"bash", "bash"}, {"c", "c"}, {"cpp", "cpp"},
{"cxx", "cpp"}, {"cc", "cpp"}, {"hpp", "h"}, {"hh", "h"},
{"hxx", "h"}, {"h", "h"}, {"css", "css"}, {"fish", "fish"},
{"go", "go"}, {"hs", "haskell"}, {"html", "html"}, {"htm", "html"},
{"js", "javascript"}, {"json", "json"}, {"lua", "lua"}, {"mk", "make"},
{"makefile", "make"}, {"py", "python"}, {"rb", "ruby"},
};
static const std::unordered_map<std::string, std::string> kMimeToLang = {
{"text/x-c", "c"},
{"text/x-c++", "cpp"},
{"text/x-shellscript", "bash"},
{"application/json", "json"},
{"text/javascript", "javascript"},
{"text/html", "html"},
{"text/css", "css"},
{"text/x-python", "python"},
{"text/x-ruby", "ruby"},
{"text/x-go", "go"},
{"text/x-haskell", "haskell"},
{"text/x-lua", "lua"},
};
#endif

View File

@@ -7,10 +7,19 @@
#include "../libs/tree-sitter/lib/include/tree_sitter/api.h"
#include <algorithm>
#include <atomic>
#include <cctype>
#include <chrono>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <filesystem>
#include <fstream>
#include <functional>
#include <limits.h>
#include <magic.h>
#include <map>
#include <mutex>
#include <nlohmann/json.hpp>

View File

@@ -9,7 +9,7 @@
extern std::unordered_map<std::string, pcre2_code *> regex_cache;
TSQuery *load_query(const char *query_path, Editor *editor);
TSQuery *load_query(const char *query_path, TSSetBase *set);
void ts_collect_spans(Editor *editor);
void clear_regex_cache();

View File

@@ -24,6 +24,13 @@ const TSLanguage *tree_sitter_lua();
const TSLanguage *tree_sitter_make();
const TSLanguage *tree_sitter_python();
const TSLanguage *tree_sitter_ruby();
const TSLanguage *tree_sitter_rust();
// TO ADD
// sql
// wasm
// conf
// yaml, toml
// godot
}
#endif