|
1 | 1 | package scala.build.preprocessing
|
2 | 2 |
|
3 |
| -import com.virtuslab.using_directives.config.Settings |
4 |
| -import com.virtuslab.using_directives.custom.model.{ |
5 |
| - UsingDirectiveKind, |
6 |
| - UsingDirectiveSyntax, |
7 |
| - UsingDirectives |
8 |
| -} |
9 |
| -import com.virtuslab.using_directives.custom.utils.ast.{UsingDef, UsingDefs} |
10 |
| -import com.virtuslab.using_directives.{Context, UsingDirectivesProcessor} |
11 | 3 | import dependency.AnyDependency
|
12 | 4 | import dependency.parser.DependencyParser
|
13 | 5 |
|
14 | 6 | import java.nio.charset.StandardCharsets
|
15 |
| - |
16 | 7 | import scala.build.EitherCps.{either, value}
|
17 | 8 | import scala.build.Ops._
|
18 | 9 | import scala.build.errors._
|
19 | 10 | import scala.build.internal.{AmmUtil, Util}
|
20 | 11 | import scala.build.options.{BuildOptions, BuildRequirements, ClassPathOptions, ShadowingSeq}
|
| 12 | +import scala.build.preprocessing.ExtractedDirectives.from |
21 | 13 | import scala.build.preprocessing.directives._
|
22 | 14 | import scala.build.{Inputs, Logger, Position, Positioned}
|
23 | 15 | import scala.collection.mutable
|
@@ -189,6 +181,7 @@ case object ScalaPreprocessor extends Preprocessor {
|
189 | 181 | ): Either[BuildException, Option[SpecialImportsProcessingOutput]] = either {
|
190 | 182 |
|
191 | 183 | import fastparse._
|
| 184 | + |
192 | 185 | import scala.build.internal.ScalaParse._
|
193 | 186 |
|
194 | 187 | val res = parse(content, Header(_))
|
@@ -275,7 +268,7 @@ case object ScalaPreprocessor extends Preprocessor {
|
275 | 268 | logger: Logger
|
276 | 269 | ): Either[BuildException, StrictDirectivesProcessingOutput] = either {
|
277 | 270 | val contentChars = content.toCharArray
|
278 |
| - val ExtractedDirectives(codeOffset, directives0, _) = value(extractUsingDirectives(contentChars, path, logger)) |
| 271 | + val ExtractedDirectives(codeOffset, directives0, _) = value(ExtractedDirectives.from(contentChars, path, logger)) |
279 | 272 |
|
280 | 273 | val updatedOptions = value {
|
281 | 274 | DirectivesProcessor.process(
|
@@ -345,103 +338,9 @@ case object ScalaPreprocessor extends Preprocessor {
|
345 | 338 | new UnusedDirectiveError(directive.key, values.map(_._1.value), values.flatMap(_._1.positions))
|
346 | 339 | }
|
347 | 340 |
|
348 |
| - case class ExtractedDirectives(offset: Int, directives: Seq[StrictDirective], kind: UsingDirectiveKind) |
349 |
| - |
350 | 341 | val changeToSpecialCommentMsg =
|
351 | 342 | "Using directive using plain comments are deprecated. Please use a special comment syntax: '//> ...' or '/*> ... */'"
|
352 | 343 |
|
353 |
| - def extractUsingDirectives( |
354 |
| - contentChars: Array[Char], |
355 |
| - path: Either[String, os.Path], |
356 |
| - logger: Logger |
357 |
| - ): Either[BuildException, ExtractedDirectives] = { |
358 |
| - val errors = new mutable.ListBuffer[Diagnostic] |
359 |
| - val reporter = CustomDirectivesReporter.create(path) { diag => |
360 |
| - if (diag.severity == Severity.Warning) |
361 |
| - logger.log(Seq(diag)) |
362 |
| - else |
363 |
| - errors += diag |
364 |
| - } |
365 |
| - val processor = { |
366 |
| - val settings = new Settings |
367 |
| - settings.setAllowStartWithoutAt(true) |
368 |
| - settings.setAllowRequire(false) |
369 |
| - val context = new Context(reporter, settings) |
370 |
| - new UsingDirectivesProcessor(context) |
371 |
| - } |
372 |
| - val all = processor.extract(contentChars, true, true).asScala |
373 |
| - if (errors.isEmpty) { |
374 |
| - |
375 |
| - def byKind(kind: UsingDirectiveKind) = all.find(_.getKind == kind).get |
376 |
| - |
377 |
| - def getDirectives(directives: UsingDirectives) = |
378 |
| - directives.getAst() match { |
379 |
| - case ud: UsingDefs => |
380 |
| - ud.getUsingDefs().asScala.toSeq |
381 |
| - case _ => |
382 |
| - Nil |
383 |
| - } |
384 |
| - |
385 |
| - val codeDirectives = byKind(UsingDirectiveKind.Code) |
386 |
| - val specialCommentDirectives = byKind(UsingDirectiveKind.SpecialComment) |
387 |
| - val plainCommentDirectives = byKind(UsingDirectiveKind.PlainComment) |
388 |
| - |
389 |
| - def reportWarning(msg: String, values: Seq[UsingDef], before: Boolean = true): Unit = |
390 |
| - values.foreach { v => |
391 |
| - val astPos = v.getPosition() |
392 |
| - val (start, end) = |
393 |
| - if (before) (0, astPos.getColumn()) |
394 |
| - else (astPos.getColumn(), astPos.getColumn() + v.getSyntax.getKeyword.size) |
395 |
| - val position = Position.File(path, (astPos.getLine(), start), (astPos.getLine(), end)) |
396 |
| - logger.diagnostic(msg, positions = Seq(position)) |
397 |
| - } |
398 |
| - |
399 |
| - val usedDirectives = |
400 |
| - if (!codeDirectives.getFlattenedMap().isEmpty()) { |
401 |
| - val msg = |
402 |
| - "This using directive is ignored. File contains directives outside comments and those have higher precedence." |
403 |
| - reportWarning( |
404 |
| - msg, |
405 |
| - getDirectives(plainCommentDirectives) ++ getDirectives(specialCommentDirectives) |
406 |
| - ) |
407 |
| - codeDirectives |
408 |
| - } |
409 |
| - else if (!specialCommentDirectives.getFlattenedMap().isEmpty()) { |
410 |
| - val msg = |
411 |
| - s"This using directive is ignored. $changeToSpecialCommentMsg" |
412 |
| - reportWarning(msg, getDirectives(plainCommentDirectives)) |
413 |
| - specialCommentDirectives |
414 |
| - } |
415 |
| - else { |
416 |
| - reportWarning(changeToSpecialCommentMsg, getDirectives(plainCommentDirectives)) |
417 |
| - plainCommentDirectives |
418 |
| - } |
419 |
| - |
420 |
| - // All using directives should use just `using` keyword, no @using or require |
421 |
| - reportWarning( |
422 |
| - "Deprecated using directive syntax, please use keyword `using`.", |
423 |
| - getDirectives(usedDirectives).filter(_.getSyntax() != UsingDirectiveSyntax.Using), |
424 |
| - before = false |
425 |
| - ) |
426 |
| - |
427 |
| - val flattened = usedDirectives.getFlattenedMap.asScala.toSeq |
428 |
| - val strictDirectives = |
429 |
| - flattened.map { |
430 |
| - case (k, l) => |
431 |
| - StrictDirective(k.getPath.asScala.mkString("."), l.asScala.toSeq) |
432 |
| - } |
433 |
| - |
434 |
| - val offset = |
435 |
| - if (usedDirectives.getKind() != UsingDirectiveKind.Code) 0 |
436 |
| - else usedDirectives.getCodeOffset() |
437 |
| - Right(ExtractedDirectives(offset, strictDirectives, usedDirectives.getKind())) |
438 |
| - } |
439 |
| - else { |
440 |
| - val errors0 = errors.map(diag => new MalformedDirectiveError(diag.message, diag.positions)) |
441 |
| - Left(CompositeBuildException(errors0.toSeq)) |
442 |
| - } |
443 |
| - } |
444 |
| - |
445 | 344 | private def parseDependency(str: String, pos: Position): Either[BuildException, AnyDependency] =
|
446 | 345 | DependencyParser.parse(str) match {
|
447 | 346 | case Left(msg) => Left(new DependencyFormatError(str, msg, positionOpt = Some(pos)))
|
|
0 commit comments