@@ -43,14 +43,59 @@ enum JSONStreamStyle {
43
43
Delimited
44
44
};
45
45
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
+
46
86
// / A transport class that performs the JSON-RPC communication with the LSP
47
87
// / client.
48
88
class JSONTransport {
49
89
public:
90
+ JSONTransport (std::unique_ptr<JSONTransportInput> in, raw_ostream &out,
91
+ bool prettyOutput = false )
92
+ : in(std::move(in)), out(out), prettyOutput(prettyOutput) {}
93
+
50
94
JSONTransport (std::FILE *in, raw_ostream &out,
51
95
JSONStreamStyle style = JSONStreamStyle::Standard,
52
96
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) {}
54
99
55
100
// / The following methods are used to send a message to the LSP client.
56
101
void notify (StringRef method, llvm::json::Value params);
@@ -66,22 +111,12 @@ class JSONTransport {
66
111
// / Writes the given message to the output stream.
67
112
void sendMessage (llvm::json::Value msg);
68
113
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;
78
117
SmallVector<char , 0 > outputBuffer;
79
- // / The input file stream.
80
- std::FILE *in;
81
118
// / The output file stream.
82
119
raw_ostream &out;
83
- // / The JSON stream style to use.
84
- JSONStreamStyle style;
85
120
// / If the output JSON should be formatted for easier readability.
86
121
bool prettyOutput;
87
122
};
0 commit comments