[xtext][documentationSupport] Introduced extension interface to IEObjectDocumentProvider which allows to access the actual documentation nodes

This commit is contained in:
Sebastian Zarnekow 2012-03-05 21:28:01 +01:00
parent 3ae1cff276
commit f7a8042a9a
3 changed files with 58 additions and 5 deletions

View file

@ -17,6 +17,10 @@ import com.google.inject.ImplementedBy;
/**
* Returns a documentation string for an EObject.
* There exists an extension interface for the documentation provider:
* <ul>
* <li>{@link IEObjectDocumentationProviderExtension} allows to query the nodes that yield the documentation</li>
* </ul>
*
* @author Christoph Kulla - Initial contribution and API
*/

View file

@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) 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
*******************************************************************************/
package org.eclipse.xtext.documentation;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.xtext.nodemodel.INode;
/**
* Allows to access the nodes that contain the documentation of an {@link EObject}
* and are considered by hte {@link IEObjectDocumentationProvider}.
* @author Sebastian Zarnekow - Initial contribution and API
* @since 2.3
*/
@NonNullByDefault
public interface IEObjectDocumentationProviderExtension {
List<INode> getDocumentationNodes(EObject object);
// IDocumentation getDocumentation2(EObject object);
}

View file

@ -10,9 +10,14 @@
*******************************************************************************/
package org.eclipse.xtext.documentation.impl;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider;
import org.eclipse.xtext.documentation.IEObjectDocumentationProviderExtension;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
@ -24,7 +29,7 @@ import com.google.inject.name.Named;
/**
* @author Christoph Kulla - Initial contribution and API
*/
public class MultiLineCommentDocumentationProvider implements IEObjectDocumentationProvider {
public class MultiLineCommentDocumentationProvider implements IEObjectDocumentationProvider, IEObjectDocumentationProviderExtension {
public final static String RULE = "org.eclipse.xtext.ui.editor.hover.MultiLineDocumentationProvider.ruleName";
public final static String START_TAG = "org.eclipse.xtext.ui.editor.hover.MultiLineDocumentationProvider.startTag";
@ -59,22 +64,37 @@ public class MultiLineCommentDocumentationProvider implements IEObjectDocumentat
protected String findComment(EObject o) {
String returnValue = null;
ICompositeNode node = NodeModelUtils.getNode(o);
List<INode> documentationNodes = getDocumentationNodes(o);
if (!documentationNodes.isEmpty()) {
return documentationNodes.get(0).getText();
}
return returnValue;
}
/**
* Returns the nearest multi line comment node that precedes the given object.
* @since 2.3
* @return a list with exactly one node or an empty list if the object is undocumented.
*/
@NonNull
public List<INode> getDocumentationNodes(@NonNull EObject object) {
ICompositeNode node = NodeModelUtils.getNode(object);
List<INode> result = Collections.emptyList();
if (node != null) {
// get the last multi line comment before a non hidden leaf node
for (INode abstractNode : node.getAsTreeIterable()) {
for (INode abstractNode : node.getLeafNodes()) {
if (abstractNode instanceof ILeafNode && !((ILeafNode) abstractNode).isHidden())
break;
if (abstractNode instanceof ILeafNode && abstractNode.getGrammarElement() instanceof TerminalRule
&& ruleName.equalsIgnoreCase(((TerminalRule) abstractNode.getGrammarElement()).getName())) {
String comment = ((ILeafNode) abstractNode).getText();
if (comment.matches("(?s)" + startTag + ".*")) {
returnValue = comment;
result = Collections.singletonList(abstractNode);
}
}
}
}
return returnValue;
return result;
}
public String getDocumentation(EObject o) {