예제 #1
0
    def save_pre_process(self):
        # Get formatted Text
        temp_file_name = util.get_temp_file(self.lang)
        with codecs.open(temp_file_name, "w", "utf-8") as f:
            f.write(self.toPlainText())
        if not util.exec_format(self.lang):
            return
        temp_file = codecs.open(temp_file_name, "r", "utf-8")
        all_text = "".join([line for line in temp_file.readlines()])
        temp_file.close()

        # Save cursor and scroll bar status
        cursor = self.textCursor()
        line_num = cursor.blockNumber()
        char_num = cursor.columnNumber()
        scroll_value = self.verticalScrollBar().value()

        # Clear all Text and insert format result
        cursor.movePosition(QtGui.QTextCursor.Start)
        cursor.movePosition(QtGui.QTextCursor.End, QtGui.QTextCursor.KeepAnchor, 1)
        cursor.removeSelectedText()
        self.insertPlainText(all_text)

        # recover cursor and scroll bar status
        cursor = QtGui.QTextCursor(self.document().findBlockByLineNumber(line_num))
        cursor.movePosition(
            QtGui.QTextCursor.NextCharacter, QtGui.QTextCursor.MoveAnchor, char_num
        )
        self.setTextCursor(cursor)
        self.verticalScrollBar().setValue(scroll_value)
        self.edited = False
        self.repaint()
예제 #2
0
    def setCompletionPrefix(self, text: str):
        temp_file_name = util.get_temp_file("rust")
        with codecs.open(temp_file_name, "w", "utf-8") as f:
            f.write(self.parent.toPlainText())
        src_line_num = str(self.parent.textCursor().blockNumber() + 1)
        src_char_num = str(self.parent.textCursor().columnNumber())

        try:
            out = subprocess.check_output(
                "racer complete" + " " + src_line_num + " " + src_char_num +
                " " + temp_file_name,
                shell=True,
            ).decode()
        except subprocess.CalledProcessError as e:
            print(e.output)
            return

        self.candidates_dict = {}
        for line in out.split("\n"):
            if line.startswith("MATCH"):
                cand = line[6:].split(",")[0]
                if cand not in self.ng_words:
                    self.candidates_dict[cand] = -1
        search_word = out.split("\n")[0].split(",")[2]

        for live_template in self.live_templates:
            if live_template.name.startswith(search_word):
                self.candidates_dict[
                    live_template.template] = live_template.rpos
        if len(self.candidates_dict
               ) >= 6 or search_word in self.candidates_dict.keys():
            self.candidates_dict = {}
        self.setModel(QtCore.QStringListModel(self.candidates_dict.keys()))
        super().setCompletionPrefix(search_word)
예제 #3
0
    def setCompletionPrefix(self, text: str):
        temp_file_name = util.get_temp_file("cpp")
        with codecs.open(temp_file_name, "w", "utf-8") as f:
            f.write(self.parent.toPlainText())
        src_line_num = str(self.parent.textCursor().blockNumber() + 1)
        src_char_num = str(self.parent.textCursor().columnNumber())

        try:
            out = subprocess.check_output(
                # read all header is too slow
                # "clang -fsyntax-only -Xclang -code-completion-at=%s:%s:%s %s"
                "clang -cc1 -fsyntax-only -code-completion-at=%s:%s:%s %s"
                % (temp_file_name, src_line_num, src_char_num, temp_file_name),
                shell=True,
            ).decode()
        except subprocess.CalledProcessError as e:
            out = e.output.decode()

        self.candidates_dict = OrderedDict()
        for line in out.split("\n"):
            if line.startswith("COMPLETION:"):
                cand = line.split(" ")[1]
                if text not in cand:
                    continue
                if cand not in self.ng_words:
                    self.candidates_dict[cand] = -1

        for live_template in self.live_templates:
            if live_template.name.startswith(text):
                self.candidates_dict[live_template.template] = live_template.rpos
        if len(self.candidates_dict) >= 10 or text in self.candidates_dict.keys():
            self.candidates_dict = {}
        self.setModel(QtCore.QStringListModel(self.candidates_dict.keys()))
        super().setCompletionPrefix(text)
예제 #4
0
    def save_pre_process(self):
        # Get formatted Text
        temp_file_name = util.get_temp_file(self.lang)
        with codecs.open(temp_file_name, "w", "utf-8") as f:
            f.write(self.toPlainText())
        if not util.exec_format(self.lang):
            return
        temp_file = codecs.open(temp_file_name, "r", "utf-8")
        formatted_text = "".join([line for line in temp_file.readlines()])
        temp_file.close()

        # Save cursor and scroll bar status
        pos = self.save_position()

        # Clear all Text and insert format result
        self.clear_and_write_text(formatted_text)

        # recover cursor and scroll bar status
        self.load_position(*pos)
        self.edited = False
        self.repaint()