@@ -43,14 +43,59 @@ 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 hasError () const = 0;
54+ virtual bool isEndOfInput () const = 0;
55+
56+ // / Read in a message from the input stream.
57+ 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+ // / Concrete implementation of the JSONTransportInput that reads from a file.
70+ class JSONTransportInputOverFile : public JSONTransportInput {
71+ public:
72+ explicit JSONTransportInputOverFile (
73+ std::FILE *in, JSONStreamStyle style = JSONStreamStyle::Standard)
74+ : JSONTransportInput(style), in(in) {}
75+
76+ bool hasError () const final { return ferror (in); }
77+ bool isEndOfInput () const final { return feof (in); }
78+
79+ LogicalResult readDelimitedMessage (std::string &json) final ;
80+ LogicalResult readStandardMessage (std::string &json) final ;
81+
82+ private:
83+ std::FILE *in;
84+ };
85+
4686// / A transport class that performs the JSON-RPC communication with the LSP
4787// / client.
4888class JSONTransport {
4989public:
90+ JSONTransport (std::unique_ptr<JSONTransportInput> in, raw_ostream &out,
91+ bool prettyOutput = false )
92+ : in(std::move(in)), out(out), prettyOutput(prettyOutput) {}
93+
5094 JSONTransport (std::FILE *in, raw_ostream &out,
5195 JSONStreamStyle style = JSONStreamStyle::Standard,
5296 bool prettyOutput = false )
53- : in(in), out(out), style(style), prettyOutput(prettyOutput) {}
97+ : in(std::make_unique<JSONTransportInputOverFile>(in, style)), out(out),
98+ prettyOutput (prettyOutput) {}
5499
55100 // / The following methods are used to send a message to the LSP client.
56101 void notify (StringRef method, llvm::json::Value params);
@@ -66,22 +111,12 @@ class JSONTransport {
66111 // / Writes the given message to the output stream.
67112 void sendMessage (llvm::json::Value msg);
68113
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);
76-
77- // / An output buffer used when building output messages.
114+ private:
115+ // / The input to read a message from.
116+ std::unique_ptr<JSONTransportInput> in;
78117 SmallVector<char , 0 > outputBuffer;
79- // / The input file stream.
80- std::FILE *in;
81118 // / The output file stream.
82119 raw_ostream &out;
83- // / The JSON stream style to use.
84- JSONStreamStyle style;
85120 // / If the output JSON should be formatted for easier readability.
86121 bool prettyOutput;
87122};
0 commit comments