Skip to content

[do not merge] integration with Intellij IDEA #1011

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

Closed
wants to merge 9 commits into from
Closed
42 changes: 0 additions & 42 deletions src/dotty/tools/dotc/CompilerCallback.scala

This file was deleted.

1 change: 1 addition & 0 deletions src/dotty/tools/dotc/Driver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dotty.tools.dotc

import config.CompilerCommand
import core.Contexts.{Context, ContextBase}
import callbacks.CompilerCallback
import util.DotClass
import reporting._
import scala.util.control.NonFatal
Expand Down
67 changes: 67 additions & 0 deletions src/dotty/tools/dotc/callbacks/CompilerCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dotty.tools.dotc.callbacks;

import java.io.File;

/** This interface contains methods that can be implemented to execute code during the
* compilation process.
*
* NOTE: This interface is used for integration of Dotty compiler with Intellij IDEA.
* If you want to change it please contact Scala Plugin team.
*
* Example usage:
* {{{
* val args: Array[String] = ...
* val callback = new CompilerCallback {
* override def onClassGenerated(source: File, generatedClass: File, className: String) =
* println(s"onClassGenerated($source, $generatedClass, $className)")
* override def onSourceCompiled(source: File) =
* println(s"onSourceCompiled($source)")
* }
* dotty.tools.dotc.process(args, callback)
* // Or, if you have a custom root context `rootCtx`:
* dotty.tools.dotc.process(args, rootCtx.setCompilerCallback(callback))
* }}}
*/
public interface CompilerCallback {

/** Called when a class has been generated.
*
* @param source The source file corresponding to this class.
* Example: ./src/library/scala/collection/Seq.scala
* @param generatedClass The generated classfile for this class.
* Example: ./scala/collection/Seq$.class
* @param className The name of this class.
* Example: scala.collection.Seq$
*/
void onClassGenerated(File source, File generatedClass, String className);

/** Called when every class for this file has been generated.
*
* @param source The source file.
* Example: ./src/library/scala/collection/Seq.scala
*/
void onSourceCompiled(File source);

/** Called when compilation phase starts on a given source
*
* @param phase Name (or other identifier) of the current phase
* @param sourcePath Path to the processed file
*/
void startUnit(String phase, String sourcePath);

/** Called to show progress of the compilation process
*/
void advance(int currentProgress, int totalProgress);

//Methods to pass compiler and debug messages and exceptions

void info(String msg, Position pos);

void warning(String msg, Position pos);

void error(String msg, Position pos);

void debug(String msg);

void trace(Throwable exception);
}
23 changes: 23 additions & 0 deletions src/dotty/tools/dotc/callbacks/Position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dotty.tools.dotc.callbacks;

import java.io.File;

/**
* NOTE: This interface is used for integration of Dotty compiler with Intellij IDEA.
* If you want to change it please contact Scala Plugin team.
*/

public interface Position
{
File sourceFile();

String sourcePath();

int line();

String lineContent();

int offset();

int column();
}
1 change: 1 addition & 0 deletions src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import NameOps._
import Uniques._
import SymDenotations._
import Flags.ParamAccessor
import callbacks.CompilerCallback
import util.Positions._
import ast.Trees._
import ast.untpd
Expand Down
3 changes: 3 additions & 0 deletions src/dotty/tools/dotc/core/Phases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ object Phases {

def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
units.map { unit =>
if (ctx.compilerCallback != null)
ctx.compilerCallback.startUnit(phaseName, unit.source.file.path)

val unitCtx = ctx.fresh.setPhase(this.start).setCompilationUnit(unit)
run(unitCtx)
unitCtx.compilationUnit
Expand Down
24 changes: 24 additions & 0 deletions src/dotty/tools/dotc/reporting/CallbackPositionAdapter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dotty.tools.dotc.reporting

import java.io.File

import dotty.tools.dotc.callbacks.Position
import dotty.tools.dotc.util.SourcePosition

/**
* @author Nikolay.Tropin
*/
class CallbackPositionAdapter(sourcePosition: SourcePosition) extends Position {

override def sourceFile(): File = sourcePosition.source.file.file

override def sourcePath(): String = sourcePosition.source.file.path

override def line(): Int = sourcePosition.line

override def lineContent(): String = sourcePosition.lineContents

override def column(): Int = sourcePosition.column

override def offset(): Int = sourcePosition.point
}
19 changes: 17 additions & 2 deletions src/dotty/tools/dotc/reporting/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object Reporter {
}
}

import Reporter._
import dotty.tools.dotc.reporting.Reporter._

trait Reporting { this: Context =>

Expand Down Expand Up @@ -234,14 +234,16 @@ abstract class Reporter {
}

def report(d: Diagnostic)(implicit ctx: Context): Unit =
if (!isHidden(d) && doReport(d)(ctx.addMode(Mode.Printing)))
if (!isHidden(d) && doReport(d)(ctx.addMode(Mode.Printing))) {
doCallbackReport(d)
d match {
case d: ConditionalWarning if !d.enablingOption.value => unreportedWarnings(d.enablingOption.name) += 1
case d: Warning => warningCount += 1
case d: Error => errorCount += 1
case d: Info => // nothing to do here
// match error if d is something else
}
}

def incomplete(d: Diagnostic)(implicit ctx: Context): Unit =
incompleteHandler(d)(ctx)
Expand All @@ -265,6 +267,19 @@ abstract class Reporter {
case _ => n + " " + elements + "s"
}

private def doCallbackReport(d: Diagnostic)(implicit ctx: Context): Unit = {
val callback = ctx.compilerCallback
if (callback != null) {
val position = if (d.pos.exists) new CallbackPositionAdapter(d.pos) else null
d match {
case d: ConditionalWarning if d.enablingOption.value => callback.warning(d.msg, position)
case d: Warning => callback.warning(d.msg, position)
case d: Error => callback.error(d.msg, position)
case d: Info => callback.info(d.msg, position)
}
}
}

/** Should this diagnostic not be reported at all? */
def isHidden(d: Diagnostic)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing)

Expand Down