-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add public API for evaluating worksheets. (#208)
Add public API for evaluating worksheets.
- Loading branch information
Showing
22 changed files
with
648 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
mdoc-interfaces/src/main/scala/mdoc/interfaces/Diagnostic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mdoc.interfaces; | ||
|
||
public abstract class Diagnostic { | ||
|
||
public abstract RangePosition position(); | ||
public abstract String message(); | ||
public abstract DiagnosticSeverity severity(); | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
mdoc-interfaces/src/main/scala/mdoc/interfaces/DiagnosticSeverity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package mdoc.interfaces; | ||
|
||
public enum DiagnosticSeverity { Info, Warning, Error} |
10 changes: 10 additions & 0 deletions
10
mdoc-interfaces/src/main/scala/mdoc/interfaces/EvaluatedWorksheet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mdoc.interfaces; | ||
|
||
import java.util.List; | ||
|
||
public abstract class EvaluatedWorksheet { | ||
|
||
public abstract List<Diagnostic> diagnostics(); | ||
public abstract List<EvaluatedWorksheetStatement> statements(); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
mdoc-interfaces/src/main/scala/mdoc/interfaces/EvaluatedWorksheetStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mdoc.interfaces; | ||
|
||
public abstract class EvaluatedWorksheetStatement { | ||
|
||
public abstract RangePosition position(); | ||
public abstract String summary(); | ||
public abstract String details(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mdoc.interfaces; | ||
|
||
import java.util.List; | ||
import java.nio.file.Path; | ||
import java.io.PrintStream; | ||
|
||
public abstract class Mdoc { | ||
|
||
public abstract EvaluatedWorksheet evaluateWorksheet(String filename, String text); | ||
public abstract Mdoc withClasspath(List<Path> classpath); | ||
public abstract Mdoc withScalacOptions(List<String> options); | ||
public abstract Mdoc withSettings(List<String> settings); | ||
public abstract Mdoc withConsoleReporter(PrintStream out); | ||
public abstract Mdoc withScreenHeight(int screenHeight); | ||
public abstract Mdoc withScreenWidth(int screenWidth); | ||
public abstract void shutdown(); | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
mdoc-interfaces/src/main/scala/mdoc/interfaces/RangePosition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mdoc.interfaces; | ||
|
||
public abstract class RangePosition { | ||
public abstract int startLine(); | ||
public abstract int startColumn(); | ||
public abstract int endLine(); | ||
public abstract int endColumn(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mdoc.internal.worksheets.Mdoc |
10 changes: 10 additions & 0 deletions
10
mdoc/src/main/scala-2.11/mdoc/internal/worksheets/Compat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mdoc.internal.worksheets | ||
|
||
import scala.tools.nsc.Global | ||
|
||
object Compat { | ||
def usedDummy() = () // Only here to avoid "unused import" warning. | ||
implicit class XtensionCompiler(compiler: Global) { | ||
def close(): Unit = () // do nothing, not available | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
mdoc/src/main/scala-2.12/mdoc/internal/worksheets/Compat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mdoc.internal.worksheets | ||
|
||
object Compat { | ||
def usedDummy() = () // Only here to avoid "unused import" warning. | ||
} |
5 changes: 5 additions & 0 deletions
5
mdoc/src/main/scala-2.13/mdoc/internal/worksheets/Compat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mdoc.internal.worksheets | ||
|
||
object Compat { | ||
def usedDummy() = () // Only here to avoid "unused import" warning. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mdoc.internal.io | ||
|
||
import mdoc.interfaces.DiagnosticSeverity | ||
import mdoc.interfaces.RangePosition | ||
|
||
case class Diagnostic( | ||
val position: RangePosition, | ||
val message: String, | ||
val severity: DiagnosticSeverity | ||
) extends mdoc.interfaces.Diagnostic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package mdoc.internal.io | ||
|
||
import scala.meta._ | ||
import scala.collection.mutable | ||
import java.io.ByteArrayOutputStream | ||
import java.io.PrintStream | ||
import mdoc.interfaces.DiagnosticSeverity | ||
import mdoc.document.RangePosition | ||
import mdoc.internal.pos.PositionSyntax._ | ||
|
||
class StoreReporter() extends ConsoleReporter(System.out) { | ||
val diagnostics: mutable.LinkedHashSet[Diagnostic] = | ||
mutable.LinkedHashSet.empty[Diagnostic] | ||
|
||
override def reset(): Unit = diagnostics.clear() | ||
|
||
override def warningCount: Int = | ||
diagnostics.count(_.severity == DiagnosticSeverity.Warning) | ||
override def errorCount: Int = | ||
diagnostics.count(_.severity == DiagnosticSeverity.Error) | ||
override def hasErrors: Boolean = errorCount > 0 | ||
override def hasWarnings: Boolean = warningCount > 0 | ||
|
||
override def warning(pos: Position, msg: String): Unit = { | ||
diagnostics += new Diagnostic( | ||
pos.toMdoc, | ||
msg, | ||
DiagnosticSeverity.Warning | ||
) | ||
super.warning(pos, msg) | ||
} | ||
override def error(pos: Position, throwable: Throwable): Unit = { | ||
val out = new ByteArrayOutputStream() | ||
throwable.printStackTrace(new PrintStream(out)) | ||
diagnostics += new Diagnostic( | ||
pos.toMdoc, | ||
out.toString(), | ||
DiagnosticSeverity.Error | ||
) | ||
} | ||
override def error(pos: Position, msg: String): Unit = { | ||
diagnostics += new Diagnostic( | ||
pos.toMdoc, | ||
msg, | ||
DiagnosticSeverity.Error | ||
) | ||
super.error(pos, msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
mdoc/src/main/scala/mdoc/internal/worksheets/EvaluatedWorksheet.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mdoc.internal.worksheets | ||
|
||
import java.{util => ju} | ||
import mdoc.{interfaces => i} | ||
|
||
case class EvaluatedWorksheet( | ||
val diagnostics: ju.List[i.Diagnostic], | ||
val statements: ju.List[i.EvaluatedWorksheetStatement] | ||
) extends mdoc.interfaces.EvaluatedWorksheet |
8 changes: 8 additions & 0 deletions
8
mdoc/src/main/scala/mdoc/internal/worksheets/EvaluatedWorksheetStatement.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mdoc.internal.worksheets | ||
import mdoc.interfaces.RangePosition | ||
|
||
case class EvaluatedWorksheetStatement( | ||
val position: RangePosition, | ||
val summary: String, | ||
val details: String | ||
) extends mdoc.interfaces.EvaluatedWorksheetStatement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package mdoc.internal.worksheets | ||
|
||
import java.{util => ju} | ||
import java.io.File | ||
import java.io.PrintStream | ||
import java.nio.file.Path | ||
import scala.collection.JavaConverters._ | ||
import mdoc.{interfaces => i} | ||
import mdoc.internal.cli.Context | ||
import mdoc.internal.cli.Settings | ||
import scala.meta.internal.io.PathIO | ||
import mdoc.internal.io.ConsoleReporter | ||
import mdoc.internal.markdown.MarkdownCompiler | ||
import scala.meta.inputs.Input | ||
import mdoc.internal.worksheets.Compat._ | ||
import mdoc.MainSettings | ||
|
||
class Mdoc(settings: MainSettings) extends i.Mdoc { | ||
|
||
private var myContext: Context = null | ||
|
||
def this() = this(MainSettings()) | ||
|
||
def withClasspath(classpath: ju.List[Path]): i.Mdoc = | ||
new Mdoc(this.settings.withClasspath(classpath.iterator().asScala.mkString(File.pathSeparator))) | ||
def withScalacOptions(options: ju.List[String]): i.Mdoc = | ||
new Mdoc(this.settings.withScalacOptions(options.iterator().asScala.mkString(" "))) | ||
def withSettings(settings: ju.List[String]): i.Mdoc = | ||
new Mdoc(this.settings.withArgs(settings.iterator().asScala.toList)) | ||
def withConsoleReporter(out: PrintStream): i.Mdoc = | ||
new Mdoc(this.settings.withReporter(new ConsoleReporter(out))) | ||
def withScreenWidth(screenWidth: Int): i.Mdoc = | ||
new Mdoc(this.settings.withScreenWidth(screenWidth)) | ||
def withScreenHeight(screenHeight: Int): i.Mdoc = | ||
new Mdoc(this.settings.withScreenHeight(screenHeight)) | ||
|
||
def shutdown(): Unit = { | ||
if (myContext != null) { | ||
myContext.compiler.global.close() | ||
usedDummy() | ||
} | ||
} | ||
|
||
def evaluateWorksheet(filename: String, text: String): EvaluatedWorksheet = | ||
new WorksheetProvider(settings.settings).evaluateWorksheet( | ||
Input.VirtualFile(filename, text), | ||
context | ||
) | ||
|
||
private def context(): Context = { | ||
if (myContext == null) { | ||
myContext = Context( | ||
settings.settings, | ||
settings.reporter, | ||
MarkdownCompiler.fromClasspath( | ||
settings.settings.classpath, | ||
settings.settings.scalacOptions | ||
) | ||
) | ||
} | ||
myContext | ||
} | ||
} |
Oops, something went wrong.