Skip to content

Commit

Permalink
Issue 522 - Allow custom notifications
Browse files Browse the repository at this point in the history
Fixes #522
Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny committed Mar 7, 2018
1 parent f14cc54 commit c51f798
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.util.List;

import javax.xml.ws.Endpoint;

import org.eclipse.jdt.ls.core.internal.handlers.LogHandler;
import org.eclipse.jdt.ls.core.internal.lsp.WorkspaceFoldersProposedClient;
import org.eclipse.lsp4j.ApplyWorkspaceEditParams;
Expand Down Expand Up @@ -46,6 +48,14 @@ public interface JavaLanguageClient extends LanguageClient, WorkspaceFoldersProp
@JsonNotification("language/actionableNotification")
void sendActionableNotification(ActionableNotification notification);

/**
* Sends the event to the client.
*
* @param method
* @param event
*/
@JsonNotification("language/event")
public void notify(String method, Object parameter);
}

private final LogHandler logHandler;
Expand Down Expand Up @@ -163,4 +173,13 @@ public void disconnect() {
}
}

/**
* Sends the event to the client.
*
* @param method
* @param event
*/
public void notify(String method, Object parameter) {
client.notify(method, parameter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,7 @@ public JavaClientConnection getClientConnection() {
return client;
}

public void notify(String method, Object parameter) {
client.notify(method, parameter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*******************************************************************************
* Copyright (c) 2017 Red Hat Inc. and others.
* 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:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal;

import static org.junit.Assert.assertEquals;

import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IElementChangedListener;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ls.core.internal.handlers.DocumentLifeCycleHandler;
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences.Severity;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class JavaModelEventProviderTest extends AbstractProjectsManagerBasedTest {
private DocumentLifeCycleHandler lifeCycleHandler;
private JavaClientConnection javaClient;

@Before
public void setup() throws Exception {
mockPreferences();

javaClient = new JavaClientConnection(client);
lifeCycleHandler = new DocumentLifeCycleHandler(javaClient, preferenceManager, projectsManager, false);
}

@After
public void tearDown() throws Exception {
javaClient.disconnect();
for (ICompilationUnit cu : JavaCore.getWorkingCopies(null)) {
cu.discardWorkingCopy();
}
}

private Preferences mockPreferences() {
Preferences mockPreferences = Mockito.mock(Preferences.class);
Mockito.when(preferenceManager.getPreferences()).thenReturn(mockPreferences);
Mockito.when(preferenceManager.getPreferences(Mockito.any())).thenReturn(mockPreferences);
Mockito.when(mockPreferences.getIncompleteClasspathSeverity()).thenReturn(Severity.ignore);
return mockPreferences;
}

@Test
public void testSendNotification() throws Exception {
JavaCore.addElementChangedListener(new IElementChangedListener() {

@Override
public void elementChanged(ElementChangedEvent event) {
client.notify("notify", event);
}
});
IJavaProject javaProject = newDefaultProject();
IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
IPackageFragment pack1 = sourceFolder.createPackageFragment("java", false, null);

// @formatter:off
String standaloneFileContent =
"package java;\n"+
"public class Foo extends UnknownType {"+
" public void method1(){\n"+
" super.whatever();"+
" }\n"+
"}";
// @formatter:on
ICompilationUnit cu1 = pack1.createCompilationUnit("Foo.java", standaloneFileContent, false, null);

List<ElementChangedEvent> diagnosticReports = getClientRequests("notify");
assertEquals(7, diagnosticReports.size());
}

@SuppressWarnings("unchecked")
private <T> List<T> getClientRequests(String name) {
List<?> requests = clientRequests.get(name);
return requests != null ? (List<T>) requests : Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
clientRequests.put(name, params);
}
params.add(args[0]);
} else if (args.length == 2 && "notify".equals(method.getName())) {
String name = method.getName();
List<Object> params = clientRequests.get(name);
if (params == null) {
params = new ArrayList<>();
clientRequests.put(name, params);
}
params.add(args[1]);
}
return null;
}
Expand Down

0 comments on commit c51f798

Please sign in to comment.