From f1ce00038ce1c55d011d40f38c67083bab4c41f2 Mon Sep 17 00:00:00 2001 From: gnikit Date: Sat, 21 Oct 2023 16:47:20 +0100 Subject: [PATCH] refactor(parser): move Diagnostic AST node --- fortls/objects.py | 46 ++----------------------- fortls/parsers/internal/diagnostics.py | 47 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 fortls/parsers/internal/diagnostics.py diff --git a/fortls/objects.py b/fortls/objects.py index 0f04ee33..27a13196 100644 --- a/fortls/objects.py +++ b/fortls/objects.py @@ -36,9 +36,10 @@ get_placeholders, get_var_stack, ) -from fortls.json_templates import diagnostic_json, location_json, range_json +from fortls.json_templates import diagnostic_json, range_json from fortls.jsonrpc import path_to_uri from fortls.parsers.internal.base import FortranObj +from fortls.parsers.internal.diagnostics import Diagnostic from fortls.parsers.internal.use import Use @@ -365,49 +366,6 @@ class AssociateMap: link_name: str -class Diagnostic: - def __init__( - self, sline: int, message: str, severity: int = 1, find_word: str = None - ): - self.sline: int = sline - self.message: str = message - self.severity: int = severity - self.find_word: str = find_word - self.has_related: bool = False - self.related_path = None - self.related_line = None - self.related_message = None - - def add_related(self, path: str, line: int, message: str): - self.has_related = True - self.related_path = path - self.related_line = line - self.related_message = message - - def build(self, file_obj): - schar = echar = 0 - if self.find_word is not None: - self.sline, obj_range = file_obj.find_word_in_code_line( - self.sline, self.find_word - ) - if obj_range.start >= 0: - schar = obj_range.start - echar = obj_range.end - diag = diagnostic_json( - self.sline, schar, self.sline, echar, self.message, self.severity - ) - if self.has_related: - diag["relatedInformation"] = [ - { - **location_json( - path_to_uri(self.related_path), self.related_line, 0 - ), - "message": self.related_message, - } - ] - return diag - - class Scope(FortranObj): def __init__(self, file_ast, line_number: int, name: str, keywords: list = None): super().__init__() diff --git a/fortls/parsers/internal/diagnostics.py b/fortls/parsers/internal/diagnostics.py new file mode 100644 index 00000000..eb94a94d --- /dev/null +++ b/fortls/parsers/internal/diagnostics.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +from fortls.json_templates import diagnostic_json, location_json +from fortls.jsonrpc import path_to_uri + + +class Diagnostic: + def __init__( + self, sline: int, message: str, severity: int = 1, find_word: str = None + ): + self.sline: int = sline + self.message: str = message + self.severity: int = severity + self.find_word: str = find_word + self.has_related: bool = False + self.related_path = None + self.related_line = None + self.related_message = None + + def add_related(self, path: str, line: int, message: str): + self.has_related = True + self.related_path = path + self.related_line = line + self.related_message = message + + def build(self, file_obj): + schar = echar = 0 + if self.find_word is not None: + self.sline, obj_range = file_obj.find_word_in_code_line( + self.sline, self.find_word + ) + if obj_range.start >= 0: + schar = obj_range.start + echar = obj_range.end + diag = diagnostic_json( + self.sline, schar, self.sline, echar, self.message, self.severity + ) + if self.has_related: + diag["relatedInformation"] = [ + { + **location_json( + path_to_uri(self.related_path), self.related_line, 0 + ), + "message": self.related_message, + } + ] + return diag