Extract constants from Nix to a new crate

This commit is contained in:
oxalica 2023-01-24 23:35:17 +08:00
parent 6e5321582c
commit 9aa5ed132f
10 changed files with 37 additions and 13 deletions

6
Cargo.lock generated
View file

@ -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"

View file

@ -1,5 +1,11 @@
[workspace]
members = ["crates/*"]
members = [
"crates/builtin",
"crates/ide",
"crates/nil",
"crates/nix-interop",
"crates/syntax",
]
[profile.dev]
debug = 1

View file

@ -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"

View file

@ -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)
})
})

View file

@ -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();

View file

@ -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"

View file

@ -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<Vec<Diagnostic>> {
@ -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() {

View file

@ -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

View file

@ -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]

View file

@ -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";