Exemplo n.º 1
0
    def test_eat_identifier(self):
        def is_valid_id(candidate):
            result = HyperParser._eat_identifier(candidate, 0, len(candidate))
            if result == len(candidate):
                return True
            elif result == 0:
                return False
            else:
                err_msg = "Unexpected result: {} (expected 0 or {}".format(
                    result, len(candidate))
                raise Exception(err_msg)

        # invalid first character which is valid elsewhere in an identifier
        self.assertFalse(is_valid_id('2notid'))

        # ASCII-only valid identifiers
        self.assertTrue(is_valid_id('valid_id'))
        self.assertTrue(is_valid_id('_valid_id'))
        self.assertTrue(is_valid_id('valid_id_'))
        self.assertTrue(is_valid_id('_2valid_id'))

        # keywords which should be "eaten"
        self.assertTrue(is_valid_id('True'))
        self.assertTrue(is_valid_id('False'))
        self.assertTrue(is_valid_id('None'))

        # keywords which should not be "eaten"
        self.assertFalse(is_valid_id('for'))
        self.assertFalse(is_valid_id('import'))
        self.assertFalse(is_valid_id('return'))

        # valid unicode identifiers
        self.assertTrue(is_valid_id('cliche'))
        self.assertTrue(is_valid_id('cliché'))
        self.assertTrue(is_valid_id('a٢'))

        # invalid unicode identifiers
        self.assertFalse(is_valid_id('2a'))
        self.assertFalse(is_valid_id('٢a'))
        self.assertFalse(is_valid_id('a²'))

        # valid identifier after "punctuation"
        self.assertEqual(HyperParser._eat_identifier('+ var', 0, 5),
                         len('var'))
        self.assertEqual(HyperParser._eat_identifier('+var', 0, 4), len('var'))
        self.assertEqual(HyperParser._eat_identifier('.var', 0, 4), len('var'))

        # invalid identifiers
        self.assertFalse(is_valid_id('+'))
        self.assertFalse(is_valid_id(' '))
        self.assertFalse(is_valid_id(':'))
        self.assertFalse(is_valid_id('?'))
        self.assertFalse(is_valid_id('^'))
        self.assertFalse(is_valid_id('\\'))
        self.assertFalse(is_valid_id('"'))
        self.assertFalse(is_valid_id('"a string"'))
Exemplo n.º 2
0
    def test_eat_identifier(self):
        def is_valid_id(candidate):
            result = HyperParser._eat_identifier(candidate, 0, len(candidate))
            if result == len(candidate):
                return True
            elif result == 0:
                return False
            else:
                err_msg = "Unexpected result: {} (expected 0 or {}".format(
                    result, len(candidate)
                )
                raise Exception(err_msg)

        # invalid first character which is valid elsewhere in an identifier
        self.assertFalse(is_valid_id('2notid'))

        # ASCII-only valid identifiers
        self.assertTrue(is_valid_id('valid_id'))
        self.assertTrue(is_valid_id('_valid_id'))
        self.assertTrue(is_valid_id('valid_id_'))
        self.assertTrue(is_valid_id('_2valid_id'))

        # keywords which should be "eaten"
        self.assertTrue(is_valid_id('True'))
        self.assertTrue(is_valid_id('False'))
        self.assertTrue(is_valid_id('None'))

        # keywords which should not be "eaten"
        self.assertFalse(is_valid_id('for'))
        self.assertFalse(is_valid_id('import'))
        self.assertFalse(is_valid_id('return'))

        # valid unicode identifiers
        self.assertTrue(is_valid_id('cliche'))
        self.assertTrue(is_valid_id('cliché'))
        self.assertTrue(is_valid_id('a٢'))

        # invalid unicode identifiers
        self.assertFalse(is_valid_id('2a'))
        self.assertFalse(is_valid_id('٢a'))
        self.assertFalse(is_valid_id('a²'))

        # valid identifier after "punctuation"
        self.assertEqual(HyperParser._eat_identifier('+ var', 0, 5), len('var'))
        self.assertEqual(HyperParser._eat_identifier('+var', 0, 4), len('var'))
        self.assertEqual(HyperParser._eat_identifier('.var', 0, 4), len('var'))

        # invalid identifiers
        self.assertFalse(is_valid_id('+'))
        self.assertFalse(is_valid_id(' '))
        self.assertFalse(is_valid_id(':'))
        self.assertFalse(is_valid_id('?'))
        self.assertFalse(is_valid_id('^'))
        self.assertFalse(is_valid_id('\\'))
        self.assertFalse(is_valid_id('"'))
        self.assertFalse(is_valid_id('"a string"'))
