Skip to content

Commit

Permalink
refactor(parser): move Diagnostic AST node
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikit committed Oct 29, 2023
1 parent 2b1b025 commit 5a21b58
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
46 changes: 2 additions & 44 deletions fortls/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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__()
Expand Down
47 changes: 47 additions & 0 deletions fortls/parsers/internal/diagnostics.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5a21b58

Please sign in to comment.