Skip to content

Commit

Permalink
Get tests passing with refactored parser
Browse files Browse the repository at this point in the history
  • Loading branch information
aazuspan committed Aug 19, 2024
1 parent d53bd91 commit c67ed82
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
11 changes: 10 additions & 1 deletion src/spinasm_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,16 @@ async def completions(
"""Returns completion items."""
parser = await ls.get_parser(params.text_document.uri)

symbol_completions = [token.completion_item for token in parser.evaluated_tokens]
# Get completions for all unique tokens (by their stxt) in the document
seen_tokens = set()
symbol_completions = []
for token in parser.evaluated_tokens:
# Temporary fix until we can get completions for all tokens at once.
if token.type not in ("LABEL", "TARGET"):
continue
if token.stxt not in seen_tokens:
symbol_completions.append(token.completion_item)
seen_tokens.add(token.stxt)

# TODO: If possible, get this from the completion item itself. This will require
# tokens to be able to query documentation.
Expand Down
2 changes: 1 addition & 1 deletion tests/server_tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def test_completions(test_case: CompletionTestCase, client: LanguageClient

assert (
len(matches) == 1
), f"Expected 1 matching label `{test_case['label']}, got {len(matches)}."
), f"Expected 1 matching label `{test_case['label']}`, got {len(matches)}."
match = matches[0]

assert match.detail == test_case["detail"]
Expand Down
23 changes: 10 additions & 13 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from spinasm_lsp.parser import SPINAsmParser
from spinasm_lsp.tokens import EvaluatedToken, ParsedToken, TokenLookup
from spinasm_lsp.tokens import ASFV1Token, EvaluatedToken, TokenLookup

from .conftest import PATCH_DIR, TEST_PATCHES

Expand All @@ -26,16 +26,14 @@ def sentence_token_lookup() -> tuple[str, TokenLookup]:
# Build a list of word tokens, ignoring whitespace. We'll build the tokens
# consistently with asfv1 parsed tokens.
words = list(filter(lambda x: x, sentence.split(" ")))
token_vals = [{"type": "LABEL", "txt": w, "stxt": w, "val": None} for w in words]
token_vals = [ASFV1Token(type="LABEL", txt=w, stxt=w, val=None) for w in words]
tokens = []
col = 0

lookup = TokenLookup()
for t in token_vals:
start = sentence.index(t["txt"], col)
parsed_token = ParsedToken.from_asfv1_token(
t, start=lsp.Position(line=0, character=start)
)
start = sentence.index(t.txt, col)
parsed_token = t.at_position(lsp.Position(line=0, character=start))
eval_token = EvaluatedToken.from_parsed_token(parsed_token)

col = eval_token.range.end.character + 1
Expand Down Expand Up @@ -96,14 +94,13 @@ def test_get_token_positions():

def test_concatenate_cho_rdal_tokens():
"""Test that CHO and RDAL tokens are concatenated correctly into CHO RDAL."""
cho = ParsedToken.from_asfv1_token(
{"type": "MNEMONIC", "txt": "CHO", "stxt": "CHO", "val": None},
start=lsp.Position(line=0, character=0),
cho = ASFV1Token(type="MNEMONIC", txt="CHO", stxt="CHO", val=None).at_position(
start=lsp.Position(line=0, character=0)
)
rdal = ParsedToken.from_asfv1_token(
{"type": "LABEL", "txt": "RDAL", "stxt": "RDAL", "val": None},
# Put whitespace between CHO and RDAL to test that range is calculated
start=lsp.Position(line=0, character=10),

# Put whitespace between CHO and RDAL to test that range is calculated
rdal = ASFV1Token(type="LABEL", txt="RDAL", stxt="RDAL", val=None).at_position(
start=lsp.Position(line=0, character=10)
)

cho_rdal = cho.concatenate(rdal)
Expand Down

0 comments on commit c67ed82

Please sign in to comment.