Java Code Examples for org.eclipse.xtext.xtype.XImportSection#getImportDeclarations()

The following examples show how to use org.eclipse.xtext.xtype.XImportSection#getImportDeclarations() . 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: RewritableImportSection.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public RewritableImportSection(XtextResource resource, IImportsConfiguration importsConfiguration, XImportSection originalImportSection,
		String lineSeparator, ImportSectionRegionUtil regionUtil, IValueConverter<String> nameConverter) {
	this.importsConfiguration = importsConfiguration;
	this.resource = resource;
	this.lineSeparator = lineSeparator;
	this.regionUtil = regionUtil;
	this.nameValueConverter = nameConverter;
	this.implicitlyImportedPackages = importsConfiguration.getImplicitlyImportedPackages(resource);
	this.importRegion = regionUtil.computeRegion(resource);
	if (originalImportSection != null) {
		for (XImportDeclaration originalImportDeclaration : originalImportSection.getImportDeclarations()) {
			this.originalImportDeclarations.add(originalImportDeclaration);
			JvmDeclaredType importedType = originalImportDeclaration.getImportedType();
			if (originalImportDeclaration.isStatic()) {
				String memberName = originalImportDeclaration.getMemberName();
				if (originalImportDeclaration.isExtension()) {
					Maps2.putIntoSetMap(importedType, memberName, staticExtensionImports);
				} else {
					Maps2.putIntoSetMap(importedType, memberName, staticImports);
				}
			} else if (importedType != null) {
				Maps2.putIntoListMap(importedType.getSimpleName(), importedType, plainImports);
			}
		}
	}
}
 
Example 2
Source File: RewritableImportSection.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void update() {
	XImportSection importSection = importsConfiguration.getImportSection(resource);
	if (importSection == null && importsConfiguration instanceof IMutableImportsConfiguration) {
		importSection = XtypeFactory.eINSTANCE.createXImportSection();

		IMutableImportsConfiguration mutableImportsConfiguration = (IMutableImportsConfiguration) importsConfiguration;
		mutableImportsConfiguration.setImportSection(resource, importSection);
	}
	if (importSection == null) {
		return;
	}
	removeObsoleteStaticImports();

	List<XImportDeclaration> allImportDeclarations = newArrayList();
	allImportDeclarations.addAll(originalImportDeclarations);
	allImportDeclarations.addAll(addedImportDeclarations);
	allImportDeclarations.removeAll(removedImportDeclarations);

	List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
	importDeclarations.clear();
	importDeclarations.addAll(allImportDeclarations);
}
 
Example 3
Source File: JavaTypeQuickfixes.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void parseImportSection(XImportSection importSection, IAcceptor<String> visiblePackages,
		IAcceptor<String> importedTypes) {
	for (XImportDeclaration importDeclaration : importSection.getImportDeclarations()) {
		if (!importDeclaration.isStatic()) {
			if (importDeclaration.getImportedNamespace() != null) {
				String importedAsString = importDeclaration.getImportedNamespace();
				if (importDeclaration.isWildcard()) {
					importedAsString = importedAsString.substring(0, importedAsString.length() - 2);
					visiblePackages.accept(importedAsString);
				} else {
					importedTypes.accept(importedAsString);
				}
			} else {
				importedTypes.accept(importDeclaration.getImportedTypeName());
			}
		}
	}
}
 
Example 4
Source File: XImportSectionNamespaceScopeProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected List<ImportNormalizer> getImportedNamespaceResolvers(XImportSection importSection, boolean ignoreCase) {
	List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
	List<ImportNormalizer> result = Lists.newArrayListWithExpectedSize(importDeclarations.size());
	for (XImportDeclaration imp: importDeclarations) {
		if (!imp.isStatic()) {
			String value = imp.getImportedNamespace();
			if(value == null)
				value = imp.getImportedTypeName();
			ImportNormalizer resolver = createImportedNamespaceResolver(value, ignoreCase);
			if (resolver != null)
				result.add(resolver);
		}
	}
	return result;
}
 
Example 5
Source File: XtypeFormatter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _format(final XImportSection section, @Extension final IFormattableDocument format) {
  EList<XImportDeclaration> _importDeclarations = section.getImportDeclarations();
  for (final XImportDeclaration imp : _importDeclarations) {
    {
      format.<XImportDeclaration>format(imp);
      XImportDeclaration _last = IterableExtensions.<XImportDeclaration>last(section.getImportDeclarations());
      boolean _notEquals = (!Objects.equal(imp, _last));
      if (_notEquals) {
        format.<XImportDeclaration>append(imp, XbaseFormatterPreferenceKeys.blankLinesBetweenImports);
      } else {
        format.<XImportDeclaration>append(imp, XbaseFormatterPreferenceKeys.blankLinesAfterImports);
      }
    }
  }
}
 
Example 6
Source File: ScriptBuilderImpl.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Finalize the script.
 *
 * <p>The finalization includes: <ul>
 * <li>The import section is created.</li>
 * </ul>
 */
public void finalizeScript() {
	if (this.isFinalized) {
		throw new IllegalStateException("already finalized");
	}
	this.isFinalized = true;
	ImportManager concreteImports = new ImportManager(true);
	XImportSection importSection = getScript().getImportSection();
	if (importSection != null) {
		for (XImportDeclaration decl : importSection.getImportDeclarations()) {
			concreteImports.addImportFor(decl.getImportedType());
		}
	}
	for (String importName : getImportManager().getImports()) {
		JvmType type = findType(getScript(), importName).getType();
		if (concreteImports.addImportFor(type) && type instanceof JvmDeclaredType) {
			XImportDeclaration declaration = XtypeFactory.eINSTANCE.createXImportDeclaration();
			declaration.setImportedType((JvmDeclaredType) type);
			if (importSection == null) {
				importSection = XtypeFactory.eINSTANCE.createXImportSection();
				getScript().setImportSection(importSection);
			}
			importSection.getImportDeclarations().add(declaration);
		}
	}
	Resource resource = getScript().eResource();
	if (resource instanceof DerivedStateAwareResource) {
		((DerivedStateAwareResource) resource).discardDerivedState();
	}
}
 
