Ejemplo n.º 1
0
    def run(self, edit, **args):
        kw_module = args.get('module')
        self.backend = BackendManager.active_backend()

        if not kw_module:
            kw_decl = args.get('decl')
            if kw_decl is None:
                qsymbol = Common.get_qualified_symbol_at_region(self.view, self.view.sel()[0])
                kw_decl = qsymbol.qualified_name()

            current_file_name = self.view.file_name()

            # Phase 1: Get the candidate import modules: the backend's query_import returns the (flag, list) tuple.
            # If successful (flag == True), then invoke add_import to add the import to the module's existing
            # modules.
            (status, self.candidates) = self.backend.query_import(kw_decl, current_file_name)
            if status:
                if len(self.candidates) == 1:
                    self.add_import(edit, self.candidates[0].module.name)
                else:
                    self.view.window().show_quick_panel([[c.module.name] for c in self.candidates], self.on_done)
            else:
                if len(self.candidates) == 1:
                    Common.sublime_status_message(self.candidates[0])
                else:
                    sublime.message_dialog('\n'.join(self.candidates))
        else:
            self.add_import(edit, kw_module)
Ejemplo n.º 2
0
    def run(self, edit, **args):
        kw_module = args.get('module')
        self.backend = BackendManager.active_backend()

        if not kw_module:
            kw_decl = args.get('decl')
            if kw_decl is None:
                qsymbol = Common.get_qualified_symbol_at_region(self.view, self.view.sel()[0])
                kw_decl = qsymbol.qualified_name()

            current_file_name = self.view.file_name()

            # Phase 1: Get the candidate import modules: the backend's query_import returns the (flag, list) tuple.
            # If successful (flag == True), then invoke add_import to add the import to the module's existing
            # modules.
            (status, self.candidates) = self.backend.query_import(kw_decl, current_file_name)
            if status:
                if len(self.candidates) == 1:
                    self.add_import(edit, self.candidates[0].module.name)
                else:
                    self.view.window().show_quick_panel([[c.module.name] for c in self.candidates], self.on_done)
            else:
                if len(self.candidates) == 1:
                    Common.show_status_message(self.candidates[0])
                else:
                    sublime.message_dialog('\n'.join(self.candidates))
        else:
            self.add_import(edit, kw_module)
Ejemplo n.º 3
0
    def run(self, _edit, **_kwargs):
        qsymbol = Common.get_qualified_symbol_at_region(self.view, self.view.sel()[0])

        if Common.view_is_haskell_symbol_info(self.view):  # Go to within symbol info window
            loc = self.view.settings().get('location')
            if loc:
                self.view.window().open_file(loc, sublime.ENCODED_POSITION)
            else:
                Common.sublime_status_message('Source location of {0} not found'.format(qsymbol.name))
        else:
            backend = BackendManager.active_backend()
            whois_name = qsymbol.qualified_name()
            full_name = qsymbol.full_name()
            current_file_name = self.view.file_name()

            candidates = []
            module_candidates = []
            if not qsymbol.is_module():
                candidates = [decl for decl in backend.whois(whois_name, current_file_name) if decl.by_source()]
                if candidates:
                    if candidates[0].has_source_location():
                        self.view.window().open_file(candidates[0].get_source_location(), sublime.ENCODED_POSITION)
                    else:
                        cands = candidates[:]
                        candidates = []
                        for cand in cands:
                            for i in cand.imported:
                                candidates = [sym for sym in backend.symbol(lookup=cand.name, search_type='exact', source=True)
                                              if sym.module.name == i.module]
                                if candidates and candidates[0].has_source_location():
                                    self.view.window().open_file(candidates[0].get_source_location(), sublime.ENCODED_POSITION)
                    return
                else:
                    candidates = backend.symbol(lookup=qsymbol.name, search_type='exact', source=True)
            else:
                module_candidates = [m for m in backend.list_modules(source=True, module=full_name) if m.name == full_name]

            if not candidates and not module_candidates:
                Common.sublime_status_message('Declaration {0} not found'.format(qsymbol.name))
            else:
                candidates_len = len(candidates) if candidates is not None else 0
                module_candidates_len = len(module_candidates) if module_candidates is not None else 0

                if candidates_len + module_candidates_len == 1:
                    if candidates_len == 1:
                        self.view.window().open_file(candidates[0].get_source_location(), sublime.ENCODED_POSITION)
                    elif module_candidates_len == 1:
                        self.view.window().open_file(module_candidates[0].location.filename)
                    return
                else:
                    # many candidates
                    self.select_candidates = [([c.brief(use_unicode=False), c.get_source_location()], True) for c in candidates]
                    self.select_candidates += [([m.name, m.location.filename], False) for m in module_candidates]

                    just_names = [c[0] for c in self.select_candidates]
                    self.view.window().show_quick_panel(just_names, self.on_done, 0, 0, self.on_highlighted)
