aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkwannheden2009-10-16 03:50:08 (EDT)
committersefftinge2009-10-16 03:50:08 (EDT)
commitba953733433cb5016e65678d5a2c732196966122 (patch)
tree0825c30ee1058a099ba7219152e3e2aaf3822fcb
parent11157c88904d84e09caa10a724e76ff98c4e167b (diff)
downloadorg.eclipse.xtext-ba953733433cb5016e65678d5a2c732196966122.zip
org.eclipse.xtext-ba953733433cb5016e65678d5a2c732196966122.tar.gz
org.eclipse.xtext-ba953733433cb5016e65678d5a2c732196966122.tar.bz2
Fix: determine quickfix context object by offset - https://bugs.eclipse.org/bugs/show_bug.cgi?id=292498
-rw-r--r--plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/quickfix/AbstractDeclarativeQuickfixProvider.java41
1 files changed, 27 insertions, 14 deletions
diff --git a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/quickfix/AbstractDeclarativeQuickfixProvider.java b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/quickfix/AbstractDeclarativeQuickfixProvider.java
index db0ef60..abbdfc2 100644
--- a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/quickfix/AbstractDeclarativeQuickfixProvider.java
+++ b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/editor/quickfix/AbstractDeclarativeQuickfixProvider.java
@@ -21,7 +21,12 @@ import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.ui.IMarkerResolutionGenerator2;
+import org.eclipse.ui.texteditor.MarkerUtilities;
import org.eclipse.xtext.concurrent.IUnitOfWork;
+import org.eclipse.xtext.parser.IParseResult;
+import org.eclipse.xtext.parsetree.AbstractNode;
+import org.eclipse.xtext.parsetree.NodeUtil;
+import org.eclipse.xtext.parsetree.ParseTreeUtil;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.core.IImageHelper;
import org.eclipse.xtext.ui.core.editor.model.IXtextDocument;
@@ -60,16 +65,23 @@ public class AbstractDeclarativeQuickfixProvider implements IMarkerResolutionGen
return XtextDocumentUtil.get(marker.getResource());
}
- protected URI getURI(final IMarker marker) {
- String uri = marker.getAttribute(EValidator.URI_ATTRIBUTE, null);
- return uri == null ? null : URI.createURI(uri);
- }
-
protected Integer getCode(final IMarker marker) {
Integer code = marker.getAttribute(IXtextResourceChecker.CODE_KEY, -1);
return code;
}
+ protected EObject getContext(XtextResource resource, final IMarker marker) {
+ IParseResult parseResult = resource.getParseResult();
+ if (parseResult != null && parseResult.getRootNode() != null) {
+ AbstractNode lastVisibleNode = ParseTreeUtil.getLastCompleteNodeByOffset(parseResult.getRootNode(),
+ MarkerUtilities.getCharStart(marker));
+ if (lastVisibleNode != null)
+ return NodeUtil.getNearestSemanticObject(lastVisibleNode);
+ }
+ String uri = marker.getAttribute(EValidator.URI_ATTRIBUTE, null);
+ return uri == null ? null : resource.getEObject(URI.createURI(uri).fragment());
+ }
+
protected EObject getContext(final IMarker marker) {
final IXtextDocument document = getDocument(marker);
if (document == null)
@@ -77,8 +89,7 @@ public class AbstractDeclarativeQuickfixProvider implements IMarkerResolutionGen
final EObject context = document.readOnly(new IUnitOfWork<EObject, XtextResource>() {
public EObject exec(XtextResource state) throws Exception {
- URI uri = getURI(marker);
- return uri != null ? state.getEObject(uri.fragment()) : null;
+ return getContext(state, marker);
}
});
return context;
@@ -128,7 +139,7 @@ public class AbstractDeclarativeQuickfixProvider implements IMarkerResolutionGen
public Image getImage() {
String image = annotation.image();
- return image != null ? getImageHelper().getImage(image) : null;
+ return AbstractDeclarativeQuickfixProvider.this.getImage(image);
}
public String getDescription() {
@@ -139,12 +150,15 @@ public class AbstractDeclarativeQuickfixProvider implements IMarkerResolutionGen
}).toArray(new IMarkerResolution[fixMethods.size()]);
}
+ protected Image getImage(String image) {
+ return image != null ? getImageHelper().getImage(image) : null;
+ }
+
protected void executeFixMethod(final Method method, final IMarker marker) {
IXtextDocument document = getDocument(marker);
- documentEditor.process(new IUnitOfWork<Void, XtextResource>() {
+ getDocumentEditor().process(new IUnitOfWork<Void, XtextResource>() {
public java.lang.Void exec(XtextResource state) throws Exception {
- URI uri = getURI(marker);
- EObject context = state.getEObject(uri.fragment());
+ EObject context = getContext(state, marker);
method.invoke(AbstractDeclarativeQuickfixProvider.this, new Object[] { context, marker });
return null;
}
@@ -178,11 +192,10 @@ public class AbstractDeclarativeQuickfixProvider implements IMarkerResolutionGen
try {
if (!marker.isSubtypeOf(EValidator.MARKER))
return false;
- }
- catch (CoreException e) {
+ } catch (CoreException e) {
return false;
}
- if (getURI(marker) == null || getCode(marker) == null)
+ if (getCode(marker) == null)
return false;
List<Method> fixMethods = getFixMethods(marker);