Skip to content

Commit 6562eef

Browse files
committed
Implement cut to file command
1 parent 55dd878 commit 6562eef

File tree

10 files changed

+120
-14
lines changed

10 files changed

+120
-14
lines changed

AdvancedNewFile.sublime-settings

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,8 @@
149149
"append_extension_on_copy": true,
150150

151151
// Same as `rename_default`, but applied to copy command.
152-
"copy_default": ""
152+
"copy_default": "",
153+
154+
// Same as `rename_default`, but applied to cut to default command.
155+
"cut_to_file_default": ""
153156
}

Default.sublime-commands

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
{ "caption": "ANF: Rename File", "command": "advanced_new_file_move"},
44
{ "caption": "ANF: Delete File", "command": "advanced_new_file_delete"},
55
{ "caption": "ANF: Delete Current File", "command": "advanced_new_file_delete", "args": {"current": true}},
6-
{ "caption": "ANF: Copy Current File", "command": "advanced_new_file_copy" }
6+
{ "caption": "ANF: Copy Current File", "command": "advanced_new_file_copy" },
7+
{ "caption": "ANF: Cut to File", "command": "advanced_new_file_cut_to_file" }
78
]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ Setting to control if the extension will be automatically applied to copied file
182182

183183
Same as `rename_default`, applied to copy command.
184184

185+
`cut_to_file_default`:
186+
187+
Same as `rename_default`, applied to cut to file command.
185188

186189
### Project Specific Settings
187190
All of the above settings can also be specified as part of the project specific settings. These values override any previous values set by higher level settings, with aliases being an exception. Alias settings will be merged with higher level configurations for alias. In addition, if the same alias exist for both default/user settings and project settings, the project setting will take precedence.

advanced_new_file/anf_util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
RELATIVE_FALLBACK_INDEX_SETTING = "relative_fallback_index"
3333
APPEND_EXTENSION_ON_COPY_SETTING = "append_extension_on_copy"
3434
COPY_DEFAULT_SETTING = "copy_default"
35+
CUT_TO_FILE_DEFAULT_SETTING = "cut_to_file_default"
3536

3637
SETTINGS = [
3738
ALIAS_SETTING,
@@ -63,7 +64,8 @@
6364
APPEND_EXTENSION_ON_MOVE_SETTING,
6465
RELATIVE_FALLBACK_INDEX_SETTING,
6566
APPEND_EXTENSION_ON_COPY_SETTING,
66-
COPY_DEFAULT_SETTING
67+
COPY_DEFAULT_SETTING,
68+
CUT_TO_FILE_DEFAULT_SETTING
6769
]
6870

6971
NIX_ROOT_REGEX = r"^/"
@@ -73,6 +75,7 @@
7375
TOP_LEVEL_SPLIT_CHAR = ":"
7476
IS_ST3 = int(sublime.version()) > 3000
7577
IS_X64 = sublime.arch() == "x64"
78+
REGION_KEY = "anf_cut_to_file"
7679

7780

7881
def generate_creation_path(settings, base, path, append_extension=False):

advanced_new_file/commands/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from .helper_commands import AnfReplaceCommand, AdvancedNewFileCommand
1+
from .helper_commands import AnfReplaceCommand, AdvancedNewFileCommand, AnfRemoveRegionContentAndRegionCommand
22
from .new_file_command import AdvancedNewFileNew, AdvancedNewFileNewAtCommand, AdvancedNewFileNewEventListener
33
from .delete_file_command import AdvancedNewFileDelete
4+
from .cut_to_file import AdvancedNewFileCutToFile
45
from .move_file_command import AdvancedNewFileMove, AdvancedNewFileMoveAtCommand
56
from .copy_file_command import AdvancedNewFileCopy, AdvancedNewFileCopyAtCommand
67

@@ -14,5 +15,7 @@
1415
"AdvancedNewFileMoveAtCommand",
1516
"AdvancedNewFileDelete",
1617
"AdvancedNewFileCopy",
17-
"AdvancedNewFileCopyAtCommand"
18+
"AdvancedNewFileCopyAtCommand",
19+
"AnfRemoveRegionContentAndRegionCommand",
20+
"AdvancedNewFileCutToFile"
1821
]

advanced_new_file/commands/command_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,18 @@ def get_cursor_path(self):
379379

380380
return path
381381

