Search in sources :

Example 61 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription 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 62 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project n4js by eclipse.

the class N4JSResource method loadFromDescription.

/**
 * Populate the contents list from the serialized type data of an {@link IEObjectDescription}. See
 * {@link #isLoadedFromDescription()} for details on resources that are being loaded from a description.
 *
 * @param description
 *            the description that carries the type data in its user data
 */
public boolean loadFromDescription(IResourceDescription description) {
    if (isLoaded)
        throw new IllegalStateException("Resource was already loaded");
    TModule deserializedModule = null;
    Iterable<IEObjectDescription> modules = description.getExportedObjectsByType(TypesPackage.Literals.TMODULE);
    for (IEObjectDescription module : modules) {
        deserializedModule = UserdataMapper.getDeserializedModuleFromDescription(module, getURI());
        if (deserializedModule != null) {
            break;
        }
    }
    if (deserializedModule != null) {
        boolean wasDeliver = eDeliver();
        try {
            eSetDeliver(false);
            ModuleAwareContentsList theContents = (ModuleAwareContentsList) getContents();
            if (!theContents.isEmpty())
                throw new IllegalStateException("There is already something in the contents list: " + theContents);
            InternalEObject astProxy = (InternalEObject) N4JSFactory.eINSTANCE.createScript();
            astProxy.eSetProxyURI(URI.createURI("#" + AST_PROXY_FRAGMENT));
            theContents.sneakyAdd(astProxy);
            theContents.sneakyAdd(deserializedModule);
            fullyInitialized = true;
            // TModule loaded from index had been fully post-processed prior to serialization
            fullyPostProcessed = true;
        } finally {
            eSetDeliver(wasDeliver);
        }
        return true;
    }
    return false;
}
Also used : TModule(org.eclipse.n4js.ts.types.TModule) InternalEObject(org.eclipse.emf.ecore.InternalEObject) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 63 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project n4js by eclipse.

the class N4JSResourceDescriptionStrategy method internalCreateEObjectDescriptionForRoot.

private void internalCreateEObjectDescriptionForRoot(final TModule module, IAcceptor<IEObjectDescription> acceptor) {
    // user data: serialized representation
    final Map<String, String> userData = createModuleUserData(module);
    QualifiedName qualifiedName = qualifiedNameProvider.getFullyQualifiedName(module);
    IEObjectDescription eod = new EObjectDescription(qualifiedName, module, userData);
    acceptor.accept(eod);
}
Also used : EObjectDescription(org.eclipse.xtext.resource.EObjectDescription) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 64 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project n4js by eclipse.

the class OrderedResourceDescriptionsData method registerDescription.

@SuppressWarnings("unchecked")
@Override
protected void registerDescription(final IResourceDescription description, final Map<QualifiedName, Object> target) {
    for (final IEObjectDescription object : description.getExportedObjects()) {
        final QualifiedName lowerCase = object.getName().toLowerCase();
        final Object existing = target.put(lowerCase, description);
        if (existing != null && existing != description) {
            Set<IResourceDescription> set = null;
            if (existing instanceof IResourceDescription) {
                // The linked hash set is the difference comparing to the super class.
                set = Sets.newLinkedHashSetWithExpectedSize(2);
                set.add((IResourceDescription) existing);
            } else {
                set = (Set<IResourceDescription>) existing;
            }
            set.add(description);
            target.put(lowerCase, set);
        }
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 65 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project n4js by eclipse.

the class GlobalObjectScope method buildMap.

@Override
protected void buildMap(Resource resource, Map<QualifiedName, IEObjectDescription> elements) {
    IDefaultResourceDescriptionStrategy strategy = ((XtextResource) resource).getResourceServiceProvider().get(IDefaultResourceDescriptionStrategy.class);
    TreeIterator<EObject> allProperContents = EcoreUtil.getAllProperContents(resource, false);
    IAcceptor<IEObjectDescription> acceptor = new IAcceptor<IEObjectDescription>() {

        @Override
        public void accept(IEObjectDescription description) {
            elements.put(description.getQualifiedName(), description);
        }
    };
    while (allProperContents.hasNext()) {
        EObject content = allProperContents.next();
        if (!strategy.createEObjectDescriptions(content, acceptor)) {
            allProperContents.prune();
        }
    }
}
Also used : IDefaultResourceDescriptionStrategy(org.eclipse.xtext.resource.IDefaultResourceDescriptionStrategy) EObject(org.eclipse.emf.ecore.EObject) IAcceptor(org.eclipse.xtext.util.IAcceptor) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)324 Test (org.junit.Test)95 EObject (org.eclipse.emf.ecore.EObject)82 QualifiedName (org.eclipse.xtext.naming.QualifiedName)79 IScope (org.eclipse.xtext.scoping.IScope)56 URI (org.eclipse.emf.common.util.URI)41 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)33 EClass (org.eclipse.emf.ecore.EClass)30 Resource (org.eclipse.emf.ecore.resource.Resource)28 EObjectDescription (org.eclipse.xtext.resource.EObjectDescription)22 AliasedEObjectDescription (org.eclipse.xtext.resource.impl.AliasedEObjectDescription)22 ArrayList (java.util.ArrayList)19 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)14 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 StyledString (org.eclipse.jface.viewers.StyledString)12 XtextResource (org.eclipse.xtext.resource.XtextResource)11 StringInputStream (org.eclipse.xtext.util.StringInputStream)11 EReference (org.eclipse.emf.ecore.EReference)10 List (java.util.List)9