Jelajahi Sumber

API to parse UserHelp added to xtext utils

Andreas Mülder 13 tahun lalu
induk
melakukan
fba7d705cf

+ 3 - 1
de.itemis.xtext.utils/plugins/de.itemis.xtext.utils.jface/META-INF/MANIFEST.MF

@@ -11,8 +11,10 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.6.0,4.0.0)",
  org.eclipse.jface;bundle-version="[3.6.0,4.0.0)",
  org.eclipse.jface.text;bundle-version="[3.6.0,4.0.0)",
  org.eclipse.emf.common;bundle-version="[2.6.0,3.0.0)",
- org.eclipse.xtext.ui;bundle-version="[2.0.0,3.0.0)"
+ org.eclipse.xtext.ui;bundle-version="[2.0.0,3.0.0)",
+ org.eclipse.help;bundle-version="3.5.100"
 Export-Package: de.itemis.utils.jface.viewers,
+ de.itemis.utils.jface.viewers.help,
  de.itemis.xtext.utils.jface.fieldassist,
  de.itemis.xtext.utils.jface.viewers,
  de.itemis.xtext.utils.jface.viewers.context,

+ 83 - 0
de.itemis.xtext.utils/plugins/de.itemis.xtext.utils.jface/src/de/itemis/utils/jface/viewers/help/AbstractUserHelpDocumentationProvider.java

@@ -0,0 +1,83 @@
+/**
+ * Copyright (c) 2012 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package de.itemis.utils.jface.viewers.help;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Locale;
+import java.util.Map;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.help.IContext;
+import org.eclipse.help.IHelpResource;
+import org.eclipse.help.internal.context.ContextFileProvider;
+import org.eclipse.xtext.documentation.IEObjectDocumentationProvider;
+
+import com.google.common.collect.Maps;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+@SuppressWarnings("restriction")
+public abstract class AbstractUserHelpDocumentationProvider implements
+		IEObjectDocumentationProvider {
+
+	public static final String EMPTY_DOCUMENTATION = "";
+
+	private Map<String, String> helpContentCache;
+
+	public AbstractUserHelpDocumentationProvider() {
+		helpContentCache = Maps.newHashMap();
+	}
+
+	protected String getHelp(String contextId) {
+		if (helpContentCache.get(contextId) != null) {
+			return helpContentCache.get(contextId);
+		}
+		try {
+			helpContentCache.put(contextId, parseHelp(contextId));
+		} catch (Exception e) {
+			e.printStackTrace();
+			helpContentCache.put(contextId, EMPTY_DOCUMENTATION);
+		}
+		return helpContentCache.get(contextId);
+	}
+
+	private String parseHelp(String contextId) throws Exception {
+		ContextFileProvider provider = new ContextFileProvider();
+		IContext context = provider.getContext(contextId, Locale.getDefault()
+				.toString());
+		IHelpResource[] relatedTopics = context.getRelatedTopics();
+		Assert.isTrue(relatedTopics != null);
+		// We assume that there is only one topic registered
+		IHelpResource helpResource = relatedTopics[0];
+		String href = helpResource.getHref().substring(1);
+		URL url = new URL("platform:/plugin/" + href);
+		return convertStreamToString(url.openConnection().getInputStream());
+	}
+
+	public String convertStreamToString(InputStream is) throws Exception {
+		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+		StringBuilder builder = new StringBuilder();
+		String line = null;
+		while ((line = reader.readLine()) != null) {
+			builder.append(line);
+			builder.append("\n");
+		}
+		is.close();
+		return builder.toString();
+	}
+
+}

+ 64 - 0
de.itemis.xtext.utils/plugins/de.itemis.xtext.utils.jface/src/de/itemis/utils/jface/viewers/help/CrossRefObjectTextHover.java

@@ -0,0 +1,64 @@
+/**
+ * Copyright (c) 2012 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package de.itemis.utils.jface.viewers.help;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.text.IInformationControlCreator;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.xtext.resource.IGlobalServiceProvider;
+import org.eclipse.xtext.ui.editor.hover.DispatchingEObjectTextHover;
+import org.eclipse.xtext.ui.editor.hover.IEObjectHover;
+import org.eclipse.xtext.ui.editor.hover.IEObjectHoverProvider;
+import org.eclipse.xtext.ui.editor.hover.IEObjectHoverProvider.IInformationControlCreatorProvider;
+
+import com.google.inject.Inject;
+
+/**
+ * Initially copied from {@link DispatchingEObjectTextHover}. This
+ * implementation bypasses the {@link IGlobalServiceProvider} because there is a
+ * check to the resource file extensions for cross references.
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class CrossRefObjectTextHover extends DispatchingEObjectTextHover implements
+		IEObjectHover {
+
+	private IInformationControlCreatorProvider lastCreatorProvider;
+	@Inject
+	private IEObjectHoverProvider hoverProvider;
+
+	
+	@Override
+	public Object getHoverInfo(EObject first, ITextViewer textViewer,
+			IRegion hoverRegion) {
+		IInformationControlCreatorProvider creatorProvider = hoverProvider
+				.getHoverInfo(first, textViewer, hoverRegion);
+		if (creatorProvider == null)
+			return null;
+		this.lastCreatorProvider = creatorProvider;
+		return lastCreatorProvider.getInfo();
+	}
+
+	@Override
+	public IInformationControlCreator getHoverControlCreator() {
+		return this.lastCreatorProvider == null ? null : lastCreatorProvider
+				.getHoverControlCreator();
+	}
+
+	@Override
+	@Deprecated
+	public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
+		final Object hoverInfo2 = getHoverInfo2(textViewer, hoverRegion);
+		return hoverInfo2 != null ? hoverInfo2.toString() : null;
+	}
+}

+ 49 - 0
de.itemis.xtext.utils/plugins/de.itemis.xtext.utils.jface/src/de/itemis/utils/jface/viewers/help/HelpHoverProvider.java

@@ -0,0 +1,49 @@
+/**
+ * Copyright (c) 2012 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package de.itemis.utils.jface.viewers.help;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.xtext.Keyword;
+import org.eclipse.xtext.ui.editor.hover.html.DefaultEObjectHoverProvider;
+
+/**
+ * Removes the first line, since the documentation already contains a header.
+ * It also removes the open declaration action
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class HelpHoverProvider extends DefaultEObjectHoverProvider {
+
+	
+	@Override
+	protected boolean hasHover(EObject o) {
+		if (o instanceof Keyword)
+			return true;
+		return super.hasHover(o);
+	}
+
+	protected String getHoverInfoAsHtml(EObject o) {
+		StringBuffer buffer = new StringBuffer();
+		String documentation = getDocumentation(o);
+		if (documentation != null && documentation.length() > 0) {
+			buffer.append(documentation);
+		}
+		String hover = buffer.toString();
+		if (hover == null
+				|| AbstractUserHelpDocumentationProvider.EMPTY_DOCUMENTATION
+						.equals(hover))
+			return null;
+		return hover;
+	}
+	
+
+}