示例#1
0
class RequireFromWordCommand(sublime_plugin.TextCommand):

    """Text command for adding require statment from hovering over word."""

    def run(self, edit):
        """Called when the command is run."""
        self.edit = edit
        cursor = self.view.sel()[0]
        word_region = self.view.word(cursor)
        word_text = self.view.substr(word_region)
        import_undefined_vars = utils.get_project_pref('import_undefined_vars',
                                                       view=self.view)

        self.module_loader = ModuleLoader(self.view.file_name())
        self.files = self.module_loader.get_file_list()

        words = [word_text]

        if cursor.empty() and import_undefined_vars:
            undef_vars = self.find_undefined_vars()
            if undef_vars:
                words = undef_vars

        for word in words:
            module = utils.best_fuzzy_match(self.files, word)
            self.view.run_command('require_insert_helper', {
                'args': {
                    'module': module,
                    'type': 'word'
                }
            })

    def find_undefined_vars(self):
        """Executes ESLint if it is installed as local module and finds undefined variables"""
        eslint_path = os.path.join(self.module_loader.project_folder,
                                   'node_modules',
                                   'eslint', 'bin', 'eslint.js')
        if not os.path.exists(eslint_path):
            return []

        args = ['-f', 'compact', '--stdin',
                '--stdin-filename', self.view.file_name()]

        try:
            text = self.view.substr(sublime.Region(0, self.view.size()))
            output = node_bridge(text, eslint_path, args)
        except Exception as e:
            return []

        return list(set([
            re.search(ESLINT_UNDEF_RE, line).group(1)
            for line in output.split('\n')
            if '(no-undef)' in line
        ]))
示例#2
0
class RequireFromWordCommand(sublime_plugin.TextCommand):
    """Text command for adding require statment from hovering over word."""
    def run(self, edit):
        """Called when the command is run."""
        self.edit = edit
        cursor = self.view.sel()[0]
        word_region = self.view.word(cursor)
        word_text = self.view.substr(word_region)
        import_undefined_vars = utils.get_project_pref('import_undefined_vars',
                                                       view=self.view)

        self.module_loader = ModuleLoader(self.view.file_name())
        self.files = self.module_loader.get_file_list()

        words = [word_text]

        if cursor.empty() and import_undefined_vars:
            undef_vars = self.find_undefined_vars()
            if undef_vars:
                words = undef_vars

        for word in words:
            module = utils.best_fuzzy_match(self.files, word)
            self.view.run_command('require_insert_helper',
                                  {'args': {
                                      'module': module,
                                      'type': 'word'
                                  }})

    def find_undefined_vars(self):
        """Executes ESLint if it is installed as local module and finds undefined variables"""
        eslint_path = os.path.join(self.module_loader.project_folder,
                                   'node_modules', 'eslint', 'bin',
                                   'eslint.js')
        if not os.path.exists(eslint_path):
            return []

        args = [
            '-f', 'compact', '--stdin', '--stdin-filename',
            self.view.file_name()
        ]

        try:
            text = self.view.substr(sublime.Region(0, self.view.size()))
            output = node_bridge(text, eslint_path, args)
        except Exception as e:
            return []

        return list(
            set([
                re.search(ESLINT_UNDEF_RE, line).group(1)
                for line in output.split('\n') if '(no-undef)' in line
            ]))
示例#3
0
class RequireFromWordCommand(sublime_plugin.TextCommand):

    """Text command for adding require statment from hovering over word."""

    def run(self, edit):
        """Called when the command is run."""
        self.edit = edit
        cursor = self.view.sel()[0]
        word_region = self.view.word(cursor)
        word_text = self.view.substr(word_region)

        self.module_loader = ModuleLoader(self.view.file_name())
        files = self.module_loader.get_file_list()

        module = utils.best_fuzzy_match(files, word_text)
        self.view.run_command("require_insert_helper", {"args": {"module": module, "type": "word"}})
示例#4
0
class RequireFromWordCommand(sublime_plugin.TextCommand):

    """Text command for adding require statment from hovering over word."""

    def run(self, edit):
        """Called when the command is run."""
        self.edit = edit
        cursor = self.view.sel()[0]
        word_region = self.view.word(cursor)
        word_text = self.view.substr(word_region)

        self.module_loader = ModuleLoader(self.view.file_name())
        files = self.module_loader.get_file_list()

        module = utils.best_fuzzy_match(files, word_text)
        self.view.run_command('require_insert_helper', {
            'args': {
                'module': module,
                'type': 'word'
            }
        })
