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()
Beispiel #2
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()
Beispiel #3
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])
        name = hp.get_expression()
        if not name or (not evalfuncs and name.find("(") != -1):
            return
        arg_text = self.fetch_tip(name)
        if not arg_text:
            return
        self.calltip = self._make_calltip_window()
        self.calltip.showtip(arg_text, sur_paren[0], sur_paren[1])
Beispiel #4
0
 def flash_paren_event(self, event):
     indices = HyperParser(self.editpage, "insert").get_surrounding_brackets()
     if indices is None:
         self.warn_mismatched()
         return
     self.activate_restore()
     self.create_tag(indices)
     self.set_timeout_last()
Beispiel #5
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
            else:
                comp_what = ""
        else:
            return

        if complete and not comp_what and not comp_start:
            return
        comp_lists = self.fetch_completions(comp_what, mode)
        if not comp_lists[0]:
            return
        self.autocompletewindow = self._make_autocomplete_window()
        return not self.autocompletewindow.show_window(
            comp_lists, "insert-%dc" % len(comp_start), complete, mode,
            userWantsWin)
Beispiel #6
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):
            self._remove_autocomplete_window()
            mode = COMPLETE_FILES
            while i and curline[i-1] in FILENAME_CHARS:
                i -= 1
            comp_start = curline[i:j]
            j = i
            while i and curline[i-1] in FILENAME_CHARS+os.sep:
                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:
                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
            else:
                comp_what = ""
        else:
            return

        if complete and not comp_what and not comp_start:
            return
        comp_lists = self.fetch_completions(comp_what, mode)
        if not comp_lists[0]:
            return
        self.autocompletewindow = self._make_autocomplete_window()
        self.autocompletewindow.show_window(comp_lists,
                                            "insert-%dc" % len(comp_start),
                                            complete,
                                            mode,
                                            userWantsWin)
        return True
    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
            else:
                comp_what = ""
        else:
            return

        if complete and not comp_what and not comp_start:
            return
        comp_lists = self.fetch_completions(comp_what, mode)
        if not comp_lists[0]:
            return
        self.autocompletewindow = self._make_autocomplete_window()
        return not self.autocompletewindow.show_window(
                comp_lists, "insert-%dc" % len(comp_start),
                complete, mode, userWantsWin)
    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):
            self._remove_autocomplete_window()
            mode = COMPLETE_FILES
            while i and curline[i-1] in FILENAME_CHARS:
                i -= 1
            comp_start = curline[i:j]
            j = i
            while i and curline[i-1] in FILENAME_CHARS+os.sep:
                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:
                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
            else:
                comp_what = ""
        else:
            return

        if complete and not comp_what and not comp_start:
            return
        comp_lists = self.fetch_completions(comp_what, mode)
        if not comp_lists[0]:
            return
        self.autocompletewindow = self._make_autocomplete_window()
        self.autocompletewindow.show_window(comp_lists,
                                            "insert-%dc" % len(comp_start),
                                            complete,
                                            mode,
                                            userWantsWin)
        return True
Beispiel #9
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])
        name = hp.get_expression()
        if not name or (not evalfuncs and name.find('(') != -1):
            return
        arg_text = self.fetch_tip(name)
        if not arg_text:
            return
        self.calltip = self._make_calltip_window()
        self.calltip.showtip(arg_text, sur_paren[0], sur_paren[1])
