sqlnbfmt 0.1.5__tar.gz → 0.1.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlnbfmt
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: A tool to format SQL code in Jupyter notebooks.
5
5
  Author-email: Qing <qingye779@gmail.com>
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sqlnbfmt"
3
- version = "0.1.5"
3
+ version = "0.1.7"
4
4
  description = "A tool to format SQL code in Jupyter notebooks."
5
5
  authors = [
6
6
  { name = "Qing", email = "qingye779@gmail.com" }
@@ -246,17 +246,18 @@ class SQLStringFormatter(ast.NodeTransformer):
246
246
  if in_function and '\n' in formatted_sql:
247
247
  # Apply Black-style indentation for function arguments
248
248
  lines = formatted_sql.split('\n')
249
- indented_lines = [lines[0]] + [
249
+ # Indent all lines including the first SQL line
250
+ indented_lines = [
250
251
  " " * 4 + line if line.strip() else line
251
- for line in lines[1:-1]
252
- ] + [" " * 4 + lines[-1] if lines[-1].strip() else lines[-1]]
252
+ for line in lines
253
+ ]
253
254
  formatted_sql = '\n'.join(indented_lines)
254
-
255
- # Reconstruct the string with appropriate quotes
256
- if '\n' in formatted_sql:
257
- formatted_str = f'"""\n{formatted_sql}\n"""'
255
+
256
+ # Reconstruct the string with appropriate quotes
257
+ formatted_str = f'"""\n{formatted_sql}\n{" " * 4}"""'
258
258
  else:
259
- formatted_str = f'"""{formatted_sql}"""'
259
+ # Ensure newlines are added around the formatted SQL
260
+ formatted_str = f'"""\n{formatted_sql}\n"""'
260
261
 
261
262
  formatted_node = ast.parse(formatted_str).body[0].value
262
263
  return formatted_node
@@ -279,9 +280,8 @@ class SQLStringFormatter(ast.NodeTransformer):
279
280
 
280
281
  if placeholders:
281
282
  for placeholder in placeholders.keys():
282
- if not placeholder: # Skip empty placeholders
283
+ if not placeholder:
283
284
  continue
284
- # Remove quotes around placeholders in formatted SQL
285
285
  formatted_sql = formatted_sql.replace(f"'{placeholder}'", placeholder)
286
286
  formatted_sql = formatted_sql.replace(f'"{placeholder}"', placeholder)
287
287
 
@@ -290,10 +290,11 @@ class SQLStringFormatter(ast.NodeTransformer):
290
290
  # Apply Black-style indentation for function arguments
291
291
  if in_function and '\n' in formatted_sql:
292
292
  lines = formatted_sql.split('\n')
293
- indented_lines = [lines[0]] + [
293
+ # Indent all lines including the first SQL line
294
+ indented_lines = [
294
295
  " " * 4 + line if line.strip() else line
295
- for line in lines[1:-1]
296
- ] + [" " * 4 + lines[-1] if lines[-1].strip() else lines[-1]]
296
+ for line in lines
297
+ ]
297
298
  formatted_sql = '\n'.join(indented_lines)
298
299
 
299
300
  # Reconstruct the f-string
@@ -307,7 +308,6 @@ class SQLStringFormatter(ast.NodeTransformer):
307
308
  match = pattern.search(formatted_sql, idx)
308
309
  if match:
309
310
  if match.start() > idx:
310
- # Add preceding text as Constant
311
311
  new_values.append(ast.Constant(value=formatted_sql[idx:match.start()]))
312
312
  placeholder = match.group()
313
313
  expr_str = placeholders[placeholder].strip()
@@ -323,23 +323,27 @@ class SQLStringFormatter(ast.NodeTransformer):
323
323
  ))
324
324
  idx = match.end()
325
325
  else:
326
- # Add the rest of the string as Constant
327
326
  new_values.append(ast.Constant(value=formatted_sql[idx:]))
328
327
  break
329
328
 
330
329
  new_node = ast.JoinedStr(values=new_values)
331
- # Wrap the f-string with adjusted triple quotes
332
330
  if '\n' in formatted_sql:
333
- # Multi-line f-string
334
- formatted_fstring = ast.JoinedStr(values=[
335
- ast.Constant(value='\n'),
336
- *new_node.values,
337
- ast.Constant(value='\n')
338
- ])
331
+ if in_function:
332
+ formatted_fstring = ast.JoinedStr(values=[
333
+ ast.Constant(value='\n'),
334
+ *new_node.values,
335
+ ast.Constant(value=f'\n{" " * 4}')
336
+ ])
337
+ else:
338
+ formatted_fstring = ast.JoinedStr(values=[
339
+ ast.Constant(value='\n'),
340
+ *new_node.values,
341
+ ast.Constant(value='\n')
342
+ ])
339
343
  else:
340
- # Single-line f-string
341
344
  formatted_fstring = new_node
342
345
  return formatted_fstring
346
+
343
347
  return None
344
348
  except SQLFormattingError as e:
345
349
  self.logger.warning(f"SQL formatting skipped: {e}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlnbfmt
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: A tool to format SQL code in Jupyter notebooks.
5
5
  Author-email: Qing <qingye779@gmail.com>
6
6
  License: MIT
File without changes
File without changes
File without changes
File without changes