示例#1
0
文件: locator.py 项目: jhg/ninja-ide
    def _grep_file_symbols(self, file_path, file_name):
        #type - file_name - file_path
        global mapping_symbols
        exts = settings.SYNTAX.get('python')['extension']
        file_ext = file_manager.get_file_extension(file_path)
        if file_ext not in exts:
            mapping_symbols[file_path] = [
                ResultItem(symbol_type=FILTERS['non-python'], name=file_name,
                    path=file_path, lineno=-1)]
        else:
            mapping_symbols[file_path] = [
                ResultItem(symbol_type=FILTERS['files'], name=file_name,
                        path=file_path, lineno=-1)]
        data = self._get_file_symbols(file_path)
        #FIXME: stat not int
        mtime = int(os.stat(file_path).st_mtime)
        if data is not None and (mtime == int(data[1])):
            results = pickle.loads(str(data[2]))
            mapping_symbols[file_path] += results
            return
        #obtain a symbols handler for this file extension
        symbols_handler = handlers.get_symbols_handler(file_ext)
        if symbols_handler is None:
            return
        results = []
        with open(file_path) as f:
            content = f.read()
            symbols = symbols_handler.obtain_symbols(content,
                filename=file_path)
            self.__parse_symbols(symbols, results, file_path)

        if results:
            self._save_file_symbols(file_path, mtime, results)
            mapping_symbols[file_path] += results
示例#2
0
 def get_symbols_for_class(self, file_path, clazzName):
     results = []
     with open(file_path) as f:
         content = f.read()
         ext = file_manager.get_file_extension(file_path)
         #obtain a symbols handler for this file extension
         symbols_handler = handlers.get_symbols_handler(ext)
         symbols = symbols_handler.obtain_symbols(content,
                                                  filename=file_path)
         self.__parse_symbols(symbols, results, file_path)
     return results
 def get_symbols_for_class(self, file_path, clazzName):
     results = []
     with open(file_path) as f:
         content = f.read()
         ext = file_manager.get_file_extension(file_path)
         #obtain a symbols handler for this file extension
         symbols_handler = handlers.get_symbols_handler(ext)
         symbols = symbols_handler.obtain_symbols(content,
                                                  filename=file_path)
         self.__parse_symbols(symbols, results, file_path)
     return results
示例#4
0
 def _load_symbols(self, neditable):
     symbols_handler = handlers.get_symbols_handler('py')
     source = neditable.editor.toPlainText()
     source = source.encode(neditable.editor.encoding)
     symbols, symbols_simplified = symbols_handler.obtain_symbols(
         source, simple=True)
     self._symbols_index = sorted(symbols_simplified.keys())
     symbols_simplified = sorted(
         list(symbols_simplified.items()), key=lambda x: x[0])
     self.bar.add_symbols(symbols_simplified)
     line = neditable.editor.textCursor().blockNumber()
     self._set_current_symbol(line, True)
     tree_symbols = IDE.get_service('symbols_explorer')
     tree_symbols.update_symbols_tree(symbols, neditable.file_path)
示例#5
0
 def _load_symbols(self, neditable):
     symbols_handler = handlers.get_symbols_handler('py')
     source = neditable.editor.toPlainText()
     source = source.encode(neditable.editor.encoding)
     symbols, symbols_simplified = symbols_handler.obtain_symbols(
         source, simple=True)
     self._symbols_index = sorted(symbols_simplified.keys())
     symbols_simplified = sorted(
         list(symbols_simplified.items()), key=lambda x: x[0])
     self.bar.add_symbols(symbols_simplified)
     line = neditable.editor.textCursor().blockNumber()
     self._set_current_symbol(line, True)
     tree_symbols = IDE.get_service('symbols_explorer')
     tree_symbols.update_symbols_tree(symbols, neditable.file_path)
示例#6
0
 def _load_symbols(self, neditable):
     # Get symbols handler by language
     symbols_handler = handlers.get_symbols_handler(neditable.language())
     if symbols_handler is None:
         return
     source = neditable.editor.text
     source = source.encode(neditable.editor.encoding)
     symbols, symbols_simplified = symbols_handler.obtain_symbols(
         source, simple=True)
     self._symbols_index = sorted(symbols_simplified.keys())
     symbols_simplified = sorted(list(symbols_simplified.items()),
                                 key=lambda x: x[0])
     self.bar.add_symbols(symbols_simplified)
     line, _ = neditable.editor.cursor_position
     self._set_current_symbol(line, True)
     tree_symbols = IDE.get_service('symbols_explorer')
     if tree_symbols is not None:
         tree_symbols.update_symbols_tree(symbols, neditable.file_path)
