@@ -43,14 +43,86 @@ 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 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
+
46
112
// / A transport class that performs the JSON-RPC communication with the LSP
47
113
// / client.
48
114
class JSONTransport {
49
115
public:
116
+ JSONTransport (std::unique_ptr<JSONTransportInput> in,
117
+ std::unique_ptr<JSONTransportOutput> out)
118
+ : in(std::move(in)), out(std::move(out)) {}
119
+
50
120
JSONTransport (std::FILE *in, raw_ostream &out,
51
121
JSONStreamStyle style = JSONStreamStyle::Standard,
52
122
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)) {}
54
126
55
127
// / The following methods are used to send a message to the LSP client.
56
128
void notify (StringRef method, llvm::json::Value params);
@@ -60,30 +132,15 @@ class JSONTransport {
60
132
// / Start executing the JSON-RPC transport.
61
133
llvm::Error run (MessageHandler &handler);
62
134
63
- private:
64
135
// / Dispatches the given incoming json message to the message handler.
65
136
bool handleMessage (llvm::json::Value msg, MessageHandler &handler);
66
- // / Writes the given message to the output stream.
67
- void sendMessage (llvm::json::Value msg);
68
137
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;
76
141
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;
87
144
};
88
145
89
146
// ===----------------------------------------------------------------------===//
0 commit comments