Skip to content

Commit

Permalink
CHE-5654. Activate java reconciling when classpath is changed
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
  • Loading branch information
RomanNikitenko committed Aug 2, 2017
1 parent 65c2201 commit 5366375
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.che.ide.ext.java.client.editor;

import com.google.common.base.Optional;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.google.web.bindery.event.shared.EventBus;
Expand All @@ -28,7 +27,8 @@
import org.eclipse.che.ide.api.resources.VirtualFile;
import org.eclipse.che.ide.ext.java.client.JavaLocalizationConstant;
import org.eclipse.che.ide.ext.java.client.editor.ReconcileOperationEvent.ReconcileOperationHandler;
import org.eclipse.che.ide.ext.java.client.util.JavaUtil;
import org.eclipse.che.ide.ext.java.client.project.classpath.ClasspathChangedEvent;
import org.eclipse.che.ide.ext.java.client.project.classpath.ClasspathChangedEvent.ClasspathChangedHandler;
import org.eclipse.che.ide.ext.java.shared.dto.HighlightedPosition;
import org.eclipse.che.ide.ext.java.shared.dto.Problem;
import org.eclipse.che.ide.ext.java.shared.dto.ReconcileResult;
Expand All @@ -43,9 +43,11 @@
import java.util.HashSet;
import java.util.List;

import static org.eclipse.che.ide.ext.java.client.util.JavaUtil.resolveFQN;
import static org.eclipse.che.ide.project.ResolvingProjectStateHolder.ResolvingProjectState.IN_PROGRESS;

