Java Code Examples for org.eclipse.xtext.naming.QualifiedName#skipLast()

The following examples show how to use org.eclipse.xtext.naming.QualifiedName#skipLast() . 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: ImportRewriter.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
	 * Add the necessary text edits to the accumulating result. Optionally return the offset of the alias.
	 */
	private AliasLocation toTextEdit(QualifiedName qualifiedName, String optionalAlias, int insertionOffset,
			MultiTextEdit result) {
		QualifiedName moduleName = qualifiedName.skipLast(1);
		// the following code for enhancing existing ImportDeclarations makes use of the Xtext serializer, which is not
		// yet compatible with fragments in Xtext grammars (as of Xtext 2.9.1) -> deactivated for now
// @formatter:off
//		String moduleNameAsString = unquoted(syntacticModuleName(moduleName));
//		for (ImportDeclaration existing : existingImports) {
//			String importedModuleName = existing.getModule().getQualifiedName();
//			if (moduleNameAsString.equals(importedModuleName)) {
//				return enhanceExistingImportDeclaration(existing, qualifiedName, optionalAlias, result);
//			}
//		}
// @formatter:on
		return addNewImportDeclaration(moduleName, qualifiedName, optionalAlias, insertionOffset, result);
	}
 
Example 2
Source File: ReferenceResolutionFinder.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private String getDescription(ReferenceResolutionCandidate rrc) {
	if (rrc.isAlias()) {
		return "alias for " + qualifiedNameConverter.toString(rrc.qualifiedName);
	}
	if (rrc.isNamespace()) {
		return qualifiedNameConverter.toString(rrc.qualifiedName);
	}
	if (rrc.newImportHasAlias()) {
		String descr = "via new alias " + rrc.addedImportNameAndAlias.alias;
		descr += " for " + qualifiedNameConverter.toString(rrc.qualifiedName);
		descr += "\n\n";
		descr += "Introduces the new alias '" + rrc.addedImportNameAndAlias.alias;
		descr += "' for element " + qualifiedNameConverter.toString(rrc.qualifiedName);
		return descr;
	}

	QualifiedName rrcQN = rrc.qualifiedName;
	QualifiedName descrQN = (rrcQN.getSegmentCount() > 1) ? rrcQN.skipLast(1) : rrcQN;
	return qualifiedNameConverter.toString(descrQN);
}
 
Example 3
Source File: ReferenceResolutionFinder.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private ImportDescriptor getImportToBeAdded(ReferenceResolutionCandidate rrc) {
	NameAndAlias requestedImport = rrc.addedImportNameAndAlias;
	if (requestedImport == null) {
		return null;
	}

	QualifiedName qualifiedName = requestedImport.name;
	String optionalAlias = requestedImport.alias;

	String projectName = rrc.candidateProject.getProjectName().getRawName();
	QualifiedName moduleName = qualifiedName.skipLast(1);
	String moduleSpecifier = qualifiedNameConverter.toString(moduleName);

	ImportDescriptor importDesc;
	if (N4JSLanguageUtils.isDefaultExport(qualifiedName)) {
		String localName = optionalAlias != null ? optionalAlias
				: N4JSLanguageUtils.lastSegmentOrDefaultHost(qualifiedName);
		importDesc = ImportDescriptor.createDefaultImport(localName, moduleSpecifier, projectName, moduleName,
				Integer.MAX_VALUE);
	} else {
		importDesc = ImportDescriptor.createNamedImport(qualifiedName.getLastSegment(), optionalAlias,
				moduleSpecifier, projectName, moduleName, Integer.MAX_VALUE);
	}

	return importDesc;
}
 
Example 4
Source File: PyGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the Python package files.
 *
 * <p>This function generates the "__init__.py" files for all the packages.
 *
 * @param name the name of the generated type.
 * @param lineSeparator the line separator.
 * @param context the generation context.
 */
