Bash++
Bash++ compiler internal documentation
BashppServer.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <iostream>
10#include <thread>
11#include <mutex>
12#include <chrono>
13#include <atomic>
14#include <memory>
15#include <optional>
16#include <unordered_map>
17#include <fstream>
18#include <nlohmann/json.hpp>
19#include <unistd.h>
20
21#include <frozen/string.h>
22#include <frozen/unordered_map.h>
23
24#include "ThreadPool.h"
25#include "ProgramPool.h"
26
27#include "static/Message.h"
28#include "generated/ErrorCodes.h"
29
30#include "generated/CompletionList.h"
31#include "generated/CompletionParams.h"
32
34#include <include/BashVersion.h>
35
36namespace bpp {
37
38using json = nlohmann::json;
39
40template<typename T>
41void printValue(std::ostream& os, const T& value) {
42 os << value;
43}
44
45template<typename... Ts>
46void printValue(std::ostream& os, const std::variant<Ts...>& v) {
47 std::visit([&os](auto&& arg) { os << arg; }, v);
48}
49
50
60 private:
61 pid_t pid = getpid();
62 // Resources
63 std::shared_ptr<std::istream> input_stream;
64 std::shared_ptr<std::ostream> output_stream;
65 std::optional<std::string> socket_path;
66 ThreadPool thread_pool = ThreadPool(std::thread::hardware_concurrency());
67 ProgramPool program_pool = ProgramPool(10); // Maximum 10 programs in the pool
68 std::ofstream log_file;
69
70 // Debouncing didChange notifications
72 std::atomic<uint64_t> change_generation{0};
73 std::atomic<uint64_t> average_reparse_time_in_microseconds{50'000}; // 50ms initial guess
74 std::atomic<uint32_t> debounce_time_in_milliseconds{100}; // Start with 100ms
75 };
76
77 // Map: program main URI -> DebounceState
78 // Used for adaptive debouncing
80 private:
81 std::unordered_map<std::string, std::shared_ptr<DebounceState>> states;
83 std::mutex map_mutex;
84
85 void cleanup() {
86 for (auto it = states.begin(); it != states.end(); ) {
87 if (!pool->has_program(it->first)) {
88 it = states.erase(it);
89 } else {
90 it++;
91 }
92 }
93 }
94 public:
95 DebounceStateMap() = delete;
97 std::shared_ptr<DebounceState> get(const std::string& uri) {
98 std::lock_guard<std::mutex> lock(map_mutex);
99 cleanup(); // Clean up stale entries on every access
100 auto it = states.find(uri);
101 if (it != states.end()) return it->second;
102 auto new_state = std::make_shared<DebounceState>();
103 states[uri] = new_state;
104 return new_state;
105 }
106 };
108 std::atomic<bool> processing_didChange{false};
109
110 static std::string readHeaderLine(std::streambuf* buffer);
111
112 void _sendMessage(const std::string& message);
113
114 static std::mutex output_mutex; // Mutex for thread-safe output
115 static std::mutex log_mutex; // Mutex for thread-safe logging
116
117 static const GenericResponseMessage invalidRequestHandler(const GenericRequestMessage& request);
118 static void invalidNotificationHandler(const GenericNotificationMessage& request);
119
120 void processRequest(const GenericRequestMessage& request);
121 void processNotification(const GenericNotificationMessage& notification);
122
124 public:
125 BashppServer();
127
128 void mainLoop();
129
130 void setInputStream(std::shared_ptr<std::istream> stream);
131 void setOutputStream(std::shared_ptr<std::ostream> stream);
132 void setSocketPath(const std::string& path);
133 void setLogFile(const std::string& path);
134 void setTargetBashVersion(const BashVersion& version);
135
136 GenericResponseMessage shutdown(const GenericRequestMessage& request);
137 void cleanup();
138
139 void processMessage(const std::string& message);
140
141 // Request-Response handlers
142 GenericResponseMessage handleInitialize(const GenericRequestMessage& request);
143 GenericResponseMessage handleDefinition(const GenericRequestMessage& request);
144 GenericResponseMessage handleHover(const GenericRequestMessage& request);
145 GenericResponseMessage handleDocumentSymbol(const GenericRequestMessage& request);
146 GenericResponseMessage handleRename(const GenericRequestMessage& request);
147 GenericResponseMessage handleReferences(const GenericRequestMessage& request);
148 GenericResponseMessage handleCompletion(const GenericRequestMessage& request);
149
150 CompletionList handleATCompletion(const CompletionParams& params);
151 CompletionList handleDOTCompletion(const CompletionParams& params);
152
153 // Notification handlers
154 void handleDidOpen(const GenericNotificationMessage& request);
155 void handleDidChange(const GenericNotificationMessage& request);
156 void handleDidChangeWatchedFiles(const GenericNotificationMessage& request);
157 void handleDidClose(const GenericNotificationMessage& request);
158
159 void sendResponse(const GenericResponseMessage& response);
160 void sendNotification(const GenericNotificationMessage& notification);
161
162 void publishDiagnostics(std::shared_ptr<bpp::bpp_program> program);
163
164 void add_include_path(const std::string& path);
165 void set_suppress_warnings(bool suppress);
166
167 template <typename... Args>
168 void log(Args&&... args) {
169 if (!log_file.is_open()) {
170 return; // Not logging
171 }
172 std::lock_guard<std::mutex> lock(log_mutex);
173 auto now = std::chrono::system_clock::now();
174 auto now_time_t = std::chrono::system_clock::to_time_t(now);
175 std::tm tm_buf{};
176 localtime_r(&now_time_t, &tm_buf);
177 log_file << "[" << std::put_time(&tm_buf, "%Y-%m-%d %H:%M:%S") << "] ";
178 log_file << "[" << std::to_string(pid) << "] ";
179 ((printValue(log_file, std::forward<Args>(args))), ...);
180 log_file << std::endl;
181 }
182
183 private:
184 using RequestHandler = GenericResponseMessage (BashppServer::*)(const GenericRequestMessage&);
185 using NotificationHandler = void (BashppServer::*)(const GenericNotificationMessage&);
186
191 static constexpr frozen::unordered_map<frozen::string, RequestHandler, 8> request_handlers = {
192 {"initialize", &BashppServer::handleInitialize},
193 {"textDocument/definition", &BashppServer::handleDefinition},
194 {"textDocument/completion", &BashppServer::handleCompletion},
195 {"textDocument/hover", &BashppServer::handleHover},
196 {"textDocument/documentSymbol", &BashppServer::handleDocumentSymbol},
197 {"textDocument/rename", &BashppServer::handleRename},
198 {"textDocument/references", &BashppServer::handleReferences},
199 {"shutdown", &BashppServer::shutdown}
200 };
201
206 static constexpr frozen::unordered_map<frozen::string, NotificationHandler, 4> notification_handlers = {
207 {"textDocument/didOpen", &BashppServer::handleDidOpen},
208 {"textDocument/didChange", &BashppServer::handleDidChange},
209 {"workspace/didChangeWatchedFiles", &BashppServer::handleDidChangeWatchedFiles},
210 {"textDocument/didClose", &BashppServer::handleDidClose}
211 };
212};
213
214} // namespace bpp
Manages a pool of bpp_program objects for efficient reuse and access.
Definition ProgramPool.h:38
bool has_program(const std::string &file_path)
Check if a program for the given file path exists in the pool.
Definition ProgramPool.cpp:236
A thread pool implementation that manages a pool of worker threads to execute tasks concurrently.
Definition ThreadPool.h:24
Definition BashppServer.h:79
ProgramPool * pool
Definition BashppServer.h:82
void cleanup()
Definition BashppServer.h:85
std::shared_ptr< DebounceState > get(const std::string &uri)
Definition BashppServer.h:97
DebounceStateMap(ProgramPool *program_pool)
Definition BashppServer.h:96
std::unordered_map< std::string, std::shared_ptr< DebounceState > > states
Definition BashppServer.h:81
std::mutex map_mutex
Definition BashppServer.h:83
The main server class for handling LSP requests and notifications.
Definition BashppServer.h:59
void publishDiagnostics(std::shared_ptr< bpp::bpp_program > program)
Definition BashppServer.cpp:329
GenericResponseMessage handleHover(const GenericRequestMessage &request)
Definition handleHover.cpp:11
void(BashppServer::*)(const GenericNotificationMessage &) NotificationHandler
Definition BashppServer.h:185
void processNotification(const GenericNotificationMessage &notification)
Definition BashppServer.cpp:275
static std::string readHeaderLine(std::streambuf *buffer)
Definition BashppServer.cpp:160
void mainLoop()
Definition BashppServer.cpp:175
static std::mutex log_mutex
Definition BashppServer.h:115
DebounceStateMap debounce_states
Definition BashppServer.h:107
GenericResponseMessage handleRename(const GenericRequestMessage &request)
Definition handleRename.cpp:11
pid_t pid
Definition BashppServer.h:61
void sendNotification(const GenericNotificationMessage &notification)
Definition BashppServer.cpp:245
void setSocketPath(const std::string &path)
Definition BashppServer.cpp:130
void processRequest(const GenericRequestMessage &request)
Definition BashppServer.cpp:251
GenericResponseMessage handleDocumentSymbol(const GenericRequestMessage &request)
Definition handleDocumentSymbol.cpp:10
static constexpr frozen::unordered_map< frozen::string, RequestHandler, 8 > request_handlers
Maps request types to the functions that handle them.
Definition BashppServer.h:191
ProgramPool program_pool
Definition BashppServer.h:67
void handleDidClose(const GenericNotificationMessage &request)
Definition handledDidClose.cpp:10
GenericResponseMessage(BashppServer::*)(const GenericRequestMessage &) RequestHandler
Definition BashppServer.h:184
std::optional< std::string > socket_path
Definition BashppServer.h:65
void sendResponse(const GenericResponseMessage &response)
Definition BashppServer.cpp:239
std::atomic< bool > processing_didChange
Definition BashppServer.h:108
ThreadPool thread_pool
Definition BashppServer.h:66
static constexpr frozen::unordered_map< frozen::string, NotificationHandler, 4 > notification_handlers
Maps notification types to the functions that handle them.
Definition BashppServer.h:206
void setOutputStream(std::shared_ptr< std::ostream > stream)
Definition BashppServer.cpp:126
GenericResponseMessage shutdown(const GenericRequestMessage &request)
Definition BashppServer.cpp:321
GenericResponseMessage handleReferences(const GenericRequestMessage &request)
Definition handleReference.cpp:12
std::shared_ptr< std::istream > input_stream
Definition BashppServer.h:63
static void invalidNotificationHandler(const GenericNotificationMessage &request)
Definition BashppServer.cpp:229
std::shared_ptr< std::ostream > output_stream
Definition BashppServer.h:64
void handleDidChange(const GenericNotificationMessage &request)
Definition handleDidChange.cpp:10
void processMessage(const std::string &message)
Definition BashppServer.cpp:291
void handleDidOpen(const GenericNotificationMessage &request)
Definition handleDidOpen.cpp:10
void log(Args &&... args)
Definition BashppServer.h:168
BashppServer()
Definition BashppServer.cpp:16
GenericResponseMessage handleInitialize(const GenericRequestMessage &request)
Definition handleInitialize.cpp:12
void setInputStream(std::shared_ptr< std::istream > stream)
Definition BashppServer.cpp:122
std::ofstream log_file
Definition BashppServer.h:68
GenericResponseMessage handleDefinition(const GenericRequestMessage &request)
Definition handleDefinition.cpp:11
CompletionList handleDOTCompletion(const CompletionParams &params)
Definition handleCompletion.cpp:125
void setLogFile(const std::string &path)
Definition BashppServer.cpp:134
CompletionList handleATCompletion(const CompletionParams &params)
Definition handleCompletion.cpp:76
void handleDidChangeWatchedFiles(const GenericNotificationMessage &request)
Definition handleDidChangeWatchedFiles.cpp:10
void add_include_path(const std::string &path)
Definition BashppServer.cpp:375
static const GenericResponseMessage invalidRequestHandler(const GenericRequestMessage &request)
Definition BashppServer.cpp:225
void cleanup()
Definition BashppServer.cpp:145
CompletionList default_completion_list
Definition BashppServer.h:123
static std::mutex output_mutex
Definition BashppServer.h:114
void setTargetBashVersion(const BashVersion &version)
Definition BashppServer.cpp:141
void _sendMessage(const std::string &message)
Definition BashppServer.cpp:233
~BashppServer()
Definition BashppServer.cpp:120
GenericResponseMessage handleCompletion(const GenericRequestMessage &request)
Definition handleCompletion.cpp:11
void set_suppress_warnings(bool suppress)
Definition BashppServer.cpp:379
Definition bash_case.cpp:9
void printValue(std::ostream &os, const T &value)
Definition BashppServer.h:41
nlohmann::json json
Definition BashppServer.h:38
Represents a Bash version to target for code generation.
Definition BashVersion.h:21
Definition BashppServer.h:71
std::atomic< uint32_t > debounce_time_in_milliseconds
Definition BashppServer.h:74
std::atomic< uint64_t > change_generation
Definition BashppServer.h:72
std::atomic< uint64_t > average_reparse_time_in_microseconds
Definition BashppServer.h:73