382+
def _expand_default_path(self, path):
383+
current_file = self.view.file_name()
384+
if current_file:
385+
directory, current_file_name = os.path.split(current_file)
386+
path = path.replace("<filepath>", current_file)
387+
path = path.replace("<filedirectory>", directory + os.sep)
388+
else:
389+
current_file_name = ""
390+
391+
path = path.replace("<filename>", current_file_name)
392+
return path
393+
382394
def _find_open_file(self, file_name):
383395
window = self.window
384396
if IS_ST3:
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import sublime
2+
import sublime_plugin
3+
import os
4+
5+
from .command_base import AdvancedNewFileBase
6+
from ..anf_util import *
7+
8+
9+
class AdvancedNewFileCutToFile(AdvancedNewFileBase, sublime_plugin.WindowCommand):
10+
def __init__(self, window):
11+
super(AdvancedNewFileCutToFile, self).__init__(window)
12+
13+
def run(self, is_python=False):
14+
self.is_python = is_python
15+
self.run_setup()
16+
self.view.add_regions(REGION_KEY, self.view.sel(), flags=sublime.HIDDEN)
17+
18+
path = self.settings.get(CUT_TO_FILE_DEFAULT_SETTING, "")
19+
path = self._expand_default_path(path)
20+
21+
self.show_filename_input(
22+
path if len(path) > 0 else self.generate_initial_path())
23+
24+
def input_panel_caption(self):
25+
caption = 'Move selection to'
26+
if self.is_python:
27+
caption = '%s (creates __init__.py in new dirs)' % caption
28+
return caption
29+
30+
def update_status_message(self, creation_path):
31+
status_base = "Cutting selection to"
32+
if self.view is not None:
33+
self.view.set_status("AdvancedNewFile", "%s %s " %
34+
(status_base, creation_path))
35+
else:
36+
sublime.status_message("%s %s" % (status_base, creation_path))
37+
38+
def entered_file_action(self, path):
39+
file_exist = os.path.exists(path)
40+
attempt_open = True
41+
if not file_exist:
42+
attempt_open = self._create_new_file(path)
43+
if attempt_open:
44+
self._open_and_add_content_to_file(path)
45+
self.view.run_command("anf_remove_region_content_and_region", { "region_key": REGION_KEY})
46+
self.open_file(path)
47+
48+
49+
def _create_new_file(self, path):
50+
attempt_open = True
51+
52+
try:
53+
self.create(path)
54+
except OSError as e:
55+
attempt_open = False
56+
sublime.error_message("Cannot create '" + path +
57+
"'. See console for details")
58+
return attempt_open
59+
60+
def _open_and_add_content_to_file(self, path):
61+
if os.path.isfile(path):
62+
content = ""
63+
for region in self.view.get_regions(REGION_KEY):
64+
content += self.view.substr(region)
65+
content += "\n"
66+
67+
with open(path, "a") as file_obj:
68+
file_obj.write(content)
69+
70+
def is_enabled(self):
71+
view = self.window.active_view()
72+
cursors = view.sel()
73+
for cursor in cursors:
74+
if not cursor.empty():
75+
return True
76+
77+
return False
78+

advanced_new_file/commands/duplicate_file_base.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,8 @@ def run(self, is_python=False, initial_path=None, rename_file=None):
1616
self.argument_name = rename_file
1717

1818
path = self.settings.get(self.get_default_setting(), "")
19-
current_file = self.view.file_name()
20-
if current_file:
21-
directory, current_file_name = os.path.split(current_file)
22-
path = path.replace("<filepath>", current_file)
23-
path = path.replace("<filedirectory>", directory + os.sep)
24-
else:
25-
current_file_name = ""
19+
path = self._expand_default_path(path)
2620

27-
path = path.replace("<filename>", current_file_name)
2821
self.duplicate_setup()
2922
self.show_filename_input(
3023
path if len(path) > 0 else self.generate_initial_path())

advanced_new_file/commands/helper_commands.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ def run(self, is_python=False, initial_path=None,
2020
args["is_python"] = is_python
2121
args["initial_path"] = initial_path
2222
self.window.run_command("advanced_new_file_new", args)
23+
24+
25+
class AnfRemoveRegionContentAndRegionCommand(sublime_plugin.TextCommand):
26+
def run(self, edit, region_key):
27+
regions = self.view.get_regions(region_key)
28+
for region in regions:
29+
self.view.erase(edit, region)
30+
self.view.erase_regions(region_key)
31+

advanced_new_file/reloader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
'.commands.new_file_command',
4848
".commands.move_file_command",
4949
".commands.delete_file_command",
50-
".commands.copy_file_command"
50+
".commands.copy_file_command",
51+
".commands.cut_to_file"
5152
]
5253

5354
for suffix in mods_load_order:

0 commit comments

Comments
 (0)