Skip to content

Commit

Permalink
Add support for Sublime Text 3
Browse files Browse the repository at this point in the history
  • Loading branch information
skuroda committed Feb 1, 2013
1 parent 37e61af commit 03276e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
63 changes: 32 additions & 31 deletions AdvancedNewFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sublime
import sublime_plugin
import re
import logging

SETTINGS = [
"alias",
Expand All @@ -16,18 +17,22 @@
"ignore_case",
"alias_root",
"alias_path",
"alias_folder_index"
"alias_folder_index",
"debug"
]
DEBUG = False
PLATFORM = sublime.platform().lower()
VIEW_NAME = "AdvancedNewFileCreation"
WIN_ROOT_REGEX = r"[a-zA-Z]:(/|\\)"
NIX_ROOT_REGEX = r"^/"
HOME_REGEX = r"^~"

# Set up logger
logging.basicConfig(format='[AdvancedNewFile] %(levelname)s %(message)s')
logger = logging.getLogger()


class AdvancedNewFileCommand(sublime_plugin.WindowCommand):
def run(self, is_python=False):
self.PLATFORM = sublime.platform().lower()
self.root = None
self.alias_root = None
self.top_level_split_char = ":"
Expand Down Expand Up @@ -64,15 +69,20 @@ def run(self, is_python=False):
alias_root = ""
self.alias_root, tmp = self.split_path(alias_root, True)

debug = settings.get("debug") or False
if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.ERROR)
# Get user input
self.show_filename_input(path)

def get_aliases(self, settings):
aliases = settings.get("alias")
all_os_aliases = settings.get("os_specific_alias")
for key in all_os_aliases:
if PLATFORM in all_os_aliases.get(key):
aliases[key] = all_os_aliases.get(key).get(PLATFORM)
if self.PLATFORM in all_os_aliases.get(key):
aliases[key] = all_os_aliases.get(key).get(self.PLATFORM)

return aliases

Expand Down Expand Up @@ -101,15 +111,15 @@ def get_default_root(self, string, is_alias=False):
elif string == "path":
root = "path"
else:
print "Invalid specifier for \"default_root\""
logger.error("Invalid specifier for \"default_root\"")
return root

def split_path(self, path="", is_alias=False):
HOME_REGEX = r"^~[/\\]"
root = None
try:
# Parse windows root
if PLATFORM == "windows":
if self.PLATFORM == "windows":
if re.match(WIN_ROOT_REGEX, path):
root = path[0:3]
path = path[3:]
Expand All @@ -134,9 +144,6 @@ def split_path(self, path="", is_alias=False):
root = root or self.window.folders()[folder_index]
except IndexError:
root = os.path.expanduser("~")
if DEBUG:
print "AdvancedNewFile[Debug]: root is " + root
print "AdvancedNewFile[Debug]: path is " + path
return root, path

def translate_alias(self, target):
Expand All @@ -158,7 +165,7 @@ def translate_alias(self, target):
if alias == target:
alias_path = self.aliases.get(alias)
if re.search(HOME_REGEX, alias_path) is None:
if PLATFORM == "windows":
if self.PLATFORM == "windows":
if re.search(WIN_ROOT_REGEX, alias_path) is None:
root = os.path.join(self.alias_root, alias_path)
break
Expand Down Expand Up @@ -190,10 +197,6 @@ def show_filename_input(self, initial=''):
view.settings().set("auto_complete_commit_on_tab", True)
view.settings().set("tab_completion", True)

# May be useful to see the popup for debugging
if DEBUG:
view.settings().set("auto_complete", True)
view.settings().set("auto_complete_selector", "text")
PathAutocomplete.set_view_id(view.id())
PathAutocomplete.set_root(self.root, True)

Expand All @@ -204,17 +207,18 @@ def update_filename_input(self, path_in):
else:
PathAutocomplete.set_root(base, True)

creation_path = self.generate_creation_path(base, path)
if self.show_path:
if self.view != None:
self.view.set_status("AdvancedNewFile", "Creating file at %s " % \
self.generate_creation_path(base, path))
creation_path)
else:
sublime.status_message("Unable to fill status bar without view")

logger.debug("Creation path is '%s'" % creation_path)
PathAutocomplete.set_path(path)