Exemplo n.º 3
0
    def test_eat_identifier(self):
        def is_valid_id(candidate):
            result = HyperParser._eat_identifier(candidate, 0, len(candidate))
            if result == len(candidate):
                return True
            elif result == 0:
                return False
            else:
                err_msg = "Unexpected result: {} (expected 0 or {}".format(result, len(candidate))
                raise Exception(err_msg)

        # invalid first character which is valid elsewhere in an identifier
        self.assertFalse(is_valid_id("2notid"))

        # ASCII-only valid identifiers
        self.assertTrue(is_valid_id("valid_id"))
        self.assertTrue(is_valid_id("_valid_id"))
        self.assertTrue(is_valid_id("valid_id_"))
        self.assertTrue(is_valid_id("_2valid_id"))

        # keywords which should be "eaten"
        self.assertTrue(is_valid_id("True"))
        self.assertTrue(is_valid_id("False"))
        self.assertTrue(is_valid_id("None"))

        # keywords which should not be "eaten"
        self.assertFalse(is_valid_id("for"))
        self.assertFalse(is_valid_id("import"))
        self.assertFalse(is_valid_id("return"))

        # valid unicode identifiers
        self.assertTrue(is_valid_id("cliche"))
        self.assertTrue(is_valid_id("cliché"))
        self.assertTrue(is_valid_id("a٢"))

        # invalid unicode identifiers
        self.assertFalse(is_valid_id("2a"))
        self.assertFalse(is_valid_id("٢a"))
        self.assertFalse(is_valid_id("a²"))

        # valid identifier after "punctuation"
        self.assertEqual(HyperParser._eat_identifier("+ var", 0, 5), len("var"))
        self.assertEqual(HyperParser._eat_identifier("+var", 0, 4), len("var"))
        self.assertEqual(HyperParser._eat_identifier(".var", 0, 4), len("var"))

        # invalid identifiers
        self.assertFalse(is_valid_id("+"))
        self.assertFalse(is_valid_id(" "))
        self.assertFalse(is_valid_id(":"))
        self.assertFalse(is_valid_id("?"))
        self.assertFalse(is_valid_id("^"))
        self.assertFalse(is_valid_id("\\"))
        self.assertFalse(is_valid_id('"'))
        self.assertFalse(is_valid_id('"a string"'))
Exemplo n.º 4
0
 def paren_closed_event(self, event):
     "Handle user input of closer."
     # If user bound non-closer to <<paren-closed>>, quit.
     closer = self.text.get("insert-1c")
     if closer not in _openers:
         return
     hp = HyperParser(self.editwin, "insert-1c")
     if not hp.is_in_code():
         return
     indices = hp.get_surrounding_brackets(_openers[closer], True)
     self.finish_paren_event(indices)
     return  # Allow calltips to see ')'
Exemplo n.º 5
0
 def paren_closed_event(self, event):
     "Handle user input of closer."
     # If user bound non-closer to <<paren-closed>>, quit.
     closer = self.text.get("insert-1c")
     if closer not in _openers:
         return
     hp = HyperParser(self.editwin, "insert-1c")
     if not hp.is_in_code():
         return
     indices = hp.get_surrounding_brackets(_openers[closer], True)
     self.finish_paren_event(indices)
     return  # Allow calltips to see ')'
