|
| 1 | +(ns integration.client |
| 2 | + (:require |
| 3 | + [clojure.core.async :as async] |
| 4 | + [clojure.string :as string] |
| 5 | + [lsp4clj.coercer :as coercer] |
| 6 | + [lsp4clj.io-chan :as lsp.io-chan] |
| 7 | + [lsp4clj.lsp.requests :as lsp.requests] |
| 8 | + [lsp4clj.lsp.responses :as lsp.responses] |
| 9 | + [lsp4clj.protocols.endpoint :as protocols.endpoint]) |
| 10 | + (:import |
| 11 | + [java.time LocalDateTime] |
| 12 | + [java.time.format DateTimeFormatter])) |
| 13 | + |
| 14 | +(def ^:private ESC \u001b) |
| 15 | + |
| 16 | +(def ^:private colors |
| 17 | + {:black (str ESC "[30m") |
| 18 | + :red-bg (str ESC "[41m") |
| 19 | + :red (str ESC "[31m") |
| 20 | + :green (str ESC "[32m") |
| 21 | + :yellow (str ESC "[33m") |
| 22 | + :blue (str ESC "[34m") |
| 23 | + :magenta (str ESC "[35m") |
| 24 | + :cyan (str ESC "[36m") |
| 25 | + :white (str ESC "[37m") |
| 26 | + :underline (str ESC "[4m") |
| 27 | + :reset (str ESC "[m")}) |
| 28 | + |
| 29 | +(defn ^:private colored [color string] |
| 30 | + (str (get colors color) string (:reset colors))) |
| 31 | + |
| 32 | +(def ^:private ld-formatter DateTimeFormatter/ISO_LOCAL_DATE_TIME) |
| 33 | +(defn ^:private local-datetime-str [] (.format ld-formatter (LocalDateTime/now))) |
| 34 | + |
| 35 | +(defprotocol IMockClient |
| 36 | + (mock-response [this method body])) |
| 37 | + |
| 38 | +(defn ^:private format-log |
| 39 | + [{:keys [client-id]} color msg params] |
| 40 | + (string/join " " |
| 41 | + [(local-datetime-str) |
| 42 | + (colored color (str "Client " client-id " " msg)) |
| 43 | + (colored :yellow params)])) |
| 44 | + |
| 45 | +(defn ^:private receive-message |
| 46 | + [client context message] |
| 47 | + (println message) |
| 48 | + (let [message-type (coercer/input-message-type message)] |
| 49 | + (try |
| 50 | + (let [response |
| 51 | + (case message-type |
| 52 | + (:parse-error :invalid-request) |
| 53 | + (protocols.endpoint/log client :red "Error reading message" message-type) |
| 54 | + :request |
| 55 | + (protocols.endpoint/receive-request client context message) |
| 56 | + (:response.result :response.error) |
| 57 | + (protocols.endpoint/receive-response client message) |
| 58 | + :notification |
| 59 | + (protocols.endpoint/receive-notification client context message))] |
| 60 | + ;; Ensure client only responds to requests |
| 61 | + (when (identical? :request message-type) |
| 62 | + response)) |
| 63 | + (catch Throwable e |
| 64 | + (protocols.endpoint/log client :red "Error receiving:" e) |
| 65 | + (throw e))))) |
| 66 | + |
| 67 | +(defonce client-id (atom 0)) |
| 68 | + |
| 69 | +(defrecord Client [client-id |
| 70 | + input output |
| 71 | + log-ch |
| 72 | + join |
| 73 | + request-id sent-requests |
| 74 | + received-requests received-notifications |
| 75 | + mock-responses] |
| 76 | + protocols.endpoint/IEndpoint |
| 77 | + (start [this context] |
| 78 | + (protocols.endpoint/log this :white "lifecycle:" "starting") |
| 79 | + (let [pipeline (async/pipeline-blocking |
| 80 | + 1 ;; no parallelism preserves server message order |
| 81 | + output |
| 82 | + ;; TODO: return error until initialize request is received? https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize |
| 83 | + ;; `keep` means we do not reply to responses and notifications |
| 84 | + (keep #(receive-message this context %)) |
| 85 | + input)] |
| 86 | + (async/go |
| 87 | + ;; wait for pipeline to close, indicating input closed |
| 88 | + (async/<! pipeline) |
| 89 | + (deliver join :done))) |
| 90 | + ;; invokers can deref the return of `start` to stay alive until server is |
| 91 | + ;; shut down |
| 92 | + join) |
| 93 | + (shutdown [this] |
| 94 | + (protocols.endpoint/log this :white "lifecycle:" "shutting down") |
| 95 | + ;; closing input will drain pipeline, then close output, then close |
| 96 | + ;; pipeline |
| 97 | + (async/close! input) |
| 98 | + (if (= :done (deref join 10e3 :timeout)) |
| 99 | + (protocols.endpoint/log this :white "lifecycle:" "shutdown") |
| 100 | + (protocols.endpoint/log this :red "lifecycle:" "shutdown timed out")) |
| 101 | + (async/close! log-ch)) |
| 102 | + (log [this msg params] |
| 103 | + (protocols.endpoint/log this :white msg params)) |
| 104 | + (log [this color msg params] |
| 105 | + (async/put! log-ch (format-log this color msg params))) |
| 106 | + (send-request [this method body] |
| 107 | + (let [req (lsp.requests/request (swap! request-id inc) method body) |
| 108 | + p (promise) |
| 109 | + start-ns (System/nanoTime)] |
| 110 | + (protocols.endpoint/log this :cyan "sending request:" req) |
| 111 | + ;; Important: record request before sending it, so it is sure to be |
| 112 | + ;; available during receive-response. |
| 113 | + (swap! sent-requests assoc (:id req) {:request p |
| 114 | + :start-ns start-ns}) |
| 115 | + (async/>!! output req) |
| 116 | + p)) |
| 117 | + (send-notification [this method body] |
| 118 | + (let [notif (lsp.requests/notification method body)] |
| 119 | + (protocols.endpoint/log this :blue "sending notification:" notif) |
| 120 | + (async/>!! output notif))) |
| 121 | + (receive-response [this {:keys [id] :as resp}] |
| 122 | + (if-let [{:keys [request start-ns]} (get @sent-requests id)] |
| 123 | + (let [ms (float (/ (- (System/nanoTime) start-ns) 1000000))] |
| 124 | + (protocols.endpoint/log this :green (format "received response (%.0fms):" ms) resp) |
| 125 | + (swap! sent-requests dissoc id) |
| 126 | + (deliver request (if (:error resp) |
| 127 | + resp |
| 128 | + (:result resp)))) |
| 129 | + (protocols.endpoint/log this :red "received response for unmatched request:" resp))) |
| 130 | + (receive-request [this _ {:keys [id method] :as req}] |
| 131 | + (protocols.endpoint/log this :magenta "received request:" req) |
| 132 | + (swap! received-requests conj req) |
| 133 | + (when-let [mock-resp (get @mock-responses (keyword method))] |
| 134 | + (let [resp (lsp.responses/response id mock-resp)] |
| 135 | + (protocols.endpoint/log this :magenta "sending mock response:" resp) |
| 136 | + resp))) |
| 137 | + (receive-notification [this _ notif] |
| 138 | + (protocols.endpoint/log this :blue "received notification:" notif) |
| 139 | + (swap! received-notifications conj notif)) |
| 140 | + IMockClient |
| 141 | + (mock-response [_this method body] |
| 142 | + (swap! mock-responses assoc method body))) |
| 143 | + |
| 144 | +(def start protocols.endpoint/start) |
| 145 | +(def shutdown protocols.endpoint/shutdown) |
| 146 | +(def send-notification protocols.endpoint/send-notification) |
| 147 | + |
| 148 | +(defn client [server-in server-out] |
| 149 | + (map->Client |
| 150 | + {:client-id (swap! client-id inc) |
| 151 | + :input (lsp.io-chan/input-stream->input-chan server-out {:keyword-function keyword}) |
| 152 | + :output (lsp.io-chan/output-stream->output-chan server-in) |
| 153 | + :log-ch (async/chan (async/sliding-buffer 20)) |
| 154 | + :join (promise) |
| 155 | + :request-id (atom 0) |
| 156 | + :sent-requests (atom {}) |
| 157 | + :received-requests (atom []) |
| 158 | + :received-notifications (atom []) |
| 159 | + :mock-responses (atom {})})) |
| 160 | + |
| 161 | +(defn ^:private keyname [key] (str (namespace key) "/" (name key))) |
| 162 | + |
| 163 | +(defn ^:private await-first-and-remove! [client pred coll-type] |
| 164 | + (let [coll* (coll-type client)] |
| 165 | + (loop [tries 0] |
| 166 | + (if (< tries 20) |
| 167 | + (if-let [elem (first (filter pred @coll*))] |
| 168 | + (do |
| 169 | + (swap! coll* #(->> % (remove #{elem}) vec)) |
| 170 | + elem) |
| 171 | + (do |
| 172 | + (Thread/sleep 500) |
| 173 | + (recur (inc tries)))) |
| 174 | + (do |
| 175 | + (protocols.endpoint/log client :red "timeout waiting:" coll-type) |
| 176 | + (throw (ex-info "timeout waiting for client to receive req/notif" {:coll-type coll-type}))))))) |
| 177 | + |
| 178 | +(defn await-server-notification [client method] |
| 179 | + (let [method-str (keyname method) |
| 180 | + notification (await-first-and-remove! client |
| 181 | + #(= method-str (:method %)) |
| 182 | + :received-notifications)] |
| 183 | + (:params notification))) |
| 184 | + |
| 185 | +(defn await-server-request [client method] |
| 186 | + (let [method-str (keyname method) |
| 187 | + msg (await-first-and-remove! client |
| 188 | + #(= method-str (:method %)) |
| 189 | + :received-requests)] |
| 190 | + (:params msg))) |
| 191 | + |
| 192 | +(defn request-and-await-server-response! [client method body] |
| 193 | + (let [timeout-ms 180000 |
| 194 | + resp (deref (protocols.endpoint/send-request client method body) |
| 195 | + timeout-ms |
| 196 | + ::timeout)] |
| 197 | + (if (= ::timeout resp) |
| 198 | + (do |
| 199 | + (protocols.endpoint/log client :red "timeout waiting for server response to client request:" |
| 200 | + {:method method :timeout-ms timeout-ms}) |
| 201 | + (throw (ex-info "timeout waiting for server response to client request" {:method method :body body}))) |
| 202 | + resp))) |
0 commit comments