示例#7
0
 def _load_symbols(self, neditable):
     # Get symbols handler by language
     symbols_handler = handlers.get_symbols_handler(neditable.language())
     if symbols_handler is None:
         return
     source = neditable.editor.text
     source = source.encode(neditable.editor.encoding)
     symbols, symbols_simplified = symbols_handler.obtain_symbols(
         source, simple=True)
     self._symbols_index = sorted(symbols_simplified.keys())
     symbols_simplified = sorted(
         list(symbols_simplified.items()), key=lambda x: x[0])
     self.bar.add_symbols(symbols_simplified)
     line, _ = neditable.editor.cursor_position
     self._set_current_symbol(line, True)
     tree_symbols = IDE.get_service('symbols_explorer')
     if tree_symbols is not None:
         tree_symbols.update_symbols_tree(symbols, neditable.file_path)
示例#8
0
    def _grep_file_symbols(self, file_path, file_name):
        # type - file_name - file_path
        global mapping_symbols
        exts = settings.SYNTAX.get('python')['extension']
        file_ext = file_manager.get_file_extension(file_path)
        if file_ext not in exts:
            mapping_symbols[file_path] = [
                ResultItem(symbol_type=FILTERS['non-python'],
                           name=file_name,
                           path=file_path,
                           lineno=-1)
            ]
        else:
            mapping_symbols[file_path] = [
                ResultItem(symbol_type=FILTERS['files'],
                           name=file_name,
                           path=file_path,
                           lineno=-1)
            ]
        data = self._get_file_symbols(file_path)
        # FIXME: stat not int
        mtime = int(os.stat(file_path).st_mtime)
        if data is not None and (mtime == int(data[1])):
            try:
                results = pickle.loads(data[2])
                mapping_symbols[file_path] += results
                return
            except BaseException:
                print("ResultItem couldn't be loaded, let's analyze it again'")
        # obtain a symbols handler for this file extension
        lang = settings.LANGUAGE_MAP.get(file_ext)
        symbols_handler = handlers.get_symbols_handler(lang)
        if symbols_handler is None:
            return
        results = []
        with open(file_path) as f:
            content = f.read()
            symbols = symbols_handler.obtain_symbols(content,
                                                     filename=file_path)
            self.__parse_symbols(symbols, results, file_path)

        if results:
            self._save_file_symbols(file_path, mtime, results)
            mapping_symbols[file_path] += results
示例#9
0
    def _grep_file_symbols(self, file_path, file_name):
        #type - file_name - file_path
        global mapping_symbols
        exts = settings.SYNTAX.get('python')['extension']
        file_ext = file_manager.get_file_extension(file_path)
        if file_ext not in exts:
            mapping_symbols[file_path] = [
                ResultItem(type=FILTERS['non-python'],
                           name=file_name,
                           path=file_path,
                           lineno=-1)
            ]
        else:
            mapping_symbols[file_path] = [
                ResultItem(type=FILTERS['files'],
                           name=file_name,
                           path=file_path,
                           lineno=-1)
            ]
        data = self._get_file_symbols(file_path)
        #FIXME: stat not int
        mtime = int(os.stat(file_path).st_mtime)
        if data is not None and (mtime == int(data[1])):
            results = pickle.loads(str(data[2]))
            mapping_symbols[file_path] += results
            return
        #obtain a symbols handler for this file extension
        symbols_handler = handlers.get_symbols_handler(file_ext)
        if symbols_handler is None:
            return
        results = []
        with open(file_path) as f:
            content = f.read()
            symbols = symbols_handler.obtain_symbols(content,
                                                     filename=file_path)
            self.__parse_symbols(symbols, results, file_path)

        if results:
            self._save_file_symbols(file_path, mtime, results)
            mapping_symbols[file_path] += results
