lsp: Code cleanup

This commit is contained in:
James Westman 2021-10-24 14:36:54 -05:00
parent 49658c634e
commit 8fc0efb642
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
5 changed files with 197 additions and 116 deletions

View file

@ -0,0 +1,57 @@
# lsp_enums.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from enum import Enum
from .utils import *
class TextDocumentSyncKind(Enum):
None_ = 0,
Full = 1,
Incremental = 2,
class OpenFile:
def __init__(self, uri, text, version):
self.uri = uri
self.text = text
self.version = version
self.diagnostics = []
self._update()
def apply_changes(self, changes):
for change in changes:
start = utils.pos_to_idx(change.range.start.line, change.range.start.character, self.text)
end = utils.pos_to_idx(change.range.end.line, change.range.end.character, self.text)
self.text = self.text[:start] + change.text + self.text[end:]
self._update()
def _update(self):
self.diagnostics = []
try:
self.tokens = tokenizer.tokenize(self.text)
self.ast = parser.parse(self.tokens)
self.diagnostics = [self._create_diagnostic(text, err) for err in list(ast.errors)]
except MultipleErrors as e:
self.diagnostics += [self._create_diagnostic(text, err) for err in e.errors]
except CompileError as e:
self.diagnostics += [self._create_diagnostic(text, e)]