Skip to content

Commit 34be7f7

Browse files
committed
Do big refactoring to use Java8 CompletableFuture for
TypeScriptServiceClient. See #63
1 parent ec08dcc commit 34be7f7

File tree

182 files changed

+3980
-2938
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+3980
-2938
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package ts.core.tests;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.ExecutionException;
7+
8+
import ts.TypeScriptException;
9+
import ts.client.Location;
10+
import ts.client2.CompletionEntry;
11+
import ts.client2.FileSpan;
12+
import ts.client2.ITypeScriptServiceClient2;
13+
import ts.client2.QuickInfo;
14+
import ts.client2.TypeScriptServiceClient2;
15+
import ts.utils.FileUtils;
16+
17+
public class Main3 {
18+
19+
public static void main(String[] args) throws TypeScriptException, InterruptedException, ExecutionException {
20+
File projectDir = new File("./samples");
21+
// sample.ts has the following content:
22+
// var s = "";s.
23+
File sampleFile = new File(projectDir, "sample.ts");
24+
String fileName = FileUtils.getPath(sampleFile);
25+
26+
// Create TypeScript client
27+
ITypeScriptServiceClient2 client = new TypeScriptServiceClient2(projectDir,
28+
new File("../ts.repository/node_modules/typescript/bin/tsserver"), null);
29+
30+
// Open "sample.ts" in an editor
31+
client.openFile(fileName, null);
32+
33+
// Completions with line/offset
34+
CompletableFuture<List<CompletionEntry>> completionPromise = client.completions(fileName, 1, 14);
35+
List<CompletionEntry> entries = completionPromise.get();
36+
displayCompletions(entries);
37+
38+
// Completions with position (only since TypeScript 2.0)
39+
completionPromise = client.completions(fileName, 14);
40+
entries = completionPromise.get();
41+
displayCompletions(entries);
42+
43+
// QuickInfo
44+
CompletableFuture<QuickInfo> quickInfoPromise = client.quickInfo(fileName, 1, 5);
45+
QuickInfo quickInfo = quickInfoPromise.get();
46+
displayQuickInfo(quickInfo);
47+
48+
// Definition
49+
CompletableFuture<List<FileSpan>> definitionPromise = client.definition(fileName, 1, 5);
50+
List<FileSpan> definition = definitionPromise.get();
51+
displayDefinition(definition);
52+
53+
// Update the editor content to set s as number
54+
// var s = 1;s.
55+
// client.changeFile(fileName, 9, 11, "1"); // position change, doesn't
56+
// work with TypeScript 2.1.4
57+
client.changeFile(fileName, 1, 9, 1, 11, "1");
58+
59+
// Do completion after the last dot of "s" variable which is a Number
60+
// (toExponential, ....)
61+
completionPromise = client.completions(fileName, 1, 14);
62+
entries = completionPromise.get();
63+
displayCompletions(entries);
64+
65+
// Close "sample.ts"
66+
client.closeFile(fileName);
67+
68+
// synchronized (client) {
69+
// try {
70+
// client.wait(2000);
71+
// } catch (InterruptedException e) {
72+
// // TODO Auto-generated catch block
73+
// e.printStackTrace();
74+
// }
75+
// }
76+
client.dispose();
77+
78+
}
79+
80+
public static void displayCompletions(List<CompletionEntry> entries) {
81+
for (CompletionEntry entry : entries) {
82+
System.err.println(entry.getName());
83+
}
84+
}
85+
86+
private static void displayQuickInfo(QuickInfo quickInfo) {
87+
System.err.println("DisplayString: " + quickInfo.getDisplayString() + ", start: "
88+
+ toString(quickInfo.getStart()) + ", end: " + toString(quickInfo.getEnd()));
89+
}
90+
91+
private static void displayDefinition(List<FileSpan> spans) {
92+
for (FileSpan span : spans) {
93+
System.err.println("file: " + span.getFile() + ", start: " + toString(span.getStart()) + ", end: "
94+
+ toString(span.getEnd()));
95+
}
96+
}
97+
98+
private static String toString(Location loc) {
99+
return "{" + loc.getLine() + ", " + loc.getOffset() + "}";
100+
}
101+
102+
}

core/ts.core/.classpath

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
55
<classpathentry kind="src" path="src"/>
66
<classpathentry kind="output" path="target/classes"/>

core/ts.core/META-INF/MANIFEST.MF

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ Bundle-Localization: plugin
66
Bundle-SymbolicName: ts.core
77
Bundle-Version: 1.2.0.qualifier
88
Bundle-Activator: ts.internal.Activator
9-
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
9+
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
1010
Import-Package: com.eclipsesource.json;version="0.9.4",
1111
org.osgi.framework;version="1.3.0"
1212
Bundle-ActivationPolicy: lazy
1313
Export-Package: ts,
1414
ts.client,
1515
ts.client.codefixes,
1616
ts.client.completions,
17-
ts.client.definition,
17+
ts.client.configure,
1818
ts.client.diagnostics,
1919
ts.client.format,
2020
ts.client.navbar,
2121
ts.client.occurrences,
22+
ts.client.projectinfo,
2223
ts.client.quickinfo,
2324
ts.client.references,
2425
ts.client.signaturehelp,

core/ts.core/src/ts/client/CommandNames.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ public enum CommandNames {
2828
Definition("definition"),
2929
SignatureHelp("signatureHelp"),
3030
QuickInfo("quickinfo"),
31-
Geterr("geterr"),
31+
Geterr("geterr"),
32+
GeterrForProject("geterrForProject"),
3233
Format("format"),
3334
References("references"),
3435
Occurrences("occurrences"),
35-
Configure("configure"),
36+
Configure("configure"),
37+
ProjectInfo("projectInfo"),
3638

3739
// 2.0.3
3840
SemanticDiagnosticsSync("semanticDiagnosticsSync", "2.0.3"),
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2015-2016 Angelo ZERR.
2+
* Copyright (c) 2015-2017 Angelo ZERR.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -8,20 +8,22 @@
88
* Contributors:
99
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
1010
*/
11-
package ts.client.completions;
11+
package ts.client;
1212

1313
/**
14-
* @see https://github.com/Microsoft/TypeScript/blob/master/src/services/services.ts
15-
*
14+
* Object found in response messages defining a span of text in a specific
15+
* source file.
16+
*
17+
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.ts
1618
*/
17-
public interface ICompletionInfo {
19+
public class FileSpan extends TextSpan {
1820

19-
boolean isMemberCompletion();
2021
/**
21-
* Returns true when the current location also allows for a new identifier
22-
* @return true when the current location also allows for a new identifier
22+
* File containing text span.
2323
*/
24-
boolean isNewIdentifierLocation();
25-
26-
ICompletionEntry[] getEntries();
24+
private String file;
25+
26+
public String getFile() {
27+
return file;
28+
}
2729
}

core/ts.core/src/ts/client/IInterceptor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
*/
1111
package ts.client;
1212

13-
import com.eclipsesource.json.JsonObject;
14-
13+
import ts.internal.client.protocol.Response;
1514
import ts.internal.client.protocol.Request;
1615

1716
public interface IInterceptor {
1817

1918
void handleRequest(Request request, ITypeScriptServiceClient client, String methodName);
2019

21-
void handleResponse(JsonObject response, ITypeScriptServiceClient client,
20+
void handleResponse(Response response, ITypeScriptServiceClient client,
2221
String methodName, long ellapsedTime);
2322

2423
void handleError(Throwable error, ITypeScriptServiceClient client, String methodName,

0 commit comments

Comments
 (0)