Ejemplo n.º 4
0
 def run(self, edit):
     result = self.get_best_type(self.get_types())
     if result:
         res = result.region(self.view)
         qsymbol = Common.get_qualified_symbol_at_region(self.view, self.view.word(res.begin()))
         line_begin = self.view.line(res).begin()
         prefix = self.view.substr(sublime.Region(line_begin, res.begin()))
         indent = re.search(r'(?P<indent>\s*)', prefix).group('indent')
         signature = '{0}{1} :: {2}\n'.format(indent, qsymbol.name, result.typename)
         self.view.insert(edit, line_begin, signature)
Ejemplo n.º 5
0
    def run(self, _edit, **kwargs):
        filename = kwargs.get('filename')
        module_name = kwargs.get('module_name')
        package_name = kwargs.get('package_name')
        symdb = kwargs.get('db')
        name = kwargs.get('name')
        qname = kwargs.get('qname')
        no_browse = kwargs.get('no_browse') or False

        if qname:
            self.full_name = qname
            self.current_file_name = self.view.file_name()
            # Try whois it, followed by file symbol and wider module searches
            self.candidates = self.collect_candidates(qname, name, filename, module_name, package_name, symdb)
        else:
            self.current_file_name = self.view.file_name()

            qsymbol = Common.get_qualified_symbol(qname) \
                      if qname \
                      else Common.get_qualified_symbol_at_region(self.view, self.view.sel()[0])
            module_word = qsymbol.module
            ident = qsymbol.name

            if ident is None:  # module
                if not no_browse:
                    self.view.window().run_command('sublime_haskell_browse_module', {'module_name': module_word,
                                                                                     'scope': self.current_file_name})
                return

            if not module_word and not ident:
                Common.sublime_status_message('No symbol selected')
                return

            self.whois_name = qsymbol.qualified_name()
            self.full_name = qsymbol.full_name()

            self.candidates = (BackendManager.active_backend().whois(self.whois_name, self.current_file_name) or [])[:1]

            if not self.candidates:
                self.candidates = BackendManager.active_backend().lookup(self.full_name, self.current_file_name)

            if not self.candidates:
                self.candidates = BackendManager.active_backend().symbol(lookup=self.full_name, search_type='exact')

        if not self.candidates:
            Common.sublime_status_message('Symbol {0} not found'.format(self.full_name))
        elif len(self.candidates) == 1:
            self.show_symbol_info(self.candidates[0])
        elif not no_browse:
            results = [[c.qualified_name(), c.defined_module().location.to_string()] for c in self.candidates]
            self.view.window().show_quick_panel(results, self.on_done)
Ejemplo n.º 6
0
    def run(self, edit, **_kwargs):
        filename = self.view.file_name()
        line, column = self.view.rowcol(self.view.sel()[0].b)
        project_name = Common.locate_cabal_project_from_view(self.view)[1]

        result = self.get_best_type(self.get_types(project_name, filename, int(line), int(column)))
        if result:
            res = result.region(self.view)
            qsymbol = Common.get_qualified_symbol_at_region(self.view, self.view.word(res.begin()))
            line_begin = self.view.line(res).begin()
            prefix = self.view.substr(sublime.Region(line_begin, res.begin()))
            indent = re.search(r'(?P<indent>\s*)', prefix).group('indent')
            signature = '{0}{1} :: {2}\n'.format(indent, qsymbol.name, result.typename)
            self.view.insert(edit, line_begin, signature)
