Skip to content

Commit

Permalink
lsp: Add gather_results function
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
alcarney committed Dec 12, 2023
1 parent 8c7cd3e commit 066de3d
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lib/esbonio/esbonio/server/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 066de3d

Please sign in to comment.