Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infrastructure for enso --docs option & signature generator #10291

Merged
merged 38 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6be7d9f
Cannot return CompilerContext.Module as that's not TruffleObject
JaroslavTulach Jun 15, 2024
e65eccc
isGenDocs seems to be totally unused
JaroslavTulach Jun 15, 2024
0dd3a5d
Generate documentation for a library into its docs/api folder
JaroslavTulach Jun 15, 2024
b10ae46
Avoid generating content for private modules
JaroslavTulach Jun 15, 2024
87b5026
Include also the documentation in the generated files
JaroslavTulach Jun 15, 2024
cbcb783
Merging with most recent develop
JaroslavTulach Jan 15, 2025
72c9d89
Hint usage of --in-project option when project path isn't specified
JaroslavTulach Jan 15, 2025
ea4f573
Generate documentation via visitor
JaroslavTulach Jan 15, 2025
37583e0
visitConstructor with Definition.Data
JaroslavTulach Jan 15, 2025
dabdbed
Exposing visitModule to tests
JaroslavTulach Jan 16, 2025
bd4f78a
Making runtime test compile
JaroslavTulach Jan 16, 2025
e30adaa
DocsGenerate test skeleton
JaroslavTulach Jan 16, 2025
8cf7fde
Expanding the test with methods
JaroslavTulach Jan 16, 2025
615e8ef
Keeping generateDocs flag in the ModuleContext
JaroslavTulach Jan 16, 2025
9afa696
Generate documentation for a project in the test
JaroslavTulach Jan 16, 2025
f0fe9c4
Process all method bindings for a type when processing a type
JaroslavTulach Jan 16, 2025
f233f28
Helper method to generateDocumentation in the test
JaroslavTulach Jan 16, 2025
69e648d
Extracting types from the IR and generating proper signature for a fu…
JaroslavTulach Jan 16, 2025
e78c17f
Generate signature of a constructor
JaroslavTulach Jan 16, 2025
69bb876
Exposing two standard implementations of DocsVisit interface
JaroslavTulach Jan 16, 2025
ce2194a
Skip self in case of static methods
JaroslavTulach Jan 16, 2025
40b50e9
--docs=api selects the DocsEmitSignatures generator
JaroslavTulach Jan 16, 2025
bd9001d
javafmtAll
JaroslavTulach Jan 16, 2025
124f415
Hiding private constructors and methods
JaroslavTulach Jan 17, 2025
b065fe0
Extract type from Vector Text
JaroslavTulach Jan 17, 2025
9bd0819
Support for union types
JaroslavTulach Jan 17, 2025
eba903c
Write down version of the API format to begin with
JaroslavTulach Jan 17, 2025
682ba26
Return ANY when the type isn't known
JaroslavTulach Jan 17, 2025
f7e1411
Prefix extension methods with type's FQN
JaroslavTulach Jan 17, 2025
ab8ddf7
Specify values in back ampersands
JaroslavTulach Jan 20, 2025
5dd13a7
Using Scala ternary operator
JaroslavTulach Jan 20, 2025
0d63f0a
Associate exitFail with an message. Differentiate between stdout and …
JaroslavTulach Jan 20, 2025
9bfd828
Scala doesn't have ternary operator
JaroslavTulach Jan 20, 2025
6dbc951
Removing commented out line
JaroslavTulach Jan 20, 2025
5c02828
Using PrintWriter instead of Appendable to avoid line separator issues
JaroslavTulach Jan 20, 2025
aeb9a4e
Fail on wrong format
JaroslavTulach Jan 20, 2025
42a9464
Use subdirectories when generating the .md file
JaroslavTulach Jan 20, 2025
c105fe1
scalafmtAll
JaroslavTulach Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ class Module(private val value: Value) {
def evalExpression(code: String): Value =
value.invokeMember(EVAL_EXPRESSION, code)

/** Triggers generation of documentation from module sources.
*
* @return value with `GENERATE_DOCS` invoked on it.
*/
def generateDocs(): Value = {
value.invokeMember(GENERATE_DOCS)
}

/** Triggers gathering of import statements from module sources.
*
* @return value with `GATHER_IMPORT_STATEMENTS` invoked on it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ class TopScope(private val value: Value) {
value.invokeMember(UNREGISTER_MODULE, qualifiedName): Unit
}

def compile(shouldCompileDependencies: Boolean): Unit = {
value.invokeMember(COMPILE, shouldCompileDependencies)
def compile(
shouldCompileDependencies: Boolean,
generateDocs: Boolean = false
): Unit = {
value.invokeMember(COMPILE, shouldCompileDependencies, generateDocs)
}

}
12 changes: 2 additions & 10 deletions engine/runner/src/main/java/org/enso/runner/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ private void compile(

var topScope = context.getTopScope();
try {
topScope.compile(shouldCompileDependencies);
topScope.compile(shouldCompileDependencies, false);
throw exitSuccess();
} catch (Throwable t) {
logger.error("Unexpected internal error", t);
Expand Down Expand Up @@ -772,16 +772,8 @@ private void generateDocsFrom(

if (main.exists(x -> x.exists())) {
var mainFile = main.get();
var mainModuleName = pkg.get().moduleNameForFile(mainFile).toString();
var topScope = executionContext.getTopScope();
var mainModule = topScope.getModule(mainModuleName);
var generated = mainModule.generateDocs();
println(generated.toString());

// TODO:
// - go through executed code and get all HTML docs
// with their corresponding atoms/methods etc.
// - Save those to files
topScope.compile(false, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.enso.compiler.dump;

import java.io.IOException;
import org.enso.compiler.context.CompilerContext;
import org.enso.compiler.core.ir.module.scope.Definition;
import org.enso.compiler.core.ir.module.scope.definition.Method;
import org.enso.compiler.pass.resolve.DocumentationComments;
import org.enso.compiler.pass.resolve.DocumentationComments$;
import org.enso.filesystem.FileSystem;
import scala.collection.immutable.Seq;
import scala.jdk.CollectionConverters;

/** Generator of documentation for an Enso project. */
public final class GenerateDocs {
private GenerateDocs() {}

/**
* Iterate over all provide modules and generate documentation using {@code pkg}'s {@link
* FileSystem}.
*
* @param <File> abstract file to operate with
* @param pkg library to generate the documentation for
* @param modules parsed modules found in the library
* @throws IOException when I/O problem occurs
*/
public static <File> void write(
org.enso.pkg.Package<File> pkg, Iterable<CompilerContext.Module> modules) throws IOException {
var fs = pkg.fileSystem();
var docs = fs.getChild(pkg.root(), "docs");
var api = fs.getChild(docs, "api");
fs.createDirectories(api);

for (var module : modules) {
var ir = module.getIr();
assert ir != null : "need IR for " + module;
var md = fs.getChild(api, module.getName() + ".md");
DONE:
try (var w = fs.newBufferedWriter(md)) {
w.append("## Documentation for " + module.getName() + "\n");

if (ir.isPrivate()) {
w.append("This module is **private**!\n");
break DONE;
}

for (var b : asJava(ir.bindings())) {
switch (b) {
case Definition.Type t -> w.append("#### **type** " + t.name().name() + "\n");
case Definition.Data d -> w.append("#### data " + d.name().name() + "\n");
case Definition.SugaredType s -> w.append("#### sugar " + s.name().name() + "\n");
case Method.Explicit m -> w.append("#### method " + m.methodName().name() + "\n");
case Method.Conversion c -> w.append("#### conversion " + c.methodName().name() + "\n");
default -> throw new AssertionError("unknown type " + b.getClass());
}
var option = b.passData().get(DocumentationComments$.MODULE$);
if (option.isDefined()) {
var doc = (DocumentationComments.Doc) option.get();
w.append(doc.documentation());
w.append("\n\n\n");
}
}
}
}

System.out.println("Documentation generated into " + api);
}

private static <T> Iterable<T> asJava(Seq<T> seq) {
return CollectionConverters.IterableHasAsJava(seq).asJava();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.compiler

import scala.jdk.CollectionConverters.IterableHasAsJava
import org.enso.compiler.context.{
CompilerContext,
FreshNameSupply,
Expand Down Expand Up @@ -139,7 +140,8 @@ class Compiler(
*/
def compile(
shouldCompileDependencies: Boolean,
useGlobalCacheLocations: Boolean
useGlobalCacheLocations: Boolean,
generateDocs: Boolean
): Future[java.lang.Boolean] = {
getPackageRepository.getMainProjectPackage match {
case None =>
Expand Down Expand Up @@ -181,6 +183,11 @@ class Compiler(
shouldCompileDependencies
)

if (generateDocs) {
org.enso.compiler.dump.GenerateDocs
.write(pkg, packageModules.asJava)
}

context.serializeLibrary(
this,
pkg.libraryName,
Expand All @@ -198,8 +205,7 @@ class Compiler(
initialize()
parseModule(
module,
irCachingEnabled && !context.isInteractive(module),
isGenDocs = true
irCachingEnabled && !context.isInteractive(module)
)
module
}
Expand Down Expand Up @@ -537,8 +543,7 @@ class Compiler(

private def parseModule(
module: Module,
useCaches: Boolean,
isGenDocs: Boolean = false
useCaches: Boolean
): Unit = {
context.log(
Compiler.defaultLogLevel,
Expand All @@ -553,7 +558,7 @@ class Compiler(
}
}

uncachedParseModule(module, isGenDocs)
uncachedParseModule(module)
}

/** Retrieve module bindings from cache, if available.
Expand All @@ -570,7 +575,7 @@ class Compiler(
} else None
}

private def uncachedParseModule(module: Module, isGenDocs: Boolean): Unit = {
private def uncachedParseModule(module: Module): Unit = {
context.log(
Compiler.defaultLogLevel,
"Loading module [{0}] from source.",
Expand All @@ -579,10 +584,9 @@ class Compiler(
context.updateModule(module, _.resetScope())

val moduleContext = ModuleContext(
module = module,
freshNameSupply = Some(freshNameSupply),
compilerConfig = config,
isGeneratingDocs = isGenDocs
module = module,
freshNameSupply = Some(freshNameSupply),
compilerConfig = config
)

val src = context.getCharacters(module)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ case class ModuleContext(
compilerConfig: CompilerConfig,
freshNameSupply: Option[FreshNameSupply] = None,
passConfiguration: Option[PassConfiguration] = None,
isGeneratingDocs: Boolean = false,
pkgRepo: Option[PackageRepository] = None
) {
def isSynthetic(): Boolean = module.isSynthetic()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ protected ExecutableNode parse(InlineParsingRequest request) throws InlineParsin
redirectConfigWithStrictErrors,
scala.Option.empty(),
scala.Option.empty(),
false,
scala.Option.empty());
var inlineContext =
new InlineContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,10 @@ private static Object evalExpression(
}

private static Object generateDocs(Module module, EnsoContext context) {
return context.getCompiler().generateDocs(module.asCompilerModule());
var compilerModule = module.asCompilerModule();
var res = context.getCompiler().generateDocs(compilerModule);
assert res == compilerModule;
return module;
}

@CompilerDirectives.TruffleBoundary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,24 @@ private static Object leakContext(EnsoContext context) {
private static Object compile(Object[] arguments, EnsoContext context)
throws UnsupportedTypeException, ArityException {
boolean useGlobalCache = context.isUseGlobalCache();
boolean shouldCompileDependencies = Types.extractArguments(arguments, Boolean.class);
boolean shouldCompileDependencies;
boolean generateDocs;
switch (arguments.length) {
case 2 -> {
var pair = Types.extractArguments(arguments, Boolean.class, Boolean.class);
shouldCompileDependencies = pair.getFirst();
generateDocs = pair.getSecond();
}
default -> {
shouldCompileDependencies = Types.extractArguments(arguments, Boolean.class);
generateDocs = false;
}
}
try {
return context.getCompiler().compile(shouldCompileDependencies, useGlobalCache).get();
return context
.getCompiler()
.compile(shouldCompileDependencies, useGlobalCache, generateDocs)
.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
Expand Down
Loading