From 066de3dfde0de65a2b2c11adc79634f809105e91 Mon Sep 17 00:00:00 2001 From: Alex Carney Date: Tue, 12 Dec 2023 23:49:47 +0000 Subject: [PATCH] lsp: Add `gather_results` function `gather_results` combines the results returned from all registered language features into a single list. While no longer necessary for diagnostic support (it was used in a previous implementation attempt). The `gather_results` function will be used for future methods so we may as well add it now it's been written. --- lib/esbonio/esbonio/server/setup.py | 32 +++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/esbonio/esbonio/server/setup.py b/lib/esbonio/esbonio/server/setup.py index ebc26d1ff..6a6fe7be2 100644 --- a/lib/esbonio/esbonio/server/setup.py +++ b/lib/esbonio/esbonio/server/setup.py @@ -4,6 +4,7 @@ import inspect import pathlib import typing +from typing import Any from typing import Dict from typing import Iterable from typing import List @@ -137,7 +138,7 @@ async def on_workspace_diagnostic( async def on_document_symbol( ls: EsbonioLanguageServer, params: types.DocumentSymbolParams ): - return await call_features_return_first(ls, "document_symbol", params) + return await return_first_result(ls, "document_symbol", params) @server.feature(types.WORKSPACE_DID_CHANGE_CONFIGURATION) async def on_did_change_configuration( @@ -174,9 +175,32 @@ async def call_features(ls: EsbonioLanguageServer, method: str, *args, **kwargs) ls.logger.error("Error in '%s.%s' handler", name, method, exc_info=True) -async def call_features_return_first( - ls: EsbonioLanguageServer, method: str, *args, **kwargs -): +async def gather_results(ls: EsbonioLanguageServer, method: str, *args, **kwargs): + """Call all features, gathering all results into a list.""" + results: List[Any] = [] + for cls, feature in ls: + try: + impl = getattr(feature, method) + + result = impl(*args, **kwargs) + if inspect.isawaitable(result): + items = await result + else: + items = result + + if isinstance(items, list): + results.extend(items) + elif items is not None: + results.append(items) + + except Exception: + name = f"{cls.__name__}" + ls.logger.error("Error in '%s.%s' handler", name, method, exc_info=True) + + return results + + +async def return_first_result(ls: EsbonioLanguageServer, method: str, *args, **kwargs): """Call all features, returning the first non ``None`` result we find.""" for cls, feature in ls: