swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67k stars 10.32k forks source link

Cursor Info of argument label should resolves to parameter declaration instead of function #67708

Open jansorg opened 11 months ago

jansorg commented 11 months ago

When textDocument/definition is invoked on the argument name of a function invocation, then the resolved definition points to the function name. If I'm not mistaken, resolving to the parameter name identifier inside the function declaration would be better. I've tested this with the current VSCode Swift plugin and latest sourcekit-lsp and also with my own wip LSP client implementation for JetBrains IDEs.

The reverse search on name of a function declaration (via textDocument/references) doesn't include the usages located inside myFunction, as far as I can tell. I‘m not certain about this.

public class MyClass {
  public init(_ first: String = "default", name: String? = nil) {
  }

  public func myFunction(_ first: String = "default", name: String? = nil) {
  }
}

func myFunction() {
    // definition of "name" points to init name identifier "init" on line 2, but should point to "name" on line 5
    let myValue = MyClass(name: "my value") 

    // definition of "name" points to function name identifier "myFunction on line 5", but should point to "name" on line 5
    myValue.myFunction(name: "value") 
}

I verified this with the current 5.9 snapshot:

# swift --version
Swift version 5.9-dev (LLVM 8250a14f5d3ef6a, Swift 73c7ebb20f64b96)
Target: x86_64-unknown-linux-gnu

I hope that this is the right place to report this. I wasn't sure if the cause would be inside sourcekit-lsp or one of its dependencies.

ahoppen commented 11 months ago

Tracked in Apple’s issue tracker as rdar://113327487

ahoppen commented 11 months ago

The textDocument/definition request is backed by the cursorInfo request in sourcekitd, so I’ll move the issue to the compiler repo, where sourcekitd lives. It’s still fine for you to report it here.

Attaching reduced cursor info test case

func foo(arg: Int) {}

// RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):5 %s -- %s
foo(arg: 1)
skrtks commented 10 months ago

@ahoppen I can write a fix for this if no one is working on it yet

ahoppen commented 10 months ago

I don’t think anyone is working on it at the moment.

bnbarham commented 10 months ago

As discussed in https://github.com/apple/swift/pull/68341, I'm going to mark this as NTBF - the argument label is really part of the call and I would expect that to give back the function definition itself for cursor info and search for references to that function for find references.