예제 #1
0
 def newFile(self):
     if self.lang_box.currentText() == "rust":
         self.editor.new_file(os.path.join(util.get_resources_dir(), "template.rs"))
     elif self.lang_box.currentText() == "c++":
         self.editor.new_file(os.path.join(util.get_resources_dir(), "template.cpp"))
     else:
         self.editor.new_file(os.path.join(util.get_resources_dir(), "template.py"))
예제 #2
0
class RacerCompleter(CompleterBase):
    # temp file for racer input
    live_template_file = os.path.join(
        util.get_resources_dir(), "live_templates_rust.xml"
    )

    # this is heavy?
    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
class PyCompleter(CompleterBase):
    live_template_file = os.path.join(util.get_resources_dir(),
                                      "live_templates_cpp.xml")

    def setCompletionPrefix(self, text: str):
        import jedi

        src_line_num = self.parent.textCursor().blockNumber() + 1
        src_char_num = self.parent.textCursor().columnNumber()
        candidates = jedi.Script(self.parent.toPlainText()).complete(
            src_line_num, src_char_num)
        candidates = [cand.name for cand in candidates]

        self.candidates_dict = {}
        for i, word in enumerate(candidates):
            self.candidates_dict[word] = -1

        self.setModel(QtCore.QStringListModel(self.candidates_dict.keys()))
        super().setCompletionPrefix(text)
예제 #4
0
class CppCompleter(CompleterBase):
    # temp file for racer input
    live_template_file = os.path.join(util.get_resources_dir(),
                                      "live_templates_cpp.xml")

    # TODO: Support clang completer
    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)