Search in sources :

Example 16 with Doclet

use of org.eclipse.n4js.jsdoc.dom.Doclet in project n4js by eclipse.

the class DocletParserTest method testJSDocParsing.

@SuppressWarnings("javadoc")
@Test
public void testJSDocParsing() {
    String in = "/** This is the description.\n * @stubLineTagTitle \n */";
    AbstractLineTagDefinition tag = new StubLineTagDefinition("stubLineTagTitle");
    DocletParser docletParser = new DocletParser(new TagDictionary<>(Arrays.asList(tag)), new TagDictionary<AbstractInlineTagDefinition>());
    Doclet doclet = docletParser.parse(in);
    Text descr = (Text) doclet.getContents().get(0);
    assertEquals("This is the description.", descr.getText());
    LineTag lineTag = doclet.getLineTags().get(0);
    assertEquals("stubLineTagTitle", lineTag.getTitle().getTitle());
}
Also used : LineTag(org.eclipse.n4js.jsdoc.dom.LineTag) AbstractLineTagDefinition(org.eclipse.n4js.jsdoc.tags.AbstractLineTagDefinition) AbstractInlineTagDefinition(org.eclipse.n4js.jsdoc.tags.AbstractInlineTagDefinition) Doclet(org.eclipse.n4js.jsdoc.dom.Doclet) Text(org.eclipse.n4js.jsdoc.dom.Text) Test(org.junit.Test)

Example 17 with Doclet

use of org.eclipse.n4js.jsdoc.dom.Doclet in project n4js by eclipse.

the class DocletParserTest method testTrailingDescriptionIsIgnored.

@SuppressWarnings("javadoc")
@Test
public void testTrailingDescriptionIsIgnored() {
    String in = "/** This is the description.\n * @stubLineTagTitle \n * Trailing description.\n */";
    AbstractLineTagDefinition tag = new StubLineTagDefinition("stubLineTagTitle");
    DocletParser docletParser = new DocletParser(new TagDictionary<>(Arrays.asList(tag)), new TagDictionary<AbstractInlineTagDefinition>());
    Doclet doclet = docletParser.parse(in);
    assertEquals(1, doclet.getContents().size());
    assertEquals(1, doclet.getContents().size());
    Text descr = (Text) doclet.getContents().get(0);
    assertEquals("This is the description.", descr.getText());
    LineTag lineTag = doclet.getLineTags().get(0);
    assertEquals("stubLineTagTitle", lineTag.getTitle().getTitle());
}
Also used : LineTag(org.eclipse.n4js.jsdoc.dom.LineTag) AbstractLineTagDefinition(org.eclipse.n4js.jsdoc.tags.AbstractLineTagDefinition) AbstractInlineTagDefinition(org.eclipse.n4js.jsdoc.tags.AbstractInlineTagDefinition) Doclet(org.eclipse.n4js.jsdoc.dom.Doclet) Text(org.eclipse.n4js.jsdoc.dom.Text) Test(org.junit.Test)

Example 18 with Doclet

use of org.eclipse.n4js.jsdoc.dom.Doclet in project n4js by eclipse.

the class InlineTagTest method testSimpleLineTag.

@SuppressWarnings("javadoc")
@Test
public void testSimpleLineTag() {
    String in = "/** Some Description {@inline me} some other text. \n */";
    AbstractInlineTagDefinition tag = new StubInlineTagDefinition("inline");
    DocletParser docletParser = new DocletParser(new TagDictionary<AbstractLineTagDefinition>(), new TagDictionary<>(Arrays.asList(tag)));
    Doclet doclet = docletParser.parse(in);
    EList<ContentNode> contents = doclet.getContents();
    Composite composite = (Composite) contents.get(0);
    ContentNode node0 = composite.getContents().get(0);
    ContentNode node1 = composite.getContents().get(1);
    ContentNode node2 = composite.getContents().get(2);
    Text descriptionText1 = (Text) node0;
    InlineTag descriptionInlineTag = (InlineTag) node1;
    Text descriptionText2 = (Text) node2;
    assertEquals("Some Description ", descriptionText1.getText());
    assertEquals(" some other text.", descriptionText2.getText());
    TagValue inlineDescription = descriptionInlineTag.getValueByKey(StubInlineTagDefinition.PARAM_VALUE);
    Text tValue = (Text) inlineDescription.getContents().get(0);
    assertEquals(" me", tValue.getText());
}
Also used : Composite(org.eclipse.n4js.jsdoc.dom.Composite) Text(org.eclipse.n4js.jsdoc.dom.Text) ContentNode(org.eclipse.n4js.jsdoc.dom.ContentNode) InlineTag(org.eclipse.n4js.jsdoc.dom.InlineTag) Doclet(org.eclipse.n4js.jsdoc.dom.Doclet) DocletParser(org.eclipse.n4js.jsdoc.DocletParser) TagValue(org.eclipse.n4js.jsdoc.dom.TagValue) Test(org.junit.Test)

