From 4475798824159e2a7dc9d12915e85da57652bcc7 Mon Sep 17 00:00:00 2001 From: herr kaste Date: Sun, 25 Sep 2022 09:58:59 +0200 Subject: [PATCH 1/3] Fix off-by-one error for the line number in the popup Fixes #2 --- HoverDocs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HoverDocs.py b/HoverDocs.py index da8f665..07c870e 100644 --- a/HoverDocs.py +++ b/HoverDocs.py @@ -270,7 +270,7 @@ def build_doc_parts(self, view, point, force_doc_string=None, force_interface=No if len(comment_str) > 0: doc_str += ("" if len(doc_str) == 0 else "
") + comment_str if (self.setting("display_file_hyperlink") or force_hyperlink == True) and (force_hyperlink != False): - doc_str += ("" if len(doc_str) == 0 else "
") + f"{fn}:{sym_loc.row+1}" + doc_str += ("" if len(doc_str) == 0 else "
") + f"{fn}:{sym_loc.row}" return doc_str, sym_loc, sym_reg From 00608bfb7a9397ccdecf02e5856c2b23af04a49d Mon Sep 17 00:00:00 2001 From: herr kaste Date: Sun, 25 Sep 2022 10:01:38 +0200 Subject: [PATCH 2/3] Mitigate file encoding errors Ref #6 Assume files are "utf-8" encoded. This is obviously not true in all cases but very often nowadays. --- HoverDocs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HoverDocs.py b/HoverDocs.py index 07c870e..739ffd6 100644 --- a/HoverDocs.py +++ b/HoverDocs.py @@ -632,7 +632,7 @@ def find_def_and_comment(self, sym_loc, sym_name): large_size = v2.settings()["syntax_detection_size_limit"] if os.path.getsize(sym_loc.path) < large_size: # if the file is small, the load the entire file - with open(sym_loc.path, 'r') as f: + with open(sym_loc.path, 'r', encoding="utf-8") as f: v2.run_command("hover_docs", args={ "mode": "append", "characters": f.read() }) pos = self.get_pos(v2, sym_loc.row, sym_loc.col) sym_reg = sublime.Region(pos, pos+len(sym_name)) From 7ecddacb8b5cab2ebcadd3e27645d9f3520d1e48 Mon Sep 17 00:00:00 2001 From: herr kaste Date: Sun, 25 Sep 2022 10:06:09 +0200 Subject: [PATCH 3/3] Simplify `get_ancestor_dist` The old code still failed (raised) because you could not guarantee that `sym_dirs[i]` is safe. `os.path.commonpath` expects that both paths are absolute which is the case here, and for Windows, that both paths are on the same drive. Just check that the first char of both sides are equal and bail out with `0` otherwise. --- HoverDocs.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/HoverDocs.py b/HoverDocs.py index 739ffd6..f5a3a7d 100644 --- a/HoverDocs.py +++ b/HoverDocs.py @@ -99,27 +99,15 @@ def syntax_match(sym_path): # # /this/is/an/example/path/foo.txt # /this/is/another/path/bar.txt - def get_dirs(path): - print(path) - if path == None or re.match(r"^$", path) != None: - return None - ret = [] - path, base = os.path.split(path) - while base != "": - path, base = os.path.split(path) - ret.append(base) - ret.reverse() - return ret - ref_dirs = get_dirs(ref_fn) def get_ancestor_dist(sym_loc): - sym_dirs = get_dirs(sym_loc.path) - if ref_dirs == None or sym_dirs == None: + if not ref_fn: return 0 - for i in range(len(ref_dirs)): - if sym_dirs[i] != ref_dirs[i]: - break - ancestor_dist = len(ref_dirs)-i - return ancestor_dist + if ref_fn[0] != sym_loc.path[0]: + return 0 + return ( + ref_fn.count(os.sep) + - os.path.commonpath([sym_loc.path, ref_fn]).count(os.sep) + ) # Build a collection of filters to find the most appropriate result. # We filter down until there aren't any sym_locs left, or we've run out of filters.