Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Sources/SwiftLanguageService/CursorInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ struct CursorInfo {
documentManager.latestSnapshotOrDisk(uri, language: .swift)
}
if let snapshot {
let position = snapshot.positionOf(zeroBasedLine: line - 1, utf8Column: column - 1)
location = Location(uri: uri, range: Range(position))
let length = (dict[keys.length] ?? 0)
let positionStart = snapshot.positionOf(zeroBasedLine: line - 1, utf8Column: column - 1)
let positionEnd = snapshot.positionOf(zeroBasedLine: line - 1, utf8Column: column + length - 1)
location = Location(uri: uri, range: positionStart..<positionEnd)
} else {
logger.error("Failed to get snapshot for \(uri.forLogging) to convert position")
location = nil
Expand Down
20 changes: 10 additions & 10 deletions Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
let project = try await SwiftPMTestProject(
files: [
"Lib/MyFile.swift": """
public func 1️⃣foo() {}
public func 1️⃣foo3️⃣() {}
""",
"MyExec/MyExec.swift": """
import Lib
Expand Down Expand Up @@ -1119,7 +1119,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
let definition = try await project.testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
XCTAssertEqual(definition, .locations([try project.location(from: "1️⃣", to: "1️⃣", in: "MyFile.swift")]))
XCTAssertEqual(definition, .locations([try project.location(from: "1️⃣", to: "3️⃣", in: "MyFile.swift")]))
}

func testCrossModuleFunctionalityEvenIfLowLevelModuleHasErrors() async throws {
Expand All @@ -1135,7 +1135,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
"LibB/LibB.swift": """
import LibA

public func 1️⃣libBTest() -> Int {
public func 1️⃣libBTest3️⃣() -> Int {
return libATest()
}
""",
Expand Down Expand Up @@ -1165,7 +1165,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
let response = try await project.testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
XCTAssertEqual(response, .locations([try project.location(from: "1️⃣", to: "1️⃣", in: "LibB.swift")]))
XCTAssertEqual(response, .locations([try project.location(from: "1️⃣", to: "3️⃣", in: "LibB.swift")]))
}

func testCrossModuleFunctionalityWithErrors() async throws {
Expand All @@ -1174,7 +1174,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
let project = try await SwiftPMTestProject(
files: [
"LibA/LibA.swift": """
public func 1️⃣libATest() -> Invalid {
public func 1️⃣libATest3️⃣() -> Invalid {
return ""
}
""",
Expand Down Expand Up @@ -1203,7 +1203,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
let response = try await project.testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
XCTAssertEqual(response, .locations([try project.location(from: "1️⃣", to: "1️⃣", in: "LibA.swift")]))
XCTAssertEqual(response, .locations([try project.location(from: "1️⃣", to: "3️⃣", in: "LibA.swift")]))
}

func testCrossModuleFunctionalityWithPreparationNoSkipping() async throws {
Expand All @@ -1219,7 +1219,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
"LibB/LibB.swift": """
import LibA

public func 1️⃣libBTest() -> Int {
public func 1️⃣libBTest3️⃣() -> Int {
return libATest()
}
""",
Expand Down Expand Up @@ -1249,7 +1249,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
let response = try await project.testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
XCTAssertEqual(response, .locations([try project.location(from: "1️⃣", to: "1️⃣", in: "LibB.swift")]))
XCTAssertEqual(response, .locations([try project.location(from: "1️⃣", to: "3️⃣", in: "LibB.swift")]))
}

func testUpdatePackageDependency() async throws {
Expand Down Expand Up @@ -1644,7 +1644,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
"LibA.swift",
newMarkedContents: """
public struct LibA {
public func 2️⃣test() {}
public func 2️⃣test3️⃣() {}
}
"""
)
Expand All @@ -1655,7 +1655,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
let definitionAfterEdit = try await project.testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
return definitionAfterEdit?.locations == [Location(uri: libAUri, range: Range(newAMarkers["2️⃣"]))]
return definitionAfterEdit?.locations == [Location(uri: libAUri, range: newAMarkers["2️⃣"]..<newAMarkers["3️⃣"])]
}
}

Expand Down
34 changes: 17 additions & 17 deletions Tests/SourceKitLSPTests/DefinitionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DefinitionTests: SourceKitLSPTestCase {

let positions = testClient.openDocument(
"""
let 1️⃣foo = 1
let 1️⃣foo3️⃣ = 1
_ = foo2️⃣
""",
uri: uri
Expand All @@ -34,7 +34,7 @@ class DefinitionTests: SourceKitLSPTestCase {
let response = try await testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
XCTAssertEqual(response?.locations, [Location(uri: uri, range: Range(positions["1️⃣"]))])
XCTAssertEqual(response?.locations, [Location(uri: uri, range: positions["1️⃣"]..<positions["3️⃣"])])
}

func testJumpToDefinitionIncludesOverrides() async throws {
Expand Down Expand Up @@ -180,8 +180,8 @@ class DefinitionTests: SourceKitLSPTestCase {
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
struct 1️⃣Foo {
2️⃣init() {}
struct 1️⃣Foo4️⃣ {
2️⃣init()5️⃣ {}
}
_ = 3️⃣Foo()
""",
Expand All @@ -194,8 +194,8 @@ class DefinitionTests: SourceKitLSPTestCase {
XCTAssertEqual(
response,
.locations([
Location(uri: uri, range: Range(positions["1️⃣"])),
Location(uri: uri, range: Range(positions["2️⃣"])),
Location(uri: uri, range: positions["1️⃣"]..<positions["4️⃣"]),
Location(uri: uri, range: positions["2️⃣"]..<positions["5️⃣"]),
])
)
}
Expand All @@ -205,8 +205,8 @@ class DefinitionTests: SourceKitLSPTestCase {
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
func 1️⃣foo() -> Int { 1 }
func 2️⃣foo() -> String { "" }
func 1️⃣foo()4️⃣ -> Int { 1 }
func 2️⃣foo()5️⃣ -> String { "" }
func test() {
_ = 3️⃣foo()
}
Expand All @@ -220,8 +220,8 @@ class DefinitionTests: SourceKitLSPTestCase {
XCTAssertEqual(
response,
.locations([
Location(uri: uri, range: Range(positions["1️⃣"])),
Location(uri: uri, range: Range(positions["2️⃣"])),
Location(uri: uri, range: positions["1️⃣"]..<positions["4️⃣"]),
Location(uri: uri, range: positions["2️⃣"]..<positions["5️⃣"]),
])
)
}
Expand Down Expand Up @@ -314,7 +314,7 @@ class DefinitionTests: SourceKitLSPTestCase {

let positions = testClient.openDocument(
"""
class 1️⃣Foo {}
class 1️⃣Foo3️⃣ {}

func test() {
2️⃣Foo()
Expand All @@ -328,7 +328,7 @@ class DefinitionTests: SourceKitLSPTestCase {
)
XCTAssertEqual(
response?.locations,
[Location(uri: uri, range: Range(positions["1️⃣"]))]
[Location(uri: uri, range: positions["1️⃣"]..<positions["3️⃣"])]
)
}

Expand Down Expand Up @@ -358,7 +358,7 @@ class DefinitionTests: SourceKitLSPTestCase {

let (aUri, updatedAPositions) = try await project.changeFileOnDisk(
"FileA.swift",
newMarkedContents: "func 2️⃣sayHello() {}"
newMarkedContents: "func 2️⃣sayHello()3️⃣ {}"
)

// Wait until SourceKit-LSP has handled the `DidChangeWatchedFilesNotification` (which it only does after a delay
Expand All @@ -375,15 +375,15 @@ class DefinitionTests: SourceKitLSPTestCase {
)
XCTAssertEqual(
afterChangingFileA,
.locations([Location(uri: aUri, range: Range(updatedAPositions["2️⃣"]))])
.locations([Location(uri: aUri, range: updatedAPositions["2️⃣"]..<updatedAPositions["3️⃣"])])
)

let afterChange = try await project.testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(bUri), position: bPositions["1️⃣"])
)
XCTAssertEqual(
afterChange,
.locations([Location(uri: aUri, range: Range(updatedAPositions["2️⃣"]))])
.locations([Location(uri: aUri, range: updatedAPositions["2️⃣"]..<updatedAPositions["3️⃣"])])
)
}

Expand All @@ -392,7 +392,7 @@ class DefinitionTests: SourceKitLSPTestCase {
let project = try await SwiftPMTestProject(
files: [
"LibA/LibA.swift": """
public func 1️⃣sayHello() {}
public func 1️⃣sayHello3️⃣() {}
""",
"LibB/LibB.swift": """
import LibA
Expand Down Expand Up @@ -447,7 +447,7 @@ class DefinitionTests: SourceKitLSPTestCase {
)
XCTAssertEqual(
afterBuilding,
.locations([try project.location(from: "1️⃣", to: "1️⃣", in: "LibA.swift")])
.locations([try project.location(from: "1️⃣", to: "3️⃣", in: "LibA.swift")])
)
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/SourceKitLSPTests/InlayHintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ final class InlayHintTests: SourceKitLSPTestCase {
func testInlayHintResolve() async throws {
try await runInlayHintTestCase(
initialText: """
struct 1️⃣MyType {}
struct 1️⃣MyType3️⃣ {}
let x2️⃣ = MyType()
""",
) { context in
Expand Down Expand Up @@ -411,7 +411,7 @@ final class InlayHintTests: SourceKitLSPTestCase {
}

XCTAssertEqual(location.uri, context.uri)
XCTAssertEqual(location.range, Range(context.positions["1️⃣"]))
XCTAssertEqual(location.range, context.positions["1️⃣"]..<context.positions["3️⃣"])
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/SourceKitLSPTests/SwiftPMIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,15 +497,15 @@ final class SwiftPMIntegrationTests: SourceKitLSPTestCase {
let topLocation = try XCTUnwrap(topResult?.locations?.only)
XCTAssertTrue(topLocation.uri.pseudoPath.hasSuffix("generated.swift"))
XCTAssertEqual(topLocation.range.lowerBound, Position(line: 0, utf16index: 4))
XCTAssertEqual(topLocation.range.upperBound, Position(line: 0, utf16index: 4))
XCTAssertEqual(topLocation.range.upperBound, Position(line: 0, utf16index: 16))

let targetResult = try await project.testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
let targetLocation = try XCTUnwrap(targetResult?.locations?.only)
XCTAssertTrue(targetLocation.uri.pseudoPath.hasSuffix("generated.swift"))
XCTAssertEqual(targetLocation.range.lowerBound, Position(line: 1, utf16index: 4))
XCTAssertEqual(targetLocation.range.upperBound, Position(line: 1, utf16index: 4))
XCTAssertEqual(targetLocation.range.upperBound, Position(line: 1, utf16index: 19))
}

// Make a change to the top level input file of the plugin command
Expand Down
16 changes: 8 additions & 8 deletions Tests/SourceKitLSPTests/TypeDefinitionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class TypeDefinitionTests: SourceKitLSPTestCase {

let positions = testClient.openDocument(
"""
struct 1️⃣MyType {}
struct 1️⃣MyType3️⃣ {}
let 2️⃣x = MyType()
""",
uri: uri
Expand All @@ -41,7 +41,7 @@ final class TypeDefinitionTests: SourceKitLSPTestCase {
}

XCTAssertEqual(location.uri, uri)
XCTAssertEqual(location.range, Range(positions["1️⃣"]))
XCTAssertEqual(location.range, positions["1️⃣"]..<positions["3️⃣"])
}

func testTypeDefinitionCrossModule() async throws {
Expand Down Expand Up @@ -93,7 +93,7 @@ final class TypeDefinitionTests: SourceKitLSPTestCase {

let positions = testClient.openDocument(
"""
struct 1️⃣Container<T> {
struct 1️⃣Container3️⃣<T> {
var value: T
}
let 2️⃣x = Container(value: 42)
Expand All @@ -114,7 +114,7 @@ final class TypeDefinitionTests: SourceKitLSPTestCase {
}

XCTAssertEqual(location.uri, uri)
XCTAssertEqual(location.range, Range(positions["1️⃣"]))
XCTAssertEqual(location.range, positions["1️⃣"]..<positions["3️⃣"])
}

func testTypeDefinitionOnTypeAnnotation() async throws {
Expand All @@ -123,7 +123,7 @@ final class TypeDefinitionTests: SourceKitLSPTestCase {

let positions = testClient.openDocument(
"""
struct 1️⃣MyType {}
struct 1️⃣MyType3️⃣ {}
let x: 2️⃣MyType = MyType()
""",
uri: uri
Expand All @@ -142,7 +142,7 @@ final class TypeDefinitionTests: SourceKitLSPTestCase {
}

XCTAssertEqual(location.uri, uri)
XCTAssertEqual(location.range, Range(positions["1️⃣"]))
XCTAssertEqual(location.range, positions["1️⃣"]..<positions["3️⃣"])
}

func testTypeDefinitionFunctionParameter() async throws {
Expand All @@ -151,7 +151,7 @@ final class TypeDefinitionTests: SourceKitLSPTestCase {

let positions = testClient.openDocument(
"""
struct 1️⃣MyType {}
struct 1️⃣MyType3️⃣ {}
func process(_ 2️⃣value: MyType) {}
""",
uri: uri
Expand All @@ -170,7 +170,7 @@ final class TypeDefinitionTests: SourceKitLSPTestCase {
}

XCTAssertEqual(location.uri, uri)
XCTAssertEqual(location.range, Range(positions["1️⃣"]))
XCTAssertEqual(location.range, positions["1️⃣"]..<positions["3️⃣"])
}

func testTypeDefinitionGeneratedInterface() async throws {
Expand Down
8 changes: 6 additions & 2 deletions Tests/SourceKitLSPTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ final class WorkspaceTests: SourceKitLSPTestCase {
""",
"Sources/lib/lib.swift": """
public struct Lib {
public func 5️⃣foo() {}
public func 5️⃣foo6️⃣() {}
public init() {}
}
""",
Expand All @@ -804,10 +804,14 @@ final class WorkspaceTests: SourceKitLSPTestCase {
let fooDefinitionResponse = try await project.testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(mainUri), position: mainPositions["3️⃣"])
)

XCTAssertEqual(
fooDefinitionResponse,
.locations([
Location(uri: try project.uri(for: "lib.swift"), range: try Range(project.position(of: "5️⃣", in: "lib.swift")))
Location(
uri: try project.uri(for: "lib.swift"),
range: try project.range(from: "5️⃣", to: "6️⃣", in: "lib.swift")
)
])
)

Expand Down