Skip to content

Commit 655b228

Browse files
committed
New features
- Close unrelated tabs to the open project. - Close unrelated tabs to the directory of the current open tab (tab with focus). - Close unrelated tabs to a user input directory.
1 parent c876d59 commit 655b228

File tree

7 files changed

+183
-10
lines changed

7 files changed

+183
-10
lines changed

Context.sublime-menu

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
[
2-
{
3-
"id": "less_tabs",
4-
"command": "less_tabs",
5-
"caption": "Close old tabs"
2+
{ "caption": "-" }
3+
,
4+
{
5+
"id" : "less_tabs",
6+
"caption" : "Less Tabs",
7+
"children" :
8+
[
9+
{
10+
"command": "less_tabs",
11+
"caption": "Close Old Tabs"
12+
}
13+
,
14+
{
15+
"command": "less_tabs_close_file_dir_unrelated",
16+
"caption": "Close Tabs Unrelated To Current Tab Directory"
17+
}
18+
,
19+
{
20+
"command": "less_tabs_close_project_unrelated",
21+
"caption": "Close Tabs Unrelated To Current Project"
22+
}
23+
]
624
}
725
]

Default (Linux).sublime-keymap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
[
2+
//Think of it as "c" Close
23
{
34
"keys": ["ctrl+alt+c"], "command": "less_tabs"
45
}
6+
,
7+
//Think of it as "u+p" Project Unrelated
8+
{
9+
"keys": ["ctrl+alt+u+p"], "command": "less_tabs_close_project_unrelated"
10+
}
11+
,
12+
//Think of it as "u+t" Tab Unrelated
13+
{
14+
"keys": ["ctrl+alt+u+t"], "command": "less_tabs_close_file_dir_unrelated"
15+
}
16+
,
17+
//Think of it as "u+d" Directory Unrelated
18+
{
19+
"keys": ["ctrl+alt+u+d"], "command": "less_tabs_close_dir_unrelated"
20+
}
521
]

Default (OSX).sublime-keymap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
[
2+
//Think of it as "c" Close
23
{
34
"keys": ["ctrl+alt+c"], "command": "less_tabs"
45
}
6+
,
7+
//Think of it as "u+p" Project Unrelated
8+
{
9+
"keys": ["ctrl+alt+u+p"], "command": "less_tabs_close_project_unrelated"
10+
}
11+
,
12+
//Think of it as "u+t" Tab Unrelated
13+
{
14+
"keys": ["ctrl+alt+u+t"], "command": "less_tabs_close_file_dir_unrelated"
15+
}
16+
,
17+
//Think of it as "u+d" Directory Unrelated
18+
{
19+
"keys": ["ctrl+alt+u+d"], "command": "less_tabs_close_dir_unrelated"
20+
}
521
]

Default (Windows).sublime-keymap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
[
2+
//Think of it as "c" Close
23
{
34
"keys": ["ctrl+alt+c"], "command": "less_tabs"
45
}
6+
,
7+
//Think of it as "u+p" Project Unrelated
8+
{
9+
"keys": ["ctrl+alt+u+p"], "command": "less_tabs_close_project_unrelated"
10+
}
11+
,
12+
//Think of it as "u+t" Tab Unrelated
13+
{
14+
"keys": ["ctrl+alt+u+t"], "command": "less_tabs_close_file_dir_unrelated"
15+
}
16+
,
17+
//Think of it as "u+d" Directory Unrelated
18+
{
19+
"keys": ["ctrl+alt+u+d"], "command": "less_tabs_close_dir_unrelated"
20+
}
521
]

Default.sublime-commands

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
[
22
{
3-
"caption": "Less tabs: close old tabs",
3+
"caption": "Less Tabs: Close Old Tabs",
44
"command": "less_tabs"
55
}
6+
,
7+
{
8+
"caption": "Less tabs: Close Tabs Unrelated To Current Project",
9+
"command": "less_tabs_close_project_unrelated"
10+
}
11+
,
12+
{
13+
"caption": "Less tabs: Close Tabs Unrelated To Current Tab Directory",
14+
"command": "less_tabs_close_file_dir_unrelated"
15+
}
16+
,
17+
{
18+
"caption": "Less tabs: Close Tabs Unrelated To Directory",
19+
"command": "less_tabs_close_dir_unrelated"
20+
}
621
]

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ There is also a command in the command palette `Less tabs: close old tabs`
3333

3434
- By setting the value of the setting value "close_on_open_new" to true, the plugin will execute the closing command when a new tab is opened.
3535

36+
- Close unrelated tabs to the open project.
37+
38+
- Close unrelated tabs to the directory of the current open tab (tab with focus).
39+
40+
- Close unrelated tabs to a user input directory.
41+
3642
##Stay tuned
3743

3844
I'm working on another package called **moreTabs**, that will help you open closed tabs in a much easier way.

lessTabs.py

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
1-
import sublime, sublime_plugin, os, time
1+
import sublime, sublime_plugin, os, time, re
2+
3+
class WM:
4+
def get_project_folder(window):
5+
project_folders = []
6+
if ( WM.has_project(window) ):
7+
for folder_data in window.project_data()["folders"]:
8+
project_folders.append(folder_data['path'])
9+
10+
return project_folders
11+
12+
def has_project(window):
13+
return window.project_data() != None
14+
15+
def file_belongs_to_path(buffer, parent_folders, related_paths):
16+
file_in_project = False
17+
file_dir = os.path.dirname(buffer.file_name())
18+
if ( not file_dir in related_paths ):
19+
for folder in parent_folders:
20+
if ( file_dir.startswith(folder) ):
21+
related_paths.append(file_dir)
22+
file_in_project = True
23+
break
24+
else:
25+
file_in_project = True
26+
27+
return file_in_project, related_paths
28+
29+
def close_buffer(window, buffer):
30+
window.focus_view(buffer)
31+
window.run_command('close_file')
32+
233

334
class lessTabsCommand(sublime_plugin.WindowCommand):
435
def run(self):
@@ -18,14 +49,69 @@ def run(self):
1849
and not buffer.is_scratch()
1950
and not buffer.is_dirty()
2051
and path
21-
and os.path.exists(path) == True
52+
and os.path.exists(path)
2253
and now - os.path.getmtime(path) > modified_ls
2354
and now - os.path.getatime(path) > accessed_ls
2455
):
25-
self.window.focus_view(buffer)
26-
self.window.run_command('close_file')
56+
WM.close_buffer(self.window, buffer)
57+
58+
59+
class lessTabsCloseProjectUnrelatedCommand(sublime_plugin.WindowCommand):
60+
def run(self):
61+
62+
window = self.window
63+
64+
if ( WM.has_project(window) ):
65+
related_paths = []
66+
project_folders = WM.get_project_folder(window)
67+
68+
for buffer in window.views():
69+
file_in_project, related_paths = WM.file_belongs_to_path(buffer, project_folders, related_paths)
70+
71+
if ( not file_in_project ):
72+
WM.close_buffer(window, buffer)
73+
else:
74+
sublime.status_message("Less Tabs : There is no open project.")
75+
76+
77+
class lessTabsCloseDirUnrelatedCommand(sublime_plugin.WindowCommand):
78+
def run(self):
79+
self.window.show_input_panel("Directory of the tabs to keep open", "", self.on_done, None, None)
80+
81+
def on_done(self, input):
82+
if ( os.path.exists(input) ):
83+
related_paths = []
84+
for buffer in self.window.views():
85+
file_in_dir, related_paths = WM.file_belongs_to_path(buffer, [input], related_paths)
86+
87+
if ( not file_in_dir ):
88+
WM.close_buffer(self.window, buffer)
89+
else:
90+
sublime.error_message('Less Tabs : directory "'+input+'" not found.')
91+
self.window.run_command('less_tabs_close_dir_unrelated')
92+
93+
94+
class lessTabsCloseFileDirUnrelatedCommand(sublime_plugin.WindowCommand):
95+
def run(self):
96+
window = self.window
97+
active_view = window.active_view()
98+
99+
if ( not active_view.is_scratch() ):
100+
file_path = active_view.file_name()
101+
if ( file_path and os.path.exists(file_path) ):
102+
related_paths = []
103+
for buffer in window.views():
104+
file_in_dir, related_paths = WM.file_belongs_to_path(buffer, [os.path.dirname(file_path)], related_paths)
105+
106+
if ( not file_in_dir ):
107+
WM.close_buffer(window, buffer)
108+
else:
109+
sublime.message_error('Less Tabs : file "'+file_path+'" not found.')
110+
else:
111+
sublime.message_error('Less Tabs : current file doesn\'t exist on physical drive.')
112+
27113

28114
class lessTabsEvents(sublime_plugin.EventListener):
29115
def on_new(self, view):
30116
if ( sublime.load_settings("lessTabs.sublime-settings").get("close_on_open_new") ):
31-
sublime.active_window().run_command("less_tabs")
117+
sublime.active_window().run_command("less_tabs")

0 commit comments

Comments
 (0)