Ejemplo n.º 7
0
    def run(self, edit, **_kwargs):
        filename = self.view.file_name()
        line, column = self.view.rowcol(self.view.sel()[0].b)
        project_name = Common.locate_cabal_project_from_view(self.view)[1]

        result = self.get_best_type(self.get_types(project_name, filename, int(line), int(column)))
        if result:
            res = result.region(self.view)
            qsymbol = Common.get_qualified_symbol_at_region(self.view, self.view.word(res.begin()))
            line_begin = self.view.line(res).begin()
            prefix = self.view.substr(sublime.Region(line_begin, res.begin()))
            indent = re.search(r'(?P<indent>\s*)', prefix).group('indent')
            signature = '{0}{1} :: {2}\n'.format(indent, qsymbol.name, result.typename)
            self.view.insert(edit, line_begin, signature)
Ejemplo n.º 8
0
    def run(self, _edit, **_kwargs):
        if Common.view_is_haskell_symbol_info(self.view):
            pack = self.view.settings().get('package')
            mod = self.view.settings().get('module')
            if pack and mod:
                webbrowser.open('http://hackage.haskell.org/package/{0}/docs/{1}.html'.format(pack, mod.replace('.', '-')))
        else:
            project_name = Common.locate_cabal_project_from_view(self.view)[1]
            qsymbol = Common.get_qualified_symbol_at_region(self.view, self.view.sel()[0])

            modules = []
            if qsymbol.is_module():  # module
                scope = self.view.file_name()
                if scope:
                    modules = [m for m in BackendManager.active_backend().scope_modules(project_name,
                                                                                        scope,
                                                                                        lookup=qsymbol.module,
                                                                                        search_type='exact')
                               if m.by_cabal()]
                else:
                    modules = [m for m in BackendManager.active_backend().list_modules(symdb=m.location.db)
                               if m.name == qsymbol.module and m.by_cabal()]
            else:  # symbol
                scope = self.view.file_name()
                if scope:
                    decls = BackendManager.active_backend().whois(qsymbol.qualified_name(), file=scope) or \
                            BackendManager.active_backend().lookup(qsymbol.full_name(), file=scope) or \
                            BackendManager.active_backend().symbol(lookup=qsymbol.full_name(), search_type='exact')
                    if not decls:
                        Common.sublime_status_message('Module for symbol {0} not found'.format(qsymbol.full_name()))
                        return
                    modules = [decl.defined_module() for decl in decls]

            if not modules:
                Common.sublime_status_message('Module {0} not found'.format(qsymbol.module))
            elif len(modules) == 1:
                pkg_id = modules[0].location.package.package_id()
                pkg_name = modules[0].name.replace('.', '-')
                webbrowser.open('http://hackage.haskell.org/package/{0}/docs/{1}.html'.format(pkg_id, pkg_name))
            else:
                self.candidates = modules[:]
                mod_strings = [[m.name, m.location.package.package_id()] for m in self.candidates]
                self.view.window().show_quick_panel(mod_strings, self.on_done)
Ejemplo n.º 9
0
    def run(self, edit, **kwargs):
        filename = kwargs.get('filename')
        decl = kwargs.get('decl')
        module_name = kwargs.get('module_name')

        self.full_name = decl
        self.current_file_name = filename
        self.edit = edit

        if module_name is not None:
            self.add_import(module_name)
            return

        if not self.current_file_name:
            self.current_file_name = self.view.file_name()

        if not self.full_name:
            qsymbol = Common.get_qualified_symbol_at_region(
                self.view,
                self.view.sel()[0])
            self.full_name = qsymbol.qualified_name()

        if hsdev.client.whois(self.full_name, self.current_file_name):
            Common.show_status_message('Symbol {0} already in scope'.format(
                self.full_name))
        else:
            self.candidates = hsdev.client.lookup(self.full_name,
                                                  self.current_file_name)

            if not self.candidates:
                Common.show_status_message('Symbol {0} not found'.format(
                    self.full_name))
            elif len(self.candidates) == 1:
                self.add_import(self.candidates[0].module.name)
            else:
                self.view.window().show_quick_panel([[c.module.name]
                                                     for c in self.candidates],
                                                    self.on_done)