protected void writePackageFiles(QualifiedName name, String lineSeparator,
		IExtraLanguageGeneratorContext context) {
	final IFileSystemAccess2 fsa = context.getFileSystemAccess();
	final String outputConfiguration = getOutputConfigurationName();
	QualifiedName libraryName = null;
	for (final String segment : name.skipLast(1).getSegments()) {
		if (libraryName == null) {
			libraryName = QualifiedName.create(segment, LIBRARY_FILENAME);
		} else {
			libraryName = libraryName.append(segment).append(LIBRARY_FILENAME);
		}
		final String fileName = toFilename(libraryName);
		if (!fsa.isFile(fileName)) {
			final String content = PYTHON_FILE_HEADER + lineSeparator
					+ getGenerationComment(context) + lineSeparator
					+ LIBRARY_CONTENT;
			if (Strings.isEmpty(outputConfiguration)) {
				fsa.generateFile(fileName, content);
			} else {
				fsa.generateFile(fileName, outputConfiguration, content);
			}
		}
		libraryName = libraryName.skipLast(1);
	}
}
 
Example 5
Source File: ImportsAwareReferenceProposalCreator.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates proposal taking semantics of the N4JS imports into account.
 *
 * @param candidate
 *            the original input for which we create proposal
 * @param reference
 *            the reference
 * @param context
 *            the context
 * @return candidate proposal adjusted to the N4JS imports
 */
private IEObjectDescription getAliasedDescription(IEObjectDescription candidate, EReference reference,
		ContentAssistContext context) {

	// Content assist at a location where only simple names are allowed:
	// We found a qualified name and we'd need an import to be allowed to use
	// that name. Consider only the simple name of the element from the index
	// and make sure that the import is inserted as soon as the proposal is applied
	QualifiedName inputQN = candidate.getName();
	int inputNameSegmentCount = inputQN.getSegmentCount();
	if (inputNameSegmentCount > 1
			&& Arrays.contains(referencesSupportingImportedElements, reference))
		return new AliasedEObjectDescription(QualifiedName.create(inputQN.getLastSegment()), candidate);

	// filter out non-importable things:
	// globally provided things should never be imported:
	if (inputNameSegmentCount == 2 && N4TSQualifiedNameProvider.GLOBAL_NAMESPACE_SEGMENT
			.equals(inputQN.getFirstSegment()))
		return new AliasedEObjectDescription(QualifiedName.create(inputQN.getLastSegment()), candidate);

	// special handling for default imports:
	if (inputQN.getLastSegment().equals(N4JSLanguageConstants.EXPORT_DEFAULT_NAME)) {
		if (TExportableElement.class.isAssignableFrom(candidate.getEClass().getInstanceClass())) {
			if (N4JSResourceDescriptionStrategy.getExportDefault(candidate)) {
				return new AliasedEObjectDescription(inputQN, candidate);
			}
		}
		// not accessed via namespace
		QualifiedName nameNoDefault = inputQN.skipLast(1);
		QualifiedName moduleName = nameNoDefault.getSegmentCount() > 1
				? QualifiedName.create(nameNoDefault.getLastSegment())
				: nameNoDefault;
		return new AliasedEObjectDescription(moduleName, candidate);
	}
	// no special handling, return original input
	return candidate;
}
 
Example 6
Source File: EObjectDescriptionBasedStubGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected QualifiedName getOwnerClassName(QualifiedName nestedClassName) {
	String lastSegment = nestedClassName.getLastSegment();
	int trimIndex = lastSegment.lastIndexOf('$');
	if(trimIndex == -1)
		return nestedClassName.skipLast(1);
	else
		return nestedClassName.skipLast(1).append(lastSegment.substring(0, trimIndex));
}
 
Example 7
Source File: PyGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
protected void generateImportStatement(QualifiedName qualifiedName, ExtraLanguageAppendable appendable,
		IExtraLanguageGeneratorContext context) {
	final String typeName = qualifiedName.getLastSegment();
	final QualifiedName packageName = qualifiedName.skipLast(1);
	appendable.append("from "); //$NON-NLS-1$
	appendable.append(packageName.toString());
	appendable.append(" import "); //$NON-NLS-1$
	appendable.append(typeName);
	appendable.newLine();
}