use of org.eclipse.n4js.ts.types.TModule in project n4js by eclipse.
the class ProjectCompareHelper method compareModules.
/**
* Get the comparison for a module in a specific implementation specified by it's ID.
*
* @param module
* can be either Implementation or API
* @param implementationID
* the current context to compare with
*
* @param includePolyfills
* {@code true} if polyfills should be considered as the part of the API and the implementation.
* Otherwise {@code false}.
* @return a comparison between API and implementation with implementationID, or <code>null</code> if no project
* could be found, the module is not part of an API/IMPL ...
*/
public ProjectComparisonEntry compareModules(TModule module, String implementationID, final boolean includePolyfills) {
Optional<? extends IN4JSProject> opt = n4jsCore.findProject(module.eResource().getURI());
if (!opt.isPresent()) {
return null;
}
IN4JSProject project = opt.get();
IN4JSProject implProject = null;
IN4JSProject apiProject = null;
TModule apiModule = null;
TModule apiImplModule = null;
if (!project.getImplementationId().isPresent()) {
// this is NOT an implementation project, so assume we have the api
// need to load the correct implementation. Since there might be multiple implementors
// TODO replace with central instance
final ApiImplMapping mapping = ApiImplMapping.of(n4jsCore);
implProject = mapping.getImpl(project.getProjectId(), implementationID);
if (implProject == null) {
// no implementation found.
return null;
}
apiProject = project;
apiModule = module;
URI impUri = artifactHelper.findArtifact(implProject, apiModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JS_FILE_EXTENSION));
if (impUri != null) {
IResourceDescriptions xtextIndex = n4jsCore.getXtextIndex(module.eResource().getResourceSet());
IResourceDescription resourceDescription = xtextIndex.getResourceDescription(impUri);
if (resourceDescription != null) {
apiImplModule = n4jsCore.loadModuleFromIndex(module.eResource().getResourceSet(), resourceDescription, false);
} else {
if (logger.isDebugEnabled()) {
logger.debug("...ouch nothing in index for " + impUri);
}
Resource implResource = module.eResource().getResourceSet().getResource(impUri, true);
apiImplModule = (TModule) implResource.getContents().get(1);
// .get(1);
if (logger.isDebugEnabled()) {
logger.debug("TModule loaded from Resource: " + apiImplModule);
}
}
} else {
// No implementation present.
if (logger.isDebugEnabled()) {
logger.debug("No implementation given. For " + apiModule.getQualifiedName());
}
}
} else {
// check that the implementation ID matches.
if (implementationID.equals(project.getImplementationId().get())) {
implProject = project;
apiImplModule = module;
ImmutableList<? extends IN4JSProject> apiProjects = implProject.getImplementedProjects();
labelA: for (IN4JSProject ap : apiProjects) {
URI apiURI = artifactHelper.findArtifact(ap, apiImplModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JSD_FILE_EXTENSION));
if (apiURI != null) {
IResourceDescriptions xtextIndex = n4jsCore.getXtextIndex(apiImplModule.eResource().getResourceSet());
IResourceDescription resourceDescription = xtextIndex.getResourceDescription(apiURI);
if (resourceDescription != null) {
apiModule = n4jsCore.loadModuleFromIndex(apiImplModule.eResource().getResourceSet(), resourceDescription, false);
if (apiModule != null)
break labelA;
}
}
}
} else {
return null;
}
}
if (apiModule != null) {
return compareModules(apiProject, apiModule, implProject, apiImplModule, includePolyfills);
} else {
// no apiModule --> this is not an implementation of API.
return null;
}
}
use of org.eclipse.n4js.ts.types.TModule in project n4js by eclipse.
the class ScriptImpl method setModule.
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setModule(TModule newModule) {
TModule oldModule = module;
module = newModule;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, N4JSPackage.SCRIPT__MODULE, oldModule, module));
}
use of org.eclipse.n4js.ts.types.TModule 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;
}
use of org.eclipse.n4js.ts.types.TModule in project n4js by eclipse.
the class N4JSResource method isLoadedFromDescription.
/**
* Returns true iff the receiving resource is in the intermediate state of having the module loaded from the index
* (or created in some other way) but the AST is still unloaded, i.e. the contents list contains an
* {@link #isASTProxy(EObject) AST proxy} at index 0 and a non-proxy TModule at index 1.
* <p>
* Some notes:
* <ol>
* <li>here, "loaded from description" means "loaded from IResourceDescription / IEObjectDescription" or "loaded
* from Xtext index".
* <li>once the resource is fully loaded, i.e. also the AST has been loaded, this method will return {@code false}.
* <li>while in the loaded-from-description state, the {@link #isLoaded() isLoaded} flag may be {@code true} or
* {@code false}. This means, from the outside, such a resource may seem to be loaded or unloaded.
* </ol>
* Thus, an N4JSResource has 4 states and the following table shows what the {@link #getContents() contents} list
* contains in each of those states:
* <!-- @formatter:off -->
* <table border=1>
* <tr><th colspan=2 rowspan=2></th><th colspan=2>isLoadedFromDescription</th></tr>
* <tr><th>false</th><th>true</th></tr>
* <tr><th rowspan=2>isLoaded</th><th>false</th><td>empty</td><td>AST proxy + TModule</td></tr>
* <tr><th>true</th><td>AST + TModule</td><td>AST proxy + TModule</td></tr>
* </table>
* <!-- @formatter:on -->
* Note how the {@link #isLoaded() isLoaded} flag does not have any impact on the resource's actual contents in case
* {@code isLoadedFromDescription} is {@code true}.
*/
public boolean isLoadedFromDescription() {
final Script script = getScript();
final TModule module = getModule();
return script != null && module != null && isASTProxy(script) && !module.eIsProxy();
}
use of org.eclipse.n4js.ts.types.TModule 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;
}
Aggregations