@@ -2,13 +2,8 @@ pub mod source;
22
33pub use source:: { Source , SourceKind } ;
44
5- use std:: path:: { Path , PathBuf } ;
6-
7- use crate :: ast:: AstConverter ;
8- use crate :: buffer:: Buffer ;
95use crate :: interners:: Interners ;
106use crate :: loader:: { EnvironmentLoader , LoadError } ;
11- use crate :: node;
127
138/// Owning the interners here gives a single `Environment` value the same
149/// role as the Ruby implementation's global name pool: names interned while
@@ -34,56 +29,12 @@ impl Environment {
3429 & self . sources
3530 }
3631
37- /// Parses `content` with this environment's own [`Interners`] and adds it
38- /// as a source, without going through an [`EnvironmentLoader`] — the
39- /// entry point for editor / in-memory callers (`RBS::Environment#add_source`
40- /// equivalent, e.g. Steep). Parsing always uses this environment's
41- /// `Interners`, so a `Source` added this way can never carry ids from a
42- /// different environment.
43- pub fn add_rbs (
44- & mut self ,
45- path : PathBuf ,
46- content : String ,
47- kind : SourceKind ,
48- ) -> Result < ( ) , LoadError > {
49- let signature = node:: parse ( & content) . map_err ( |message| LoadError :: Parse {
50- path : path. clone ( ) ,
51- message,
52- } ) ?;
53-
54- let mut converter =
55- AstConverter :: new ( & mut self . interners . strings , & mut self . interners . type_names ) ;
56- let directives = signature
57- . directives ( )
58- . iter ( )
59- . map ( |node| converter. convert_directive ( & node) )
60- . collect ( ) ;
61- let declarations = signature
62- . declarations ( )
63- . iter ( )
64- . map ( |node| converter. convert_declaration ( & node) )
65- . collect ( ) ;
66- // SignatureNode borrows `content` and has a Drop impl; drop it
67- // explicitly before moving `content` into the Buffer.
68- drop ( signature) ;
69-
70- self . sources . push ( Source {
71- buffer : Buffer :: new ( path, content) ,
72- directives,
73- declarations,
74- kind,
75- } ) ;
76- Ok ( ( ) )
32+ pub ( crate ) fn interners_mut ( & mut self ) -> & mut Interners {
33+ & mut self . interners
7734 }
7835
79- /// Reads `path` and adds it as a source; a thin wrapper around
80- /// [`Environment::add_rbs`].
81- pub fn add_rbs_file ( & mut self , path : & Path , kind : SourceKind ) -> Result < ( ) , LoadError > {
82- let content = std:: fs:: read_to_string ( path) . map_err ( |source| LoadError :: Io {
83- path : path. to_path_buf ( ) ,
84- source,
85- } ) ?;
86- self . add_rbs ( path. to_path_buf ( ) , content, kind)
36+ pub ( crate ) fn add_source ( & mut self , source : Source ) {
37+ self . sources . push ( source) ;
8738 }
8839
8940 pub fn from_loader ( loader : & EnvironmentLoader ) -> Result < Environment , LoadError > {
@@ -102,27 +53,21 @@ impl Default for Environment {
10253#[ cfg( test) ]
10354mod tests {
10455 use super :: * ;
105- use crate :: ast:: Declaration ;
10656
10757 #[ test]
10858 fn environment_owns_interners ( ) {
10959 let mut env = Environment :: new ( ) ;
110- env. add_rbs (
111- PathBuf :: from ( "test.rbs" ) ,
112- "class Foo\n end\n " . to_string ( ) ,
113- SourceKind :: Dir {
114- path : PathBuf :: from ( "." ) ,
115- } ,
116- )
117- . unwrap ( ) ;
11860
119- let [ Declaration :: Class ( class) ] = env. sources ( ) [ 0 ] . declarations . as_slice ( ) else {
120- panic ! ( "expected one class declaration" ) ;
121- } ;
61+ let interners = env. interners_mut ( ) ;
62+ let symbol = interners. strings . intern ( "Foo" ) ;
63+ let root = interners. type_names . absolute_root ( ) ;
64+ let name = interners. type_names . append ( root, symbol) ;
65+
12266 let interners = env. interners ( ) ;
12367 assert_eq ! (
124- interners. type_names. display( class . name, & interners. strings) ,
125- "Foo"
68+ interners. type_names. display( name, & interners. strings) ,
69+ ":: Foo"
12670 ) ;
71+ assert ! ( env. sources( ) . is_empty( ) ) ;
12772 }
12873}
0 commit comments