Skip to content

Commit adde57a

Browse files
committed
Remove snippet context feature
1 parent b2869dc commit adde57a

20 files changed

+42
-184
lines changed

library/src/scala/quoted/Exprs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Exprs:
55
/** Matches literal sequence of literal constant value expressions and return a sequence of values.
66
*
77
* Usage:
8-
* ```scala sc:macrocompile
8+
* ```scala
99
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
1010
* def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match
1111
* case Varargs(Exprs(args)) => ???

library/src/scala/quoted/Varargs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ object Varargs {
3535
/** Matches a literal sequence of expressions and return a sequence of expressions.
3636
*
3737
* Usage:
38-
* ```scala sc:macrocompile
38+
* ```scala
3939
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
4040
* def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match
4141
* case Varargs(argVarargs) => ???

scaladoc-testcases/src/tests/snippetCompilerTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class A {
5555
class B { }
5656

5757
/**
58-
* ```scala sc:macrocompile
58+
* ```scala
5959
* import scala.quoted._
6060
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
6161
* def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match

scaladoc-testcases/src/tests/snippetTestcase1.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package tests.snippetTestcase1
22

33
class SnippetTestcase1:
44
/**
5-
* SNIPPET(OUTERLINEOFFSET:8,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
5+
* SNIPPET(OUTERLINEOFFSET:7,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
66
* ERROR(LINE:8,COLUMN:8)
77
* ```scala sc:fail
88
* 2 + List()
@@ -11,19 +11,19 @@ class SnippetTestcase1:
1111
*/
1212
def a = 3
1313
/**
14-
* SNIPPET(OUTERLINEOFFSET:16,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
14+
* SNIPPET(OUTERLINEOFFSET:15,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
1515
* ```scala sc:compile sc-name:1
1616
* val xs: List[Int] = List()
1717
* ```
1818
*
19-
* SNIPPET(OUTERLINEOFFSET:21,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
19+
* SNIPPET(OUTERLINEOFFSET:20,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
2020
* ```scala sc:compile sc-compile-with:1 sc-name:2
2121
* val ys = xs.map(x => x * 2)
2222
* ```
2323
*
24-
* SNIPPET(OUTERLINEOFFSET:26,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
24+
* SNIPPET(OUTERLINEOFFSET:25,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
2525
* ```scala sc:compile sc-compile-with:2
2626
* xs ++ ys
2727
* ```
2828
*/
29-
def b = 3
29+
def b = 3

