|
30 | 30 | import ts.client.completions.ITypeScriptCompletionCollector;
|
31 | 31 | import ts.client.completions.ITypeScriptCompletionEntryDetailsCollector;
|
32 | 32 | import ts.client.definition.ITypeScriptDefinitionCollector;
|
| 33 | +import ts.client.format.ITypeScriptFormatCollector; |
33 | 34 | import ts.client.geterr.ITypeScriptGeterrCollector;
|
34 | 35 | import ts.client.protocol.ChangeRequest;
|
35 | 36 | import ts.client.protocol.CloseRequest;
|
36 | 37 | import ts.client.protocol.CompletionDetailsRequest;
|
37 | 38 | import ts.client.protocol.CompletionsRequest;
|
38 | 39 | import ts.client.protocol.DefinitionRequest;
|
| 40 | +import ts.client.protocol.FormatRequest; |
39 | 41 | import ts.client.protocol.GeterrRequest;
|
40 | 42 | import ts.client.protocol.OpenRequest;
|
41 | 43 | import ts.client.protocol.QuickInfoRequest;
|
@@ -316,6 +318,29 @@ private void collect(JsonObject response, ITypeScriptGeterrCollector collector)
|
316 | 318 | }
|
317 | 319 | }
|
318 | 320 |
|
| 321 | + @Override |
| 322 | + public void format(String fileName, int line, int offset, int endLine, int endOffset, |
| 323 | + ITypeScriptFormatCollector collector) throws TypeScriptException { |
| 324 | + Request request = new FormatRequest(fileName, line, offset, endLine, endOffset); |
| 325 | + JsonObject response = execute(request, true, null).asObject(); |
| 326 | + collectFormat(response.get("body").asArray(), collector); |
| 327 | + } |
| 328 | + |
| 329 | + private void collectFormat(JsonArray body, ITypeScriptFormatCollector collector) throws TypeScriptException { |
| 330 | + JsonObject item = null; |
| 331 | + String newText = null; |
| 332 | + JsonObject start = null; |
| 333 | + JsonObject end = null; |
| 334 | + for (JsonValue b : body) { |
| 335 | + item = b.asObject(); |
| 336 | + start = item.get("start").asObject(); |
| 337 | + end = item.get("end").asObject(); |
| 338 | + newText = item.getString("newText", null); |
| 339 | + collector.format(start.getInt("line", -1), start.getInt("offset", -1), end.getInt("line", -1), |
| 340 | + end.getInt("offset", -1), newText); |
| 341 | + } |
| 342 | + } |
| 343 | + |
319 | 344 | /**
|
320 | 345 | * Write the buffer of editor content to a temporary file and have the
|
321 | 346 | * server reload it
|
@@ -511,7 +536,8 @@ private JsonValue execute(Request request, boolean expectsResult, CancellationTo
|
511 | 536 | TypeScriptException tse = getTypeScriptException(e);
|
512 | 537 | if (tse != null) {
|
513 | 538 | if (tse instanceof TypeScriptTimeoutException) {
|
514 |
| - // when time out exception, we must be sure that the request is removed |
| 539 | + // when time out exception, we must be sure that the request |
| 540 | + // is removed |
515 | 541 | tryCancelRequest(((TypeScriptTimeoutException) tse).getRequest().getSeq());
|
516 | 542 | }
|
517 | 543 | throw (TypeScriptException) tse;
|
|
0 commit comments