66 SelectCommand , CreateCommand , IdentifierFromFile , Segment , Marker , MoveClause , DeleteClause , \
77 InsertClause , ReplaceClause , EditingAction , BodyOrWhole , RegionClause , MarkerType
88from cedarscript_ast_parser .cedarscript_ast_parser import MarkerCompatible , RelativeMarker , \
9- RelativePositionType
9+ RelativePositionType , Region , SingleFileClause
1010from text_manipulation import (
1111 IndentationInfo , IdentifierBoundaries , RangeSpec , read_file , write_file , bow_to_search_range
1212)
1313
14- from .tree_sitter_identifier_finder import IdentifierFinder , find_identifier
14+ from .tree_sitter_identifier_finder import IdentifierFinder
1515
1616
1717class CEDARScriptEditorException (Exception ):
@@ -105,26 +105,21 @@ def _update_command(self, cmd: UpdateCommand):
105105 src = read_file (file_path )
106106 lines = src .splitlines ()
107107
108- source_info : tuple [str | bytes , str | Sequence [str ]] = (file_path , src )
109-
110- identifier_finder = find_identifier (source_info )
111-
112- # TODO test...
108+ identifier_finder = IdentifierFinder (file_path , src , RangeSpec .EMPTY )
113109
110+ search_range = RangeSpec .EMPTY
114111 match action :
115112 case MoveClause ():
116- # (Check parse_update_command)
117- # when action=MoveClause example (MOVE roll TO AFTER score):
118- # action.deleteclause.region=WHOLE
119- # action.as_marker = action.insertclause.as_marker
120- # action.insertclause.insert_position=FUNCTION(score)
121- # target.as_marker = FUNCTION(roll) (the one to delete)
122- search_range = RangeSpec .EMPTY
123- move_src_range = restrict_search_range (action , target , identifier_finder , lines )
113+ # READ + DELETE region : action.region (PARENT RESTRICTION: target.as_marker)
114+ move_src_range = restrict_search_range (action .region , target , identifier_finder , lines )
115+ # WRITE region: action.insert_position
116+ search_range = restrict_search_range (action .insert_position , None , identifier_finder , lines )
124117 case _:
125118 move_src_range = None
126119 # Set range_spec to cover the identifier
127- search_range = restrict_search_range (action , target , identifier_finder , lines )
120+ match action :
121+ case RegionClause (region = region ):
122+ search_range = restrict_search_range (action .region , target , identifier_finder , lines )
128123
129124 # UPDATE FUNCTION "_check_raw_id_fields_item"
130125 # FROM FILE "refactor-benchmark/checks_BaseModelAdminChecks__check_raw_id_fields_item/checks.py"
@@ -134,11 +129,17 @@ def _update_command(self, cmd: UpdateCommand):
134129 # ''';
135130 # target = IdentifierFromFile(file_path='refactor-benchmark/checks_BaseModelAdminChecks__check_raw_id_fields_item/checks.py', identifier_type=<MarkerType.FUNCTION: 'function'>, name='_check_raw_id_fields_item', where_clause=None, offset=None)
136131 # action = ReplaceClause(region=Marker(type=<MarkerType.LINE: line>, value=def _check_raw_id_fields_item(self, obj, field_name, label):, offset=None))
137- if search_range .line_count and not isinstance (action .region if hasattr (action , 'region' ) else None , Segment ):
138- marker , search_range = find_marker_or_segment (action , lines , search_range )
139- search_range = restrict_search_range_for_marker (
140- marker , action , lines , search_range , identifier_finder
141- )
132+ if search_range .line_count :
133+ match action :
134+ case RegionClause (region = Segment ()):
135+ pass
136+ case RegionClause (region = Marker ()) if action .region .type in [MarkerType .FUNCTION , MarkerType .METHOD , MarkerType .CLASS ]:
137+ pass
138+ case _:
139+ marker , search_range = find_marker_or_segment (action , lines , search_range )
140+ search_range = restrict_search_range_for_marker (
141+ marker , action , lines , search_range , identifier_finder
142+ )
142143
143144 match content :
144145 case str () | [str (), * _] | (str (), * _):
@@ -263,7 +264,7 @@ def find_index_range_for_region(region: BodyOrWhole | Marker | Segment | Relativ
263264 pass
264265 case _:
265266 # TODO transform to RangeSpec
266- mos = find_identifier (( "TODO?.py" , lines )) (mos ).body
267+ mos = IdentifierFinder ( "TODO?.py" , lines , RangeSpec . EMPTY ) (mos , search_range ).body
267268 index_range = mos .to_search_range (
268269 lines ,
269270 search_range .start if search_range else 0 ,
@@ -296,28 +297,38 @@ def find_marker_or_segment(
296297 return marker , search_range
297298
298299
299- def restrict_search_range (action , target , identifier_finder : IdentifierFinder , lines : Sequence [str ]) -> RangeSpec :
300- match target :
301- case IdentifierFromFile () as identifier_from_file :
302- identifier_marker = identifier_from_file .as_marker
303- identifier_boundaries = identifier_finder (identifier_marker )
304- if not identifier_boundaries :
305- raise ValueError (f"'{ identifier_marker } ' not found" )
306- match action :
307- case RegionClause (region = region ):
308- match region :
309- case BodyOrWhole () | RelativePositionType ():
310- return identifier_boundaries .location_to_search_range (region )
311- case Marker () as inner_marker :
312- match identifier_finder (inner_marker , identifier_boundaries .whole ):
313- case IdentifierBoundaries () as inner_boundaries :
314- return inner_boundaries .whole
315- case RangeSpec () as inner_range_spec :
316- return inner_range_spec
317- case Segment () as segment :
318- return segment .to_search_range (lines , identifier_boundaries .whole )
319- case _ as invalid :
320- raise ValueError (f'Unsupported region type: { type (invalid )} ' )
300+ def restrict_search_range (
301+ region : Region , parent_restriction : any ,
302+ identifier_finder : IdentifierFinder , lines : Sequence [str ]
303+ ) -> RangeSpec :
304+ identifier_boundaries = None
305+ match parent_restriction :
306+ case IdentifierFromFile ():
307+ identifier_boundaries = identifier_finder (parent_restriction .as_marker )
308+ match region :
309+ case BodyOrWhole () | RelativePositionType ():
310+ match parent_restriction :
311+ case IdentifierFromFile ():
312+ match identifier_boundaries :
313+ case None :
314+ raise ValueError (f"'{ parent_restriction } ' not found" )
315+ case SingleFileClause ():
316+ return RangeSpec .EMPTY
317+ case None :
318+ raise ValueError (f"'{ region } ' requires parent_restriction" )
319+ case _:
320+ raise ValueError (f"'{ region } ' isn't compatible with { parent_restriction } " )
321+ return identifier_boundaries .location_to_search_range (region )
322+ case Marker () as inner_marker :
323+ match identifier_finder (inner_marker , identifier_boundaries .whole if identifier_boundaries is not None else None ):
324+ case IdentifierBoundaries () as inner_boundaries :
325+ return inner_boundaries .location_to_search_range (BodyOrWhole .WHOLE )
326+ case RangeSpec () as inner_range_spec :
327+ return inner_range_spec
328+ case Segment () as segment :
329+ return segment .to_search_range (lines , identifier_boundaries .whole if identifier_boundaries is not None else None )
330+ case _ as invalid :
331+ raise ValueError (f'Unsupported region type: { type (invalid )} ' )
321332 return RangeSpec .EMPTY
322333
323334
0 commit comments