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