Exemplo n.º 6
0
 def paren_closed_event(self, event):
     closer = self.text.get('insert-1c')
     if closer not in _openers:
         return
     hp = HyperParser(self.editwin, 'insert-1c')
     if not hp.is_in_code():
         return
     indices = hp.get_surrounding_brackets(_openers[closer], True)
     if indices is None:
         self.bell()
         return
     self.activate_restore()
     self.create_tag(indices)
     self.set_timeout()
Exemplo n.º 7
0
 def paren_closed_event(self, event):
     # If it was a shortcut and not really a closing paren, quit.
     closer = self.text.get("insert-1c")
     if closer not in _openers:
         return
     hp = HyperParser(self.editwin, "insert-1c")
     if not hp.is_in_code():
         return
     indices = hp.get_surrounding_brackets(_openers[closer], True)
     if indices is None:
         self.bell()
         return
     self.activate_restore()
     self.create_tag(indices)
     self.set_timeout()
Exemplo n.º 8
0
 def paren_closed_event(self, event):
     # If it was a shortcut and not really a closing paren, quit.
     closer = self.text.get("insert-1c")
     if closer not in _openers:
         return
     hp = HyperParser(self.editwin, "insert-1c")
     if not hp.is_in_code():
         return
     indices = hp.get_surrounding_brackets(_openers[closer], True)
     if indices is None:
         self.warn_mismatched()
         return
     self.activate_restore()
     self.create_tag(indices)
     self.set_timeout()
Exemplo n.º 9
0
 def paren_closed_event(self, event):
     "Handle user input of closer."
     # If user bound non-closer to <<paren-closed>>, quit.
     closer = self.text.get("insert-1c")
     if closer not in _openers:
         return
     hp = HyperParser(self.editwin, "insert-1c")
     if not hp.is_in_code():
         return
     indices = hp.get_surrounding_brackets(_openers[closer], True)
     if indices is None:
         self.bell()
         return
     self.activate_restore()
     self.create_tag(indices)
     self.set_timeout()
     return
Exemplo n.º 10
0
 def paren_closed_event(self, event):
     "Handle user input of closer."
     # If user bound non-closer to <<paren-closed>>, quit.
     closer = self.text.get("insert-1c")
     if closer not in _openers:
         return
     hp = HyperParser(self.editwin, "insert-1c")
     if not hp.is_in_code():
         return
     indices = hp.get_surrounding_brackets(_openers[closer], True)
     if indices is None:
         self.bell()
         return
     self.activate_restore()
     self.create_tag(indices)
     self.set_timeout()
     return
Exemplo n.º 11
0
    def open_completions(self, args):
        """Find the completions and create the AutoCompleteWindow.
        Return True if successful (no syntax error or so found).
        If complete is True, then if there's nothing to complete and no
        start of completion, won't open completions and return False.
        If mode is given, will open a completion list only in this mode.
        """
        evalfuncs, complete, wantwin, mode = args
        # Cancel another delayed call, if it exists.
        if self._delayed_completion_id is not None:
            self.text.after_cancel(self._delayed_completion_id)
            self._delayed_completion_id = None

        hp = HyperParser(self.editwin, "insert")
        curline = self.text.get("insert linestart", "insert")
        i = j = len(curline)
        if hp.is_in_string() and (not mode or mode == FILES):
            # Find the beginning of the string.
            # fetch_completions will look at the file system to determine
            # whether the string value constitutes an actual file name
            # XXX could consider raw strings here and unescape the string
            # value if it's not raw.
            self._remove_autocomplete_window()
            mode = FILES
            # Find last separator or string start
            while i and curline[i - 1] not in "'\"" + SEPS:
                i -= 1
            comp_start = curline[i:j]
            j = i
            # Find string start
            while i and curline[i - 1] not in "'\"":
                i -= 1
            comp_what = curline[i:j]
        elif hp.is_in_code() and (not mode or mode == ATTRS):
            self._remove_autocomplete_window()
            mode = ATTRS
            while i and (curline[i - 1] in ID_CHARS
                         or ord(curline[i - 1]) > 127):
                i -= 1
            comp_start = curline[i:j]
            if i and curline[i - 1] == '.':  # Need object with attributes.
                hp.set_index("insert-%dc" % (len(curline) - (i - 1)))
                comp_what = hp.get_expression()
                if (not comp_what
                        or (not evalfuncs and comp_what.find('(') != -1)):
                    return None
            else:
                comp_what = ""
        else:
            return None

        if complete and not comp_what and not comp_start:
            return None
        comp_lists = self.fetch_completions(comp_what, mode)
        if not comp_lists[0]:
            return None
        self.autocompletewindow = self._make_autocomplete_window()
        return not self.autocompletewindow.show_window(
            comp_lists, "insert-%dc" % len(comp_start), complete, mode,
            wantwin)
