Skip to content

Commit d12d805

Browse files
committed
Fixed incorrect handling of commas in documentation comments #13
1 parent 431ce36 commit d12d805

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

Sources/SwiftMCPMacros/Documentation.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ struct Documentation {
7878
// Helper to flush the current parameter's accumulated lines into our dictionary.
7979
func flushCurrentParameter() {
8080
if let paramName = currentParameterName {
81-
let fullDescription = currentParameterLines.joined(separator: " ").trimmingCharacters(in: .whitespaces)
81+
// Join lines with a single space, but preserve existing spaces
82+
let fullDescription = currentParameterLines
83+
.map { $0.trimmingCharacters(in: .whitespaces) }
84+
.filter { !$0.isEmpty }
85+
.joined(separator: " ")
8286
// Escape the description when storing it
8387
parameters[paramName] = fullDescription.escapedForSwiftString
8488
}
@@ -200,7 +204,11 @@ struct Documentation {
200204
else {
201205
if currentParameterName != nil {
202206
// If we are in the middle of a parameter, treat the line as a continuation.
203-
currentParameterLines.append(line)
207+
// Only add non-empty lines and trim whitespace
208+
let trimmedLine = line.trimmingCharacters(in: .whitespaces)
209+
if !trimmedLine.isEmpty {
210+
currentParameterLines.append(trimmedLine)
211+
}
204212
} else if inReturnsSection && !inOtherSection {
205213
// If we're in the returns section, add to returns lines
206214
returnsLines.append(line)

Tests/SwiftMCPTests/MacroDocumentationTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,29 @@ func testParagraphBreaksAndQuotes() {
240240
#expect(doc.description == "A Calculator for simple math doing additionals, subtractions etc.\\n\\nTesting \\\"quoted\\\" stuff. And on multiple lines. \\'single quotes\\'")
241241
}
242242

243+
@Test("Handles parameter descriptions with commas and newlines")
244+
func testParameterDescriptionsWithCommasAndNewlines() {
245+
let docText = """
246+
/**
247+
Get reminders from the reminders app with flexible filtering options.
248+
249+
- Parameters:
250+
- completed: If true, fetch completed reminders. If false, fetch incomplete reminders. If not specified, fetch all reminders.
251+
- startDate: ISO date string for the start of the date range to fetch reminders from
252+
- endDate: ISO date string for the end of the date range to fetch reminders from
253+
- listNames: Names of reminder lists to fetch from. If empty or not specified,
254+
fetches from all lists.
255+
- searchText: Text to search for in reminder titles
256+
*/
257+
"""
258+
let doc = Documentation(from: docText)
259+
#expect(doc.description == "Get reminders from the reminders app with flexible filtering options.")
260+
#expect(doc.parameters["completed"] == "If true, fetch completed reminders. If false, fetch incomplete reminders. If not specified, fetch all reminders.")
261+
#expect(doc.parameters["startDate"] == "ISO date string for the start of the date range to fetch reminders from")
262+
#expect(doc.parameters["endDate"] == "ISO date string for the end of the date range to fetch reminders from")
263+
#expect(doc.parameters["listNames"] == "Names of reminder lists to fetch from. If empty or not specified, fetches from all lists.")
264+
#expect(doc.parameters["searchText"] == "Text to search for in reminder titles")
265+
#expect(doc.returns == nil)
266+
}
267+
243268
#endif

0 commit comments

Comments
 (0)