Skip to content

Diagnostics

Problem reporting and language server features.

Languages

Languages(client: VSCodeClient)

Language-related operations for VS Code.

Source code in vscode_sockpuppet/diagnostics.py
def __init__(self, client: "VSCodeClient"):
    self.client = client
    self._collections: dict[str, DiagnosticCollection] = {}

create_diagnostic_collection

create_diagnostic_collection(name: str = 'default') -> DiagnosticCollection

Create a diagnostic collection.

Parameters:

Name Type Description Default
name str

Name of the collection (default: 'default')

'default'

Returns:

Type Description
DiagnosticCollection

A new diagnostic collection

Source code in vscode_sockpuppet/diagnostics.py
def create_diagnostic_collection(self, name: str = "default") -> DiagnosticCollection:
    """
    Create a diagnostic collection.

    Args:
        name: Name of the collection (default: 'default')

    Returns:
        A new diagnostic collection
    """
    if name in self._collections:
        return self._collections[name]

    self.client._send_request("languages.createDiagnosticCollection", {"name": name})
    collection = DiagnosticCollection(self.client, name)
    self._collections[name] = collection
    return collection

Diagnostic

Diagnostic

Diagnostic(range: dict, message: str, severity: Union[int, str] = DiagnosticSeverity.Error, source: Optional[str] = None, code: Optional[Union[str, int]] = None)

Represents a diagnostic, such as a compiler error or warning.

Create a diagnostic.

Parameters:

Name Type Description Default
range dict

Range dict with start and end positions

required
message str

Diagnostic message

required
severity Union[int, str]

Severity level (Error, Warning, Information, Hint)

Error
source Optional[str]

Source of the diagnostic (e.g., 'python', 'mypy')

None
code Optional[Union[str, int]]

Diagnostic code

None
Source code in vscode_sockpuppet/diagnostics.py
def __init__(
    self,
    range: dict,
    message: str,
    severity: Union[int, str] = DiagnosticSeverity.Error,
    source: Optional[str] = None,
    code: Optional[Union[str, int]] = None,
):
    """
    Create a diagnostic.

    Args:
        range: Range dict with start and end positions
        message: Diagnostic message
        severity: Severity level (Error, Warning, Information, Hint)
        source: Source of the diagnostic (e.g., 'python', 'mypy')
        code: Diagnostic code
    """
    self.range = range
    self.message = message
    self.severity = severity
    self.source = source
    self.code = code
    self.related_information: List[DiagnosticRelatedInformation] = []
add_related_information(location: dict, message: str) -> Diagnostic

Add related information to this diagnostic.

Parameters:

Name Type Description Default
location dict

Location dict with uri and range

required
message str

Related message

required

Returns:

Type Description
Diagnostic

Self for chaining

Source code in vscode_sockpuppet/diagnostics.py
def add_related_information(self, location: dict, message: str) -> "Diagnostic":
    """
    Add related information to this diagnostic.

    Args:
        location: Location dict with uri and range
        message: Related message

    Returns:
        Self for chaining
    """
    self.related_information.append(DiagnosticRelatedInformation(location, message))
    return self

to_dict

to_dict() -> dict

Convert to dictionary for JSON serialization.

Source code in vscode_sockpuppet/diagnostics.py
def to_dict(self) -> dict:
    """Convert to dictionary for JSON serialization."""
    result = {
        "range": self.range,
        "message": self.message,
        "severity": self.severity,
    }
    if self.source:
        result["source"] = self.source
    if self.code is not None:
        result["code"] = self.code
    if self.related_information:
        result["relatedInformation"] = [info.to_dict() for info in self.related_information]
    return result

DiagnosticSeverity

DiagnosticSeverity

Diagnostic severity levels.

DiagnosticCollection

DiagnosticCollection

DiagnosticCollection(client: VSCodeClient, name: str)

A collection of diagnostics for a specific source.

Create a diagnostic collection.

Parameters:

Name Type Description Default
client VSCodeClient

The VS Code client

required
name str

Name of the collection

required
Source code in vscode_sockpuppet/diagnostics.py
def __init__(self, client: "VSCodeClient", name: str):
    """
    Create a diagnostic collection.

    Args:
        client: The VS Code client
        name: Name of the collection
    """
    self.client = client
    self.name = name

set

set(uri: str, diagnostics: Optional[List[Diagnostic]] = None) -> None

Set diagnostics for a URI.

Parameters:

