@@ -150,33 +150,21 @@ def get_start_and_end_of_model_block(
150150 except StopIteration :
151151 return None
152152
153- # 3) Find the matching closing parenthesis for that list by tracking depth
154- depth = 0
155- rparen_idx : t .Optional [int ] = None
156- for i in range (lparen_idx , len (tokens )):
157- tt = tokens [i ].token_type
158- if tt is TokenType .L_PAREN :
159- depth += 1
160- elif tt is TokenType .R_PAREN :
161- depth -= 1
162- if depth == 0 :
163- rparen_idx = i
164- break
165-
166- if rparen_idx is None :
167- # Fallback: stop at the first semicolon after MODEL
168- try :
169- rparen_idx = next (
170- i
171- for i in range (lparen_idx + 1 , len (tokens ))
172- if tokens [i ].token_type is TokenType .SEMICOLON
173- )
174- except StopIteration :
175- return None
176- return (
177- lparen_idx ,
178- rparen_idx ,
179- )
153+ # 3) Find the matching closing parenthesis by looking for the first semicolon after
154+ # the opening parenthesis and assuming the MODEL block ends there.
155+ try :
156+ closing_semicolon = next (
157+ i
158+ for i in range (lparen_idx + 1 , len (tokens ))
159+ if tokens [i ].token_type is TokenType .SEMICOLON
160+ )
161+ # If we find a semicolon, we can assume the MODEL block ends there
162+ rparen_idx = closing_semicolon - 1
163+ if tokens [rparen_idx ].token_type is TokenType .R_PAREN :
164+ return (lparen_idx , rparen_idx )
165+ return None
166+ except StopIteration :
167+ return None
180168
181169
182170def get_range_of_model_block (
@@ -187,11 +175,9 @@ def get_range_of_model_block(
187175 Get the range of the model block in an SQL file,
188176 """
189177 tokens = tokenize (sql , dialect = dialect )
190-
191178 block = get_start_and_end_of_model_block (tokens )
192179 if not block :
193180 return None
194-
195181 (start_idx , end_idx ) = block
196182 start = tokens [start_idx - 1 ]
197183 end = tokens [end_idx + 1 ]
0 commit comments