Skip to content

Commit 1f6f935

Browse files
author
angelozerr
committed
Support for asynch occurrence collector to avoid freeze Eclipse with
large TypeScript project. See #17
1 parent db6efd6 commit 1f6f935

25 files changed

+333
-100
lines changed

core/ts.core/src/ts/client/CancellationToken.java

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ts.client;
2+
3+
public interface ICancellationToken {
4+
5+
boolean isCancellationRequested();
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ts.client;
2+
3+
import ts.TypeScriptException;
4+
5+
public interface ITypeScriptAsynchCollector {
6+
7+
void startCollect();
8+
9+
void endCollect();
10+
11+
void onError(TypeScriptException e);
12+
}

core/ts.core/src/ts/client/TypeScriptServiceClient.java

+32-60
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private void collect(JsonObject response, ITypeScriptGeterrCollector collector)
323323
}
324324

325325
// -------------------------- Format
326-
326+
327327
@Override
328328
public void format(String fileName, int line, int offset, int endLine, int endOffset,
329329
ITypeScriptFormatCollector collector) throws TypeScriptException {
@@ -346,63 +346,31 @@ private void collectFormat(JsonArray body, ITypeScriptFormatCollector collector)
346346
end.getInt("offset", -1), newText);
347347
}
348348
}
349-
349+
350350
// ----------------- Find References
351-
352-
@Override
353-
public void references(String fileName, int line, int offset,
354-
ITypeScriptReferencesCollector collector) throws TypeScriptException {
355-
ReferencesRequest request = new ReferencesRequest(fileName, line, offset);
356-
JsonObject response = execute(request, true, null).asObject();
357-
collectReferences(response, collector);
358-
}
359351

