tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

function.js (1174B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 import { isFulfilled } from "./async-value";
      6 import { findClosestFunction } from "./ast";
      7 import { correctIndentation } from "./indentation";
      8 
      9 export function findFunctionText(line, source, sourceTextContent, symbols) {
     10  const func = findClosestFunction(symbols, {
     11    sourceId: source.id,
     12    line,
     13    column: Infinity,
     14  });
     15 
     16  if (
     17    source.isWasm ||
     18    !func ||
     19    !sourceTextContent ||
     20    !isFulfilled(sourceTextContent) ||
     21    sourceTextContent.value.type !== "text"
     22  ) {
     23    return null;
     24  }
     25 
     26  const {
     27    location: { start, end },
     28  } = func;
     29  const lines = sourceTextContent.value.value.split("\n");
     30  const firstLine = lines[start.line - 1].slice(start.column);
     31  const lastLine = lines[end.line - 1].slice(0, end.column);
     32  const middle = lines.slice(start.line, end.line - 1);
     33  const functionText = [firstLine, ...middle, lastLine].join("\n");
     34  const indentedFunctionText = correctIndentation(functionText);
     35 
     36  return indentedFunctionText;
     37 }