lean4-htt/tests/lean/interactive/goTo.lean
Marc Huisinga 2ede81fe10
fix: search path related bugs (#7873)
This PR fixes a number of bugs related to the handling of the source
search path in the language server, where deleting files could cause
several features to stop functioning and both untitled files and files
that don't exist on disc could have conflicting module names.

In detail, it makes the following adjustments:
- The URI <-> module name conversion was adjusted to produce no name
collisions.
- File URIs in the search path yield a module name relative to the
search path, as before.
- File URIs not in the search path, non-file URIs and non-`.lean` files
yield a `«external:<full uri>»` module name.
- To avoid the issue of the URI -> module name conversion failing when a
file is deleted from disc, we now cache the result of this conversion in
the watchdog and the file worker when the file is first opened.
- All of the URI <-> module name conversions now consistently go through
`Server.documentUriFromModule?` and `moduleFromDocumentUri` to ensure
that we don't have minor deviations for this conversion all over the
place.
- The threading of the source search path through the file worker (from
`lake setup-file`) is removed. It turns out that `lake serve` already
sets the correct source search path in the environment, so we can just
always use the search path from the environment.
- Since we can now answer more requests that need the .ileans in
untitled files, a lot of the tests that test 'Go to definition' needed
to be adjusted so that they use the information from the watchdog, not
the file worker. As we load references asynchronously, this PR adds an
internal `$/lean/waitForILeans` request that tests can use to wait for
all .ilean files to be loaded and for the ilean references from the file
worker for the current document version to be finalized.
- As part of this PR, we noticed that the .ileans aren't available in
the NixOS setup, so @Kha adjusted the Nix CI to fix this.

### Breaking changes
- `Server.documentUriFromModule` has been renamed to
`Server.documentUriFromModule?` and doesn't take a `SearchPath` argument
anymore, as the `SearchPath` is now computed from the `LEAN_SRC_PATH`
environment variable. It has also been moved from `Lean.Server.GoTo` to
`Lean.Server.Utils`.
- `Server.moduleFromDocumentUri` does not take a `SearchPath` argument
anymore and won't return an `Option` anymore. It has also been moved
from `Lean.Server.GoTo` to `Lean.Server.Utils`.
- The `System.SearchPath.searchModuleNameOfUri` function has been
removed. It is recommended to use `Server.moduleFromDocumentUri`
instead.
- The `initSrcSearchPath` function has been renamed to
`getSrcSearchPath` and has been moved from `Lean.Util.Paths` to
`Lean.Util.Path`. It also doesn't need to take a `pkgSearchPath`
argument anymore.

---------

Co-authored-by: Sebastian Ullrich <sebasti@nullri.ch>
2025-04-09 15:37:49 +00:00

114 lines
2.3 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Lean.Elab
--^ waitForILeans
structure Bar where
structure Foo where
foo₁ : Nat
foo₂ : Nat
bar : Bar
def mkFoo₁ : Foo := {
--v textDocument/definition
foo₁ := 1
--^ textDocument/definition
--v textDocument/declaration
foo₂ := 2
--v textDocument/typeDefinition
bar := ⟨⟩
}
--v textDocument/definition
#check (Bar)
structure HandWrittenStruct where
n : Nat
-- def HandWrittenStruct.n := fun | mk n => n
--v textDocument/definition
def hws : HandWrittenStruct := {
--v textDocument/definition
n := 3
}
--v textDocument/declaration
def mkFoo₂ := mkFoo₁
syntax (name := elabTest) "test" : term
@[term_elab elabTest] def elabElabTest : Lean.Elab.Term.TermElab := fun orig _ => do
let stx ← `(2)
Lean.Elab.withMacroExpansionInfo orig stx $ Lean.Elab.Term.elabTerm stx none
--v textDocument/declaration
#check test
--^ textDocument/definition
def Baz (α : Type) := α
#check fun (b : Baz Nat) => b
--^ textDocument/typeDefinition
example : Nat :=
let a := 1
--v textDocument/definition
a + b
--^ textDocument/definition
where
b := 2
macro_rules | `(test) => `(3)
#check test
--^ textDocument/definition
class Foo2 where
foo : Nat → Nat
foo' : Nat
class Foo3 [Foo2] where
foo : [Foo2] → Nat
class inductive Foo4 : Nat → Type where
| mk : Nat → Foo4 0
def Foo4.foo : [Foo4 n] → Nat
| .mk n => n
class Foo5 where
foo : Foo2
instance : Foo2 := .mk id 0
instance : Foo3 := .mk 0
instance : Foo4 0 := .mk 0
instance [foo2 : Foo2] : Foo5 := .mk foo2
-- should go-to instance
--v textDocument/definition
#check Foo2.foo 2
--^ textDocument/definition
#check (Foo2.foo)
--^ textDocument/definition
#check (Foo2.foo')
--^ textDocument/definition
-- should go-to projection
#check @Foo2.foo
--^ textDocument/definition
-- test that the correct instance index is extracted
#check (Foo3.foo)
--^ textDocument/definition
-- non-projections should not go-to instance
#check (Foo4.foo)
--^ textDocument/definition
set_option pp.all true in
-- test that multiple instances can be extracted
#check (Foo5.foo)
--^ textDocument/definition
-- duplicate definitions link to the original
def mkFoo₁ := 1
--^ textDocument/definition