Search in sources :

Example 6 with JSDocCharScanner

use of org.eclipse.n4js.jsdoc.JSDocCharScanner in project n4js by eclipse.

the class TextTokenizerTest method test.

@SuppressWarnings("javadoc")
@Test
public void test() {
    TextTokenizer TEXT = TextTokenizer.INSTANCE;
    assertEquals(new JSDocToken("Hello World", 0, 10), TEXT.nextToken(new JSDocCharScanner("Hello World")));
    assertEquals(new JSDocToken("Hello World", 7, 17), TEXT.nextToken(new JSDocCharScanner("/**\n * Hello World")));
    assertEquals(new JSDocToken("Hello World", 7, 17), TEXT.nextToken(new JSDocCharScanner("/**\n * Hello World", 7)));
    assertEquals(new JSDocToken("Hello World", 7, 17), TEXT.nextToken(new JSDocCharScanner("/**\n * Hello World */", 7)));
    assertEquals(new JSDocToken("Hello World ", 7, 18), TEXT.nextToken(new JSDocCharScanner("/**\n * Hello World {@code sample} */")));
}
Also used : JSDocCharScanner(org.eclipse.n4js.jsdoc.JSDocCharScanner) JSDocToken(org.eclipse.n4js.jsdoc.JSDocToken) Test(org.junit.Test)

Example 7 with JSDocCharScanner

use of org.eclipse.n4js.jsdoc.JSDocCharScanner 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)

Example 8 with JSDocCharScanner

use of org.eclipse.n4js.jsdoc.JSDocCharScanner in project n4js by eclipse.

the class RegionTokenizerTest method testSimpleNestedSkipable.

@SuppressWarnings("javadoc")
@Test
public void testSimpleNestedSkipable() {
    RegionTokenizer rt = new RegionTokenizer("{", "}", true, '\\', false, true);
    JSDocCharScanner scanner = new JSDocCharScanner("{simple{nest\ned}cont} and more");
    assertEquals("simple{nest\ned}cont", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{simple{nest\n *ed}cont} and more");
    assertEquals("simple{nest\ned}cont", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{simple{nest\n * ed}cont} and more");
    assertEquals("simple{nest\ned}cont", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{simple{nest\n *  ed}cont} and more");
    assertEquals("simple{nest\n ed}cont", rt.nextToken(scanner).token);
}
Also used : JSDocCharScanner(org.eclipse.n4js.jsdoc.JSDocCharScanner) Test(org.junit.Test)

Example 9 with JSDocCharScanner

use of org.eclipse.n4js.jsdoc.JSDocCharScanner in project n4js by eclipse.

the class RegionTokenizerTest method testSimpleNotNested.

@SuppressWarnings("javadoc")
@Test
public void testSimpleNotNested() {
    RegionTokenizer rt = new RegionTokenizer("{", "}", false, '\\', false, false);
    JSDocCharScanner scanner = new JSDocCharScanner("{simple{nested}cont} and more");
    assertEquals("simple{nested", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{simple{nested}} and more");
    assertEquals("simple{nested", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{{nested}simple} and more");
    assertEquals("{nested", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{{nested}} and more");
    assertEquals("{nested", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{{{nested}}} and more");
    assertEquals("{{nested", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{simple{and{nested}}} and more");
    assertEquals("simple{and{nested", rt.nextToken(scanner).token);
}
Also used : JSDocCharScanner(org.eclipse.n4js.jsdoc.JSDocCharScanner) Test(org.junit.Test)

Example 10 with JSDocCharScanner

use of org.eclipse.n4js.jsdoc.JSDocCharScanner in project n4js by eclipse.

the class RegionTokenizerTest method testSimpleNested.

@SuppressWarnings("javadoc")
@Test
public void testSimpleNested() {
    RegionTokenizer rt = new RegionTokenizer("{", "}", true, '\\', false, false);
    JSDocCharScanner scanner = new JSDocCharScanner("{simple{nested}cont} and more");
    assertEquals("simple{nested}cont", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{simple{nested}} and more");
    assertEquals("simple{nested}", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{{nested}simple} and more");
    assertEquals("{nested}simple", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{{nested}} and more");
    assertEquals("{nested}", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{{nested}}} and more");
    assertEquals("{nested}", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{{{nested}}} and more");
    assertEquals("{{nested}}", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{simple{and{nested}}} and more");
    assertEquals("simple{and{nested}}", rt.nextToken(scanner).token);
    scanner = new JSDocCharScanner("{{{nested}} and more");
    assertNull(rt.nextToken(scanner));
}
Also used : JSDocCharScanner(org.eclipse.n4js.jsdoc.JSDocCharScanner) Test(org.junit.Test)

Aggregations

JSDocCharScanner (org.eclipse.n4js.jsdoc.JSDocCharScanner)12 Test (org.junit.Test)11 JSDocToken (org.eclipse.n4js.jsdoc.JSDocToken)4 ArrayList (java.util.ArrayList)1 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 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 ITagDefinition (org.eclipse.n4js.jsdoc.ITagDefinition)1 ScannerState (org.eclipse.n4js.jsdoc.JSDocCharScanner.ScannerState)1 JSDocCompletionHint (org.eclipse.n4js.jsdoc.JSDocCompletionHint)1 MODULESPEC (org.eclipse.n4js.jsdoc.JSDocCompletionHint.CompletionKind.MODULESPEC)1 NOCOMPLETION (org.eclipse.n4js.jsdoc.JSDocCompletionHint.CompletionKind.NOCOMPLETION)1 JSDocletUtils (org.eclipse.n4js.jsdoc.JSDocletUtils)1 N4JSDocletParser (org.eclipse.n4js.jsdoc.N4JSDocletParser)1 Doclet (org.eclipse.n4js.jsdoc.dom.Doclet)1 FullMemberReference (org.eclipse.n4js.jsdoc.dom.FullMemberReference)1 Tag (org.eclipse.n4js.jsdoc.dom.Tag)1 LineTagWithFullElementReference (org.eclipse.n4js.jsdoc.tags.LineTagWithFullElementReference)1