Exemplo n.º 12
0
    def open_calltip(self, evalfuncs):
        self._remove_calltip_window()

        hp = HyperParser(self.editwin, "insert")
        sur_paren = hp.get_surrounding_brackets('(')
        if not sur_paren:
            return
        hp.set_index(sur_paren[0])
        expression  = hp.get_expression()
        if not expression:
            return
        if not evalfuncs and (expression.find('(') != -1):
            return
        argspec = self.fetch_tip(expression)
        if not argspec:
            return
        self.active_calltip = self._calltip_window()
        self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1])
Exemplo n.º 13
0
 def flash_paren_event(self, event):
     indices = (HyperParser(self.editwin,
                            "insert").get_surrounding_brackets())
     if indices is None:
         self.bell()
         return
     self.activate_restore()
     self.create_tag(indices)
     self.set_timeout_last()
Exemplo n.º 14
0
 def is_valid_id(candidate):
     result = HyperParser._eat_identifier(candidate, 0, len(candidate))
     if result == len(candidate):
         return True
     elif result == 0:
         return False
     else:
         err_msg = "Unexpected result: {} (expected 0 or {}".format(result, len(candidate))
         raise Exception(err_msg)
Exemplo n.º 15
0
 def is_valid_id(candidate):
     result = HyperParser._eat_identifier(candidate, 0, len(candidate))
     if result == len(candidate):
         return True
     elif result == 0:
         return False
     else:
         err_msg = "Unexpected result: {} (expected 0 or {}".format(
             result, len(candidate))
         raise Exception(err_msg)
Exemplo n.º 16
0
 def flash_paren_event(self, event):
     "Handle editor 'show surrounding parens' event (menu or shortcut)."
     indices = (HyperParser(self.editwin,
                            "insert").get_surrounding_brackets())
     if indices is None:
         self.bell()
         return "break"
     self.activate_restore()
     self.create_tag(indices)
     self.set_timeout()
     return "break"
Exemplo n.º 17
0
    def test_eat_identifier(self):
        def is_valid_id(candidate):
            result = HyperParser._eat_identifier(candidate, 0, len(candidate))
            if result == len(candidate):
                return True
            elif result == 0:
                return False
            else:
                err_msg = 'Unexpected result: {} (expected 0 or {}'.format(
                    result, len(candidate))
                raise Exception(err_msg)

        self.assertFalse(is_valid_id('2notid'))
        self.assertTrue(is_valid_id('valid_id'))
        self.assertTrue(is_valid_id('_valid_id'))
        self.assertTrue(is_valid_id('valid_id_'))
        self.assertTrue(is_valid_id('_2valid_id'))
        self.assertTrue(is_valid_id('True'))
        self.assertTrue(is_valid_id('False'))
        self.assertTrue(is_valid_id('None'))
        self.assertFalse(is_valid_id('for'))
        self.assertFalse(is_valid_id('import'))
        self.assertFalse(is_valid_id('return'))
        self.assertTrue(is_valid_id('cliche'))
        self.assertTrue(is_valid_id('cliché'))
        self.assertTrue(is_valid_id('a٢'))
        self.assertFalse(is_valid_id('2a'))
        self.assertFalse(is_valid_id('٢a'))
        self.assertFalse(is_valid_id('a²'))
        self.assertEqual(HyperParser._eat_identifier('+ var', 0, 5),
                         len('var'))
        self.assertEqual(HyperParser._eat_identifier('+var', 0, 4), len('var'))
        self.assertEqual(HyperParser._eat_identifier('.var', 0, 4), len('var'))
        self.assertFalse(is_valid_id('+'))
        self.assertFalse(is_valid_id(' '))
        self.assertFalse(is_valid_id(':'))
        self.assertFalse(is_valid_id('?'))
        self.assertFalse(is_valid_id('^'))
        self.assertFalse(is_valid_id('\\'))
        self.assertFalse(is_valid_id('"'))
        self.assertFalse(is_valid_id('"a string"'))