Beispiel #10
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])
Beispiel #11
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

        # If the window is already open, show the big list of completions.
        # This means that a double Ctrl-space opens the big list every time, which is nice.
        if self.autocompletewindow is not None and self.autocompletewindow.autocompletewindow is not None:
            showbig = True
        else:
            showbig = False

        hp = HyperParser(self.editwin, "insert")
        curline = self.text.get("insert linestart", "insert")
        i = j = len(curline)

        # Check if it's an import statement.
        # This is seperated from the rest since the pattern check is in ModuleCompletion.
        imports = self.get_module_completion(curline)
        if imports:
            if imports == ([], []):
                return
            comp_lists = imports
            while i and curline[i - 1] in ID_CHARS:
                i -= 1
            comp_start = curline[i:j]
        elif self.dictkeys and hp.is_in_dict() and (not mode or mode==COMPLETE_KEYS) and evalfuncs:
            self._remove_autocomplete_window()
            mode = COMPLETE_KEYS
            while i and curline[i - 1] in ID_CHARS + '"' + "'":
                i -= 1
            comp_start = curline[i:j]
            if curline[i - 1:i] == "[":
                hp.set_index("insert-%dc" % (len(curline) - (i - 1)))
                comp_what = hp.get_expression()
            else:
                comp_what = ""
        elif (hp.is_in_string() or hp.is_in_command()) \
            and (not mode or mode==COMPLETE_FILES):
            self._remove_autocomplete_window()
            mode = COMPLETE_FILES
            while i and curline[i-1] in FILENAME_CHARS:
                i -= 1
            comp_start = curline[i:j]
            j = i
            while i and curline[i-1] in FILENAME_CHARS + SEPS:
                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:
                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
            else:
                comp_what = ""
        else:
            return

        # For everything but imports, call fetch_completions
        if not imports:
            if complete and not comp_what and not comp_start:
                return
            comp_lists = self.fetch_completions(comp_what, mode)
            if not comp_lists[0]:
                return

        # It's nice to be able to see the length of a tuple/list (but not anything more complicated)
        if comp_lists[0] == SHOWCALLTIP:
            parenleft = self.text.index('insert-1c')
            CallTip(self.text).showtip(comp_lists[1], parenleft, parenleft.split('.')[0] + '.end')
            return

        if mode == COMPLETE_ATTRIBUTES and not imports:
            calltips = self.editwin.extensions.get('CallTips')
            if calltips:
                args = calltips.arg_names(evalfuncs)
                if args:
                    args = [a + '=' for a in args]
                    comp_lists = sorted(comp_lists[0] + args), sorted(comp_lists[1] + args)

        # Check if we want to show only completion containing typed word.
        if self.onlycontaining:
            # Small optimization
            comp_lower = comp_start.lower()
            # Find such completions.
            comp_lists = [name for name in comp_lists[0] if comp_lower in name.lower()], comp_lists[1]
            # If none were found, look in big list.
            if not comp_lists[0]:
                comp_lists = [name for name in comp_lists[1] if comp_lower in name.lower()], comp_lists[1]
            # If still none were found, just return the big list - which is the default anyway.
            if not comp_lists[0]:
                comp_lists = comp_lists[1], comp_lists[1]

        if showbig:
            comp_lists = comp_lists[1], []

        self.autocompletewindow = self._make_autocomplete_window()
        return not self.autocompletewindow.show_window(
                comp_lists, "insert-%dc" % len(comp_start),
                complete, mode, userWantsWin, onlycontaining=self.onlycontaining)
Beispiel #12
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

        # If the window is already open, show the big list of completions.
        # This means that a double Ctrl-space opens the big list every time, which is nice.
        if self.autocompletewindow is not None and self.autocompletewindow.autocompletewindow is not None:
            showbig = True
        else:
            showbig = False

        hp = HyperParser(self.editwin, "insert")
        curline = self.text.get("insert linestart", "insert")
        i = j = len(curline)

        # Check if it's an import statement.
        # This is seperated from the rest since the pattern check is in ModuleCompletion.
        imports = self.get_module_completion(curline)
        if imports:
            if imports == ([], []):
                return
            comp_lists = imports
            while i and curline[i - 1] in ID_CHARS:
                i -= 1
            comp_start = curline[i:j]
        elif self.dictkeys and hp.is_in_dict() and (
                not mode or mode == COMPLETE_KEYS) and evalfuncs:
            self._remove_autocomplete_window()
            mode = COMPLETE_KEYS
            while i and curline[i - 1] in ID_CHARS + '"' + "'":
                i -= 1
            comp_start = curline[i:j]
            if curline[i - 1:i] == "[":
                hp.set_index("insert-%dc" % (len(curline) - (i - 1)))
                comp_what = hp.get_expression()
            else:
                comp_what = ""
        elif (hp.is_in_string() or hp.is_in_command()) \
            and (not mode or mode==COMPLETE_FILES):
            self._remove_autocomplete_window()
            mode = COMPLETE_FILES
            while i and curline[i - 1] in FILENAME_CHARS:
                i -= 1
            comp_start = curline[i:j]
            j = i
            while i and curline[i - 1] in FILENAME_CHARS + SEPS:
                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:
                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
            else:
                comp_what = ""
        else:
            return

        # For everything but imports, call fetch_completions
        if not imports:
            if complete and not comp_what and not comp_start:
                return
            comp_lists = self.fetch_completions(comp_what, mode)
            if not comp_lists[0]:
                return

        # It's nice to be able to see the length of a tuple/list (but not anything more complicated)
        if comp_lists[0] == SHOWCALLTIP:
            parenleft = self.text.index('insert-1c')
            CallTip(self.text).showtip(comp_lists[1], parenleft,
                                       parenleft.split('.')[0] + '.end')
            return

        if mode == COMPLETE_ATTRIBUTES and not imports:
            calltips = self.editwin.extensions.get('CallTips')
            if calltips:
                args = calltips.arg_names(evalfuncs)
                if args:
                    args = [a + '=' for a in args]
                    comp_lists = sorted(comp_lists[0] +
                                        args), sorted(comp_lists[1] + args)

        # Check if we want to show only completion containing typed word.
        if self.onlycontaining:
            # Small optimization
            comp_lower = comp_start.lower()
            # Find such completions.
            comp_lists = [
                name for name in comp_lists[0] if comp_lower in name.lower()
            ], comp_lists[1]
            # If none were found, look in big list.
            if not comp_lists[0]:
                comp_lists = [
                    name for name in comp_lists[1]
                    if comp_lower in name.lower()
                ], comp_lists[1]
            # If still none were found, just return the big list - which is the default anyway.
            if not comp_lists[0]:
                comp_lists = comp_lists[1], comp_lists[1]

        if showbig:
            comp_lists = comp_lists[1], []

        self.autocompletewindow = self._make_autocomplete_window()
        return not self.autocompletewindow.show_window(
            comp_lists,
            "insert-%dc" % len(comp_start),
            complete,
            mode,
            userWantsWin,
            onlycontaining=self.onlycontaining)
