lsp: Add code actions

"Did you mean" hints are automatically converted into code actions.
This commit is contained in:
James Westman 2021-11-11 22:59:49 -06:00
parent d89f2356b4
commit d511b3f1e3
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
5 changed files with 82 additions and 16 deletions

View file

@ -17,7 +17,7 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from dataclasses import dataclass
import typing as T
import sys, traceback
from . import utils
@ -45,13 +45,14 @@ class CompileError(PrintableError):
category = "error"
def __init__(self, message, start=None, end=None, did_you_mean=None, hints=None):
def __init__(self, message, start=None, end=None, did_you_mean=None, hints=None, actions=None):
super().__init__(message)
self.message = message
self.start = start
self.end = end
self.hints = hints or []
self.actions = actions or []
if did_you_mean is not None:
self._did_you_mean(*did_you_mean)
@ -72,6 +73,7 @@ class CompileError(PrintableError):
self.hint(f"Did you mean `{recommend}` (note the capitalization)?")
else:
self.hint(f"Did you mean `{recommend}`?")
self.actions.append(CodeAction(f"Change to `{recommend}`", recommend))
else:
self.hint("Did you check your spelling?")
self.hint("Are your dependencies up to date?")
@ -92,6 +94,12 @@ at {filename} line {line_num} column {col_num}:
print()
@dataclass
class CodeAction:
title: str
replace_with: str
class AlreadyCaughtError(Exception):
""" Emitted when a validation has already failed and its error message
should not be repeated. """