public class JavaReconcilerStrategy implements ReconcilingStrategy, ResolvingProjectStateListener, ReconcileOperationHandler {
public class JavaReconcilerStrategy
implements ReconcilingStrategy, ResolvingProjectStateListener, ReconcileOperationHandler, ClasspathChangedHandler {
private final TextEditor editor;
private final JavaCodeAssistProcessor codeAssistProcessor;
private final AnnotationModel annotationModel;
Expand Down Expand Up @@ -82,29 +84,30 @@ public JavaReconcilerStrategy(@Assisted @NotNull final TextEditor editor,

HandlerRegistration reconcileOperationHandlerRegistration = eventBus.addHandler(ReconcileOperationEvent.TYPE, this);
handlerRegistrations.add(reconcileOperationHandlerRegistration);

HandlerRegistration classpathChangedHandlerRegistration = eventBus.addHandler(ClasspathChangedEvent.TYPE, this);
handlerRegistrations.add(classpathChangedHandlerRegistration);
}

@Override
public void setDocument(final Document document) {
highlighter.init(editor.getEditorWidget(), document);

if (getFile() instanceof Resource) {
final Optional<Project> project = ((Resource)getFile()).getRelatedProject();

if (!project.isPresent()) {
return;
}
VirtualFile file = getFile();
Project project = getProject(file);
if (project == null) {
return;
}

String projectType = project.get().getType();
resolvingProjectStateHolder = resolvingProjectStateHolderRegistry.getResolvingProjectStateHolder(projectType);
if (resolvingProjectStateHolder == null) {
return;
}
resolvingProjectStateHolder.addResolvingProjectStateListener(this);
String projectType = project.getType();
resolvingProjectStateHolder = resolvingProjectStateHolderRegistry.getResolvingProjectStateHolder(projectType);
if (resolvingProjectStateHolder == null) {
return;
}
resolvingProjectStateHolder.addResolvingProjectStateListener(this);

if (isProjectResolving()) {
disableReconciler(localizationConstant.codeAssistErrorMessageResolvingProject());
}
if (isProjectResolving()) {
disableReconciler(localizationConstant.codeAssistErrorMessageResolvingProject());
}
}

Expand All @@ -114,35 +117,31 @@ public void reconcile(final DirtyRegion dirtyRegion, final Region subRegion) {
}

void parse() {
if (getFile() instanceof Resource) {
final Optional<Project> project = ((Resource)getFile()).getRelatedProject();
VirtualFile file = getFile();
Project project = getProject(file);
if (project == null) {
return;
}

String fqn = resolveFQN(file);
String projectPath = project.getPath();

if (!project.isPresent()) {
client.reconcile(fqn, projectPath).onSuccess(reconcileResult -> {
if (isProjectResolving()) {
disableReconciler(localizationConstant.codeAssistErrorMessageResolvingProject());
return;
} else {
codeAssistProcessor.enableCodeAssistant();
}

String fqn = JavaUtil.resolveFQN(getFile());
String projectPath = project.get().getLocation().toString();

client.reconcile(fqn, projectPath).onSuccess(reconcileResult -> {
if (isProjectResolving()) {
disableReconciler(localizationConstant.codeAssistErrorMessageResolvingProject());
return;
} else {
codeAssistProcessor.enableCodeAssistant();
}

if (reconcileResult == null) {
return;
}
if (reconcileResult == null) {
return;
}

doReconcile(reconcileResult.getProblems());
highlighter.reconcile(reconcileResult.getHighlightedPositions());
eventBus.fireEvent(new JavaReconsilerEvent(editor));
}).onFailure(jsonRpcError -> {
Log.info(getClass(), jsonRpcError.getMessage());
});
}
doReconcile(reconcileResult.getProblems());
highlighter.reconcile(reconcileResult.getHighlightedPositions());
eventBus.fireEvent(new JavaReconsilerEvent(editor));
}).onFailure(jsonRpcError -> Log.info(getClass(), jsonRpcError.getMessage()));
}


Expand All @@ -151,10 +150,19 @@ public void reconcile(final Region partition) {
parse();
}

public VirtualFile getFile() {
private VirtualFile getFile() {
return editor.getEditorInput().getFile();
}

private Project getProject(VirtualFile file) {
if (file == null || !(file instanceof Resource)) {
return null;
}

Project project = ((Resource)file).getProject();
return (project != null && project.exists()) ? project : null;
}

private void doReconcile(final List<Problem> problems) {
if (this.annotationModel == null) {
return;
Expand Down Expand Up @@ -247,4 +255,18 @@ public void onReconcileOperation(ReconcileResult reconcileResult) {
doReconcile(reconcileResult.getProblems());
highlighter.reconcile(reconcileResult.getHighlightedPositions());
}

@Override
public void onClasspathChanged(ClasspathChangedEvent event) {
VirtualFile file = getFile();
Project project = getProject(file);
if (project == null) {
return;
}

String projectPath = project.getPath();
if (projectPath.equals(event.getPath())) {
parse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.ext.java.client.JavaLocalizationConstant;
import org.eclipse.che.ide.ext.java.client.project.classpath.ClasspathChangedEvent;
import org.eclipse.che.ide.ext.java.shared.dto.HighlightedPosition;
import org.eclipse.che.ide.ext.java.shared.dto.ReconcileResult;
import org.eclipse.che.ide.project.ResolvingProjectStateHolder;
Expand Down Expand Up @@ -60,8 +61,9 @@
*/
@RunWith(MockitoJUnitRunner.class)
public class JavaReconcilerStrategyTest {
private static final String FILE_NAME = "TestClass.java";
private static final String FILE_PATH = "some/path/to/file/TestClass.java";
private static final String FILE_NAME = "TestClass.java";
private static final String FILE_PATH = "some/path/to/file/TestClass.java";
private static final String PROJECT_PATH = "some/path/to/project";

@Mock
private EventBus eventBus;
Expand Down Expand Up @@ -100,20 +102,18 @@ public class JavaReconcilerStrategyTest {
@Before
public void setUp() throws Exception {
EditorInput editorInput = mock(EditorInput.class);
Optional project = mock(Optional.class);
Project projectConfig = mock(Project.class);
Project project = mock(Project.class);
when(project.exists()).thenReturn(true);
Optional<Resource> srcFolder = mock(Optional.class);
Container startPoint = mock(Container.class);

when(editor.getEditorInput()).thenReturn(editorInput);
when(editorInput.getFile()).thenReturn(file);
when(file.getName()).thenReturn(FILE_NAME);
when(file.getRelatedProject()).thenReturn(project);
when(file.getProject()).thenReturn(project);
when(file.getLocation()).thenReturn(Path.valueOf(FILE_PATH));

when(project.get()).thenReturn(projectConfig);
when(projectConfig.getLocation()).thenReturn(Path.valueOf("some/path/to/project"));
when(project.isPresent()).thenReturn(true);
when(project.getPath()).thenReturn(PROJECT_PATH);
when(file.getParentWithMarker(any())).thenReturn(srcFolder);
when(srcFolder.isPresent()).thenReturn(true);
when(srcFolder.get()).thenReturn(startPoint);
Expand Down Expand Up @@ -195,4 +195,24 @@ public void shouldApplyReconcileResultAtReconcileOperation() throws Exception {
verify(codeAssistProcessor, never()).disableCodeAssistant(anyString());
verify(highlighter).reconcile(eq(positions));
}

@Test
public void shouldDoReconcileWhenClasspathIsChanged() throws Exception {
ClasspathChangedEvent event = mock(ClasspathChangedEvent.class);
when(event.getPath()).thenReturn(PROJECT_PATH);

javaReconcilerStrategy.onClasspathChanged(event);

verify(client).reconcile(anyString(), anyString());
}

@Test
public void shouldSkipReconcileWhenClasspathIsChangedForAnotherProject() throws Exception {
ClasspathChangedEvent event = mock(ClasspathChangedEvent.class);
when(event.getPath()).thenReturn("some/another/project");

javaReconcilerStrategy.onClasspathChanged(event);

verify(client, never()).reconcile(anyString(), anyString());
}
}

0 comments on commit 5366375

Please sign in to comment.