<commit_before>// @flow

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Redux actions for the sources state
 * @module actions/sources
 */

const defer = require("../utils/defer");
const { PROMISE } = require("../utils/redux/middleware/promise");
const assert = require("../utils/assert");
const { updateFrameLocations } = require("../utils/pause");
const {
  getOriginalURLs, getOriginalSourceText,
  generatedToOriginalId, isOriginalId,
  isGeneratedId, applySourceMap, shouldSourceMap
} = require("../utils/source-map");

const { prettyPrint } = require("../utils/pretty-print");

const constants = require("../constants");
const { isEnabled } = require("devtools-config");
const { removeDocument } = require("../utils/source-documents");

const {
  getSource, getSourceByURL, getSourceText,
  getPendingSelectedLocation, getFrames
} = require("../selectors");

import type { Source } from "../types";
import type { ThunkArgs } from "./types";

/**
 * Handler for the debugger client's unsolicited newSource notification.
 * @memberof actions/sources
 * @static
 */
function newSource(source: Source) {
  return ({ dispatch, getState }: ThunkArgs) => {
    if (shouldSourceMap()) {
      dispatch(loadSourceMap(source));
    }

    dispatch({
      type: constants.ADD_SOURCE,
      source
    });

    // If a request has been made to show this source, go ahead and
    // select it.
    const pendingLocation = getPendingSelectedLocation(getState());
    if (pendingLocation && pendingLocation.url === source.url) {
      dispatch(selectSource(source.id, { line: pendingLocation.line }));
    }
  };
}

function newSources(sources: Source[]) {
  return ({ dispatch, getState }: ThunkArgs) => {
    sources.filter(source => !getSource(getState(), source.id))
      .forEach(source => dispatch(newSource(source)));
  };
}

/**
 * @memberof actions/sources
 * @static
 */
function loadSourceMap(generatedSource) {
  return async function({ dispatch, getState }: ThunkArgs) {
    const urls = await getOriginalURLs(generatedSource);
    if (!urls) {
      // If this source doesn't have a sourcemap, do nothing.
      return;
    }

    const originalSources = urls.map(originalUrl => {
      return {
        url: originalUrl,
        id: generatedToOriginalId(generatedSource.id, originalUrl),
        isPrettyPrinted: false
      };
    });

    originalSources.forEach(s => dispatch(newSource(s)));
  };
}

type SelectSourceOptions = {
  tabIndex?: number,
  line?: number
};

/**
 * Deterministically select a source that has a given URL. This will
 * work regardless of the connection status or if the source exists
 * yet. This exists mostly for external things to interact with the
 * debugger.
 *
 * @memberof actions/sources
 * @static
 */
function selectSourceURL(url: string, options: SelectSourceOptions = {}) {
  return ({ dispatch, getState }: ThunkArgs) => {
    const source = getSourceByURL(getState(), url);
    if (source) {
      dispatch(selectSource(source.get("id"), options));
    } else {
      dispatch({
        type: constants.SELECT_SOURCE_URL,
        url: url,
        tabIndex: options.tabIndex,
        line: options.line
      });
    }
  };
}

/**
 * @memberof actions/sources
 * @static
 */
function selectSource(id: string, options: SelectSourceOptions = {}) {
  return ({ dispatch, getState, client }: ThunkArgs) => {
    if (!client) {
      // No connection, do nothing. This happens when the debugger is
      // shut down too fast and it tries to display a default source.
      return;
    }

    const source = getSource(getState(), id).toJS();

    // Make sure to start a request to load the source text.
    dispatch(loadSourceText(source));

    dispatch({
      type: constants.SELECT_SOURCE,
      source: source,
      tabIndex: options.tabIndex,
      line: options.line
    });
  };
}

/**
 * @memberof actions/sources
 * @static
 */
function closeTab(id: string) {
  removeDocument(id);
  return {
    type: constants.CLOSE_TAB,
    id: id,
  };
}<commit_msg>close source search when a source is selected<commit_after>// @flow

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Redux actions for the sources state
 * @module actions/sources
 */

