<commit_before>/**
 * This maintains the User interface Tabs for app,
 * e.g. selected tab, any handling of open tab requests etc.
 */

import * as ui from "../ui";
import * as React from "react";
import * as tab from "./tab";
import * as tabRegistry from "./tabRegistry";
// import {DashboardTab} from "./dashboardTab";
import {Code} from "./codeTab";
import {DependencyView} from "./dependencyView";
import * as commands from "../commands/commands";
import * as utils from "../../common/utils";
import csx = require('csx');
import {createId} from "../../common/utils";

import {tabHeaderContainer,tabHeader,tabHeaderActive,tabHeaderUnsaved} from "../styles/styles";

import {server} from "../../socket/socketClient";
import {Types} from "../../socket/socketContract";
import {rangeLimited} from "../../common/utils";
import {statusBar} from "../statusBar";

import * as state from "../state/state";
import * as types from "../../common/types";
import {connect} from "react-redux";
import * as styles from "../styles/styles";
import {Tips} from "./tips";
import {Icon} from "../icon";
import {cast} from "../../socket/socketClient";

export interface Props {
    // redux connected below
    tabs?: state.TabInstance[];
    selectedTabIndex?: number;
}

export interface State {
}

/**
 * The singleton
 */
export var appTabsContainer: AppTabsContainer;

@connect((state: state.StoreState): Props => {
    return {
        tabs: state.tabs,
        selectedTabIndex: state.selectedTabIndex
    };
})
@ui.Radium
export class AppTabsContainer extends ui.BaseComponent<Props, State>{

    closedTabs: state.TabInstance[] = [];

    constructor(props: Props) {
        super(props);

        this.state = {
            selected: 0,
        };
    }

    componentWillReceiveProps(nextProps: Props) {
        setTimeout(() => {
            let comp = this.getSelectedComponent();
            if (comp) {
                comp.focus();
            }
        });
    }

    refs: { [string: string]: tab.Component; }

