Search in sources :

Example 16 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class RelativeSourcePath 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.sourcePaths(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);
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IProject(org.metaborg.core.project.IProject)

Example 17 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class LanguageIncludeDirectoriesPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException, IOException {
    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 Iterable<FileObject> includeLocations = languagePathService.includePaths(project, languageName);
    final List<IStrategoTerm> terms = Lists.newArrayList();
    for (FileObject includeLocation : includeLocations) {
        terms.add(factory.makeString(includeLocation.getName().getURI()));
    }
    return factory.makeList(terms);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) FileObject(org.apache.commons.vfs2.FileObject) IProject(org.metaborg.core.project.IProject)

Example 18 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class OutlineService method outline.

@Override
public IOutline outline(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<OutlineFacet> facetContrib = facet(langImpl);
    final OutlineFacet facet = facetContrib.facet;
    final ILanguageComponent contributor = facetContrib.contributor;
    final String strategy = facet.strategyName;
    try {
        final HybridInterpreter interpreter;
        if (context == null) {
            interpreter = strategoRuntimeService.runtime(contributor, source, true);
        } else {
            interpreter = strategoRuntimeService.runtime(contributor, context, true);
        }
        final IStrategoTerm input = common.builderInputTerm(result.ast(), source, source);
        final IStrategoTerm outlineTerm = common.invoke(interpreter, input, strategy);
        if (outlineTerm == null) {
            return null;
        }
        final IOutline outline = toOutline(outlineTerm, facet.expandTo, contributor.location());
        return outline;
    } catch (MetaborgException e) {
        throw new MetaborgException("Creating outline failed", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IOutline(org.metaborg.core.outline.IOutline) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IProject(org.metaborg.core.project.IProject) ContextException(org.metaborg.core.context.ContextException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) Nullable(javax.annotation.Nullable)

Example 19 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class LanguageSpecPpNamePrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
    final FileObject location = context.location();
    final IProject project = projectService.get(location);
    if (project == null) {
        return null;
    }
    if (languageSpecServiceProvider == null) {
        // Indicates that meta-Spoofax is not available (ISpoofaxLanguageSpecService cannot be injected), but this
        // should never happen because this primitive is inside meta-Spoofax. Check for null just in case.
        logger.error("Language specification service is not available; static injection failed");
        return null;
    }
    final ISpoofaxLanguageSpecService languageSpecService = languageSpecServiceProvider.get();
    if (!languageSpecService.available(project)) {
        return null;
    }
    final ISpoofaxLanguageSpec languageSpec;
    try {
        languageSpec = languageSpecService.get(project);
    } catch (ConfigException e) {
        throw new MetaborgException("Unable to get language specification name for " + location, e);
    }
    if (languageSpec == null) {
        return null;
    }
    return factory.makeString(languageSpec.config().prettyPrintLanguage());
}
Also used : ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) ISpoofaxLanguageSpecService(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService) MetaborgException(org.metaborg.core.MetaborgException) ConfigException(org.metaborg.core.config.ConfigException) FileObject(org.apache.commons.vfs2.FileObject) IProject(org.metaborg.core.project.IProject)

Example 20 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class HoverService method hover.

@Override
public Hover hover(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
    if (!result.valid() || !result.hasAst()) {
        return null;
    }
    final FileObject source = result.source();
    final IContext context = result.context();
    final ILanguageImpl language = context.language();
    final FacetContribution<HoverFacet> facetContrib = facet(language);
    final HoverFacet facet = facetContrib.facet;
    final String strategy = facet.strategyName;
    try {
        final IProject project = context.project();
        final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
        final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple;
        try (IClosableLock lock = context.read()) {
            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);
    }
}
Also used : IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Aggregations

IProject (org.metaborg.core.project.IProject)21 FileObject (org.apache.commons.vfs2.FileObject)13 MetaborgException (org.metaborg.core.MetaborgException)12 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)9 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)8 IContext (org.metaborg.core.context.IContext)6 HybridInterpreter (org.strategoxt.HybridInterpreter)6 ConfigException (org.metaborg.core.config.ConfigException)5 ISpoofaxLanguageSpec (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec)5 ISpoofaxLanguageSpecService (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)4 SourceRegion (org.metaborg.core.source.SourceRegion)4 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)4 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)4 Nullable (javax.annotation.Nullable)3 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)3 ContextException (org.metaborg.core.context.ContextException)3 ILanguage (org.metaborg.core.language.ILanguage)3 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)3 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)3