mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
[ty] Ask the LSP client to watch all project search paths
This change rejiggers how we register globs for file watching with the LSP client. Previously, we registered a few globs like `**/*.py`, `**/pyproject.toml` and more. There were two problems with this approach. Firstly, it only watches files within the project root. Search paths may be outside the project root. Such as virtualenv directory. Secondly, there is variation on how tools interact with virtual environments. In the case of uv, depending on its link mode, we might not get any file change notifications after running `uv add foo` or `uv remove foo`. To remedy this, we instead just list for file change notifications on all files for all search paths. This simplifies the globs we use, but does potentially increase the number of notifications we'll get. However, given the somewhat simplistic interface supported by the LSP protocol, I think this is unavoidable (unless we used our own file watcher, which has its own considerably downsides). Moreover, this is seemingly consistent with how `ty check --watch` works. This also required moving file watcher registration to *after* workspaces are initialized, or else we don't know what the right search paths are. This change is in service of #19883, which in order for cache invalidation to work right, the LSP client needs to send notifications whenever a dependency is added or removed. This change should make that possible. I tried this patch with #19883 in addition to my work to activate Salsa caching, and everything seems to work as I'd expect. That is, completions no longer show stale results after a dependency is added or removed.
This commit is contained in:
parent
0967e7e088
commit
5e943d3539
3 changed files with 128 additions and 76 deletions
|
@ -30,9 +30,10 @@ bitflags::bitflags! {
|
|||
const HIERARCHICAL_DOCUMENT_SYMBOL_SUPPORT = 1 << 10;
|
||||
const WORK_DONE_PROGRESS = 1 << 11;
|
||||
const FILE_WATCHER_SUPPORT = 1 << 12;
|
||||
const DIAGNOSTIC_DYNAMIC_REGISTRATION = 1 << 13;
|
||||
const WORKSPACE_CONFIGURATION = 1 << 14;
|
||||
const RENAME_DYNAMIC_REGISTRATION = 1 << 15;
|
||||
const RELATIVE_FILE_WATCHER_SUPPORT = 1 << 13;
|
||||
const DIAGNOSTIC_DYNAMIC_REGISTRATION = 1 << 14;
|
||||
const WORKSPACE_CONFIGURATION = 1 << 15;
|
||||
const RENAME_DYNAMIC_REGISTRATION = 1 << 16;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -107,6 +108,14 @@ impl ResolvedClientCapabilities {
|
|||
self.contains(Self::FILE_WATCHER_SUPPORT)
|
||||
}
|
||||
|
||||
/// Returns `true` if the client supports relative file watcher capabilities.
|
||||
///
|
||||
/// This permits specifying a "base uri" that a glob is interpreted
|
||||
/// relative to.
|
||||
pub(crate) const fn supports_relative_file_watcher(self) -> bool {
|
||||
self.contains(Self::RELATIVE_FILE_WATCHER_SUPPORT)
|
||||
}
|
||||
|
||||
/// Returns `true` if the client supports dynamic registration for diagnostic capabilities.
|
||||
pub(crate) const fn supports_diagnostic_dynamic_registration(self) -> bool {
|
||||
self.contains(Self::DIAGNOSTIC_DYNAMIC_REGISTRATION)
|
||||
|
@ -144,11 +153,15 @@ impl ResolvedClientCapabilities {
|
|||
flags |= Self::INLAY_HINT_REFRESH;
|
||||
}
|
||||
|
||||
if workspace
|
||||
.and_then(|workspace| workspace.did_change_watched_files?.dynamic_registration)
|
||||
.unwrap_or_default()
|
||||
if let Some(capabilities) =
|
||||
workspace.and_then(|workspace| workspace.did_change_watched_files.as_ref())
|
||||
{
|
||||
flags |= Self::FILE_WATCHER_SUPPORT;
|
||||
if capabilities.dynamic_registration == Some(true) {
|
||||
flags |= Self::FILE_WATCHER_SUPPORT;
|
||||
}
|
||||
if capabilities.relative_pattern_support == Some(true) {
|
||||
flags |= Self::RELATIVE_FILE_WATCHER_SUPPORT;
|
||||
}
|
||||
}
|
||||
|
||||
if text_document.is_some_and(|text_document| text_document.diagnostic.is_some()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue