Skip to content

Commit

Permalink
Merge pull request #9 from liam-strand/autocompletion
Browse files Browse the repository at this point in the history
Autocompletion
  • Loading branch information
liam-strand authored Jul 8, 2022
2 parents 540aca4 + e5ad2e3 commit c270a37
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
3 changes: 3 additions & 0 deletions mfdls/medford_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def _get_minor_tokens(major: Dict, definitions: Dict) -> Dict[str, List[str]]:

minors = _extract_minors_from_def(definitions[def_name])

if major_name == "Medford":
major_name = "MEDFORD"

if set(minors) == _DATA_PROV_NAMES:
return _data_prov_minors(major, definitions)
else:
Expand Down
40 changes: 28 additions & 12 deletions mfdls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,28 @@
"""
import logging
from typing import Union
from typing import Optional, Union

from pygls.lsp.methods import (
COMPLETION,
TEXT_DOCUMENT_DID_CHANGE,
TEXT_DOCUMENT_DID_OPEN,
TEXT_DOCUMENT_DID_SAVE,
)
from pygls.lsp.types import (
CompletionItem,
CompletionList,
CompletionOptions,
CompletionParams,
DidChangeTextDocumentParams,
DidOpenTextDocumentParams,
DidSaveTextDocumentParams,
MessageType,
)
from pygls.server import LanguageServer

from mfdls.medford_syntax import validate_syntax
from mfdls.medford_tokens import get_available_tokens
from mfdls.medford_validation import ValidationMode, validate_data
from mfdls.pip_helpers import pip_install, pip_uninstall, pip_upgrade

# Set up logging to pygls.log
logging.basicConfig(filename="pygls.log", filemode="w", level=logging.WARNING)
Expand All @@ -40,24 +43,31 @@ class MEDFORDLanguageServer(LanguageServer):

#### COMMANDS ####

CMD_INSTALL_MFDLS = "installMFDLS"
CMD_UPDATE_MFDLS = "updateMFDLS"
CMD_UNINSTALL_MFDLS = "uninstallMFDLS"

#### LS CONSTANTS ####

CONFIGURATION_SECTION = "medfordServer"

def __init__(self):
self.validation_mode = ValidationMode.OTHER
self.macros = {}
self.tokens = get_available_tokens()
super().__init__()


medford_server = MEDFORDLanguageServer()
# Here we can generate all of the tokens once on compilation to increase performance
def _generate_completion_list() -> CompletionList:
tokens = get_available_tokens()

clist = []
for token, minors in tokens.items():
clist.append(CompletionItem(label=token))
for value in minors:
clist.append(CompletionItem(label=token + "-" + value))
return CompletionList(is_incomplete=False, items=clist)


medford_server = MEDFORDLanguageServer()
completion_list = _generate_completion_list()

#### #### #### LSP METHODS #### #### ####


Expand All @@ -73,7 +83,13 @@ def did_open(ls: MEDFORDLanguageServer, params: DidOpenTextDocumentParams):
_generate_syntactic_diagnostics(ls, params)


# Does not work yet, commenting out for a merge
@medford_server.feature(COMPLETION, CompletionOptions(trigger_characters=["@"]))
def completions(_params: Optional[CompletionParams] = None) -> CompletionList:
"""Returns completion items."""
# Since we gathered the tokens on launch, we can just refer our completions to those.
return completion_list


@medford_server.feature(TEXT_DOCUMENT_DID_SAVE)
def did_save(ls: MEDFORDLanguageServer, params: DidSaveTextDocumentParams):
"""Text document did save notification."""
Expand All @@ -89,9 +105,9 @@ def did_save(ls: MEDFORDLanguageServer, params: DidSaveTextDocumentParams):
def _generate_syntactic_diagnostics(
ls: MEDFORDLanguageServer,
params: Union[
DidChangeTextDocumentParams,
DidSaveTextDocumentParams,
DidOpenTextDocumentParams,
DidChangeTextDocumentParams,
],
) -> None:
"""Wrapper around validation function to request and display Diagnostics
Expand All @@ -109,7 +125,7 @@ def _generate_syntactic_diagnostics(
# Publish the diagnostics
ls.publish_diagnostics(doc.uri, diagnostics)

# Store the defined macros in the languge server, if we successfully parse the file
# Store the defined macros in the languge server
if details:
ls.macros = details[0].macro_dictionary

Expand Down

0 comments on commit c270a37

Please sign in to comment.