示例#10
0
    def __complete_braces(self, event):
        """Complete () [] and {} using a mild inteligence to see if corresponds
        and also do some more magic such as complete in classes and functions.
        """
        brace = event.text()
        if brace not in settings.BRACES:
            # Thou shalt not waste cpu cycles if this brace compleion dissabled
            return
        line, index = self.getCursorPosition()
        text = self.text(line)
        complementary_brace = BRACE_DICT.get(brace)
        token_buffer = []
        _, tokens = self.__tokenize_text(text)
        is_unbalance = 0
        for tkn_type, tkn_rep, tkn_begin, tkn_end in tokens:
            if tkn_rep == brace:
                is_unbalance += 1
            elif tkn_rep == complementary_brace:
                is_unbalance -= 1
            if tkn_rep.strip() != "":
                token_buffer.append((tkn_rep, tkn_end[1]))
            is_unbalance = (is_unbalance >= 0) and is_unbalance or 0

        if (self.lang == "python") and (len(token_buffer) == 3) and \
                (token_buffer[2][0] == brace) and (token_buffer[0][0] in
                                                   ("def", "class")):
            self.insertAt("):", line, index)
            #are we in presence of a function?
            #TODO: IMPROVE THIS AND GENERALIZE IT WITH INTELLISENSEI
            if token_buffer[0][0] == "def":
                symbols_handler = handlers.get_symbols_handler('py')
                split_source = self.text().split("\n")
                indent = re.match('^\s+', str(split_source[line]))
                indentation = (indent.group() + " " * self._indent
                               if indent is not None else " " * self._indent)
                new_line = "%s%s" % (indentation, 'pass')
                split_source.insert(line + 1, new_line)
                source = '\n'.join(split_source)
                source = source.encode(self.encoding)
                _, symbols_simplified = symbols_handler.obtain_symbols(
                    source, simple=True, only_simple=True)
                symbols_index = sorted(symbols_simplified.keys())
                symbols_simplified = sorted(list(symbols_simplified.items()),
                                            key=lambda x: x[0])
                index_symbol = bisect.bisect(symbols_index, line)
                if (index_symbol >= len(symbols_index)
                        or symbols_index[index_symbol] > (line + 1)):
                    index -= 1
                belongs_to_class = symbols_simplified[index_symbol][1][2]
                if belongs_to_class:
                    self.insertAt("self", line, index)
                    index += 4
                    if self.selected_text != "":
                        self.insertAt(", ", line, index)
                        index += 2
            self.insertAt(self.selected_text, line, index)
            self.setCursorPosition(line, index)
        elif (token_buffer and (not is_unbalance) and self.selected_text):
            self.insertAt(self.selected_text, line, index)
        elif is_unbalance:
            next_char = text[index:index + 1].strip()
            if self.selected_text or next_char == "":
                self.insertAt(complementary_brace, line, index)
                self.insertAt(self.selected_text, line, index)
示例#11
0
    def __complete_braces(self, event):
        """Complete () [] and {} using a mild inteligence to see if corresponds
        and also do some more magic such as complete in classes and functions.
        """
        brace = event.text()
        if brace not in settings.BRACES:
            # Thou shalt not waste cpu cycles if this brace compleion dissabled
            return
        line, index = self.getCursorPosition()
        text = self.text(line)
        complementary_brace = BRACE_DICT.get(brace)
        token_buffer = []
        _, tokens = self.__tokenize_text(text)
        is_unbalance = 0
        for tkn_type, tkn_rep, tkn_begin, tkn_end in tokens:
            if tkn_rep == brace:
                is_unbalance += 1
            elif tkn_rep == complementary_brace:
                is_unbalance -= 1
            if tkn_rep.strip() != "":
                token_buffer.append((tkn_rep, tkn_end[1]))
            is_unbalance = (is_unbalance >= 0) and is_unbalance or 0

        if (
            (self.lang == "python")
            and (len(token_buffer) == 3)
            and (token_buffer[2][0] == brace)
            and (token_buffer[0][0] in ("def", "class"))
        ):
            self.insertAt("):", line, index)
            # are we in presence of a function?
            # TODO: IMPROVE THIS AND GENERALIZE IT WITH INTELLISENSEI
            if token_buffer[0][0] == "def":
                symbols_handler = handlers.get_symbols_handler("py")
                split_source = self.text().split("\n")
                indent = re.match("^\s+", str(split_source[line]))
                indentation = indent.group() + " " * self._indent if indent is not None else " " * self._indent
                new_line = "%s%s" % (indentation, "pass")
                split_source.insert(line + 1, new_line)
                source = "\n".join(split_source)
                source = source.encode(self.encoding)
                _, symbols_simplified = symbols_handler.obtain_symbols(source, simple=True, only_simple=True)
                symbols_index = sorted(symbols_simplified.keys())
                symbols_simplified = sorted(list(symbols_simplified.items()), key=lambda x: x[0])
                index_symbol = bisect.bisect(symbols_index, line)
                if index_symbol >= len(symbols_index) or symbols_index[index_symbol] > (line + 1):
                    index -= 1
                belongs_to_class = symbols_simplified[index_symbol][1][2]
                if belongs_to_class:
                    self.insertAt("self", line, index)
                    index += 4
                    if self.selected_text != "":
                        self.insertAt(", ", line, index)
                        index += 2
            self.insertAt(self.selected_text, line, index)
            self.setCursorPosition(line, index)
        elif token_buffer and (not is_unbalance) and self.selected_text:
            self.insertAt(self.selected_text, line, index)
        elif is_unbalance:
            next_char = text[index : index + 1].strip()
            if self.selected_text or next_char == "":
                self.insertAt(complementary_brace, line, index)
                self.insertAt(self.selected_text, line, index)