use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-eclipse by eclipse.
the class NewFeatureNameUtil method checkNewFeatureName.
public void checkNewFeatureName(String newFeatureName, boolean isLookupInScope, RefactoringStatus status) {
if (isEmpty(newFeatureName)) {
status.addFatalError("Choose a name");
return;
}
try {
Object value = valueConverterService.toValue(newFeatureName, "ValidID", null);
valueConverterService.toString(value, "ValidID");
} catch (ValueConverterException exc) {
status.addFatalError(exc.getMessage());
}
if (Character.isUpperCase(newFeatureName.charAt(0)))
status.addError("Discouraged name '" + newFeatureName + "'. Name should start with a lowercase letter. ");
if (isKeyword(newFeatureName))
status.addFatalError("'" + newFeatureName + "' is keyword.");
@SuppressWarnings("restriction") Class<?> asPrimitive = org.eclipse.xtext.common.types.access.impl.Primitives.forName(newFeatureName);
if (asPrimitive != null)
status.addFatalError("'" + newFeatureName + "' is reserved.");
if (isLookupInScope && featureCallScope != null && isAlreadyDefined(newFeatureName))
status.addError("The name '" + newFeatureName + "' is already defined in this scope.");
}
use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-eclipse by eclipse.
the class DefaultRenameStrategy method validateNewName.
@Override
public RefactoringStatus validateNewName(String newName) {
RefactoringStatus status = super.validateNewName(newName);
if (nameRuleName != null) {
try {
String value = getNameAsValue(newName);
String text = getNameAsText(value);
if (!equal(text, newName)) {
status.addError("Illegal name: '" + newName + "'. Consider using '" + text + "' instead.");
}
} catch (ValueConverterException vce) {
status.addFatalError("Illegal name: " + notNull(vce.getMessage()));
}
}
return status;
}
use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-eclipse by eclipse.
the class RefactoringCrossReferenceSerializer method getCrossRefText.
public String getCrossRefText(EObject owner, CrossReference crossref, EObject target, RefTextEvaluator refTextEvaluator, ITextRegion linkTextRegion, StatusWrapper status) {
try {
final EReference ref = GrammarUtil.getReference(crossref, owner.eClass());
final IScope scope = scopeProvider.getScope(owner, ref);
if (scope == null) {
throw new IllegalStateException("Could not create scope for the given cross reference.");
}
String ruleName = linkingHelper.getRuleNameFrom(crossref);
Iterable<IEObjectDescription> descriptionsForCrossRef = scope.getElements(target);
String bestRefText = null;
List<String> badNames = new ArrayList<String>();
for (IEObjectDescription desc : descriptionsForCrossRef) {
try {
String unconvertedRefText = qualifiedNameConverter.toString(desc.getName());
String convertedRefText = valueConverter.toString(unconvertedRefText, ruleName);
if (refTextEvaluator.isValid(desc) && (bestRefText == null || refTextEvaluator.isBetterThan(convertedRefText, bestRefText)))
bestRefText = convertedRefText;
} catch (ValueConverterException e) {
// this is a problem only if we don't find any matching value
badNames.add(desc.getName().toString());
}
}
if (bestRefText == null && !badNames.isEmpty()) {
status.add(WARNING, "Misconfigured language: New reference text has invalid syntax. Following names are in the scope: " + IterableExtensions.join(badNames, ", "), owner, linkTextRegion);
}
return bestRefText;
} catch (Exception exc) {
log.error(exc.getMessage(), exc);
status.add(ERROR, exc.getMessage(), owner, linkTextRegion);
return null;
}
}
use of org.eclipse.xtext.conversion.ValueConverterException in project dsl-devkit by dsldevkit.
the class ScopeLinkingService method getIncludedModel.
/**
* Returns the {@link ScopeModel#getIncludes() included scope model} corresponding to the given node from the node model.
*
* @param context
* context object
* @param node
* parser node corresponding to include reference
* @return referenced {@link ScopeModel} to include
*/
private List<EObject> getIncludedModel(final ScopeModel context, final INode node) {
// based on XtextLinkingService#getUsedGrammar()
try {
String scopeModelName = (String) valueConverterService.toValue("", grammarAccess.getDottedIDRule().getName(), node);
if (scopeModelName != null) {
final ResourceSet resourceSet = context.eResource().getResourceSet();
List<Resource> resources = resourceSet.getResources();
for (int i = 0; i < resources.size(); i++) {
Resource resource = resources.get(i);
EObject rootElement = null;
if (resource instanceof XtextResource && ((XtextResource) resource).getLanguageName().equals(languageName) && !resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}
if (rootElement instanceof ScopeModel) {
ScopeModel otherModel = (ScopeModel) rootElement;
if (scopeModelName.equals(otherModel.getName())) {
return Collections.<EObject>singletonList(otherModel);
}
}
}
URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + scopeModelName.replace('.', '/') + "." + fileExtensionProvider.getPrimaryFileExtension());
final Resource resource = resourceSet.getResource(classpathURI, true);
if (!resource.getContents().isEmpty()) {
final ScopeModel otherModel = (ScopeModel) resource.getContents().get(0);
if (scopeModelName.equals(otherModel.getName())) {
return Collections.<EObject>singletonList(otherModel);
}
}
}
return Collections.emptyList();
} catch (ClasspathUriResolutionException e) {
LOG.warn("Cannot load included scope.", e);
return Collections.emptyList();
} catch (ValueConverterException e) {
LOG.warn("Cannot convert included scope name.", e);
return Collections.emptyList();
}
}
use of org.eclipse.xtext.conversion.ValueConverterException in project dsl-devkit by dsldevkit.
the class AbstractFastLinkingService method getUsedGrammar.
/**
* Tries to find a grammar.
*
* @param resourceSet
* to use for loading
* @param grammarName
* qualified grammar name
* @return A singleton list containing the grammar, or an empty list if not found.
*/
protected List<EObject> getUsedGrammar(final ResourceSet resourceSet, final String grammarName) {
// copied from XtextLinkingService#getUsedGrammar()
try {
if (grammarName != null) {
List<Resource> resources = resourceSet.getResources();
for (int i = 0; i < resources.size(); i++) {
Resource resource = resources.get(i);
EObject rootElement = null;
if (resource instanceof XtextResource) {
IParseResult parseResult = ((XtextResource) resource).getParseResult();
if (parseResult != null) {
rootElement = parseResult.getRootASTElement();
}
} else if (!resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}
if (rootElement instanceof Grammar) {
Grammar otherGrammar = (Grammar) rootElement;
if (grammarName.equals(otherGrammar.getName())) {
if (resource instanceof DerivedStateAwareResource) {
resource.getContents();
}
return Collections.<EObject>singletonList(otherGrammar);
}
}
}
// $NON-NLS-1$ //$NON-NLS-2$
URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + ".xtext");
URI normalizedURI = null;
if (resourceSet instanceof XtextResourceSet) {
XtextResourceSet set = (XtextResourceSet) resourceSet;
normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
} else {
normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
}
final Resource resource = resourceSet.getResource(normalizedURI, true);
if (!resource.getContents().isEmpty()) {
final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
if (grammarName.equals(usedGrammar.getName())) {
return Collections.<EObject>singletonList(usedGrammar);
}
}
}
return Collections.emptyList();
} catch (ClasspathUriResolutionException e) {
return Collections.emptyList();
} catch (ValueConverterException e) {
return Collections.emptyList();
}
}
Aggregations