|
| 1 | +import { Position, Range } from 'vscode'; |
| 2 | +import { DclAtom } from './dclAtom'; |
| 3 | +import { IDclContainer, IDclFragment } from './dclInterfaces'; |
| 4 | + |
| 5 | +export class DclAttribute implements IDclContainer { |
| 6 | + |
| 7 | + get line(): number { |
| 8 | + return this.length === 0 ? -1 : this.firstAtom.line; |
| 9 | + } |
| 10 | + |
| 11 | + get column(): number { |
| 12 | + return this.length === 0 ? -1 : this.firstAtom.column; |
| 13 | + } |
| 14 | + |
| 15 | + get asAttribute(): DclAttribute { |
| 16 | + return this; |
| 17 | + } |
| 18 | + |
| 19 | + get asContainer(): IDclContainer { |
| 20 | + return this; |
| 21 | + } |
| 22 | + |
| 23 | + get isComment(): boolean { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + get isBlockComment(): boolean { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + get isString(): boolean { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + get range(): Range { |
| 36 | + const lastAtom = this.atoms[this.length - 1]; |
| 37 | + return new Range(this.firstAtom.range.start, lastAtom.range.end); |
| 38 | + } |
| 39 | + |
| 40 | + equal(dclObject: IDclFragment): boolean { |
| 41 | + return JSON.stringify(this) === JSON.stringify(dclObject); |
| 42 | + } |
| 43 | + |
| 44 | + contains(position: Position): boolean { |
| 45 | + return this.range.contains(position); |
| 46 | + } |
| 47 | + |
| 48 | + getAtomFromPosition(position: Position): IDclFragment { |
| 49 | + if (this.contains(position)) { |
| 50 | + for (let i = 0; i < this.length; i++) { |
| 51 | + if (!this.atoms[i].contains(position)) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + return this.atoms[i]; |
| 55 | + } |
| 56 | + } |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + public atoms: Array<IDclFragment>; |
| 61 | + |
| 62 | + get length(): number { |
| 63 | + return this.atoms.length; |
| 64 | + } |
| 65 | + |
| 66 | + get firstAtom(): IDclFragment { |
| 67 | + return this.atoms[0]; |
| 68 | + } |
| 69 | + |
| 70 | + get lastAtom(): IDclFragment { |
| 71 | + return this.atoms[this.length - 1]; |
| 72 | + } |
| 73 | + |
| 74 | + get firstNonComment(): IDclFragment { |
| 75 | + return this.length === 0 ? null : this.firstAtom; |
| 76 | + } |
| 77 | + |
| 78 | + getParentFrom(from: Position|IDclFragment, tilesOnly: boolean = false): IDclContainer { |
| 79 | + const pos = from instanceof Position ? from : from.range.start; |
| 80 | + if (this.contains(pos)) { |
| 81 | + return tilesOnly ? null : this; |
| 82 | + } |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + flatten(into?: Array<DclAtom>): Array<DclAtom> { |
| 87 | + if (!into) { |
| 88 | + into = []; |
| 89 | + } |
| 90 | + this.atoms.forEach(item => { |
| 91 | + into.push(item as DclAtom); |
| 92 | + }); |
| 93 | + return into; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + // Everything above this point is sequenced by IDclFragment & then IDclContainer contract |
| 98 | + // Everything below this point is unique to DclAttribute and not an interface requirement |
| 99 | + |
| 100 | + |
| 101 | + constructor(atoms: Array<DclAtom>) { |
| 102 | + this.atoms = [...atoms]; |
| 103 | + } |
| 104 | + |
| 105 | + get key(): IDclFragment { |
| 106 | + return this.length === 0 ? null : this.firstAtom; |
| 107 | + } |
| 108 | + get delineator(): IDclFragment { |
| 109 | + return this.length < 3 ? null : this.atoms[1]; |
| 110 | + } |
| 111 | + get value(): IDclFragment { |
| 112 | + return this.length < 3 ? null : this.atoms[2]; |
| 113 | + } |
| 114 | + |
| 115 | + // Context: DCL Attributes are valid in 2 arrangements: "Key = Value;" OR "key;" |
| 116 | + // Any other arrangement should be considered a malformed syntax error. |
| 117 | + get isWellFormed(): boolean { |
| 118 | + const invalid = this.length < 2 // probably has key, but missing semi-colon |
| 119 | + || this.length === 3 // probably missing equal or semi-colon |
| 120 | + || this.length > 4 // exceeds possible key-value-pair structure |
| 121 | + || this.lastAtom.symbol !== ';' // invalid attribute termination |
| 122 | + || this.firstAtom.isString // strings are invalid keys |
| 123 | + || (this.length === 4 && this.atoms[1].symbol !== '='); |
| 124 | + return !invalid; |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments