Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlena Laskavaia2011-03-19 14:15:57 -0400
committerAlena Laskavaia2011-03-19 14:15:57 -0400
commit9afbc979e69d3d9cbf508a46a2f602886c1916a3 (patch)
tree6136e69e424f4be9af0c33174111b095c1138783 /codan/org.eclipse.cdt.codan.ui.cxx
parent899b6159ac95b3155cac268b959bc605663a8d58 (diff)
downloadorg.eclipse.cdt-9afbc979e69d3d9cbf508a46a2f602886c1916a3.tar.gz
org.eclipse.cdt-9afbc979e69d3d9cbf508a46a2f602886c1916a3.tar.xz
org.eclipse.cdt-9afbc979e69d3d9cbf508a46a2f602886c1916a3.zip
- moved generic base class for ast rewrite quick fix to common plugins
Diffstat (limited to 'codan/org.eclipse.cdt.codan.ui.cxx')
-rw-r--r--codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/internal/ui/cxx/Activator.java32
-rw-r--r--codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/ui/AbstractAstRewriteQuickFix.java106
2 files changed, 138 insertions, 0 deletions
diff --git a/codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/internal/ui/cxx/Activator.java b/codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/internal/ui/cxx/Activator.java
index 60851c11b1..c4ec43b028 100644
--- a/codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/internal/ui/cxx/Activator.java
+++ b/codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/internal/ui/cxx/Activator.java
@@ -10,6 +10,8 @@
*******************************************************************************/
package org.eclipse.cdt.codan.internal.ui.cxx;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
@@ -60,4 +62,34 @@ public class Activator extends AbstractUIPlugin {
public static Activator getDefault() {
return plugin;
}
+
+ /**
+ * Logs the specified status with this plug-in's log.
+ *
+ * @param status
+ * status to log
+ */
+ public static void log(IStatus status) {
+ getDefault().getLog().log(status);
+ }
+
+ /**
+ * Logs an internal error with the specified throwable
+ *
+ * @param e
+ * the exception to be logged
+ */
+ public static void log(Throwable e) {
+ log(new Status(IStatus.ERROR, PLUGIN_ID, 1, "Internal Error", e)); //$NON-NLS-1$
+ }
+
+ /**
+ * Logs an internal error with the specified message.
+ *
+ * @param message
+ * the error message to log
+ */
+ public static void log(String message) {
+ log(new Status(IStatus.ERROR, PLUGIN_ID, 1, message, null));
+ }
}
diff --git a/codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/ui/AbstractAstRewriteQuickFix.java b/codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/ui/AbstractAstRewriteQuickFix.java
new file mode 100644
index 0000000000..e630398aba
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.ui.cxx/src/org/eclipse/cdt/codan/ui/AbstractAstRewriteQuickFix.java
@@ -0,0 +1,106 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Alena Laskavaia
+ * 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:
+ * Alena Laskavaia - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.ui;
+
+import org.eclipse.cdt.codan.internal.ui.cxx.Activator;
+import org.eclipse.cdt.core.dom.ast.IASTName;
+import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
+import org.eclipse.cdt.core.index.IIndex;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.FindReplaceDocumentAdapter;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+
+/**
+ *
+ * Abstract class to simply ast rewrite quick fixers
+ * @since 2.0
+ */
+public abstract class AbstractAstRewriteQuickFix extends AbstractCodanCMarkerResolution {
+ private IDocument document;
+
+ @Override
+ public void apply(final IMarker marker, IDocument document) {
+ try {
+ this.document = document;
+ openEditor(marker).doSave(new NullProgressMonitor());
+ IIndex index;
+ try {
+ index = getIndexFromMarker(marker);
+ } catch (CoreException e) {
+ Activator.log(e);
+ return;
+ }
+ // lock the index for read access
+ try {
+ index.acquireReadLock();
+ } catch (InterruptedException e) {
+ return;
+ }
+ try {
+ modifyAST(index, marker);
+ } finally {
+ index.releaseReadLock();
+ }
+ } catch (Exception e) {
+ Activator.log(e);
+ }
+ }
+
+ /**
+ *
+ * @param ast
+ * @param astName
+ * @param r
+ */
+ public abstract void modifyAST(IIndex index, IMarker marker);
+
+ /**
+ * @return the document
+ */
+ public IDocument getDocument() {
+ return document;
+ }
+
+ /**
+ * @param marker
+ * @param ast
+ * @param argumentIndex TODO
+ * @return
+ * @throws BadLocationException
+ */
+ public IASTName getAstNameFromProblemArgument(IMarker marker, IASTTranslationUnit ast, int argumentIndex) {
+ IASTName astName = null;
+ int pos = getOffset(marker, getDocument());
+ String name = null;
+ try {
+ name = getProblemArgument(marker, argumentIndex);
+ } catch (Exception e) {
+ return null;
+ }
+ if (name == null)
+ return null;
+ FindReplaceDocumentAdapter dad = new FindReplaceDocumentAdapter(getDocument());
+ IRegion region;
+ try {
+ region = dad.find(pos, name,
+ /* forwardSearch */true, /* caseSensitive */true,
+ /* wholeWord */true, /* regExSearch */false);
+ } catch (BadLocationException e) {
+ return null;
+ }
+ astName = getASTNameFromPositions(ast, region.getOffset(), region.getLength());
+ return astName;
+ }
+}

Back to the top