示例#5
0
class RequireCommand(sublime_plugin.TextCommand):
    """Text Command which prompts for a module and inserts it into the file."""
    def run(self, edit, command):
        """Called when the command is run."""
        self.edit = edit

        # Simple Require Command
        if command is 'simple':
            # Must copy the core modules so modifying self.files
            # does not change the core_modules list
            self.files = list(core_modules)
            func = self.insert
        # Export Command
        else:
            self.files = []
            self.exports = ['------ Select One or More Options ------']
            self.selected_exports = []
            func = self.show_exports

        self.module_loader = ModuleLoader(self.view.file_name())
        self.files += self.module_loader.get_file_list()

        sublime.active_window().show_quick_panel(
            self.files, self.on_done_call_func(self.files, func))

    def on_path_entered(self, path):
        """When a path is entered, set the project data."""
        sublime.active_window().set_project_data({'folders': [{'path': path}]})

    def on_path_changed(self, text):
        """Do nothing when path is changed."""
        return None

    def on_canceled(self):
        """Send error message if user cancels after entering a path."""
        return sublime.error_message(
            'You must configure the absolute path '
            'for your project before using NodeRequirer. '
            'See the readme for more information.')

    def on_done_call_func(self, choices, func):
        """Return a function which is used with sublime list picking."""
        def on_done(index):
            if index >= 0:
                return func(choices[index])

        return on_done

    def insert(self, module):
        """Run the insert helper command with the module selected."""
        self.view.run_command('require_insert_helper',
                              {'args': {
                                  'module': module,
                                  'type': 'standard'
                              }})

    def show_exports(self, module=None):
        """Prompt selection of exports for previously selected file."""
        if module is not None:
            self.selected_module = module
            self.exports += self.module_loader.get_exports(module)
        sublime.set_timeout(
            lambda: sublime.active_window().show_quick_panel(
                self.exports, self.on_export_done), 10)

    def on_export_done(self, index):
        """Handle selection of exports."""
        if index > 0:
            self.exports[0] = ['------ Finish Selecting ------']
            # Add selected export to selected_exports list and
            # remove it from the list
            self.selected_exports.append(self.exports.pop(index))

            if len(self.exports) > 1:
                # Show remaining exports for further selection
                self.show_exports()
            elif len(self.selected_exports) > 0:
                # insert current selected exports
                self.insert_exports()
        elif index == 0 and len(self.selected_exports) > 0:
            # insert current selected exports
            self.insert_exports()

    def insert_exports(self):
        """Run export helper to insert selected exports into file."""
        self.view.run_command(
            'export_insert_helper', {
                'args': {
                    'module': self.selected_module,
                    'exports': self.selected_exports
                }
            })
示例#6
0
class RequireCommand(sublime_plugin.TextCommand):

    """Text Command which prompts for a module and inserts it into the file."""

    def run(self, edit, command):
        """Called when the command is run."""
        self.edit = edit

        # Simple Require Command
        if command is "simple":
            # Must copy the core modules so modifying self.files
            # does not change the core_modules list
            self.files = list(core_modules)
            func = self.insert
        # Export Command
        else:
            self.files = []
            self.exports = ["------ Select One or More Options ------"]
            self.selected_exports = []
            func = self.show_exports

        self.module_loader = ModuleLoader(self.view.file_name())
        self.files += self.module_loader.get_file_list()

        sublime.active_window().show_quick_panel(self.files, self.on_done_call_func(self.files, func))

    def on_path_entered(self, path):
        """When a path is entered, set the project data."""
        sublime.active_window().set_project_data({"folders": [{"path": path}]})

    def on_path_changed(self, text):
        """Do nothing when path is changed."""
        return None

    def on_canceled(self):
        """Send error message if user cancels after entering a path."""
        return sublime.error_message(
            "You must configure the absolute path "
            "for your project before using NodeRequirer. "
            "See the readme for more information."
        )

    def on_done_call_func(self, choices, func):
        """Return a function which is used with sublime list picking."""

        def on_done(index):
            if index >= 0:
                return func(choices[index])

        return on_done

    def insert(self, module):
        """Run the insert helper command with the module selected."""
        self.view.run_command("require_insert_helper", {"args": {"module": module, "type": "standard"}})

    def show_exports(self, module=None):
        """Prompt selection of exports for previously selected file."""
        if module is not None:
            self.selected_module = module
            self.exports += self.module_loader.get_exports(module)
        sublime.set_timeout(lambda: sublime.active_window().show_quick_panel(self.exports, self.on_export_done), 10)

    def on_export_done(self, index):
        """Handle selection of exports."""
        if index > 0:
            self.exports[0] = ["------ Finish Selecting ------"]
            # Add selected export to selected_exports list and
            # remove it from the list
            self.selected_exports.append(self.exports.pop(index))

            if len(self.exports) > 1:
                # Show remaining exports for further selection
                self.show_exports()
            elif len(self.selected_exports) > 0:
                # insert current selected exports
                self.insert_exports()
        elif index == 0 and len(self.selected_exports) > 0:
            # insert current selected exports
            self.insert_exports()

    def insert_exports(self):
        """Run export helper to insert selected exports into file."""
        self.view.run_command(
            "export_insert_helper", {"args": {"module": self.selected_module, "exports": self.selected_exports}}
        )