@@ -43,14 +43,86 @@ enum JSONStreamStyle {
4343 Delimited
4444};
4545
46+ // / An abstract class used by the JSONTransport to read JSON message.
47+ class JSONTransportInput {
48+ public:
49+ explicit JSONTransportInput (JSONStreamStyle style = JSONStreamStyle::Standard)
50+ : style(style) {}
51+ virtual ~JSONTransportInput () = default ;
52+
53+ virtual bool getError () const = 0;
54+ virtual bool isEndOfInput () const = 0;
55+
56+ // / Read in a message from the input stream.
57+ virtual LogicalResult readMessage (std::string &json) {
58+ return style == JSONStreamStyle::Delimited ? readDelimitedMessage (json)
59+ : readStandardMessage (json);
60+ }
61+ virtual LogicalResult readDelimitedMessage (std::string &json) = 0;
62+ virtual LogicalResult readStandardMessage (std::string &json) = 0;
63+
64+ private:
65+ // / The JSON stream style to use.
66+ JSONStreamStyle style;
67+ };
68+
69+ // / An abstract class used by the JSONTransport to write JSON messages.
70+ class JSONTransportOutput {
71+ public:
72+ explicit JSONTransportOutput () = default;
73+ virtual ~JSONTransportOutput () = default ;
74+
75+ virtual void sendMessage (const llvm::json::Value &msg) = 0;
76+ };
77+
78+ // / Concrete implementation of the JSONTransportInput that reads from a file.
79+ class JSONTransportInputOverFile : public JSONTransportInput {
80+ public:
81+ explicit JSONTransportInputOverFile (
82+ std::FILE *in, JSONStreamStyle style = JSONStreamStyle::Standard)
83+ : JSONTransportInput(style), in(in) {}
84+
85+ bool getError () const final { return ferror (in); }
86+ bool isEndOfInput () const final { return feof (in); }
87+
88+ LogicalResult readDelimitedMessage (std::string &json) final ;
89+ LogicalResult readStandardMessage (std::string &json) final ;
90+
91+ private:
92+ std::FILE *in;
93+ };
94+
95+ // / Concrete implementation of the JSONTransportOutput that writes to a stream.
96+ class JSONTransportOutputOverStream : public JSONTransportOutput {
97+ public:
98+ explicit JSONTransportOutputOverStream (raw_ostream &out,
99+ bool prettyOutput = false )
100+ : JSONTransportOutput(), out(out), prettyOutput(prettyOutput) {}
101+
102+ // / Writes the given message to the output stream.
103+ void sendMessage (const llvm::json::Value &msg) final ;
104+
105+ private:
106+ SmallVector<char , 0 > outputBuffer;
107+ raw_ostream &out;
108+ // / If the output JSON should be formatted for easier readability.
109+ bool prettyOutput;
110+ };
111+
46112// / A transport class that performs the JSON-RPC communication with the LSP
47113// / client.
48114class JSONTransport {
49115public:
116+ JSONTransport (std::unique_ptr<JSONTransportInput> in,
117+ std::unique_ptr<JSONTransportOutput> out)
118+ : in(std::move(in)), out(std::move(out)) {}
119+
50120 JSONTransport (std::FILE *in, raw_ostream &out,
51121 JSONStreamStyle style = JSONStreamStyle::Standard,
52122 bool prettyOutput = false )
53- : in(in), out(out), style(style), prettyOutput(prettyOutput) {}
123+ : in(std::make_unique<JSONTransportInputOverFile>(in, style)),
124+ out (std::make_unique<JSONTransportOutputOverStream>(out,
125+ prettyOutput)) {}
54126
55127 // / The following methods are used to send a message to the LSP client.
56128 void notify (StringRef method, llvm::json::Value params);
@@ -60,30 +132,15 @@ class JSONTransport {
60132 // / Start executing the JSON-RPC transport.
61133 llvm::Error run (MessageHandler &handler);
62134
63- private:
64135 // / Dispatches the given incoming json message to the message handler.
65136 bool handleMessage (llvm::json::Value msg, MessageHandler &handler);
66- // / Writes the given message to the output stream.
67- void sendMessage (llvm::json::Value msg);
68137
69- // / Read in a message from the input stream.
70- LogicalResult readMessage (std::string &json) {
71- return style == JSONStreamStyle::Delimited ? readDelimitedMessage (json)
72- : readStandardMessage (json);
73- }
74- LogicalResult readDelimitedMessage (std::string &json);
75- LogicalResult readStandardMessage (std::string &json);
138+ private:
139+ // / The input to read a message from.
140+ std::unique_ptr<JSONTransportInput> in;
76141
77- // / An output buffer used when building output messages.
78- SmallVector<char , 0 > outputBuffer;
79- // / The input file stream.
80- std::FILE *in;
81- // / The output file stream.
82- raw_ostream &out;
83- // / The JSON stream style to use.
84- JSONStreamStyle style;
85- // / If the output JSON should be formatted for easier readability.
86- bool prettyOutput;
142+ // / The output to send a messages to.
143+ std::unique_ptr<JSONTransportOutput> out;
87144};
88145
89146// ===----------------------------------------------------------------------===//
0 commit comments