|
| 1 | +/** |
| 2 | + * @module |
| 3 | + * Facilitator of code formatting by |
| 4 | + * converting original text to 'healed' formattable text, |
| 5 | + * and converting formatted 'healed' text to |
| 6 | + * an 'unhealed' formatted string fit for use as a substitute for the original |
| 7 | + */ |
| 8 | + |
| 9 | +import * as cursorDocUtils from '../../cursor-doc/utilities'; |
| 10 | + |
| 11 | +/** |
| 12 | + * 'Healed' text suitable for code-formatting |
| 13 | + * and details with which a formatted version of it |
| 14 | + * can be translated back to unhealed replacment text |
| 15 | + */ |
| 16 | +type BandageContext = { |
| 17 | + healedText: string; |
| 18 | + details: { |
| 19 | + originalIndent: number; |
| 20 | + originalText: string; |
| 21 | + eol: string; |
| 22 | + missingTexts: cursorDocUtils.MissingTexts; |
| 23 | + }; |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Formattable text (fixed up to compensate for apparently missing list openers or closers) |
| 28 | + * and context with which to unbandage a formatted version of the same and recover the exact text |
| 29 | + * to substitute back into the document. |
| 30 | + * @param originalText Text to be reformatted |
| 31 | + * @param originalIndent Character offset where the first line of originalText begins |
| 32 | + * @param eol Newline characters that should be used in the code |
| 33 | + * @returns healedText - patched-up forms to be given to a code formatter, and other context the unbandage function needs |
| 34 | + */ |
| 35 | +export function bandage(originalText: string, originalIndent: number, eol: string): BandageContext { |
| 36 | + const missingTexts = cursorDocUtils.getMissingBrackets(originalText); |
| 37 | + const healedText = `${missingTexts.prepend}${originalText.trim()}${missingTexts.append}`; |
| 38 | + return { |
| 39 | + healedText, |
| 40 | + details: { eol, originalText, originalIndent, missingTexts }, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Formatted substitute text derived from formattedHealedText by removing bandages and reimposing an indentation. |
| 46 | + * @param context Context from bandage |
| 47 | + * @param formattedHealedText Formatted version of the healedText |
| 48 | + * @returns |
| 49 | + */ |
| 50 | +export function unbandage(context: BandageContext, formattedHealedText: string) { |
| 51 | + const d = context.details; |
| 52 | + const leadingWs = d.originalText.match(/^\s*/)[0]; |
| 53 | + const trailingWs = d.originalText.match(/\s*$/)[0]; |
| 54 | + const leadingEolPos = leadingWs.lastIndexOf(d.eol); |
| 55 | + const startIndent = |
| 56 | + leadingEolPos === -1 ? d.originalIndent : leadingWs.length - leadingEolPos - d.eol.length; |
| 57 | + const formattedText = formattedHealedText |
| 58 | + .substring( |
| 59 | + d.missingTexts.prepend.length, |
| 60 | + d.missingTexts.prepend.length + formattedHealedText.length - d.missingTexts.append.length |
| 61 | + ) |
| 62 | + .split(d.eol) |
| 63 | + .map((line: string, i: number) => (i === 0 ? line : `${' '.repeat(startIndent)}${line}`)) |
| 64 | + .join(d.eol); |
| 65 | + const newText = `${formattedText.startsWith(leadingWs) ? '' : leadingWs}${formattedText}${ |
| 66 | + formattedText.endsWith(trailingWs) ? '' : trailingWs |
| 67 | + }`; |
| 68 | + return newText; |
| 69 | +} |
0 commit comments