Skip to content

Commit

Permalink
Move hover code to dedicated handler
Browse files Browse the repository at this point in the history
Delete AST Analyzer
  • Loading branch information
z80dev committed Dec 15, 2024
1 parent 349cabc commit b102a14
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
8 changes: 4 additions & 4 deletions tests/test_info.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from lsprotocol.types import Position, SignatureHelpParams, TextDocumentIdentifier
from pygls.workspace import Document
from vyper_lsp.ast import AST
from vyper_lsp.analyzer.AstAnalyzer import AstAnalyzer
from vyper_lsp.handlers.signatures import SignatureHandler
from vyper_lsp.handlers.hover import HoverHandler


def test_signature_help(ast: AST):
Expand Down Expand Up @@ -84,8 +84,8 @@ def baz():

pos = Position(line=10, character=11)

analyzer = AstAnalyzer(ast)
hover = analyzer.hover_info(doc, pos)
handler = HoverHandler(ast)
hover = handler.hover_info(doc, pos)
assert hover
assert (
hover
Expand All @@ -96,6 +96,6 @@ def baz():
)

pos = Position(line=18, character=11)
hover = analyzer.hover_info(doc, pos)
hover = handler.hover_info(doc, pos)
assert hover
assert hover == "(Internal Function) def noreturn(x: uint256):"
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from vyper_lsp.utils import (
get_expression_at_cursor,
get_word_at_cursor,
get_installed_vyper_version,
)

pattern_text = r"(.+) is deprecated\. Please use `(.+)` instead\."
Expand All @@ -22,14 +21,9 @@
logger = logging.getLogger("vyper-lsp")


class AstAnalyzer:
class HoverHandler:
def __init__(self, ast: AST) -> None:
super().__init__()
self.ast = ast
if get_installed_vyper_version() < min_vyper_version:
self.diagnostics_enabled = False
else:
self.diagnostics_enabled = True

def _format_fn_signature(self, node: nodes.FunctionDef) -> str:
pattern = r"def\s+(\w+)\((?:[^()]|\n)*\)(?:\s*->\s*[\w\[\], \n]+)?:"
Expand Down
10 changes: 3 additions & 7 deletions vyper_lsp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
)
from packaging.version import Version
from pygls.server import LanguageServer
from vyper_lsp.analyzer.AstAnalyzer import AstAnalyzer
from vyper_lsp.handlers.signatures import SignatureHandler
from vyper_lsp.handlers.completion import CompletionHandler
from vyper_lsp.handlers.hover import HoverHandler
from vyper_lsp.analyzer.SourceAnalyzer import SourceAnalyzer
from vyper_lsp.debounce import Debouncer

Expand All @@ -47,15 +47,11 @@
server = LanguageServer("vyper", "v0.0.1")
navigator = ASTNavigator(ast)

# AstAnalyzer is faster and better, but depends on the locally installed vyper version
# we should keep it around for now and use it when the contract version pragma is missing
# or if the version pragma matches the system version. its much faster so we can run it
# on every keystroke, with sourceanalyzer we should only run it on save
ast_analyzer = AstAnalyzer(ast)
completer = CompletionHandler(ast)
source_analyzer = SourceAnalyzer()

signature_handler = SignatureHandler(ast)
hover_handler = HoverHandler(ast)

debouncer = Debouncer(wait=0.5)

Expand Down Expand Up @@ -144,7 +140,7 @@ def find_references(ls: LanguageServer, params: ReferenceParams) -> List[Locatio
@server.feature(TEXT_DOCUMENT_HOVER)
def hover(ls: LanguageServer, params: HoverParams):
document = ls.workspace.get_text_document(params.text_document.uri)
hover_info = ast_analyzer.hover_info(document, params.position)
hover_info = hover_handler.hover_info(document, params.position)
if hover_info:
return Hover(contents=hover_info, range=None)

Expand Down

0 comments on commit b102a14

Please sign in to comment.