Skip to content

Commit f1d6a08

Browse files
committed
Don’t show Add documentation refactoring for declarations that are not on a new line
1 parent a6f85d8 commit f1d6a08

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

Sources/SourceKitLSP/Swift/CodeActions/AddDocumentation.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,20 @@ import SwiftSyntax
3737
public struct AddDocumentation: EditRefactoringProvider {
3838
@_spi(Testing)
3939
public static func textRefactor(syntax: DeclSyntax, in context: Void) -> [SourceEdit] {
40-
let hasDocumentation = syntax.leadingTrivia.contains(where: { trivia in
40+
let hasDocumentation = syntax.leadingTrivia.contains { trivia in
4141
switch trivia {
42-
case .blockComment(_), .docBlockComment(_), .lineComment(_), .docLineComment(_):
42+
case .blockComment, .docBlockComment, .lineComment, .docLineComment:
4343
return true
4444
default:
4545
return false
4646
}
47-
})
47+
}
48+
49+
// We consider nodes at the start of the source file at being on a new line
50+
let isOnNewLine =
51+
syntax.leadingTrivia.contains(where: \.isNewline) || syntax.previousToken(viewMode: .sourceAccurate) == nil
4852

49-
guard !hasDocumentation else {
53+
guard !hasDocumentation && isOnNewLine else {
5054
return []
5155
}
5256

Tests/SourceKitLSPTests/CodeActionTests.swift

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,12 @@ final class CodeActionTests: XCTestCase {
909909
}
910910
}
911911

912-
func testAddDocumentationRefactorSingleParameter() async throws {
912+
func testAddDocumentationRefactorNotAtStartOfFile() async throws {
913913
try await assertCodeActions(
914914
"""
915+
struct Foo {
915916
1️⃣func 2️⃣refactor(3️⃣syntax: 4️⃣Decl5️⃣Syntax)6️⃣ { }7️⃣
917+
}
916918
""",
917919
ranges: [("1️⃣", "2️⃣"), ("1️⃣", "6️⃣"), ("1️⃣", "7️⃣")],
918920
exhaustive: false
@@ -942,6 +944,49 @@ final class CodeActionTests: XCTestCase {
942944
}
943945
}
944946

947+
func testAddDocumentationRefactorAtStartOfFile() async throws {
948+
try await assertCodeActions(
949+
"""
950+
1️⃣func 2️⃣refactor(3️⃣syntax: 4️⃣Decl5️⃣Syntax)6️⃣ { }7️⃣
951+
""",
952+
ranges: [("1️⃣", "2️⃣"), ("1️⃣", "6️⃣"), ("1️⃣", "7️⃣")],
953+
exhaustive: false
954+
) { uri, positions in
955+
[
956+
CodeAction(
957+
title: "Add documentation",
958+
kind: .refactorInline,
959+
diagnostics: nil,
960+
edit: WorkspaceEdit(
961+
changes: [
962+
uri: [
963+
TextEdit(
964+
range: Range(positions["1️⃣"]),
965+
newText: """
966+
/// A description
967+
/// - Parameter syntax:
968+
\("")
969+
"""
970+
)
971+
]
972+
]
973+
),
974+
command: nil
975+
)
976+
]
977+
}
978+
}
979+
980+
func testAddDocumentationDoesNotShowUpIfItIsNotOnItsOwnLine() async throws {
981+
try await assertCodeActions(
982+
"""
983+
var x = 1; var 1️⃣y = 2
984+
"""
985+
) { uri, positions in
986+
[]
987+
}
988+
}
989+
945990
/// Retrieves the code action at a set of markers and asserts that it matches a list of expected code actions.
946991
///
947992
/// - Parameters:

0 commit comments

Comments
 (0)