change the way webflux code lenses for handler methods are displayed

This commit is contained in:
Martin Lippert
2018-03-08 09:54:10 +01:00
parent 069323b7f0
commit 57b83da757
5 changed files with 84 additions and 33 deletions

View File

@@ -71,7 +71,14 @@ public class WebfluxHandlerCodeLensProvider implements CodeLensProvider {
CodeLens codeLens = new CodeLens();
codeLens.setRange(document.toRange(node.getName().getStartPosition(), node.getName().getLength()));
codeLens.setCommand(new Command(handlerInfo.getSymbol(), null));
String codeLensCommand = handlerInfo.getHttpMethod() != null ? handlerInfo.getHttpMethod() + " " : "";
codeLensCommand += handlerInfo.getPath();
codeLensCommand += handlerInfo.getContentType() != null ? " Content-Type: " + handlerInfo.getContentType() : "";
codeLensCommand += handlerInfo.getAcceptType() != null ? " Accept: " + handlerInfo.getAcceptType() : "";
codeLens.setCommand(new Command(codeLensCommand, null));
resultAccumulator.add(codeLens);
} catch (BadLocationException e) {

View File

@@ -15,18 +15,22 @@ package org.springframework.ide.vscode.boot.java.requestmapping;
*/
public class WebfluxHandlerInformation {
private final String symbol;
private String handlerClass;
private String handlerMethod;
private final String handlerClass;
private final String handlerMethod;
public WebfluxHandlerInformation(String symbol, String handlerClass, String handlerMethod) {
this.symbol = symbol;
private final String path;
private final String httpMethod;
private final String contentType;
private final String acceptType;
public WebfluxHandlerInformation(String handlerClass, String handlerMethod, String path, String httpMethod, String contentType, String acceptType) {
this.handlerClass = handlerClass;
this.handlerMethod = handlerMethod;
}
public String getSymbol() {
return symbol;
this.path = path;
this.httpMethod = httpMethod;
this.contentType = contentType;
this.acceptType = acceptType;
}
public String getHandlerClass() {
@@ -36,5 +40,21 @@ public class WebfluxHandlerInformation {
public String getHandlerMethod() {
return handlerMethod;
}
public String getPath() {
return path;
}
public String getHttpMethod() {
return httpMethod;
}
public String getContentType() {
return contentType;
}
public String getAcceptType() {
return acceptType;
}
}

View File

@@ -91,6 +91,9 @@ public class WebfluxRouterSymbolProvider implements SymbolProvider {
String path = extractPath(node, foundPath);
String httpMethod = extractMethod(node);
String contentType = null;
String acceptType = null;
int methodNameStart = node.getName().getStartPosition();
int invocationStart = node.getStartPosition();
@@ -99,7 +102,7 @@ public class WebfluxRouterSymbolProvider implements SymbolProvider {
Location location = new Location(doc.getUri(), doc.toRange(methodNameStart, node.getLength() - (methodNameStart - invocationStart)));
String label = "@" + (path.startsWith("/") ? path : ("/" + path)) + (httpMethod == null || httpMethod.isEmpty() ? "" : " -- " + httpMethod);
WebfluxHandlerInformation handler = extractHandlerInformation(node, label);
WebfluxHandlerInformation handler = extractHandlerInformation(node, path, httpMethod, contentType, acceptType);
result.add(new EnhancedSymbolInformation(new SymbolInformation(label, SymbolKind.Interface, location), handler));
} catch (BadLocationException e) {
@@ -161,7 +164,7 @@ public class WebfluxRouterSymbolProvider implements SymbolProvider {
return method;
}
private WebfluxHandlerInformation extractHandlerInformation(MethodInvocation node, String symbol) {
private WebfluxHandlerInformation extractHandlerInformation(MethodInvocation node, String path, String httpMethod, String contentType, String acceptType) {
List<?> arguments = node.arguments();
if (arguments != null) {
@@ -177,7 +180,7 @@ public class WebfluxRouterSymbolProvider implements SymbolProvider {
String handlerMethod = methodBinding.getMethodDeclaration().toString();
if (handlerMethod != null) handlerMethod = handlerMethod.trim();
return new WebfluxHandlerInformation(symbol, handlerClass, handlerMethod);
return new WebfluxHandlerInformation(handlerClass, handlerMethod, path, httpMethod, contentType, acceptType);
}
}
}

View File

@@ -53,10 +53,10 @@ public class WebFluxCodeLensProviderTest {
assertEquals(4, codeLenses.size());
assertTrue(containsCodeLens(codeLenses, "@/hello -- GET", 25, 29, 25, 34));
assertTrue(containsCodeLens(codeLenses, "@/echo -- POST", 30, 29, 30, 33));
assertTrue(containsCodeLens(codeLenses, "@/quotes -- GET", 35, 29, 35, 41));
assertTrue(containsCodeLens(codeLenses, "@/quotes -- GET", 41, 29, 41, 40));
assertTrue(containsCodeLens(codeLenses, "GET /hello", 25, 29, 25, 34));
assertTrue(containsCodeLens(codeLenses, "POST /echo", 30, 29, 30, 33));
assertTrue(containsCodeLens(codeLenses, "GET /quotes", 35, 29, 35, 41));
assertTrue(containsCodeLens(codeLenses, "GET /quotes", 41, 29, 41, 40));
}
private boolean containsCodeLens(List<? extends CodeLens> codeLenses, String commandTitle, int startLine, int startPosition, int endLine, int endPosition) {

View File

@@ -69,23 +69,35 @@ public class WebFluxMappingSymbolProviderTest {
List<? extends Object> addons = getAdditionalInformation(docUri);
assertEquals(4, addons.size());
WebfluxHandlerInformation handlerInfo1 = getWebfluxHandler(addons, "@/hello -- GET").get(0);
assertEquals("@/hello -- GET", handlerInfo1.getSymbol());
WebfluxHandlerInformation handlerInfo1 = getWebfluxHandler(addons, "/hello", "GET").get(0);
assertEquals("/hello", handlerInfo1.getPath());
assertEquals("GET", handlerInfo1.getHttpMethod());
assertNull(handlerInfo1.getContentType());
assertNull(handlerInfo1.getAcceptType());
assertEquals("org.test.QuoteHandler", handlerInfo1.getHandlerClass());
assertEquals("public Mono<org.springframework.web.reactive.function.server.ServerResponse> hello(org.springframework.web.reactive.function.server.ServerRequest)", handlerInfo1.getHandlerMethod());
WebfluxHandlerInformation handlerInfo2 = getWebfluxHandler(addons, "@/echo -- POST").get(0);
assertEquals("@/echo -- POST", handlerInfo2.getSymbol());
WebfluxHandlerInformation handlerInfo2 = getWebfluxHandler(addons, "/echo", "POST").get(0);
assertEquals("/echo", handlerInfo2.getPath());
assertEquals("POST", handlerInfo2.getHttpMethod());
assertNull(handlerInfo2.getContentType());
assertNull(handlerInfo2.getAcceptType());
assertEquals("org.test.QuoteHandler", handlerInfo2.getHandlerClass());
assertEquals("public Mono<org.springframework.web.reactive.function.server.ServerResponse> echo(org.springframework.web.reactive.function.server.ServerRequest)", handlerInfo2.getHandlerMethod());
WebfluxHandlerInformation handlerInfo3 = getWebfluxHandler(addons, "@/quotes -- GET").get(0);
assertEquals("@/quotes -- GET", handlerInfo3.getSymbol());
WebfluxHandlerInformation handlerInfo3 = getWebfluxHandler(addons, "/quotes", "GET").get(0);
assertEquals("/quotes", handlerInfo3.getPath());
assertEquals("GET", handlerInfo3.getHttpMethod());
assertNull(handlerInfo3.getContentType());
assertNull(handlerInfo3.getAcceptType());
assertEquals("org.test.QuoteHandler", handlerInfo3.getHandlerClass());
assertEquals("public Mono<org.springframework.web.reactive.function.server.ServerResponse> streamQuotes(org.springframework.web.reactive.function.server.ServerRequest)", handlerInfo3.getHandlerMethod());
WebfluxHandlerInformation handlerInfo4 = getWebfluxHandler(addons, "@/quotes -- GET").get(1);
assertEquals("@/quotes -- GET", handlerInfo4.getSymbol());
WebfluxHandlerInformation handlerInfo4 = getWebfluxHandler(addons, "/quotes", "GET").get(1);
assertEquals("/quotes", handlerInfo4.getPath());
assertEquals("GET", handlerInfo4.getHttpMethod());
assertNull(handlerInfo4.getContentType());
assertNull(handlerInfo4.getAcceptType());
assertEquals("org.test.QuoteHandler", handlerInfo4.getHandlerClass());
assertEquals("public Mono<org.springframework.web.reactive.function.server.ServerResponse> fetchQuotes(org.springframework.web.reactive.function.server.ServerRequest)", handlerInfo4.getHandlerMethod());
}
@@ -105,18 +117,27 @@ public class WebFluxMappingSymbolProviderTest {
List<? extends Object> addons = getAdditionalInformation(docUri);
assertEquals(3, addons.size());
WebfluxHandlerInformation handlerInfo1 = getWebfluxHandler(addons, "@/person/{id} -- GET").get(0);
assertEquals("@/person/{id} -- GET", handlerInfo1.getSymbol());
WebfluxHandlerInformation handlerInfo1 = getWebfluxHandler(addons, "/person/{id}", "GET").get(0);
assertEquals("/person/{id}", handlerInfo1.getPath());
assertEquals("GET", handlerInfo1.getHttpMethod());
assertNull(handlerInfo1.getContentType());
assertNull(handlerInfo1.getAcceptType());
assertEquals("org.test.PersonHandler", handlerInfo1.getHandlerClass());
assertEquals("public Mono<org.springframework.web.reactive.function.server.ServerResponse> getPerson(org.springframework.web.reactive.function.server.ServerRequest)", handlerInfo1.getHandlerMethod());
WebfluxHandlerInformation handlerInfo2 = getWebfluxHandler(addons, "@/person/ -- POST").get(0);
assertEquals("@/person/ -- POST", handlerInfo2.getSymbol());
WebfluxHandlerInformation handlerInfo2 = getWebfluxHandler(addons, "/person/", "POST").get(0);
assertEquals("/person/", handlerInfo2.getPath());
assertEquals("POST", handlerInfo2.getHttpMethod());
assertNull(handlerInfo2.getContentType());
assertNull(handlerInfo2.getAcceptType());
assertEquals("org.test.PersonHandler", handlerInfo2.getHandlerClass());
assertEquals("public Mono<org.springframework.web.reactive.function.server.ServerResponse> createPerson(org.springframework.web.reactive.function.server.ServerRequest)", handlerInfo2.getHandlerMethod());
WebfluxHandlerInformation handlerInfo3 = getWebfluxHandler(addons, "@/person -- GET").get(0);
assertEquals("@/person -- GET", handlerInfo3.getSymbol());
WebfluxHandlerInformation handlerInfo3 = getWebfluxHandler(addons, "/person", "GET").get(0);
assertEquals("/person", handlerInfo3.getPath());
assertEquals("GET", handlerInfo3.getHttpMethod());
assertNull(handlerInfo3.getContentType());
assertNull(handlerInfo3.getAcceptType());
assertEquals("org.test.PersonHandler", handlerInfo3.getHandlerClass());
assertEquals("public Mono<org.springframework.web.reactive.function.server.ServerResponse> listPeople(org.springframework.web.reactive.function.server.ServerRequest)", handlerInfo3.getHandlerMethod());
}
@@ -146,11 +167,11 @@ public class WebFluxMappingSymbolProviderTest {
return harness.getServerWrapper().getComponents().getSpringIndexer().getAdditonalInformation(docUri);
}
private List<WebfluxHandlerInformation> getWebfluxHandler(List<? extends Object> addons, String symbol) {
private List<WebfluxHandlerInformation> getWebfluxHandler(List<? extends Object> addons, String path, String httpMethod) {
return addons.stream()
.filter((obj) -> obj instanceof WebfluxHandlerInformation)
.map((obj -> (WebfluxHandlerInformation) obj))
.filter((addon) -> addon.getSymbol().equals(symbol))
.filter((addon) -> addon.getPath().equals(path) && addon.getHttpMethod().equals(httpMethod))
.collect(Collectors.toList());
}