Exemplo n.º 18
0
    def calltip_event(self, event=None):
        window = self.docWindow.window
        if window is None:
            return

        if not window.update_calltip.get():
            # don't process calltip event
            return

        # get calltip
        # code borrows from CallTips.py::open_calltip
        evalfuncs = False
        hp = HyperParser(self.editwin, "insert")
        sur_paren = hp.get_surrounding_brackets('(')
        if not sur_paren:
            return
        hp.set_index(sur_paren[0])
        name = hp.get_expression()
        if not name or (not evalfuncs and name.find('(') != -1):
            return

        w = window
        w.entry.delete("0", "end")
        w.entry.insert("insert", name)
        w.get_doc()
Exemplo n.º 19
0
    def open_completions(self, evalfuncs, complete, userWantsWin, mode=None):
        """Find the completions and create the AutoCompleteWindow.
        Return True if successful (no syntax error or so found).
        if complete is True, then if there's nothing to complete and no
        start of completion, won't open completions and return False.
        If mode is given, will open a completion list only in this mode.
        """
        # Cancel another delayed call, if it exists.
        if self._delayed_completion_id is not None:
            self.text.after_cancel(self._delayed_completion_id)
            self._delayed_completion_id = None

        hp = HyperParser(self.editwin, "insert")
        curline = self.text.get("insert linestart", "insert")
        i = j = len(curline)
        if hp.is_in_string() and (not mode or mode==COMPLETE_FILES):
            # Find the beginning of the string
            # fetch_completions will look at the file system to determine whether the
            # string value constitutes an actual file name
            # XXX could consider raw strings here and unescape the string value if it's
            # not raw.
            self._remove_autocomplete_window()
            mode = COMPLETE_FILES
            # Find last separator or string start
            while i and curline[i-1] not in "'\"" + SEPS:
                i -= 1
            comp_start = curline[i:j]
            j = i
            # Find string start
            while i and curline[i-1] not in "'\"":
                i -= 1
            comp_what = curline[i:j]
        elif hp.is_in_code() and (not mode or mode==COMPLETE_ATTRIBUTES):
            self._remove_autocomplete_window()
            mode = COMPLETE_ATTRIBUTES
            while i and (curline[i-1] in ID_CHARS or ord(curline[i-1]) > 127):
                i -= 1
            comp_start = curline[i:j]
            if i and curline[i-1] == '.':
                hp.set_index("insert-%dc" % (len(curline)-(i-1)))
                comp_what = hp.get_expression()
                if not comp_what or \
                   (not evalfuncs and comp_what.find('(') != -1):
                    return None
            else:
                comp_what = ""
        else:
            return None

        if complete and not comp_what and not comp_start:
            return None
        comp_lists = self.fetch_completions(comp_what, mode)
        if not comp_lists[0]:
            return None
        self.autocompletewindow = self._make_autocomplete_window()
        return not self.autocompletewindow.show_window(
                comp_lists, "insert-%dc" % len(comp_start),
                complete, mode, userWantsWin)
