use of org.eclipse.lsp4j.InitializeParams in project xtext-core by eclipse.
the class WorkspaceFoldersTest method testInitialize.
@Test
public void testInitialize() throws Exception {
File rootFolder1 = temporaryFolder.newFolder("root1");
File rootFolder2 = temporaryFolder.newFolder("root2");
writeFile(rootFolder1, "one.testlang", "type Foo { Bar bar }");
String twoUri = writeFile(rootFolder2, "two.testlang", "type Bar { Foo foo }");
initialize((InitializeParams it) -> {
it.setWorkspaceFolders(Lists.newArrayList(new WorkspaceFolder(uriExtensions.toUriString(rootFolder1.toURI()), "root1"), new WorkspaceFolder(uriExtensions.toUriString(rootFolder2.toURI()), "root2")));
});
Assert.assertEquals(2, getDiagnostics().size());
Assert.assertEquals(1, getDiagnostics().get(twoUri).size());
withBuild(() -> {
DidChangeWorkspaceFoldersParams didChangeWorkspaceFoldersParams = new DidChangeWorkspaceFoldersParams();
WorkspaceFoldersChangeEvent workspaceFoldersChangeEvent = new WorkspaceFoldersChangeEvent();
workspaceFoldersChangeEvent.setRemoved(Lists.newArrayList(new WorkspaceFolder(uriExtensions.toUriString(rootFolder2.toURI()), "root2")));
didChangeWorkspaceFoldersParams.setEvent(workspaceFoldersChangeEvent);
languageServer.didChangeWorkspaceFolders(didChangeWorkspaceFoldersParams);
});
Assert.assertEquals(0, getDiagnostics().get(twoUri).size());
withBuild(() -> {
DidChangeWorkspaceFoldersParams didChangeWorkspaceFoldersParams = new DidChangeWorkspaceFoldersParams();
WorkspaceFoldersChangeEvent workspaceFoldersChangeEvent = new WorkspaceFoldersChangeEvent();
workspaceFoldersChangeEvent.setAdded(Lists.newArrayList(new WorkspaceFolder(uriExtensions.toUriString(rootFolder2.toURI()), "root2")));
didChangeWorkspaceFoldersParams.setEvent(workspaceFoldersChangeEvent);
languageServer.didChangeWorkspaceFolders(didChangeWorkspaceFoldersParams);
});
Assert.assertEquals(1, getDiagnostics().get(twoUri).size());
}
use of org.eclipse.lsp4j.InitializeParams in project xtext-core by eclipse.
the class AbstractIdeQuickfixTest method quickfixesAreOffered.
private void quickfixesAreOffered(EObject target, String issueCode, String originalText, QuickfixExpectation... expected) {
List<QuickfixExpectation> expectedSorted = IterableExtensions.sortBy(Arrays.asList(expected), it -> it.label);
ICompositeNode elementNode = NodeModelUtils.getNode(target);
LineAndColumn elementStartPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getOffset());
LineAndColumn elementEndPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getEndOffset());
Position startPos = new Position(elementStartPosition.getLine() - 1, elementStartPosition.getColumn() - 1);
Position endPos = new Position(elementEndPosition.getLine() - 1, elementEndPosition.getColumn() - 1);
Diagnostic issue = new Diagnostic();
issue.setCode(issueCode);
issue.setMessage("error");
issue.setSeverity(DiagnosticSeverity.Error);
issue.setSource("source");
issue.setRange(new Range(startPos, endPos));
ICodeActionService2.Options options = new ICodeActionService2.Options();
options.setCancelIndicator(CancelIndicator.NullImpl);
options.setDocument(new Document(Integer.valueOf(0), originalText));
options.setResource((XtextResource) target.eResource());
options.setLanguageServerAccess(new ILanguageServerAccess() {
@Override
public void addBuildListener(ILanguageServerAccess.IBuildListener listener) {
throw new UnsupportedOperationException();
}
@Override
public <T extends Object> CompletableFuture<T> doRead(String uri, Function<ILanguageServerAccess.Context, T> function) {
ILanguageServerAccess.Context ctx = new ILanguageServerAccess.Context(options.getResource(), options.getDocument(), true, CancelIndicator.NullImpl);
return CompletableFuture.completedFuture(function.apply(ctx));
}
@Override
public <T extends Object> CompletableFuture<T> doReadIndex(Function<? super ILanguageServerAccess.IndexContext, ? extends T> function) {
return null;
}
@Override
public InitializeParams getInitializeParams() {
return null;
}
@Override
public InitializeResult getInitializeResult() {
return null;
}
@Override
public LanguageClient getLanguageClient() {
return null;
}
@Override
public ResourceSet newLiveScopeResourceSet(URI uri) {
// re-using the existing ResourceSet because it contains the URI protocol mapping for "inmemory" resources.
ResourceSet resourceSet = options.getResource().getResourceSet();
return resourceSet;
}
});
CodeActionParams codeActionParams = new CodeActionParams();
codeActionParams.setRange(new Range(startPos, endPos));
codeActionParams.setTextDocument(new TextDocumentIdentifier(target.eResource().getURI().toString()));
CodeActionContext codeActionContext = new CodeActionContext();
codeActionContext.setDiagnostics(Collections.singletonList(issue));
codeActionParams.setContext(codeActionContext);
options.setCodeActionParams(codeActionParams);
List<DiagnosticResolution> actualIssueResolutions = IterableExtensions.sortBy(quickFixProvider.getResolutions(options, issue), DiagnosticResolution::getLabel);
assertEquals("The number of quickfixes does not match!", expectedSorted.size(), actualIssueResolutions.size());
for (int i = 0; i < actualIssueResolutions.size(); i++) {
DiagnosticResolution actualIssueResolution = actualIssueResolutions.get(i);
QuickfixExpectation expectedIssueResolution = expectedSorted.get(i);
assertEquals(expectedIssueResolution.label, actualIssueResolution.getLabel());
assertEquals(expectedIssueResolution.description, actualIssueResolution.getLabel());
assertIssueResolutionResult(toUnixLineSeparator(expectedIssueResolution.getExpectedResult()), actualIssueResolution, originalText, options.getDocument());
}
}
use of org.eclipse.lsp4j.InitializeParams in project xtext-core by eclipse.
the class PrepareRenameTest method initializeWithPrepareSupport.
private InitializeResult initializeWithPrepareSupport() {
return super.initialize((InitializeParams params) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceClientCapabilities = new WorkspaceClientCapabilities();
clientCapabilities.setWorkspace(workspaceClientCapabilities);
TextDocumentClientCapabilities textDocumentClientCapabilities = new TextDocumentClientCapabilities();
textDocumentClientCapabilities.setRename(new RenameCapabilities(true, false));
clientCapabilities.setTextDocument(textDocumentClientCapabilities);
params.setCapabilities(clientCapabilities);
});
}
use of org.eclipse.lsp4j.InitializeParams in project xtext-core by eclipse.
the class LanguageServerImpl method initialize.
@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
if (initializeParams != null) {
throw new IllegalStateException("This language server has already been initialized.");
}
URI baseDir = getBaseDir(params);
if (languagesRegistry.getExtensionToFactoryMap().isEmpty()) {
throw new IllegalStateException("No Xtext languages have been registered. Please make sure you have added the languages\'s setup class in \'/META-INF/services/org.eclipse.xtext.ISetup\'");
}
initializeParams = params;
InitializeResult result = new InitializeResult();
result.setCapabilities(createServerCapabilities(params));
access.addBuildListener(this);
return requestManager.runWrite(() -> {
if (workspaceManager.isSupportsWorkspaceFolders()) {
List<WorkspaceFolder> workspaceFolders = params.getWorkspaceFolders();
if (workspaceFolders == null)
workspaceFolders = Collections.emptyList();
workspaceManager.initialize(workspaceFolders, this::publishDiagnostics, CancelIndicator.NullImpl);
} else {
workspaceManager.initialize(baseDir, this::publishDiagnostics, CancelIndicator.NullImpl);
}
return result;
}, (cancelIndicator, it) -> it).thenApply(it -> initializeResult = it);
}
use of org.eclipse.lsp4j.InitializeParams in project xtext-core by eclipse.
the class LanguageServerImpl method createServerCapabilities.
/**
* Configure the server capabilities for this instance.
*
* @param params
* the initialization parametrs
* @return the server capabilities
* @since 2.20
*/
protected ServerCapabilities createServerCapabilities(InitializeParams params) {
ServerCapabilities serverCapabilities = new ServerCapabilities();
serverCapabilities.setHoverProvider(true);
serverCapabilities.setDefinitionProvider(true);
serverCapabilities.setReferencesProvider(true);
serverCapabilities.setDocumentSymbolProvider(true);
serverCapabilities.setWorkspaceSymbolProvider(true);
Set<? extends IResourceServiceProvider> allLanguages = getAllLanguages();
if (allLanguages.stream().anyMatch((serviceProvider) -> serviceProvider.get(ICodeLensService.class) != null)) {
CodeLensOptions codeLensOptions = new CodeLensOptions();
codeLensOptions.setResolveProvider(allLanguages.stream().anyMatch((serviceProvider) -> serviceProvider.get(ICodeLensResolver.class) != null));
serverCapabilities.setCodeLensProvider(codeLensOptions);
}
serverCapabilities.setCodeActionProvider(allLanguages.stream().anyMatch((serviceProvider) -> serviceProvider.get(ICodeActionService2.class) != null));
serverCapabilities.setSignatureHelpProvider(new SignatureHelpOptions(ImmutableList.of("(", ",")));
serverCapabilities.setTextDocumentSync(TextDocumentSyncKind.Incremental);
CompletionOptions completionOptions = new CompletionOptions();
completionOptions.setResolveProvider(false);
completionOptions.setTriggerCharacters(ImmutableList.of("."));
serverCapabilities.setCompletionProvider(completionOptions);
serverCapabilities.setDocumentFormattingProvider(true);
serverCapabilities.setDocumentRangeFormattingProvider(true);
serverCapabilities.setDocumentHighlightProvider(true);
ClientCapabilities clientCapabilities = null;
if (params != null) {
clientCapabilities = params.getCapabilities();
}
TextDocumentClientCapabilities textDocument = null;
if (clientCapabilities != null) {
textDocument = clientCapabilities.getTextDocument();
}
RenameCapabilities rename = null;
FoldingRangeCapabilities folding = null;
if (textDocument != null) {
rename = textDocument.getRename();
folding = textDocument.getFoldingRange();
}
if (folding != null) {
serverCapabilities.setFoldingRangeProvider(allLanguages.stream().anyMatch(serviceProvider -> serviceProvider.get(FoldingRangeService.class) != null));
}
Boolean prepareSupport = null;
if (rename != null) {
prepareSupport = rename.getPrepareSupport();
}
boolean clientPrepareSupport = Objects.equal(Boolean.TRUE, prepareSupport);
if (clientPrepareSupport && allLanguages.stream().anyMatch(serviceProvider -> serviceProvider.get(IRenameService2.class) != null)) {
RenameOptions renameOptions = new RenameOptions();
renameOptions.setPrepareProvider(true);
serverCapabilities.setRenameProvider(Either.<Boolean, RenameOptions>forRight(renameOptions));
} else {
serverCapabilities.setRenameProvider(Either.forLeft(allLanguages.stream().anyMatch((serviceProvider) -> serviceProvider.get(IRenameService2.class) != null)));
}
WorkspaceClientCapabilities workspace = null;
if (clientCapabilities != null) {
workspace = clientCapabilities.getWorkspace();
}
ExecuteCommandCapabilities executeCommand = null;
if (workspace != null) {
executeCommand = workspace.getExecuteCommand();
if (workspace.getWorkspaceFolders() == Boolean.TRUE && workspaceManager.isSupportsWorkspaceFolders()) {
WorkspaceFoldersOptions workspaceFoldersOptions = new WorkspaceFoldersOptions();
workspaceFoldersOptions.setSupported(true);
workspaceFoldersOptions.setChangeNotifications(true);
serverCapabilities.setWorkspace(new WorkspaceServerCapabilities(workspaceFoldersOptions));
}
}
if (executeCommand != null) {
commandRegistry.initialize(allLanguages, clientCapabilities, client);
ExecuteCommandOptions executeCommandOptions = new ExecuteCommandOptions();
executeCommandOptions.setCommands(commandRegistry.getCommands());
serverCapabilities.setExecuteCommandProvider(executeCommandOptions);
}
for (IResourceServiceProvider language : allLanguages) {
ICapabilitiesContributor capabilitiesContributor = language.get(ICapabilitiesContributor.class);
if (capabilitiesContributor != null) {
capabilitiesContributor.contribute(serverCapabilities, params);
}
}
return serverCapabilities;
}
Aggregations