@@ -11,7 +11,10 @@ use dupe::Dupe;
1111use pyrefly_util:: arc_id:: ArcId ;
1212use pyrefly_util:: lined_buffer:: DisplayPos ;
1313use pyrefly_util:: lined_buffer:: DisplayRange ;
14+ use pyrefly_util:: lined_buffer:: LineNumber ;
1415use pyrefly_util:: lined_buffer:: LinedBuffer ;
16+ use ruff_notebook:: Notebook ;
17+ use ruff_source_file:: OneIndexed ;
1518use ruff_text_size:: TextRange ;
1619use ruff_text_size:: TextSize ;
1720
@@ -32,6 +35,7 @@ struct ModuleInner {
3235 ignore : Ignore ,
3336 is_generated : bool ,
3437 contents : LinedBuffer ,
38+ notebook : Option < Box < Notebook > > ,
3539}
3640
3741impl Module {
@@ -46,6 +50,22 @@ impl Module {
4650 ignore,
4751 is_generated,
4852 contents,
53+ notebook : None ,
54+ } ) )
55+ }
56+
57+ pub fn new_notebook ( name : ModuleName , path : ModulePath , notebook : Notebook ) -> Self {
58+ let contents: Arc < String > = Arc :: from ( notebook. source_code ( ) . to_owned ( ) ) ;
59+ let ignore = Ignore :: new ( & contents) ;
60+ let is_generated = contents. contains ( GENERATED_TOKEN ) ;
61+ let contents = LinedBuffer :: new ( contents) ;
62+ Self ( ArcId :: new ( ModuleInner {
63+ name,
64+ path,
65+ ignore,
66+ is_generated,
67+ contents,
68+ notebook : Some ( Box :: new ( notebook) ) ,
4969 } ) )
5070 }
5171
@@ -87,6 +107,10 @@ impl Module {
87107 & self . 0 . path
88108 }
89109
110+ pub fn is_notebook ( & self ) -> bool {
111+ self . 0 . notebook . is_some ( )
112+ }
113+
90114 pub fn is_generated ( & self ) -> bool {
91115 self . 0 . is_generated
92116 }
@@ -116,6 +140,10 @@ impl Module {
116140 pub fn ignore ( & self ) -> & Ignore {
117141 & self . 0 . ignore
118142 }
143+
144+ pub fn notebook ( & self ) -> Option < & Notebook > {
145+ self . 0 . notebook . as_deref ( )
146+ }
119147}
120148
121149#[ derive( Debug , Clone ) ]
@@ -129,3 +157,21 @@ impl TextRangeWithModule {
129157 Self { module, range }
130158 }
131159}
160+
161+ /// Given a one-indexed row and column in the concatenated source,
162+ /// return the cell number, and the row/column in that cell.
163+ pub fn map_notebook_position (
164+ notebook : & Notebook ,
165+ position : DisplayPos ,
166+ ) -> Option < ( OneIndexed , DisplayPos ) > {
167+ let index = notebook. index ( ) ;
168+ let cell = index. cell ( position. line . to_one_indexed ( ) ) ?;
169+ let cell_row = index
170+ . cell_row ( position. line . to_one_indexed ( ) )
171+ . unwrap_or ( OneIndexed :: MIN ) ;
172+ let display_pos = DisplayPos {
173+ line : LineNumber :: from_one_indexed ( cell_row) ,
174+ column : position. column ,
175+ } ;
176+ Some ( ( cell, display_pos) )
177+ }
0 commit comments