コード例 #1
0
    def _index_file_cb(self, subprocess, result, task):
        try:
            _, stdout, stderr = subprocess.communicate_utf8_finish(result)

            ide_file = Ide.File(file=task.file, context=self.get_context())
            try:
                root_node = JsSymbolTree._node_from_dict(json.loads(stdout), ide_file)
            except (json.JSONDecodeError, UnicodeDecodeError) as e:
                raise GLib.Error('Failed to decode gjs json: {}'.format(e))
            except (IndexError, KeyError) as e:
                raise GLib.Error('Failed to extract information from ast: {}'.format(e))

            entries = []
            # TODO: Avoid recreating the same data
            for node in self._flatten_node_list(root_node):
                entry = Ide.CodeIndexEntry(
                    key=node.props.file.get_path() + '|' + node.props.name, # Some unique value..
                    name=self._get_node_name(node),
                    kind=node.props.kind,
                    flags=node.props.flags,
                    begin_line=node.props.line + 1,  # Not sure why offset here doesn't match tree
                    begin_line_offset=node.props.col + 1,
                )
                entries.append(entry)

            task.entries = JsCodeIndexEntries(task.file, entries)
            task.return_boolean(True)

        except Exception as ex:
            print(repr(ex))
            task.return_error(GLib.Error(message=repr(ex)))
コード例 #2
0
    def do_index_file(self, file_, build_flags, cancellable):
        if 'node_modules' in file_.get_path().split(os.sep):
            return None  # Avoid indexing these

        launcher = GjsSymbolProvider._get_launcher(self.get_context(), file_)
        proc = launcher.spawn()
        success, stdout, stderr = proc.communicate_utf8(None, None)

        if not success:
            return None

        ide_file = Ide.File(file=file_, context=self.get_context())
        try:
            root_node = JsSymbolTree._node_from_dict(json.loads(stdout),
                                                     ide_file)
        except (json.JSONDecodeError, UnicodeDecodeError) as e:
            raise GLib.Error('Failed to decode gjs json: {}'.format(e))
        except (IndexError, KeyError) as e:
            raise GLib.Error(
                'Failed to extract information from ast: {}'.format(e))

        entries = []
        # TODO: Avoid recreating the same data
        for node in self._flatten_node_list(root_node):
            entry = Ide.CodeIndexEntry(
                key=node.props.file.get_path() + '|' +
                node.props.name,  # Some unique value..
                name=self._get_node_name(node),
                kind=node.props.kind,
                flags=node.props.flags,
                begin_line=node.props.line +
                1,  # Not sure why offset here doesn't match tree
                begin_line_offset=node.props.col + 1,
            )
            entries.append(entry)
        return JsCodeIndexEntries(entries)