-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcopy_python_path.py
More file actions
59 lines (42 loc) · 1.93 KB
/
Copy pathcopy_python_path.py
File metadata and controls
59 lines (42 loc) · 1.93 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
# -*- coding: utf-8 -*-
import os
import sys
import sublime
import sublime_plugin
class CopyPythonPathCommand(sublime_plugin.TextCommand):
def run(self, edit):
python_path_items = []
head, tail = os.path.split(self.view.file_name())
module = tail.rsplit('.', 1)[0]
if module != '__init__':
python_path_items.append(module)
head, tail = os.path.split(head)
while tail:
if '__init__.py' in os.listdir(os.path.join(head, tail)):
python_path_items.insert(0, tail)
else:
break
head, tail = os.path.split(head)
caret_point = self.view.sel()[0].begin()
class_entity = 'entity.name.class.python' if sys.version_info < (3, 0, 0) else 'entity.name.type.class.python'
if class_entity in self.view.scope_name(caret_point):
python_path_items.append(self.view.substr(self.view.word(caret_point)))
if 'entity.name.function.python' in self.view.scope_name(caret_point):
method_name = self.view.substr(self.view.word(caret_point))
if self.view.indentation_level(caret_point) > 0:
regions = self.view.find_by_selector(class_entity)
possible_class_point = 0
for region in regions:
if region.b < caret_point:
possible_class_point = region.a
else:
break
class_name = self.view.substr(self.view.word(possible_class_point))
python_path_items.append(class_name)
python_path_items.append(method_name)
python_path = '.'.join(python_path_items)
sublime.set_clipboard(python_path)
sublime.status_message('"%s" copied to clipboard' % python_path)
def is_enabled(self):
matcher = 'source.python'
return self.view.match_selector(self.view.sel()[0].begin(), matcher)