Skip to content

Commit

Permalink
[ui] Print a warning when the errors in the editor and the resources …
Browse files Browse the repository at this point in the history
…are not the same.

see #661

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 15, 2018
1 parent b8608b4 commit 56c77ad
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,12 @@ Workflow {
bind = "org.eclipse.xtext.ui.editor.IXtextEditorCallback"
to = "io.sarl.lang.ui.editor.SARLNatureAddingEditorCallback"
}
ui = {
bind = "org.eclipse.xtext.ui.editor.IXtextEditorCallback"
annotatedWithName = "IXtextEditorCallBack"
to = "io.sarl.lang.ui.editor.SARLEditorErrorTickUpdater"
functionName = "configureXtextEditorErrorTickUpdater"
}
}

// Xtend-extension bindings
Expand Down Expand Up @@ -1037,12 +1043,6 @@ Workflow {
to = "org.eclipse.xtend.ide.contentassist.antlr.FlexProposalConflictHelper"
override = true
}
ui = {
bind = "org.eclipse.xtext.ui.editor.IXtextEditorCallback"
annotatedWithName = "IXtextEditorCallBack"
to = "org.eclipse.xtend.ide.editor.XtendEditorErrorTickUpdater"
functionName = "configureXtextEditorErrorTickUpdater"
}
}

