Skip to content

Commit

Permalink
Implement refactors tsserver commands. See
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed May 20, 2017
1 parent cddd2e7 commit fc05cf7
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 4 deletions.
6 changes: 5 additions & 1 deletion core/ts.core/src/ts/client/CommandNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public enum CommandNames implements ISupportable {
// 2.1.0
Implementation("implementation", "2.1.0"),
GetSupportedCodeFixes("getSupportedCodeFixes", "2.1.0"),
GetCodeFixes("getCodeFixes", "2.1.0");
GetCodeFixes("getCodeFixes", "2.1.0"),

// 2.4.0
GetApplicableRefactors("getApplicableRefactors", "2.4.0"),
GetRefactorCodeActions("getRefactorCodeActions", "2.4.0");

private final String name;
private final String sinceVersion;

Expand Down
11 changes: 10 additions & 1 deletion core/ts.core/src/ts/client/ITypeScriptServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import ts.client.occurrences.OccurrencesResponseItem;
import ts.client.projectinfo.ProjectInfo;
import ts.client.quickinfo.QuickInfo;
import ts.client.refactors.ApplicableRefactorInfo;
import ts.client.refactors.RefactorCodeActions;
import ts.client.references.ReferencesResponseBody;
import ts.client.rename.RenameResponseBody;
import ts.client.signaturehelp.SignatureHelpItems;
Expand Down Expand Up @@ -257,7 +259,6 @@ CompletableFuture<List<CodeAction>> getCodeFixes(String fileName, IPositionProvi

CompletableFuture<List<String>> getSupportedCodeFixes() throws TypeScriptException;

//
/**
* Definition for the given fileName at the given line/offset.
*
Expand All @@ -269,6 +270,14 @@ CompletableFuture<List<CodeAction>> getCodeFixes(String fileName, IPositionProvi
*/
CompletableFuture<List<FileSpan>> implementation(String fileName, int line, int offset) throws TypeScriptException;

// Since 2.4.0

CompletableFuture<List<ApplicableRefactorInfo>> getApplicableRefactors(String fileName, int line, int offset)
throws TypeScriptException;

CompletableFuture<RefactorCodeActions> getRefactorCodeActions(String fileName, int line, int offset,
String refactorName) throws TypeScriptException;

void addClientListener(ITypeScriptClientListener listener);

void removeClientListener(ITypeScriptClientListener listener);
Expand Down
21 changes: 19 additions & 2 deletions core/ts.core/src/ts/client/TypeScriptServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import ts.client.completions.ICompletionEntryFactory;
import ts.client.completions.ICompletionEntryMatcherProvider;
import ts.client.configure.ConfigureRequestArguments;
import ts.client.diagnostics.Diagnostic;
import ts.client.diagnostics.DiagnosticEvent;
import ts.client.diagnostics.DiagnosticEventBody;
import ts.client.diagnostics.IDiagnostic;
Expand All @@ -44,6 +43,8 @@
import ts.client.occurrences.OccurrencesResponseItem;
import ts.client.projectinfo.ProjectInfo;
import ts.client.quickinfo.QuickInfo;
import ts.client.refactors.ApplicableRefactorInfo;
import ts.client.refactors.RefactorCodeActions;
import ts.client.references.ReferencesResponseBody;
import ts.client.rename.RenameResponseBody;
import ts.client.signaturehelp.SignatureHelpItems;
Expand All @@ -60,6 +61,8 @@
import ts.internal.client.protocol.DefinitionRequest;
import ts.internal.client.protocol.DocCommentTemplateRequest;
import ts.internal.client.protocol.FormatRequest;
import ts.internal.client.protocol.GetApplicableRefactorsRequest;
import ts.internal.client.protocol.GetRefactorCodeActionsRequest;
import ts.internal.client.protocol.GetSupportedCodeFixesRequest;
import ts.internal.client.protocol.GeterrForProjectRequest;
import ts.internal.client.protocol.GeterrRequest;
Expand Down Expand Up @@ -461,7 +464,7 @@ public CompletableFuture<TextInsertion> docCommentTemplate(String fileName, int
throws TypeScriptException {
return execute(new DocCommentTemplateRequest(fileName, line, offset), true);
}

// Since 2.1.0

@Override
Expand All @@ -482,6 +485,20 @@ public CompletableFuture<List<FileSpan>> implementation(String fileName, int lin
return execute(new ImplementationRequest(fileName, line, offset), true);
}

// Since 2.4.0

@Override
public CompletableFuture<List<ApplicableRefactorInfo>> getApplicableRefactors(String fileName, int line, int offset)
throws TypeScriptException {
return execute(new GetApplicableRefactorsRequest(fileName, line, offset), true);
}

@Override
public CompletableFuture<RefactorCodeActions> getRefactorCodeActions(String fileName, int line, int offset,
String refactorName) throws TypeScriptException {
return execute(new GetRefactorCodeActionsRequest(fileName, line, offset, refactorName), true);
}

private <T> CompletableFuture<T> execute(Request<?> request, boolean expectsResult) throws TypeScriptException {
if (!expectsResult) {
sendRequest(request);
Expand Down
16 changes: 16 additions & 0 deletions core/ts.core/src/ts/client/refactors/ApplicableRefactorInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ts.client.refactors;

public class ApplicableRefactorInfo {

private String name;

private String description;

public String getName() {
return name;
}

public String getDescription() {
return description;
}
}
20 changes: 20 additions & 0 deletions core/ts.core/src/ts/client/refactors/RefactorCodeActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ts.client.refactors;

import java.util.List;

import ts.client.codefixes.CodeAction;

public class RefactorCodeActions {

private List<CodeAction> actions;

private Integer renameLocation;

public List<CodeAction> getActions() {
return actions;
}

public Integer getRenameLocation() {
return renameLocation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package ts.internal.client.protocol;

import java.util.List;

import com.google.gson.JsonObject;

import ts.client.CommandNames;
import ts.client.refactors.ApplicableRefactorInfo;

/**
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.ts
*/
public class GetApplicableRefactorsRequest extends Request<GetApplicableRefactorsRequestArgs> {

public GetApplicableRefactorsRequest(String file, int line, int offset) {
super(CommandNames.GetApplicableRefactors.getName(), new GetApplicableRefactorsRequestArgs(file, line, offset));
}

@Override
public Response<List<ApplicableRefactorInfo>> parseResponse(JsonObject json) {
return GsonHelper.DEFAULT_GSON.fromJson(json, GetApplicableRefactorsResponse.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package ts.internal.client.protocol;

/**
*
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.ts
*
*/
public class GetApplicableRefactorsRequestArgs extends FileLocationRequestArgs {

public GetApplicableRefactorsRequestArgs(String file, int position) {
super(file, position);
}

public GetApplicableRefactorsRequestArgs(String file, int line, int offset) {
super(file, line, offset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package ts.internal.client.protocol;

import java.util.List;

import ts.client.refactors.ApplicableRefactorInfo;

/**
* Refactors response message.
*
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.ts
*/
public class GetApplicableRefactorsResponse extends Response<List<ApplicableRefactorInfo>> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package ts.internal.client.protocol;

import com.google.gson.JsonObject;

import ts.client.CommandNames;
import ts.client.refactors.RefactorCodeActions;

/**
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.ts
*/
public class GetRefactorCodeActionsRequest extends Request<GetRefactorCodeActionsRequestArgs> {

public GetRefactorCodeActionsRequest(String file, int line, int offset, String refactorName) {
super(CommandNames.GetRefactorCodeActions.getName(),
new GetRefactorCodeActionsRequestArgs(file, line, offset, refactorName));
}

@Override
public Response<RefactorCodeActions> parseResponse(JsonObject json) {
return GsonHelper.DEFAULT_GSON.fromJson(json, GetRefactorCodeActionsResponse.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package ts.internal.client.protocol;

/**
*
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.ts
*
*/
public class GetRefactorCodeActionsRequestArgs extends FileLocationRequestArgs {

/* The kind of the applicable refactor */
private String refactorName;

public GetRefactorCodeActionsRequestArgs(String file, int position, String refactorName) {
super(file, position);
this.refactorName = refactorName;
}

public GetRefactorCodeActionsRequestArgs(String file, int line, int offset, String refactorName) {
super(file, line, offset);
this.refactorName = refactorName;
}

public String getRefactorName() {
return refactorName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package ts.internal.client.protocol;

import ts.client.refactors.RefactorCodeActions;

/**
* Refactor code actions response message.
*
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.ts
*/
public class GetRefactorCodeActionsResponse extends Response<RefactorCodeActions> {

}

0 comments on commit fc05cf7

Please sign in to comment.