Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add terminal hyperlinks to diagnostic check name [1.1] #11

Open
wants to merge 1 commit into
base: v1.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions clangd_tidy/diagnostic_formatter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
import os
import pathlib
import re
from abc import ABC, abstractmethod
from typing import Dict, Iterable, List, Optional

from .lsp.messages import Diagnostic, DiagnosticSeverity
Expand Down Expand Up @@ -150,6 +150,10 @@ class ColorSeqTty:
MAGENTA = "\033[95m"
BOLD = "\033[1m"
ENDC = "\033[0m"
START_LINK = "\033]8;;"
END_LINK = "\033\\"

colors_enabled = True

class ColorSeqNoTty:
ERROR = ""
Expand All @@ -161,6 +165,10 @@ class ColorSeqNoTty:
MAGENTA = ""
BOLD = ""
ENDC = ""
START_LINK = ""
END_LINK = ""

colors_enabled = False

def __init__(self, enable_color: bool):
self.color_seq = self.ColorSeqTty if enable_color else self.ColorSeqNoTty
Expand All @@ -185,6 +193,18 @@ def note(self, message: str):
def format(self, message: str):
return f"{self.color_seq.MAGENTA}{message}{self.color_seq.ENDC}"

def link(self, message: str, url: str | None):
if url is None:
return message

if not self.color_seq.colors_enabled:
return message

return (
f"{self.color_seq.START_LINK}{url}{self.color_seq.END_LINK}"
f"{message}{self.color_seq.START_LINK}{self.color_seq.END_LINK}"
)

def __init__(self, extra_context: int, enable_color: bool):
self._extra_context = extra_context
self._colorizer = self.Colorizer(enable_color)
Expand Down Expand Up @@ -269,7 +289,12 @@ def _format_one_diagnostic(

if diagnostic.code is None:
return None
code = f"[{diagnostic.code}]"

code_url = None
if diagnostic.codeDescription:
code_url = diagnostic.codeDescription.href
format_code = self._colorizer.link(diagnostic.code, code_url)
code = f"[{format_code}]"

severity = (
self._colorized_severity(diagnostic.severity.value)
Expand Down
7 changes: 6 additions & 1 deletion clangd_tidy/lsp/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,18 @@ def __lt__(self, other) -> bool:
return NotImplemented


@define
class CodeDescription:
href: str


@define
class Diagnostic:
range: Range
message: str
severity: Optional[DiagnosticSeverity] = None
code: Any = None
codeDescription: Any = None
codeDescription: CodeDescription = None
source: Optional[str] = None
tags: Optional[List[Any]] = None
relatedInformation: Optional[List[Any]] = None
Expand Down
Loading