// Injection recommender for recommending missed injections in the SARL module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.sarl.lang.ui.contentassist.javadoc.SARLJavaDocContentAssistProcessor;
import io.sarl.lang.ui.contentassist.templates.SARLTemplateContextType;
import io.sarl.lang.ui.contentassist.templates.SARLTemplateProposalProvider;
import io.sarl.lang.ui.editor.SARLEditorErrorTickUpdater;
import io.sarl.lang.ui.editor.SARLNatureAddingEditorCallback;
import io.sarl.lang.ui.editor.SARLSourceViewer;
import io.sarl.lang.ui.editor.SARLStandardEditor;
Expand Down Expand Up @@ -111,7 +112,6 @@
import org.eclipse.xtend.ide.editor.RichStringAwareToggleCommentAction;
import org.eclipse.xtend.ide.editor.SingleLineCommentHelper;
import org.eclipse.xtend.ide.editor.XtendDoubleClickStrategyProvider;
import org.eclipse.xtend.ide.editor.XtendEditorErrorTickUpdater;
import org.eclipse.xtend.ide.editor.XtendSourceViewerConfiguration;
import org.eclipse.xtend.ide.hover.XtendAnnotationHover;
import org.eclipse.xtend.ide.hover.XtendHoverProvider;
Expand Down Expand Up @@ -593,6 +593,11 @@ public Class<? extends ISemanticHighlightingCalculator> bindIdeSemanticHighlight
return SARLHighlightingCalculator.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings provided by SARL API]
public void configureXtextEditorErrorTickUpdater(Binder binder) {
binder.bind(IXtextEditorCallback.class).annotatedWith(Names.named("IXtextEditorCallBack")).to(SARLEditorErrorTickUpdater.class);
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings provided by SARL API]
public void configureDiagnosticDecorator(Binder binder) {
binder.bind(ILabelDecorator.class).annotatedWith(Names.named("DiagnosticDecorator")).to(SARLDiagnosticLabelDecorator.class);
Expand Down Expand Up @@ -740,11 +745,6 @@ public Class<? extends XtextEditor> bindXtextEditor() {
return SARLStandardEditor.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings required by extended Xtend API]
public void configureXtextEditorErrorTickUpdater(Binder binder) {
binder.bind(IXtextEditorCallback.class).annotatedWith(Names.named("IXtextEditorCallBack")).to(XtendEditorErrorTickUpdater.class);
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings required by extended Xtend API]
public Class<? extends AbstractFileSystemSupport> bindAbstractFileSystemSupport() {
return EclipseFileSystemSupportImpl.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2018 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sarl.lang.ui.editor;

import org.eclipse.osgi.util.NLS;

/** Localized Messages.
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @ExcludeFromApidoc
*/
@SuppressWarnings("all")
public class Messages extends NLS {
private static final String BUNDLE_NAME = Messages.class.getPackage().getName() + ".messages"; //$NON-NLS-1$
public static String SARLEditorErrorTickUpdater_0;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}

private Messages() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2018 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sarl.lang.ui.editor;

import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.inject.Inject;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.xtend.ide.editor.XtendEditorErrorTickUpdater;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;
import org.eclipse.xtext.ui.resource.IStorage2UriMapper;
import org.eclipse.xtext.xbase.lib.util.ReflectExtensions;

import io.sarl.lang.ui.internal.LangActivator;

/** Updater of the error ticks into the SARL editor.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8.6
*/
public class SARLEditorErrorTickUpdater extends XtendEditorErrorTickUpdater {

private static final String MARKER_TYPE = "org.eclipse.xtext.ui.editor.error"; //$NON-NLS-1$

@Inject
private IResourceSetProvider resourceSetProvider;

@Inject
private IStorage2UriMapper uriMapper;

@Inject
private ReflectExtensions reflect;

/**
* Replies the editor that is linked to this updater.
*
* @return the linked editor, or {@code null} if no editor was linked.
*/
protected XtextEditor getEditor() {
// FIXME: Remove when Xbase is fixed.
try {
return (XtextEditor) this.reflect.get(this, "editor"); //$NON-NLS-1$
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}

/**
* Replies the current Xtext document that is supported by the linked editor.
*
* @return the Xtext document, or {@code null} if neither an editor nor a
* Xtext document was linked.
*/
protected Resource getXtextResource() {
//FIXME: for helping to resolve #661
final XtextEditor editor = getEditor();
if (editor != null) {
final IResource resource = editor.getResource();
if (resource instanceof IStorage) {
final IProject project = resource.getProject();
if (project != null) {
final ResourceSet resourceSet = this.resourceSetProvider.get(project);
assert resourceSet != null;
final URI uri = this.uriMapper.getUri((IStorage) resource);
return resourceSet.getResource(uri, true);
}
}
}
return null;
}

@Override
@SuppressWarnings("checkstyle:nestedifdepth")
public void modelChanged(IAnnotationModel model) {
super.modelChanged(model);
//FIXME: for helping to resolve #661
final XtextEditor editor = getEditor();
if (editor != null && editor.getInternalSourceViewer() != null) {
final IAnnotationModel currentModel = editor.getInternalSourceViewer().getAnnotationModel();
if (currentModel != null && currentModel == model) {
final Resource resource = getXtextResource();
if (isReconciliable(resource)) {
final Set<String> markers = extractErrorMarkerMessages(currentModel);
final List<Diagnostic> resourceErrors = resource.getErrors();
if (markers.size() != resourceErrors.size() || notSame(markers, resourceErrors)) {
final Display display = PlatformUI.getWorkbench().getDisplay();
display.asyncExec(new Runnable() {
@Override
public void run() {
LangActivator.getInstance().getLog().log(
new Status(IStatus.ERROR, LangActivator.PLUGIN_ID,
MessageFormat.format(Messages.SARLEditorErrorTickUpdater_0, resource.getURI())));
}
});
}
}
}
}
}

private static boolean isReconciliable(Resource resource) {
//FIXME: for helping to resolve #661
if (resource != null) {
return true;
}
return false;
}

private static boolean notSame(Set<String> markers, List<Diagnostic> diagnostics) {
//FIXME: for helping to resolve #661
for (final Diagnostic diag : diagnostics) {
if (!markers.contains(diag.getMessage())) {
return true;
}
}
return false;
}

private static Set<String> extractErrorMarkerMessages(IAnnotationModel currentModel) {
//FIXME: for helping to resolve #661
final Iterator<Annotation> annotations = currentModel.getAnnotationIterator();
final Set<String> errors = new TreeSet<>();
while (annotations.hasNext()) {
final Annotation annotation = annotations.next();
if (!annotation.isMarkedDeleted() && MARKER_TYPE.equals(annotation.getType())) {
errors.add(annotation.getText());
}
}
return errors;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SARLEditorErrorTickUpdater_0=Possible inconsistency problem between the editor and the resource {0}. Reconciliation of the editor is forced.

0 comments on commit 56c77ad

Please sign in to comment.