Skip to content

Commit

Permalink
Fail more gracefully if clangd is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovank committed Feb 26, 2024
1 parent 9d1cdc7 commit 1fa6500
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/chatdbg/chatdbg_lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def find_definition(filename: str, lineno: int, character: int) -> str:
if "result" not in definition or not definition["result"]:
return "No definition found."

path = clangd_lsp_integration._uri_to_path(definition["result"][0]["uri"])
path = clangd_lsp_integration.uri_to_path(definition["result"][0]["uri"])
start_lineno = definition["result"][0]["range"]["start"]["line"] + 1
end_lineno = definition["result"][0]["range"]["end"]["line"] + 1
(lines, first) = llm_utils.read_lines(path, start_lineno - 5, end_lineno + 5)
Expand All @@ -495,7 +495,13 @@ def find_definition(filename: str, lineno: int, character: int) -> str:

assistant.add_function(lldb)
assistant.add_function(get_code_surrounding)
assistant.add_function(find_definition)

if not clangd_lsp_integration.is_available():
print("[WARNING] clangd is not available.")
print("[WARNING] The `find_definition` function will not be made available.")
else:
assistant.add_function(find_definition)

return assistant


Expand Down
15 changes: 14 additions & 1 deletion src/chatdbg/clangd_lsp_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _path_to_uri(path):
return "file://" + os.path.abspath(path)


def _uri_to_path(uri):
def uri_to_path(uri):
data = urllib.parse.urlparse(uri)

assert data.scheme == "file"
Expand All @@ -61,6 +61,19 @@ def _uri_to_path(uri):
return urllib.parse.unquote(path) # clangd seems to escape paths.


def is_available():
try:
clangd = subprocess.Popen(
["clangd", "--version"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
text=True,
)
return clangd.returncode == 0
except FileNotFoundError:
return False


class clangd:
def __init__(
self,
Expand Down

0 comments on commit 1fa6500

Please sign in to comment.