Bash++
Bash++ compiler internal documentation
BashppServer.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <iostream>
9#include <thread>
10#include <mutex>
11#include <chrono>
12#include <atomic>
13#include <memory>
14#include <optional>
15#include <unordered_map>
16#include <fstream>
17#include <nlohmann/json.hpp>
18#include <unistd.h>
19
20#include <frozen/string.h>
21#include <frozen/unordered_map.h>
22
23#include "ThreadPool.h"
24#include "ProgramPool.h"
25
26#include "static/Message.h"
27#include "generated/ErrorCodes.h"
28
29#include "generated/CompletionList.h"
30#include "generated/CompletionParams.h"
31
32#include "../bpp_include/bpp_codegen.h"
33
34namespace bpp {
35
36using json = nlohmann::json;
37
38template<typename T>
39void printValue(std::ostream& os, const T& value) {
40 os << value;
41}
42
43template<typename... Ts>
44void printValue(std::ostream& os, const std::variant<Ts...>& v) {
45 std::visit([&os](auto&& arg) { os << arg; }, v);
46}
47
48
58 private:
59 pid_t pid = getpid();
60 // Resources
61 std::shared_ptr<std::istream> input_stream;
62 std::shared_ptr<std::ostream> output_stream;
63 std::optional<std::string> socket_path;
64 ThreadPool thread_pool = ThreadPool(std::thread::hardware_concurrency());
65 ProgramPool program_pool = ProgramPool(10); // Maximum 10 programs in the pool
66 std::ofstream log_file;
67 std::unordered_map<std::string, std::string> unsaved_changes; // Maps file paths to their unsaved contents
69 std::atomic<bool> stored_changes_content_updating = false;
70
71 // Debouncing didChange notifications
72 std::unordered_map<std::string, std::shared_ptr<std::atomic<uint64_t>>> debounce_timestamps;
73 std::mutex debounce_mutex;
74
75 void _sendMessage(const std::string& message);
76
77 static std::mutex output_mutex; // Mutex for thread-safe output
78 static std::mutex log_mutex; // Mutex for thread-safe logging
79
80 using RequestHandler = GenericResponseMessage (BashppServer::*)(const GenericRequestMessage&);
81 using NotificationHandler = void (BashppServer::*)(const GenericNotificationMessage&);
86 static const frozen::unordered_map<frozen::string, RequestHandler, 8> request_handlers;
87
92 static const frozen::unordered_map<frozen::string, NotificationHandler, 4> notification_handlers;
93
94 static const GenericResponseMessage invalidRequestHandler(const GenericRequestMessage& request);
95 static void invalidNotificationHandler(const GenericNotificationMessage& request);
96
97 void processRequest(const GenericRequestMessage& request);
98 void processNotification(const GenericNotificationMessage& notification);
99
101 public:
102 BashppServer();
104
105 void mainLoop();
106
107 void setInputStream(std::shared_ptr<std::istream> stream);
108 void setOutputStream(std::shared_ptr<std::ostream> stream);
109 void setSocketPath(const std::string& path);
110 void setLogFile(const std::string& path);
111
112 GenericResponseMessage shutdown(const GenericRequestMessage& request);
113 void cleanup();
114
115 void processMessage(const std::string& message);
116
117 // Request-Response handlers
118 GenericResponseMessage handleInitialize(const GenericRequestMessage& request);
119 GenericResponseMessage handleDefinition(const GenericRequestMessage& request);
120 GenericResponseMessage handleHover(const GenericRequestMessage& request);
121 GenericResponseMessage handleDocumentSymbol(const GenericRequestMessage& request);
122 GenericResponseMessage handleRename(const GenericRequestMessage& request);
123 GenericResponseMessage handleReferences(const GenericRequestMessage& request);
124 GenericResponseMessage handleCompletion(const GenericRequestMessage& request);
125
126 CompletionList handleATCompletion(const CompletionParams& params);
127 CompletionList handleDOTCompletion(const CompletionParams& params);
128
129 // Notification handlers
130 void handleDidOpen(const GenericNotificationMessage& request);
131 void handleDidChange(const GenericNotificationMessage& request);
132 void handleDidChangeWatchedFiles(const GenericNotificationMessage& request);
133 void handleDidClose(const GenericNotificationMessage& request);
134
135 void sendResponse(const GenericResponseMessage& response);
136 void sendNotification(const GenericNotificationMessage& notification);
137
138 void publishDiagnostics(std::shared_ptr<bpp::bpp_program> program);
139
140 void add_include_path(const std::string& path);
141 void set_suppress_warnings(bool suppress);
142
143 template <typename... Args>
144 void log(Args&&... args) {
145 if (!log_file.is_open()) {
146 return; // Not logging
147 }
148 std::lock_guard<std::mutex> lock(log_mutex);
149 log_file << "[" << std::to_string(pid) << "] ";
150 ((printValue(log_file, std::forward<Args>(args))), ...);
151 log_file << std::endl;
152 }
153};
154
155} // namespace bpp
Manages a pool of bpp_program objects for efficient reuse and access.
Definition ProgramPool.h:36
A thread pool implementation that manages a pool of worker threads to execute tasks concurrently.
Definition ThreadPool.h:23
The main server class for handling LSP requests and notifications.
Definition BashppServer.h:57
void publishDiagnostics(std::shared_ptr< bpp::bpp_program > program)
Definition BashppServer.cpp:327
GenericResponseMessage handleHover(const GenericRequestMessage &request)
Definition handleHover.cpp:10
static const frozen::unordered_map< frozen::string, NotificationHandler, 4 > notification_handlers
Maps notification types to the functions that handle them.
Definition BashppServer.h:26
void(BashppServer::*)(const GenericNotificationMessage &) NotificationHandler
Definition BashppServer.h:81
void processNotification(const GenericNotificationMessage &notification)
Definition BashppServer.cpp:273
void mainLoop()
Definition BashppServer.cpp:173
static std::mutex log_mutex
Definition BashppServer.h:78
GenericResponseMessage handleRename(const GenericRequestMessage &request)
Definition handleRename.cpp:10
pid_t pid
Definition BashppServer.h:59
void sendNotification(const GenericNotificationMessage &notification)
Definition BashppServer.cpp:243
void setSocketPath(const std::string &path)
Definition BashppServer.cpp:147
void processRequest(const GenericRequestMessage &request)
Definition BashppServer.cpp:249
GenericResponseMessage handleDocumentSymbol(const GenericRequestMessage &request)
Definition handleDocumentSymbol.cpp:9
ProgramPool program_pool
Definition BashppServer.h:65
std::unordered_map< std::string, std::string > unsaved_changes
Definition BashppServer.h:67
void handleDidClose(const GenericNotificationMessage &request)
Definition handledDidClose.cpp:9
GenericResponseMessage(BashppServer::*)(const GenericRequestMessage &) RequestHandler
Definition BashppServer.h:80
std::optional< std::string > socket_path
Definition BashppServer.h:63
void sendResponse(const GenericResponseMessage &response)
Definition BashppServer.cpp:237
ThreadPool thread_pool
Definition BashppServer.h:64
std::mutex debounce_mutex
Definition BashppServer.h:73
void setOutputStream(std::shared_ptr< std::ostream > stream)
Definition BashppServer.cpp:143
GenericResponseMessage shutdown(const GenericRequestMessage &request)
Definition BashppServer.cpp:319
GenericResponseMessage handleReferences(const GenericRequestMessage &request)
Definition handleReference.cpp:11
std::shared_ptr< std::istream > input_stream
Definition BashppServer.h:61
std::mutex unsaved_changes_mutex
Definition BashppServer.h:68
static void invalidNotificationHandler(const GenericNotificationMessage &request)
Definition BashppServer.cpp:227
std::shared_ptr< std::ostream > output_stream
Definition BashppServer.h:62
void handleDidChange(const GenericNotificationMessage &request)
Definition handleDidChange.cpp:9
void processMessage(const std::string &message)
Definition BashppServer.cpp:289
std::atomic< bool > stored_changes_content_updating
Definition BashppServer.h:69
void handleDidOpen(const GenericNotificationMessage &request)
Definition handleDidOpen.cpp:9
void log(Args &&... args)
Definition BashppServer.h:144
BashppServer()
Definition BashppServer.cpp:33
GenericResponseMessage handleInitialize(const GenericRequestMessage &request)
Definition handleInitialize.cpp:11
void setInputStream(std::shared_ptr< std::istream > stream)
Definition BashppServer.cpp:139
std::ofstream log_file
Definition BashppServer.h:66
GenericResponseMessage handleDefinition(const GenericRequestMessage &request)
Definition handleDefinition.cpp:10
CompletionList handleDOTCompletion(const CompletionParams &params)
Definition handleCompletion.cpp:115
void setLogFile(const std::string &path)
Definition BashppServer.cpp:151
CompletionList handleATCompletion(const CompletionParams &params)
Definition handleCompletion.cpp:66
void handleDidChangeWatchedFiles(const GenericNotificationMessage &request)
Definition handleDidChangeWatchedFiles.cpp:9
std::unordered_map< std::string, std::shared_ptr< std::atomic< uint64_t > > > debounce_timestamps
Definition BashppServer.h:72
void add_include_path(const std::string &path)
Definition BashppServer.cpp:373
static const GenericResponseMessage invalidRequestHandler(const GenericRequestMessage &request)
Definition BashppServer.cpp:223
void cleanup()
Definition BashppServer.cpp:158
CompletionList default_completion_list
Definition BashppServer.h:100
static std::mutex output_mutex
Definition BashppServer.h:77
void _sendMessage(const std::string &message)
Definition BashppServer.cpp:231
~BashppServer()
Definition BashppServer.cpp:137
GenericResponseMessage handleCompletion(const GenericRequestMessage &request)
Definition handleCompletion.cpp:10
static const frozen::unordered_map< frozen::string, RequestHandler, 8 > request_handlers
Maps request types to the functions that handle them.
Definition BashppServer.h:15
void set_suppress_warnings(bool suppress)
Definition BashppServer.cpp:377
Definition bash_case.cpp:8
void printValue(std::ostream &os, const T &value)
Definition BashppServer.h:39
nlohmann::json json
Definition BashppServer.h:36