From 3619ae2b12807e5b79a5656b7b762c1586abe6af Mon Sep 17 00:00:00 2001 From: Jonathan Dierksen Date: Tue, 20 Nov 2018 00:58:31 -0800 Subject: [PATCH] [swift mode] Support multi-line strings --- mode/swift/index.html | 11 +++++++++++ mode/swift/swift.js | 15 +++++++++------ mode/swift/test.js | 7 ++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/mode/swift/index.html b/mode/swift/index.html index d33eba4fcd..53d4134341 100644 --- a/mode/swift/index.html +++ b/mode/swift/index.html @@ -71,6 +71,17 @@

Swift mode

} + func funWithStrings() { + var numLines = 3 + print("This is a string!") + print(""" + This is a + multi-line + string! + """) + print("The preceding string had \(numLines) lines!") + } + } diff --git a/mode/swift/swift.js b/mode/swift/swift.js index b751185197..b7b3da56c0 100644 --- a/mode/swift/swift.js +++ b/mode/swift/swift.js @@ -73,9 +73,8 @@ stream.match("..") return "punctuation" } - if (ch == '"' || ch == "'") { - stream.next() - var tokenize = tokenString(ch) + if (ch = stream.match(/("{3}|"|')/)) { + var tokenize = tokenString(ch[0]) state.tokenize.push(tokenize) return tokenize(stream, state) } @@ -117,6 +116,7 @@ } function tokenString(quote) { + var singleLine = quote.length == 1 return function(stream, state) { var ch, escaped = false while (ch = stream.next()) { @@ -126,13 +126,16 @@ return "string" } escaped = false - } else if (ch == quote) { - break + } else if (stream.match(quote)) { + state.tokenize.pop() + return "string" } else { escaped = ch == "\\" } } - state.tokenize.pop() + if (singleLine) { + state.tokenize.pop() + } return "string" } } diff --git a/mode/swift/test.js b/mode/swift/test.js index d8a0609b8f..dc0a1f8fdc 100644 --- a/mode/swift/test.js +++ b/mode/swift/test.js @@ -35,7 +35,12 @@ // Strings and string interpolation. MT("strings", "[keyword var] [def a][punctuation :] [variable-2 String] [operator =] [string \"test\"]", - "[keyword var] [def b][punctuation :] [variable-2 String] [operator =] [string \"\\(][variable a][string )\"]"); + "[keyword var] [def b][punctuation :] [variable-2 String] [operator =] [string \"\\(][variable a][string )\"]", + "[keyword var] [def c] [operator =] [string \"\"\"]", + "[string multi]", + "[string line]", + "[string \"test\"]", + "[string \"\"\"]"); // Comments. MT("comments",