scaladoc-testcases/src/tests/snippetTestcase2.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait Quotes2[A] {
77
type X
88
object Y {
99
/**
10-
* SNIPPET(OUTERLINEOFFSET:13,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:8,INNERCOLUMNOFFSET:6)
10+
* SNIPPET(OUTERLINEOFFSET:12,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
1111
* ERROR(LINE:13,COLUMN:12)
1212
* ```scala sc:fail
1313
* 2 + List()
@@ -19,7 +19,7 @@ trait Quotes2[A] {
1919
val z: zModule = ???
2020
trait zModule {
2121
/**
22-
* SNIPPET(OUTERLINEOFFSET:25,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:9,INNERCOLUMNOFFSET:6)
22+
* SNIPPET(OUTERLINEOFFSET:24,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
2323
* ERROR(LINE:25,COLUMN:12)
2424
* ```scala sc:fail
2525
* 2 + List()
@@ -33,7 +33,7 @@ trait Quotes2[A] {
3333
type X
3434
object Y {
3535
/**
36-
* SNIPPET(OUTERLINEOFFSET:39,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:7,INNERCOLUMNOFFSET:6)
36+
* SNIPPET(OUTERLINEOFFSET:38,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
3737
* ERROR(LINE:39,COLUMN:12)
3838
* ```scala sc:fail
3939
* 2 + List()
@@ -45,7 +45,7 @@ trait Quotes2[A] {
4545
val z: zModule = ???
4646
trait zModule {
4747
/**
48-
* SNIPPET(OUTERLINEOFFSET:51,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:8,INNERCOLUMNOFFSET:6)
48+
* SNIPPET(OUTERLINEOFFSET:50,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
4949
* ERROR(LINE:51,COLUMN:12)
5050
* ```scala sc:fail
5151
* 2 + List()
@@ -55,4 +55,4 @@ trait Quotes2[A] {
5555
type ZZ
5656
}
5757
}
58-
}
58+
}

scaladoc-testcases/src/tests/snippetTestcase3.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package snippetTestcase3
55
class SnippetTestcase3:
66
/** Text on line 0.
77
*
8-
* SNIPPET(OUTERLINEOFFSET:11,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
8+
* SNIPPET(OUTERLINEOFFSET:10,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
99
* ERROR(LINE:11,COLUMN:8)
1010
* ```scala sc:fail
1111
* 2 + List()
@@ -15,7 +15,7 @@ class SnippetTestcase3:
1515
/**
1616
* Text on line 1.
1717
*
18-
* SNIPPET(OUTERLINEOFFSET:21,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
18+
* SNIPPET(OUTERLINEOFFSET:20,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
1919
* ERROR(LINE:21,COLUMN:8)
2020
* ```scala sc:fail
2121
* 2 + List()
@@ -26,10 +26,10 @@ class SnippetTestcase3:
2626
*
2727
* Text on line 2.
2828
*
29-
* SNIPPET(OUTERLINEOFFSET:32,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
29+
* SNIPPET(OUTERLINEOFFSET:31,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:4,INNERCOLUMNOFFSET:2)
3030
* ERROR(LINE:32,COLUMN:8)
3131
* ```scala sc:fail
3232
* 2 + List()
3333
* ```
3434
*/
35-
def c = 3
35+
def c = 3

scaladoc/src/dotty/tools/scaladoc/api.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,8 @@ case class TastyMemberSource(path: java.nio.file.Path, lineNumber: Int)
251251

252252
object SnippetCompilerData:
253253
case class Position(line: Int, column: Int)
254-
case class ClassInfo(tpe: Option[String], names: Seq[String], generics: Option[String])
255-
256254
case class SnippetCompilerData(
257255
packageName: String,
258-
classInfos: Seq[SnippetCompilerData.ClassInfo],
259-
imports: List[String],
260256
position: SnippetCompilerData.Position
261257
)
262258

scaladoc/src/dotty/tools/scaladoc/site/templates.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ case class TemplateFile(
8686
val arg = argOverride.fold(pathBasedArg)(pathBasedArg.overrideFlag(_))
8787
val compilerData = SnippetCompilerData(
8888
"staticsitesnippet",
89-
Seq(SnippetCompilerData.ClassInfo(None, Nil, None)),
90-
Nil,
9189
SnippetCompilerData.Position(configOffset - 1, 0)
9290
)
9391
ssctx.snippetChecker.checkSnippet(str, Some(compilerData), arg, lineOffset, sourceFile).collect {
@@ -124,7 +122,7 @@ case class TemplateFile(
124122
// Snippet compiler currently supports markdown only
125123
val parser: Parser = Parser.builder(defaultMarkdownOptions).build()
126124
val parsedMd = parser.parse(rendered).pipe { md =>
127-
FlexmarkSnippetProcessor.processSnippets(md, None, snippetCheckingFunc, withContext = false)(using ssctx.outerCtx)
125+
FlexmarkSnippetProcessor.processSnippets(md, None, snippetCheckingFunc)(using ssctx.outerCtx)
128126
}.pipe { md =>
129127
FlexmarkSectionWrapper(md)
130128
}

scaladoc/src/dotty/tools/scaladoc/snippets/FlexmarkSnippetProcessor.scala

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import dotty.tools.scaladoc.tasty.comments.markdown.ExtendedFencedCodeBlock
1111
import dotty.tools.scaladoc.tasty.comments.PreparsedComment
1212

1313
object FlexmarkSnippetProcessor:
14-
def processSnippets[T <: mdu.Node](root: T, preparsed: Option[PreparsedComment], checkingFunc: => SnippetChecker.SnippetCheckingFunc, withContext: Boolean)(using CompilerContext): T = {
14+
def processSnippets[T <: mdu.Node](root: T, preparsed: Option[PreparsedComment], checkingFunc: => SnippetChecker.SnippetCheckingFunc)(using CompilerContext): T = {
1515
lazy val cf: SnippetChecker.SnippetCheckingFunc = checkingFunc
1616

1717
val nodes = root.getDescendants().asScala.collect {
@@ -68,23 +68,15 @@ object FlexmarkSnippetProcessor:
6868

6969
val fullSnippet = Seq(snippetImports, snippet).mkString("\n").trim
7070
val snippetCompilationResult = cf(fullSnippet, lineOffset, argOverride) match {
71-
case Some(result @ SnippetCompilationResult(wrapped, _, _, messages)) if !withContext =>
71+
case Some(result @ SnippetCompilationResult(wrapped, _, _, messages)) =>
7272
node.setContentString(fullSnippet)
73-
val innerLineOffset = wrapped.innerLineOffset
74-
Some(result.copy(messages = result.messages.map {
75-
case m @ SnippetCompilerMessage(Some(pos), _, _) =>
76-
m.copy(position = Some(pos.copy(relativeLine = pos.relativeLine - innerLineOffset)))
77-
case m => m
78-
}))
79-
case result@Some(SnippetCompilationResult(wrapped, _, _, _)) =>
80-
node.setContentString(wrapped.snippet)
81-
result
73+
Some(result)
8274
case result =>
8375
node.setContentString(fullSnippet)
8476
result
8577
}
8678

87-
node.insertBefore(ExtendedFencedCodeBlock(id, node, snippetCompilationResult, withContext))
79+
node.insertBefore(ExtendedFencedCodeBlock(id, node, snippetCompilationResult))
8880
node.unlink()
8981
id.fold(snippetMap)(id =>
9082
val snippetAsImport = s"""|//{i:$id

scaladoc/src/dotty/tools/scaladoc/snippets/SelfTypePrinter.scala

Lines changed: 0 additions & 55 deletions
This file was deleted.

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetChecker.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ class SnippetChecker(val args: Scaladoc.Args)(using cctx: CompilerContext):
4848
val wrapped = WrappedSnippet(
4949
snippet,
5050
data.map(_.packageName),
51-
data.fold(Nil)(_.classInfos),
52-
data.map(_.imports).getOrElse(Nil),
5351
lineOffset + data.fold(0)(_.position.line) + constantLineOffset,
54-
data.fold(0)(_.position.column) + constantColumnOffset,
55-
arg.flag == SCFlags.MacroCompile
52+
data.fold(0)(_.position.column) + constantColumnOffset
5653
)
5754
Some(compiler.compile(wrapped, arg, sourceFile))
5855
else

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompiler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class SnippetCompiler(
6363
case _ => NoSourcePosition
6464
val offsetFromLine = sourceFile match
6565
case NoSource => 0
66-
case sf: SourceFile => sf.lineToOffset(diagPos.line + line - innerLineOffset - 1)
66+
case sf: SourceFile => sf.lineToOffset(diagPos.line + line - innerLineOffset)
6767
val offsetFromColumn = diagPos.column + column - innerColumnOffset
6868
val span = Span(offsetFromLine + offsetFromColumn, offsetFromLine + offsetFromColumn)
6969
val pos = Some(
70-
Position(dotty.tools.dotc.util.SourcePosition(sourceFile, span), diagPos.line)
70+
Position(dotty.tools.dotc.util.SourcePosition(sourceFile, span), diagPos.line - innerLineOffset)
7171
)
7272
val dmsg = Try(diagnostic.message) match {
7373
case Success(msg) => msg

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompilerArgs.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ case class SnippetCompilerArg(flag: SCFlags):
88

99
enum SCFlags(val flagName: String):
1010
case Compile extends SCFlags("compile")
11-
case MacroCompile extends SCFlags("macrocompile")
1211
case NoCompile extends SCFlags("nocompile")
1312
case Fail extends SCFlags("fail")
1413

@@ -38,7 +37,6 @@ object SnippetCompilerArgs:
3837
|
3938
|Available flags:
4039
|compile - Enables snippet checking.
41-
|macrocompile - Enables snippet checking for macros.
4240
|nocompile - Disables snippet checking.
4341
|fail - Enables snippet checking, asserts that snippet doesn't compile.
4442
|

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompilerDataCollector.scala

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,57 +20,10 @@ class SnippetCompilerDataCollector[Q <: Quotes](val qctx: Q):
2020

2121
def getSnippetCompilerData(sym: Symbol, originalSym: Symbol): SnippetCompilerData =
2222
val packageName = sym.packageName
23-
if !sym.isPackageDef then sym.tree match {
24-
case c: qctx.reflect.ClassDef =>
25-
import dotty.tools.dotc
26-
import dotty.tools.dotc.core.Decorators._
27-
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
28-
val printer: dotc.printing.Printer = SelfTypePrinter()
29-
val classSym = c.symbol.asInstanceOf[Symbols.ClassSymbol]
30-
31-
def getOwners(sym: Symbols.ClassSymbol): Seq[Symbols.ClassSymbol] = Seq(sym) ++
32-
(if sym.owner.isClass && !sym.owner.is(dotc.core.Flags.Package) then getOwners(sym.owner.asInstanceOf[Symbols.ClassSymbol]) else Nil)
33-
34-
def collectNames(tp: Types.Type): Seq[String] = tp match {
35-
case Types.AndType(t1, t2) => collectNames(t1) ++ collectNames(t2)
36-
case Types.AppliedType(tpe, _) => collectNames(tpe)
37-
case Types.AnnotatedType(tpe, _) => collectNames(tpe)
38-
case t: Types.NamedType => if t.symbol.is(dotc.core.Flags.Module) then Seq() else Seq(t.symbol.name.show)
39-
case t: Types.ThisType => Seq(t.cls.name.show)
40-
case _ => Seq()
41-
}
42-
43-
val allSyms = getOwners(classSym)
44-
45-
val allProcessed = allSyms.map { cSym =>
46-
def createTypeConstructor(tpe: Types.Type, topLevel: Boolean = true): String = tpe match {
47-
case t @ Types.TypeBounds(upper, lower) => lower match {
48-
case l: Types.HKTypeLambda =>
49-
(if topLevel then "" else "?") + l.paramInfos.map(p => createTypeConstructor(p, false)).mkString("[",", ","]")
50-
case _ => (if topLevel then "" else "_")
51-
}
52-
}
53-
val classType =
54-
val ct = cSym.classInfo.selfType.toText(printer).show.replace(".this","").replace("\n", " ").stripPrefix(s"$packageName.")
55-
Some(ct)
56-
val classNames = collectNames(cSym.classInfo.selfType)
57-
val classGenerics = Option.when(
58-
!cSym.typeParams.isEmpty
59-
)(
60-
cSym.typeParams.map(_.typeRef).map(t =>
61-
t.show +
62-
createTypeConstructor(t.asInstanceOf[Types.TypeRef].underlying)
63-
).mkString("[",", ","]")
64-
)
65-
SnippetCompilerData.ClassInfo(classType, classNames, classGenerics)
66-
}
67-
val firstProcessed = allProcessed.head
68-
SnippetCompilerData(packageName, allProcessed.reverse, Nil, position(hackGetPositionOfDocstring(using qctx)(originalSym)))
69-
case _ => getSnippetCompilerData(sym.maybeOwner, originalSym)
70-
} else SnippetCompilerData(packageName, Nil, Nil, position(hackGetPositionOfDocstring(using qctx)(originalSym)))
23+
SnippetCompilerData(packageName, position(hackGetPositionOfDocstring(using qctx)(originalSym)))
7124

7225
private def position(p: Option[qctx.reflect.Position]): SnippetCompilerData.Position =
73-
p.fold(SnippetCompilerData.Position(0, 0))(p => SnippetCompilerData.Position(p.startLine, p.startColumn))
26+
p.fold(SnippetCompilerData.Position(0, 0))(p => SnippetCompilerData.Position(p.startLine - 1, p.startColumn))
7427

7528
private def hackGetPositionOfDocstring(using Quotes)(s: qctx.reflect.Symbol): Option[qctx.reflect.Position] =
7629
import dotty.tools.dotc.core.Comments.CommentsContext
@@ -88,4 +41,4 @@ class SnippetCompilerDataCollector[Q <: Quotes](val qctx: Q):
8841
docstring.span
8942
).asInstanceOf[qctx.reflect.Position]
9043
}
91-
}
44+
}

0 commit comments

Comments
 (0)