Skip to content

Commit b21dc34

Browse files
committed
✨ Add lsp-public-functions printer
1 parent ae08813 commit b21dc34

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

wake_printers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .lsp_inheritance_graph import LspInheritanceGraphPrinter
99
from .lsp_linearized_inheritance_graph import LspLinearizedInheritanceGraphPrinter
1010
from .lsp_openzeppelin_docs import LspOpenzeppelinDocsPrinter
11+
from .lsp_public_functions import LspPublicFunctionsPrinter
1112
from .lsp_references import LspReferencesPrinter
1213
from .lsp_selectors import LspSelectorsPrinter
1314
from .lsp_yul_definitions import LspYulDefinitionsPrinter

wake_printers/lsp_public_functions.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from __future__ import annotations
2+
3+
from functools import partial
4+
5+
import networkx as nx
6+
import rich_click as click
7+
import wake.ir as ir
8+
import wake.ir.types as types
9+
from rich import print
10+
from wake.cli import SolidityName
11+
from wake.printers import Printer, printer
12+
13+
14+
class LspPublicFunctionsPrinter(Printer):
15+
execution_mode = "lsp"
16+
17+
pure: bool
18+
view: bool
19+
20+
def print(self) -> None:
21+
pass
22+
23+
def _on_click(self, node: ir.ContractDefinition) -> None:
24+
from wake.core.lsp_provider import GoToLocationsCommand
25+
26+
functions = {}
27+
for c in node.linearized_base_contracts:
28+
for f in c.functions:
29+
if f.kind in {ir.enums.FunctionKind.RECEIVE, ir.enums.FunctionKind.FALLBACK} and f.kind not in functions:
30+
functions[f.kind] = f
31+
elif f.function_selector is not None and f.function_selector not in functions:
32+
functions[f.function_selector] = f
33+
34+
filtered = [
35+
f for f in functions.values()
36+
if f.state_mutability in {ir.enums.StateMutability.NONPAYABLE, ir.enums.StateMutability.PAYABLE} or
37+
f.state_mutability == ir.enums.StateMutability.PURE and self.pure or
38+
f.state_mutability == ir.enums.StateMutability.VIEW and self.view
39+
]
40+
41+
self.lsp_provider.add_commands([GoToLocationsCommand.from_nodes(
42+
node,
43+
filtered,
44+
"peek",
45+
"No public functions",
46+
)])
47+
48+
def visit_contract_definition(self, node: ir.ContractDefinition):
49+
self.lsp_provider.add_code_lens(node, "Public functions", on_click=partial(self._on_click, node))
50+
51+
@printer.command(name="lsp-public-functions")
52+
@click.option(
53+
"--pure/--no-pure",
54+
default=False,
55+
help="Include pure functions",
56+
)
57+
@click.option(
58+
"--view/--no-view",
59+
default=False,
60+
help="Include view functions",
61+
)
62+
def cli(self, pure: bool, view: bool) -> None:
63+
self.pure = pure
64+
self.view = view

0 commit comments

Comments
 (0)