# Path: plugins/lib/view_utils.py
# def has_file_ext(view, ext):
#     """Returns `True` if `view` has file extension `ext`.
#     `ext` may be specified with or without leading ".".
#     """
#     if not view.file_name() or not ext.strip().replace('.', ''):
#         return False
# 
#     if not ext.startswith('.'):
#         ext = '.' + ext
# 
#     return view.file_name().endswith(ext)
# 
# def get_text(view):
#     """Returns the whole string of a buffer. Alias for `coorded_substr(view)`.
#     """
#     return coorded_substr(view)
# 
# Path: plugins/lib/syntax_paths.py
# _BASE_TMPL = "Packages/{package_name}/Package/{sub_package_name}/{file_name}{ext}"
# PLIST              = _build_path("Property List")
# BUILD_SYSTEM       = _build_path("Sublime Text Build System")
# COLOR_SCHEME       = _build_path("Sublime Text Color Scheme")
# COMMANDS           = _build_path("Sublime Text Commands")
# COMPLETIONS        = _build_path("Sublime Text Completions")
# KEYMAP             = _build_path("Sublime Text Keymap")
# MACROS             = _build_path("Sublime Text Macros")
# MENU               = _build_path("Sublime Text Menu")
# MOUSEMAP           = _build_path("Sublime Text Mousemap")
# SETTINGS           = _build_path("Sublime Text Settings")
# SNIPPET            = _build_path("Sublime Text Snippet")
# SNIPPET_RAW        = _build_path("Sublime Text Snippet", "Sublime Text Snippet (Raw)")
# SYNTAX_DEF         = _build_path("Sublime Text Syntax Definition")
# THEME              = _build_path("Sublime Text Theme")
# TM_PREFERENCES     = _build_path("TextMate Preferences")
# TM_SYNTAX_DEF_JSON = _build_path("TextMate Syntax Definition (JSON)", textmate=True)
# TM_SYNTAX_DEF_YAML = _build_path("TextMate Syntax Definition (YAML)", textmate=True)
# XML = "Packages/XML/XML.tmLanguage"
# def _build_path(sub_package_name, file_name=None, textmate=False):


from xml.etree import ElementTree as ET
from sublime import Region
from .lib.view_utils import has_file_ext, get_text
from .lib import syntax_paths
import sublime_plugin
def _insert_unindented(view, text):
    # circumvent auto-indentation with this method
    view.run_command('insert_snippet', {'contents': "$bla", 'bla': text})


class PackagedevSnippetFromRawSnippetCommand(sublime_plugin.TextCommand):
    def is_enabled(self):
        return self.view.match_selector(0, "source.sublime.snippet - source.json")

    def run(self, edit):
        content = get_text(self.view)
        self.view.erase(edit, Region(0, self.view.size()))
        self.view.run_command('insert_snippet', {'name': SNIPPET_PATH})

        # defuse CDATA end sequence with an undefined variable
        content = content.replace("]]>", "]]$UNDEFINED>")
        # Insert existing contents into CDATA section. We rely on the fact
        # that Sublime will place the first selection in the first field of
        # the newly inserted snippet.
        _insert_unindented(self.view, content)
        self.view.run_command('next_field')

        self.view.assign_syntax(syntax_paths.SNIPPET)


class PackagedevRawSnippetFromSnippetCommand(sublime_plugin.TextCommand):
    def is_enabled(self):
        return has_file_ext(self.view, 'sublime-snippet')

    def run(self, edit):

# Path: plugins/lib/view_utils.py
# def has_file_ext(view, ext):
#     """Returns `True` if `view` has file extension `ext`.
#     `ext` may be specified with or without leading ".".
#     """
#     if not view.file_name() or not ext.strip().replace('.', ''):
#         return False
# 
#     if not ext.startswith('.'):
#         ext = '.' + ext
# 
#     return view.file_name().endswith(ext)
# 
# def get_text(view):
#     """Returns the whole string of a buffer. Alias for `coorded_substr(view)`.
#     """
#     return coorded_substr(view)
# 
# Path: plugins/lib/syntax_paths.py
# _BASE_TMPL = "Packages/{package_name}/Package/{sub_package_name}/{file_name}{ext}"
# PLIST              = _build_path("Property List")
# BUILD_SYSTEM       = _build_path("Sublime Text Build System")
# COLOR_SCHEME       = _build_path("Sublime Text Color Scheme")
# COMMANDS           = _build_path("Sublime Text Commands")
# COMPLETIONS        = _build_path("Sublime Text Completions")
# KEYMAP             = _build_path("Sublime Text Keymap")
# MACROS             = _build_path("Sublime Text Macros")
# MENU               = _build_path("Sublime Text Menu")
# MOUSEMAP           = _build_path("Sublime Text Mousemap")
# SETTINGS           = _build_path("Sublime Text Settings")
# SNIPPET            = _build_path("Sublime Text Snippet")
# SNIPPET_RAW        = _build_path("Sublime Text Snippet", "Sublime Text Snippet (Raw)")
# SYNTAX_DEF         = _build_path("Sublime Text Syntax Definition")
# THEME              = _build_path("Sublime Text Theme")
# TM_PREFERENCES     = _build_path("TextMate Preferences")
# TM_SYNTAX_DEF_JSON = _build_path("TextMate Syntax Definition (JSON)", textmate=True)
# TM_SYNTAX_DEF_YAML = _build_path("TextMate Syntax Definition (YAML)", textmate=True)
# XML = "Packages/XML/XML.tmLanguage"
# def _build_path(sub_package_name, file_name=None, textmate=False):


# Path: plugins/snippet_dev.py
from xml.etree import ElementTree as ET
from sublime import Region
from .lib.view_utils import has_file_ext, get_text
from .lib import syntax_paths
import sublime_plugin

def _insert_unindented(view, text):
    # circumvent auto-indentation with this method
    view.run_command('insert_snippet', {'contents': "$bla", 'bla': text})


class PackagedevSnippetFromRawSnippetCommand(sublime_plugin.TextCommand):
    def is_enabled(self):
        return self.view.match_selector(0, "source.sublime.snippet - source.json")

    def run(self, edit):
        content = get_text(self.view)
        self.view.erase(edit, Region(0, self.view.size()))
        self.view.run_command('insert_snippet', {'name': SNIPPET_PATH})

        # defuse CDATA end sequence with an undefined variable
        content = content.replace("]]>", "]]$UNDEFINED>")
        # Insert existing contents into CDATA section. We rely on the fact
        # that Sublime will place the first selection in the first field of
        # the newly inserted snippet.
        _insert_unindented(self.view, content)
        self.view.run_command('next_field')

        self.view.assign_syntax(syntax_paths.SNIPPET)


class PackagedevRawSnippetFromSnippetCommand(sublime_plugin.TextCommand):
    def is_enabled(self):
        return has_file_ext(self.view, 'sublime-snippet')

    def run(self, edit):

        snippet = get_text(self.view)