Example 19 with Doclet

use of org.eclipse.n4js.jsdoc.dom.Doclet in project n4js by eclipse.

the class ProjectCompareTreeHelper method readDocumentation.

/**
 * Read documentation from the special <code>&#64;api</code> tag in the JSdoc of all types and members related to
 * the given entry. Documentation of the API element and of the implementation elements for all given implementation
 * IDs will be merged into a single string (separated by '|').
 *
 * @return string containing the documentation of the API element and of the implementation elements for all given
 *         implementation IDs or <code>null</code> if none of the elements has documentation provided.
 */
private String readDocumentation(ResourceSet resourceSetForDocRetrieval, ProjectComparisonEntry entry, int[] implIndices) {
    if (entry.isElementEntry()) {
        // collect API element and the requested implementation elements
        final List<EObject> elemsToConsider = new ArrayList<>();
        elemsToConsider.add(entry.getElementAPI());
        for (int currImplIdx : implIndices) {
            elemsToConsider.add(entry.getElementImpl(currImplIdx));
        }
        // read documentation from those elements
        final StringBuilder sb = new StringBuilder();
        for (EObject currElem : elemsToConsider) {
            if (currElem != null && !currElem.eIsProxy()) {
                final String docStr = n4jsDocHelper.getDocSafely(resourceSetForDocRetrieval, currElem);
                if (docStr != null) {
                    final Doclet dl = n4jsDocHelper.getDoclet(docStr);
                    if (dl != null) {
                        final String docApiTag = N4JSDocletParser.TAG_API_NOTE.getValue(dl, null);
                        if (docApiTag != null && docApiTag.trim().length() > 0) {
                            // disallow NL within tag text
                            final String sanitized = docApiTag.replaceAll("\\s*\n\\s*", " ").trim();
                            if (sb.length() > 0)
                                // separate API and implementations
                                sb.append(" | ");
                            sb.append(sanitized);
                        }
                    }
                }
            }
        }
        final String doc = sb.toString().trim();
        if (doc.length() > 0) {
            return doc;
        }
    }
    return null;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Doclet(org.eclipse.n4js.jsdoc.dom.Doclet) ArrayList(java.util.ArrayList)

Example 20 with Doclet

use of org.eclipse.n4js.jsdoc.dom.Doclet in project n4js by eclipse.

the class JSDocCompletionProposalComputer method exec.

@SuppressWarnings("unchecked")
@Override
public ICompletionProposal[] exec(XtextResource xtextResource) throws Exception {
    ContentAssistContext[] contexts = processor.getContextFactory().create(viewer, offset, xtextResource);
    if (contexts.length > 0) {
        ArrayList<ICompletionProposal> proposals = new ArrayList<>();
        ContentAssistContext contentAssistContext = contexts[contexts.length - 1];
        INode currentNode = contentAssistContext.getCurrentNode();
        String content = currentNode.getText();
        int offsetInNode = contentAssistContext.getOffset() - currentNode.getOffset() - contentAssistContext.getPrefix().length();
        // String textInFront = content.substring(0, offsetInNode);
        // System.out.println(textInFront);
        N4JSDocletParser docletParser = processor.getDocletParser();
        Doclet doclet = docletParser.parse(content);
        Optional<String> lineTagPrefix = getLineTagTitlePrefix(content, offsetInNode);
        if (lineTagPrefix.isPresent()) {
            createLineTagProposal(lineTagPrefix.get(), docletParser, proposals);
        }
        Tag tag = JSDocletUtils.getTagAtOffset(doclet, offsetInNode);
        if (tag != null) {
            ITagDefinition tagDef = tag.getTagDefinition();
            JSDocCharScanner scanner = new JSDocCharScanner(content);
            ScannerState state = scanner.saveState();
            scanner.setNextOffset(offsetInNode);
            JSDocCompletionHint completionHint = tagDef.completionHint(scanner);
            scanner.restoreState(state);
            if (completionHint.kind != NOCOMPLETION) {
                int replacementOffset = offset - completionHint.prefix.length();
                if (completionHint.isTypeModelRef()) {
                    // get reference as far as it can be parsed:
                    FullMemberReference ref = completionHint.nodeAsFullMemberReference();
                    IContentAssistScopeProvider scopeProvider = (IContentAssistScopeProvider) processor.getScopeProvider();
                    IScope moduleSpecScope = scopeProvider.getScopeForContentAssist(// context for finding modules
                    xtextResource.getContents().get(0), N4JSPackage.Literals.IMPORT_DECLARATION__MODULE);
                    // complete module or type
                    if (!completionHint.isModuleNameCompleted()) {
                        for (IEObjectDescription moduleDescr : moduleSpecScope.getAllElements()) {
                            String moduleSpec = moduleDescr.getName().toString("/");
                            String moduleSimpleName = moduleDescr.getName().getLastSegment();
                            if (!moduleSpec.startsWith("#")) {
                                if (moduleSpec.startsWith(completionHint.prefix) || moduleSimpleName.startsWith(completionHint.prefix)) {
                                    if (moduleSpec.length() == completionHint.prefix.length()) {
                                        int replacementLength = 0;
                                        int cursorPosition = 1;
                                        ICompletionProposal proposal = new CompletionProposal(".", replacementOffset + completionHint.prefix.length(), replacementLength, cursorPosition);
                                        proposals.add(proposal);
                                    } else {
                                        int replacementLength = completionHint.prefix.length();
                                        int cursorPosition = moduleSpec.length();
                                        ICompletionProposal proposal = new CompletionProposal(moduleSpec, replacementOffset, replacementLength, cursorPosition);
                                        proposals.add(proposal);
                                    }
                                }
                            }
                        }
                    } else {
                        QualifiedName moduleQN = QualifiedName.create(ref.getModuleName().split("/"));
                        IEObjectDescription descr = moduleSpecScope.getSingleElement(moduleQN);
                        if (descr != null && descr.getEObjectOrProxy() instanceof TModule) {
                            TModule module = (TModule) descr.getEObjectOrProxy();
                            if (module.eIsProxy())
                                module = (TModule) EcoreUtil.resolve(module, xtextResource);
                            if (!completionHint.isTypeNameCompleted(false) && completionHint.kind != MODULESPEC) {
                                String typePrefix = ref.getTypeName();
                                for (Type t : module.getTopLevelTypes()) {
                                    String typeName = t.getName();
                                    if (typeName.startsWith(typePrefix)) {
                                        String completion = module.getModuleSpecifier() + "." + typeName;
                                        int replacementLength = completionHint.prefix.length();
                                        int cursorPosition = completion.length();
                                        ICompletionProposal proposal = new CompletionProposal(completion, replacementOffset, replacementLength, cursorPosition);
                                        proposals.add(proposal);
                                    }
                                }
                            } else {
                                // completionHint.kind == MEMBER
                                Optional<Type> optType = module.getTopLevelTypes().stream().filter(t -> t.getName().equals(ref.getTypeName())).findAny();
                                if (optType.isPresent()) {
                                    Type t = optType.get();
                                    if (t instanceof ContainerType) {
                                        String memberPrefix = ref.getMemberName();
                                        for (TMember m : ((ContainerType<? extends TMember>) t).getOwnedMembers()) {
                                            String memberName = m.getName();
                                            if (memberName.startsWith(memberPrefix)) {
                                                String completion = LineTagWithFullElementReference.createReferenceFromType(m).toString();
                                                int replacementLength = completionHint.prefix.length();
                                                int cursorPosition = completion.length();
                                                ICompletionProposal proposal = new CompletionProposal(completion, replacementOffset, replacementLength, cursorPosition);
                                                proposals.add(proposal);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        ICompletionProposal[] result = new ICompletionProposal[proposals.size()];
        proposals.toArray(result);
        return result;
    }
    return null;
}
Also used : JSDocCharScanner(org.eclipse.n4js.jsdoc.JSDocCharScanner) FullMemberReference(org.eclipse.n4js.jsdoc.dom.FullMemberReference) N4JSPackage(org.eclipse.n4js.n4JS.N4JSPackage) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ArrayList(java.util.ArrayList) ContainerType(org.eclipse.n4js.ts.types.ContainerType) TModule(org.eclipse.n4js.ts.types.TModule) Doclet(org.eclipse.n4js.jsdoc.dom.Doclet) Type(org.eclipse.n4js.ts.types.Type) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ITextViewer(org.eclipse.jface.text.ITextViewer) INode(org.eclipse.xtext.nodemodel.INode) XtextResource(org.eclipse.xtext.resource.XtextResource) JSDocCompletionHint(org.eclipse.n4js.jsdoc.JSDocCompletionHint) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) IScope(org.eclipse.xtext.scoping.IScope) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) N4JSDocletParser(org.eclipse.n4js.jsdoc.N4JSDocletParser) TMember(org.eclipse.n4js.ts.types.TMember) LineTagWithFullElementReference(org.eclipse.n4js.jsdoc.tags.LineTagWithFullElementReference) MODULESPEC(org.eclipse.n4js.jsdoc.JSDocCompletionHint.CompletionKind.MODULESPEC) NOCOMPLETION(org.eclipse.n4js.jsdoc.JSDocCompletionHint.CompletionKind.NOCOMPLETION) ScannerState(org.eclipse.n4js.jsdoc.JSDocCharScanner.ScannerState) ITagDefinition(org.eclipse.n4js.jsdoc.ITagDefinition) Tag(org.eclipse.n4js.jsdoc.dom.Tag) JSDocletUtils(org.eclipse.n4js.jsdoc.JSDocletUtils) Optional(java.util.Optional) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IContentAssistScopeProvider(org.eclipse.n4js.scoping.IContentAssistScopeProvider) INode(org.eclipse.xtext.nodemodel.INode) N4JSDocletParser(org.eclipse.n4js.jsdoc.N4JSDocletParser) ArrayList(java.util.ArrayList) JSDocCompletionHint(org.eclipse.n4js.jsdoc.JSDocCompletionHint) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Doclet(org.eclipse.n4js.jsdoc.dom.Doclet) IScope(org.eclipse.xtext.scoping.IScope) TModule(org.eclipse.n4js.ts.types.TModule) FullMemberReference(org.eclipse.n4js.jsdoc.dom.FullMemberReference) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) JSDocCharScanner(org.eclipse.n4js.jsdoc.JSDocCharScanner) QualifiedName(org.eclipse.xtext.naming.QualifiedName) ContainerType(org.eclipse.n4js.ts.types.ContainerType) JSDocCompletionHint(org.eclipse.n4js.jsdoc.JSDocCompletionHint) ITagDefinition(org.eclipse.n4js.jsdoc.ITagDefinition) ContainerType(org.eclipse.n4js.ts.types.ContainerType) Type(org.eclipse.n4js.ts.types.Type) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) IContentAssistScopeProvider(org.eclipse.n4js.scoping.IContentAssistScopeProvider) Tag(org.eclipse.n4js.jsdoc.dom.Tag) TMember(org.eclipse.n4js.ts.types.TMember) ScannerState(org.eclipse.n4js.jsdoc.JSDocCharScanner.ScannerState)

Aggregations

Doclet (org.eclipse.n4js.jsdoc.dom.Doclet)28 Test (org.junit.Test)21 LineTag (org.eclipse.n4js.jsdoc.dom.LineTag)14 Text (org.eclipse.n4js.jsdoc.dom.Text)14 DocletParser (org.eclipse.n4js.jsdoc.DocletParser)11 AbstractInlineTagDefinition (org.eclipse.n4js.jsdoc.tags.AbstractInlineTagDefinition)11 ContentNode (org.eclipse.n4js.jsdoc.dom.ContentNode)8 AbstractLineTagDefinition (org.eclipse.n4js.jsdoc.tags.AbstractLineTagDefinition)8 TagValue (org.eclipse.n4js.jsdoc.dom.TagValue)6 N4JSDocletParser (org.eclipse.n4js.jsdoc.N4JSDocletParser)5 FullMemberReference (org.eclipse.n4js.jsdoc.dom.FullMemberReference)4 Composite (org.eclipse.n4js.jsdoc.dom.Composite)3 InlineTag (org.eclipse.n4js.jsdoc.dom.InlineTag)3 ArrayList (java.util.ArrayList)2 EObject (org.eclipse.emf.ecore.EObject)2 TMember (org.eclipse.n4js.ts.types.TMember)2 Optional (java.util.Optional)1 EcoreUtil (org.eclipse.emf.ecore.util.EcoreUtil)1 ITextViewer (org.eclipse.jface.text.ITextViewer)1 CompletionProposal (org.eclipse.jface.text.contentassist.CompletionProposal)1