|
| 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 | +} |
0 commit comments