|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Equinor ASA |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { SepticCnfg } from "./cnfg"; |
| 7 | +import { SepticAttribute } from "./elements"; |
| 8 | +import { |
| 9 | + SepticMetaInfoProvider, |
| 10 | + SepticAttributeDocumentation, |
| 11 | + ISepticObjectDocumentation, |
| 12 | +} from "./metaInfoProvider"; |
| 13 | + |
| 14 | +export interface ObjectDocProvider { |
| 15 | + getObjectDocumentation( |
| 16 | + objectType: string, |
| 17 | + ): ISepticObjectDocumentation | undefined; |
| 18 | +} |
| 19 | + |
| 20 | +interface OffsetRange { |
| 21 | + start: number; |
| 22 | + end: number; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Returns the text of a cnfg with all attributes set to their default values removed. |
| 27 | + */ |
| 28 | +export function removeDefaultLines( |
| 29 | + cnfg: SepticCnfg, |
| 30 | + provider?: ObjectDocProvider, |
| 31 | +): string { |
| 32 | + const metaInfo = provider ?? SepticMetaInfoProvider.getInstance(); |
| 33 | + const text = cnfg.doc.getText(); |
| 34 | + const rangesToRemove: OffsetRange[] = []; |
| 35 | + |
| 36 | + for (const obj of cnfg.objects) { |
| 37 | + const objDoc = metaInfo.getObjectDocumentation(obj.type); |
| 38 | + if (!objDoc) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + for (const attr of obj.attributes) { |
| 42 | + const attrDoc = objDoc.getAttribute(attr.key); |
| 43 | + if (!attrDoc || !attrDoc.default || attrDoc.default.length === 0) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + if (isDefault(attr, attrDoc)) { |
| 47 | + const range = getAttributeLineRange(text, attr); |
| 48 | + if (range) { |
| 49 | + rangesToRemove.push(range); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (rangesToRemove.length === 0) { |
| 56 | + return text; |
| 57 | + } |
| 58 | + |
| 59 | + // Sort by start offset descending so removals don't shift earlier offsets |
| 60 | + rangesToRemove.sort((a, b) => b.start - a.start); |
| 61 | + |
| 62 | + let result = text; |
| 63 | + for (const range of rangesToRemove) { |
| 64 | + result = result.slice(0, range.start) + result.slice(range.end); |
| 65 | + } |
| 66 | + return result; |
| 67 | +} |
| 68 | + |
| 69 | +function isDefault( |
| 70 | + attr: SepticAttribute, |
| 71 | + attrDoc: SepticAttributeDocumentation, |
| 72 | +): boolean { |
| 73 | + const values = attr.getValues(); |
| 74 | + const defaults: (string | number)[] = attrDoc.default; |
| 75 | + |
| 76 | + if (values.length !== defaults.length) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + for (let i = 0; i < values.length; i++) { |
| 81 | + if (normalizeValue(values[i]!) !== normalizeValue(defaults[i]!)) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + return true; |
| 86 | +} |
| 87 | + |
| 88 | +function normalizeValue(value: string | number): string { |
| 89 | + let str = String(value); |
| 90 | + // Remove surrounding quotes |
| 91 | + if ( |
| 92 | + (str.startsWith('"') && str.endsWith('"')) || |
| 93 | + (str.startsWith("'") && str.endsWith("'")) |
| 94 | + ) { |
| 95 | + str = str.slice(1, -1); |
| 96 | + } |
| 97 | + return str.trim(); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Returns the full line range (including trailing newline) that contains the attribute. |
| 102 | + * Expands to cover all lines the attribute spans. |
| 103 | + */ |
| 104 | +function getAttributeLineRange( |
| 105 | + text: string, |
| 106 | + attr: SepticAttribute, |
| 107 | +): OffsetRange | undefined { |
| 108 | + // Find start of line containing attr.start |
| 109 | + let lineStart = attr.start; |
| 110 | + while (lineStart > 0 && text[lineStart - 1] !== "\n") { |
| 111 | + lineStart--; |
| 112 | + } |
| 113 | + |
| 114 | + // Find end of line containing attr.end |
| 115 | + let lineEnd = attr.end; |
| 116 | + while (lineEnd < text.length && text[lineEnd] !== "\n") { |
| 117 | + lineEnd++; |
| 118 | + } |
| 119 | + // Include the newline character |
| 120 | + if (lineEnd < text.length && text[lineEnd] === "\n") { |
| 121 | + lineEnd++; |
| 122 | + } |
| 123 | + |
| 124 | + return { start: lineStart, end: lineEnd }; |
| 125 | +} |
0 commit comments