Java Code Examples for org.eclipse.xtext.naming.QualifiedName#getSegmentCount()
The following examples show how to use
org.eclipse.xtext.naming.QualifiedName#getSegmentCount() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XtextProposalProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Override public boolean isCandidateMatchingPrefix(String name, String prefix) { if (delegate.isCandidateMatchingPrefix(name, prefix)) return true; QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(name); QualifiedName qualifiedPrefix = qualifiedNameConverter.toQualifiedName(prefix); if (qualifiedName.getSegmentCount() > 1) { if (qualifiedPrefix.getSegmentCount() == 1) return delegate.isCandidateMatchingPrefix(qualifiedName.getSegment(1), qualifiedPrefix.getFirstSegment()); if (!delegate.isCandidateMatchingPrefix(qualifiedName.getFirstSegment(), qualifiedPrefix.getFirstSegment())) return false; return delegate.isCandidateMatchingPrefix(qualifiedName.getSegment(1), qualifiedPrefix.getSegment(1)); } return false; }
Example 2
Source File: ImportedNamespaceAwareLocalScopeProvider.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
/** * Create a new {@link ImportNormalizer} for the given namespace. * @param namespace the namespace. * @param ignoreCase <code>true</code> if the resolver should be case insensitive. * @return a new {@link ImportNormalizer} or <code>null</code> if the namespace cannot be converted to a valid * qualified name. */ protected ImportNormalizer createImportedNamespaceResolver(final String namespace, boolean ignoreCase) { if (Strings.isEmpty(namespace)) return null; QualifiedName importedNamespace = qualifiedNameConverter.toQualifiedName(namespace); if (importedNamespace == null || importedNamespace.isEmpty()) { return null; } boolean hasWildCard = ignoreCase ? importedNamespace.getLastSegment().equalsIgnoreCase(getWildCard()) : importedNamespace.getLastSegment().equals(getWildCard()); if (hasWildCard) { if (importedNamespace.getSegmentCount() <= 1) return null; return doCreateImportNormalizer(importedNamespace.skipLast(1), true, ignoreCase); } else { return doCreateImportNormalizer(importedNamespace, false, ignoreCase); } }
Example 3
Source File: NewFeatureNameUtil.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
public String getDefaultName(XExpression expression) { String baseName = getBaseName(expression); final List<String> candidates = newArrayList(); Set<String> excludedNames = new HashSet<String>(allKeywords); for(IEObjectDescription featureDescription: featureCallScope.getAllElements()) { QualifiedName featureQName = featureDescription.getQualifiedName(); if(featureQName.getSegmentCount() == 1) excludedNames.add(featureQName.getLastSegment()); } jdtVariableCompletions.getVariableProposals(baseName, expression, VariableType.LOCAL_VAR, excludedNames, new CompletionDataAcceptor() { @Override public void accept(String replaceText, StyledString label, Image img) { candidates.add(replaceText); } }); return candidates.isEmpty() ? "dingenskirchen" : candidates.get(0); }
Example 4
Source File: NestedTypeAwareImportNormalizer.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@Override protected QualifiedName resolveNonWildcard(QualifiedName relativeName) { if (relativeName.getSegmentCount()==1) { // legacy import support, e.g. import java.util.Map$Entry allows to use Map$Entry as the simple name if (getImportedNamespacePrefix().getLastSegment().equals(relativeName.getFirstSegment())) { return getImportedNamespacePrefix(); } return internalResolve(relativeName); } else { StringBuilder concatenated = new StringBuilder(); for(int i = 0; i < relativeName.getSegmentCount(); i++) { String segment = relativeName.getSegment(i); if (segment.indexOf('$') == -1) { if (concatenated.length() != 0) { concatenated.append('$'); } concatenated.append(segment); } else { return null; } } return internalResolve(QualifiedName.create(concatenated.toString())); } }
Example 5
Source File: XbaseImportedNamespaceScopeProvider.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
/** * Create a new {@link ImportNormalizer} for the given namespace. * @param namespace the namespace. * @param ignoreCase <code>true</code> if the resolver should be case insensitive. * @return a new {@link ImportNormalizer} or <code>null</code> if the namespace cannot be converted to a valid * qualified name. */ protected ImportNormalizer createImportedNamespaceResolver(final String namespace, boolean ignoreCase) { if (Strings.isEmpty(namespace)) return null; QualifiedName importedNamespace = qualifiedNameConverter.toQualifiedName(namespace); if (importedNamespace == null || importedNamespace.isEmpty()) { return null; } boolean hasWildCard = ignoreCase ? importedNamespace.getLastSegment().equalsIgnoreCase(getWildCard()) : importedNamespace.getLastSegment().equals(getWildCard()); if (hasWildCard) { if (importedNamespace.getSegmentCount() <= 1) return null; return doCreateImportNormalizer(importedNamespace.skipLast(1), true, ignoreCase); } else { return doCreateImportNormalizer(importedNamespace, false, ignoreCase); } }
Example 6
Source File: ReferenceUpdater.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected boolean containsReferenceText(Delta delta, QualifiedName exp) { DESC: for (IEObjectDescription desc : delta.getDescriptions()) { QualifiedName cand = desc.getQualifiedName(); if (cand.getSegmentCount() >= exp.getSegmentCount()) { for (int i = 1; i <= exp.getSegmentCount(); i++) { String expSeg = exp.getSegment(exp.getSegmentCount() - i); String candSeg = cand.getSegment(cand.getSegmentCount() - i); if (!expSeg.equals(candSeg)) { continue DESC; } } } return true; } return false; }
Example 7
Source File: DefaultInferredElementFragmentProvider.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** * Computes a hash code for the given {@link #getEClass(EObject) EClass} and {@link #getQualifiedName(EObject) qualified name}. * * @param eClass * EClass to base hash on, must not be {@code null} * @param name * qualified name of inferred model element, can be {@code null} * @return hash code, never {@code null} */ protected HashCode computeHash(final EClass eClass, final QualifiedName name) { byte[] eClassUriBytes = eClassToUriBytesMap.get(eClass); if (eClassUriBytes == null) { eClassUriBytes = EcoreUtil.getURI(eClass).toString().getBytes(Charsets.UTF_8); eClassToUriBytesMap.put(eClass, eClassUriBytes); } Hasher hasher = hashFunction.newHasher(HASHER_CAPACITY); hasher.putBytes(eClassUriBytes); if (name != null) { hasher.putChar('/'); for (int j = 0; j < name.getSegmentCount(); j++) { hasher.putUnencodedChars(name.getSegment(j)).putChar('.'); } } return hasher.hash(); }
Example 8
Source File: ImportNormalizer.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
public QualifiedName deresolve(QualifiedName fullyQualifiedName) { if (hasWildCard) { if (!ignoreCase) { if (fullyQualifiedName.startsWith(importedNamespacePrefix) && fullyQualifiedName.getSegmentCount() != importedNamespacePrefix.getSegmentCount()) { return fullyQualifiedName.skipFirst(importedNamespacePrefix.getSegmentCount()); } } else { if (fullyQualifiedName.startsWithIgnoreCase(importedNamespacePrefix) && fullyQualifiedName.getSegmentCount() != importedNamespacePrefix.getSegmentCount()) { return fullyQualifiedName.skipFirst(importedNamespacePrefix.getSegmentCount()); } } } else { if (!ignoreCase) { if (fullyQualifiedName.equals(importedNamespacePrefix)) return QualifiedName.create(fullyQualifiedName.getLastSegment()); } else { if (fullyQualifiedName.equalsIgnoreCase(importedNamespacePrefix)) return QualifiedName.create(fullyQualifiedName.getLastSegment()); } } return null; }
Example 9
Source File: NestedTypeAwareImportNormalizerWithDotSeparator.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@Override protected QualifiedName resolveNonWildcard(QualifiedName relativeName) { if (relativeName.getSegmentCount()==1) { List<String> split = Strings.split(relativeName.getFirstSegment(), '$'); if (split.size() == 0) { // relativeName may be just something like '$' return internalResolve(relativeName); } return internalResolve(QualifiedName.create(split)); } else { StringBuilder concatenated = new StringBuilder(); for(int i = 0; i < relativeName.getSegmentCount(); i++) { String segment = relativeName.getSegment(i); if (segment.indexOf('$') == -1) { if (concatenated.length() != 0) { concatenated.append('$'); } concatenated.append(segment); } else { return null; } } return internalResolve(relativeName); } }
Example 10
Source File: KnownTypesScope.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
protected JvmType getExactMatch(JvmType type, int index, QualifiedName name) { String qn = type.getQualifiedName(); if (Strings.isEmpty(qn)) { return null; } QualifiedName typeName = QualifiedName.create(Strings.split(qn, '.')); if (name.equals(typeName)) { return type; } if (name.startsWith(typeName)) { JvmType result = findNestedType(type, index, name.skipFirst(typeName.getSegmentCount()-1)); return result; } if (name.getSegmentCount() > typeName.getSegmentCount()) { if (typeName.skipLast(1).equals(name.skipLast(1))) { if (typeName.getLastSegment().equals(name.skipFirst(typeName.getSegmentCount() - 1).toString("$"))) { return type; } } } return null; }
Example 11
Source File: N4JSImportedNamespaceAwareLocalScopeProvider.java From n4js with Eclipse Public License 1.0 | 5 votes |
@Override protected IScope getResourceScope(Resource res, EReference reference) { if (res == null) { return IScope.NULLSCOPE; } EObject context = res.getContents().get(0); // ||-- changed super-impl here: // IDE-662 filtering ArgumentsType & EnumBaseType from globalScobe, since it is a VirtualBaseType. Predicate<IEObjectDescription> filter = p -> { QualifiedName name = p.getName(); if (name.getSegmentCount() == 1) { String singleSegment = name.getFirstSegment(); return !("ArgumentsType".equals(singleSegment) || "EnumBaseType".equals(singleSegment)); } return true; }; IScope globalScope = getGlobalScope(res, reference, filter); // -- done change --|| List<ImportNormalizer> normalizers = getImplicitImports(isIgnoreCase(reference)); // IDE-1735 adding support for static-polyfills: TModule module = (TModule) res.getContents().get(1); if (module.isStaticPolyfillModule()) { // limit to situations of resources, that contain at least // one @StaticPolyfill normalizers.add(createImportedNamespaceResolver( module.getQualifiedName() + N4JSQualifiedNameConverter.DELIMITER + "*", false)); } if (!normalizers.isEmpty()) { globalScope = createImportScope(globalScope, normalizers, null, reference.getEReferenceType(), isIgnoreCase(reference)); } IScope resScope = getResourceScope(globalScope, context, reference); return resScope; }
Example 12
Source File: NestedTypeAwareImportNormalizer.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected boolean importedNamespaceHasDollar() { if (!hasWildCard()) // don't care return true; QualifiedName importedNamespace = getImportedNamespacePrefix(); int segmentCount = importedNamespace.getSegmentCount(); for(int i = 0; i < segmentCount; i++) { if (importedNamespace.getSegment(i).indexOf('$') != -1) { return false; } } return true; }
Example 13
Source File: KnownTypesScope.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
@Override protected IEObjectDescription toDescription(QualifiedName name, JvmType result, int dollarIndex, int index) { if (result != null) { JvmType actualResult = dollarIndex > 0 || name.getSegmentCount() > 0 ? findNestedType(result, index, name) : result; if (actualResult != null) { return EObjectDescription.create(name, actualResult); } } return null; }
Example 14
Source File: TestLanguageRenameService.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override protected EObject getElementWithIdentifierAt(XtextResource xtextResource, int offset) { if (offset >= 0) { if (xtextResource != null) { IParseResult parseResult = xtextResource.getParseResult(); if (parseResult != null) { ICompositeNode rootNode = parseResult.getRootNode(); if (rootNode != null) { ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset); if (leaf != null && isIdentifier(leaf)) { EObject element = eObjectAtOffsetHelper.resolveElementAt(xtextResource, offset); if (element != null) { IQualifiedNameProvider nameProvider = xtextResource.getResourceServiceProvider() .get(IQualifiedNameProvider.class); QualifiedName fqn = nameProvider.getFullyQualifiedName(element); if (fqn != null) { String leafText = NodeModelUtils.getTokenText(leaf); if (fqn.getSegmentCount() == 1 && Objects.equal(fqn.toString(), leafText) || Objects.equal(fqn.getLastSegment(), leafText)) { return element; } } } } } } } } return null; }
Example 15
Source File: QualifiedNameSegmentTreeLookup.java From dsl-devkit with Eclipse Public License 1.0 | 5 votes |
/** * Finds the node for the given qualified name. * * @param name * name to find node for * @param segIdx * index into segment of {@code name} (used for recursion), initially 0 (when receiver is root node) * @param exactMatch * when {@code false} the returned node will be the successor node if there is no node with the exact name looked for * @return matching node or successor node */ @SuppressWarnings("PMD.NPathComplexity") public SegmentNode find(final QualifiedName name, int segIdx, final boolean exactMatch) { if (children == null || children.isEmpty()) { return null; } String seg = name.getSegment(segIdx++); boolean lastSeg = name.getSegmentCount() == segIdx; int idx = binarySearch(children, seg); if (idx < 0) { if (exactMatch || !lastSeg) { return null; } idx = -(idx + 1); } if (idx == children.size()) { return null; } if (lastSeg) { return children.get(idx); } SegmentNode result = children.get(idx++).find(name, segIdx, exactMatch); if (result == null && !exactMatch) { result = idx == children.size() ? null : children.get(idx); } return result; }
Example 16
Source File: N4TSQualifiedNameProvider.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** * Returns true if the given name describes a polyfill, that is if it's second last segment matches * {@link #POLYFILL_SEGMENT}. */ public static boolean isPolyfill(QualifiedName name) { if (name == null || name.getSegmentCount() < 2) { return false; } // as long as Module-polyfill is not enforced, we check for presence and then adapt: int polyModuleOffest = isModulePolyfill(name) ? 1 : 0; return POLYFILL_SEGMENT.equals(name.getSegment(name.getSegmentCount() - (2 + polyModuleOffest))); }
Example 17
Source File: ReferenceResolutionFinder.java From n4js with Eclipse Public License 1.0 | 4 votes |
private NameAndAlias getImportChanges() { if (parentImportElement != null) { return null; } if (accessType != CandidateAccessType.direct) { return null; } QualifiedName importName = getImportName(); if (importName == null) { return null; } // we could create an import statement if there is no conflict if (importName.getSegmentCount() == 1) { // type name is a simple name - no need to hassle with imports return null; } // Globally available elements should not generate imports if (importName.getSegmentCount() == 2 && N4TSQualifiedNameProvider.GLOBAL_NAMESPACE_SEGMENT.equals(importName.getFirstSegment())) { // type name is a simple name from global Namespace - no need to hassle with imports return null; } String alias = null; if (candidateViaScopeShortName != null && isScopedCandidateCollisioning) { // accessing default export via already imported namespace if (candidateViaScopeShortName.getEObjectOrProxy() instanceof ModuleNamespaceVirtualType) { // return null; } // the simple name is already reachable, i.e. already in use - another import is present // try to use an alias alias = "Alias_" + UtilN4.toUpperCaseFirst(qualifiedName.toString().replace(".", "_")); } return new NameAndAlias(importName, alias); }
Example 18
Source File: ProjectStatePersister.java From n4js with Eclipse Public License 1.0 | 4 votes |
private void writeQualifiedName(QualifiedName qualifiedName, DataOutput output) throws IOException { output.writeInt(qualifiedName.getSegmentCount()); for (int i = 0, max = qualifiedName.getSegmentCount(); i < max; i++) { output.writeUTF(qualifiedName.getSegment(i)); } }
Example 19
Source File: AbstractMemberScope.java From n4js with Eclipse Public License 1.0 | 4 votes |
@Override protected IEObjectDescription getSingleLocalElementByName(QualifiedName name) { if (name.getSegmentCount() != 1) { return null; } final String nameAsString = name.getFirstSegment(); // both read/write required if (ExpressionExtensions.isBothReadFromAndWrittenTo(context)) { TMember reader = findMember(nameAsString, false, staticAccess); TMember writer = findMember(nameAsString, true, staticAccess); if (null == reader && null == writer) { // will be caught as error "Could not resolve reference" return null; } if (null == reader) { return new UnsatisfiedRWAccessDescription(EObjectDescription.create(writer.getName(), writer), true); } if (null == writer) { return new UnsatisfiedRWAccessDescription(EObjectDescription.create(reader.getName(), reader), false); } // pick arbitrarily the setter return createSingleElementDescription(writer); } // either read or write requirement that moreover is satisfied final boolean accessForWriteOperation = ExpressionExtensions.isLeftHandSide(context); TMember existingMember = findMember(nameAsString, accessForWriteOperation, staticAccess); if (existingMember != null) { return createSingleElementDescription(existingMember); } // wrong read/write existingMember = findMember(nameAsString, !accessForWriteOperation, staticAccess); if (existingMember != null) { // allowed special case: writing in the ctor to a final field that lacks init value final boolean isAssOfFinalInCtor = N4JSASTUtils .isSemiLegalAssignmentToFinalFieldInCtor(context.eContainer(), existingMember); final boolean isLegalAssOfFinalInCtor = isAssOfFinalInCtor && !((TField) existingMember).isHasExpression(); if (isLegalAssOfFinalInCtor) { return createSingleElementDescription(existingMember); } // allowed special case: accessing a setter for read operation in context of structural field init typing if (structFieldInitMode && !accessForWriteOperation && existingMember instanceof TSetter) { return createSingleElementDescription(existingMember); } // allowed special case: wrong read/write in a mode other than N4JS if (jsVariantHelper.allowWrongReadWrite(context)) { // cf. sec. 13.1 return createSingleElementDescription(existingMember); } return new WrongWriteAccessDescription( EObjectDescription.create(existingMember.getName(), existingMember), accessForWriteOperation, isAssOfFinalInCtor); } // wrong static / non-static existingMember = findMember(nameAsString, accessForWriteOperation, !staticAccess); if (existingMember == null) { // if both read/write access and static access are wrong, we want to // complain (only) about "wrong static access" -> so include this case here existingMember = findMember(nameAsString, !accessForWriteOperation, !staticAccess); } if (existingMember != null && !isDynamicType) { return new WrongStaticAccessDescription( EObjectDescription.create(existingMember.getName(), existingMember), staticAccess); } return null; }
Example 20
Source File: N4JSReplacementTextApplier.java From n4js with Eclipse Public License 1.0 | 4 votes |
/** * Modify the document and start linked editing if necessary. * * Imports will be added if necessary. * * @see #applyWithImport(QualifiedName, String, IDocument, ConfigurableCompletionProposal) */ @Override public void apply(IDocument document, ConfigurableCompletionProposal proposal) throws BadLocationException { final String syntacticReplacementString = proposal.getReplacementString(); // does it even happen? check logs if first and/or second check passes { String actualSyntacticReplacementString = getActualReplacementString(proposal); // there is an import statement - apply computed replacementString if (!syntacticReplacementString.equals(actualSyntacticReplacementString)) { QualifiedName shortQualifiedName = applyValueConverter(actualSyntacticReplacementString); if (shortQualifiedName.getSegmentCount() == 1) { simpleApply(document, actualSyntacticReplacementString, proposal); return; } } } final QualifiedName qualifiedName = (QualifiedName) proposal.getAdditionalData(KEY_QUALIFIED_NAME); final QualifiedName originalQualifiedName = (QualifiedName) proposal .getAdditionalData(KEY_ORIGINAL_QUALIFIED_NAME); if (qualifiedName == null) { simpleApply(document, syntacticReplacementString, proposal); return; } // we could create an import statement if there is no conflict if (qualifiedName.getSegmentCount() == 1) { // type name is a simple name - no need to hassle with imports simpleApply(document, syntacticReplacementString, proposal); return; } // Globally available elements should not generate imports if (qualifiedName.getSegmentCount() == 2 && N4TSQualifiedNameProvider.GLOBAL_NAMESPACE_SEGMENT.equals(qualifiedName.getFirstSegment())) { // type name is a simple name from global Namespace - no need to hassle with imports simpleApply(document, syntacticReplacementString, proposal); return; } String alias = null; String shortQName = lastSegmentOrDefaultHost(originalQualifiedName); IEObjectDescription descriptionFullQN = scope .getSingleElement(QualifiedName.create(shortQName)); // element is already imported via namespace if (descriptionFullQN instanceof PlainAccessOfNamespacedImportDescription) { QualifiedName namespaceQN = ((PlainAccessOfNamespacedImportDescription) descriptionFullQN) .getNamespacedName(); simpleApply(document, namespaceQN.toString(), proposal); return; } // element is already imported via an alias if (descriptionFullQN instanceof PlainAccessOfAliasedImportDescription) { simpleApply(document, ((PlainAccessOfAliasedImportDescription) descriptionFullQN).getAlias(), proposal); return; } if (descriptionFullQN != null) { if (descriptionFullQN.getEObjectOrProxy() instanceof ModuleNamespaceVirtualType) { // accessing default export via already imported namespace simpleApply(document, qualifiedName.toString(), proposal); return; } // the simple name is already reachable, i.e. already in use - another import is present // try to use an alias alias = "Alias" + UtilN4.toUpperCaseFirst(shortQName); } applyWithImport(qualifiedName, alias, document, proposal); }