Skip to content

Commit 82398f1

Browse files
committed
Add command to create file from side bar file selection
1 parent 525f66c commit 82398f1

File tree

7 files changed

+45
-18
lines changed

7 files changed

+45
-18
lines changed

AdvancedNewFile.sublime-settings

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,8 @@
176176

177177
// On empty input of file name, execute an alternative action.
178178
// Currently only implemented for the new file command, which will open a new unnamed file.
179-
"empty_filename_action": false
179+
"empty_filename_action": false,
180+
181+
// When specifying initial input, this boolean will place the cursor prior to the .<content>
182+
"cursor_before_extension": false
180183
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ Same as `default_root` for copy file commands. In addition to the valid values l
206206
`empty_filename_action`:
207207
On empty input of file name, execute an alternative action. Currently only implemented for the new file command, which will open a new unnamed file. Default value is `false`
208208

209+
210+
`cursor_before_extension`:
211+
When specifying initial input, this boolean will place the cursor prior to the last occurring dot. Default value is False.
209212
### Project Specific Settings
210213
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.
211214

advanced_new_file/anf_util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
RENAME_FILE_DEFAULT_ROOT_SETTING = "rename_file_default_root"
4040
COPY_FILE_DEFAULT_ROOT_SETTING = "copy_file_default_root"
4141
DEFAULT_NEW_FILE = "empty_filename_action"
42+
CURSOR_BEFORE_EXTENSION_SETTING = "cursor_before_extension"
4243

4344

4445
SETTINGS = [
@@ -78,7 +79,8 @@
7879
NEW_FILE_DEFAULT_ROOT_SETTING,
7980
RENAME_FILE_DEFAULT_ROOT_SETTING,
8081
COPY_FILE_DEFAULT_ROOT_SETTING,
81-
DEFAULT_NEW_FILE
82+
DEFAULT_NEW_FILE,
83+
CURSOR_BEFORE_EXTENSION_SETTING
8284
]
8385

8486
NIX_ROOT_REGEX = r"^/"

advanced_new_file/commands/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .helper_commands import AnfReplaceCommand, AdvancedNewFileCommand, AnfRemoveRegionContentAndRegionCommand
2-
from .new_file_command import AdvancedNewFileNew, AdvancedNewFileNewAtCommand, AdvancedNewFileNewEventListener
2+
from .new_file_command import AdvancedNewFileNew, AdvancedNewFileNewAtCommand, AdvancedNewFileNewAtFileCommand, AdvancedNewFileNewEventListener
33
from .delete_file_command import AdvancedNewFileDelete
44
from .cut_to_file import AdvancedNewFileCutToFile
55
from .move_file_command import AdvancedNewFileMove, AdvancedNewFileMoveAtCommand
@@ -10,6 +10,7 @@
1010
"AdvancedNewFileCommand",
1111
"AdvancedNewFileNew",
1212
"AdvancedNewFileNewAtCommand",
13+
"AdvancedNewFileNewAtFileCommand",
1314
"AdvancedNewFileNewEventListener",
1415
"AdvancedNewFileMove",
1516
"AdvancedNewFileMoveAtCommand",

advanced_new_file/commands/command_base.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import re
44
import sublime
5+
import sublime_plugin
56
import shlex
67

78
from ..anf_util import *
@@ -19,6 +20,7 @@
1920

2021
VIEW_NAME = "AdvancedNewFileCreation"
2122

23+
2224
class AdvancedNewFileBase(object):
2325

2426
def __init__(self, window):
@@ -181,7 +183,8 @@ def split_path(self, path=""):
181183
if self.view.file_name() is not None:
182184
root = os.path.dirname(self.view.file_name())
183185
else:
184-
folder_index = self.settings.get(RELATIVE_FALLBACK_INDEX_SETTING, 0)
186+
folder_index = self.settings.get(
187+
RELATIVE_FALLBACK_INDEX_SETTING, 0)
185188
folder_index = self.__validate_folder_index(folder_index)
186189
root = self.__project_folder_from_index(folder_index)
187190
if re.match(r"^\.{2}[/\\]", path):
@@ -264,6 +267,7 @@ def input_panel_caption(self):
264267

265268
def show_filename_input(self, initial):
266269
caption = self.input_panel_caption()
270+
267271
self.input_panel_view = self.window.show_input_panel(
268272
caption, initial,
269273
self.on_done, self.__update_filename_input, self.clear
@@ -275,6 +279,8 @@ def show_filename_input(self, initial):
275279
self.input_panel_view.settings().set("tab_completion", False)
276280
self.input_panel_view.settings().set("translate_tabs_to_spaces", False)
277281
self.input_panel_view.settings().set("anf_panel", True)
282+
if self.settings.get(CURSOR_BEFORE_EXTENSION_SETTING):
283+
self.__place_cursor_before_extension(self.input_panel_view)
278284

279285
def __update_filename_input(self, path_in):
280286
new_content = path_in
@@ -449,16 +455,14 @@ def _get_default_root(self):
449455
return self.settings.get(DEFAULT_ROOT_SETTING)
450456
return root_setting
451457

452-
def test_split(s, comments=False, posix=True):
453-
is_str = False
454-
if type(s) is str:
455-
s = unicode(s)
456-
is_str = True
457-
lex = shlex(s, posix=posix)
458-
lex.whitespace_split = True
459-
if not comments:
460-
lex.commenters = ''
461-
if is_str:
462-
return [ str(x) for x in list(lex) ]
463-
else:
464-
return list(lex)
458+
def __place_cursor_before_extension(self, view):
459+
if view.settings().get("anf_panel", False):
460+
cursors = view.sel()
461+
cursor = cursors[0]
462+
line_region = view.line(cursor)
463+
content = view.substr(line_region)
464+
matcher = re.match(r"(.+)\..+", content)
465+
if matcher:
466+
initial_position = len(matcher.group(1))
467+
cursors.clear()
468+
cursors.add(sublime.Region(initial_position, initial_position))

advanced_new_file/commands/new_file_command.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def flatten_list(self, initial_list):
7272
else:
7373
return [initial_list]
7474

75-
7675
# Assumes curly braces are balanced
7776
def expand_single_curly_brace(self, path):
7877
if "{" not in path:
@@ -135,6 +134,17 @@ def is_visible(self, dirs):
135134
return len(dirs) == 1
136135

137136

137+
class AdvancedNewFileNewAtFileCommand(sublime_plugin.WindowCommand):
138+
def run(self, files):
139+
if len(files) != 1:
140+
return
141+
self.window.run_command("advanced_new_file_new",
142+
{"initial_path": files[0]})
143+
144+
def is_visible(self, files):
145+
return len(files) == 1
146+
147+
138148
class AdvancedNewFileNewEventListener(sublime_plugin.EventListener):
139149
def on_load(self, view):
140150
if view.settings().get("_anf_new", False):

messages/1.7.0.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* New command `AdvancedNewFileNewAtFileCommand`. This command can be mapped to in "Side Bar.sublime-menu" to allow right clicking on files offer to create a new file from that directory, with the specified file as input.
2+
* New property `cursor_before_extension` that will place the cursor before the last occurring dot when an initial input is specified. Default value is false.
3+
4+
Thank you for using AdvancedNewFile.

0 commit comments

Comments
 (0)