Skip to content

Commit ddbe797

Browse files
committed
Refactor skim viewer plugin
1. use pathlib 2. re-arrange methods to avoid duplicate expensive calls
1 parent 6dc254a commit ddbe797

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

plugins/viewer/skim_viewer.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import os
2-
1+
from __future__ import annotations
2+
import stat
33
import sublime
44

5+
from pathlib import Path
6+
57
from ...latextools.utils.external_command import check_output
68
from ...latextools.utils.external_command import external_command
79

@@ -12,49 +14,55 @@
1214

1315
class SkimViewer(BaseViewer):
1416

15-
def forward_sync(self, pdf_file, tex_file, line, col, **kwargs):
16-
keep_focus = kwargs.pop("keep_focus", True)
17-
path_to_skim = "/Applications/Skim.app"
18-
19-
if not os.path.exists(path_to_skim):
20-
path_to_skim = check_output(
21-
[
22-
"osascript",
23-
"-e",
24-
'POSIX path of (path to app id "net.sourceforge.skim-app.skim")',
25-
],
26-
use_texpath=False,
17+
def forward_sync(self, pdf_file: str, tex_file: str, line: int, col: int, **kwargs) -> None:
18+
keep_focus = kwargs.get("keep_focus", True)
19+
skim_app = Path("/Applications/Skim.app")
20+
21+
if not skim_app.exists():
22+
skim_app = Path(
23+
check_output(
24+
[
25+
"osascript",
26+
"-e",
27+
'POSIX path of (path to app id "net.sourceforge.skim-app.skim")',
28+
],
29+
use_texpath=False,
30+
)
2731
)
2832

29-
command = [
30-
os.path.join(path_to_skim, "Contents", "SharedSupport", "displayline"),
31-
"-r",
32-
]
33+
command = [str(skim_app / "Contents" / "SharedSupport" / "displayline"), "-r"]
3334

3435
if keep_focus:
3536
command.append("-g")
3637

3738
command += [str(line), pdf_file, tex_file]
3839

3940
external_command(command, use_texpath=False)
40-
41-
def view_file(self, pdf_file, **kwargs):
42-
keep_focus = kwargs.pop("keep_focus", True)
43-
script_dir = os.path.join(sublime.cache_path(), "LaTeXTools", "viewer", "skim")
44-
script_file = os.path.join(script_dir, "displayfile")
45-
if not os.path.exists(script_file):
41+
if keep_focus:
42+
self.focus_st()
43+
44+
def view_file(self, pdf_file: str, **kwargs) -> None:
45+
keep_focus = kwargs.get("keep_focus", True)
46+
script_dir = Path(sublime.cache_path()) / "LaTeXTools" / "viewer" / "skim"
47+
script_dir.mkdir(parents=True, exist_ok=True)
48+
script_file = script_dir / "displayfile"
49+
if not script_file.exists():
4650
try:
47-
data = sublime.load_binary_resource(
48-
f"Packages/LaTeXTools/plugins/viewer/skim/displayfile"
51+
data = (
52+
sublime.load_binary_resource(
53+
f"Packages/LaTeXTools/plugins/viewer/skim/displayfile"
54+
)
55+
.replace(b"\r\n", b"\n")
56+
.replace(b"\r", b"\n")
4957
)
5058
except FileNotFoundError:
5159
sublime.error_message(
52-
"Cannot find required scripts\n"
53-
"for 'skim' viewer in LaTeXTools package."
60+
"Cannot find required scripts\nfor 'skim' viewer in LaTeXTools package."
5461
)
55-
else:
56-
with open(script_file, "wb") as fobj:
57-
fobj.write(data)
62+
return
63+
64+
script_file.write_bytes(data)
65+
script_file.chmod(script_file.stat().st_mode | stat.S_IXUSR)
5866

5967
command = ["/bin/sh", script_file, "-r"]
6068

@@ -64,9 +72,11 @@ def view_file(self, pdf_file, **kwargs):
6472
command.append(pdf_file)
6573

6674
external_command(command, use_texpath=False)
75+
if keep_focus:
76+
self.focus_st()
6777

68-
def supports_keep_focus(self):
78+
def supports_keep_focus(self) -> bool:
6979
return True
7080

71-
def supports_platform(self, platform):
81+
def supports_platform(self, platform: str) -> bool:
7282
return platform == "osx"

0 commit comments

Comments
 (0)