Beispiel #13
0
def writeSyntaxError(shell, value, win):
    text = win.text
    msg = getattr(value, 'msg', '') or value or "<no detail available>"
    lineno = getattr(value, 'lineno', '') or 1
    offset = getattr(value, 'offset', '') or 0
    linepos = "startpos + %d lines" % (lineno - 1)
    prevcode = text.get("startpos", linepos + " linestart")
    errorline = text.get(linepos, linepos + " lineend")
    if prevcode.startswith('>>> '):
        prevcode = prevcode[4:]
    if errorline.startswith('>>> '):
        errorline = errorline[4:]

    extramsg = ""

    # Check for missing closing bracket
    hasBrackets = (HyperParser(win, linepos +
                               " lineend").get_surrounding_brackets())
    if hasBrackets is not None:  # Has brackets at all
        matchedBrackets = (HyperParser(
            win, linepos).get_surrounding_brackets(mustclose=True))
        if matchedBrackets is None:  # No closing bracket
            extramsg = ERR_PAREN % int(hasBrackets[0].split('.')[0])
            text.tag_remove("ERROR", "1.0", "end")
            win.colorize_syntax_error(text, hasBrackets[0])

    try:
        tokens = list(
            tokenize.tokenize(io.BytesIO(bytes(errorline, 'utf-8')).readline))

        # Order of tests matters because of course it does

        # Check for missing colon at end of statement
        statementName = ''
        for i in range(len(tokens)):
            if tokens[i].type is tokenize.NAME:
                statementName = tokens[i].string
                break
        if statementName in [
                'if', 'elif', 'else', 'for', 'while', 'class', 'def'
        ]:
            hasColon = False
            for i in range(len(tokens))[::-1]:
                if tokens[i].exact_type is tokenize.COLON:
                    hasColon = True
                    break
            if not hasColon:
                extramsg = ERR_COLON % lineno

        # Check for using = (assignment) in if/elif/while
        if statementName in ['if', 'elif', 'while']:
            for i in range(len(tokens)):
                if tokens[i].exact_type is tokenize.EQUAL:
                    extramsg = ERR_ASSIGN % lineno
                    break

        # Check for using reserved word as variable name (assignment)
        equalsPos = -1
        for i in range(len(tokens)):
            if tokens[i].exact_type is tokenize.EQUAL:
                equalsPos = i
                break
        if equalsPos > 0 and tokens[equalsPos - 1].type is tokenize.NAME:
            for i in range(equalsPos)[::-1]:
                if tokens[i].type is tokenize.NAME:
                    if keyword.iskeyword(tokens[i].string):
                        extramsg = ERR_RESERVED % (tokens[i].string, lineno)
                        break
                    else:
                        if i > 0 and tokens[
                                i - 1].exact_type is not tokenize.COMMA:
                            break

        # Check for using reserved word as variable name (other cases)
        for t in tokens:
            if t.type is tokenize.NAME and keyword.iskeyword(t.string):
                newtestline = errorline[:t.start[1]] + 'x' + errorline[t.
                                                                       end[1]:]
                try:
                    ast.parse(prevcode + newtestline)
                    extramsg = ERR_RESERVED % (t.string, lineno)
                    break
                except SyntaxError as err:
                    newlineno = getattr(err, 'lineno', '') or 1
                    newoffset = getattr(err, 'offset', '') or 0
                    if newlineno > lineno or (newlineno is lineno
                                              and newoffset > offset):
                        extramsg = ERR_RESERVED % (t.string, lineno)
                        break

    except Exception as e:
        print('error', e)

    shell.write("\nSyntaxError: " + msg + "\n" + extramsg + "\n")
    # Logging code for testing
    #if not extramsg:
    #    extramsg = msg
    #with open("../error_results.txt", "a") as f:
    #    f.write(extramsg + "\n")
    shell.tkconsole.showprompt()