Exemplo n.º 20
0
 def open_completions(self, evalfuncs, complete, userWantsWin, mode=None):
     """Find the completions and create the AutoCompleteWindow.
     Return True if successful (no syntax error or so found).
     if complete is True, then if there's nothing to complete and no
     start of completion, won't open completions and return False.
     If mode is given, will open a completion list only in this mode.
     """
     if self._delayed_completion_id is not None:
         self.text.after_cancel(self._delayed_completion_id)
         self._delayed_completion_id = None
     hp = HyperParser(self.editwin, 'insert')
     curline = self.text.get('insert linestart', 'insert')
     i = j = len(curline)
     if hp.is_in_string() and (not mode or mode == COMPLETE_FILES):
         self._remove_autocomplete_window()
         mode = COMPLETE_FILES
         while i and curline[i - 1] not in '\'"' + SEPS:
             i -= 1
         comp_start = curline[i:j]
         j = i
         while i and curline[i - 1] not in '\'"':
             i -= 1
         comp_what = curline[i:j]
     elif hp.is_in_code() and (not mode or mode == COMPLETE_ATTRIBUTES):
         self._remove_autocomplete_window()
         mode = COMPLETE_ATTRIBUTES
         while i and (curline[i - 1] in ID_CHARS or ord(curline[i - 1]) >
             127):
             i -= 1
         comp_start = curline[i:j]
         if i and curline[i - 1] == '.':
             hp.set_index('insert-%dc' % (len(curline) - (i - 1)))
             comp_what = hp.get_expression()
             if not comp_what or not evalfuncs and comp_what.find('('
                 ) != -1:
                 return None
         else:
             comp_what = ''
     else:
         return None
     if complete and not comp_what and not comp_start:
         return None
     comp_lists = self.fetch_completions(comp_what, mode)
     if not comp_lists[0]:
         return None
     self.autocompletewindow = self._make_autocomplete_window()
     return not self.autocompletewindow.show_window(comp_lists, 
         'insert-%dc' % len(comp_start), complete, mode, userWantsWin)
Exemplo n.º 21
0
    def open_calltip(self, evalfuncs):
        """Maybe close an existing calltip and maybe open a new calltip.

        Called from (force_open|try_open|refresh)_calltip_event functions.
        """
        hp = HyperParser(self.editwin, "insert")
        sur_paren = hp.get_surrounding_brackets('(')

        # If not inside parentheses, no calltip.
        if not sur_paren:
            self.remove_calltip_window()
            return

        # If a calltip is shown for the current parentheses, do
        # nothing.
        if self.active_calltip:
            opener_line, opener_col = map(int, sur_paren[0].split('.'))
            if ((opener_line, opener_col) == (self.active_calltip.parenline,
                                              self.active_calltip.parencol)):
                return

        hp.set_index(sur_paren[0])
        try:
            expression = hp.get_expression()
        except ValueError:
            expression = None
        if not expression:
            # No expression before the opening parenthesis, e.g.
            # because it's in a string or the opener for a tuple:
            # Do nothing.
            return

        # At this point, the current index is after an opening
        # parenthesis, in a section of code, preceded by a valid
        # expression. If there is a calltip shown, it's not for the
        # same index and should be closed.
        self.remove_calltip_window()

        # Simple, fast heuristic: If the preceding expression includes
        # an opening parenthesis, it likely includes a function call.
        if not evalfuncs and (expression.find('(') != -1):
            return

        argspec = self.fetch_tip(expression)
        if not argspec:
            return
        self.active_calltip = self._calltip_window()
        self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1])
Exemplo n.º 22
0
 def open_calltip(self, evalfuncs):
     self._remove_calltip_window()
     hp = HyperParser(self.editwin, 'insert')
     sur_paren = hp.get_surrounding_brackets('(')
     if not sur_paren:
         return
     hp.set_index(sur_paren[0])
     expression = hp.get_expression()
     if not expression:
         return
     if not evalfuncs and expression.find('(') != -1:
         return
     argspec = self.fetch_tip(expression)
     if not argspec:
         return
     self.active_calltip = self._calltip_window()
     self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1])
Exemplo n.º 23
0
 def flash_paren_event(self, event):
     "Handle editor 'show surrounding parens' event (menu or shortcut)."
     indices = (HyperParser(self.editwin,
                            "insert").get_surrounding_brackets())
     self.finish_paren_event(indices)
     return "break"
Exemplo n.º 24
0
 def get_parser(self, index):
     """
     Return a parser object with index at 'index'
     """
     return HyperParser(self.editwin, index)