1
1
package ts .core .tests ;
2
2
3
3
import java .io .File ;
4
- import java .io .IOException ;
5
4
import java .util .List ;
5
+ import java .util .concurrent .CompletableFuture ;
6
+ import java .util .concurrent .ExecutionException ;
6
7
7
8
import ts .TypeScriptException ;
8
- import ts .client .CodeEdit ;
9
+ import ts .client .FileSpan ;
9
10
import ts .client .ITypeScriptServiceClient ;
11
+ import ts .client .Location ;
12
+ import ts .client .LoggingInterceptor ;
10
13
import ts .client .TypeScriptServiceClient ;
11
- import ts .client .completions .CompletionInfo ;
12
- import ts .client .completions .ICompletionEntry ;
13
- import ts .client .completions .ICompletionInfo ;
14
- import ts .client .definition .DefinitionsInfo ;
15
- import ts .client .format .ITypeScriptFormatCollector ;
16
- import ts .client .navbar .ITypeScriptNavBarCollector ;
17
- import ts .client .navbar .NavigationBarItemRoot ;
14
+ import ts .client .completions .CompletionEntry ;
15
+ import ts .client .diagnostics .DiagnosticEvent ;
16
+ import ts .client .projectinfo .ProjectInfo ;
17
+ import ts .client .quickinfo .QuickInfo ;
18
18
import ts .utils .FileUtils ;
19
19
20
20
public class Main {
21
21
22
- public static void main (String [] args ) throws InterruptedException , TypeScriptException , IOException {
23
-
22
+ public static void main (String [] args ) throws TypeScriptException , InterruptedException , ExecutionException {
24
23
File projectDir = new File ("./samples" );
25
- // sample2 .ts has the following content:
24
+ // sample .ts has the following content:
26
25
// var s = "";s.
27
26
File sampleFile = new File (projectDir , "sample.ts" );
28
27
String fileName = FileUtils .getPath (sampleFile );
@@ -31,60 +30,104 @@ public static void main(String[] args) throws InterruptedException, TypeScriptEx
31
30
ITypeScriptServiceClient client = new TypeScriptServiceClient (projectDir ,
32
31
new File ("../ts.repository/node_modules/typescript/bin/tsserver" ), null );
33
32
34
- // Open "sample2.ts" in an editor
33
+ client .addInterceptor (LoggingInterceptor .getInstance ());
34
+
35
+ // Open "sample.ts" in an editor
35
36
client .openFile (fileName , null );
36
37
37
- // Do completion after the last dot of "s" variable which is a String
38
- // (charAt, ....)
39
- CompletionInfo completionInfo = new CompletionInfo (null );
40
- client .completions (fileName , 1 , 14 , null , completionInfo );
41
- display (completionInfo );
38
+ // compile on save
39
+ client .compileOnSaveEmitFile (fileName , true );
40
+
41
+
42
+ // Completions with line/offset
43
+ CompletableFuture <List <CompletionEntry >> completionPromise = client .completions (fileName , 1 , 14 );
44
+ List <CompletionEntry > entries = completionPromise .get ();
45
+ displayCompletions (entries );
46
+
47
+ // Completions with position (only since TypeScript 2.0)
48
+ // completionPromise = client.completions(fileName, 14);
49
+ // entries = completionPromise.get();
50
+ // displayCompletions(entries);
51
+
52
+ // QuickInfo
53
+ CompletableFuture <QuickInfo > quickInfoPromise = client .quickInfo (fileName , 1 , 5 );
54
+ QuickInfo quickInfo = quickInfoPromise .get ();
55
+ displayQuickInfo (quickInfo );
56
+
57
+ // Definition
58
+ CompletableFuture <List <FileSpan >> definitionPromise = client .definition (fileName , 1 , 5 );
59
+ List <FileSpan > definition = definitionPromise .get ();
60
+ displayDefinition (definition );
42
61
43
62
// Update the editor content to set s as number
44
- client .updateFile (fileName , "var s = 1;s." );
63
+ // var s = 1;s.
64
+ // client.changeFile(fileName, 9, 11, "1"); // position change, doesn't
65
+ // work with TypeScript 2.1.4
66
+ client .changeFile (fileName , 1 , 9 , 1 , 11 , "1" );
45
67
46
68
// Do completion after the last dot of "s" variable which is a Number
47
69
// (toExponential, ....)
48
- completionInfo = new CompletionInfo (null );
49
- client .completions (fileName , 1 , 14 , null , completionInfo );
50
- display (completionInfo );
51
-
52
- DefinitionsInfo definitionInfo = new DefinitionsInfo ();
53
- client .definition (fileName , 1 , 12 , definitionInfo );
54
- display (definitionInfo );
55
-
56
- client .format (fileName , 1 , 1 , 1 , 12 , new ITypeScriptFormatCollector () {
57
- @ Override
58
- public void format (List <CodeEdit > codeEdits ) throws TypeScriptException {
59
- for (CodeEdit codeEdit : codeEdits ) {
60
- System .err .println (codeEdit .getNewText ());
61
- }
62
- }
63
- });
64
-
65
- client .navbar (fileName , null , new ITypeScriptNavBarCollector () {
66
-
67
- @ Override
68
- public void setNavBar (NavigationBarItemRoot root ) {
69
- System .err .println (root );
70
- }
71
- });
72
-
73
- // client.join();
70
+ completionPromise = client .completions (fileName , 1 , 14 );
71
+ entries = completionPromise .get ();
72
+ displayCompletions (entries );
73
+
74
+ // geterr
75
+ List <DiagnosticEvent > events = client .geterr (new String [] { fileName }, 0 ).get ();
76
+ displayDiagnostics (events );
77
+
78
+ // projectInfo
79
+ ProjectInfo projectInfo = client .projectInfo (fileName , null , true ).get ();
80
+ displayProjectInfo (projectInfo );
81
+
82
+ //
83
+ // client.geterrForProjectRequest(file, delay, projectInfo)
84
+
85
+ // Close "sample.ts"
86
+ client .closeFile (fileName );
87
+
88
+
89
+ // synchronized (client) {t
90
+ // try {
91
+ // client.wait(2000);
92
+ // } catch (InterruptedException e) {
93
+ // // TODO Auto-generated catch block
94
+ // e.printStackTrace();
95
+ // }
96
+ // }
74
97
client .dispose ();
75
98
76
99
}
77
100
78
- private static void display (DefinitionsInfo definitionInfo ) {
79
- // TODO Auto-generated method stub
101
+ public static void displayCompletions (List <CompletionEntry > entries ) {
102
+ for (CompletionEntry entry : entries ) {
103
+ System .err .println (entry .getName ());
104
+ }
105
+ }
80
106
107
+ private static void displayQuickInfo (QuickInfo quickInfo ) {
108
+ System .err .println ("DisplayString: " + quickInfo .getDisplayString () + ", start: "
109
+ + toString (quickInfo .getStart ()) + ", end: " + toString (quickInfo .getEnd ()));
81
110
}
82
111
83
- private static void display (ICompletionInfo completionInfo ) {
84
- System .out .println ("getCompletionsAtLineOffset:" );
85
- ICompletionEntry [] entries = completionInfo .getEntries ();
86
- for (ICompletionEntry entry : entries ) {
87
- System .out .println (entry .getName ());
112
+ private static void displayDefinition (List <FileSpan > spans ) {
113
+ for (FileSpan span : spans ) {
114
+ System .err .println ("file: " + span .getFile () + ", start: " + toString (span .getStart ()) + ", end: "
115
+ + toString (span .getEnd ()));
88
116
}
89
117
}
118
+
119
+ private static void displayDiagnostics (List <DiagnosticEvent > events ) {
120
+ for (DiagnosticEvent event : events ) {
121
+ System .err .println (event .getBody ().getFile ());
122
+ }
123
+ }
124
+
125
+ private static void displayProjectInfo (ProjectInfo projectInfo ) {
126
+ System .err .println (projectInfo .getConfigFileName ());
127
+ }
128
+
129
+ private static String toString (Location loc ) {
130
+ return "{" + loc .getLine () + ", " + loc .getOffset () + "}" ;
131
+ }
132
+
90
133
}
0 commit comments