Skip to content

Commit 71f5119

Browse files
authored
Merge pull request #13 from tayloraswift/linebreak-long-signatures
linebreak long function signatures
2 parents 776f54a + 81fef46 commit 71f5119

File tree

66 files changed

+1460
-490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1460
-490
lines changed

Assets/css/Main.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/css/Main.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ let package:Package = .init(
7373
.package(url: "https://github.com/tayloraswift/swift-grammar", .upToNextMinor(
7474
from: "0.3.2")),
7575
.package(url: "https://github.com/tayloraswift/swift-mongodb", .upToNextMinor(
76-
from: "0.8.1")),
76+
from: "0.8.2")),
7777

7878
.package(url: "https://github.com/swift-server/swift-backtrace", .upToNextMinor(
7979
from: "1.3.4")),
@@ -236,6 +236,9 @@ let package:Package = .init(
236236
.target(name: "MarkdownPluginSwift", dependencies:
237237
[
238238
.target(name: "MarkdownABI"),
239+
.target(name: "Signatures"),
240+
.target(name: "Symbols"),
241+
239242
.product(name: "IDEUtils", package: "swift-syntax"),
240243
.product(name: "SwiftParser", package: "swift-syntax"),
241244
]),
@@ -330,6 +333,7 @@ let package:Package = .init(
330333
[
331334
.target(name: "JSON"),
332335
.target(name: "LexicalPaths"),
336+
.target(name: "MarkdownPluginSwift"),
333337
.target(name: "ModuleGraphs"),
334338
.target(name: "Signatures"),
335339
.target(name: "Symbols"),
@@ -481,6 +485,13 @@ let package:Package = .init(
481485
.product(name: "Testing", package: "swift-grammar"),
482486
]),
483487

488+
.executableTarget(name: "MarkdownPluginSwiftTests", dependencies:
489+
[
490+
.target(name: "MarkdownPluginSwift"),
491+
.target(name: "MarkdownRendering"),
492+
.product(name: "Testing", package: "swift-grammar"),
493+
]),
494+
484495
.executableTarget(name: "MarkdownRenderingTests", dependencies:
485496
[
486497
.target(name: "MarkdownRendering"),

Sources/MarkdownABI/Bytecode/MarkdownBytecode.Context.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ extension MarkdownBytecode
5555
case `class`
5656
case type
5757
case `typealias`
58+
/// New in 8.0.
59+
case indent
5860

5961
// Section elements.
6062
case parameters = 0xC0

Sources/MarkdownABI/Bytecode/MarkdownBytecode.Marker.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
extension MarkdownBytecode
22
{
3+
/// Markers inhabit the unassigned codepoints of the UTF-8 encoding.
4+
///
5+
/// https://en.wikipedia.org/wiki/UTF-8
36
@frozen public
47
enum Marker:UInt8, Equatable, Hashable, Sendable
58
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import IDEUtils
2+
import MarkdownABI
3+
4+
extension MarkdownBytecode.Context
5+
{
6+
init?(classification:SyntaxClassification)
7+
{
8+
switch classification
9+
{
10+
case .none,
11+
.editorPlaceholder: return nil
12+
13+
case .attribute: self = .attribute
14+
15+
case .buildConfigId: self = .directive
16+
case .poundDirectiveKeyword: self = .magic
17+
18+
case .lineComment,
19+
.blockComment: self = .comment
20+
case .docLineComment,
21+
.docBlockComment: self = .doccomment
22+
23+
case .dollarIdentifier: self = .pseudo
24+
case .identifier: self = .identifier
25+
case .operatorIdentifier: self = .operator
26+
27+
case .integerLiteral,
28+
.floatingLiteral: self = .literalNumber
29+
case .stringLiteral,
30+
.objectLiteral: self = .literalString
31+
32+
case .keyword: self = .keyword
33+
case .stringInterpolationAnchor: self = .interpolation
34+
case .typeIdentifier: self = .type
35+
}
36+
}
37+
}

Sources/MarkdownPluginSwift/MarkdownCodeLanguage.Swift.Highlighter.swift

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,14 @@ extension MarkdownCodeLanguage.Swift.Highlighter:MarkdownCodeHighlighter
3636
start: base + span.offset,
3737
count: span.length)
3838

39-
let context:MarkdownBytecode.Context
40-
switch span.kind
39+
if let context:MarkdownBytecode.Context = .init(classification: span.kind)
4140
{
42-
case .none,
43-
.editorPlaceholder: binary += text ; continue
44-
45-
case .attribute: context = .attribute
46-
47-
case .buildConfigId: context = .directive
48-
case .poundDirectiveKeyword: context = .magic
49-
50-
case .lineComment,
51-
.blockComment: context = .comment
52-
case .docLineComment,
53-
.docBlockComment: context = .doccomment
54-
55-
case .dollarIdentifier: context = .pseudo
56-
case .identifier: context = .identifier
57-
case .operatorIdentifier: context = .operator
58-
59-
case .integerLiteral,
60-
.floatingLiteral: context = .literalNumber
61-
case .stringLiteral,
62-
.objectLiteral: context = .literalString
63-
64-
case .keyword: context = .keyword
65-
case .stringInterpolationAnchor: context = .interpolation
66-
case .typeIdentifier: context = .type
41+
binary[context] { $0 += text }
42+
}
43+
else
44+
{
45+
binary += text
6746
}
68-
69-
binary[context] { $0 += text }
7047
}
7148
}
7249
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import MarkdownABI
2+
import Signatures
3+
4+
extension Signature.Abridged
5+
{
6+
@inlinable public
7+
init(_ fragments:__shared some Sequence<Signature<Scalar>.Fragment>, actor:Bool = false)
8+
{
9+
var utf8:[UInt8] = []
10+
for fragment:Signature.Fragment in fragments
11+
{
12+
utf8 += fragment.spelling.utf8
13+
}
14+
self.init(utf8: utf8, actor: actor)
15+
}
16+
17+
@_spi(testable) public
18+
init(_ string:String)
19+
{
20+
self.init(utf8: [UInt8].init(string.utf8))
21+
}
22+
23+
@usableFromInline internal
24+
init(utf8:[UInt8], actor:Bool = false)
25+
{
26+
let signature:SignatureSyntax = utf8.withUnsafeBufferPointer(SignatureSyntax.init)
27+
let bytecode:MarkdownBytecode = .init
28+
{
29+
for span:SignatureSyntax.Span in signature.elements
30+
{
31+
switch span
32+
{
33+
case .wbr(indent: false):
34+
$0[.wbr]
35+
36+
case .wbr(indent: true):
37+
$0[.indent]
38+
39+
case .text(let range, nil, _):
40+
$0 += utf8[range]
41+
42+
case .text(let range, let color?, let depth):
43+
let text:String = .init(decoding: utf8[range], as: Unicode.UTF8.self)
44+
switch (color, text, depth)
45+
{
46+
case (.keyword, "subscript", .toplevel?),
47+
(.keyword, "deinit", .toplevel?),
48+
(.keyword, "init", .toplevel?),
49+
(.identifier, _, _):
50+
$0[.identifier] = text
51+
52+
case (.keyword, "class", .toplevel?):
53+
guard actor
54+
else
55+
{
56+
fallthrough
57+
}
58+
59+
$0 += "actor"
60+
61+
case (_, _, _):
62+
$0 += text
63+
}
64+
}
65+
}
66+
}
67+
self.init(bytecode: bytecode)
68+
}
69+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
3+
import MarkdownABI
4+
import Signatures
5+
6+
extension Signature.Expanded
7+
{
8+
@inlinable public
9+
init(_ fragments:__shared some Collection<Signature<Scalar>.Fragment>,
10+
keywords:inout InterestingKeywords)
11+
{
12+
var utf8:[UInt8] = []
13+
utf8.reserveCapacity(fragments.reduce(0) { $0 + $1.spelling.utf8.count })
14+
15+
var symbols:[Int: Scalar] = [:]
16+
for fragment:Signature.Fragment in fragments
17+
{
18+
let i:Int = utf8.endIndex
19+
utf8 += fragment.spelling.utf8
20+
21+
if let referent:Scalar = fragment.referent
22+
{
23+
symbols[i] = referent
24+
}
25+
}
26+
27+
self.init(utf8: utf8, keywords: &keywords, symbols: &symbols)
28+
29+
if !symbols.isEmpty
30+
{
31+
fatalError("""
32+
syntax didn’t round-trip, failed to match symbols: \(symbols), \
33+
source: '\(String.init(decoding: utf8, as: Unicode.UTF8.self))'
34+
""")
35+
}
36+
}
37+
38+
@inlinable @_spi(testable) public
39+
init(_ string:String)
40+
{
41+
var ignored:InterestingKeywords = .init()
42+
self.init(string, keywords: &ignored)
43+
}
44+
45+
@inlinable @_spi(testable) public
46+
init(_ string:String, keywords:inout InterestingKeywords)
47+
{
48+
var empty:[Int: Scalar] = [:]
49+
self.init(utf8: [UInt8].init(string.utf8), keywords: &keywords, symbols: &empty)
50+
}
51+
52+
@inlinable internal
53+
init(utf8:[UInt8], keywords:inout InterestingKeywords, symbols:inout [Int: Scalar])
54+
{
55+
let signature:SignatureSyntax = utf8.withUnsafeBufferPointer(SignatureSyntax.init)
56+
var references:[Scalar: Int] = [:]
57+
var referents:[Scalar] = []
58+
59+
let bytecode:MarkdownBytecode = .init
60+
{
61+
for span:SignatureSyntax.Span in signature.elements
62+
{
63+
switch span
64+
{
65+
case .wbr(indent: false):
66+
$0[.wbr]
67+
68+
case .wbr(indent: true):
69+
$0[.indent]
70+
71+
case .text(let range, nil, _):
72+
$0 += utf8[range]
73+
74+
case .text(let range, let color?, .toplevel?):
75+
if case .keyword = color
76+
{
77+
// The `actor` and `async` keywords are contextual; there is no
78+
// other way to detect them besides inspecting token text!
79+
switch String.init(decoding: utf8[range], as: Unicode.UTF8.self)
80+
{
81+
case "actor": keywords.actor = true
82+
case "class": keywords.class = true
83+
default: break
84+
}
85+
}
86+
87+
fallthrough
88+
89+
case .text(let range, let color?, _):
90+
91+
$0[color]
92+
{
93+
if let referent:Scalar = symbols.removeValue(forKey: range.lowerBound)
94+
{
95+
$0[.href] =
96+
{
97+
if let reference:Int = $0
98+
{
99+
return reference
100+
}
101+
else
102+
{
103+
let next:Int = referents.endIndex
104+
referents.append(referent)
105+
$0 = next
106+
return next
107+
}
108+
} (&references[referent])
109+
}
110+
}
111+
content:
112+
{
113+
$0 += utf8[range]
114+
}
115+
}
116+
}
117+
}
118+
119+
self.init(bytecode: bytecode, scalars: referents)
120+
}
121+
}

0 commit comments

Comments
 (0)