Skip to content

Commit c8b0a50

Browse files
author
angelozerr
committed
Implement service with tsserver for format + bind Ctrl+Shift+F for
prepapre format action. See #36
1 parent 2431a48 commit c8b0a50

Some content is hidden

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

46 files changed

+1051
-543
lines changed

core/ts.core.tests/src/ts/core/tests/Main.java

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import ts.client.completions.ICompletionEntry;
1111
import ts.client.completions.ICompletionInfo;
1212
import ts.client.definition.DefinitionsInfo;
13+
import ts.client.format.ITypeScriptFormatCollector;
1314
import ts.utils.FileUtils;
1415

1516
public class Main {
@@ -48,6 +49,14 @@ public static void main(String[] args) throws InterruptedException, TypeScriptEx
4849
client.definition(fileName, 1, 12, definitionInfo);
4950
display(definitionInfo);
5051

52+
client.format(fileName, 1, 1, 1, 12, new ITypeScriptFormatCollector() {
53+
@Override
54+
public void format(int startLine, int startOffset, int endLine, int endOffset, String newText)
55+
throws TypeScriptException {
56+
System.err.println(newText);
57+
}
58+
});
59+
5160
client.join();
5261
client.dispose();
5362

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

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Export-Package: ts,
1414
ts.client,
1515
ts.client.completions,
1616
ts.client.definition,
17+
ts.client.format,
1718
ts.client.geterr,
1819
ts.client.protocol,
1920
ts.client.quickinfo,

core/ts.core/src/ts/client/ITypeScriptServiceClient.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import ts.client.completions.ITypeScriptCompletionCollector;
1515
import ts.client.completions.ITypeScriptCompletionEntryDetailsCollector;
1616
import ts.client.definition.ITypeScriptDefinitionCollector;
17+
import ts.client.format.ITypeScriptFormatCollector;
1718
import ts.client.geterr.ITypeScriptGeterrCollector;
1819
import ts.client.quickinfo.ITypeScriptQuickInfoCollector;
1920
import ts.client.signaturehelp.ITypeScriptSignatureHelpCollector;
@@ -52,6 +53,8 @@ void changeFile(String fileName, int line, int offset, int endLine, int endOffse
5253

5354
void geterr(String[] files, int delay, ITypeScriptGeterrCollector collector) throws TypeScriptException;
5455

56+
void format(String fileName, int line, int offset, int endLine, int endOffset, ITypeScriptFormatCollector collector) throws TypeScriptException;
57+
5558
void join() throws InterruptedException;
5659

5760
void addClientListener(ITypeScriptClientListener listener);

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
import ts.client.completions.ITypeScriptCompletionCollector;
3131
import ts.client.completions.ITypeScriptCompletionEntryDetailsCollector;
3232
import ts.client.definition.ITypeScriptDefinitionCollector;
33+
import ts.client.format.ITypeScriptFormatCollector;
3334
import ts.client.geterr.ITypeScriptGeterrCollector;
3435
import ts.client.protocol.ChangeRequest;
3536
import ts.client.protocol.CloseRequest;
3637
import ts.client.protocol.CompletionDetailsRequest;
3738
import ts.client.protocol.CompletionsRequest;
3839
import ts.client.protocol.DefinitionRequest;
40+
import ts.client.protocol.FormatRequest;
3941
import ts.client.protocol.GeterrRequest;
4042
import ts.client.protocol.OpenRequest;
4143
import ts.client.protocol.QuickInfoRequest;
@@ -316,6 +318,29 @@ private void collect(JsonObject response, ITypeScriptGeterrCollector collector)
316318
}
317319
}
318320

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+
319344
/**
320345
* Write the buffer of editor content to a temporary file and have the
321346
* server reload it
@@ -511,7 +536,8 @@ private JsonValue execute(Request request, boolean expectsResult, CancellationTo
511536
TypeScriptException tse = getTypeScriptException(e);
512537
if (tse != null) {
513538
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
515541
tryCancelRequest(((TypeScriptTimeoutException) tse).getRequest().getSeq());
516542
}
517543
throw (TypeScriptException) tse;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ts.client.format;
2+
3+
import ts.TypeScriptException;
4+
import ts.client.ITypeScriptCollector;
5+
6+
public interface ITypeScriptFormatCollector extends ITypeScriptCollector {
7+
8+
void format(int startLine, int startOffset, int endLine, int endOffset, String newText) throws TypeScriptException;
9+
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
/**

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
/**
1414
* Close request.
15-
*
15+
*
16+
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.
17+
* d.ts
1618
*/
1719
public class CloseRequest extends SimpleRequest {
1820

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
*/
1717
public enum CommandNames {
1818

19-
Open("open"), Close("close"), Change("change"), NavBar("navbar"), Completions("completions"), CompletionEntryDetails(
20-
"completionEntryDetails"), Reload("reload"), Definition("definition"), SignatureHelp("signatureHelp"), QuickInfo(
21-
"quickinfo"), Geterr("geterr");
19+
Open("open"), Close("close"), Change("change"), NavBar("navbar"), Completions(
20+
"completions"), CompletionEntryDetails("completionEntryDetails"), Reload("reload"), Definition(
21+
"definition"), SignatureHelp(
22+
"signatureHelp"), QuickInfo("quickinfo"), Geterr("geterr"), Format("format");
2223

2324
private final String name;
2425

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

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
/**

core/ts.core/src/ts/client/protocol/CompletionsRequestArgs.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
/**

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

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
/**

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

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
/**

core/ts.core/src/ts/client/protocol/FileLocationRequestArgs.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
/**

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

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
/**

core/ts.core/src/ts/client/protocol/FileRequestArgs.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
import com.eclipsesource.json.JsonObject;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
11+
package ts.client.protocol;
12+
13+
/**
14+
* Format request; value of command field is "format". Return response giving
15+
* zero or more edit instructions. The edit instructions will be sorted in file
16+
* order. Applying the edit instructions in reverse to file will result in
17+
* correctly reformatted text.
18+
*
19+
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.
20+
* d.ts
21+
*/
22+
public class FormatRequest extends FileLocationRequest {
23+
24+
public FormatRequest(String fileName, int line, int offset, int endLine, int endOffset) {
25+
super(CommandNames.Format, new FormatRequestArgs(fileName, line, offset, endLine, endOffset));
26+
}
27+
28+
}

core/ts.core/src/ts/client/protocol/FormatRequestArgs.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.protocol;
212

313
/**

0 commit comments

Comments
 (0)