Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# scalafmt v3.9.10
505443d572b777b1b40c87eb3e14958140769498
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ jobs:
- name: Run tests
run: |
nix-shell --command "yarn install && sbt -DSnippetsContainerTest.mongo=true cachedCiTest"
- name: Check formatting
run: |
nix-shell --command "sbt scalafmtCheckAll"
13 changes: 7 additions & 6 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 3.8.2
version = 3.9.10

style = default
maxColumn = 120
Expand All @@ -8,8 +8,8 @@ fileOverride {
"glob:**/metals-runner/src/**" {
runner.dialect = scala3
}
"glob:**/scalajvm-3/com.olegych.scastie.api.runtime/**" {
runner.dialect = scala3
"glob:**/scalajvm-3/scastie/runtime/**" {
runner.dialect = scala3
}
}

Expand All @@ -25,7 +25,7 @@ project.excludeFilters = [
storage/src/test/resources
demo/
]
align.preset = more
align.preset = some

rewrite.rules = [Imports]
rewrite.imports.sort = original
Expand All @@ -38,8 +38,9 @@ newlines.topLevelStatements = [before, after]
newlines.topLevelStatementsMinBreaks = 2
newlines.implicitParamListModifierForce = [before]

continuationIndent.defnSite = 2
continuationIndent.extendSite = 2
indent.defnSite = 2
indent.extendSite = 2
indent.ctorSite = 4

rewrite.imports.expand = false
rewrite.trailingCommas.style = "never"
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ open `http://localhost:9000`

## Scalafmt

Make sure to run `bin/scalafmt` to format your code.
Make sure to run `sbt scalafmtAll` to format your code.

You can install a pre-commit hook with `bin/hooks.sh`

Expand Down
51 changes: 32 additions & 19 deletions api/src/main/scala/org/scastie/api/ApiModels.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.scastie.api

import io.circe.generic.semiauto._
import io.circe._
import io.circe.generic.semiauto._

case object RunnerPing
case object RunnerPong
Expand Down Expand Up @@ -37,7 +37,8 @@ case class FormatResponse(result: String)
object FetchResult {
implicit val fetchResultEncoder: Encoder[FetchResult] = deriveEncoder[FetchResult]
implicit val fetchResultDecoder: Decoder[FetchResult] = deriveDecoder[FetchResult]
def create(inputs: BaseInputs, progresses: List[SnippetProgress]) = FetchResult(inputs, progresses.sortBy(p => (p.id, p.ts)))
def create(inputs: BaseInputs, progresses: List[SnippetProgress]) =
FetchResult(inputs, progresses.sortBy(p => (p.id, p.ts)))
}

case class FetchResult private (inputs: BaseInputs, progresses: List[SnippetProgress])
Expand All @@ -56,7 +57,13 @@ object ScalaDependency {
implicit val scalaDependencyDecoder: Decoder[ScalaDependency] = deriveDecoder[ScalaDependency]
}

case class ScalaDependency(groupId: String, artifact: String, target: ScalaTarget, version: String, isAutoResolve: Boolean = true) {
case class ScalaDependency(
groupId: String,
artifact: String,
target: ScalaTarget,
version: String,
isAutoResolve: Boolean = true
) {
def matches(sd: ScalaDependency): Boolean = sd.groupId == this.groupId && sd.artifact == this.artifact

def renderSbt: String = {
Expand All @@ -68,6 +75,7 @@ case class ScalaDependency(groupId: String, artifact: String, target: ScalaTarge
val resolveSymbol = if (isAutoResolve) "::" else ":"
s"//> using dep $groupId$resolveSymbol$artifact:$version"
}

}

case class ScastieMetalsOptions(dependencies: Set[ScalaDependency], scalaTarget: ScalaTarget, code: String)
Expand All @@ -87,7 +95,6 @@ case class NoResult(msg: String) extends FailureType
case class PresentationCompilerFailure(msg: String) extends FailureType
case class InvalidScalaVersion(msg: String) extends FailureType


object FailureType {
implicit val failureTypeEncoder: Encoder[FailureType] = deriveEncoder[FailureType]
implicit val noResultDecoder: Decoder[FailureType] = deriveDecoder[FailureType]
Expand All @@ -99,8 +106,10 @@ object NoResult {
}

object PresentationCompilerFailure {
implicit val presentationCompilerFailureEncoder: Encoder[PresentationCompilerFailure] = deriveEncoder[PresentationCompilerFailure]
implicit val presentationCompilerFailureDecoder: Decoder[PresentationCompilerFailure] = deriveDecoder[PresentationCompilerFailure]
implicit val presentationCompilerFailureEncoder: Encoder[PresentationCompilerFailure] =
deriveEncoder[PresentationCompilerFailure]
implicit val presentationCompilerFailureDecoder: Decoder[PresentationCompilerFailure] =
deriveDecoder[PresentationCompilerFailure]
}

object ScastieOffsetParams {
Expand Down Expand Up @@ -128,16 +137,15 @@ case class EditRange(startLine: Int, startChar: Int, endLine: Int, endChar: Int)
case class ScalaCompletionList(items: Set[CompletionItemDTO], isIncomplete: Boolean)

case class CompletionItemDTO(
label: String,
detail: String,
tpe: String,
order: Option[Int],
instructions: InsertInstructions,
additionalInsertInstructions: List[AdditionalInsertInstructions],
symbol: Option[String]
label: String,
detail: String,
tpe: String,
order: Option[Int],
instructions: InsertInstructions,
additionalInsertInstructions: List[AdditionalInsertInstructions],
symbol: Option[String]
)


case class HoverDTO(from: Int, to: Int, content: String)

case class CompletionsDTO(items: Set[CompletionItemDTO])
Expand All @@ -153,8 +161,10 @@ object InsertInstructions {
}

object AdditionalInsertInstructions {
implicit val additionalInsertInstructionsEncoder: Encoder[AdditionalInsertInstructions] = deriveEncoder[AdditionalInsertInstructions]
implicit val additionalInsertInstructionsDecoder: Decoder[AdditionalInsertInstructions] = deriveDecoder[AdditionalInsertInstructions]
implicit val additionalInsertInstructionsEncoder: Encoder[AdditionalInsertInstructions] =
deriveEncoder[AdditionalInsertInstructions]
implicit val additionalInsertInstructionsDecoder: Decoder[AdditionalInsertInstructions] =
deriveDecoder[AdditionalInsertInstructions]
}

object ScalaCompletionList {
Expand Down Expand Up @@ -199,19 +209,22 @@ case class KeepAlive(msg: String = "") extends AnyVal

sealed trait EditorMode
case object Default extends EditorMode
case object Vim extends EditorMode
case object Emacs extends EditorMode
case object Vim extends EditorMode
case object Emacs extends EditorMode

object EditorMode {

implicit val editorModeFormat: Encoder[EditorMode] = Encoder.encodeString.contramap {
case Default => "Default"
case Vim => "Vim"
case Emacs => "Emacs"
}

implicit val editorModeDecoder: Decoder[EditorMode] = Decoder.decodeString.emap {
case "Default" => Right(Default)
case "Vim" => Right(Vim)
case "Emacs" => Right(Emacs)
case other => Left(s"Unknown EditorMode: $other")
}
}

}
2 changes: 1 addition & 1 deletion api/src/main/scala/org/scastie/api/CompilerInfo.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.scastie.api

import io.circe.generic.semiauto._
import io.circe._
import io.circe.generic.semiauto._

object Severity {
implicit val severityEncoder: Encoder[Severity] = deriveEncoder[Severity]
Expand Down
12 changes: 6 additions & 6 deletions api/src/main/scala/org/scastie/api/ConsoleOutput.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.scastie.api

import io.circe.generic.semiauto._
import io.circe._
import io.circe.generic.semiauto._

sealed trait ConsoleOutput {
def show: String
Expand All @@ -27,9 +27,9 @@ object ConsoleOutput {
implicit val consoleOutputEncoder: Encoder[ConsoleOutput] = deriveEncoder[ConsoleOutput]
implicit val consoleOutputDecoder: Decoder[ConsoleOutput] = deriveDecoder[ConsoleOutput]

def systemOutput(target: ScalaTarget)(output: ProcessOutput): ConsoleOutput =
target.targetType match {
case ScalaTargetType.ScalaCli => ScalaCliOutput(output)
case _ => SbtOutput(output)
}
def systemOutput(target: ScalaTarget)(output: ProcessOutput): ConsoleOutput = target.targetType match {
case ScalaTargetType.ScalaCli => ScalaCliOutput(output)
case _ => SbtOutput(output)
}

}
75 changes: 35 additions & 40 deletions api/src/main/scala/org/scastie/api/Inputs.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.scastie.api

import io.circe.generic.semiauto._
import io.circe._
import io.circe.generic.semiauto._
import org.scastie.buildinfo.BuildInfo

import System.{lineSeparator => nl}

sealed trait BaseInputs {
Expand All @@ -16,7 +15,7 @@ sealed trait BaseInputs {

def markAsCopied: BaseInputs = {
this match {
case s: SbtInputs => s.copy(isShowingInUserProfile = false, forked = None)
case s: SbtInputs => s.copy(isShowingInUserProfile = false, forked = None)
case s: ScalaCliInputs => s.copy(isShowingInUserProfile = false, forked = None)
}
}
Expand All @@ -33,23 +32,24 @@ sealed trait BaseInputs {
isShowingInUserProfile: Boolean = this.isShowingInUserProfile,
code: String = this.code,
libraries: Set[ScalaDependency] = this.libraries,
forked: Option[SnippetId] = this.forked,
forked: Option[SnippetId] = this.forked
): BaseInputs = this match {
case scalaCliInputs: ScalaCliInputs => scalaCliInputs.copy(
isWorksheetMode = isWorksheetMode,
isShowingInUserProfile = isShowingInUserProfile,
code = code,
forked = forked,
libraries = libraries
)
isWorksheetMode = isWorksheetMode,
isShowingInUserProfile = isShowingInUserProfile,
code = code,
forked = forked,
libraries = libraries
)
case sbtInputs: SbtInputs => sbtInputs.copy(
isWorksheetMode = isWorksheetMode,
isShowingInUserProfile = isShowingInUserProfile,
code = code,
libraries = libraries,
forked = forked
)
isWorksheetMode = isWorksheetMode,
isShowingInUserProfile = isShowingInUserProfile,
code = code,
libraries = libraries,
forked = forked
)
}

}

object BaseInputs {
Expand Down Expand Up @@ -92,15 +92,13 @@ object SbtInputs {
}

case class ScalaCliInputs(
isWorksheetMode: Boolean,
code: String,
target: ScalaCli,
isShowingInUserProfile: Boolean,
forked: Option[SnippetId] = None,
libraries: Set[ScalaDependency] = Set.empty
) extends BaseInputs {

}
isWorksheetMode: Boolean,
code: String,
target: ScalaCli,
isShowingInUserProfile: Boolean,
forked: Option[SnippetId] = None,
libraries: Set[ScalaDependency] = Set.empty
) extends BaseInputs {}

object ScalaCliInputs {
val defaultCode = """List("Hello", "World").mkString("", ", ", "!")"""
Expand Down Expand Up @@ -169,9 +167,11 @@ case class SbtInputs(

lazy val isDefault: Boolean = copy(code = "").withSavedConfig == SbtInputs.default.copy(code = "").withSavedConfig

def modifyConfig(inputs: SbtInputs => SbtInputs): SbtInputs = inputs(this).copy(sbtConfigSaved = None, sbtPluginsConfigSaved = None)
def modifyConfig(inputs: SbtInputs => SbtInputs): SbtInputs =
inputs(this).copy(sbtConfigSaved = None, sbtPluginsConfigSaved = None)

def withSavedConfig: SbtInputs = copy(sbtConfigSaved = Some(sbtConfigGenerated), sbtPluginsConfigSaved = Some(sbtPluginsConfigGenerated))
def withSavedConfig: SbtInputs =
copy(sbtConfigSaved = Some(sbtConfigGenerated), sbtPluginsConfigSaved = Some(sbtPluginsConfigGenerated))

def clearDependencies: SbtInputs = {
modifyConfig {
Expand Down Expand Up @@ -204,9 +204,8 @@ case class SbtInputs(
val newScalaDependency = scalaDependency.copy(version = version)
val newLibraries = libraries.filterNot(_.matches(scalaDependency)) + newScalaDependency
val newLibrariesFromList = librariesFromList.collect {
case (l, p) if l.matches(scalaDependency) =>
newScalaDependency -> p
case (l, p) => l -> p
case (l, p) if l.matches(scalaDependency) => newScalaDependency -> p
case (l, p) => l -> p
}
modifyConfig {
_.copy(
Expand All @@ -216,8 +215,7 @@ case class SbtInputs(
}
}

lazy val sbtConfig: String =
mapToConfig(sbtConfigGenerated, sbtConfigExtra)
lazy val sbtConfig: String = mapToConfig(sbtConfigGenerated, sbtConfigExtra)

lazy val sbtConfigGenerated: String = sbtConfigSaved.getOrElse {
val targetConfig = target.sbtConfig
Expand All @@ -226,8 +224,7 @@ case class SbtInputs(
if (target.hasWorksheetMode) Some(target.runtimeDependency)
else None

val allLibraries =
optionalTargetDependency.map(libraries + _).getOrElse(libraries)
val allLibraries = optionalTargetDependency.map(libraries + _).getOrElse(libraries)

val librariesConfig =
if (allLibraries.isEmpty) ""
Expand All @@ -249,26 +246,24 @@ case class SbtInputs(
mapToConfig(targetConfig, librariesConfig)
}

lazy val sbtPluginsConfig: String =
mapToConfig(sbtPluginsConfigGenerated, sbtPluginsConfigExtra)
lazy val sbtPluginsConfig: String = mapToConfig(sbtPluginsConfigGenerated, sbtPluginsConfigExtra)

lazy val sbtPluginsConfigGenerated: String = sbtPluginsConfigSaved.getOrElse {
sbtPluginsConfig0(withSbtScastie = true)
}

private def mapToConfig(parts: String*): String =
parts.filter(_.nonEmpty).mkString("\n")
private def mapToConfig(parts: String*): String = parts.filter(_.nonEmpty).mkString("\n")

private def sbtPluginsConfig0(withSbtScastie: Boolean): String = {
val targetConfig = target.sbtPluginsConfig

val sbtScastie =
if (withSbtScastie)
s"""addSbtPlugin("org.scastie" % "sbt-scastie" % "${BuildInfo.versionRuntime}")"""
if (withSbtScastie) s"""addSbtPlugin("org.scastie" % "sbt-scastie" % "${BuildInfo.versionRuntime}")"""
else ""

mapToConfig(targetConfig, sbtScastie)
}

}

object EditInputs {
Expand Down
Loading
Loading