Skip to content

Commit

Permalink
fix: True LSP Diagnostic param, with version
Browse files Browse the repository at this point in the history
Fixes #211

Includes deprecation warning for old method signature
  • Loading branch information
tombh committed Dec 21, 2022
1 parent af3f318 commit e9f7271
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ venv.bak/

# mypy
.mypy_cache
.dmypy.json
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ and this project adheres to [Semantic Versioning][semver].
### Fixed

- Fix progress example in json extension. ([#230])
- Provide `version` param for publishing diagnostics ([#303])

[#230]: https://github.com/openlawlibrary/pygls/issues/230
[#303]: https://github.com/openlawlibrary/pygls/issues/303

## [1.0.0] - 2/12/2022
### Changed
Expand Down
54 changes: 50 additions & 4 deletions pygls/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,56 @@ def log_trace(self, message: str, verbose: Optional[str] = None) -> None:

self.notify(LOG_TRACE, params)

def publish_diagnostics(self, doc_uri: str, diagnostics: List[Diagnostic]) -> None:
"""Sends diagnostic notification to the client."""
self.notify(TEXT_DOCUMENT_PUBLISH_DIAGNOSTICS,
PublishDiagnosticsParams(uri=doc_uri, diagnostics=diagnostics))
def _publish_diagnostics_deprecator(
self,
params_or_uri: Union[str, PublishDiagnosticsParams],
diagnostics: Optional[List[Diagnostic]],
version: Optional[int],
**kwargs
) -> PublishDiagnosticsParams:
if isinstance(params_or_uri, str):
message = "DEPRECATION: "
"`publish_diagnostics("
"self, doc_uri: str, diagnostics: List[Diagnostic], version: Optional[int] = None)`"
"will be replaced with `publish_diagnostics(self, params: PublishDiagnosticsParams)`"
logging.warning(message)

if diagnostics is None:
diagnostics = []

args = {
**{
"uri": params_or_uri,
"diagnostics": diagnostics,
"version": version
},
**kwargs
}

params = PublishDiagnosticsParams(**args) # type:ignore
else:
params = params_or_uri
return params

def publish_diagnostics(
self,
params_or_uri: Union[str, PublishDiagnosticsParams],
diagnostics: Optional[List[Diagnostic]] = None,
version: Optional[int] = None,
**kwargs
):
"""
Sends diagnostic notification to the client.
Deprecation:
`uri`, `diagnostics` and `version` fields will be deprecated
"""
params = self._publish_diagnostics_deprecator(
params_or_uri,
diagnostics,
version,
**kwargs
)
self.notify(TEXT_DOCUMENT_PUBLISH_DIAGNOSTICS, params)

def register_capability(self, params: RegistrationParams,
callback: Optional[Callable[[], None]] = None) -> Future:
Expand Down
14 changes: 11 additions & 3 deletions pygls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,17 @@ def progress(self) -> Progress:
"""Gets the object to manage client's progress bar."""
return self.lsp.progress

def publish_diagnostics(self, doc_uri: str, diagnostics: List[Diagnostic]):
"""Sends diagnostic notification to the client."""
self.lsp.publish_diagnostics(doc_uri, diagnostics)
def publish_diagnostics(
self,
uri: str,
diagnostics: Optional[List[Diagnostic]] = None,
version: Optional[int] = None,
**kwargs
):
"""
Sends diagnostic notification to the client.
"""
self.lsp.publish_diagnostics(uri, diagnostics, version, **kwargs)

def register_capability(self, params: RegistrationParams,
callback: Optional[Callable[[], None]] = None) -> Future:
Expand Down
95 changes: 95 additions & 0 deletions tests/lsp/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
############################################################################
# Copyright(c) Open Law Library. All rights reserved. #
# See ThirdPartyNotices.txt in the project root for additional notices. #
# #
# Licensed under the Apache License, Version 2.0 (the "License") #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http: // www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################

from typing import List

import time
from lsprotocol.types import (
TEXT_DOCUMENT_PUBLISH_DIAGNOSTICS,
PublishDiagnosticsParams,
Diagnostic,
Range,
Position,
)
from ..conftest import ClientServer


class ConfiguredLS(ClientServer):
def __init__(self):
super().__init__()
self.client.notifications: List[PublishDiagnosticsParams] = []

@self.client.feature(TEXT_DOCUMENT_PUBLISH_DIAGNOSTICS)
def f1(params: PublishDiagnosticsParams):
self.client.notifications.append(params)


@ConfiguredLS.decorate()
def test_diagnostics_notifications(client_server):
client, server = client_server
diagnostic = Diagnostic(
range=Range(
start=Position(line=0, character=0),
end=Position(line=1, character=1),
),
message="test diagnostic",
)
server.lsp.publish_diagnostics(
PublishDiagnosticsParams(uri="", diagnostics=[diagnostic], version=1),
)
server.lsp.publish_diagnostics(
"",
diagnostics=[diagnostic],
version=1,
)

time.sleep(0.1)

assert len(client.notifications) == 2
expected = PublishDiagnosticsParams(
uri="",
diagnostics=[diagnostic],
version=1,
)
assert client.notifications[0] == expected


@ConfiguredLS.decorate()
def test_diagnostics_notifications_deprecated(client_server):
client, server = client_server
diagnostic = Diagnostic(
range=Range(
start=Position(line=0, character=0),
end=Position(line=1, character=1),
),
message="test diagnostic",
)
server.publish_diagnostics(
"",
diagnostics=[diagnostic],
version=1,
)

time.sleep(0.1)

assert len(client.notifications) == 1
expected = PublishDiagnosticsParams(
uri="",
diagnostics=[diagnostic],
version=1,
)
assert client.notifications[0] == expected

0 comments on commit e9f7271

Please sign in to comment.