Repository navigation
Expand file tree
/
Copy pathplugin.py
More file actions
76 lines (62 loc) · 2.13 KB
/
Copy pathplugin.py
File metadata and controls
76 lines (62 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import logging
import difflib
import sublime
# logger by package name
LOG = logging.getLogger(__package__)
def get_log_level(level_name):
"""Maps log level names to logging constants."""
return getattr(logging, level_name.upper(), logging.ERROR)
def update_log_level(settings):
"""
Reads the log_level from settings and reconfigures the logger.
"""
level_name = settings.get("log_level", "ERROR")
level = get_log_level(level_name)
LOG.setLevel(level)
LOG.propagate = False
if not LOG.handlers:
handler = logging.StreamHandler()
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
LOG.addHandler(handler)
def show_diff(window, old_text, new_text, name):
"""
Generate and show a git-style unified diff between old and new text.
Args:
window: Sublime Text window to create the view in
old_text: Original text content
new_text: Modified text content
name: Name for the diff view tab
"""
a = old_text.splitlines(keepends=True)
b = new_text.splitlines(keepends=True)
# Generate unified diff with context
diff_lines = list(difflib.unified_diff(
a, b,
fromfile="a/" + name,
tofile="b/" + name,
lineterm='',
n=5 # lines of context
))
if not diff_lines:
sublime.status_message("No changes")
return
# Build git-style diff output
output_parts = []
# Add git diff header
output_parts.append(f"diff a/{name} b/{name}\n")
output_parts.append(f"--- a/{name}\n")
output_parts.append(f"+++ b/{name}\n")
# Add the actual diff content (skip the default --- +++ lines from unified_diff)
for line in diff_lines[2:]:
if not line.endswith('\n'):
line += '\n'
output_parts.append(line)
difftxt = "".join(output_parts)
# Create and configure the diff view
v = window.new_file()
v.set_name(name)
v.set_scratch(True)
v.assign_syntax('Packages/Diff/Diff.sublime-syntax')
v.run_command('append', {'characters': difftxt, 'disable_tab_translation': True})
v.set_read_only(True)