### Install and use metap CLI tool Source: https://scalameta.org/docs/semanticdb/guide.html Install the metap command-line tool using 'cs install metap'. Use it to prettyprint .semanticdb files found in the provided classpath. Options control the prettyprinting format. ```bash # downloads CLI tool based on org.scalameta.semanticdb-metap, version 4.17.0 cs install metap # if necessary metap [options] ``` -------------------------------- ### Install metac and metap with Coursier Source: https://scalameta.org/docs/semanticdb/guide.html Installs the metac and metap command-line tools using the Coursier package manager. Ensure Coursier is installed first. ```bash cs install metac metap ``` -------------------------------- ### Install and Use Metac CLI Tool Source: https://scalameta.org/docs/semanticdb/guide.html Instructions for installing the metac command-line tool and its basic usage. Metac acts as a replacement for scalac for generating SemanticDB files. ```bash # downloads CLI tool based on org.scalameta.metac, version 4.17.0 cs install metac # if necessary metac [ ...] [ ...] [ ...] ``` -------------------------------- ### AnnotationTree Examples Source: https://scalameta.org/docs/semanticdb/specification.html Provides examples of how AnnotationTree can represent various Java annotation syntaxes, from simple annotations to those with arguments and nested annotations. ```scala AnnotationTree(TypeRef(None, , List()), None) ``` ```scala AnnotationTree(TypeRef(None, , List()), List(AssignTree(IdTree(), LiteralTree(StringConstant("arg"))))) ``` ```scala AnnotationTree(TypeRef(None, , List()), List(AssignTree(IdTree(), ApplyTree(IdTree(), List()))) ``` ```scala AnnotationTree(TypeRef(None, , List()), List(AssignTree(IdTree(), SelectTree(IdTree(), IdTree()))) ``` ```scala AnnotationTree(TypeRef(None, , List()), List(AssignTree(IdTree(), LiteralTree(StringConstant("/"))))) ``` ```scala AnnotationTree(TypeRef(None, , List()), List(AssignTree(IdTree(), AnnotationTree(TypeRef(None, , List()), None)))) ``` -------------------------------- ### Scala Program Example Source: https://scalameta.org/docs/semanticdb/guide.html A simple Scala object with a main method, used to demonstrate SemanticDB generation. ```scala object Test { def main(args: Array[String]): Unit = { println("hello world") } } ``` -------------------------------- ### Scala Annotation Examples Source: https://scalameta.org/docs/semanticdb/specification.html Illustrates how various Scala annotations are represented using AnnotationTree. Note that expression annotations are not supported. ```scala AnnotationTree(TypeRef(None, , List()), None) ``` ```scala AnnotationTree(TypeRef(None, , List()), List(AssignTree(IdTree(), LiteralTree(StringConstant("arg"))))) ``` ```scala AnnotationTree(TypeRef(None, , List()), List(AssignTree(IdTree(), ApplyTree(IdTree(), List())))) ``` ```scala AnnotationTree(TypeRef(None, , List()), List(AssignTree(IdTree(), SelectTree(IdTree(), IdTree()))) ``` -------------------------------- ### Access Modifier Examples Source: https://scalameta.org/docs/semanticdb/specification.html Illustrates how different Java access modifiers (private, protected, public) are represented using the Access protobuf messages. Notes unsupported Java constructs. ```text `private F f;` ``` ```text `package x; class A {}` ``` ```text `protected F f;` ``` ```text `public F f;` ``` -------------------------------- ### Scala Class Definition Example Source: https://scalameta.org/docs/semanticdb/specification.html Illustrates a Scala class definition with a method that uses a structural type, relevant for understanding hardlinking in SemanticDB. ```scala class C { def m = new { def x: Int = ??? } } ``` -------------------------------- ### Scala Class and Constructor Declaration Source: https://scalameta.org/docs/semanticdb/specification.html Example of declaring a class with nested classes and constructors in Scala. This serves as a basis for symbol representation. ```scala package a; class Outer { Outer() {} class Inner { Inner() {} } } ``` -------------------------------- ### Parse Code with Comments Attached to Different Trees Source: https://scalameta.org/docs/trees/examples.html Illustrates how scalameta attaches comments to trees, showing an example where a comment can be accessible from both a value definition and a term name. This highlights the parser's logic for comment association. ```scala val foo = bar // some comment ``` -------------------------------- ### Get structure of a syntax tree Source: https://scalameta.org/docs/trees/guide.html Obtain the structural representation of an existing syntax tree node by calling the `.structure` method. This output can be safely copied and pasted into code to reconstruct the tree. ```scala println("function(argument)".parse[Stat].get.structure) ``` -------------------------------- ### Initialize Ammonite REPL with Scalameta Source: https://scalameta.org/docs/trees/guide.html Run the Ammonite REPL with the --thin option and import Scalameta to experiment with its features interactively. ```bash $ amm --thin Loading... Welcome to the Ammonite Repl 2.3.8 (Scala 2.13.5 Java 11.0.7) @ import $ivy.`org.scalameta::scalameta:4.17.0`, scala.meta._ ``` -------------------------------- ### Construct a simple function call with quasiquotes Source: https://scalameta.org/docs/trees/guide.html Use quasiquotes with `q"""` to construct code. The `.structure` method displays the resulting tree. ```scala println(q"function(argument)".structure) ``` -------------------------------- ### Parse Package to Source Tree Source: https://scalameta.org/docs/trees/examples.html Demonstrates parsing a package string into a Scalameta Source tree, showing the representation of a package declaration. ```scala "package a".parse[Source].get.structure ``` -------------------------------- ### Inspect SemanticDB files with protoc Source: https://scalameta.org/docs/semanticdb/guide.html Use the protoc command-line tool with --decode or --decode_raw options to inspect SemanticDB protobuf payloads. This method is lower-level and metap is generally recommended. ```bash $ tree . ├── META-INF │ └── semanticdb │ └── Test.scala.semanticdb └── Test.scala $ protoc --proto_path \ --decode scala.meta.internal.semanticdb.TextDocuments\ semanticdb.proto < META-INF/semanticdb/Test.scala.semanticdb ``` ```protobuf documents { schema: SEMANTICDB4 uri: "Test.scala" symbols { symbol: "_empty_/Test.main().(args)" kind: PARAMETER name: "args" access { publicAccess { } } language: SCALA signature { valueSignature { tpe { typeRef { symbol: "scala/Array#" type_arguments { typeRef { symbol: "scala/Predef.String#" } } } } } } } symbols { symbol: "_empty_/Test." kind: OBJECT properties: 8 ... ``` -------------------------------- ### Define Range Structure Source: https://scalameta.org/docs/semanticdb/specification.html Represents a range within a document using zero-based line and character offsets. The start point is inclusive, and the end point is exclusive. ```protobuf message Range { int32 start_line = 1; int32 start_character = 2; int32 end_line = 3; int32 end_character = 4; } ``` -------------------------------- ### Print syntax tree to source code Source: https://scalameta.org/docs/trees/guide.html Convert a Scalameta syntax tree back into its original Scala source code representation using the `.syntax` method. ```scala println(tree.syntax) // object Main extends App { print("Hello!") } ``` -------------------------------- ### Inspect SemanticDB file structure Source: https://scalameta.org/docs/semanticdb/guide.html Displays the directory structure after running metac, showing the generated .semanticdb file within the META-INF/semanticdb directory. ```bash $ tree . ├── META-INF │ └── semanticdb │ └── Test.scala.semanticdb └── Test.scala ``` -------------------------------- ### Scala Access Modifier Examples Source: https://scalameta.org/docs/semanticdb/specification.html Maps Scala access modifiers to their corresponding SemanticDB Access messages. Covers private, protected, and public access with 'this' and 'within' qualifiers. ```scala PrivateAccess() ``` ```scala PrivateThisAccess() ``` ```scala PrivateWithinAccess() ``` ```scala ProtectedAccess() ``` ```scala ProtectedThisAccess() ``` ```scala ProtectedWithinAccess() ``` ```scala PublicAccess() ``` -------------------------------- ### Parse virtual file with Sbt1 dialect Source: https://scalameta.org/docs/trees/guide.html Demonstrates parsing a virtual file with multiple top-level statements using the `dialects.Sbt1` dialect, similar to parsing a string directly. ```scala println( dialects.Sbt1( Input.VirtualFile("build.sbt", buildSbt) ).parse[Source].get.stats ) ``` -------------------------------- ### Import Scalameta Source: https://scalameta.org/docs/trees/guide.html Ensure this import is in your scope to use Scalameta's extension methods for parsing and other functionalities. ```scala import scala.meta._ ``` -------------------------------- ### Init Quasiquote Source: https://scalameta.org/docs/trees/quasiquotes.html Represent initialization of classes or traits using `init"$tpe(...$exprss)"` or `init"this(...$exprss)"`. `exprss` is a comma-separated list of expressions. ```scala init"$tpe(...$exprss)" init"this(...$exprss)" ``` -------------------------------- ### Parse and display SemanticDB with metap Source: https://scalameta.org/docs/semanticdb/guide.html Uses the metap command-line tool to prettyprint the contents of a .semanticdb file. This tool helps in understanding the semantic information stored in the protobuf payload. ```bash $ metap . Test.scala ---------- Summary: Schema => SemanticDB v4 Uri => Test.scala Text => empty Language => Scala Symbols => 3 entries Occurrences => 7 entries Symbols: _empty_/Test. => final object Test extends AnyRef { +1 decls } _empty_/Test.main(). => method main(args: Array[String]): Unit _empty_/Test.main().(args) => param args: Array[String] Occurrences: [0:7..0:11) <= _empty_/Test. [1:6..1:10) <= _empty_/Test.main(). [1:11..1:15) <= _empty_/Test.main().(args) [1:17..1:22) => scala/Array# [1:23..1:29) => scala/Predef.String# [1:33..1:37) => scala/Unit# [2:4..2:11) => scala/Predef.println(+1). ``` -------------------------------- ### Define build.sbt content Source: https://scalameta.org/docs/trees/guide.html Define a string containing the content of a build.sbt file with multiple top-level statements. ```scala val buildSbt = """ val core = project val cli = project.dependsOn(core) """ ``` -------------------------------- ### Generate SemanticDB with metac Source: https://scalameta.org/docs/semanticdb/guide.html Uses the metac command-line tool to generate .semanticdb files for a given Scala source file. Metac acts as a wrapper around the Scala compiler. ```bash metac Test.scala ``` -------------------------------- ### Import Quasiquote Source: https://scalameta.org/docs/trees/quasiquotes.html Construct or deconstruct import statements in Scala. ```scala q"import ..$importersnel" ``` -------------------------------- ### Expression Quasiquotes: New Source: https://scalameta.org/docs/trees/quasiquotes.html Represent `new` expressions for creating instances of classes using `q"new $init"`. `$init` refers to an `Init` quasiquote. ```scala q"new $init" ``` -------------------------------- ### Comparing Scalameta Trees for Equality Source: https://scalameta.org/docs/trees/guide.html Understand that scalameta trees use reference equality by default (`==`). Use pattern matching or `.structure` for structural equality. For efficiency, consider `isEqual` from `scala.meta.contrib`. ```scala "true".parse[Term].get == q"true" // res27: Boolean = false ``` ```scala q"true" == q"true" // res28: Boolean = false ``` ```scala { val treeReference = q"true" treeReference == treeReference } // res29: Boolean = true ``` ```scala q"true" match { case q"true" => println("YAY!") } // YAY! ``` ```scala q"true".structure == q"true".structure // res31: Boolean = true ``` ```scala import scala.meta.contrib._ q"true".isEqual(q"true") // res32: Boolean = true ``` -------------------------------- ### Pattern Matching and Variable Declarations in Scala Source: https://scalameta.org/docs/semanticdb/specification.html Illustrates how pattern variables in matching expressions and pattern definitions are represented as symbols. ```scala class C { ??? match { case List(x) => ??? } val List(xval) = ??? var List(xvar) = ??? } ``` -------------------------------- ### Print virtual file syntax tree Source: https://scalameta.org/docs/trees/guide.html Prints the syntax tree obtained from parsing a virtual file. The output reflects the structure of the Scala code within that file. ```scala print(exampleTree.syntax) // object Example extends App { // println("Hello from a file!") // } ``` -------------------------------- ### Scalac Plugin Options Format Source: https://scalameta.org/docs/semanticdb/guide.html The format for passing options to the semanticdb-scalac plugin via scalac. Options are prefixed with -P:semanticdb:. ```bash -P:semanticdb: