lodash#forEachRight TypeScript Examples

The following examples show how to use lodash#forEachRight. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: snapshots-utils.ts    From ui5-language-assistant with Apache License 2.0 7 votes vote down vote up
export function computeXMLWithMarkedRanges(
  testDir: string,
  ranges: Range[]
): string {
  const xmlTextSnippet = readInputXMLSnippet(testDir);
  const xmlTextDoc = TextDocument.create(
    `file://${getInputXMLSnippetPath(testDir)}`,
    "xml",
    0,
    xmlTextSnippet
  );

  // this assumes no overlapping ranges
  const issueRangesByOffsetAscending = sortBy(
    ranges,
    // sort by line first and column second
    (_) => xmlTextDoc.offsetAt(_.start)
  );

  // Changes are made from the "end" of the text to the start to ensure correctness
  let xmlSnippetWithRangeMarkers = xmlTextSnippet;
  forEachRight(issueRangesByOffsetAscending, (_) => {
    const startOffset = xmlTextDoc.offsetAt(_.start);
    const endOffset = xmlTextDoc.offsetAt(_.end);
    const prefix = xmlSnippetWithRangeMarkers.substring(0, startOffset);
    const toBeMarked = xmlSnippetWithRangeMarkers.substring(
      startOffset,
      endOffset
    );
    const suffix = xmlSnippetWithRangeMarkers.substring(endOffset);
    xmlSnippetWithRangeMarkers = prefix + "?" + toBeMarked + "?" + suffix;
  });

  return xmlSnippetWithRangeMarkers;
}
Example #2
Source File: completion-items.ts    From ui5-language-assistant with Apache License 2.0 6 votes vote down vote up
function createTextEditsForClassInTagName(
  originalRange: Range,
  suggestion: UI5ClassesInXMLTagNameCompletion,
  additionalTextEdits: TextEdit[]
): { range: Range; newText: string; filterText: string } {
  const range = getXMLTagNameRange(suggestion.astNode) ?? originalRange;
  let newText = suggestion.ui5Node.name;
  let tagName = newText;
  const nsPrefix = getClassNamespacePrefix(suggestion, additionalTextEdits);
  if (nsPrefix !== undefined) {
    tagName = `${nsPrefix}:${tagName}`;
    newText = tagName;
  }
  // If the additionalTextEdits are in the same position as the insert text add them to
  // the insert text instead.
  // This could happen if the suggestion is on the root tag and we also want to add the namespace.
  // Looping backwards so we can remove elements from the array.
  forEachRight(additionalTextEdits, (edit, index) => {
    if (rangeContains(range, edit.range)) {
      newText += edit.newText;
      additionalTextEdits.splice(index, 1);
    }
  });
  // Auto-close tag and put the cursor where attributes can be added (only if there is nothing else in this tag)
  /* istanbul ignore else */
  if (shouldCloseXMLElement(suggestion.astNode)) {
    newText += ` \${1}>\${0}</${tagName}>`;
  } else {
    additionalTextEdits.push(
      ...getClosingTagTextEdits(suggestion.astNode, tagName)
    );
  }
  // Class name in tag can be filtered by FQN or xmlns (or none of them): all of "m:But", "But" and "sap.m.But"
  // should return "m:Button" in the tag name for sap.m.Button.
  // Use both namespaced options in the filter text to make sure the result is not filtered out
  // (the simple name option is contained in both).
  const filterText = `${tagName} ${ui5NodeToFQN(suggestion.ui5Node)}`;
  return { range, newText, filterText };
}