Example 7
Source File: XtendImportedNamespaceScopeProvider.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
private AbstractScope getImportScope(XImportSection importSection, AbstractScope parent, RecordingTypeScope typeScope) {
	if (importSection == null)
		return parent;
	List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
	if (importDeclarations.isEmpty()) {
		return parent;
	}
	List<ImportNormalizer> wildcardImports = null;
	List<JvmType> concreteImports = null;
	List<QualifiedName> importedNames = null;
	boolean hasLegacyImport = false;
	for(XImportDeclaration importDeclaration: importDeclarations) {
		if (!importDeclaration.isStatic()) {
			if (importDeclaration.isWildcard()) {
				if (wildcardImports == null) {
					wildcardImports = Lists.newArrayListWithCapacity(4);
				}
				String importedNamespace = importDeclaration.getImportedNamespace();
				importedNamespace = importedNamespace.substring(0, importedNamespace.length() - 2);
				QualifiedName qualifiedImportedNamespace = qualifiedNameConverter.toQualifiedName(importedNamespace);
				wildcardImports.add(AbstractNestedTypeAwareImportNormalizer.createNestedTypeAwareImportNormalizer(qualifiedImportedNamespace, true, false));
			} else {
				JvmDeclaredType importedType = importDeclaration.getImportedType();
				if (importedType != null && !importedType.eIsProxy()) {
					if (concreteImports == null || importedNames == null /* to make JDT happy */) {
						concreteImports = Lists.newArrayListWithCapacity(10);
						importedNames = Lists.newArrayListWithCapacity(10);
					}
					concreteImports.add(importedType);
					if (importedType.eContainer() instanceof JvmDeclaredType) {
						String importSyntax = getImportsConfiguration().getLegacyImportSyntax(importDeclaration);
						if (importSyntax != null) {
							hasLegacyImport = true;
							importedNames.add(getQualifiedNameConverter().toQualifiedName(importSyntax));
						} else
							importedNames.add(null);
					} else {
						importedNames.add(null);
					}
				}
			}
		}
	}
	return getImportScope(wildcardImports, concreteImports, hasLegacyImport ? importedNames : null, parent, typeScope);
}
 
Example 8
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * looks up the static final fields which are accessible in unqualified form for the given expression.
 * That essentially includes static imports and the fields declared in the containing types
 */
protected Map<String, JvmIdentifiableElement> findVisibleFeatures(final XExpression expression) {
  HashMap<String, JvmIdentifiableElement> _xblockexpression = null;
  {
    Resource _eResource = expression.eResource();
    final Resource res = _eResource;
    boolean _matched = false;
    if (res instanceof StorageAwareResource) {
      boolean _isLoadedFromStorage = ((StorageAwareResource)res).isLoadedFromStorage();
      if (_isLoadedFromStorage) {
        _matched=true;
        return CollectionLiterals.<String, JvmIdentifiableElement>newHashMap();
      }
    }
    JvmDeclaredType _switchResult_1 = null;
    JvmIdentifiableElement _nearestLogicalContainer = this.containerProvider.getNearestLogicalContainer(expression);
    final JvmIdentifiableElement cont = _nearestLogicalContainer;
    boolean _matched_1 = false;
    if (cont instanceof JvmGenericType) {
      _matched_1=true;
      _switchResult_1 = ((JvmGenericType)cont);
    }
    if (!_matched_1) {
      if (cont instanceof JvmMember) {
        _matched_1=true;
        _switchResult_1 = ((JvmMember)cont).getDeclaringType();
      }
    }
    final JvmDeclaredType container = _switchResult_1;
    Pair<String, JvmDeclaredType> _mappedTo = Pair.<String, JvmDeclaredType>of("visibleFeaturesForAnnotationValues", container);
    final Provider<HashMap<String, JvmIdentifiableElement>> _function = () -> {
      final HashMap<String, JvmIdentifiableElement> result = CollectionLiterals.<String, JvmIdentifiableElement>newHashMap();
      Resource _eResource_1 = expression.eResource();
      final XImportSection section = this.importSectionLocator.getImportSection(((XtextResource) _eResource_1));
      if ((section != null)) {
        EList<XImportDeclaration> _importDeclarations = section.getImportDeclarations();
        for (final XImportDeclaration imp : _importDeclarations) {
          boolean _isStatic = imp.isStatic();
          if (_isStatic) {
            final String importedTypeName = imp.getImportedTypeName();
            if ((importedTypeName != null)) {
              final JvmType type = this.findTypeByName(imp, importedTypeName);
              boolean _matched_2 = false;
              if (type instanceof JvmGenericType) {
                _matched_2=true;
                this.collectAllVisibleFields(((JvmDeclaredType)type), result);
              }
              if (!_matched_2) {
                if (type instanceof JvmEnumerationType) {
                  _matched_2=true;
                  EList<JvmEnumerationLiteral> _literals = ((JvmEnumerationType)type).getLiterals();
                  for (final JvmEnumerationLiteral feature : _literals) {
                    result.put(feature.getSimpleName(), feature);
                  }
                }
              }
            }
          }
        }
      }
      this.collectAllVisibleFields(container, result);
      return result;
    };
    _xblockexpression = this.cache.<HashMap<String, JvmIdentifiableElement>>get(_mappedTo, expression.eResource(), _function);
  }
  return _xblockexpression;
}