diff --git a/Cargo.toml b/Cargo.toml index 1a27b75..383cd87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,14 +38,14 @@ serde_json = "1.0" # Pure Rust, so the analysis library still builds for wasm32. The same # release line `comline-codegen`'s `toml` 0.8 parses with. toml_edit = { version = "0.22", default-features = false, features = ["parse"] } -comline-core = { git = "https://github.com/ComlineProject/core", rev = "14ce54658455206d639ddc4fbe6d609d20292587" } +comline-core = { git = "https://github.com/ComlineProject/core", rev = "69039e7ab05953760bb7a6818f61e98c14b43922" } # What each generator supports, feature by feature (`comline_codegen::features`) # - for warning at a use a declared language can't generate. Only the # generators' `SUPPORT` tables are read; nothing is generated. Pinned to the # same commits as the CLI, so a build has one `comline-codegen`. -comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "e22109e1a6ca3c8e5a63338e2fe9acda44123ee1" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "dd1c29e6560a0ffd7893c59715b31d43b88c585f" } -comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "d377fbf07cf6c0f004e9d5dd4fbd6fa15ea9d0bc" } +comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "e900919375429591d73c906380f1d354d636ab22" } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "768afc0bbe8fd193aa56af8bd4a8798046817d88" } +comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "a384b62b22073a209c494553ab28febe9f8166b4" } # `server`-only. tower-lsp = { version = "0.20.0", optional = true } diff --git a/README.md b/README.md index e80bd94..2711b60 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A Language Server Protocol (LSP) implementation for [Comline](https://github.com ### ✅ Fully Implemented -- **Diagnostics** - Real-time syntax errors plus `comline-core`'s validation pass (undefined types, duplicate declarations, ...) — the same checks `comline build` runs. A type from another file of the package used without a `use` gets a specific error with an **"Add `use ...`" quick fix**; a `use` that reaches one of the package's own schemas by a bare path (`use types::User` instead of `use package::types::User`) is an error with a **"Write `package::…`" quick fix** +- **Diagnostics** - Real-time syntax errors plus `comline-core`'s validation pass (undefined types, duplicate declarations, ...) — the same checks `comline build` runs. A type from another file of the package used without a `use` gets a specific error with an **"Add `use ...`" quick fix**; a `use` that reaches one of the package's own schemas by a bare path (`use types::User` instead of `use package::types::User`) is an error with a **"Write `package::…`" quick fix**; a type written against the Rust model (`reference::not-in-scope`, `reference::qualifier-not-in-scope`, `reference::defined-twice`) is core's own error with its **quick fix** - **Whole-package analysis** - every `.ids` file in a package's `src/` takes part, open or not, kept current through file watching; each package only sees its own files - **Dependency packages** - the dependencies `config.idp` declares are indexed (path dependencies, and git pins `comline check` has fetched; the editor never fetches), so imports, hover, go-to-definition and completion work into them under the dependency's name. Unresolved imports are errors, worded like `comline check`'s, with a "did you mean" quick fix; imports of a dependency that isn't fetched yet are marked as not checked, and its `config.idp` entry says why. A dependency whose `@unverified` protocols aren't accepted is an error on its entry, with a quick fix that writes the `accept_unverified` line `comline deps accept` would - **Standard library** - `use std::…` resolves against the std embedded in `comline-core`, like a read-only dependency: hover, go-to-definition (into virtual `comline-std:` documents, whose text clients fetch with the `comline/stdSource` request), completion, and unresolved-import errors worded like `comline check`'s (`std has no schema matching 'std::htp::Request' - did you mean 'std::http'?`) @@ -261,6 +261,30 @@ package's own schemas, and a dependency's or `std`'s path as it is. A dependency that isn't fetched yet is offered, marked as such, with nothing under it until `comline check` fetches it. +A type is written as in Rust: a name in scope, or a path that starts with one or +with a root. A whole-namespace `use` brings in only its own name, so after +`use package::types` the type is `types::User` and a bare `User` is the error +`reference::not-in-scope` (fix: "Add `use package::types::User`", or write +`types::User`). A full path from a root needs no `use`: `package::types::User`, +`parent::common::Error`, `self::nested::Type`, `std::http::Request`, a +dependency's `greeting::hello::Greeting`; and a path goes down from a name in +scope (`billing::api::Invoice` after `use package::billing`, `b::Invoice` after +`use package::billing::api as b`). These are core's own rule and errors +(`reference::not-in-scope`, `reference::qualifier-not-in-scope`, +`reference::defined-twice`, each with its fix), run over the files the server +has. Completion in a type position follows it: + +```comline +use package::types + +struct S { + a: types:: // → what `types` declares; a bare `User` is not offered + b: package:: // → the package's schemas, then what's under them + c: // → the names in scope (declared here, `use`d, `types`), then + // the roots package:: parent:: self:: std:: and the dependencies +} +``` + ### Go to Definition Ctrl/Cmd + Click on any type reference to jump to its definition: diff --git a/src/analysis/diagnostics.rs b/src/analysis/diagnostics.rs index 947daad..884a82c 100644 --- a/src/analysis/diagnostics.rs +++ b/src/analysis/diagnostics.rs @@ -5,8 +5,8 @@ use std::collections::{BTreeSet, HashMap}; use crate::analysis::generation; use crate::analysis::import_check::{self, MissingImport, UnresolvedImport, UnverifiedImport}; +use crate::analysis::names::Names; use crate::analysis::project::Project; -use crate::analysis::symbols::Namespace; use crate::util::{byte_range_to_lsp_range, comline_diagnostic}; use comline_core::diagnostics::{Fix, Severity}; use comline_core::edition::{self, Edition}; @@ -247,6 +247,8 @@ pub fn validation_diagnostics(source: &str, document: &Document) -> Vec { project: &'p Project<'a>, + /// What a name written in this file names, by core's reference rule. + names: &'p Names<'p, 'a>, doc: usize, /// Names [`import_check`] gave a stand-in import because their `use` is /// missing - reported as such, never judged against a validator. @@ -254,23 +256,23 @@ struct ProjectImports<'p, 'a> { } impl ProjectImports<'_, '_> { + /// The declaration `name`, as this file's units hold it (a qualified type + /// by its path from the package's root), refers to: its file and its own + /// name there. In scope only, as `comline build` resolves: the + /// project-wide fallback would answer for a validator this file never + /// imported. fn declaration(&self, name: &str) -> Option<(usize, String)> { - declaration_in(self.project, self.doc, name) + self.names.declaration(self.doc, name) } } -/// The declaration `name`, written in `project.docs[doc]`, refers to: its file -/// and its own name there. -fn declaration_in(project: &Project, doc: usize, name: &str) -> Option<(usize, String)> { - let bare = name.rsplit("::").next()?; - // In scope only, as `comline build` resolves: the project-wide - // fallback would answer for a validator this file never imported. - let target = project.resolve(doc, bare, Namespace::Declaration)?; - let real_name = project.symbol(&target)?.name.clone(); - Some((target.doc, real_name)) -} - impl validation::ImportLookup for ProjectImports<'_, '_> { + /// A qualified type is a path from the package's root (core#290), which + /// needs no `use` of the schema it reaches. + fn reaches(&self, name: &str) -> bool { + self.names.reaches(name) + } + /// Declared by the package's `config.idp` or a dependency's, as far as /// the view has them: core's own `encodings::is_declared`, read from /// the manifests' text. @@ -313,7 +315,7 @@ impl validation::ImportLookup for ProjectImports<'_, '_> { let (doc, real_name) = self.declaration(name)?; Some( DeclaredData { - project: self.project, + names: self.names, answers: HashMap::new(), } .named(doc, &real_name), @@ -329,7 +331,7 @@ impl validation::ImportLookup for ProjectImports<'_, '_> { /// that refers to itself, in one file or across several, ends the walk as /// carrying data, as core's does. struct DeclaredData<'p, 'a> { - project: &'p Project<'a>, + names: &'p Names<'p, 'a>, answers: HashMap<(usize, String), Option>, } @@ -345,7 +347,7 @@ impl DeclaredData<'_, '_> { None => {} } self.answers.insert(key.clone(), None); - let project = self.project; + let project = self.names.project(); let answer = project.docs[doc] .document .0 @@ -379,7 +381,7 @@ impl DeclaredData<'_, '_> { length: Some(0), .. } => false, TypeRef::Array { element, .. } => self.carries(doc, element), - TypeRef::Named(name) => match declaration_in(self.project, doc, name) { + TypeRef::Named(name) => match self.names.declaration(doc, name) { Some((doc, real_name)) => self.named(doc, &real_name), None => true, }, @@ -603,17 +605,30 @@ pub fn all_diagnostics( /// missing-import error - in place of core's bare "Unknown type" - where a /// type declared in another file of the package is used without a `use`. pub fn project_diagnostics(project: &Project, doc: usize) -> Vec { + project_diagnostics_with(project, &Names::of(project), doc) +} + +/// [`project_diagnostics`], with the project's [`Names`] already built: what +/// asking for every file of a package shares. +pub fn project_diagnostics_with<'a>( + project: &Project<'a>, + names: &Names<'_, 'a>, + doc: usize, +) -> Vec { let here = &project.docs[doc]; - let check = import_check::check(project, doc); - let stand_ins = check.missing.iter().map(|m| m.name.clone()).collect(); + let check = import_check::check_with(project, names, doc); - let units = frozen_units(&here.document, check.scope, &here.namespace); + // A qualified type is frozen as its path from the package's root, as + // core's compiler does (core#290), which is what validation reads. + let mut units = frozen_units(&here.document, check.scope, &here.namespace); + names.freeze(doc, &mut units); let imports = ProjectImports { project, + names, doc, - stand_ins, + stand_ins: check.stand_ins, }; - let judged = judged_units(project, doc, &units); + let judged = judged_units(project, names, doc, &units); let mut diagnostics = validation_diagnostics_with( here.source, &here.document, @@ -647,11 +662,17 @@ pub fn project_diagnostics(project: &Project, doc: usize) -> Vec { check .missing .iter() + // A type is core's `reference::not-in-scope` (below); a validator + // in a `@validators` list isn't a type, which core doesn't check. + .filter(|m| m.validator) .map(|m| missing_import_diagnostic(here.source, m)), ); // Core's own error for a bare path to one of the package's schemas, with // its fix (`package::` written in front), carried to the quick fix. diagnostics.extend(check.bare_own.iter().map(|d| from_core(here.source, d))); + // Core's own errors for a name nothing brought into scope, a qualifier + // that isn't, and a name defined twice, with their fixes (core#290). + diagnostics.extend(check.references.iter().map(|d| from_core(here.source, d))); diagnostics.extend( check .unresolved @@ -696,7 +717,12 @@ struct Judged { /// With no manifest to say what the package's wire or settings are (none in /// view, or one that can't be read, or whose `settings` didn't resolve), the /// file is judged as it was before: by its own units alone. -fn judged_units(project: &Project, doc: usize, units: &[FrozenUnit]) -> Judged { +fn judged_units<'a>( + project: &Project<'a>, + names: &Names<'_, 'a>, + doc: usize, + units: &[FrozenUnit], +) -> Judged { use comline_core::package::config::ir::frozen as config; use comline_core::schema::ir::compiler::bounds; use comline_core::schema::ir::unverified; @@ -725,7 +751,7 @@ fn judged_units(project: &Project, doc: usize, units: &[FrozenUnit]) -> Judged { wire.freeze_framing(&mut judged); bounds::apply_default_bounds(&mut judged, &project.settings); - let sizes = ImportedBounds::new(project); + let sizes = ImportedBounds::new(project, names); let unbounded = bounds::check_bounded(&mut judged, &|name| sizes.imported(doc, name)); Judged { units: judged, @@ -769,15 +795,17 @@ fn package_wire( /// its own `use`s bring in. struct ImportedBounds<'p, 'a> { project: &'p Project<'a>, + names: &'p Names<'p, 'a>, /// Per file: `None` while it's being worked out, which is what a cycle of /// imports reads, and which answers yes: like core, it fails closed. unbounded: std::cell::RefCell>>>, } impl<'p, 'a> ImportedBounds<'p, 'a> { - fn new(project: &'p Project<'a>) -> Self { + fn new(project: &'p Project<'a>, names: &'p Names<'p, 'a>) -> Self { Self { project, + names, unbounded: Default::default(), } } @@ -785,7 +813,7 @@ impl<'p, 'a> ImportedBounds<'p, 'a> { /// Whether `name`, as written in `docs[doc]`, is another file's type that /// holds a value without a maximum. fn imported(&self, doc: usize, name: &str) -> bool { - let Some((declared_in, real_name)) = declaration_in(self.project, doc, name) else { + let Some((declared_in, real_name)) = self.names.declaration(doc, name) else { return false; }; if declared_in == doc { @@ -808,6 +836,7 @@ impl<'p, 'a> ImportedBounds<'p, 'a> { let here = &self.project.docs[doc]; let mut units = frozen_units(&here.document, vec![], &here.namespace); + self.names.freeze(doc, &mut units); let settings = match here.dependency { Some(name) => self.project.dependency_settings.get(name), None => Some(&self.project.settings), @@ -890,11 +919,19 @@ fn missing_import_diagnostic(source: &str, missing: &MissingImport) -> Diagnosti "`{name}` is imported here as `{alias}` — write `{alias}`, or add `use {}`", first.use_path(name) ), - (None, [only]) => format!( - "`{name}` is declared in `{}` but not imported here — add `use {}`", - only.file, - only.use_path(name) - ), + (None, [only]) => { + let mut message = format!( + "`{name}` is declared in `{}` but not imported here — add `use {}`", + only.file, + only.use_path(name) + ); + // A `use` here may already bring the schema in as a name to + // qualify with, which is the other way to reach it. + if let Some(written) = only.qualified(name) { + message.push_str(&format!(", or write `{written}`")); + } + message + } (_, many) => format!( "`{name}` is declared in {} but not imported here — add a `use` for one of them", many.iter() @@ -1704,7 +1741,7 @@ struct User { assert!( messages .iter() - .any(|m| m.1 == Some(NumberOrString::String(MISSING_IMPORT.to_string()))), + .any(|m| m.1 == Some(NumberOrString::String("reference::not-in-scope".to_string()))), "{messages:?}" ); @@ -1832,7 +1869,6 @@ struct User { for source in [ "use package::types::*\n\nstruct S {\n m: Message\n}\n", "use package::types::{Message, Other}\n\nstruct S {\n m: Message\n}\n", - "use package::types\n\nstruct S {\n m: Message\n}\n", "use package::types\n\nstruct S {\n m: types::Message\n}\n", ] { assert_eq!(messages(source), Vec::::new(), "{source}"); @@ -1869,7 +1905,7 @@ struct User { } #[test] - fn a_missing_import_replaces_the_unknown_type_error() { + fn a_type_another_file_declares_is_core_s_not_in_scope_not_an_unknown_type() { let found = project_messages(&[ ( "file:///pkg/src/chat.ids", @@ -1880,8 +1916,8 @@ struct User { assert_eq!( found, vec![( - "`Message` is declared in `types.ids` but not imported here — add `use package::types::Message`".to_string(), - Some(NumberOrString::String(MISSING_IMPORT.to_string())), + "'Message' is not in scope — 'Message' is declared in `package::types`: add `use package::types::Message`, or write `package::types::Message`".to_string(), + Some(NumberOrString::String("reference::not-in-scope".to_string())), 1, 7 )] @@ -1893,18 +1929,19 @@ struct User { let chat = "use package::types::*\n\nstruct S {\n m: Message\n n: Nope\n}\n"; let found = project_messages(&[("file:///pkg/src/chat.ids", chat), TYPES]); assert_eq!(found.len(), 1, "{found:?}"); - assert!(found[0].0.starts_with("Unknown type 'Nope'"), "{found:?}"); + assert!(found[0].0.starts_with("'Nope' is not in scope"), "{found:?}"); } #[test] - fn using_the_real_name_of_an_aliased_import_says_so() { + fn using_the_real_name_of_an_aliased_import_is_not_in_scope() { let chat = "use package::types::Message as Msg\n\nstruct S {\n m: Message\n}\n"; let found = project_messages(&[("file:///pkg/src/chat.ids", chat), TYPES]); assert_eq!(found.len(), 1, "{found:?}"); assert_eq!( - found[0].0, - "`Message` is imported here as `Msg` — write `Msg`, or add `use package::types::Message`" + found[0].1, + Some(NumberOrString::String("reference::not-in-scope".to_string())) ); + assert!(found[0].0.starts_with("'Message' is not in scope"), "{found:?}"); } const MANIFEST_URI: &str = "file:///pkg/config.idp"; @@ -2953,3 +2990,222 @@ mod bare_own_path_tests { assert!(!found.iter().any(is_bare), "{found:#?}"); } } + +/// Qualified references follow the Rust model (ComlineProject/language-server#124, +/// core#290): a name in scope is a declaration, what a `use` brings in or a +/// root; a whole-namespace `use` brings in only its own name; a full path from +/// a root needs no `use`. What the rule refuses is core's own error, with core's +/// fix, which `fix_actions` offers as a quick fix. +#[cfg(test)] +mod reference_tests { + use super::*; + use crate::analysis::source::SourceFile; + use lsp_types::NumberOrString; + + const MANIFEST: &str = "congregation pkg\nspecification_version = 1\n\n\ + dependencies = {\n shared = {\n path = \"../shared\"\n }\n}\n"; + + fn local(path: &str, text: &str) -> SourceFile { + SourceFile::local( + Url::parse(&format!("file:///pkg/{path}")).unwrap(), + text.to_string(), + ) + } + + /// The package: manifest, `types` (`User`, `Kind`), the directory + /// `billing` (`api` with `Invoice`), a `shared` dependency, and the file + /// under test, `src/chat.ids`, first after the manifest. + fn package(chat: &str) -> Vec { + vec![ + local("config.idp", MANIFEST), + local("src/chat.ids", chat), + local( + "src/types.ids", + "struct User {\n id: u64\n}\n\nenum Kind {\n A\n}\n", + ), + local( + "src/billing/api.ids", + "struct Invoice {\n id: u64\n}\n", + ), + SourceFile::of_dependency( + Url::parse("file:///shared/src/models.ids").unwrap(), + "struct Thing {\n id: u64\n}\n".to_string(), + "shared", + vec!["shared".to_string(), "models".to_string()], + ), + ] + } + + fn of_chat(chat: &str) -> Vec { + let files = package(chat); + let project = Project::new(&files); + let doc = project.index_of(&files[1].uri).expect("chat.ids parses"); + project_diagnostics(&project, doc) + } + + fn codes(found: &[Diagnostic]) -> Vec { + found + .iter() + .filter_map(|d| match &d.code { + Some(NumberOrString::String(code)) => Some(code.clone()), + _ => None, + }) + .collect() + } + + /// `source` with the diagnostic's quick fix applied. + fn fixed(source: &str, d: &Diagnostic) -> String { + let fix = &d.data.as_ref().expect("a fix")[FIX_DATA]; + let mut edits: Vec = serde_json::from_value(fix["edits"].clone()).unwrap(); + edits.sort_by_key(|e| std::cmp::Reverse((e.range.start.line, e.range.start.character))); + edits.iter().fold(source.to_string(), |text, edit| { + crate::test_support::apply_edit(&text, edit) + }) + } + + fn title(d: &Diagnostic) -> String { + d.data.as_ref().expect("a fix")[FIX_DATA]["title"] + .as_str() + .unwrap() + .to_string() + } + + #[test] + fn the_codes_are_the_ones_core_exports() { + use comline_core::schema::ir::compiler::references::{ + DEFINED_TWICE, NOT_IN_SCOPE, QUALIFIER_NOT_IN_SCOPE, + }; + assert_eq!(NOT_IN_SCOPE.id, "reference::not-in-scope"); + assert_eq!(QUALIFIER_NOT_IN_SCOPE.id, "reference::qualifier-not-in-scope"); + assert_eq!(DEFINED_TWICE.id, "reference::defined-twice"); + } + + #[test] + fn a_full_path_from_a_root_needs_no_use() { + for chat in [ + "struct S {\n u: package::types::User\n}\n", + "struct S {\n i: package::billing::api::Invoice\n}\n", + "struct S {\n t: shared::models::Thing\n}\n", + ] { + let found = of_chat(chat); + assert!(found.is_empty(), "{chat}: {found:#?}"); + } + } + + #[test] + fn a_full_path_into_std_needs_no_use() { + let mut files = package("struct S {\n r: std::http::Request\n}\n"); + files.extend(crate::analysis::stdlib::files( + &crate::analysis::stdlib::root(), + )); + let project = Project::new(&files); + let found = project_diagnostics(&project, project.index_of(&files[1].uri).unwrap()); + assert!(found.is_empty(), "{found:#?}"); + } + + #[test] + fn a_path_goes_down_from_a_name_in_scope_and_through_an_alias() { + for chat in [ + "use package::types\n\nstruct S {\n u: types::User\n}\n", + "use package::billing\n\nstruct S {\n i: billing::api::Invoice\n}\n", + "use package::billing::api\n\nstruct S {\n i: api::Invoice\n}\n", + "use package::billing::api as b\n\nstruct S {\n i: b::Invoice\n}\n", + "use package::types::User\n\nstruct S {\n u: User\n}\n", + "use package::types::*\n\nstruct S {\n u: User\n k: Kind\n}\n", + "use shared\n\nstruct S {\n t: shared::models::Thing\n}\n", + ] { + let found = of_chat(chat); + assert!(found.is_empty(), "{chat}: {found:#?}"); + } + } + + #[test] + fn a_bare_name_after_only_a_whole_namespace_use_is_not_in_scope() { + let chat = "use package::types\n\nstruct S {\n u: User\n}\n"; + let found = of_chat(chat); + assert_eq!(codes(&found), ["reference::not-in-scope"], "{found:#?}"); + assert_eq!( + found[0].message, + "'User' is not in scope — 'User' is declared in `package::types`: add \ + `use package::types::User`, or write `types::User`" + ); + assert_eq!(title(&found[0]), "Add `use package::types::User`"); + } + + #[test] + fn a_qualified_name_after_an_item_use_is_refused_and_the_fix_writes_the_bare_name() { + let chat = "use package::types::User\n\nstruct S {\n u: types::User\n}\n"; + let found = of_chat(chat); + assert_eq!(codes(&found), ["reference::qualifier-not-in-scope"], "{found:#?}"); + assert_eq!(found[0].severity, Some(DiagnosticSeverity::ERROR)); + assert!(found[0].message.starts_with("'types' is not in scope, in `types::User`")); + assert_eq!(title(&found[0]), "Write `User`"); + let repaired = fixed(chat, &found[0]); + assert_eq!( + repaired, + "use package::types::User\n\nstruct S {\n u: User\n}\n" + ); + assert!(of_chat(&repaired).is_empty()); + } + + #[test] + fn after_an_alias_the_namespace_s_own_name_is_refused() { + let chat = "use package::types as t\n\nstruct S {\n u: types::User\n v: t::User\n}\n"; + let found = of_chat(chat); + assert_eq!(codes(&found), ["reference::qualifier-not-in-scope"], "{found:#?}"); + assert_eq!(title(&found[0]), "Write `t::User`"); + let repaired = fixed(chat, &found[0]); + assert!(of_chat(&repaired).is_empty(), "{repaired}"); + } + + #[test] + fn a_qualifier_that_matches_nothing_is_refused_without_a_fix() { + let found = of_chat("struct S {\n u: nothing::User\n}\n"); + assert_eq!(codes(&found), ["reference::qualifier-not-in-scope"], "{found:#?}"); + assert!(found[0].data.is_none(), "nothing declares it: {found:#?}"); + } + + #[test] + fn a_name_declared_nowhere_is_not_in_scope_and_not_also_an_unknown_type() { + let found = of_chat("struct S {\n u: Nope\n}\n"); + assert_eq!(codes(&found), ["reference::not-in-scope"], "{found:#?}"); + assert!(found[0].message.starts_with("'Nope' is not in scope"), "{found:#?}"); + assert!(found[0].data.is_none(), "no fix where nothing declares it"); + } + + #[test] + fn a_namespace_is_no_type() { + let found = of_chat("use package::types\n\nstruct S {\n u: types\n}\n"); + assert_eq!(codes(&found), ["reference::not-in-scope"], "{found:#?}"); + assert!( + found[0].message.starts_with("'types' is a namespace, not a type"), + "{found:#?}" + ); + } + + #[test] + fn a_name_defined_twice_is_refused_and_the_fix_imports_it_under_an_alias() { + let chat = "use package::types::User\n\nstruct User {\n id: u64\n}\n"; + let found = of_chat(chat); + assert_eq!(codes(&found), ["reference::defined-twice"], "{found:#?}"); + assert_eq!(title(&found[0]), "Import `User` as `TypesUser`"); + let repaired = fixed(chat, &found[0]); + assert_eq!( + repaired, + "use package::types::User as TypesUser\n\nstruct User {\n id: u64\n}\n" + ); + assert!(of_chat(&repaired).is_empty(), "{repaired}"); + + // Two `use`s that bring in the same name. + let found = of_chat("use package::types::User\nuse shared::models::Thing as User\n"); + assert_eq!(codes(&found), ["reference::defined-twice"], "{found:#?}"); + } + + #[test] + fn a_full_path_is_judged_like_a_written_name() { + // A path into a schema that doesn't declare the name is unknown, as + // any other name nothing declares. + let found = of_chat("struct S {\n u: package::types::Nope\n}\n"); + assert_eq!(codes(&found).len(), 1, "{found:#?}"); + } +} diff --git a/src/analysis/import_check.rs b/src/analysis/import_check.rs index 67bdcf2..b24b075 100644 --- a/src/analysis/import_check.rs +++ b/src/analysis/import_check.rs @@ -25,15 +25,19 @@ use comline_core::schema::idl::grammar::Declaration; use comline_core::schema::ir::compiler::import_resolver::{ absolute_first_segment, check_imports, ImportResolver, BARE_OWN_SCHEMA_PATH, }; +use comline_core::schema::ir::compiler::references::{ + Refusal, NOT_IN_SCOPE, QUALIFIER_NOT_IN_SCOPE, +}; use comline_core::schema::ir::frozen::unit::FrozenUnit; use lsp_types::SymbolKind; use crate::analysis::imports::ResolvedUse; +use crate::analysis::names::{self, Names}; use crate::analysis::project::{named_type_sites, validator_sites, Project, ProjectDoc}; use crate::analysis::source::DependencyKind; use crate::analysis::stdlib; use crate::analysis::symbols::Namespace; -use crate::util::{clamped, closest, follows_as, word_occurrences}; +use crate::util::{clamped, closest, follows_as, is_identifier, word_occurrences}; /// What [`check`] found for one file. pub struct ImportCheck { @@ -52,6 +56,16 @@ pub struct ImportCheck { /// (`use types::User`, for `use types::User`): core's own error /// for it, with its fix - see [`bare_own_schema_paths`]. pub bare_own: Vec, + /// What core's reference rule refuses in the file (core#290): a name + /// nothing brought into scope, a qualifier that isn't in scope, a name + /// defined twice - its own errors, with their fixes (see + /// [`Names::reference_errors`]). A bare type another file declares is + /// core's `reference::not-in-scope`, as `comline check` reports it. + pub references: Vec, + /// The names core's validation must not also call unknown: those of + /// [`ImportCheck::missing`] and of `references`, where the error is + /// already the editor's to report. + pub stand_ins: BTreeSet, } /// A `use` that doesn't resolve, the way core's `check_imports` sees it. @@ -85,6 +99,11 @@ pub struct MissingImport { /// binds only the alias (`use types::User as U` makes `U` usable, not /// `User`). pub imported_as: Option, + /// A validator named in a `@validators` list, which core's reference rule + /// doesn't cover: the only name `import::missing` is still reported for. + /// A type is core's `reference::not-in-scope`; what is here for it is + /// what its quick fixes are made of (every file that declares it). + pub validator: bool, } /// A project file a missing name could be imported from. @@ -95,6 +114,11 @@ pub struct Candidate { pub own: bool, /// The file's name, for messages (`types.ids`). pub file: String, + /// How the file is written through a namespace a `use` here brought in + /// (`types` after `use package::types`, `b::sub` after + /// `use package::billing as b`), when one reaches it: the other fix to a + /// missing `use`. + pub via: Option, } impl Candidate { @@ -104,6 +128,12 @@ impl Candidate { pub fn use_path(&self, name: &str) -> String { use_path(self.own, &self.namespace, name) } + + /// `name` written through a namespace a `use` here brought in + /// (`types::User`), if one reaches the file. + pub fn qualified(&self, name: &str) -> Option { + self.via.as_ref().map(|via| format!("{via}::{name}")) + } } /// Where one `use` points, as far as the project's files can tell. @@ -118,6 +148,11 @@ pub(crate) enum Target { } pub fn check(project: &Project, doc: usize) -> ImportCheck { + check_with(project, &Names::of(project), doc) +} + +/// [`check`], with the project's [`Names`] already built. +pub fn check_with(project: &Project, names: &Names, doc: usize) -> ImportCheck { let here = &project.docs[doc]; let sites = named_type_sites(here); let bare_sites: BTreeSet<&str> = sites @@ -197,25 +232,36 @@ pub fn check(project: &Project, doc: usize) -> ImportCheck { } // Validators too: a `use` brings one in the same way, and core calls a - // missing one an unknown validator. + // missing one an unknown validator. A type is in scope by the Rust model + // (core#290), which is core's own rule, [`Names::scope`]; the older, wider + // reading of a `use` stays for what core still reads that way. let validators = validator_sites(here); let named = sites .iter() - .map(|site| (site, declares_type as fn(&ProjectDoc, &str) -> bool)) - .chain( - validators - .iter() - .map(|site| (site, declares_validator as fn(&ProjectDoc, &str) -> bool)), - ); + .map(|site| (site, true)) + .chain(validators.iter().map(|site| (site, false))); + let types = names.scope(doc); + let bindings = names::namespace_bindings(project, &here.imports); let mut missing = Vec::new(); - for ((name, offset), declares) in named { - if name.contains("::") - || here.symbols.get_in(name, Namespace::Declaration).is_some() - || covered.contains(name) - { + for ((name, offset), is_type) in named { + if name.contains("::") || here.symbols.get_in(name, Namespace::Declaration).is_some() { + continue; + } + let in_scope = match is_type { + true => !matches!( + types.resolve(name), + Err(Refusal::NotInScope { namespace: false }) + ), + false => covered.contains(name), + }; + if in_scope { continue; } + let declares = match is_type { + true => declares_type as fn(&ProjectDoc, &str) -> bool, + false => declares_validator as fn(&ProjectDoc, &str) -> bool, + }; let candidates: Vec = project .docs @@ -226,6 +272,7 @@ pub fn check(project: &Project, doc: usize) -> ImportCheck { namespace: d.namespace.clone(), own: d.dependency.is_none(), file: file_name(d), + via: is_type.then(|| through(&bindings, &d.namespace)).flatten(), }) .collect(); @@ -243,10 +290,36 @@ pub fn check(project: &Project, doc: usize) -> ImportCheck { range: (*offset, offset + name.len()), candidates, imported_as: aliases.get(name).cloned(), + validator: !is_type, }); } } + // What core's reference rule refuses, each with its own fix. A + // dependency's files are written against its own package. + let mut stand_ins: BTreeSet = missing.iter().map(|m| m.name.clone()).collect(); + let mut references = vec![]; + if here.dependency.is_none() { + for error in names.reference_errors(doc) { + let refused = error.code == NOT_IN_SCOPE || error.code == QUALIFIER_NOT_IN_SCOPE; + // The name as written, which core's validation must not also call + // an unknown type. + if let (true, Some(span)) = (refused, error.span) { + let written = here.source.get(span.0..span.1).unwrap_or_default(); + if !written.is_empty() && written.split("::").all(is_identifier) { + scope.push(FrozenUnit::Import(written.to_string(), None, span)); + stand_ins.insert(written.to_string()); + } + } + // Without the manifest the view may not be the whole package, so a + // qualifier nothing here matches may be a dependency's name. + let unknown_root = error.code == QUALIFIER_NOT_IN_SCOPE && !project.has_manifest; + if !unknown_root { + references.push(error); + } + } + } + let bare_own = bare_own_schema_paths(project, doc); let (unresolved, unverified) = resolution(project, doc, &bare_own); ImportCheck { @@ -255,9 +328,24 @@ pub fn check(project: &Project, doc: usize) -> ImportCheck { unresolved, unverified, bare_own, + references, + stand_ins, } } +/// How the schema `namespace` is written from a name a whole-namespace `use` +/// brought in: through the deepest one it is under (`types`, or `b::sub` +/// after `use package::billing as b`). +pub(crate) fn through(bindings: &[(String, Vec)], namespace: &[String]) -> Option { + let (name, path) = bindings + .iter() + .filter(|(_, path)| namespace.starts_with(path)) + .max_by_key(|(_, path)| path.len())?; + let mut parts = vec![name.as_str()]; + parts.extend(namespace[path.len()..].iter().map(String::as_str)); + Some(parts.join("::")) +} + /// Every `use` in `docs[doc]` that doesn't resolve, by core's own rule /// (`check_imports`): a path no schema of the project matches, or an item /// the matched schema doesn't declare. `std::` paths aren't checked (std @@ -376,6 +464,15 @@ fn resolution( } } + // A directory of schemas, `std` or a dependency's name on its + // own is a name to qualify with, as in core (core#290): + // `use package::billing`, `use shared`. + if use_decl.resolved.symbols.is_empty() + && names::is_namespace(project, &namespace) + { + continue; + } + if first == stdlib::NAME { unresolved.push(unknown_std_path(project, text, span, &namespace)); continue; @@ -387,16 +484,33 @@ fn resolution( } else { find_in(text, span.0, first, true) }; + // One of the package's own schemas is reached through + // `package::`, so that is what is suggested for a bare path to + // it, as core does (core#286); a dependency's name and `std` + // are absolute as they are. + let bare = absolute_first_segment(&use_stmt.path) == Some(first); + let suggestion = range + .and(closest( + first, + top_level.iter().copied().chain(declared.keys().copied()), + )) + .map(|closest| { + let own = top_level.contains(closest.as_str()) + && !declared.contains_key(closest.as_str()) + && !indexed.contains(closest.as_str()) + && closest != stdlib::NAME; + match own && bare { + true => format!("package::{closest}"), + false => closest, + } + }); unresolved.push(UnresolvedImport { range: range.unwrap_or(span), detail: format!( "no schema in this package or its dependencies matches '{}'", namespace.join("::") ), - suggestion: range.and(closest( - first, - top_level.iter().copied().chain(declared.keys().copied()), - )), + suggestion, }); } } @@ -542,7 +656,7 @@ fn find_in(text: &str, base: usize, word: &str, segment: bool) -> Option<(usize, /// Every name a `use` can import from a schema: its types, protocols and /// consts, plus errors (`! Name` throws), validators and settings - what /// core's `check_imports` accepts. -fn importable_names(doc: &ProjectDoc) -> BTreeSet { +pub(crate) fn importable_names(doc: &ProjectDoc) -> BTreeSet { let mut names: BTreeSet = doc .symbols .all_symbols() @@ -732,7 +846,6 @@ mod tests { "use package::types::Message", "use package::types::*", "use package::types::{Message, Kind}", - "use package::types", "import types::Message", ] { let source = format!("{header}\n\nstruct S {{\n m: Message\n}}\n"); @@ -795,18 +908,115 @@ mod tests { } #[test] - fn a_glob_or_whole_namespace_use_of_a_file_outside_the_project_gets_the_benefit_of_the_doubt() { - for header in ["use package::other::*", "use package::other"] { + fn a_glob_of_a_file_outside_the_project_gets_the_benefit_of_the_doubt() { + let source = "use package::other::*\n\nstruct S {\n m: Message\n}\n"; + let files = project(&[("file:///pkg/src/chat.ids", source), TYPES]); + assert_eq!( + missing_names(&files, 0), + vec![], + "`Message` may come from the glob" + ); + } + + /// A whole-namespace `use` brings in its own name and nothing in it + /// (core#290), whether or not the schema is among the files. + #[test] + fn a_whole_namespace_use_brings_in_only_its_name() { + for header in ["use package::types", "use package::other"] { let source = format!("{header}\n\nstruct S {{\n m: Message\n}}\n"); let files = project(&[("file:///pkg/src/chat.ids", &source), TYPES]); assert_eq!( missing_names(&files, 0), - vec![], - "`Message` may come from `{header}`" + vec![("Message".into(), vec!["package::types::Message".into()])], + "`{header}` doesn't import `Message`" ); } } + #[test] + fn a_missing_name_can_be_written_through_the_namespace_a_use_brought_in() { + let source = "use package::types\n\nstruct S {\n m: Message\n}\n"; + let files = project(&[("file:///pkg/src/chat.ids", source), TYPES]); + let view = Project::new(files.iter()); + let missing = check(&view, 0).missing; + assert_eq!(missing.len(), 1); + let candidate = &missing[0].candidates[0]; + assert_eq!(candidate.use_path("Message"), "package::types::Message"); + assert_eq!(candidate.qualified("Message").as_deref(), Some("types::Message")); + + // Through an alias, and a directory: the name it brought in, then the + // rest of the path. + let source = "use package::types as t\n\nstruct S {\n m: Message\n}\n"; + let files = project_files(source); + let view = Project::new(files.iter()); + let missing = check(&view, 0).missing; + assert_eq!( + missing[0].candidates[0].qualified("Message").as_deref(), + Some("t::Message") + ); + let source = "use package::legacy\n\nstruct S {\n m: Message\n}\n"; + let files = project_files(source); + let view = Project::new(files.iter()); + let missing = check(&view, 0).missing; + let qualified: Vec<_> = missing[0] + .candidates + .iter() + .map(|c| c.qualified("Message")) + .collect(); + assert_eq!(qualified, vec![None, Some("legacy::types::Message".to_string())]); + } + + /// `chat.ids` with `source`, `types.ids` and `legacy/types.ids`. + fn project_files(source: &str) -> Vec<(Url, String)> { + project(&[ + ("file:///pkg/src/chat.ids", source), + TYPES, + ( + "file:///pkg/src/legacy/types.ids", + "struct Message {\n old: bool\n}\n", + ), + ]) + } + + #[test] + fn what_core_refuses_is_reported_with_its_fix_and_not_twice() { + use comline_core::schema::ir::compiler::references::DEFINED_TWICE; + + // A bare name after only a whole-namespace `use`: core's + // `reference::not-in-scope`, which `missing` holds the candidates of. + let source = "use package::types\n\nstruct S {\n m: Message\n}\n"; + let files = project(&[("file:///pkg/src/chat.ids", source), TYPES]); + let view = Project::new(files.iter()); + let found = check(&view, 0); + assert_eq!(found.missing.len(), 1); + assert_eq!(found.references.len(), 1, "{:?}", found.references); + assert_eq!(found.references[0].code, NOT_IN_SCOPE); + assert!(!found.missing[0].validator); + assert!(found.stand_ins.contains("Message")); + + // A name declared nowhere: core's own error, and no unknown type. + let source = "struct S {\n m: Nope\n}\n"; + let files = project(&[("file:///pkg/src/chat.ids", source), TYPES]); + let view = Project::new(files.iter()); + let found = check(&view, 0); + assert!(found.missing.is_empty()); + assert_eq!(found.references.len(), 1); + assert_eq!(found.references[0].code, NOT_IN_SCOPE); + assert!(found.stand_ins.contains("Nope")); + + // A qualifier that matches nothing, and a name defined twice. + let source = "use package::types::Message\n\nstruct Message {\n x: bool\n}\n\nstruct S {\n m: typse::Message\n}\n"; + let files = project(&[("file:///pkg/src/chat.ids", source), TYPES]); + let view = Project::new(files.iter()); + let found = check(&view, 0); + let codes: Vec<_> = found.references.iter().map(|d| d.code).collect(); + assert!(codes.contains(&DEFINED_TWICE), "{codes:?}"); + assert!( + !codes.contains(&QUALIFIER_NOT_IN_SCOPE), + "without a manifest the qualifier may be a dependency's name: {codes:?}" + ); + } + #[test] fn a_glob_of_a_project_file_that_doesnt_declare_the_name_does_not_count() { let files = project(&[ @@ -948,9 +1158,18 @@ mod tests { vec![( "typse".to_string(), "no schema in this package or its dependencies matches 'typse::User'".to_string(), - Some("types".to_string()) + // One of the package's own schemas is reached as `package::`, + // as core suggests it (core#286). + Some("package::types".to_string()) )] ); + + // Written `package::`, the typo is of the next segment alone. + let (unresolved, _) = + resolution_of("use package::typse::User\n\nstruct S {\n u: User\n}\n"); + assert_eq!(unresolved.len(), 1, "{unresolved:?}"); + assert_eq!(unresolved[0].0, "typse"); + assert_eq!(unresolved[0].2.as_deref(), Some("types")); } #[test] @@ -972,11 +1191,14 @@ mod tests { for body in [ "use package::types::User\n\nstruct S {\n u: User\n}\n", "use package::types::Gone\n\nstruct S {\n id: u64\n}\n", - "use package::types\n\nstruct S {\n u: User\n}\n", "use std::http::{Request, Response}\n\nstruct S {\n r: Request\n}\n", "use std::validators::StringBounds\n\nstruct S {\n id: u64\n}\n", "use package::broken::Anything\n\nstruct S {\n id: u64\n}\n", "use shared::models::Thing\n\nstruct S {\n t: Thing\n}\n", + // A dependency's name and `std` on their own are names to qualify with. + "use shared\n\nstruct S {\n t: shared::models::Thing\n}\n", + "use std\n\nstruct S {\n r: std::http::Request\n}\n", + "use package::types\n\nstruct S {\n u: types::User\n}\n", ] { let (unresolved, unverified) = resolution_of(body); assert!( diff --git a/src/analysis/mod.rs b/src/analysis/mod.rs index 050c8cb..91a32fd 100644 --- a/src/analysis/mod.rs +++ b/src/analysis/mod.rs @@ -3,6 +3,7 @@ pub mod generation; pub mod import_check; pub mod imports; pub mod modules; +pub mod names; pub mod parse_cache; pub mod project; pub mod source; diff --git a/src/analysis/modules.rs b/src/analysis/modules.rs index 0ee1b53..ef83d2f 100644 --- a/src/analysis/modules.rs +++ b/src/analysis/modules.rs @@ -15,6 +15,7 @@ use comline_core::schema::ir::compiler::import_resolver::ImportResolver; use lsp_types::Url; use crate::analysis::imports; +use crate::analysis::names; use crate::analysis::project::{Project, ProjectDoc}; use crate::util::{follows_as, in_comment_or_string, path_at}; @@ -266,7 +267,16 @@ pub fn path_module_at( return None; } - let namespace = resolve_path(&at.segments, keyword == Some("import"), uri)?; + // In a type position, a name a `use` of this file brought in is a path + // prefix too (`b` in `b::Invoice`, after `use package::billing::api as b`). + let namespace = match keyword { + None => through_binding(project, uri, &at.segments), + Some(_) => None, + }; + let namespace = match namespace { + Some(namespace) => namespace, + None => resolve_path(&at.segments, keyword == Some("import"), uri)?, + }; if !at.continues { let (last, parent) = namespace.split_last()?; @@ -280,6 +290,22 @@ pub fn path_module_at( Some((namespace, at.range)) } +/// The namespace `segments` name when the first is a name a whole-namespace +/// `use` of the file at `uri` brought in: what it stands for, then the rest. +fn through_binding(project: &Project, uri: &Url, segments: &[String]) -> Option> { + let doc = &project.docs[project.index_of(uri)?]; + let (first, rest) = segments.split_first()?; + let (_, namespace) = names::namespace_bindings(project, &doc.imports) + .into_iter() + .find(|(name, _)| name == first)?; + Some(namespace.into_iter().chain(rest.iter().cloned()).collect()) +} + +/// Whether the line `offset` is on is a `use` or an `import`. +pub(crate) fn in_use_statement(source: &str, offset: usize) -> bool { + statement_keyword(source, offset).is_some() +} + /// `use` or `import` when the line `offset` is on starts with it. fn statement_keyword(source: &str, offset: usize) -> Option<&'static str> { let line_start = source[..offset.min(source.len())] diff --git a/src/analysis/names.rs b/src/analysis/names.rs new file mode 100644 index 0000000..2374ec5 --- /dev/null +++ b/src/analysis/names.rs @@ -0,0 +1,407 @@ +//! What a type name written in a schema names: core's rule, over the view. +//! +//! A qualified reference follows the Rust model (core#290): a name in scope is +//! a declaration of the schema, what a `use` of it brings in (its alias, else +//! its last segment) or a root (`package`, `parent`, `self`, `std`, a +//! dependency's name). A whole-namespace `use` (`use package::types`) brings in +//! only `types`, and a full path from a root needs no `use` at all. The rule is +//! core's [`Scope`], run here over the files the server has: [`Names`] builds +//! the [`ProjectContext`] core reads (each file's declarations and text under +//! its namespace, the dependencies the manifest declares) and asks it, so the +//! editor refuses and resolves exactly the names `comline build` does. +//! +//! A [`ProjectContext`] holds `Rc`s, so it can't live in the [`Project`] a +//! server keeps across `await`s; a [`Names`] is built for one request, and +//! dropped with it. +//! +//! A qualified type freezes as its path from the package's root +//! (`api::Invoice` after `use package::billing::api`, an alias's +//! `b::Invoice` and `package::billing::api::Invoice` are all +//! `billing::api::Invoice`), which is what core's validation reads and what +//! [`Names::freeze`] writes into a file's units. + +use std::collections::BTreeSet; + +use comline_core::diagnostics::Diagnostic as CoreDiagnostic; +use comline_core::package::config::idl::grammar as config_grammar; +use comline_core::package::config::ir::context::ProjectContext; +use comline_core::schema::ir::compiler::references::{check_references, Refusal, Resolved, Scope}; +use comline_core::schema::ir::context::SchemaContext; +use comline_core::schema::ir::frozen::types::TypeRef; +use comline_core::schema::ir::frozen::unit::FrozenUnit; +use comline_core::utils::codemap::CodeMap; + +use crate::analysis::import_check::importable_names; +use crate::analysis::imports::ResolvedUse; +use crate::analysis::project::{Project, Target}; +use crate::analysis::stdlib; +use crate::analysis::symbols::Namespace; + +/// The names of a project, as core's reference rule reads them. +pub struct Names<'p, 'a> { + project: &'p Project<'a>, + context: ProjectContext, +} + +impl<'p, 'a> Names<'p, 'a> { + /// `project`'s schemas (each with its declarations and its text, under + /// the namespace it is seen as) and the dependencies it names. + pub fn of(project: &'p Project<'a>) -> Self { + let congregation = config_grammar::parse(&manifest(project)) + .or_else(|_| config_grammar::parse("congregation package")) + .expect("a bare congregation parses"); + let mut context = ProjectContext::with_config(congregation); + let schema = |declarations, namespace: &[String], codemap| { + let schema = + SchemaContext::with_declarations(declarations, namespace.to_vec(), codemap); + std::rc::Rc::new(std::cell::RefCell::new(schema)) + }; + for doc in &project.docs { + let mut codemap = CodeMap::new(); + codemap.insert_file(doc.namespace.join("/"), doc.source); + context + .schema_contexts + .push(schema(doc.document.0.clone(), &doc.namespace, codemap)); + } + // They exist, so a path to one is no unknown path; what they declare + // isn't known. + for namespace in &project.unparsed { + context + .schema_contexts + .push(schema(vec![], namespace, CodeMap::new())); + } + Self { project, context } + } + + /// The project these are the names of. + pub fn project(&self) -> &'p Project<'a> { + self.project + } + + /// The names in scope in `docs[doc]`. + pub fn scope(&self, doc: usize) -> Scope<'_> { + let doc = &self.project.docs[doc]; + Scope::new(&doc.document.0, &doc.namespace, &self.context) + } + + /// What `written`, a type name in `docs[doc]`, names, or why nothing. + pub fn resolve(&self, doc: usize, written: &str) -> Result { + self.scope(doc).resolve(written) + } + + /// The declaration `written`, a type name in `docs[doc]`, names, when the + /// project holds it. + pub fn target(&self, doc: usize, written: &str) -> Option { + self.target_in(&self.scope(doc), written) + } + + /// [`Names::target`], in a scope already built. + pub fn target_in(&self, scope: &Scope<'_>, written: &str) -> Option { + let (namespace, name) = scope.resolve(written).ok()?.target?; + self.target_of(&namespace, &name) + } + + /// The symbol `name` of the schema `namespace`. + fn target_of(&self, namespace: &[String], name: &str) -> Option { + let doc = self + .project + .docs + .iter() + .position(|d| d.namespace == namespace)?; + let symbol = self.project.docs[doc] + .symbols + .index_in(name, Namespace::Declaration)?; + Some(Target { doc, symbol }) + } + + /// The schema (an index into the project's files) and the name in it that + /// `name`, as a unit of `docs[doc]` holds it, stands for: a qualified type + /// is frozen as its path from the package's root, so it names its schema + /// outright; anything else is read through the file's scope, and for the + /// names a `use` is still read for in the older, wider way (a validator a + /// whole-namespace `use` brought in), through that. + pub fn declaration(&self, doc: usize, name: &str) -> Option<(usize, String)> { + if let Some(found) = self.by_path(name) { + return Some(found); + } + if let Ok(Resolved { + target: Some((namespace, real)), + .. + }) = self.scope(doc).resolve(name) + { + let index = self + .project + .docs + .iter() + .position(|d| d.namespace == namespace)?; + return Some((index, real)); + } + let bare = name.rsplit("::").next()?; + let target = self + .project + .resolve_wide(doc, bare, Namespace::Declaration)?; + let real = self.project.symbol(&target)?.name.clone(); + Some((target.doc, real)) + } + + /// The schema whose namespace is `name`'s qualifier, when it declares it: + /// the way a frozen qualified type is read. + fn by_path(&self, name: &str) -> Option<(usize, String)> { + let (qualifier, bare) = name.rsplit_once("::")?; + let parts: Vec = qualifier.split("::").map(String::from).collect(); + let index = self + .project + .docs + .iter() + .position(|d| d.namespace == parts)?; + importable_names(&self.project.docs[index]) + .contains(bare) + .then(|| (index, bare.to_string())) + } + + /// Whether `name`, a qualified type as a unit holds it, is a path to a + /// declaration the project holds (core's `ImportLookup::reaches`): in a + /// schema that doesn't parse right now, taken as declared. + pub fn reaches(&self, name: &str) -> bool { + if self.by_path(name).is_some() { + return true; + } + let Some((qualifier, _)) = name.rsplit_once("::") else { + return false; + }; + self.project + .unparsed + .iter() + .any(|namespace| namespace.join("::") == qualifier) + } + + /// `units` of `docs[doc]` with every qualified type named by its path from + /// the package's root, as core's compiler freezes it. + pub fn freeze(&self, doc: usize, units: &mut [FrozenUnit]) { + let scope = self.scope(doc); + for unit in units { + unit.for_each_type_mut(&mut |ty| { + *ty = ty.map_named(&mut |name| scope.frozen(name).map(TypeRef::Named)); + }); + } + } + + /// The errors core's [`check_references`] reports for `docs[doc]`: a name + /// nothing brought in, a qualifier that isn't in scope, a name defined + /// twice, each with its fix. Every one, in source order. + pub fn reference_errors(&self, doc: usize) -> Vec { + let doc = &self.project.docs[doc]; + check_references(&doc.document.0, &doc.namespace, &self.context) + .err() + .unwrap_or_default() + } + + /// The namespaces `docs[doc]` writes a type path into, for what a hover + /// reaches (a full path needs no `use`). + pub fn written_namespaces(&self, doc: usize) -> Vec> { + let scope = self.scope(doc); + let declarations = &self.project.docs[doc].document.0; + let mut found: Vec> = vec![]; + for written in comline_core::schema::ir::compiler::references::named_types(declarations) { + if let Some(namespace) = scope.reaches(&written.name) { + if !found.contains(&namespace) { + found.push(namespace); + } + } + } + found + } +} + +/// A manifest for core to read the dependencies from: those `config.idp` +/// declares, and any whose files the view holds. +fn manifest(project: &Project) -> String { + let mut names: BTreeSet<&str> = project + .dependencies + .iter() + .map(|d| d.name.as_str()) + .chain(project.docs.iter().filter_map(|d| d.dependency)) + .collect(); + // Not a dependency's name: `std` is the library's own, and core refuses + // the prefixes. + names.retain(|name| { + *name != stdlib::NAME && !crate::analysis::modules::is_relative_prefix(name) + }); + + let mut text = String::from("congregation package\n"); + if !names.is_empty() { + text.push_str("\ndependencies = {\n"); + for name in names { + text.push_str(&format!(" {name} = {{\n path = \".\"\n }}\n")); + } + text.push_str("}\n"); + } + text +} + +/// A name a whole-namespace `use` brings in (`use package::types`, +/// `use package::billing::api as b`), and the namespace it stands for: to +/// qualify with, not a type. From `uses`, the `use`s as written, so it reads +/// a file that doesn't parse as well as one that does. +pub fn namespace_bindings(project: &Project, uses: &[ResolvedUse]) -> Vec<(String, Vec)> { + uses.iter() + .filter_map(|u| { + let namespace = &u.resolved.absolute_namespace; + if !u.resolved.symbols.is_empty() { + return None; + } + let name = u.alias.clone().or_else(|| namespace.last().cloned())?; + is_namespace(project, namespace).then(|| (name, namespace.clone())) + }) + .collect() +} + +/// Whether `namespace` is a schema or a directory of them, `std` or a +/// dependency's name: something to qualify with. +pub(crate) fn is_namespace(project: &Project, namespace: &[String]) -> bool { + if let [only] = namespace { + if only == stdlib::NAME || project.dependencies.iter().any(|d| d.name == *only) { + return true; + } + } + !namespace.is_empty() + && project + .docs + .iter() + .map(|d| &d.namespace) + .chain(&project.unparsed) + .any(|schema| schema.starts_with(namespace)) +} + +#[cfg(test)] +mod tests { + use super::*; + use lsp_types::Url; + + fn files(list: &[(&str, &str)]) -> Vec<(Url, String)> { + list.iter() + .map(|(uri, text)| (Url::parse(uri).unwrap(), text.to_string())) + .collect() + } + + const MANIFEST: &str = "congregation app\nspecification_version = 1\n\ndependencies = {\n shared = {\n path = \"../shared\"\n }\n}\n"; + + fn package() -> Vec<(Url, String)> { + files(&[ + ("file:///pkg/config.idp", MANIFEST), + ( + "file:///pkg/src/types.ids", + "struct User {\n id: u64\n}\n", + ), + ( + "file:///pkg/src/billing/api.ids", + "struct Invoice {\n id: u64\n}\n", + ), + ( + "file:///pkg/src/app.ids", + "struct Local {\n id: u64\n}\n", + ), + ]) + } + + fn app(source: &str) -> Vec<(Url, String)> { + let mut all = package(); + all.retain(|(uri, _)| !uri.path().ends_with("app.ids")); + all.push(( + Url::parse("file:///pkg/src/app.ids").unwrap(), + source.to_string(), + )); + all + } + + fn resolved(source: &str, written: &str) -> Option<(Vec, String)> { + let all = app(source); + let project = Project::new(all.iter()); + let doc = project.index_of(&Url::parse("file:///pkg/src/app.ids").unwrap())?; + Names::of(&project).resolve(doc, written).ok()?.target + } + + fn ns(path: &str) -> Vec { + path.split("::").map(String::from).collect() + } + + #[test] + fn a_whole_namespace_use_brings_in_only_its_name() { + let source = "use package::types\n\nstruct S {\n u: types::User\n}\n"; + assert_eq!( + resolved(source, "types::User"), + Some((ns("types"), "User".into())) + ); + assert_eq!( + resolved(source, "User"), + None, + "nothing in `types` is in scope bare" + ); + } + + #[test] + fn a_full_path_from_a_root_needs_no_use() { + let source = "struct S {\n u: package::types::User\n}\n"; + assert_eq!( + resolved(source, "package::types::User"), + Some((ns("types"), "User".into())) + ); + } + + #[test] + fn a_path_goes_down_from_a_name_in_scope_and_through_an_alias() { + let down = "use package::billing\n\nstruct S {\n i: billing::api::Invoice\n}\n"; + assert_eq!( + resolved(down, "billing::api::Invoice"), + Some((ns("billing::api"), "Invoice".into())) + ); + let alias = "use package::billing::api as b\n\nstruct S {\n i: b::Invoice\n}\n"; + assert_eq!( + resolved(alias, "b::Invoice"), + Some((ns("billing::api"), "Invoice".into())) + ); + } + + #[test] + fn after_an_alias_the_namespace_s_own_name_is_not_in_scope() { + let source = "use package::types as t\n\nstruct S {\n u: types::User\n}\n"; + assert_eq!(resolved(source, "types::User"), None); + assert_eq!( + resolved(source, "t::User"), + Some((ns("types"), "User".into())) + ); + } + + #[test] + fn a_qualified_type_freezes_as_its_path_and_reaches_what_it_names() { + let all = app("use package::billing::api as b\n\nstruct S {\n i: b::Invoice\n}\n"); + let project = Project::new(all.iter()); + let doc = project + .index_of(&Url::parse("file:///pkg/src/app.ids").unwrap()) + .unwrap(); + let names = Names::of(&project); + assert_eq!( + names.scope(doc).frozen("b::Invoice").as_deref(), + Some("billing::api::Invoice") + ); + assert!(names.reaches("billing::api::Invoice")); + assert!(!names.reaches("billing::api::Nope")); + assert!(!names.reaches("billing::Invoice")); + } + + #[test] + fn a_namespace_use_is_a_binding_and_an_item_use_is_not() { + let all = app("use package::types\nuse package::billing::api::Invoice\nuse package::billing::api as b\n"); + let project = Project::new(all.iter()); + let doc = project + .index_of(&Url::parse("file:///pkg/src/app.ids").unwrap()) + .unwrap(); + assert_eq!( + namespace_bindings(&project, &project.docs[doc].imports), + vec![ + ("types".to_string(), ns("types")), + ("b".to_string(), ns("billing::api")), + ] + ); + } +} diff --git a/src/analysis/project.rs b/src/analysis/project.rs index 8988cff..eae0d67 100644 --- a/src/analysis/project.rs +++ b/src/analysis/project.rs @@ -11,9 +11,12 @@ use comline_core::schema::idl::grammar::{ Annotation, AnnotationValue, Declaration, Document, Field, Type, }; use comline_core::schema::idl::module_docs::module_docs; +use comline_core::schema::ir::compiler::references::Scope; use lsp_types::{Location, Url}; use crate::analysis::imports::{self, ProjectFile, ResolvedUse}; +use crate::analysis::modules; +use crate::analysis::names::Names; use crate::analysis::parse_cache; use crate::analysis::source::{self, DeclaredDependency, ProjectSource}; use crate::analysis::symbols::{self, Namespace, Symbol, SymbolTable}; @@ -308,18 +311,84 @@ impl<'a> Project<'a> { self.docs.iter().position(|d| d.uri == uri) } - /// What `name`, written in `docs[doc]`, refers to, by the file's own - /// scope - the way `comline build` sees it: + /// What `written`, a name in a type position of `docs[doc]`, refers to, by + /// the file's own scope - the way `comline build` sees it (core#290): /// 1. a declaration in that same file; - /// 2. a sibling that one of that file's own `use`/`import`s brings - /// `name` into scope from (`imports::resolve_symbol` - so through an - /// `as` alias, the target's real name comes back). + /// 2. what one of that file's own `use`s brings in: a name it imports (or + /// its `as` alias, so the target's real name comes back), or a + /// namespace to qualify with (`types::User`, `b::Invoice`); + /// 3. a full path from a root, which needs no `use` (`package::types::User`). /// - /// Nothing else: a name the file never imports refers to nothing, even - /// where another file declares it - for diagnostics, references and - /// rename, and for go-to-definition and hover alike. The missing `use` - /// is the diagnostic's to report, and its quick fix's to add. - pub fn resolve(&self, doc: usize, name: &str, namespace: Namespace) -> Option { + /// A whole-namespace `use` brings in only its own name: after + /// `use package::types`, `types::User` refers to `User` and a bare `User` + /// to nothing. Nothing else: a name the file never brings in refers to + /// nothing, even where another file declares it - for diagnostics, + /// references and rename, and for go-to-definition and hover alike. The + /// missing `use` is the diagnostic's to report, and its quick fix's to add. + pub fn resolve(&self, doc: usize, written: &str, namespace: Namespace) -> Option { + if let Some(target) = self.local(doc, written, namespace) { + return Some(target); + } + if namespace != Namespace::Declaration { + return None; + } + let names = Names::of(self); + self.resolve_in(&names.scope(doc), doc, written, namespace) + } + + /// A declaration of `docs[doc]` itself named `written` (a bare name). + fn local(&self, doc: usize, written: &str, namespace: Namespace) -> Option { + if written.contains("::") { + return None; + } + let symbol = self.docs[doc].symbols.index_in(written, namespace)?; + Some(Target { doc, symbol }) + } + + /// [`Project::resolve`] with the file's `scope` already built: what a loop + /// over many names asks. + pub(crate) fn resolve_in( + &self, + scope: &Scope<'_>, + doc: usize, + written: &str, + namespace: Namespace, + ) -> Option { + if let Some(target) = self.local(doc, written, namespace) { + return Some(target); + } + if namespace != Namespace::Declaration { + return None; + } + match scope.resolve(written) { + Ok(resolved) => match resolved.target { + Some((schema, name)) => { + let index = self.docs.iter().position(|d| d.namespace == schema)?; + let symbol = self.docs[index].symbols.index_in(&name, namespace)?; + Some(Target { doc: index, symbol }) + } + // A `use` core can't read into a schema (the legacy `import`, + // one that doesn't resolve): by the older rule, where it stood + // for its own name. + None if !written.contains("::") => self.resolve_wide(doc, written, namespace), + None => None, + }, + Err(_) => None, + } + } + + /// What `name`, written in `docs[doc]`, refers to by the older, wider rule: + /// a declaration in that same file, or a sibling that one of that file's + /// own `use`/`import`s brings `name` into scope from + /// (`imports::resolve_symbol`), where a whole-namespace `use` brings in + /// every name the schema has. Still how a validator, a constant or an error + /// a `use` brings in is read, as core reads them. + pub(crate) fn resolve_wide( + &self, + doc: usize, + name: &str, + namespace: Namespace, + ) -> Option { let here = &self.docs[doc]; if let Some(symbol) = here.symbols.index_in(name, namespace) { @@ -368,11 +437,28 @@ impl<'a> Project<'a> { return Some(target); } let (start, end) = crate::util::word_range_at(source, offset)?; - self.resolve( - doc, - &source[start..end], - symbols::namespace_at(source, offset), - ) + let namespace = symbols::namespace_at(source, offset); + // In a type position the name is the whole path the cursor ends + // (`types::User`, `b::Invoice`, `package::types::User`); its other + // segments name a namespace, not a declaration. A `use`'s path is + // read as it always was, by the name under the cursor. + let written = match crate::util::path_at(source, offset) { + Some(path) if !modules::in_use_statement(source, offset) => { + if path.continues { + return None; + } + path.segments.join("::") + } + // In a `use`, the name written is the item it imports, whatever it + // is bound as here (`Message` in `use a::Message as Msg`). + _ => { + let word = &source[start..end]; + return self + .resolve(doc, word, namespace) + .or_else(|| self.resolve_wide(doc, word, namespace)); + } + }; + self.resolve(doc, &written, namespace) } /// The symbol `target` names. @@ -394,6 +480,7 @@ impl<'a> Project<'a> { let Some(target_name) = self.symbol(target).map(|s| s.name.as_str()) else { return found; }; + let names = Names::of(self); for (index, doc) in self.docs.iter().enumerate() { // A dependency's own files aren't this package's to search: their @@ -402,25 +489,26 @@ impl<'a> Project<'a> { continue; } let mut seen = BTreeSet::new(); + let scope = names.scope(index); for span in type_spans(&doc.document) { - // The symbol each names: the last segment (`types::User` - // names `User`). - let names: BTreeSet<&str> = named_types(span.1) - .into_iter() - .filter_map(|name| name.rsplit("::").next()) - .collect(); - - for name in names { + // Each name as written (`User`, `types::User`); the symbol it + // names is the last segment. + for written in named_types(span.1) { // A type position, so the declaration namespace - a // settings preset of the same name is a different // symbol and must not be swept up here. // In scope only: a file naming `User` without importing // it doesn't refer to this one, and rename mustn't edit it. - if self.resolve(index, name, Namespace::Declaration).as_ref() != Some(target) { + if self + .resolve_in(&scope, index, written, Namespace::Declaration) + .as_ref() + != Some(target) + { continue; } - for offset in occurrences_in(doc.source, span.0, name) { + let name = written.rsplit("::").next().unwrap_or(written); + for offset in path_occurrences(doc.source, span.0, written) { if seen.insert(offset) { found.push(self.reference(doc, offset, name, name == target_name)); } @@ -563,6 +651,25 @@ fn occurrences_in(source: &str, span: (usize, usize), name: &str) -> Vec .collect() } +/// Where the last segment of `written` (`User` in `types::User`) is in `span` +/// each time `written` is written there as a whole path - not part of a longer +/// one, and, for a bare name, not the end of a qualified one: in +/// `use User::User`, only the second `User` is the symbol. +fn path_occurrences(source: &str, span: (usize, usize), written: &str) -> Vec { + let (start, text) = clamped(source, span); + let name_len = written.rsplit("::").next().map_or(0, str::len); + let part_of_path = |c: char| crate::util::is_ident_char(c) || c == ':'; + + text.match_indices(written) + .filter(|&(i, _)| { + let before = text[..i].chars().next_back(); + let after = text[i + written.len()..].chars().next(); + !before.is_some_and(part_of_path) && !after.is_some_and(part_of_path) + }) + .map(|(i, _)| start + i + written.len() - name_len) + .collect() +} + /// Every type position in a document, with its byte span. fn type_spans(document: &Document) -> Vec<((usize, usize), &Type)> { let mut spans = Vec::new(); @@ -973,4 +1080,116 @@ mod tests { Some(&SettingsValue::Integer(4)) ); } + + // ---- qualified references (ComlineProject/language-server#124, core#290) ---- + + #[test] + fn a_whole_namespace_use_brings_in_only_its_name() { + let all = files(&[ + ("file:///pkg/src/types.ids", "struct User {\n name: string\n}\n"), + ( + "file:///pkg/src/chat.ids", + "use package::types\n\nstruct S {\n a: types::User\n b: User\n}\n", + ), + ]); + let project = Project::new(all.iter()); + let target = project + .resolve(1, "types::User", Namespace::Declaration) + .expect("through the namespace"); + assert_eq!( + (target.doc, project.symbol(&target).unwrap().name.as_str()), + (0, "User") + ); + assert_eq!( + project.resolve(1, "User", Namespace::Declaration), + None, + "nothing in `types` is in scope bare" + ); + + // Only the qualified one refers to it, and at its own `User`. + assert_eq!(lines(&project.references(&target)), vec![("chat.ids".into(), 3, 14)]); + } + + #[test] + fn every_spelling_of_a_path_reaches_the_same_declaration() { + let chat = "use package::billing\nuse package::billing::api as b\nuse package::billing::api::Invoice\n\n\ + struct S {\n a: billing::api::Invoice\n b: b::Invoice\n c: package::billing::api::Invoice\n d: Invoice\n}\n"; + let all = files(&[ + ( + "file:///pkg/src/billing/api.ids", + "struct Invoice {\n id: u64\n}\n", + ), + ("file:///pkg/src/chat.ids", chat), + ]); + let project = Project::new(all.iter()); + let want = project + .resolve(1, "Invoice", Namespace::Declaration) + .expect("by its `use`"); + assert_eq!(want.doc, 0); + for written in [ + "billing::api::Invoice", + "b::Invoice", + "package::billing::api::Invoice", + ] { + assert_eq!( + project.resolve(1, written, Namespace::Declaration).as_ref(), + Some(&want), + "{written}" + ); + } + + // Each is a reference at its last segment, and the `use` that imports it. + assert_eq!( + lines(&project.references(&want)), + vec![ + ("chat.ids".into(), 2, 27), + ("chat.ids".into(), 5, 21), + ("chat.ids".into(), 6, 10), + ("chat.ids".into(), 7, 30), + ("chat.ids".into(), 8, 7), + ] + ); + } + + #[test] + fn a_name_that_is_no_qualifier_resolves_nothing() { + let all = files(&[ + ("file:///pkg/src/types.ids", "struct User {\n name: string\n}\n"), + ( + "file:///pkg/src/chat.ids", + "use package::types as t\nuse package::types::User\n\nstruct S {\n a: User\n}\n", + ), + ]); + let project = Project::new(all.iter()); + // After an alias the namespace's own name isn't in scope, and an + // imported item has nothing under it. + assert_eq!(project.resolve(1, "types::User", Namespace::Declaration), None); + assert_eq!(project.resolve(1, "User::User", Namespace::Declaration), None); + assert!(project.resolve(1, "t::User", Namespace::Declaration).is_some()); + } + + #[test] + fn a_cursor_on_a_path_resolves_by_the_whole_path() { + let chat = "use package::billing\n\nstruct Invoice {\n mine: bool\n}\n\n\ + struct S {\n a: billing::api::Invoice\n b: Invoice\n}\n"; + let all = files(&[ + ( + "file:///pkg/src/billing/api.ids", + "struct Invoice {\n id: u64\n}\n", + ), + ("file:///pkg/src/chat.ids", chat), + ]); + let project = Project::new(all.iter()); + let at = |needle: &str| { + let offset = chat.find(needle).unwrap(); + project.target_at_cursor(1, chat, offset) + }; + // The qualified one is billing's, the bare one is this file's own. + let qualified = at("Invoice\n b").expect("through the path"); + assert_eq!(qualified.doc, 0); + let bare = at("Invoice\n}").expect("the local struct"); + assert_eq!(bare.doc, 1); + // A qualifier is a namespace, not a declaration. + assert_eq!(at("billing::api"), None); + } } diff --git a/src/backend.rs b/src/backend.rs index 42a85cb..ed280ae 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -6,6 +6,7 @@ use std::sync::{Arc, RwLock}; // use crate::analysis::comline_toml::{self, is_comline_toml}; use crate::analysis::imports::namespace_of; +use crate::analysis::names::Names; use crate::analysis::source::SourceFile; use crate::analysis::stdlib; use crate::dependencies; @@ -875,17 +876,34 @@ impl Backend { let view = self.package_view(&documents[0].uri); let project = Project::new(view.iter()); - for document in documents { - // In the project exactly when it parsed cleanly. - let lsp_diagnostics = match project.index_of(&document.uri) { - Some(index) => diagnostics::project_diagnostics(&project, index), - None => parser::parse(&document.text) - .map(|result| { - diagnostics::generate_diagnostics(&document.text, &result.errors) - }) - .unwrap_or_default(), - }; + // What each file's names are is asked of the one `Names` the + // package shares, which holds `Rc`s and so is dropped before the + // first `await`. + let computed: Vec<_> = { + let names = Names::of(&project); + documents + .iter() + .map(|document| { + // In the project exactly when it parsed cleanly. + let lsp_diagnostics = match project.index_of(&document.uri) { + Some(index) => { + diagnostics::project_diagnostics_with(&project, &names, index) + } + None => parser::parse(&document.text) + .map(|result| { + diagnostics::generate_diagnostics( + &document.text, + &result.errors, + ) + }) + .unwrap_or_default(), + }; + (document, lsp_diagnostics) + }) + .collect() + }; + for (document, lsp_diagnostics) in computed { self.client .publish_diagnostics( document.uri.clone(), diff --git a/src/handlers/code_actions.rs b/src/handlers/code_actions.rs index 6ee4132..37cf7b3 100644 --- a/src/handlers/code_actions.rs +++ b/src/handlers/code_actions.rs @@ -11,6 +11,7 @@ use crate::analysis::source::ProjectSource; use crate::analysis::symbols::{Namespace, Symbol}; use crate::handlers::rename; use crate::util::{byte_range_to_lsp_range, file_edit, position_to_offset}; +use comline_core::schema::ir::compiler::references::NOT_IN_SCOPE; use lsp_types::{ CodeAction, CodeActionKind, CodeActionOrCommand, CodeActionParams, Command, Diagnostic, NumberOrString, Range, SymbolKind, TextEdit, Url, @@ -73,7 +74,33 @@ pub fn get_code_actions_with_project( continue; } - let diagnostics = matching_diagnostics(params, range, MISSING_IMPORT); + // A type is core's `reference::not-in-scope`, which carries the fix for + // the one schema that declares it; these are the fixes for each of + // several, and the other way to write it. A validator is the editor's + // own `import::missing`. + let code = match missing.validator { + true => MISSING_IMPORT, + false => NOT_IN_SCOPE.id, + }; + let diagnostics = matching_diagnostics(params, range, code); + + // Imported here under an alias: it is that name that is in scope. + if let Some(alias) = &missing.imported_as { + actions.push(CodeActionOrCommand::CodeAction(CodeAction { + title: format!("Write `{alias}`"), + kind: Some(CodeActionKind::QUICKFIX), + diagnostics: diagnostics.clone(), + edit: Some(file_edit( + uri, + vec![TextEdit { + range, + new_text: alias.clone(), + }], + )), + is_preferred: Some(false), + ..Default::default() + })); + } let only_one = missing.candidates.len() == 1; for candidate in &missing.candidates { @@ -87,6 +114,25 @@ pub fn get_code_actions_with_project( is_preferred: Some(only_one), ..Default::default() })); + + // Or, where a `use` here brought the schema in as a namespace + // (`use package::types`), the name written through it. + if let Some(written) = candidate.qualified(&missing.name) { + actions.push(CodeActionOrCommand::CodeAction(CodeAction { + title: format!("Write `{written}`"), + kind: Some(CodeActionKind::QUICKFIX), + diagnostics: diagnostics.clone(), + edit: Some(file_edit( + uri, + vec![TextEdit { + range, + new_text: written, + }], + )), + is_preferred: Some(false), + ..Default::default() + })); + } } } @@ -99,7 +145,19 @@ pub fn get_code_actions_with_project( other_files, )); actions.extend(f_string_actions(source, uri, params)); - actions.extend(fix_actions(uri, params)); + // Core's own fix of a diagnostic is the same one an action above already + // makes (core's "Add `use ...`" for the one schema that declares the name). + let made: Vec = actions + .iter() + .filter_map(|a| match a { + CodeActionOrCommand::CodeAction(a) => Some(a.title.clone()), + _ => None, + }) + .collect(); + actions.extend(fix_actions(uri, params).into_iter().filter(|a| match a { + CodeActionOrCommand::CodeAction(a) => !made.contains(&a.title), + _ => true, + })); // Several uses of the same name in range: one fix per candidate is enough. actions.dedup_by(|a, b| match (a, b) { @@ -566,8 +624,8 @@ mod tests { assert_eq!( titles_and_edits(actions), vec![( - "Change `typse` to `types`".to_string(), - "types".to_string(), + "Change `typse` to `package::types`".to_string(), + "package::types".to_string(), 0 )] ); @@ -1080,3 +1138,228 @@ mod own_schema_path_tests { ); } } + +/// Quick fixes that write a `use` or a reference follow the Rust model too +/// (ComlineProject/language-server#124, core#290): core's own fixes for what the +/// reference rule refuses, and the fixes for a missing `use`. +#[cfg(test)] +mod reference_tests { + use super::*; + use crate::analysis::diagnostics::project_diagnostics; + use crate::analysis::source::SourceFile; + use lsp_types::{CodeActionContext, Position, TextDocumentIdentifier}; + + const MANIFEST: &str = "congregation app\nspecification_version = 1\n\n\ + dependencies = {\n shared = {\n path = \"../shared\"\n }\n}\n"; + + fn local(path: &str, text: &str) -> SourceFile { + SourceFile::local( + Url::parse(&format!("file:///pkg/{path}")).unwrap(), + text.to_string(), + ) + } + + /// Everything but `chat.ids`: the manifest, `types` and a dependency. + fn others() -> Vec { + vec![ + local("config.idp", MANIFEST), + local( + "src/types.ids", + "struct Message {\n id: u64\n}\n\nenum Kind {\n A\n}\n", + ), + SourceFile::of_dependency( + Url::parse("file:///shared/src/models.ids").unwrap(), + "struct Thing {\n id: u64\n}\n".to_string(), + "shared", + vec!["shared".to_string(), "models".to_string()], + ), + ] + } + + fn chat_uri() -> Url { + Url::parse("file:///pkg/src/chat.ids").unwrap() + } + + fn diagnostics_of(chat: &str) -> Vec { + let mut all = vec![SourceFile::local(chat_uri(), chat.to_string())]; + all.extend(others()); + project_diagnostics(&Project::new(&all), 0) + } + + /// The quick fixes over the whole of `chat`, for the diagnostics it has. + fn actions_for(chat: &str) -> Vec { + let uri = chat_uri(); + let params = CodeActionParams { + text_document: TextDocumentIdentifier { uri: uri.clone() }, + range: Range::new(Position::new(0, 0), Position::new(20, 0)), + context: CodeActionContext { + diagnostics: diagnostics_of(chat), + only: None, + trigger_kind: None, + }, + work_done_progress_params: Default::default(), + partial_result_params: Default::default(), + }; + get_code_actions_with_project(chat, &uri, ¶ms, &others()) + .into_iter() + .map(|a| match a { + CodeActionOrCommand::CodeAction(a) => a, + other => panic!("expected a code action, got {other:?}"), + }) + .collect() + } + + /// The edits `action` makes to the file, applied to `source`. + fn apply(source: &str, action: &CodeAction) -> String { + let changes = action.edit.as_ref().unwrap().changes.as_ref().unwrap(); + let mut edits = changes[&chat_uri()].clone(); + edits.sort_by_key(|e| std::cmp::Reverse((e.range.start.line, e.range.start.character))); + let mut out = source.to_string(); + for edit in edits { + let start = crate::util::position_to_offset(&out, edit.range.start).unwrap(); + let end = crate::util::position_to_offset(&out, edit.range.end).unwrap(); + out.replace_range(start..end, &edit.new_text); + } + out + } + + fn titles(actions: &[CodeAction]) -> Vec<&str> { + actions.iter().map(|a| a.title.as_str()).collect() + } + + #[test] + fn a_bare_name_after_a_whole_namespace_use_is_fixed_by_a_use_or_by_the_path() { + let chat = "use package::types\n\nstruct S {\n m: Message\n}\n"; + let actions = actions_for(chat); + assert_eq!( + titles(&actions), + ["Add `use package::types::Message`", "Write `types::Message`"] + ); + assert_eq!(actions[0].is_preferred, Some(true)); + assert_eq!(actions[1].is_preferred, Some(false)); + // Both are fixes of core's `reference::not-in-scope`. + for action in &actions { + let attached = action.diagnostics.as_ref().expect("attached to the error"); + assert_eq!(attached.len(), 1); + assert_eq!( + attached[0].code, + Some(NumberOrString::String("reference::not-in-scope".to_string())) + ); + } + + // An item `use`, not a namespace `use` followed by the bare name. + let with_use = apply(chat, &actions[0]); + assert_eq!( + with_use, + "use package::types\nuse package::types::Message\n\nstruct S {\n m: Message\n}\n" + ); + assert!(diagnostics_of(&with_use).is_empty(), "{with_use}"); + + let written = apply(chat, &actions[1]); + assert_eq!( + written, + "use package::types\n\nstruct S {\n m: types::Message\n}\n" + ); + assert!(diagnostics_of(&written).is_empty(), "{written}"); + } + + #[test] + fn a_name_imported_under_an_alias_can_be_written_as_the_alias() { + let chat = "use package::types::Message as Msg\n\nstruct S {\n m: Message\n}\n"; + let actions = actions_for(chat); + assert_eq!( + titles(&actions), + ["Write `Msg`", "Add `use package::types::Message`"] + ); + let fixed = apply(chat, &actions[0]); + assert!(diagnostics_of(&fixed).is_empty(), "{fixed}"); + } + + #[test] + fn several_schemas_declaring_the_name_each_get_an_add_use() { + let chat = "struct S {\n m: Message\n}\n"; + let uri = chat_uri(); + let mut files = others(); + files.push(local("src/legacy/types.ids", "struct Message {\n id: u64\n}\n")); + let mut all = vec![SourceFile::local(uri.clone(), chat.to_string())]; + all.extend(files.iter().cloned()); + let diagnostics = project_diagnostics(&Project::new(&all), 0); + assert_eq!(diagnostics.len(), 1, "{diagnostics:#?}"); + assert_eq!( + diagnostics[0].code, + Some(NumberOrString::String("reference::not-in-scope".to_string())) + ); + let params = CodeActionParams { + text_document: TextDocumentIdentifier { uri: uri.clone() }, + range: Range::new(Position::new(0, 0), Position::new(20, 0)), + context: CodeActionContext { + diagnostics, + only: None, + trigger_kind: None, + }, + work_done_progress_params: Default::default(), + partial_result_params: Default::default(), + }; + let titles: Vec = get_code_actions_with_project(chat, &uri, ¶ms, &files) + .into_iter() + .filter_map(|a| match a { + CodeActionOrCommand::CodeAction(a) => Some(a.title), + _ => None, + }) + .collect(); + assert_eq!( + titles, + [ + "Add `use package::types::Message`", + "Add `use package::legacy::types::Message`" + ] + ); + } + + #[test] + fn with_no_use_at_all_only_an_item_use_is_offered() { + let chat = "struct S {\n m: Message\n}\n"; + let actions = actions_for(chat); + assert_eq!(titles(&actions), ["Add `use package::types::Message`"]); + let fixed = apply(chat, &actions[0]); + assert_eq!( + fixed, + "use package::types::Message\n\nstruct S {\n m: Message\n}\n" + ); + assert!(diagnostics_of(&fixed).is_empty(), "{fixed}"); + } + + #[test] + fn core_s_fixes_for_a_refused_reference_are_quick_fixes() { + // A qualified name after an item `use`: write the bare name. + let chat = "use package::types::Message\n\nstruct S {\n m: types::Message\n}\n"; + let actions = actions_for(chat); + assert_eq!(titles(&actions), ["Write `Message`"]); + assert_eq!(actions[0].kind, Some(CodeActionKind::QUICKFIX)); + assert_eq!(actions[0].is_preferred, Some(true)); + let fixed = apply(chat, &actions[0]); + assert_eq!( + fixed, + "use package::types::Message\n\nstruct S {\n m: Message\n}\n" + ); + assert!(diagnostics_of(&fixed).is_empty(), "{fixed}"); + + // A name defined twice: import it under an alias. + let chat = "use package::types::Message\n\nstruct Message {\n x: bool\n}\n"; + let actions = actions_for(chat); + assert_eq!(titles(&actions), ["Import `Message` as `TypesMessage`"]); + let fixed = apply(chat, &actions[0]); + assert!(diagnostics_of(&fixed).is_empty(), "{fixed}"); + } + + #[test] + fn a_did_you_mean_for_a_bare_own_path_writes_the_package_path() { + let chat = "use typse::Message\n\nstruct S {\n m: Message\n}\n"; + let actions = actions_for(chat); + assert_eq!(titles(&actions), ["Change `typse` to `package::types`"]); + assert_eq!( + apply(chat, &actions[0]), + "use package::types::Message\n\nstruct S {\n m: Message\n}\n" + ); + } +} diff --git a/src/handlers/completion/mod.rs b/src/handlers/completion/mod.rs index fb794b6..a91fbfe 100644 --- a/src/handlers/completion/mod.rs +++ b/src/handlers/completion/mod.rs @@ -1,5 +1,6 @@ // Completion handler - provides auto-completion suggestions +mod type_path; mod use_path; mod validator_body; mod validators; @@ -11,6 +12,7 @@ use std::collections::BTreeSet; use crate::analysis::import_check::{self, declares_type, file_name, is_type_kind, Target}; use crate::analysis::import_check::use_path; use crate::analysis::imports::{self, add_use_edit, ResolvedUse}; +use crate::analysis::names; use crate::analysis::project::{Project, ProjectDoc}; use crate::analysis::source::ProjectSource; use crate::analysis::stdlib; @@ -117,11 +119,17 @@ fn completions_at( .map(|document| symbols::build_symbol_table(&document, uri, source)); // Every known type: primitives first, then user-declared struct/enum/ - // alias names, then the ones other files declare. + // alias names, then the ones other files declare, then the roots a path + // can start with. After a `::` (`types::`, `package::billing::`), only what + // that path can go on with. let type_items = || { + if let Some(path) = type_path::path_before(source, offset) { + return type_path::completions(&path, source, uri, offset, other_files); + } let mut items = get_primitive_type_completions(); items.extend(get_type_completions(symbol_table.as_ref(), source)); items.extend(get_import_completions(source, uri, other_files, symbol_table.as_ref())); + items.extend(type_path::roots(source, offset, other_files)); items }; @@ -205,10 +213,12 @@ fn completions_at( } /// Type names from the package's other files. First the ones this file's `use`s -/// bring into scope - under their alias, if they have one - then every -/// other type those files declare, sorted after everything else, which -/// inserts the missing `use` line along with the name. Names already -/// declared here, or already in scope, aren't offered again. +/// bring into scope - under their alias, if they have one - and the namespaces +/// it brought in, to qualify with (a whole-namespace `use` brings in only its +/// own name, core#290); then every other type those files declare, sorted after +/// everything else: through a namespace in scope as `types::User` where one +/// reaches it, else inserting the missing `use` line along with the name. +/// Names already declared here, or already in scope, aren't offered again. fn get_import_completions( source: &str, uri: &Url, @@ -222,9 +232,10 @@ fn get_import_completions( // (file, declared name) already imported, under whatever name. let mut imported: BTreeSet<(usize, String)> = BTreeSet::new(); let mut items = Vec::new(); + let uses = uses_in(source, uri); - for use_decl in uses_in(source, uri) { - let Target::Found(sibling, remaining) = import_check::target_of(&siblings, None, &use_decl) + for use_decl in &uses { + let Target::Found(sibling, remaining) = import_check::target_of(&siblings, None, use_decl) else { continue; // can't list what a file outside the project declares }; @@ -232,24 +243,26 @@ fn get_import_completions( let symbols = &use_decl.resolved.symbols; // (name in scope here, name declared there) - let names: Vec<(String, String)> = - if symbols == &["*"] || (symbols.is_empty() && remaining.is_empty()) { - type_names(doc).map(|n| (n.clone(), n)).collect() - } else if !symbols.is_empty() { - symbols - .iter() - .filter(|n| declares_type(doc, n)) - .map(|n| (n.clone(), n.clone())) - .collect() - } else { - let real = remaining.join("::"); - match declares_type(doc, &real) { - true => vec![(use_decl.alias.clone().unwrap_or_else(|| real.clone()), real)], - false => vec![], - } - }; + let brought: Vec<(String, String)> = if symbols == &["*"] { + type_names(doc).map(|n| (n.clone(), n)).collect() + } else if !symbols.is_empty() { + symbols + .iter() + .filter(|n| declares_type(doc, n)) + .map(|n| (n.clone(), n.clone())) + .collect() + } else if remaining.is_empty() { + // `use package::types` brings in `types` and nothing in it. + vec![] + } else { + let real = remaining.join("::"); + match declares_type(doc, &real) { + true => vec![(use_decl.alias.clone().unwrap_or_else(|| real.clone()), real)], + false => vec![], + } + }; - for (label, real) in names { + for (label, real) in brought { imported.insert((sibling, real.clone())); if in_scope.insert(label.clone()) { let detail = imported_detail(doc, &label, &real); @@ -258,20 +271,63 @@ fn get_import_completions( } } + // What a whole-namespace `use` did bring in: a name to go on with. + let bindings = names::namespace_bindings(&siblings, &uses); + for (name, namespace) in &bindings { + if in_scope.insert(name.clone()) { + items.push(namespace_item(name, namespace)); + } + } + for (index, doc) in siblings.docs.iter().enumerate() { + let through = import_check::through(&bindings, &doc.namespace); for name in type_names(doc) { if in_scope.contains(&name) || imported.contains(&(index, name.clone())) { continue; } - items.push(unimported_item(doc, &name, source, |detail| { - type_item(doc, &name, name.clone(), detail) - })); + match &through { + Some(qualifier) => items.push(qualified_item(doc, &name, qualifier)), + None => items.push(unimported_item(doc, &name, source, |detail| { + type_item(doc, &name, name.clone(), detail) + })), + } } } items } +/// A name a whole-namespace `use` brought in, to qualify with: accepting it +/// writes `name::` and asks for what is under it. +fn namespace_item(name: &str, namespace: &[String]) -> CompletionItem { + CompletionItem { + label: name.to_string(), + kind: Some(CompletionItemKind::MODULE), + detail: Some(format!("`{}`, to qualify with", namespace.join("::"))), + insert_text: Some(format!("{name}::")), + command: Some(use_path::trigger_suggest()), + ..Default::default() + } +} + +/// A type of `doc` that no `use` here imports, but that a namespace one did +/// bring in reaches: written through it (`types::User`), which needs no new +/// `use`. Sorted like an unimported one. +fn qualified_item(doc: &ProjectDoc, name: &str, qualifier: &str) -> CompletionItem { + let mut item = type_item( + doc, + name, + format!("{qualifier}::{name}"), + format!("from {} — through `{qualifier}`", origin(doc)), + ); + item.filter_text = Some(name.to_string()); + item.sort_text = Some(match doc.dependency { + Some(_) => format!("~~{name}"), + None => format!("~{name}"), + }); + item +} + /// The `use`/`import` declarations in `source`, each parsed on its own /// line, so they're there even while the rest of the file doesn't parse. fn uses_in(source: &str, uri: &Url) -> Vec { diff --git a/src/handlers/completion/tests.rs b/src/handlers/completion/tests.rs index c4891f3..2421726 100644 --- a/src/handlers/completion/tests.rs +++ b/src/handlers/completion/tests.rs @@ -1130,3 +1130,208 @@ fn an_unimported_own_type_adds_a_package_path_and_a_dependencys_type_its_own_nam assert_eq!(edit("Error").as_deref(), Some("use package::api::common::Error\n\n")); assert_eq!(edit("Thing").as_deref(), Some("use shared::models::Thing\n\n")); } + +// ---- qualified references (ComlineProject/language-server#124, core#290) ---- + +use lsp_types::CompletionTextEdit; + +/// A package of its own schemas (`types`, a directory `billing`), a dependency +/// and std, and the completions at the end of `source`, written in +/// `pkg/src/`. +fn type_completions_in(file: &str, source: &str) -> Vec { + completions_in_package(file, source, source.len()) +} + +/// [`type_completions_in`], with the cursor at byte `offset` of `source`. +fn completions_in_package(file: &str, source: &str, offset: usize) -> Vec { + use crate::analysis::source::SourceFile; + let uri = Url::parse(&format!("file:///pkg/src/{file}")).unwrap(); + let mut files = vec![ + SourceFile::local( + Url::parse("file:///pkg/config.idp").unwrap(), + "congregation app\nspecification_version = 1\n\ndependencies = {\n shared = {\n path = \"../shared\"\n }\n}\n".to_string(), + ), + SourceFile::local( + Url::parse("file:///pkg/src/types.ids").unwrap(), + TYPES.to_string(), + ), + SourceFile::local( + Url::parse("file:///pkg/src/billing/api.ids").unwrap(), + "struct Invoice {\n id: u64\n}\n".to_string(), + ), + SourceFile::local( + Url::parse("file:///pkg/src/billing/ledger.ids").unwrap(), + "struct Entry {\n id: u64\n}\n".to_string(), + ), + SourceFile::of_dependency( + Url::parse("file:///shared/src/models.ids").unwrap(), + "struct Thing {\n id: u64\n}\n".to_string(), + "shared", + vec!["shared".to_string(), "models".to_string()], + ), + ]; + files.extend(crate::analysis::stdlib::files(&crate::analysis::stdlib::root())); + // The file being edited isn't one of the other files. + files.retain(|file| file.uri != uri); + let at = crate::util::offset_to_position(source, offset); + get_completions_with_project(source, &uri, at, &files) +} + +fn type_completions(source: &str) -> Vec { + type_completions_in("chat.ids", source) +} + +fn labels_of(items: &[CompletionItem]) -> Vec<&str> { + items.iter().map(|c| c.label.as_str()).collect() +} + +#[test] +fn after_a_whole_namespace_use_the_namespace_completes_its_members() { + let items = type_completions("use package::types\n\nstruct S {\n a: types::"); + // The types it declares, not its protocol, nor a primitive. + assert_eq!(labels_of(&items), ["Message", "Kind"]); + let partial = type_completions("use package::types\n\nstruct S {\n a: types::Me"); + assert_eq!(labels_of(&partial), ["Message", "Kind"], "the client filters by what's typed"); +} + +#[test] +fn a_bare_name_of_a_whole_namespace_is_not_offered_but_the_namespace_is() { + let items = type_completions("use package::types\n\nstruct S {\n a: "); + let labels = labels_of(&items); + assert!(!labels.contains(&"Message"), "no bare name from `types`: {labels:?}"); + assert!(!labels.contains(&"Kind"), "{labels:?}"); + + // The name the `use` brought in, to go on with. + let types = items.iter().find(|c| c.label == "types").expect("the namespace"); + assert_eq!(types.kind, Some(CompletionItemKind::MODULE)); + assert_eq!(types.insert_text.as_deref(), Some("types::")); + assert_eq!( + types.command.as_ref().map(|c| c.command.as_str()), + Some("editor.action.triggerSuggest") + ); + + // And its types, written through it: no `use` to add. + let message = items.iter().find(|c| c.label == "types::Message").expect("through `types`"); + assert_eq!(message.filter_text.as_deref(), Some("Message")); + assert!(message.additional_text_edits.is_none(), "{message:?}"); +} + +#[test] +fn a_type_position_offers_the_names_in_scope_and_the_roots() { + let items = type_completions("use package::types::Message as Msg\n\nstruct Local {\n x: u8\n}\n\nstruct S {\n a: "); + let labels = labels_of(&items); + // Declared here, an alias, a primitive. + for expected in ["Local", "Msg", "u8"] { + assert!(labels.contains(&expected), "{expected}: {labels:?}"); + } + // The roots, which go on with `::`. + for root in ["package", "parent", "self", "std", "shared"] { + let item = items + .iter() + .find(|c| c.label == root) + .unwrap_or_else(|| panic!("{root}: {labels:?}")); + match &item.text_edit { + Some(CompletionTextEdit::Edit(edit)) => assert_eq!(edit.new_text, format!("{root}::")), + other => panic!("{root}: {other:?}"), + } + assert_eq!( + item.command.as_ref().map(|c| c.command.as_str()), + Some("editor.action.triggerSuggest") + ); + } + // Sorted after what is in scope. + let sort = |label: &str| { + items + .iter() + .find(|c| c.label == label) + .and_then(|c| c.sort_text.clone().or_else(|| Some(c.label.clone()))) + .unwrap() + }; + assert!(sort("Msg") < sort("package"), "{} {}", sort("Msg"), sort("package")); +} + +#[test] +fn a_root_completes_the_next_segment() { + let items = type_completions("struct S {\n a: package::"); + assert_eq!(labels_of(&items), ["billing", "types"], "the package's own, no dependency"); + // A schema is gone through, so it asks for what is in it. + let types = items.iter().find(|c| c.label == "types").unwrap(); + match &types.text_edit { + Some(CompletionTextEdit::Edit(edit)) => assert_eq!(edit.new_text, "types::"), + other => panic!("{other:?}"), + } + + let items = type_completions("struct S {\n a: package::billing::"); + assert_eq!(labels_of(&items), ["api", "ledger"]); + let items = type_completions("struct S {\n a: package::billing::api::"); + assert_eq!(labels_of(&items), ["Invoice"]); + let items = type_completions("struct S {\n a: package::types::"); + assert_eq!(labels_of(&items), ["Message", "Kind"]); +} + +#[test] +fn a_dependency_and_std_complete_too() { + let items = type_completions("struct S {\n a: shared::"); + assert_eq!(labels_of(&items), ["models"]); + let items = type_completions("struct S {\n a: shared::models::"); + assert_eq!(labels_of(&items), ["Thing"]); + + let items = type_completions("struct S {\n a: std::http::"); + assert!(items.iter().any(|c| c.label == "Request"), "{:?}", labels_of(&items)); + assert!(items.iter().any(|c| c.label == "HttpMethod"), "{:?}", labels_of(&items)); +} + +#[test] +fn a_name_through_a_namespace_use_or_an_alias_completes_what_is_under_it() { + // The directory, then what is under it. + let items = type_completions("use package::billing\n\nstruct S {\n a: billing::"); + assert_eq!(labels_of(&items), ["api", "ledger"]); + let items = type_completions("use package::billing\n\nstruct S {\n a: billing::api::"); + assert_eq!(labels_of(&items), ["Invoice"]); + + // An alias stands for the namespace; the namespace's own name doesn't. + let source = "use package::billing::api as b\n\nstruct S {\n a: b::"; + assert_eq!(labels_of(&type_completions(source)), ["Invoice"]); + let source = "use package::billing::api as b\n\nstruct S {\n a: api::"; + assert!(type_completions(source).is_empty(), "`api` isn't in scope"); +} + +#[test] +fn a_name_that_is_no_qualifier_completes_nothing() { + // A declaration of the file, or a name imported, has no `::` to go on with. + let source = "struct Local {\n x: u8\n}\n\nstruct S {\n a: Local::"; + assert!(type_completions(source).is_empty()); + let source = "use package::types::Message\n\nstruct S {\n a: Message::"; + assert!(type_completions(source).is_empty()); + assert!(type_completions("struct S {\n a: nothing::").is_empty()); +} + +#[test] +fn parent_and_self_stay_in_the_package() { + let items = type_completions_in("billing/ledger.ids", "struct S {\n a: parent::"); + assert_eq!(labels_of(&items), ["api"], "the other schema of the directory"); + // The file's own declarations, read without the line being typed. + let source = "struct Own {\n id: u64\n}\n\nstruct S {\n a: self::\n}\n"; + let at = source.find("self::").unwrap() + "self::".len(); + let items = completions_in_package("billing/ledger.ids", source, at); + assert_eq!(labels_of(&items), ["Own", "S"]); +} + +#[test] +fn an_auto_import_writes_an_item_use_never_a_namespace_use() { + let items = type_completions("struct S {\n a: "); + let mut seen = 0; + for item in &items { + let Some(edits) = &item.additional_text_edits else { + continue; + }; + seen += 1; + let written = edits[0].new_text.trim_end(); + assert!( + written.starts_with("use ") && written.ends_with(&format!("::{}", item.label)), + "`{}` adds `{written}`", + item.label + ); + } + assert!(seen > 0, "something is offered to import"); +} diff --git a/src/handlers/completion/type_path.rs b/src/handlers/completion/type_path.rs new file mode 100644 index 0000000..cf37a96 --- /dev/null +++ b/src/handlers/completion/type_path.rs @@ -0,0 +1,206 @@ +//! Completion in a type position along a path (core#290): `types::` after +//! `use package::types`, `package::billing::`, `b::` after +//! `use package::billing::api as b`, `std::`. +//! +//! A qualified type starts with a name in scope or a root: `package`, `parent`, +//! `self`, `std` or a dependency's name (see [`crate::analysis::names`]). What +//! follows the `::` is what is under that namespace: its modules, and the types +//! its schema declares (a path in a type position ends at a type, not at a +//! module). The first segment is read the way core reads it: a name a +//! whole-namespace `use` of this file brought in comes before a root, then a +//! root, and nothing else (a declaration of this file, or a name a `use` +//! imports, is no qualifier). +//! +//! Also the roots themselves, offered along with the names in scope where a +//! type is expected, which complete to `root::` and ask for what comes next. + +use lsp_types::{CompletionItem, CompletionTextEdit, Range, TextEdit, Url}; + +use crate::analysis::imports; +use crate::analysis::modules::{self, ChildModule, DeclKind}; +use crate::analysis::names; +use crate::analysis::project::Project; +use crate::analysis::source::ProjectSource; +use crate::analysis::symbols; +use crate::handlers::completion::use_path; +use crate::parser; +use crate::util::{byte_range_to_lsp_range, is_ident_char, is_identifier}; + +/// The path written before the word being typed in a type position. +#[derive(Debug, PartialEq)] +pub(super) struct TypePath { + /// The segments before the one being typed (`types` for `types::Us`). + pub segments: Vec, + /// Byte offset where the segment being typed starts. + pub partial_start: usize, +} + +/// Where the word ending at `offset` starts. +pub(super) fn word_start(source: &str, offset: usize) -> usize { + source[..offset] + .char_indices() + .rev() + .take_while(|(_, c)| is_ident_char(*c)) + .last() + .map_or(offset, |(i, _)| i) +} + +/// The path before the word being typed at `offset`, when there is one: +/// `types::` and `types::Us` both say `["types"]`. `None` where the word has no +/// `::` in front of it. +pub(super) fn path_before(source: &str, offset: usize) -> Option { + let partial_start = word_start(source, offset); + let mut segments = vec![]; + let mut rest = &source[..partial_start]; + while let Some(before) = rest.strip_suffix("::") { + let start = before + .char_indices() + .rev() + .find(|(_, c)| !is_ident_char(*c)) + .map_or(0, |(i, c)| i + c.len_utf8()); + let segment = &before[start..]; + if !is_identifier(segment) { + return None; + } + segments.insert(0, segment.to_string()); + rest = &before[..start]; + } + (!segments.is_empty()).then_some(TypePath { + segments, + partial_start, + }) +} + +/// What can come next in `path`, written in the file at `uri` (its text +/// `source`, the cursor at `offset`), with `other_files` the rest of its +/// package. +pub(super) fn completions( + path: &TypePath, + source: &str, + uri: &Url, + offset: usize, + other_files: &[S], +) -> Vec { + let project = Project::new(other_files.iter()); + let range = byte_range_to_lsp_range(source, path.partial_start, offset); + let first = &path.segments[0]; + + // A name a whole-namespace `use` here brought in comes first, as it does + // in core's scope; then a root. + let bindings = names::namespace_bindings(&project, &super::uses_in(source, uri)); + let (base, relative) = match bindings.iter().find(|(name, _)| name == first) { + Some((_, namespace)) => ( + namespace + .iter() + .chain(&path.segments[1..]) + .cloned() + .collect::>(), + false, + ), + None => { + let relative = modules::is_relative_prefix(first); + if !relative && !use_path::leaves_the_package(&project, first) { + return vec![]; + } + match modules::resolve_path(&path.segments, false, uri) { + Some(base) => (base, relative), + None => return vec![], // `parent::` above the top + } + } + }; + + // A relative path stays inside the package. + let schema = project + .docs + .iter() + .find(|d| d.namespace == base && (!relative || d.dependency.is_none())); + let mut items: Vec = modules::children(&project, &base, relative) + .iter() + .map(|child| module_item(child, range)) + .collect(); + if let Some(schema) = schema { + items.extend( + modules::declarations(schema) + .iter() + .filter(|declared| { + matches!( + declared.kind, + DeclKind::Struct | DeclKind::Enum | DeclKind::Type + ) + }) + .map(|declared| use_path::declared_item(declared, range)), + ); + } + // The file itself (`self::`, or its own path) isn't among the other files: + // what it declares is read from it without the line being typed, which is + // what keeps it from parsing. + if base == imports::namespace_of(uri) { + let blanked = super::blank_line_at(source, offset); + if let Some(document) = parser::parse_document(&blanked) { + let table = symbols::build_symbol_table(&document, uri, &blanked); + items.extend(super::get_type_completions(Some(&table), &blanked)); + } + } + items +} + +/// A module one level down. A path in a type position ends at a type, so a +/// schema is gone through as a directory is: `::` is inserted too. +fn module_item(child: &ChildModule, range: Range) -> CompletionItem { + let mut item = use_path::child_item(child, range); + if child.schema { + item.text_edit = Some(CompletionTextEdit::Edit(TextEdit { + range, + new_text: format!("{}::", child.name), + })); + item.command = Some(use_path::trigger_suggest()); + } + item +} + +/// The roots a path in a type position can start with, after the names in +/// scope: the package's dependencies, `self`, `parent`, `package` and `std`. +pub(super) fn roots( + source: &str, + offset: usize, + other_files: &[S], +) -> Vec { + let project = Project::new(other_files.iter()); + let range = byte_range_to_lsp_range(source, word_start(source, offset), offset); + use_path::path_starts(&project, false, range) + .into_iter() + .map(|mut item| { + item.sort_text = Some(format!("~0{}", item.label)); + item + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_the_path_before_the_word() { + let at = |source: &str| path_before(source, source.len()); + let path = |segments: &[&str], partial_start| { + Some(TypePath { + segments: segments.iter().map(|s| s.to_string()).collect(), + partial_start, + }) + }; + assert_eq!(at("struct S {\n a: types::"), path(&["types"], 25)); + assert_eq!(at("struct S {\n a: types::Us"), path(&["types"], 25)); + assert_eq!( + at("struct S {\n a: package::billing::"), + path(&["package", "billing"], 36) + ); + assert_eq!(at("struct S {\n a: Us"), None); + assert_eq!(at("struct S {\n a: "), None); + assert_eq!( + at("struct S {\n a: u8[]::"), + None, + "no path before a bracket" + ); + } +} diff --git a/src/handlers/completion/use_path.rs b/src/handlers/completion/use_path.rs index 9aca53c..d3ec982 100644 --- a/src/handlers/completion/use_path.rs +++ b/src/handlers/completion/use_path.rs @@ -227,7 +227,7 @@ pub(super) fn completions( /// Whether an absolute path starting with `first` goes into `std` or a /// dependency, rather than naming one of the package's own schemas. -fn leaves_the_package(project: &Project, first: &str) -> bool { +pub(super) fn leaves_the_package(project: &Project, first: &str) -> bool { first == stdlib::NAME || project.dependencies.iter().any(|d| d.name == first) || project.docs.iter().any(|d| d.dependency == Some(first)) @@ -238,7 +238,7 @@ fn leaves_the_package(project: &Project, first: &str) -> bool { /// are reached through `package::`, `parent::` or `self::`, so a bare one would /// be an error (`import::bare-own-schema-path`). The legacy `import` has no /// such prefixes and still lists them. -fn path_starts(project: &Project, legacy: bool, range: Range) -> Vec { +pub(super) fn path_starts(project: &Project, legacy: bool, range: Range) -> Vec { let mut items: Vec = match legacy { true => modules::children(project, &[], true) .iter() @@ -307,7 +307,7 @@ fn path_starts(project: &Project, legacy: bool, range: Range) -> Vec CompletionItem { +pub(super) fn child_item(child: &ChildModule, range: Range) -> CompletionItem { let name = &child.name; let mut detail = match child.schema { true => format!("`{name}.ids`"), @@ -328,7 +328,7 @@ fn child_item(child: &ChildModule, range: Range) -> CompletionItem { } /// A declaration a `use` can import, with its docstring. -fn declared_item(declared: &Declared, range: Range) -> CompletionItem { +pub(super) fn declared_item(declared: &Declared, range: Range) -> CompletionItem { let kind = match declared.kind { DeclKind::Struct => CompletionItemKind::STRUCT, DeclKind::Enum => CompletionItemKind::ENUM, diff --git a/src/handlers/definition.rs b/src/handlers/definition.rs index b9c34fb..33f0f1b 100644 --- a/src/handlers/definition.rs +++ b/src/handlers/definition.rs @@ -513,4 +513,80 @@ struct User { }; assert_eq!(location.uri.as_str(), "comline-std:/validators.ids"); } + + // ---- qualified references (ComlineProject/language-server#124, core#290) ---- + + /// Go-to-definition at the last `needle` in `source`, written in + /// `pkg/src/chat.ids`, among the documented package, std and a directory + /// `billing` with `api`. + fn qualified_definition(source: &str, needle: &str) -> Option { + let mut files = documented_package(); + files.push(local( + "src/billing/api.ids", + "struct Invoice {\n id: u64\n}\n", + )); + let uri = Url::parse("file:///pkg/src/chat.ids").unwrap(); + let offset = source.rfind(needle).unwrap(); + let position = crate::util::offset_to_position(source, offset); + match find_definition_with_project(source, &uri, position, &files)? { + GotoDefinitionResponse::Scalar(location) => Some(location), + other => panic!("expected a scalar location, got {other:?}"), + } + } + + #[test] + fn every_form_of_a_type_goes_to_its_declaration() { + for source in [ + "use package::billing::api::Invoice\n\nstruct S {\n i: Invoice\n}\n", + "use package::billing\n\nstruct S {\n i: billing::api::Invoice\n}\n", + "use package::billing::api\n\nstruct S {\n i: api::Invoice\n}\n", + "use package::billing::api as b\n\nstruct S {\n i: b::Invoice\n}\n", + "struct S {\n i: package::billing::api::Invoice\n}\n", + ] { + let location = qualified_definition(source, "Invoice") + .unwrap_or_else(|| panic!("no definition: {source}")); + assert_eq!(location.uri.as_str(), "file:///pkg/src/billing/api.ids", "{source}"); + assert_eq!(location.range.start.line, 0, "{source}"); + } + } + + #[test] + fn a_namespace_use_and_a_full_path_from_a_root_reach_the_declaration() { + let location = qualified_definition( + "use package::types\n\nstruct S {\n u: types::User\n}\n", + "User", + ) + .expect("through the namespace"); + assert_eq!(location.uri.as_str(), "file:///pkg/src/types.ids"); + + let location = qualified_definition( + "struct S {\n r: std::http::Request\n}\n", + "Request", + ) + .expect("a full path into std"); + assert_eq!(location.uri.as_str(), "comline-std:/http.ids"); + } + + #[test] + fn a_bare_name_after_only_a_whole_namespace_use_goes_nowhere() { + let source = "use package::types\n\nstruct S {\n u: User\n}\n"; + assert!(qualified_definition(source, "User").is_none()); + } + + #[test] + fn a_qualifier_goes_to_the_schema_it_stands_for() { + let location = qualified_definition( + "use package::types\n\nstruct S {\n u: types::User\n}\n", + "types::User", + ) + .expect("the schema `types`"); + assert_eq!(location.uri.as_str(), "file:///pkg/src/types.ids"); + + let location = qualified_definition( + "use package::billing::api as b\n\nstruct S {\n i: b::Invoice\n}\n", + "b::Invoice", + ) + .expect("what the alias stands for"); + assert_eq!(location.uri.as_str(), "file:///pkg/src/billing/api.ids"); + } } diff --git a/src/handlers/hovers/ids/mod.rs b/src/handlers/hovers/ids/mod.rs index b6cd95f..bfdde2b 100644 --- a/src/handlers/hovers/ids/mod.rs +++ b/src/handlers/hovers/ids/mod.rs @@ -3,8 +3,9 @@ #[cfg(test)] mod tests; -use crate::analysis::imports::{self, ProjectFile}; +use crate::analysis::imports::{self, ResolvedUse}; use crate::analysis::modules::{self, Module}; +use crate::analysis::names::Names; use crate::analysis::project::Project; use crate::analysis::source::{self, ProjectSource}; use crate::analysis::symbols; @@ -21,6 +22,7 @@ use comline_core::schema::idl::grammar::{ use comline_core::schema::idl::module_docs::summary; use comline_core::schema::idl::size::{self, Range, SizeLookup, SizeTarget, Sizes}; use comline_core::schema::idl::vocabulary; +use comline_core::schema::ir::compiler::import_resolver::ResolvedImport; use comline_core::encodings::{official, EncodingKind}; use comline_core::schema::ir::compiler::interpreter::incremental::IncrementalInterpreter; use comline_core::schema::ir::compiler::Compile; @@ -128,19 +130,25 @@ pub fn get_hover_info_with_project( }) .collect(); - // Only the files this one's `use`s reach - and the files *their* `use`s - // reach, since a struct's size follows its fields into wherever they are - // imported from. A file nothing imports isn't in scope here, so nothing - // in it is either: not for the hover, not for a size estimate. + // Only the files this one's `use`s, and the type paths it writes, reach - + // and the files *theirs* reach, since a struct's size follows its fields + // into wherever they are imported from. A file nothing reaches isn't in + // scope here, so nothing in it is either: not for the hover, not for a size + // estimate. + let project = Project::with_active(uri, source, other_files); + let names = Names::of(&project); let own_namespace = imports::namespace_of(uri); - let own_imports = imports::resolved_imports(&document, &own_namespace); + let mut own_imports = imports::resolved_imports(&document, &own_namespace); + own_imports.extend(written_reach(&project, &names, &own_namespace)); let mut reached = vec![false; parsed.len()]; let mut pending = vec![imports::resolved_imports(&document, &own_namespace)]; + pending.push(written_reach(&project, &names, &own_namespace)); while let Some(uses) = pending.pop() { for (i, ((_, _, sibling), namespace)) in parsed.iter().enumerate() { if !reached[i] && imports_reach(&uses, namespace) { reached[i] = true; pending.push(imports::resolved_imports(sibling, namespace)); + pending.push(written_reach(&project, &names, namespace)); } } } @@ -152,7 +160,7 @@ pub fn get_hover_info_with_project( // each group is otherwise unchanged), so a name collision with a file // only reached through another's `use` resolves to the nearer one. parsed.sort_by_key(|(_, namespace)| !imports_reach(&own_imports, namespace)); - let (other_docs, origins): (Vec, Vec) = parsed.into_iter().unzip(); + let (other_docs, _): (Vec, Vec) = parsed.into_iter().unzip(); let lookup = HoverSizeLookup { local: &document, @@ -162,47 +170,32 @@ pub fn get_hover_info_with_project( let symbol_table = symbols::build_symbol_table(&document, uri, source); - // A symbol in this file. The cursor sitting on a declaration's own - // name resolves by position; anything else is a reference, resolved in - // whatever namespace its position allows. + // The declaration the cursor sits on, by position. // // By name alone, a file declaring `struct Test` beside a // `settings Test` answered both with whichever the symbol table had // kept - so hovering the struct showed the settings block. - let own = symbol_table - .at_offset(offset) - .or_else(|| symbol_table.get_in(word, symbols::namespace_at(source, offset))); - if let Some(symbol) = own { + if let Some(symbol) = symbol_table.at_offset(offset) { return Some(create_symbol_hover(symbol, &document, &lookup)); } - let siblings: Vec = other_docs - .iter() - .zip(&origins) - .map(|((u, s, _), namespace)| ProjectFile { - uri: u, - source: s, - namespace: namespace.clone(), - }) - .collect(); - - if let Some(resolved) = imports::resolve_symbol(word, &own_imports, &siblings) { - let sibling = resolved.file; - if let Some((_, _, other_document)) = - other_docs.iter().find(|(u, _, _)| **u == *sibling.uri) - { - let other_table = - symbols::build_symbol_table(other_document, sibling.uri, sibling.source); - // Look up `real_name`, not `word` — they differ when `word` - // is an `as` alias, which `sibling`'s own symbol table never - // heard of (see `ResolvedSymbol::real_name`'s doc). - // A `use` only ever brings in a declaration - there is no - // import that names a settings preset. - if let Some(symbol) = - other_table.get_in(&resolved.real_name, symbols::Namespace::Declaration) - { - return Some(create_symbol_hover(symbol, other_document, &lookup)); - } + // A reference: whatever the file's own scope names there (core#290) - a + // declaration of this file, a name a `use` brought in (through its `as` + // alias too: the declaration's own name is what's shown), a path through a + // namespace it brought in (`types::User`, `b::Invoice`, + // `billing::api::Invoice`), or a full path from a root + // (`package::types::User`, `std::http::Request`). Resolved in whatever + // namespace its position allows, as go-to-definition does. + let target = project + .index_of(uri) + .and_then(|active| project.target_at_cursor(active, source, offset)); + if let Some(target) = target { + if let Some(symbol) = project.symbol(&target) { + return Some(create_symbol_hover( + symbol, + &project.docs[target.doc].document, + &lookup, + )); } } @@ -219,6 +212,28 @@ pub fn get_hover_info_with_project( None } +/// The schemas the file seen under `namespace` writes a type path into, as +/// `use`s of them: a full path needs no `use`, and a size follows it. +fn written_reach(project: &Project, names: &Names, namespace: &[String]) -> Vec { + let Some(doc) = project.docs.iter().position(|d| d.namespace == namespace) else { + return vec![]; + }; + names + .written_namespaces(doc) + .into_iter() + .map(|absolute_namespace| ResolvedUse { + resolved: ResolvedImport { + absolute_namespace, + schema_path: None, + symbols: vec![], + alias: None, + }, + alias: None, + span: (0, 0), + }) + .collect() +} + /// Whether one of `uses` points into the file seen under `namespace`. fn imports_reach(uses: &[imports::ResolvedUse], namespace: &[String]) -> bool { !namespace.is_empty() diff --git a/src/handlers/hovers/ids/tests.rs b/src/handlers/hovers/ids/tests.rs index fb3c55d..a68015d 100644 --- a/src/handlers/hovers/ids/tests.rs +++ b/src/handlers/hovers/ids/tests.rs @@ -1106,4 +1106,79 @@ protocol Chat { text.contains("Checks a string's length is within a minimum and a maximum."), "got: {text}" ); - } \ No newline at end of file + } + // ---- qualified references (ComlineProject/language-server#124, core#290) ---- + + /// The documented package, and a directory `billing` with `api`. + fn qualified_package() -> Vec { + let mut files = documented_package(); + files.push(local( + "src/billing/api.ids", + "//! What is owed.\n\n/// A bill.\nstruct Invoice {\n id: u64\n}\n", + )); + files + } + + /// The hover at the last `needle` in `source`, written in `pkg/src/chat.ids`. + fn hover_over(source: &str, needle: &str) -> Option { + let uri = Url::parse("file:///pkg/src/chat.ids").unwrap(); + let offset = source.rfind(needle).unwrap(); + let position = crate::util::offset_to_position(source, offset); + get_hover_info_with_project(source, &uri, position, &qualified_package()).map(hover_text) + } + + #[test] + fn every_form_of_a_type_hovers_on_its_declaration() { + for source in [ + "use package::billing::api::Invoice\n\nstruct S {\n i: Invoice\n}\n", + "use package::billing\n\nstruct S {\n i: billing::api::Invoice\n}\n", + "use package::billing::api\n\nstruct S {\n i: api::Invoice\n}\n", + "use package::billing::api as b\n\nstruct S {\n i: b::Invoice\n}\n", + "struct S {\n i: package::billing::api::Invoice\n}\n", + ] { + let text = hover_over(source, "Invoice").unwrap_or_else(|| panic!("no hover: {source}")); + assert!(text.contains("struct Invoice"), "{source}: {text}"); + assert!(text.contains("A bill."), "{source}: {text}"); + } + } + + #[test] + fn a_full_path_into_std_and_a_namespace_from_std_hover_on_the_declaration() { + let text = hover_over("struct S {\n r: std::http::Request\n}\n", "Request").unwrap(); + assert!(text.contains("struct Request"), "{text}"); + let text = hover_over( + "use std::http\n\nstruct S {\n r: http::Request\n}\n", + "Request", + ) + .unwrap(); + assert!(text.contains("struct Request"), "{text}"); + } + + #[test] + fn a_bare_name_after_only_a_whole_namespace_use_hovers_on_nothing() { + let source = "use package::types\n\nstruct S {\n u: User\n}\n"; + assert_eq!(hover_over(source, "User"), None, "`User` isn't in scope"); + let source = "use package::types\n\nstruct S {\n u: types::User\n}\n"; + let text = hover_over(source, "User").unwrap(); + assert!(text.contains("struct User"), "{text}"); + } + + #[test] + fn a_name_in_scope_that_shadows_nothing_hovers_on_what_the_path_names() { + // The file declares its own `Invoice`; the qualified one is billing's. + let source = "struct Invoice {\n mine: bool\n}\n\nstruct S {\n a: package::billing::api::Invoice\n b: Invoice\n}\n"; + let text = hover_over(source, "Invoice\n b").unwrap(); + assert!(text.contains("A bill."), "billing's: {text}"); + let text = hover_over(source, "Invoice\n}").unwrap(); + assert!(!text.contains("A bill."), "the local one: {text}"); + } + + #[test] + fn a_name_that_a_use_brought_in_is_a_module_to_hover_on() { + let source = "use package::types\n\nstruct S {\n u: types::User\n}\n"; + let text = hover_over(source, "types::User").unwrap(); + assert!(text.contains("module types"), "{text}"); + let source = "use package::billing::api as b\n\nstruct S {\n i: b::Invoice\n}\n"; + let text = hover_over(source, "b::Invoice").unwrap(); + assert!(text.contains("module billing::api"), "the alias stands for it: {text}"); + }