const defer = require("../utils/defer");
const { PROMISE } = require("../utils/redux/middleware/promise");
const assert = require("../utils/assert");
const { updateFrameLocations } = require("../utils/pause");
const {
  getOriginalURLs, getOriginalSourceText,
  generatedToOriginalId, isOriginalId,
  isGeneratedId, applySourceMap, shouldSourceMap
} = require("../utils/source-map");

const { prettyPrint } = require("../utils/pretty-print");

const constants = require("../constants");
const { isEnabled } = require("devtools-config");
const { removeDocument } = require("../utils/source-documents");

const {
  getSource, getSourceByURL, getSourceText,
  getPendingSelectedLocation, getFrames
} = require("../selectors");

import type { Source } from "../types";
import type { ThunkArgs } from "./types";

/**
 * Handler for the debugger client's unsolicited newSource notification.
 * @memberof actions/sources
 * @static
 */
function newSource(source: Source) {
  return ({ dispatch, getState }: ThunkArgs) => {
    if (shouldSourceMap()) {
      dispatch(loadSourceMap(source));
    }

    dispatch({
      type: constants.ADD_SOURCE,
      source
    });

    // If a request has been made to show this source, go ahead and
    // select it.
    const pendingLocation = getPendingSelectedLocation(getState());
    if (pendingLocation && pendingLocation.url === source.url) {
      dispatch(selectSource(source.id, { line: pendingLocation.line }));
    }
  };
}

function newSources(sources: Source[]) {
  return ({ dispatch, getState }: ThunkArgs) => {
    sources.filter(source => !getSource(getState(), source.id))
      .forEach(source => dispatch(newSource(source)));
  };
}

/**
 * @memberof actions/sources
 * @static
 */
function loadSourceMap(generatedSource) {
  return async function({ dispatch, getState }: ThunkArgs) {
    const urls = await getOriginalURLs(generatedSource);
    if (!urls) {
      // If this source doesn't have a sourcemap, do nothing.
      return;
    }

    const originalSources = urls.map(originalUrl => {
      return {
        url: originalUrl,
        id: generatedToOriginalId(generatedSource.id, originalUrl),
        isPrettyPrinted: false
      };
    });

    originalSources.forEach(s => dispatch(newSource(s)));
  };
}

type SelectSourceOptions = {
  tabIndex?: number,
  line?: number
};

/**
 * Deterministically select a source that has a given URL. This will
 * work regardless of the connection status or if the source exists
 * yet. This exists mostly for external things to interact with the
 * debugger.
 *
 * @memberof actions/sources
 * @static
 */
function selectSourceURL(url: string, options: SelectSourceOptions = {}) {
  return ({ dispatch, getState }: ThunkArgs) => {
    const source = getSourceByURL(getState(), url);
    if (source) {
      dispatch(selectSource(source.get("id"), options));
    } else {
      dispatch({
        type: constants.SELECT_SOURCE_URL,
        url: url,
        tabIndex: options.tabIndex,
        line: options.line
      });
    }
  };
}

/**
 * @memberof actions/sources
 * @static
 */
function selectSource(id: string, options: SelectSourceOptions = {}) {
  return ({ dispatch, getState, client }: ThunkArgs) => {
    if (!client) {
      // No connection, do nothing. This happens when the debugger is
      // shut down too fast and it tries to display a default source.
      return;
    }

    const source = getSource(getState(), id).toJS();

    // Make sure to start a request to load the source text.
    dispatch(loadSourceText(source));

    dispatch({ type: constants.TOGGLE_FILE_SEARCH, searchOn: false });

    dispatch({
      type: constants.SELECT_SOURCE,
      source: source,
      tabIndex: options.tabIndex,
      line: options.line
    });
  };
}

/**
 * @memberof actions/sources
 * @static
 */
function closeTab(id: string) {
  removeDocument(id);
  return {
    type: constants.CLOSE_TAB,
    id: id,
  };
}
{'fraction_non_alphanumeric': 0.1324120009449563, 'fraction_numerical': 0.0009449562957713206, 'mean_word_length': 3.2936105476673427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6173663', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2428, 'n_words': 694}