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 31 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,24 @@ 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
): Unit = {
compile(shouldCompileDependencies, None)
}
def compile(
shouldCompileDependencies: Boolean,
generateDocs: Option[String]
): Unit = {
val docsArg = generateDocs.map {
case "api" => "api"
case _ => "md"
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
}
value.invokeMember(
COMPILE,
shouldCompileDependencies,
docsArg.getOrElse(false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this correct? It seems that the underlying value of docsArg is a String?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was:

  • false - don't generate
  • true - generate and use default "style"
  • any String - generate and use the "style" specified by the string

)
}

}
40 changes: 22 additions & 18 deletions engine/runner/src/main/java/org/enso/runner/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ private static Options buildOptions() {
.build();
var docs =
cliOptionBuilder()
.hasArg(true)
.numberOfArgs(1)
.optionalArg(true)
.longOpt(DOCS_OPTION)
.desc("Runs the Enso documentation generator.")
.desc(
"Runs the Enso documentation generator. Additional argument may specify format -"
+ " either the default `md` or `api`.")
.build();
var preinstall =
cliOptionBuilder()
Expand Down Expand Up @@ -652,7 +657,7 @@ private void compile(

var topScope = context.getTopScope();
try {
topScope.compile(shouldCompileDependencies);
topScope.compile(shouldCompileDependencies, scala.Option.empty());
throw exitSuccess();
} catch (Throwable t) {
logger.error("Unexpected internal error", t);
Expand Down Expand Up @@ -785,12 +790,16 @@ private void handleRun(
* @param enableIrCaches are the IR caches enabled
*/
private void genDocs(
String projectPath, Level logLevel, boolean logMasking, boolean enableIrCaches) {
if (projectPath.isEmpty()) {
println("Path hasn't been provided.");
String docsFormat,
String projectPath,
Level logLevel,
boolean logMasking,
boolean enableIrCaches) {
if (projectPath == null || projectPath.isEmpty()) {
println("Specify path to a project with --in-project option");
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
throw exitFail();
}
generateDocsFrom(projectPath, logLevel, logMasking, enableIrCaches);
generateDocsFrom(docsFormat, projectPath, logLevel, logMasking, enableIrCaches);
throw exitSuccess();
}

Expand All @@ -799,7 +808,7 @@ private void genDocs(
* path.
*/
private void generateDocsFrom(
String path, Level logLevel, boolean logMasking, boolean enableIrCaches) {
String docsFormat, String path, Level logLevel, boolean logMasking, boolean enableIrCaches) {
var executionContext =
new PolyglotContext(
ContextFactory.create()
Expand All @@ -816,17 +825,8 @@ private void generateDocsFrom(
var main = pkg.map(x -> x.mainFile());

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, scala.Option.apply(docsFormat == null ? "md" : docsFormat));
}
}

Expand Down Expand Up @@ -1164,7 +1164,11 @@ final void mainEntry(CommandLine line, Level logLevel, boolean logMasking) throw
}
if (line.hasOption(DOCS_OPTION)) {
genDocs(
line.getOptionValue(IN_PROJECT_OPTION), logLevel, logMasking, shouldEnableIrCaches(line));
line.getOptionValue(DOCS_OPTION),
line.getOptionValue(IN_PROJECT_OPTION),
logLevel,
logMasking,
shouldEnableIrCaches(line));
}
if (line.hasOption(PREINSTALL_OPTION)) {
preinstallDependencies(line.getOptionValue(IN_PROJECT_OPTION), logLevel);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.enso.compiler.dump;

import java.io.IOException;
import org.enso.compiler.core.ir.Module;
import org.enso.compiler.core.ir.module.scope.Definition;
import org.enso.compiler.core.ir.module.scope.definition.Method;
import org.enso.pkg.QualifiedName;

/**
* Class to use from {@link DocsGenerate} to dispatch individual IR elements to provided visitor.
*/
abstract class DocsDispatch {
static DocsDispatch create(DocsVisit visitor, Appendable writer) {
return new DocsDispatch() {
@Override
boolean dispatchModule(QualifiedName name, Module ir) throws IOException {
return visitor.visitModule(name, ir, writer);
}

@Override
void dispatchMethod(Definition.Type t, Method.Explicit m) throws IOException {
visitor.visitMethod(t, m, writer);
}

@Override
void dispatchConversion(Method.Conversion c) throws IOException {
visitor.visitConversion(c, writer);
}

@Override
boolean dispatchType(Definition.Type t) throws IOException {
return visitor.visitType(t, writer);
}

@Override
void dispatchConstructor(Definition.Type t, Definition.Data d) throws IOException {
visitor.visitConstructor(t, d, writer);
}
};
}

private DocsDispatch() {}

abstract boolean dispatchModule(QualifiedName name, Module ir) throws IOException;

abstract void dispatchMethod(Definition.Type t, Method.Explicit m) throws IOException;

abstract void dispatchConversion(Method.Conversion c) throws IOException;

abstract boolean dispatchType(Definition.Type t) throws IOException;

abstract void dispatchConstructor(Definition.Type t, Definition.Data d) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.enso.compiler.dump;

import java.io.IOException;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.Module;
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.pkg.QualifiedName;

/** Visitor that emits documentation in markdown format. */
final class DocsEmitMarkdown implements DocsVisit {

@Override
public boolean visitUnknown(IR ir, Appendable w) {
return true;
}

@Override
public boolean visitModule(QualifiedName name, Module module, Appendable w) throws IOException {
w.append("## Documentation for " + name + "\n");
writeDocs(module, w);
return true;
}

@Override
public void visitMethod(Definition.Type t, Method.Explicit m, Appendable w) throws IOException {
w.append("#### method " + m.methodName().name() + "\n");
writeDocs(m, w);
}

@Override
public void visitConversion(Method.Conversion c, Appendable w) throws IOException {
w.append("#### conversion " + c.methodName().name() + "\n");
writeDocs(c, w);
}

private void writeDocs(IR b, Appendable w) throws IOException {
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");
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Override
public boolean visitType(Definition.Type t, Appendable w) throws IOException {
w.append("#### **type** " + t.name().name() + "\n");
return true;
}

@Override
public void visitConstructor(Definition.Type t, Definition.Data d, Appendable w)
throws IOException {
w.append("#### data " + d.name().name() + "\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.enso.compiler.dump;

import static org.enso.scala.wrapper.ScalaConversions.asJava;

import java.io.IOException;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.Module;
import org.enso.compiler.core.ir.module.scope.Definition;
import org.enso.compiler.core.ir.module.scope.definition.Method;
import org.enso.pkg.QualifiedName;

/** Visitor that emits documentation in markdown format. */
final class DocsEmitSignatures implements DocsVisit {

@Override
public boolean visitUnknown(IR ir, Appendable w) throws IOException {
w.append("- Unknown IR " + ir.getClass().getName() + "\n");
return true;
}

@Override
public boolean visitModule(QualifiedName name, Module module, Appendable w) throws IOException {
w.append("## Enso Signatures 1.0\n");
w.append("## module " + name + "\n");
return true;
}

@Override
public void visitMethod(Definition.Type t, Method.Explicit m, Appendable w) throws IOException {
if (t != null) {
w.append(" - ");
} else {
if (m.typeName().isDefined()) {
var fqn = DocsUtils.toFqnOrSimpleName(m.typeName().get());
w.append(fqn + ".");
}
}
w.append(DocsVisit.toSignature(m) + "\n");
}

@Override
public void visitConversion(Method.Conversion c, Appendable w) throws IOException {
w.append("#### conversion " + c.methodName().name() + "\n");
}

@Override
public boolean visitType(Definition.Type t, Appendable w) throws IOException {
var sb = new StringBuilder();
sb.append("- type ").append(t.name().name());
for (var a : asJava(t.params())) {
sb.append(" ").append(DocsVisit.toSignature(a));
}
sb.append("\n");
w.append(sb.toString());
return true;
}

@Override
public void visitConstructor(Definition.Type t, Definition.Data d, Appendable w)
throws IOException {
w.append(" - " + DocsVisit.toSignature(d) + "\n");
}
}
Loading
Loading