function indentObjcDeclaration()

in src/utils/indentation.js [13:45]


function indentObjcDeclaration(codeElement) {
  // find all param name spans (which are tokenized as "token-identifier"s)
  const params = codeElement.getElementsByClassName('token-identifier');
  if (params.length < 2) {
    return;
  }

  // use the position of the first keyword colon separator as the offset
  // to determine indentation for following lines
  const offset = codeElement.textContent.indexOf(':') + 1;

  // loop through every param name (after the first one) and calculate/apply the
  // number of spaces to indent by subtracting the length of the name from the
  // original offset
  // eslint-disable-next-line no-plusplus
  for (let i = 1; i < params.length; i++) {
    const originalHtml = params[i].innerHTML.trim();
    const originalText = params[i].textContent.trim();

    const paramLen = originalText.length;
    const numSpaces = Math.max(0, offset - paramLen);

    params[i].innerHTML = `\n${' '.repeat(numSpaces)}${originalHtml}`;
  }

  // Adds a newline to the end of the code listing if there isn't one. Without it,
  // the <wbr> elements will impact items on the last line, when we don't want
  // it to (after applying this indentation logic)
  if (codeElement.innerHTML.charAt(codeElement.innerHTML.length - 1) !== '\n') {
    // eslint-disable-next-line no-param-reassign
    codeElement.innerHTML = `${codeElement.innerHTML}\n`;
  }
}