    componentDidMount() {
        appTabsContainer = this;

        commands.nextTab.on(() => {
            let selected = rangeLimited({ min: 0, max: this.props.tabs.length - 1, num: this.props.selectedTabIndex + 1, loopAround: true });
            this.selectTab(selected);
        });
        commands.prevTab.on(() => {
            let selected = rangeLimited({ min: 0, max: this.props.tabs.length - 1, num: this.props.selectedTabIndex - 1, loopAround: true });
            this.selectTab(selected);
        });

        commands.doOpenFile.on((e) =>{
            let codeTab: state.TabInstance = {
                id: createId(),
                url: `file://${e.filePath}`,
                saved: true
            }

            this.afterComponentDidUpdate(this.sendTabInfoToServer);
            this.afterComponentDidUpdate(this.focusAndUpdateStuffWeKnowAboutCurrentTab);
            if (e.position) {
                this.afterComponentDidUpdate(() => this.gotoPositionOnSelectedTab(e.position));
            }
            state.addTabAndSelect(codeTab);
        });

        commands.doOpenDependencyView.on((e) =>{
            let codeTab: state.TabInstance = {
                id: createId(),
                url: `dependency://Dependency View`,
                saved: true
            }

            this.afterComponentDidUpdate(this.sendTabInfoToServer);
            this.afterComponentDidUpdate(this.focusAndUpdateStuffWeKnowAboutCurrentTab);
            state.addTabAndSelect(codeTab);
        });

        commands.findAndReplaceMulti.on((e) =>{
            // if open and active => focus
            // if open and not active => active
            // if not open and active
            if (this.props.tabs.length
                && utils.getFilePathAndProtocolFromUrl(this.props.tabs[this.props.selectedTabIndex].url).protocol === 'farm'){
                this.getSelectedComponent().focus();
                return;
            }

            let openTabIndex = this.props.tabs.map(t=> utils.getFilePathAndProtocolFromUrl(t.url).protocol === 'farm').indexOf(true);
            if (openTabIndex !== -1) {
                this.selectTab(openTabIndex);
                return;
            }

            let farmTab: state.TabInstance = {
                id: createId(),
                url: `farm://Find And Replace`,
                saved: true
            }
            this.afterComponentDidUpdate(this.sendTabInfoToServer);
            this.afterComponentDidUpdate(this.focusAndUpdateStuffWeKnowAboutCurrentTab);
            state.addTabAndSelect(farmTab);
        });

        let getCurrentFilePathOrWarn = () => {
            let tab = state.getSelectedTab();
            if (!tab) {
                ui.notifyWarningNormalDisappear('Need a valid file path for this action');
                return;
            }
            let {protocol,filePath} = utils.getFilePathAndProtocolFromUrl(tab.url);
            if (protocol !== 'file'){
                ui.notifyWarningNormalDisappear('Need a valid file path for this action');
                return;
            }
            return filePath;
        }

        let openAst = (mode:Types.ASTMode)=>{
            let filePath = getCurrentFilePathOrWarn();
            if (!filePath) return;

            let codeTab: state.TabInstance = {
                id: createId(),
                url: `${mode == Types.ASTMode.visitor ? 'ast' : 'astfull'}://${filePath}`,<commit_msg>better help for active file command<commit_after>/**
 * This maintains the User interface Tabs for app,
 * e.g. selected tab, any handling of open tab requests etc.
 */

import * as ui from "../ui";
import * as React from "react";
import * as tab from "./tab";
import * as tabRegistry from "./tabRegistry";
// import {DashboardTab} from "./dashboardTab";
import {Code} from "./codeTab";
import {DependencyView} from "./dependencyView";
import * as commands from "../commands/commands";
import * as utils from "../../common/utils";
import csx = require('csx');
import {createId} from "../../common/utils";

import {tabHeaderContainer,tabHeader,tabHeaderActive,tabHeaderUnsaved} from "../styles/styles";

import {server} from "../../socket/socketClient";
import {Types} from "../../socket/socketContract";
import {rangeLimited} from "../../common/utils";
import {statusBar} from "../statusBar";

import * as state from "../state/state";
import * as types from "../../common/types";
import {connect} from "react-redux";
import * as styles from "../styles/styles";
import {Tips} from "./tips";
import {Icon} from "../icon";
import {cast} from "../../socket/socketClient";

export interface Props {
    // redux connected below
    tabs?: state.TabInstance[];
    selectedTabIndex?: number;
}

export interface State {
}

/**
 * The singleton
 */
export var appTabsContainer: AppTabsContainer;

@connect((state: state.StoreState): Props => {
    return {
        tabs: state.tabs,
        selectedTabIndex: state.selectedTabIndex
    };
})
@ui.Radium
export class AppTabsContainer extends ui.BaseComponent<Props, State>{

    closedTabs: state.TabInstance[] = [];

    constructor(props: Props) {
        super(props);

        this.state = {
            selected: 0,
        };
    }

    componentWillReceiveProps(nextProps: Props) {
        setTimeout(() => {
            let comp = this.getSelectedComponent();
            if (comp) {
                comp.focus();
            }
        });
    }

    refs: { [string: string]: tab.Component; }

    componentDidMount() {
        appTabsContainer = this;

        commands.nextTab.on(() => {
            let selected = rangeLimited({ min: 0, max: this.props.tabs.length - 1, num: this.props.selectedTabIndex + 1, loopAround: true });
            this.selectTab(selected);
        });
        commands.prevTab.on(() => {
            let selected = rangeLimited({ min: 0, max: this.props.tabs.length - 1, num: this.props.selectedTabIndex - 1, loopAround: true });
            this.selectTab(selected);
        });

        commands.doOpenFile.on((e) =>{
            let codeTab: state.TabInstance = {
                id: createId(),
                url: `file://${e.filePath}`,
                saved: true
            }

            this.afterComponentDidUpdate(this.sendTabInfoToServer);
            this.afterComponentDidUpdate(this.focusAndUpdateStuffWeKnowAboutCurrentTab);
            if (e.position) {
                this.afterComponentDidUpdate(() => this.gotoPositionOnSelectedTab(e.position));
            }
            state.addTabAndSelect(codeTab);
        });

        commands.doOpenDependencyView.on((e) =>{
            let codeTab: state.TabInstance = {
                id: createId(),
                url: `dependency://Dependency View`,
                saved: true
            }

            this.afterComponentDidUpdate(this.sendTabInfoToServer);
            this.afterComponentDidUpdate(this.focusAndUpdateStuffWeKnowAboutCurrentTab);
            state.addTabAndSelect(codeTab);
        });

        commands.findAndReplaceMulti.on((e) =>{
            // if open and active => focus
            // if open and not active => active
            // if not open and active
            if (this.props.tabs.length
                && utils.getFilePathAndProtocolFromUrl(this.props.tabs[this.props.selectedTabIndex].url).protocol === 'farm'){
                this.getSelectedComponent().focus();
                return;
            }

            let openTabIndex = this.props.tabs.map(t=> utils.getFilePathAndProtocolFromUrl(t.url).protocol === 'farm').indexOf(true);
            if (openTabIndex !== -1) {
                this.selectTab(openTabIndex);
                return;
            }

            let farmTab: state.TabInstance = {
                id: createId(),
                url: `farm://Find And Replace`,
                saved: true
            }
            this.afterComponentDidUpdate(this.sendTabInfoToServer);
            this.afterComponentDidUpdate(this.focusAndUpdateStuffWeKnowAboutCurrentTab);
            state.addTabAndSelect(farmTab);
        });

        let getCurrentFilePathOrWarn = () => {
            let tab = state.getSelectedTab();
            let notify = () => ui.notifyWarningNormalDisappear('Need a valid file path for this action. Make sure you have a *file* tab as active');
            if (!tab) {
                notify();
                return;
            }
            let {protocol,filePath} = utils.getFilePathAndProtocolFromUrl(tab.url);
            if (protocol !== 'file'){
                notify();
                return;
            }
            return filePath;
        }

        let openAst = (mode:Types.ASTMode)=>{
            let filePath = getCurrentFilePathOrWarn();
            if (!filePath) return;

            let codeTab: state.TabInstance = {
                id: createId(),
                url: `${mode == Types.ASTMode.visitor ? 'ast' : 'astfull'}://${filePath}`,
{'fraction_non_alphanumeric': 0.1321770880876312, 'fraction_numerical': 0.001460520310360566, 'mean_word_length': 2.089678510998308, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, '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': '4492367', 'n_tokens_mistral': 2978, 'n_tokens_neox': 2906, 'n_words': 683}