Skip to content

Commit 0992a68

Browse files
committed
initial commit
1 parent 482b45f commit 0992a68

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# macOS
2+
.DS_Store
3+
4+
# sbt specific
5+
dist/*
6+
target/
7+
lib_managed/
8+
src_managed/
9+
project/boot/
10+
project/plugins/project/
11+
project/local-plugins.sbt
12+
.history
13+
.ensime
14+
.ensime_cache/
15+
.sbt-scripted/
16+
local.sbt
17+
18+
# Bloop
19+
.bsp
20+
21+
# VS Code
22+
.vscode/
23+
24+
# Metals
25+
.bloop/
26+
.metals/
27+
metals.sbt
28+
29+
# IDEA
30+
.idea
31+
.idea_modules
32+
/.worksheet/

.scalafmt.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = "3.5.3"
2+
runner.dialect = scala3

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## qw.scala
2+
3+
This library ports `qw()` syntax from Perl.
4+
5+
You can save time from double quotation hell when coding long `String` list.
6+
7+
### Usage
8+
9+
Usage is very simple!
10+
11+
#### Basic usage
12+
13+
Instead of writing `List[String](...)` directly, you can express it writing space-separated string:
14+
15+
```scala
16+
import com.github.windymelt.qw.Syntax._
17+
18+
val lis: List[String] = qw"You can save time from double-quotation hell"
19+
// => List("You", "can", "save", "time", "from", "double-quotation", "hell")
20+
```
21+
22+
#### Advanced usage
23+
24+
You can embed `String` variable into notation:
25+
26+
```scala
27+
import com.github.windymelt.qw.Syntax._
28+
29+
val (x, y, z) = ("a", "b", "c")
30+
31+
val lis: List[String] = qw"$a $b $c" // => List("a", "b", "c")
32+
val lis2: List[String] = qw"$a$b$c" // => List("a", "b", "c")
33+
```

build.sbt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
val scala3Version = "3.2.0"
2+
3+
lazy val root = project
4+
.in(file("."))
5+
.settings(
6+
name := "qw.scala",
7+
version := "0.1.0-SNAPSHOT",
8+
9+
scalaVersion := scala3Version,
10+
11+
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test
12+
)

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.7.1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.windymelt.qw
2+
3+
object Syntax:
4+
extension (sc: StringContext)
5+
def qw(args: String*): List[String] =
6+
val strings = sc.parts.iterator
7+
val expressions = args.iterator
8+
var buf = collection.mutable.ListBuffer(strings.next().split(" ")*)
9+
while strings.hasNext do
10+
buf.append(expressions.next().toString)
11+
buf.append(strings.next().toString.split(" ")*)
12+
buf.filterNot(_.isEmpty).toList
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.windymelt.qw
2+
// For more information on writing tests, see
3+
// https://scalameta.org/munit/docs/getting-started.html
4+
class SyntaxSuite extends munit.FunSuite {
5+
import Syntax._
6+
7+
test("qw\"\" returns Nil") {
8+
assertEquals(qw"", Nil)
9+
}
10+
11+
test("qw\"foo\" returns List(foo)") {
12+
assertEquals(qw"foo", List("foo"))
13+
}
14+
15+
test("qw\"foo bar buzz\" returns List(foo, bar, buzz)") {
16+
assertEquals(qw"foo bar buzz", List("foo", "bar", "buzz"))
17+
}
18+
19+
test("qw\"$x $y $z\" where x,y,z = a,b,c returns List(a, b, c)") {
20+
val (x, y, z) = ("a", "b", "c")
21+
assertEquals(qw"$x $y $z", List("a", "b", "c"))
22+
}
23+
24+
test("qw\"$x$y$z\" where x,y,z = a,b,c returns List(a, b, c)") {
25+
val (x, y, z) = ("a", "b", "c")
26+
assertEquals(qw"$x$y$z", List("a", "b", "c"))
27+
}
28+
}

0 commit comments

Comments
 (0)