<commit_before>import {BaseLevel} from "level";
import {PubSub} from "pubsub";

interface HierarchyController extends _mithril.MithrilController {
    tree: d3.layout.Tree<ObjectHierarchy>,
    diagonal: d3.svg.Diagonal<d3.layout.tree.Link<d3.layout.tree.Node>, d3.layout.tree.Node>,
    svg: d3.Selection<any>,
    currentClass: _mithril.MithrilProperty<ObjectHierarchy>,
    newMethod: _mithril.MithrilProperty<string>,
}

export interface ObjectHierarchy extends d3.layout.tree.Node {
    name: string,
    children?: ObjectHierarchy[],
    methods?: string[],
    userMethods?: string[],

    x?: number,
    y?: number,
    depth?: number,
    id?: number,
}

export const Component: _mithril.MithrilComponent<HierarchyController> = <any> {
    controller: function(): HierarchyController {
        return {
            tree: null,
            diagonal: null,
            svg: null,
            currentClass: <any> m.prop(null),
            newMethod: m.prop(""),
        };
    },

    view: function(controller: HierarchyController, args: {
        showHierarchy: _mithril.MithrilProperty<boolean>,
        hierarchy: ObjectHierarchy,
        changeContext: (className: string, method: string) => void,
        event: PubSub,
        level: BaseLevel,
    }): _mithril.MithrilVirtualElement<HierarchyController> {
        function update() {
            let i = 0;
            // Compute tree layout
            let nodes = controller.tree.nodes(args.hierarchy).reverse();
            let links = controller.tree.links(nodes);

            // Set the distance between the nodes
            nodes.forEach(function(d) { d.y = d.depth * 150; });

            // Declare and create the nodes
            let node = controller.svg.selectAll("g.node")
                .data<ObjectHierarchy>(nodes, function(d) { return d.id || (d.id = ++i); });
            var nodeEnter = node.enter().append("g")
                .attr("class", "node")
                .attr("transform", function(d) {
                    // Flip x and y to get a horizontal tree
                    return "translate(" + d.y + "," + d.x + ")"; })
                .on("click", (d) => {
                    m.startComputation();
                    controller.currentClass(d);
                    m.endComputation();
                });
            nodeEnter.append("text")
                .attr("dy", ".35em")
                .attr("dx", "-10px")
                .attr("text-anchor", "start")
                .text(function(d) { return d.name; })
                .style("fill-opacity", 1);
            nodeEnter.append("rect")
                .attr("fill", "url(#blueprintGrid)")
                .attr("width", function() {
                    var text = this.parentNode.querySelector("text");
                    text.parentElement.insertBefore(this, text);
                    return text.getBBox().width + 10;
                })
                .attr("height", function() {
                    var text = this.parentNode.querySelector("text");
                    return text.getBBox().height + 10;
                })
                .attr("y", function() {
                    var text = this.parentNode.querySelector("text");
                    return text.getBBox().y - 5;
                })
                .attr("x", function() {
                    var text = this.parentNode.querySelector("text");
                    return text.getBBox().x - 5;
                });


            // Declare and create the links between nodes
            var link = controller.svg.selectAll("path.link")
                .data(links, function(d) { return d.target.id; });
            link.enter().insert("path", "g")
                .attr("class", "link")
                .attr("d", controller.diagonal);
        }

        if (args.hierarchy === null) {
            return m(<any> "div#hierarchy", {
                key: "hierarchy",
            });
        }

        let hierarchy = m(<any> "div#hierarchy", {
            style: args.showHierarchy() ? "display: block;" : "display: none",
            key: "hierarchy",
        }, [
            m(".image", {
                config: function(element: HTMLElement, isInitialized: boolean) {
                    if (!isInitialized) {
                        let margin = {top: 20, right: 20, bottom: 20, left: 120},
                        width = 500 - margin.right - margin.left,
                        height = 500 - margin.top - margin.bottom;

                        controller.tree = d3.layout.tree<ObjectHierarchy>()
                            .size([height, width]);

                        controller.diagonal = d3.svg.diagonal()
                            .projection(function(d) { return [d.y, d.x]; });

                        let svg = d3.select(element).append("svg")
                            .attr("height", "500px")
                            .attr("width", "500px")
                            .attr("viewBox", "0 0 500 500");
                        svg.append("defs")
                            .append("pattern")<commit_msg>fix(Class Hierarchy): use less space for hierarchy tree<commit_after>import {BaseLevel} from "level";
import {PubSub} from "pubsub";

interface HierarchyController extends _mithril.MithrilController {
    tree: d3.layout.Tree<ObjectHierarchy>,
    diagonal: d3.svg.Diagonal<d3.layout.tree.Link<d3.layout.tree.Node>, d3.layout.tree.Node>,
    svg: d3.Selection<any>,
    currentClass: _mithril.MithrilProperty<ObjectHierarchy>,
    newMethod: _mithril.MithrilProperty<string>,
}

export interface ObjectHierarchy extends d3.layout.tree.Node {
    name: string,
    children?: ObjectHierarchy[],
    methods?: string[],
    userMethods?: string[],

    x?: number,
    y?: number,
    depth?: number,
    id?: number,
}

export const Component: _mithril.MithrilComponent<HierarchyController> = <any> {
    controller: function(): HierarchyController {
        return {
            tree: null,
            diagonal: null,
            svg: null,
            currentClass: <any> m.prop(null),
            newMethod: m.prop(""),
        };
    },

    view: function(controller: HierarchyController, args: {
        showHierarchy: _mithril.MithrilProperty<boolean>,
        hierarchy: ObjectHierarchy,
        changeContext: (className: string, method: string) => void,
        event: PubSub,
        level: BaseLevel,
    }): _mithril.MithrilVirtualElement<HierarchyController> {
        function update() {
            let i = 0;
            // Compute tree layout
            let nodes = controller.tree.nodes(args.hierarchy).reverse();
            let links = controller.tree.links(nodes);

            // Set the distance between the nodes
            nodes.forEach(function(d) { d.y = d.depth * 100; });

            // Declare and create the nodes
            let node = controller.svg.selectAll("g.node")
                .data<ObjectHierarchy>(nodes, function(d) { return d.id || (d.id = ++i); });
            var nodeEnter = node.enter().append("g")
                .attr("class", "node")
                .attr("transform", function(d) {
                    // Flip x and y to get a horizontal tree
                    return "translate(" + d.y + "," + d.x + ")"; })
                .on("click", (d) => {
                    m.startComputation();
                    controller.currentClass(d);
                    m.endComputation();
                });
            nodeEnter.append("text")
                .attr("dy", ".35em")
                .attr("dx", "-10px")
                .attr("text-anchor", "start")
                .text(function(d) { return d.name; })
                .style("fill-opacity", 1);
            nodeEnter.append("rect")
                .attr("fill", "url(#blueprintGrid)")
                .attr("width", function() {
                    var text = this.parentNode.querySelector("text");
                    text.parentElement.insertBefore(this, text);
                    return text.getBBox().width + 10;
                })
                .attr("height", function() {
                    var text = this.parentNode.querySelector("text");
                    return text.getBBox().height + 10;
                })
                .attr("y", function() {
                    var text = this.parentNode.querySelector("text");
                    return text.getBBox().y - 5;
                })
                .attr("x", function() {
                    var text = this.parentNode.querySelector("text");
                    return text.getBBox().x - 5;
                });


            // Declare and create the links between nodes
            var link = controller.svg.selectAll("path.link")
                .data(links, function(d) { return d.target.id; });
            link.enter().insert("path", "g")
                .attr("class", "link")
                .attr("d", controller.diagonal);
        }

        if (args.hierarchy === null) {
            return m(<any> "div#hierarchy", {
                key: "hierarchy",
            });
        }

        let hierarchy = m(<any> "div#hierarchy", {
            style: args.showHierarchy() ? "display: block;" : "display: none",
            key: "hierarchy",
        }, [
            m(".image", {
                config: function(element: HTMLElement, isInitialized: boolean) {
                    if (!isInitialized) {
                        let margin = {top: 20, right: 20, bottom: 20, left: 80},
                        width = 500 - margin.right - margin.left,
                        height = 500 - margin.top - margin.bottom;

                        controller.tree = d3.layout.tree<ObjectHierarchy>()
                            .size([height, width]);

                        controller.diagonal = d3.svg.diagonal()
                            .projection(function(d) { return [d.y, d.x]; });

                        let svg = d3.select(element).append("svg")
                            .attr("height", "500px")
                            .attr("width", "500px")
                            .attr("viewBox", "0 0 500 500");
                        svg.append("defs")
                            .append("pattern")
{'fraction_non_alphanumeric': 0.13572481572481573, 'fraction_numerical': 0.010515970515970516, 'mean_word_length': 1.5313432835820895, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2486104', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2663, 'n_words': 576}