Skip to content

Commit

Permalink
added support for model validation in Pysa VSCode Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
m0mosenpai committed Jun 9, 2021
1 parent e5b2b74 commit 20ad506
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions client/commands/v2/pysa_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
version of persistent.py.
"""

from pathlib import Path
import asyncio
import logging

Expand All @@ -32,6 +33,9 @@
InitializationSuccess,
InitializationFailure,
)
from api import query, connection as api_connection
from api.connection import PyreQueryError
from typing import List, Sequence, Dict

LOG: logging.Logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -60,6 +64,51 @@ def __init__(
self.binary_location = binary_location
self.server_identifier = server_identifier

def invalid_model_to_diagnostic(
self,
invalid_model: query.InvalidModel,
) -> lsp.Diagnostic:
return lsp.Diagnostic(
range=lsp.Range(
start=lsp.Position(
line=invalid_model.line - 1, character=invalid_model.column
),
end=lsp.Position(
line=invalid_model.stop_line - 1,
character=invalid_model.stop_column,
),
),
message=invalid_model.full_error_message,
severity=lsp.DiagnosticSeverity.ERROR,
code=None,
source="Pysa",
)

def invalid_models_to_diagnostics(
self,
invalid_models: Sequence[query.InvalidModel],
) -> Dict[Path, List[lsp.Diagnostic]]:
result: Dict[Path, List[lsp.Diagnostic]] = {}
for model in invalid_models:
result.setdefault(Path(model.path), []).append(
self.invalid_model_to_diagnostic(model)
)
return result

async def update_errors(self, document_path) -> None:
await _publish_diagnostics(self.output_channel, document_path, [])
pyre_connection = api_connection.PyreConnection(
Path(self.pyre_arguments.global_root)
)
try:
model_errors = query.get_invalid_taint_models(pyre_connection)
diagnostics = self.invalid_models_to_diagnostics(model_errors)
await self.show_model_errors_to_client(diagnostics)
except PyreQueryError as e:
await self.log_and_show_message_to_client(
f"Error querying Pyre: {e}", lsp.MessageType.WARNING
)

async def show_message_to_client(
self, message: str, level: lsp.MessageType = lsp.MessageType.INFO
) -> None:
Expand Down Expand Up @@ -106,6 +155,7 @@ async def process_open_request(
raise json_rpc.InvalidRequestError(
f"Document URI is not a file: {parameters.text_document.uri}"
)
await self.update_errors(document_path)

async def process_close_request(
self, parameters: lsp.DidCloseTextDocumentParameters
Expand All @@ -129,6 +179,7 @@ async def process_did_save_request(
raise json_rpc.InvalidRequestError(
f"Document URI is not a file: {parameters.text_document.uri}"
)
await self.update_errors(document_path)

async def run(self) -> int:
while True:
Expand Down

0 comments on commit 20ad506

Please sign in to comment.