use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class LanguageIncludeFilesPrimitive method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
if (!Tools.isTermString(tvars[0])) {
return null;
}
final IProject project = projectService.get(context.location());
if (project == null) {
return factory.makeList();
}
// GTODO: require language identifier instead of language name
final String languageName = Tools.asJavaString(tvars[0]);
final ILanguage language = languageService.getLanguage(languageName);
if (language == null) {
final String message = String.format("Getting include files for %s failed, language could not be found", languageName);
throw new MetaborgException(message);
}
final ILanguageImpl impl = language.activeImpl();
if (impl == null) {
final String message = String.format("Getting include files for %s failed, no active language implementation could be found", languageName);
throw new MetaborgException(message);
}
final Iterable<IdentifiedResource> includeFiles = languagePathService.includeFiles(project, impl);
final List<IStrategoTerm> terms = Lists.newArrayList();
for (IdentifiedResource includeFile : includeFiles) {
terms.add(factory.makeString(includeFile.resource.getName().getURI()));
}
return factory.makeList(terms);
}
use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class RelativeSourceOrIncludePath method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) {
if (!Tools.isTermString(tvars[0])) {
return null;
}
if (!Tools.isTermString(current)) {
return null;
}
final String path = Tools.asJavaString(current);
final FileObject resource = resourceService.resolve(context.project().location(), path);
FileObject base = context.location();
final IProject project = projectService.get(context.location());
if (project != null) {
// GTODO: require language identifier instead of language name
final String languageName = Tools.asJavaString(tvars[0]);
final Iterable<FileObject> sourceLocations = languagePathService.sourceAndIncludePaths(project, languageName);
for (FileObject sourceLocation : sourceLocations) {
if (sourceLocation.getName().isDescendent(resource.getName())) {
base = sourceLocation;
break;
}
}
}
final String relativePath = ResourceUtils.relativeName(resource.getName(), base.getName(), true);
return factory.makeString(relativePath);
}
use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class CLIUtils method getOrCreateProject.
/**
* Get or create project for the given resource
*/
public IProject getOrCreateProject(FileObject resource) throws MetaborgException {
final ISimpleProjectService projectService = spoofax.injector.getInstance(ISimpleProjectService.class);
final IProject project = projectService.get(resource);
if (project == null) {
return projectService.create(resource);
}
return project;
}
use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class LegacyForeignCallPrimitive method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext currentContext) throws MetaborgException {
final String languageName = Tools.asJavaString(tvars[0]);
final String strategyName = Tools.asJavaString(tvars[1]);
// GTODO: require language identifier instead of language name
final ILanguage language = languageService.getLanguage(languageName);
if (language == null) {
final String message = String.format("Stratego strategy call of '%s' into language %s failed, language could not be found", strategyName, languageName);
throw new MetaborgException(message);
}
final ILanguageImpl activeImpl = language.activeImpl();
if (activeImpl == null) {
final String message = String.format("Stratego strategy call of '%s' into language %s failed, no active language implementation could be found", strategyName, languageName);
throw new MetaborgException(message);
}
try {
final IProject project = projectService.get(currentContext.location());
IContext context = contextService.get(currentContext.location(), project, activeImpl);
return common.invoke(activeImpl, context, current, strategyName);
} catch (MetaborgException e) {
final String message = String.format("Stratego strategy call of '%s' into language %s failed unexpectedly", strategyName, languageName);
throw new MetaborgException(message, e);
}
}
use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class HoverService method hover.
@Override
public Hover hover(int offset, ISpoofaxParseUnit result) throws MetaborgException {
if (!result.valid()) {
return null;
}
final FileObject source = result.source();
final IProject project = projectService.get(source);
final ILanguageImpl langImpl = result.input().langImpl();
@Nullable IContext context;
if (project == null) {
context = null;
} else {
try {
context = contextService.get(source, project, langImpl);
} catch (ContextException | MetaborgRuntimeException e) {
// Failed to get a context, ignore and use the source file to get a stratego runtime later.
context = null;
}
}
final FacetContribution<HoverFacet> facetContrib = facet(langImpl);
final HoverFacet facet = facetContrib.facet;
final ILanguageComponent contributor = facetContrib.contributor;
final String strategy = facet.strategyName;
try {
final ITermFactory termFactory = termFactoryService.get(contributor, project, true);
final HybridInterpreter interpreter;
if (context == null) {
interpreter = strategoRuntimeService.runtime(contributor, source, true);
} else {
interpreter = strategoRuntimeService.runtime(contributor, context, true);
}
final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
final TermWithRegion tuple = common.outputs(termFactory, interpreter, context.location(), source, result.ast(), inRegion, strategy);
return hover(tuple);
} catch (MetaborgException e) {
throw new MetaborgException("Getting hover tooltip information failed unexpectedly", e);
}
}
Aggregations