Skip to main content

code_moniker_workspace/source/
identity.rs

1use std::path::Path;
2
3use code_moniker_core::core::moniker::{Moniker, MonikerBuilder};
4use code_moniker_core::core::uri::{UriConfig, to_uri};
5
6use crate::snapshot::{ReferenceId, SourceId, SymbolId};
7
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub struct LocalIdentityResolver {
10	scheme: String,
11}
12
13impl LocalIdentityResolver {
14	pub fn new(scheme: impl Into<String>) -> Self {
15		Self {
16			scheme: scheme.into(),
17		}
18	}
19
20	pub fn scheme(&self) -> &str {
21		&self.scheme
22	}
23
24	pub fn source_id(&self, file_idx: usize, rel_path: &Path) -> SourceId {
25		let _ = rel_path;
26		SourceId::at(file_idx)
27	}
28
29	pub fn source_index(&self, id: &SourceId) -> Option<usize> {
30		Some(id.file())
31	}
32
33	pub fn source_uri(&self, rel_path: &Path) -> String {
34		let moniker = MonikerBuilder::new()
35			.project(b".")
36			.segment(b"file", rel_path.display().to_string().as_bytes())
37			.build();
38		self.moniker_uri(&moniker)
39	}
40
41	pub fn symbol_id(&self, file_idx: usize, def_idx: usize) -> SymbolId {
42		SymbolId::at(file_idx, def_idx)
43	}
44
45	pub fn symbol_location(&self, id: &SymbolId) -> Option<(usize, usize)> {
46		Some((id.file(), id.def()))
47	}
48
49	pub fn reference_id(&self, file_idx: usize, ref_idx: usize) -> ReferenceId {
50		ReferenceId::at(file_idx, ref_idx)
51	}
52
53	pub fn reference_location(&self, id: &ReferenceId) -> Option<(usize, usize)> {
54		Some((id.file(), id.reference()))
55	}
56
57	pub fn moniker_uri(&self, moniker: &Moniker) -> String {
58		to_uri(
59			moniker,
60			&UriConfig {
61				scheme: &self.scheme,
62			},
63		)
64	}
65}
66
67impl Default for LocalIdentityResolver {
68	fn default() -> Self {
69		Self::new(crate::DEFAULT_IDENTITY_SCHEME)
70	}
71}