use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeActionHandler method textEditToCommand.
private static Command textEditToCommand(ICompilationUnit unit, String label, TextEdit textEdit) {
TextEditConverter converter = new TextEditConverter(unit, textEdit);
String uri = JDTUtils.toURI(unit);
WorkspaceEdit $ = new WorkspaceEdit();
$.getChanges().put(uri, converter.convert());
return new Command(label, COMMAND_ID_APPLY_EDIT, Arrays.asList($));
}
use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.
the class CodeLensHandlerTest method testResolveImplementationsCodeLense.
@SuppressWarnings("unchecked")
@Test
public void testResolveImplementationsCodeLense() {
String source = "src/java/IFoo.java";
String payload = createCodeLensImplementationsRequest(source, 5, 17, 21);
CodeLens lens = getParams(payload);
Range range = lens.getRange();
assertRange(5, 17, 21, range);
CodeLens result = handler.resolve(lens, monitor);
assertNotNull(result);
// Check if command found
Command command = result.getCommand();
assertNotNull(command);
assertEquals("2 implementations", command.getTitle());
assertEquals("java.show.implementations", command.getCommand());
// Check codelens args
List<Object> args = command.getArguments();
assertEquals(3, args.size());
// Check we point to the Bar class
String sourceUri = args.get(0).toString();
assertTrue(sourceUri.endsWith("IFoo.java"));
// CodeLens position
Position p = (Position) args.get(1);
assertEquals(5, p.getLine());
assertEquals(17, p.getCharacter());
// Reference location
List<Location> locations = (List<Location>) args.get(2);
assertEquals(2, locations.size());
Location loc = locations.get(0);
assertTrue(loc.getUri().endsWith("src/java/Foo2.java"));
assertRange(5, 13, 17, loc.getRange());
}
use of org.eclipse.lsp4j.Command in project ballerina by ballerina-lang.
the class CommandUtil method getCommandsByDiagnostic.
/**
* Get the command instances for a given diagnostic.
* @param diagnostic Diagnostic to get the command against
* @param params Code Action parameters
* @param lsPackageCache Lang Server Package cache
* @return {@link List} List of commands related to the given diagnostic
*/
public static List<Command> getCommandsByDiagnostic(Diagnostic diagnostic, CodeActionParams params, LSPackageCache lsPackageCache) {
String diagnosticMessage = diagnostic.getMessage();
List<Command> commands = new ArrayList<>();
if (isUndefinedPackage(diagnosticMessage)) {
String packageAlias = diagnosticMessage.substring(diagnosticMessage.indexOf("'") + 1, diagnosticMessage.lastIndexOf("'"));
LSDocument sourceDocument = new LSDocument(params.getTextDocument().getUri());
Path openedPath = CommonUtil.getPath(sourceDocument);
String sourceRoot = TextDocumentServiceUtil.getSourceRoot(openedPath);
sourceDocument.setSourceRoot(sourceRoot);
lsPackageCache.getPackageMap().entrySet().stream().filter(pkgEntry -> {
String fullPkgName = pkgEntry.getValue().packageID.orgName.getValue() + "/" + pkgEntry.getValue().packageID.getName().getValue();
return fullPkgName.endsWith("." + packageAlias) || fullPkgName.endsWith("/" + packageAlias);
}).forEach(pkgEntry -> {
PackageID packageID = pkgEntry.getValue().packageID;
String commandTitle = CommandConstants.IMPORT_PKG_TITLE + " " + packageID.getName().toString();
String fullPkgName = packageID.getOrgName() + "/" + packageID.getName().getValue();
CommandArgument pkgArgument = new CommandArgument(CommandConstants.ARG_KEY_PKG_NAME, fullPkgName);
CommandArgument docUriArgument = new CommandArgument(CommandConstants.ARG_KEY_DOC_URI, params.getTextDocument().getUri());
commands.add(new Command(commandTitle, CommandConstants.CMD_IMPORT_PACKAGE, new ArrayList<>(Arrays.asList(pkgArgument, docUriArgument))));
});
}
return commands;
}
use of org.eclipse.lsp4j.Command in project sts4 by spring-projects.
the class LanguageServerHarness method getCodeActions.
public List<CodeAction> getCodeActions(TextDocumentInfo doc, Diagnostic problem) throws Exception {
CodeActionContext context = new CodeActionContext(ImmutableList.of(problem));
List<? extends Command> actions = getServer().getTextDocumentService().codeAction(new CodeActionParams(doc.getId(), problem.getRange(), context)).get();
return actions.stream().map((command) -> new CodeAction(this, command)).collect(Collectors.toList());
}
use of org.eclipse.lsp4j.Command in project sts4 by spring-projects.
the class WebFluxCodeLensProviderTest method containsCodeLens.
private boolean containsCodeLens(List<? extends CodeLens> codeLenses, String commandTitle, int startLine, int startPosition, int endLine, int endPosition) {
for (CodeLens codeLens : codeLenses) {
Command command = codeLens.getCommand();
Range range = codeLens.getRange();
if (command.getTitle().equals(commandTitle) && range.getStart().getLine() == startLine && range.getStart().getCharacter() == startPosition && range.getEnd().getLine() == endLine && range.getEnd().getCharacter() == endPosition) {
return true;
}
}
return false;
}
Aggregations