Skip to content

Commit

Permalink
Using PrintWriter instead of Appendable to avoid line separator issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Jan 20, 2025
1 parent 6dbc951 commit 5c02828
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Class to use from {@link DocsGenerate} to dispatch individual IR elements to provided visitor.
*/
abstract class DocsDispatch {
static DocsDispatch create(DocsVisit visitor, Appendable writer) {
static DocsDispatch create(DocsVisit visitor, java.io.PrintWriter writer) {
return new DocsDispatch() {
@Override
boolean dispatchModule(QualifiedName name, Module ir) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.compiler.dump;

import java.io.IOException;
import java.io.PrintWriter;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.Module;
import org.enso.compiler.core.ir.module.scope.Definition;
Expand All @@ -13,47 +14,48 @@
final class DocsEmitMarkdown implements DocsVisit {

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

@Override
public boolean visitModule(QualifiedName name, Module module, Appendable w) throws IOException {
w.append("## Documentation for " + name + "\n");
public boolean visitModule(QualifiedName name, Module module, PrintWriter w) throws IOException {
w.println("## Documentation for " + name);
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");
public void visitMethod(Definition.Type t, Method.Explicit m, PrintWriter w) throws IOException {
w.println("#### method " + m.methodName().name());
writeDocs(m, w);
}

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

private void writeDocs(IR b, Appendable w) throws IOException {
private void writeDocs(IR b, PrintWriter 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");
w.println(doc.documentation());
w.println();
w.println();
}
}

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

@Override
public void visitConstructor(Definition.Type t, Definition.Data d, Appendable w)
public void visitConstructor(Definition.Type t, Definition.Data d, PrintWriter w)
throws IOException {
w.append("#### data " + d.name().name() + "\n");
w.println("#### data " + d.name().name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.enso.scala.wrapper.ScalaConversions.asJava;

import java.io.IOException;
import java.io.PrintWriter;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.Module;
import org.enso.compiler.core.ir.module.scope.Definition;
Expand All @@ -13,20 +14,20 @@
final class DocsEmitSignatures implements DocsVisit {

@Override
public boolean visitUnknown(IR ir, Appendable w) throws IOException {
w.append("- Unknown IR " + ir.getClass().getName() + "\n");
public boolean visitUnknown(IR ir, PrintWriter w) throws IOException {
w.println("- Unknown IR " + ir.getClass().getName());
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");
public boolean visitModule(QualifiedName name, Module module, PrintWriter w) throws IOException {
w.println("## Enso Signatures 1.0");
w.println("## module " + name);
return true;
}

@Override
public void visitMethod(Definition.Type t, Method.Explicit m, Appendable w) throws IOException {
public void visitMethod(Definition.Type t, Method.Explicit m, PrintWriter w) throws IOException {
if (t != null) {
w.append(" - ");
} else {
Expand All @@ -35,29 +36,28 @@ public void visitMethod(Definition.Type t, Method.Explicit m, Appendable w) thro
w.append(fqn + ".");
}
}
w.append(DocsVisit.toSignature(m) + "\n");
w.println(DocsVisit.toSignature(m));
}

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

@Override
public boolean visitType(Definition.Type t, Appendable w) throws IOException {
public boolean visitType(Definition.Type t, PrintWriter 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());
w.println(sb.toString());
return true;
}

@Override
public void visitConstructor(Definition.Type t, Definition.Data d, Appendable w)
public void visitConstructor(Definition.Type t, Definition.Data d, PrintWriter w)
throws IOException {
w.append(" - " + DocsVisit.toSignature(d) + "\n");
w.println(" - " + DocsVisit.toSignature(d));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.compiler.dump;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.IdentityHashMap;
import org.enso.compiler.context.CompilerContext;
import org.enso.compiler.core.IR;
Expand Down Expand Up @@ -43,15 +44,16 @@ public static <File> File write(
}
var moduleName = module.getName();
var md = fs.getChild(api, moduleName + ".md");
try (var mdWriter = fs.newBufferedWriter(md)) {
visitModule(visitor, moduleName, ir, mdWriter);
try (var mdWriter = fs.newBufferedWriter(md);
var pw = new PrintWriter(mdWriter)) {
visitModule(visitor, moduleName, ir, pw);
}
}
return api;
}

public static void visitModule(
DocsVisit visitor, QualifiedName moduleName, Module ir, Appendable w) throws IOException {
DocsVisit visitor, QualifiedName moduleName, Module ir, PrintWriter w) throws IOException {
var dispatch = DocsDispatch.create(visitor, w);

if (dispatch.dispatchModule(moduleName, ir)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.compiler.dump;

import java.io.IOException;
import java.io.PrintWriter;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.DefinitionArgument;
import org.enso.compiler.core.ir.Module;
Expand All @@ -14,17 +15,17 @@
* with the {@link IR}.
*/
public interface DocsVisit {
boolean visitModule(QualifiedName name, Module ir, Appendable writer) throws IOException;
boolean visitModule(QualifiedName name, Module ir, PrintWriter writer) throws IOException;

boolean visitUnknown(IR ir, Appendable w) throws IOException;
boolean visitUnknown(IR ir, PrintWriter w) throws IOException;

void visitMethod(Definition.Type t, Method.Explicit m, Appendable writer) throws IOException;
void visitMethod(Definition.Type t, Method.Explicit m, PrintWriter writer) throws IOException;

void visitConversion(Method.Conversion c, Appendable w) throws IOException;
void visitConversion(Method.Conversion c, PrintWriter w) throws IOException;

boolean visitType(Definition.Type t, Appendable w) throws IOException;
boolean visitType(Definition.Type t, PrintWriter w) throws IOException;

void visitConstructor(Definition.Type t, Definition.Data d, Appendable w) throws IOException;
void visitConstructor(Definition.Type t, Definition.Data d, PrintWriter w) throws IOException;

//
// helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import org.enso.compiler.Compiler;
Expand Down Expand Up @@ -346,37 +347,37 @@ private static final class MockVisitor implements DocsVisit {
private final List<Method.Conversion> visitConversion = new ArrayList<>();

@Override
public boolean visitModule(QualifiedName name, Module ir, Appendable writer)
public boolean visitModule(QualifiedName name, Module ir, PrintWriter writer)
throws IOException {
visitModule.add(ir);
return true;
}

@Override
public boolean visitUnknown(IR ir, Appendable w) throws IOException {
public boolean visitUnknown(IR ir, PrintWriter w) throws IOException {
visitUnknown.add(ir);
return true;
}

@Override
public void visitMethod(Definition.Type t, Method.Explicit m, Appendable writer)
public void visitMethod(Definition.Type t, Method.Explicit m, PrintWriter writer)
throws IOException {
visitMethod.add(new TypeAnd<>(t, m));
}

@Override
public void visitConversion(Method.Conversion c, Appendable w) throws IOException {
public void visitConversion(Method.Conversion c, PrintWriter w) throws IOException {
visitConversion.add(c);
}

@Override
public boolean visitType(Definition.Type t, Appendable w) throws IOException {
public boolean visitType(Definition.Type t, PrintWriter w) throws IOException {
visitType.add(t);
return true;
}

@Override
public void visitConstructor(Definition.Type t, Definition.Data d, Appendable w)
public void visitConstructor(Definition.Type t, Definition.Data d, PrintWriter w)
throws IOException {
visitConstructor.add(d);
}
Expand Down

0 comments on commit 5c02828

Please sign in to comment.