360-
private void collectReferences(JsonObject response, ITypeScriptReferencesCollector collector) throws TypeScriptException {
361-
JsonObject body = response.get("body").asObject();
362-
JsonArray refs = body.get("refs").asArray();
363-
JsonObject ref = null;
364-
String file = null;
365-
JsonObject start = null;
366-
JsonObject end = null;
367-
String lineText = null;
368-
for (JsonValue r : refs) {
369-
ref = r.asObject();
370-
file = ref.getString("file", null);
371-
start = ref.get("start").asObject();
372-
end = ref.get("end").asObject();
373-
lineText = ref.getString("lineText", null);
374-
collector.ref(file, start.getInt("line", -1), start.getInt("offset", -1), end.getInt("line", -1),
375-
end.getInt("offset", -1), lineText);
376-
377-
}
352+
@Override
353+
public void references(String fileName, int line, int offset, ITypeScriptReferencesCollector collector)
354+
throws TypeScriptException {
355+
ReferencesRequest request = new ReferencesRequest(fileName, line, offset, collector);
356+
execute(request);
378357
}
379358

380359
// ----------------- Occurrences
381-
360+
382361
@Override
383-
public void occurrences(String fileName, int line, int offset,
384-
ITypeScriptOccurrencesCollector collector) throws TypeScriptException {
385-
OccurrencesRequest request = new OccurrencesRequest(fileName, line, offset);
386-
JsonObject response = execute(request, true, null).asObject();
387-
collectOccurrences(response, collector);
362+
public void occurrences(String fileName, int line, int offset, ITypeScriptOccurrencesCollector collector)
363+
throws TypeScriptException {
364+
OccurrencesRequest request = new OccurrencesRequest(fileName, line, offset, collector);
365+
execute(request);
388366
}
389367

390-
private void collectOccurrences(JsonObject response, ITypeScriptOccurrencesCollector collector) throws TypeScriptException {
391-
JsonArray body = response.get("body").asArray();
392-
JsonObject occurrence = null;
393-
String file = null;
394-
JsonObject start = null;
395-
JsonObject end = null;
396-
boolean isWriteAccess = false;
397-
for (JsonValue b : body) {
398-
occurrence = b.asObject();
399-
file = occurrence.getString("file", null);
400-
start = occurrence.get("start").asObject();
401-
end = occurrence.get("end").asObject();
402-
isWriteAccess= occurrence.getBoolean("isWriteAccess", false);
403-
collector.addOccurrence(file, start.getInt("line", -1), start.getInt("offset", -1), end.getInt("line", -1),
404-
end.getInt("offset", -1), isWriteAccess);
405-
368+
private void execute(Request request) throws TypeScriptException {
369+
if (!request.isAsynch()) {
370+
JsonObject response = execute(request, true, null).asObject();
371+
request.collect(response);
372+
} else {
373+
execute(request, true, null);
406374
}
407375
}
408376

@@ -575,7 +543,7 @@ public void join() throws InterruptedException {
575543
* @return the result of the request if it needed and null otherwise.
576544
* @throws TypeScriptException
577545
*/
578-
private JsonValue execute(Request request, boolean expectsResult, CancellationToken token)
546+
private JsonValue execute(Request request, boolean expectsResult, ICancellationToken token)
579547
throws TypeScriptException {
580548
boolean eventRequest = (request instanceof GeterrRequest);
581549
RequestItem requestInfo = null;
@@ -600,11 +568,11 @@ private JsonValue execute(Request request, boolean expectsResult, CancellationTo
600568
handleError(request, e, request.getStartTime());
601569
TypeScriptException tse = getTypeScriptException(e);
602570
if (tse != null) {
603-
if (tse instanceof TypeScriptTimeoutException) {
604-
// when time out exception, we must be sure that the request
605-
// is removed
606-
tryCancelRequest(((TypeScriptTimeoutException) tse).getRequest().getSeq());
607-
}
571+
// if (tse instanceof TypeScriptTimeoutException) {
572+
// // when time out exception, we must be sure that the request
573+
// // is removed
574+
// tryCancelRequest(((TypeScriptTimeoutException) tse).getRequest().getSeq());
575+
// }
608576
throw (TypeScriptException) tse;
609577
}
610578
throw new TypeScriptException(e);
@@ -633,7 +601,7 @@ private boolean tryCancelRequest(int seq) {
633601
return false;
634602
}
635603

636-
private void sendNextRequests() throws TypeScriptException {
604+
private void sendNextRequests() {
637605
RequestItem requestItem = null;
638606
while (this.pendingResponses.get() == 0 && !this.requestQueue.isEmpty()) {
639607
synchronized (requestQueue) {
@@ -643,7 +611,7 @@ private void sendNextRequests() throws TypeScriptException {
643611
}
644612
}
645613

646-
private void sendRequest(RequestItem requestItem) throws TypeScriptException {
614+
private void sendRequest(RequestItem requestItem) {
647615
Request serverRequest = requestItem.request;
648616
// log request
649617
handleRequest(serverRequest);
@@ -680,18 +648,18 @@ private void sendRequest(RequestItem requestItem) throws TypeScriptException {
680648
synchronized (this.callbacks) {
681649
ICallbackItem callback = this.callbacks.get(serverRequest.getSeq());
682650
if (callback != null) {
683-
// callback.e(err);
651+
callback.error(e);
684652
this.callbacks.remove(serverRequest.getSeq());
685653
}
686654
this.pendingResponses.getAndDecrement();
687655
}
688656
}
689-
throw e;
690657
}
691658

692659
}
693660

694661
private void dispatchMessage(JsonObject response) {
662+
try {
695663
String type = response.getString("type", null);
696664
if ("response".equals(type)) {
697665
int seq = response.getInt("request_seq", -1);
@@ -725,6 +693,10 @@ private void dispatchMessage(JsonObject response) {
725693
}
726694
}
727695
}
696+
}
697+
finally {
698+
this.sendNextRequests();
699+
}
728700
}
729701

730702
// --------------------------- Handler for Request/response/Error

core/ts.core/src/ts/client/TypeScriptTimeoutException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class TypeScriptTimeoutException extends TypeScriptException {
77

88
private final Request request;
99

10-
public TypeScriptTimeoutException(Request<?> request, long timeout) {
10+
public TypeScriptTimeoutException(Request request, long timeout) {
1111
super("Timeout error " + timeout + "ms");
1212
this.request = request;
1313
}

core/ts.core/src/ts/internal/client/ICallbackItem.java

+4
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
public interface ICallbackItem<T> extends Callable<T> {
88

99
boolean complete(JsonObject response);
10+
11+
void error(Throwable e);
12+
13+
boolean isAsynch();
1014

1115
}

core/ts.core/src/ts/internal/client/protocol/ChangeRequest.java

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
package ts.internal.client.protocol;
1212

13+
import com.eclipsesource.json.JsonObject;
14+
15+
import ts.TypeScriptException;
16+
1317
/**
1418
* Change request message; value of command field is "change". Update the
1519
* server's view of the file named by argument 'file'. Server does not currently
@@ -24,4 +28,9 @@ public ChangeRequest(String fileName, int line, int offset, int endLine, int end
2428
super(CommandNames.Change, new ChangeRequestArgs(fileName, line, offset, endLine, endOffset, insertString));
2529
}
2630

31+
@Override
32+
public void collect(JsonObject response) throws TypeScriptException {
33+
// None response
34+
}
35+
2736
}

core/ts.core/src/ts/internal/client/protocol/CloseRequest.java

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
package ts.internal.client.protocol;
1212

13+
import com.eclipsesource.json.JsonObject;
14+
15+
import ts.TypeScriptException;
16+
1317
/**
1418
* Close request.
1519
*
@@ -22,4 +26,8 @@ public CloseRequest(String fileName) {
2226
super(CommandNames.Close, new FileRequestArgs(fileName), null);
2327
}
2428

29+
@Override
30+
public void collect(JsonObject response) throws TypeScriptException {
31+
// None response
32+
}
2533
}

core/ts.core/src/ts/internal/client/protocol/CompletionDetailsRequest.java

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
package ts.internal.client.protocol;
1212

13+
import com.eclipsesource.json.JsonObject;
14+
15+
import ts.TypeScriptException;
16+
1317
/**
1418
* Completion entry details request; value of command field is
1519
* "completionEntryDetails". Given a file location (file, line, col) and an
@@ -21,4 +25,9 @@ public class CompletionDetailsRequest extends FileLocationRequest {
2125
public CompletionDetailsRequest(String fileName, int line, int offset, String[] entryNames) {
2226
super(CommandNames.CompletionEntryDetails, new CompletionDetailsRequestArgs(fileName, line, offset, entryNames));
2327
}
28+
29+
@Override
30+
public void collect(JsonObject response) throws TypeScriptException {
31+
// None response
32+
}
2433
}

core/ts.core/src/ts/internal/client/protocol/CompletionsRequest.java

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
package ts.internal.client.protocol;
1212

13+
import com.eclipsesource.json.JsonObject;
14+
15+
import ts.TypeScriptException;
16+
1317
/**
1418
* Completions request; value of command field is "completions". Given a file
1519
* location (file, line, col) and a prefix (which may be the empty string),
@@ -23,5 +27,10 @@ public class CompletionsRequest extends FileLocationRequest {
2327
public CompletionsRequest(String fileName, int line, int offset, String prefix) {
2428
super(CommandNames.Completions, new CompletionsRequestArgs(fileName, line, offset, prefix));
2529
}
30+
31+
@Override
32+
public void collect(JsonObject response) throws TypeScriptException {
33+
// None response
34+
}
2635

2736
}

core/ts.core/src/ts/internal/client/protocol/DefinitionRequest.java

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
package ts.internal.client.protocol;
1212

13+
import com.eclipsesource.json.JsonObject;
14+
15+
import ts.TypeScriptException;
16+
1317
/**
1418
* Go to definition request; value of command field is "definition". Return
1519
* response giving the file locations that define the symbol found in file at
@@ -23,5 +27,10 @@ public class DefinitionRequest extends FileLocationRequest {
2327
public DefinitionRequest(String fileName, int line, int offset) {
2428
super(CommandNames.Definition, new FileLocationRequestArgs(fileName, line, offset));
2529
}
30+
31+
@Override
32+
public void collect(JsonObject response) throws TypeScriptException {
33+
// None response
34+
}
2635

2736
}

core/ts.core/src/ts/internal/client/protocol/FileLocationRequest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
*/
1111
package ts.internal.client.protocol;
1212

13+
import ts.client.ITypeScriptCollector;
14+
1315
/**
1416
* A request whose arguments specify a file location (file, line, col).
1517
*
1618
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.
1719
* d.ts
1820
*/
19-
public class FileLocationRequest extends FileRequest {
21+
public abstract class FileLocationRequest<C extends ITypeScriptCollector> extends FileRequest<C> {
2022

2123
public FileLocationRequest(CommandNames command, FileLocationRequestArgs args) {
2224
super(command, args, null);

core/ts.core/src/ts/internal/client/protocol/FileRequest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
*/
1111
package ts.internal.client.protocol;
1212

13+
import ts.client.ITypeScriptCollector;
14+
1315
/**
1416
* Request whose sole parameter is a file name.
1517
*
1618
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.
1719
* d.ts
1820
*/
19-
public class FileRequest extends SimpleRequest {
21+
public abstract class FileRequest<C extends ITypeScriptCollector> extends SimpleRequest<C> {
2022

2123
public FileRequest(CommandNames command, FileRequestArgs args, Integer seq) {
2224
super(command, args, seq);

core/ts.core/src/ts/internal/client/protocol/FormatRequest.java

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
package ts.internal.client.protocol;
1212

13+
import com.eclipsesource.json.JsonObject;
14+
15+
import ts.TypeScriptException;
16+
1317
/**
1418
* Format request; value of command field is "format". Return response giving
1519
* zero or more edit instructions. The edit instructions will be sorted in file
@@ -25,4 +29,8 @@ public FormatRequest(String fileName, int line, int offset, int endLine, int end
2529
super(CommandNames.Format, new FormatRequestArgs(fileName, line, offset, endLine, endOffset));
2630
}
2731

32+
@Override
33+
public void collect(JsonObject response) throws TypeScriptException {
34+
// None response
35+
}
2836
}

core/ts.core/src/ts/internal/client/protocol/GeterrRequest.java

+4
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,8 @@ protected Object getResult() throws Exception {
9999
return result;
100100
}
101101

102+
@Override
103+
public void collect(JsonObject response) {
104+
105+
}
102106
}

0 commit comments

Comments
 (0)