Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JAXRS code lens range to method name #317

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4mp.jdt.core.utils.IJDTUtils;

Expand Down Expand Up @@ -133,12 +134,18 @@ public static CodeLens createURLCodeLens(String baseURL, String rootPath, String
}

private static CodeLens createURLCodeLens(IMethod method, IJDTUtils utils) throws JavaModelException {
ISourceRange r = method.getNameRange();
if (r == null) {
IAnnotation[] annotations = method.getAnnotations();
if (annotations == null) {
return null;
}
ISourceRange r = annotations[annotations.length - 1].getSourceRange();

CodeLens lens = new CodeLens();
final Range range = utils.toRange(method.getOpenable(), r.getOffset(), r.getLength());
Range range = utils.toRange(method.getOpenable(), r.getOffset(), r.getLength());
// Increment line number for code lens to appear on the line right after the last annotation
Position codeLensPosition = new Position(range.getEnd().getLine() + 1, range.getEnd().getCharacter());
range.setStart(codeLensPosition);
range.setEnd(codeLensPosition);
lens.setRange(range);
return lens;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
}

@DELETE
@Path("{id}")
@Transactional
public Response delete(@PathParam Integer id) {
@Path(
"{id}"
)
public
Response
delete(@PathParam Integer id) {
Fruit entity = entityManager.getReference(Fruit.class, id);
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
}

@DELETE
@Path("{id}")
@Transactional
public Response delete(@PathParam Integer id) {
@Path(
"{id}"
)
public
Response
delete(@PathParam Integer id) {
Fruit entity = entityManager.getReference(Fruit.class, id);
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCodeLensParams;
import org.eclipse.lsp4mp.jdt.core.BasePropertiesManagerTest;
import org.eclipse.lsp4mp.jdt.core.PropertiesManagerForJava;
Expand Down Expand Up @@ -101,11 +103,22 @@ private static void assertCodeLenses(int port, MicroProfileJavaCodeLensParams pa
Assert.assertEquals("http://localhost:" + port + "/fruits/{id}", lensForPut.getCommand().getTitle());

//@DELETE
//@Path("{id}")
//public Response delete(@PathParam Integer id) {
//@Transactional
//@Path(
//"{id}"
//)
//--> code lens should appear here
//public
//Response
//delete(@PathParam Integer id) {
CodeLens lensForDelete = lenses.get(4);
Assert.assertNotNull(lensForDelete.getCommand());
Assert.assertEquals("http://localhost:" + port + "/fruits/{id}", lensForDelete.getCommand().getTitle());
Assert.assertEquals(r(81, 9, 81, 9), lensForDelete.getRange());
}

public static Range r(int startLine, int startCharacter, int endLine, int endCharacter) {
return new Range(new Position(startLine, startCharacter), new Position(endLine, endCharacter));
}

}