Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema diagnostics in language server #8349

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions edb/errors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ def details(self):
def pgext_code(self):
return self._pgext_code

@property
def filename(self):
return self._attrs.get(FIELD_FILENAME, None)


@contextlib.contextmanager
def ensure_span(span: Optional[edb_span.Span]) -> Iterator[None]:
Expand Down
13 changes: 13 additions & 0 deletions edb/language_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

import dataclasses
from typing import Generic, Optional, TypeVar


T = TypeVar('T', covariant=True)
E = TypeVar('E', covariant=True)


@dataclasses.dataclass(kw_only=True, slots=True)
class Result(Generic[T, E]):
ok: Optional[T] = None
err: Optional[E] = None
45 changes: 31 additions & 14 deletions edb/language_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
)
def main(*, version: bool, stdio: bool):
if version:
print(f"edgedb-ls, version {buildmeta.get_version()}")
print(f"gel-ls, version {buildmeta.get_version()}")
sys.exit(0)

ls = init()
Expand All @@ -48,8 +48,8 @@ def main(*, version: bool, stdio: bool):
print("Error: no LSP transport enabled. Use --stdio.")


def init() -> ls_server.EdgeDBLanguageServer:
ls = ls_server.EdgeDBLanguageServer()
def init() -> ls_server.GelLanguageServer:
ls = ls_server.GelLanguageServer()

@ls.feature(
lsp_types.INITIALIZE,
Expand Down Expand Up @@ -84,29 +84,46 @@ def completions(params: lsp_types.CompletionParams):
return ls


def document_updated(ls: ls_server.EdgeDBLanguageServer, doc_uri: str):
def document_updated(ls: ls_server.GelLanguageServer, doc_uri: str):
# each call to this function should yield in exactly one publish_diagnostics
# for this document

document = ls.workspace.get_text_document(doc_uri)
ql_ast = ls_parsing.parse(document, ls)
if diagnostics := ql_ast.error:
ls.publish_diagnostics(document.uri, diagnostics, document.version)
return
assert ql_ast.ok
diagnostic_set: ls_server.DiagnosticsSet

try:
if isinstance(ql_ast.ok, list):
diagnostics = ls_server.compile(ls, ql_ast.ok)
ls.publish_diagnostics(document.uri, diagnostics, document.version)
if doc_uri.endswith('.esdl') or doc_uri.endswith('.gel'):
# schema file

ls_server.update_schema_doc(ls, document)

# recompile schema
ls.state.schema = None
_schema, diagnostic_set = ls_server.get_schema(ls)
else:
ls.publish_diagnostics(document.uri, [], document.version)
# query file
ql_ast_res = ls_parsing.parse(document, ls)
if diag := ql_ast_res.err:
ls.publish_diagnostics(document.uri, diag, document.version)
return
assert ql_ast_res.ok
ql_ast = ql_ast_res.ok

if isinstance(ql_ast, list):
diagnostic_set = ls_server.compile(ls, document, ql_ast)
else:
# SDL in query files?
ls.publish_diagnostics(document.uri, [], document.version)
diagnostic_set = ls_server.DiagnosticsSet()

for doc, diags in diagnostic_set.by_doc.items():
ls.publish_diagnostics(doc.uri, diags, doc.version)
except BaseException as e:
send_internal_error(ls, e)
ls.publish_diagnostics(document.uri, [], document.version)


def send_internal_error(ls: ls_server.EdgeDBLanguageServer, e: BaseException):
def send_internal_error(ls: ls_server.GelLanguageServer, e: BaseException):
text = edb_traceback.format_exception(e)
ls.show_message_log(f'Internal error: {text}')

Expand Down
21 changes: 8 additions & 13 deletions edb/language_server/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
# limitations under the License.
#

from typing import Any, List, Tuple, Optional, TypeVar, Generic
from dataclasses import dataclass
from typing import Any, List, Tuple, Optional

from pygls.server import LanguageServer
from pygls.workspace import TextDocument
Expand All @@ -30,21 +29,17 @@
from edb.edgeql.parser.grammar import tokens as qltokens
import edb._edgeql_parser as rust_parser


T = TypeVar('T', covariant=True)
E = TypeVar('E', covariant=True)


@dataclass(kw_only=True, slots=True)
class Result(Generic[T, E]):
ok: Optional[T] = None
error: Optional[E] = None
from . import Result


def parse(
doc: TextDocument, ls: LanguageServer
) -> Result[List[qlast.Base] | qlast.Schema, List[lsp_types.Diagnostic]]:
sdl = doc.filename.endswith('.esdl') if doc.filename else False
sdl = (
doc.filename.endswith('.esdl') or doc.filename.endswith('.gel')
if doc.filename
else False
)

source, result, productions = _parse_inner(doc.source, sdl)

Expand Down Expand Up @@ -77,7 +72,7 @@ def parse(
)
)

return Result(error=diagnostics)
return Result(err=diagnostics)

# parsing successful
assert isinstance(result.out, rust_parser.CSTNode)
Expand Down
Loading