Skip to content

Commit

Permalink
Improve logging on syntax errors (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Nov 6, 2023
1 parent 7d8cd9b commit 87c6469
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
import sysconfig
import traceback
from typing import Any, Dict, List, Optional, Sequence, Union
from typing import Any, Dict, List, Optional, Sequence


# **********************************************************
Expand Down Expand Up @@ -111,10 +111,10 @@ def range_formatting(params: lsp.DocumentFormattingParams) -> list[lsp.TextEdit]
return _formatting_helper(document)


def is_python(code: str) -> bool:
def is_python(code: str, file_path: str) -> bool:
"""Ensures that the code provided is python."""
try:
ast.parse(code)
ast.parse(code, file_path)
except SyntaxError:
log_error(f"Syntax error in code: {traceback.format_exc()}")
return False
Expand Down Expand Up @@ -155,7 +155,7 @@ def _formatting_helper(document: workspace.Document) -> list[lsp.TextEdit] | Non
return None


def _get_filename_for_black(document: workspace.Document) -> Union[str, None]:
def _get_filename_for_black(document: workspace.Document) -> str:
"""Gets or generates a file name to use with black when formatting."""
if document.uri.startswith("vscode-notebook-cell") and document.path.endswith(
".ipynb"
Expand Down Expand Up @@ -383,8 +383,10 @@ def _run_tool_on_document(
log_warning(f"Skipping standard library file: {document.path}")
return None

if not is_python(document.source):
log_warning(f"Skipping non python code: {document.path}")
if not is_python(document.source, document.path):
log_warning(
f"Skipping non python code or code with syntax errors: {document.path}"
)
return None

# deep copy here to prevent accidentally updating global settings.
Expand Down

0 comments on commit 87c6469

Please sign in to comment.