Skip to content

Commit

Permalink
Format Scala code (#1450)
Browse files Browse the repository at this point in the history
- Formatted scala code
- Update .git-blame-ignore-revs

Pull request: #1450
  • Loading branch information
lefou authored Mar 6, 2024
2 parents 7b0f3bf + b78b8d2 commit 0b2ae1a
Show file tree
Hide file tree
Showing 166 changed files with 4,617 additions and 4,380 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Reformatted scala code
78d98a149f2a4d7ad81f398edc72475a4fe8c5bf
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,31 @@ import ammonite.util.Util.CodeSource
abstract class CodeWrapper {
def wrapperPath: Seq[Name] = Nil
def apply(
code: String,
source: CodeSource,
imports: Imports,
printCode: String,
indexedWrapper: Name,
extraCode: String
code: String,
source: CodeSource,
imports: Imports,
printCode: String,
indexedWrapper: Name,
extraCode: String
): (String, String, Int)

def wrapCode(codeSource: CodeSource,
indexedWrapperName: Name,
code: String,
printCode: String,
imports: Imports,
extraCode: String,
markScript: Boolean) = {
def wrapCode(
codeSource: CodeSource,
indexedWrapperName: Name,
code: String,
printCode: String,
imports: Imports,
extraCode: String,
markScript: Boolean
) = {

//we need to normalize topWrapper and bottomWrapper in order to ensure
//the snippets always use the platform-specific newLine
// we need to normalize topWrapper and bottomWrapper in order to ensure
// the snippets always use the platform-specific newLine
val extraCode0 =
if (markScript) extraCode + "/*</generated>*/"
else extraCode
val (topWrapper, bottomWrapper, userCodeNestingLevel) =
apply(code, codeSource, imports, printCode, indexedWrapperName, extraCode0)
apply(code, codeSource, imports, printCode, indexedWrapperName, extraCode0)
val (topWrapper0, bottomWrapper0) =
if (markScript) (topWrapper + "/*<script>*/", "/*</script>*/ /*<generated>*/" + bottomWrapper)
else (topWrapper, bottomWrapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import ammonite.util.{Imports, Printer}
abstract class Compiler {

def compile(
src: Array[Byte],
printer: Printer,
importsLen: Int,
userCodeNestingLevel: Int,
fileName: String
src: Array[Byte],
printer: Printer,
importsLen: Int,
userCodeNestingLevel: Int,
fileName: String
): Option[Compiler.Output]

def preprocessor(fileName: String, markGeneratedSections: Boolean = false): Preprocessor
Expand All @@ -19,10 +19,9 @@ abstract class Compiler {
object Compiler {

case class Output(
classFiles: Vector[(String, Array[Byte])],
imports: Imports,
usedEarlierDefinitions: Option[Seq[String]]
classFiles: Vector[(String, Array[Byte])],
imports: Imports,
usedEarlierDefinitions: Option[Seq[String]]
)

}

Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import ammonite.util.Frame
abstract class CompilerBuilder {

def newManager(
rtCacheDir: Option[Path],
headFrame: => Frame,
dependencyCompleter: => Option[String => (Int, Seq[String])],
whiteList: Set[Seq[String]],
initialClassLoader: ClassLoader,
settings: Seq[String]
rtCacheDir: Option[Path],
headFrame: => Frame,
dependencyCompleter: => Option[String => (Int, Seq[String])],
whiteList: Set[Seq[String]],
initialClassLoader: ClassLoader,
settings: Seq[String]
): CompilerLifecycleManager

def create(
initialClassPath: Seq[URL],
classPath: Seq[URL],
dynamicClassPath: Seq[(String, Array[Byte])],
evalClassLoader: ClassLoader,
pluginClassLoader: ClassLoader,
reporter: Option[CompilerBuilder.Message => Unit],
settings: Seq[String],
classPathWhiteList: Set[Seq[String]],
lineNumberModifier: Boolean
initialClassPath: Seq[URL],
classPath: Seq[URL],
dynamicClassPath: Seq[(String, Array[Byte])],
evalClassLoader: ClassLoader,
pluginClassLoader: ClassLoader,
reporter: Option[CompilerBuilder.Message => Unit],
settings: Seq[String],
classPathWhiteList: Set[Seq[String]],
lineNumberModifier: Boolean
): Compiler

def scalaVersion: String
Expand All @@ -34,10 +34,10 @@ abstract class CompilerBuilder {
object CompilerBuilder {

case class Message(
severity: String,
start: Int,
end: Int,
message: String
severity: String,
start: Int,
end: Int,
message: String
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ abstract class CompilerLifecycleManager {
def init(force: Boolean = false): Unit

def complete(
offset: Int,
previousImports: String,
snippet: String
offset: Int,
previousImports: String,
snippet: String
): (Int, Seq[String], Seq[String])

def compileClass(
processed: Preprocessor.Output,
printer: Printer,
fileName: String
processed: Preprocessor.Output,
printer: Printer,
fileName: String
): Option[Compiler.Output]

def addToClasspath(classFiles: ClassFiles): Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,74 @@ import ammonite.util.Util.CodeSource
abstract class Parser {

def split(
code: String,
ignoreIncomplete: Boolean = true,
fileName: String = "(console)"
code: String,
ignoreIncomplete: Boolean = true,
fileName: String = "(console)"
): Option[Either[String, Seq[String]]]

final def parseImportHooks(
source: CodeSource,
stmts: Seq[String]
source: CodeSource,
stmts: Seq[String]
): (Seq[String], Seq[ImportTree]) =
parseImportHooksWithIndices(source, stmts.map((0, _)))
def parseImportHooksWithIndices(
source: CodeSource,
stmts: Seq[(Int, String)]
source: CodeSource,
stmts: Seq[(Int, String)]
): (Seq[String], Seq[ImportTree])

/**
* Splits up a script file into its constituent blocks, each of which
* is a tuple of (leading-whitespace, statements). Leading whitespace
* is returned separately so we can later manipulate the statements e.g.
* by adding `val res2 = ` without the whitespace getting in the way
*/
* Splits up a script file into its constituent blocks, each of which
* is a tuple of (leading-whitespace, statements). Leading whitespace
* is returned separately so we can later manipulate the statements e.g.
* by adding `val res2 = ` without the whitespace getting in the way
*/
def splitScript(
rawCode: String,
fileName: String
rawCode: String,
fileName: String
): Either[String, IndexedSeq[(String, Seq[String])]]

def scriptBlocksWithStartIndices(
rawCode: String,
fileName: String
rawCode: String,
fileName: String
): Either[Parser.ScriptSplittingError, Seq[Parser.ScriptBlock]]

def defaultHighlight(buffer: Vector[Char],
comment: fansi.Attrs,
`type`: fansi.Attrs,
literal: fansi.Attrs,
keyword: fansi.Attrs,
reset: fansi.Attrs,
notImplemented: fansi.Attrs): Vector[Char]
def defaultHighlight(
buffer: Vector[Char],
comment: fansi.Attrs,
`type`: fansi.Attrs,
literal: fansi.Attrs,
keyword: fansi.Attrs,
reset: fansi.Attrs,
notImplemented: fansi.Attrs
): Vector[Char]

def isObjDef(code: String): Boolean
}

object Parser {

case class ParsedImportHooks(
hookStatements: Seq[String],
importTrees: Seq[ImportTree]
hookStatements: Seq[String],
importTrees: Seq[ImportTree]
)

case class ScriptBlock(
startIndex: Int,
ncomment: String,
codeWithStartIndices: Seq[(Int, String)]
startIndex: Int,
ncomment: String,
codeWithStartIndices: Seq[(Int, String)]
)

object ScriptBlock {
def apply(
ncomment: String,
codeWithStartIndices: Seq[(Int, String)]
ncomment: String,
codeWithStartIndices: Seq[(Int, String)]
): ScriptBlock =
ScriptBlock(0, ncomment, codeWithStartIndices)
}

class ScriptSplittingError(
message: String,
val index: Int = -1,
val expected: String = ""
message: String,
val index: Int = -1,
val expected: String = ""
) extends Exception(message)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@ import ammonite.util.{Imports, Name, Res}
import ammonite.util.Util.CodeSource

/**
* Responsible for all scala-source-code-munging that happens within the
* Ammonite REPL.
*
* Performs several tasks:
*
* - Takes top-level Scala expressions and assigns them to `res{1, 2, 3, ...}`
* values so they can be accessed later in the REPL
*
* - Wraps the code snippet with an wrapper `object` since Scala doesn't allow
* top-level expressions
*
* - Mangles imports from our [[ammonite.util.ImportData]] data structure into a source
* String
*
* - Combines all of these into a complete compilation unit ready to feed into
* the Scala compiler
*/
* Responsible for all scala-source-code-munging that happens within the
* Ammonite REPL.
*
* Performs several tasks:
*
* - Takes top-level Scala expressions and assigns them to `res{1, 2, 3, ...}`
* values so they can be accessed later in the REPL
*
* - Wraps the code snippet with an wrapper `object` since Scala doesn't allow
* top-level expressions
*
* - Mangles imports from our [[ammonite.util.ImportData]] data structure into a source
* String
*
* - Combines all of these into a complete compilation unit ready to feed into
* the Scala compiler
*/
abstract class Preprocessor {

def transform(
stmts: Seq[String],
resultIndex: String,
leadingSpaces: String,
codeSource: CodeSource,
indexedWrapperName: Name,
imports: Imports,
printerTemplate: String => String,
extraCode: String,
skipEmpty: Boolean,
markScript: Boolean,
codeWrapper: CodeWrapper
stmts: Seq[String],
resultIndex: String,
leadingSpaces: String,
codeSource: CodeSource,
indexedWrapperName: Name,
imports: Imports,
printerTemplate: String => String,
extraCode: String,
skipEmpty: Boolean,
markScript: Boolean,
codeWrapper: CodeWrapper
): Res[Preprocessor.Output]

}

object Preprocessor {

case class Output(
code: String,
prefixCharLength: Int,
userCodeNestingLevel: Int
code: String,
prefixCharLength: Int,
userCodeNestingLevel: Int
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ object CompilerCompatibility extends ExtraCompilerCompatibility {
}
}

def interactiveAnalyzer(g: InteractiveGlobal,
cl: ClassLoader): InteractiveAnalyzer { val global: g.type } = {
def interactiveAnalyzer(
g: InteractiveGlobal,
cl: ClassLoader
): InteractiveAnalyzer { val global: g.type } = {
new { val global: g.type = g } with InteractiveAnalyzer {
override def findMacroClassLoader() = cl
}
}
def importInfo(g: Global)(t: g.Import) =
new g.analyzer.ImportInfo(t, 0)



def initGlobal(settings: Settings,
reporter: AbstractReporter,
jcp: AggregateClassPath,
evalClassloader: ClassLoader,
createPlugins: Global => List[Plugin]): Global = {
def initGlobal(
settings: Settings,
reporter: AbstractReporter,
jcp: AggregateClassPath,
evalClassloader: ClassLoader,
createPlugins: Global => List[Plugin]
): Global = {

new nsc.Global(settings, reporter) { g =>
override lazy val plugins = createPlugins(g)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ trait AmmClassPath extends ClassPath {
ammList(inPackage.dottedString)
}


def ammClasses(inPackage: String): Seq[ClassFileEntry]
def classes(inPackage: scala.tools.nsc.classpath.PackageName): Seq[ClassFileEntry] = {
ammClasses(inPackage.dottedString)
}


def ammHasPackage(pkg: String): Boolean
def hasPackage(pkg: scala.tools.nsc.classpath.PackageName) = ammHasPackage(pkg.dottedString)
}
Loading

0 comments on commit 0b2ae1a

Please sign in to comment.