-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyFilePath.py
More file actions
46 lines (32 loc) · 1.32 KB
/
Copy pathCopyFilePath.py
File metadata and controls
46 lines (32 loc) · 1.32 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
import os
import sublime
import sublime_plugin
# Credits:
# - https://forum.sublimetext.com/t/file-name-and-full-path-to-clipboard/4833/9
# - https://stackoverflow.com/a/42633058
# - https://github.com/titoBouzout/SideBarEnhancements
class FilenameToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard(os.path.basename(self.view.file_name()))
class PathToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
full_path = self.view.file_name()
filename = os.path.basename(full_path)
dirs = []
current_path = full_path
while True:
parent_dir_path = os.path.dirname(current_path)
parent_dir_name = os.path.basename(parent_dir_path)
if current_path == parent_dir_path:
# Reached the root directory
break
if os.path.exists(os.path.join(parent_dir_path, ".git")):
break
dirs.append(parent_dir_name)
current_path = parent_dir_path
full_parts = list(reversed(dirs)) + [filename]
relative_path = os.path.join(*full_parts)
sublime.set_clipboard(relative_path)
class FullPathToClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.set_clipboard(self.view.file_name())