def generate_creation_path(self, base, path):
if PLATFORM == "windows":
if self.PLATFORM == "windows":
if not re.match(WIN_ROOT_REGEX, base):
return base + ":" + path
else:
Expand All @@ -225,7 +229,7 @@ def generate_creation_path(self, base, path):

def entered_filename(self, filename):
# Check if valid root specified for windows.
if PLATFORM == "windows":
if self.PLATFORM == "windows":
if re.match(WIN_ROOT_REGEX, filename):
root = filename[0:3]
if not os.path.isdir(root):
Expand All @@ -237,24 +241,23 @@ def entered_filename(self, filename):
file_path = os.path.join(base, path)
# Check for invalid alias specified.
if self.top_level_split_char in filename and \
not (PLATFORM == "windows" and re.match(WIN_ROOT_REGEX, base)) and \
not (PLATFORM != "windows" and re.match(NIX_ROOT_REGEX, base)):
not (self.PLATFORM == "windows" and re.match(WIN_ROOT_REGEX, base)) and \
not (self.PLATFORM != "windows" and re.match(NIX_ROOT_REGEX, base)):
if base == "":
error_message = "Current file cannot be resolved."
else:
error_message = "'" + base + "' is an invalid alias."
sublime.error_message(error_message)
else:
attempt_open = True
if DEBUG:
print "AdvancedNewFile[Debug]: Creating file at " + file_path
logger.debug("Creating file at %s", file_path)
if not os.path.exists(file_path):
try:
self.create(file_path)
except Exception as e:
attempt_open = False
sublime.error_message("Cannot create '" + file_path + "'. See console for details")
print "Exception: %s" % e.strerror
logger.error("Exception: %s" % e.strerror)
if attempt_open:
if os.path.isdir(file_path):
if not re.search(r"(/|\\)$", file_path):
Expand Down Expand Up @@ -360,9 +363,8 @@ def on_query_completions(self, view, prefix, locations):

auto_complete_prefix = ""
if self.continue_previous_autocomplete() and prefix != "":
if DEBUG:
print "AdvancedNewFile[Debug]: (Prev) Suggestions"
print pac.prev_suggestions
logger.debug("(Prev) Suggestions")
logger.debug(pac.prev_suggestions)
if len(pac.prev_suggestions) > 1:
return (pac.prev_suggestions, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
elif len(pac.prev_suggestions) == 1:
Expand Down Expand Up @@ -409,9 +411,8 @@ def on_query_completions(self, view, prefix, locations):
pac.prev_root = root_path
pac.prev_prefix = prefix
pac.prev_locations = locations
if DEBUG:
print "AdvancedNewFile[Debug]: Suggestions:"
print suggestions
logger.debug("Suggestions:")
logger.debug(suggestions)
return (suggestions, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

def generate_project_auto_complete(self, base):
Expand Down Expand Up @@ -541,6 +542,6 @@ def get_settings(view):
else:
local_settings[key] = project_settings[key]
else:
print "AdvancedNewFile[Warning]: Invalid key '" + key + "' in project settings."
logger.error("AdvancedNewFile[Warning]: Invalid key '%s' in project settings.", key)

return local_settings
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog for AdvancedNewFile

- 2 February 2013
- Update to be compatible with Sublime Text 3.
- 14 January 2013
- Add `alias_root` setting, used with aliases with relative paths.
- Add setting to allow user to specify which folder from the project should be used.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# AdvancedNewFile
Advanced file creation for Sublime Text 2
Advanced file creation for Sublime Text 2 and Sublime Text 3.

## Overview

Expand Down Expand Up @@ -28,7 +28,7 @@ Simply bring up the AdvancedNewFile input through the appropriate [key binding](
The default directory is specified by the `default_root` setting. By default, it will be the top directory of the folders listed in the window. If this cannot be resolved, the home directory will be used. See [Settings](https://github.com/skuroda/Sublime-AdvancedNewFile#settings) (`default_root`) for more information.

## Keymaps
If you have issues with keymaps, consider running [FindKeyConflicts](https://github.com/skuroda/FindKeyConflicts), also available through the package manager.
If you have issues with keymaps, consider running [FindKeyConflicts](https://github.com/skuroda/FindKeyConflicts), also available through the package manager. Alternatively, set command logging to true by entering `sublime.log_commands(True)` in the Sublime Text console.

### Windows
`ctrl+alt+n`: General keymap to create new files.
Expand Down

0 comments on commit 03276e6

Please sign in to comment.