Skip to content

Commit 9e4c6da

Browse files
committed
Initial work for templates when creating files
1 parent 6412ef6 commit 9e4c6da

5 files changed

Lines changed: 71 additions & 5 deletions

File tree

AdvancedNewFile.sublime-settings

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,14 @@
111111
"rename_default": "",
112112

113113
// Setting to control if VCS management is used when moving and removing files.
114-
"vcs_management": false
114+
"vcs_management": false,
115+
116+
// An object containing information to use for templates when creating new files.
117+
// The key values for this object should be a file extension. The value may either
118+
// be a string of the content to be inserted or a list of paths. If a list of paths
119+
// is specified, the name of the file will be displayed during selection. The paths
120+
// must either be absolute, or be from the home directory of the user (`~/`). If a
121+
// string is used, or the list contains a single entry, it will be automatically
122+
// inserted into any newly created files.
123+
"file_templates": {}
115124
}

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ String representing permissions to be applied to newly created files. E.g. "777"
152152

153153
Default input for renaming a file. Special value `<filename>` will be replaced with the current file name. Special value `<filepath>` will be replaced with the absolute path of the current file.
154154

155+
`vcs_management`:
156+
157+
Setting to control if VCS management is used when moving and removing files.
158+
159+
`file_templates`:
160+
161+
An object containing information to use for templates when creating new files. The key values for this object should be a file extension. The value may either be a string of the content to be inserted or a list of paths. If a list of paths is specified, the name of the file will be displayed during selection. The paths must either be absolute, or be from the home directory of the user (`~/`). If a string is used, or the list contains a single entry, it will be automatically inserted into any newly created files.
162+
155163
### Project Specific Settings
156164
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.
157165

advanced_new_file/anf_util.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
FOLDER_PERMISSIONS_SETTING = "folder_permissions"
2727
RENAME_DEFAULT_SETTING = "rename_default"
2828
VCS_MANAGEMENT_SETTING = "vcs_management"
29+
FILE_TEMPLATES_SETTING = "file_templates"
2930

3031
SETTINGS = [
3132
ALIAS_SETTING,
@@ -51,7 +52,8 @@
5152
FILE_PERMISSIONS_SETTING,
5253
FOLDER_PERMISSIONS_SETTING,
5354
RENAME_DEFAULT_SETTING,
54-
VCS_MANAGEMENT_SETTING
55+
VCS_MANAGEMENT_SETTING,
56+
FILE_TEMPLATES_SETTING
5557
]
5658

5759
NIX_ROOT_REGEX = r"^/"
@@ -100,6 +102,10 @@ def get_settings(view):
100102
for setting in SETTINGS:
101103
local_settings[setting] = settings.get(setting)
102104

105+
if type(project_settings) != dict:
106+
print("Invalid type %s for project settings" % type(project_settings))
107+
return local_settings
108+
103109
for key in project_settings:
104110
if key in SETTINGS:
105111
if key == "alias":

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
2-
from .new_file_command import AdvancedNewFileNew, AdvancedNewFileNewAtCommand
2+
from .new_file_command import AdvancedNewFileNew, AdvancedNewFileNewAtCommand, AdvancedNewFileNewEventListener
33
from .delete_file_command import AdvancedNewFileDelete
44
from .move_file_command import AdvancedNewFileMove, AdvancedNewFileMoveAtCommand
55

@@ -8,6 +8,7 @@
88
"AdvancedNewFileCommand",
99
"AdvancedNewFileNew",
1010
"AdvancedNewFileNewAtCommand",
11+
"AdvancedNewFileNewEventListener",
1112
"AdvancedNewFileMove",
1213
"AdvancedNewFileMoveAtCommand",
1314
"AdvancedNewFileDelete"

advanced_new_file/commands/new_file_command.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sublime_plugin
22
import os
3+
import xml.etree.ElementTree as ET
4+
35
from .command_base import AdvancedNewFileBase
46
from ..anf_util import *
57

@@ -21,7 +23,8 @@ def input_panel_caption(self):
2123

2224
def entered_file_action(self, path):
2325
attempt_open = True
24-
if not os.path.exists(path):
26+
file_exist = os.path.exists(path)
27+
if not file_exist:
2528
try:
2629
self.create(path)
2730
except OSError as e:
@@ -30,7 +33,9 @@ def entered_file_action(self, path):
3033
"'. See console for details")
3134
print("Exception: %s '%s'" % (e.strerror, e.filename))
3235
if attempt_open:
33-
self.open_file(path)
36+
file_view = self.open_file(path)
37+
if not file_exist:
38+
file_view.settings().set("_anf_new", True)
3439

3540
def update_status_message(self, creation_path):
3641
if self.view is not None:
@@ -50,3 +55,40 @@ def run(self, dirs):
5055

5156
def is_visible(self, dirs):
5257
return len(dirs) == 1
58+
59+
60+
class AdvancedNewFileNewEventListener(sublime_plugin.EventListener):
61+
def on_load(self, view):
62+
if view.settings().get("_anf_new", False):
63+
_, extension = os.path.splitext(view.file_name())
64+
extension = extension[1:]
65+
settings = get_settings(view)
66+
if extension in settings.get(FILE_TEMPLATES_SETTING):
67+
template = settings.get(FILE_TEMPLATES_SETTING)[extension]
68+
if type(template) == list:
69+
if len(template) == 1:
70+
view.run_command("insert_snippet", {"contents": self.get_snippet_from_file(template[0])})
71+
else:
72+
entries = list(map(self.get_basename, template))
73+
self.entries = list(map(self.expand_path, template))
74+
self.view = view
75+
view.window().show_quick_panel(entries, self.quick_panel_selection)
76+
else:
77+
view.run_command("insert_snippet", {"contents": template})
78+
view.settings().set("_anf_new", "")
79+
80+
def get_basename(self, path):
81+
return os.path.basename(os.path.expanduser(path))
82+
83+
def expand_path(self, path):
84+
return os.path.expanduser(path)
85+
86+
def quick_panel_selection(self, index):
87+
if index < 0:
88+
return
89+
self.view.run_command("insert_snippet", {"contents": self.get_snippet_from_file(self.entries[index])})
90+
91+
def get_snippet_from_file(self, path):
92+
tree = ET.parse(os.path.expanduser(path))
93+
content = tree.find("content")
94+
return content.text

0 commit comments

Comments
 (0)