Name Type Description Default
uri str

The URI to set diagnostics for

required
diagnostics Optional[List[Diagnostic]]

List of diagnostics (None or [] clears)

None
Source code in vscode_sockpuppet/diagnostics.py
def set(self, uri: str, diagnostics: Optional[List[Diagnostic]] = None) -> None:
    """
    Set diagnostics for a URI.

    Args:
        uri: The URI to set diagnostics for
        diagnostics: List of diagnostics (None or [] clears)
    """
    diag_list = [d.to_dict() for d in diagnostics] if diagnostics else []
    self.client._send_request(
        "languages.setDiagnostics",
        {"name": self.name, "uri": uri, "diagnostics": diag_list},
    )

delete

delete(uri: str) -> None

Delete diagnostics for a URI.

Parameters:

Name Type Description Default
uri str

The URI to clear diagnostics for

required
Source code in vscode_sockpuppet/diagnostics.py
def delete(self, uri: str) -> None:
    """
    Delete diagnostics for a URI.

    Args:
        uri: The URI to clear diagnostics for
    """
    self.client._send_request("languages.clearDiagnostics", {"name": self.name, "uri": uri})

clear

clear() -> None

Clear all diagnostics in this collection.

Source code in vscode_sockpuppet/diagnostics.py
def clear(self) -> None:
    """Clear all diagnostics in this collection."""
    self.client._send_request("languages.clearDiagnostics", {"name": self.name})

dispose

dispose() -> None

Dispose this diagnostic collection.

Source code in vscode_sockpuppet/diagnostics.py
def dispose(self) -> None:
    """Dispose this diagnostic collection."""
    self.client._send_request("languages.disposeDiagnosticCollection", {"name": self.name})

DiagnosticRelatedInformation

DiagnosticRelatedInformation

DiagnosticRelatedInformation(location: dict, message: str)

Related information for a diagnostic.

Create diagnostic related information.

Parameters:

Name Type Description Default
location dict

Location dict with uri and range

required
message str

Related message

required
Source code in vscode_sockpuppet/diagnostics.py
def __init__(self, location: dict, message: str):
    """
    Create diagnostic related information.

    Args:
        location: Location dict with uri and range
        message: Related message
    """
    self.location = location
    self.message = message

to_dict

to_dict() -> dict

Convert to dictionary for JSON serialization.

Source code in vscode_sockpuppet/diagnostics.py
def to_dict(self) -> dict:
    """Convert to dictionary for JSON serialization."""
    return {
        "location": self.location,
        "message": self.message,
    }

Utility Functions

create_position

create_position(line: int, character: int) -> dict

Create a position object.

Parameters:

Name Type Description Default
line int

Line number (0-based)

required
character int

Character position (0-based)

required

Returns:

Type Description
dict

Position dictionary

Source code in vscode_sockpuppet/diagnostics.py
def create_position(line: int, character: int) -> dict:
    """
    Create a position object.

    Args:
        line: Line number (0-based)
        character: Character position (0-based)

    Returns:
        Position dictionary
    """
    return {"line": line, "character": character}

create_range

create_range(start_line: int, start_char: int, end_line: int, end_char: int) -> dict

Create a range object.

Parameters:

Name Type Description Default
start_line int

Start line (0-based)

required
start_char int

Start character (0-based)

required
end_line int

End line (0-based)

required
end_char int

End character (0-based)

required

Returns:

Type Description
dict

Range dictionary

Source code in vscode_sockpuppet/diagnostics.py
def create_range(start_line: int, start_char: int, end_line: int, end_char: int) -> dict:
    """
    Create a range object.

    Args:
        start_line: Start line (0-based)
        start_char: Start character (0-based)
        end_line: End line (0-based)
        end_char: End character (0-based)

    Returns:
        Range dictionary
    """
    return {
        "start": {"line": start_line, "character": start_char},
        "end": {"line": end_line, "character": end_char},
    }

create_location

create_location(uri: str, range: dict) -> dict

Create a location object.

Parameters:

Name Type Description Default
uri str

The URI of the location

required
range dict

The range in the document

required

Returns:

Type Description
dict

Location dictionary

Source code in vscode_sockpuppet/diagnostics.py
def create_location(uri: str, range: dict) -> dict:
    """
    Create a location object.

    Args:
        uri: The URI of the location
        range: The range in the document

    Returns:
        Location dictionary
    """
    return {"uri": uri, "range": range}