From 9aa5ed132fc91b445bc40a85d664f5b3ccd77bb8 Mon Sep 17 00:00:00 2001 From: oxalica Date: Tue, 24 Jan 2023 23:35:17 +0800 Subject: [PATCH] Extract constants from Nix to a new crate --- Cargo.lock | 6 ++++++ Cargo.toml | 8 +++++++- crates/ide/Cargo.toml | 1 + crates/ide/src/def/mod.rs | 3 ++- crates/ide/src/tests.rs | 5 +++-- crates/nil/Cargo.toml | 1 + crates/nil/src/handler.rs | 6 +++--- crates/nil/src/vfs.rs | 8 ++------ crates/nix-interop/Cargo.toml | 8 ++++++++ crates/nix-interop/src/lib.rs | 4 ++++ 10 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 crates/nix-interop/Cargo.toml create mode 100644 crates/nix-interop/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 25089cf..62fb4cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,6 +119,7 @@ dependencies = [ "indexmap", "itertools", "la-arena", + "nix-interop", "ordered-float", "salsa", "smol_str", @@ -268,6 +269,7 @@ dependencies = [ "log", "lsp-server", "lsp-types", + "nix-interop", "serde", "serde_json", "text-size", @@ -275,6 +277,10 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "nix-interop" +version = "0.0.0" + [[package]] name = "num-traits" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index 92ff24f..ccb7b67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,11 @@ [workspace] -members = ["crates/*"] +members = [ + "crates/builtin", + "crates/ide", + "crates/nil", + "crates/nix-interop", + "crates/syntax", +] [profile.dev] debug = 1 diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml index dde2cc5..1299a99 100644 --- a/crates/ide/Cargo.toml +++ b/crates/ide/Cargo.toml @@ -12,6 +12,7 @@ either = "1.8.0" indexmap = "1.9.1" itertools = "0.10.5" la-arena = "0.2.1" +nix-interop = { path = "../nix-interop" } ordered-float = "3.4.0" salsa = "0.17.0-pre.2" smol_str = "0.1.23" diff --git a/crates/ide/src/def/mod.rs b/crates/ide/src/def/mod.rs index a0ca272..c0e35be 100644 --- a/crates/ide/src/def/mod.rs +++ b/crates/ide/src/def/mod.rs @@ -9,6 +9,7 @@ mod tests; use crate::base::SourceDatabase; use crate::{Diagnostic, FileId, SourceRootId, VfsPath}; use la_arena::{Arena, ArenaMap, Idx}; +use nix_interop::DEFAULT_IMPORT_FILE; use ordered_float::OrderedFloat; use smol_str::SmolStr; use std::collections::{HashMap, HashSet}; @@ -154,7 +155,7 @@ impl Module { }; let mut vpath = path.resolve(db)?; source_root.file_for_path(&vpath).or_else(|| { - vpath.push_segment("default.nix"); + vpath.push_segment(DEFAULT_IMPORT_FILE); source_root.file_for_path(&vpath) }) }) diff --git a/crates/ide/src/tests.rs b/crates/ide/src/tests.rs index be4d5a5..74ba00e 100644 --- a/crates/ide/src/tests.rs +++ b/crates/ide/src/tests.rs @@ -4,6 +4,7 @@ use crate::ty::TyDatabaseStorage; use crate::{Change, DefDatabase, FileId, FilePos, FileRange, FileSet, SourceRoot, VfsPath}; use anyhow::{ensure, Context, Result}; use indexmap::IndexMap; +use nix_interop::DEFAULT_IMPORT_FILE; use std::{mem, ops}; use syntax::ast::AstNode; use syntax::{NixLanguage, SyntaxNode, TextSize}; @@ -37,7 +38,7 @@ impl TestDB { change.change_file(file, text.to_owned().into()); } let entry = file_set - .file_for_path(&VfsPath::new("/default.nix").unwrap()) + .file_for_path(&VfsPath::new(format!("/{DEFAULT_IMPORT_FILE}")).unwrap()) .context("Missing entry file")?; change.set_roots(vec![SourceRoot::new_local(file_set, Some(entry))]); change.apply(&mut db); @@ -109,7 +110,7 @@ impl Fixture { } else { if cur_path.is_none() { missing_header = true; - cur_path = Some(VfsPath::new("/default.nix").unwrap()); + cur_path = Some(VfsPath::new(format!("/{DEFAULT_IMPORT_FILE}")).unwrap()); } let mut iter = line.chars().peekable(); diff --git a/crates/nil/Cargo.toml b/crates/nil/Cargo.toml index 4069550..aff3315 100644 --- a/crates/nil/Cargo.toml +++ b/crates/nil/Cargo.toml @@ -13,6 +13,7 @@ indexmap = "1.9.1" log = "0.4.17" lsp-server = "0.7.0" lsp-types = "0.93.0" +nix-interop = { path = "../nix-interop" } serde = "1.0.140" serde_json = "1.0.82" text-size = "1.1.0" diff --git a/crates/nil/src/handler.rs b/crates/nil/src/handler.rs index 05ee81d..688875b 100644 --- a/crates/nil/src/handler.rs +++ b/crates/nil/src/handler.rs @@ -10,12 +10,12 @@ use lsp_types::{ SemanticTokensParams, SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, TextDocumentPositionParams, TextEdit, Url, WorkspaceEdit, }; +use nix_interop::DEFAULT_IMPORT_FILE; use std::path::Path; use std::process; use std::sync::Arc; use text_size::TextRange; -const DEFAULT_CHILD: &str = "default.nix"; const MAX_DIAGNOSTICS_CNT: usize = 128; pub(crate) fn diagnostics(snap: StateSnapshot, uri: &Url) -> Result> { @@ -37,7 +37,7 @@ pub(crate) fn goto_definition( None => return Ok(None), Some(GotoDefinitionResult::Path(vpath)) => { let path = Path::new(vpath.as_str()); - let default_child = path.join(DEFAULT_CHILD); + let default_child = path.join(DEFAULT_IMPORT_FILE); let target_path = if path.is_file() { path } else if default_child.is_file() { @@ -280,7 +280,7 @@ pub(crate) fn document_links( // FIXME: Duplicated with `goto_definition`. LinkTarget::VfsPath(vpath) => { let path = Path::new(vpath.as_str()); - let default_child = path.join(DEFAULT_CHILD); + let default_child = path.join(DEFAULT_IMPORT_FILE); let target_path = if path.is_file() { path } else if default_child.is_file() { diff --git a/crates/nil/src/vfs.rs b/crates/nil/src/vfs.rs index 4cedd12..38acb44 100644 --- a/crates/nil/src/vfs.rs +++ b/crates/nil/src/vfs.rs @@ -117,14 +117,10 @@ impl Vfs { pub fn take_change(&mut self) -> Change { let mut change = mem::take(&mut self.change); if mem::take(&mut self.root_changed) { - // TODO: Configurable. - let entry = ["/flake.nix", "/default.nix"].iter().find_map(|&path| { - let path = VfsPath::new(path).unwrap(); - self.local_file_set.file_for_path(&path) - }); change.set_roots(vec![SourceRoot::new_local( self.local_file_set.clone(), - entry, + // TODO: Entry. + None, )]); } change diff --git a/crates/nix-interop/Cargo.toml b/crates/nix-interop/Cargo.toml new file mode 100644 index 0000000..bc21bd6 --- /dev/null +++ b/crates/nix-interop/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "nix-interop" +version = "0.0.0" +edition = "2021" +license = "MIT OR Apache-2.0" +rust-version = "1.66" + +[dependencies] diff --git a/crates/nix-interop/src/lib.rs b/crates/nix-interop/src/lib.rs new file mode 100644 index 0000000..835a4e7 --- /dev/null +++ b/crates/nix-interop/src/lib.rs @@ -0,0 +1,4 @@ +//! Nix defined file structures and interoperation with Nix. +pub const DEFAULT_IMPORT_FILE: &str = "default.nix"; +pub const FLAKE_FILE: &str = "flake.nix"; +pub const FLAKE_LOCK_FILE: &str = "flake.lock";