Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 104x 104x 1x 1x 63x 63x 1x 1x 91x 91x 1x 1x 16x 16x 16x 1x 1x 16x 16x 12x 16x 11x 16x 16x 16x 16x 9x 9x 16x 1x 1x 5x 5x 5x 5x 11x 11x 11x 11x 5x 5x 5x 5x 5x 5x 5x 5x 5x 6x 6x 6x 6x 1x 1x 7x 7x 5x 5x 5x 7x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 7x 3x 3x 3x 3x 2x 2x 7x 7x 7x 7x 7x 7x 7x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 3x 3x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 4x 4x 9x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 6x 6x 2x 2x 11x 6x 6x 6x 6x 6x 6x 11x 11x 4x 4x 4x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x 1x 1x 27x 27x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 3x 3x 3x 5x | import { imemo, trimArray } from "../coreTable/CoreTableUtils"
import { isPresent } from "../clientUtils/isPresent"
import { didYouMean, isBlankLine, isEmpty } from "./GrammarUtils"
import {
CellPosition,
CellDef,
CellHasErrorsClass,
MatrixLine,
MatrixProgram,
Grammar,
FrontierCellClass,
ParsedCell,
CommentCellDef,
SubTableValueCellDef,
WorkInProgressCellDef,
NothingGoesThereCellDef,
} from "./GridLangConstants"
export class GridCell implements ParsedCell {
private position: CellPosition
private matrix: MatrixProgram
private rootDefinition: CellDef
constructor(
matrix: MatrixProgram,
position: CellPosition,
rootDefinition: CellDef
) {
this.position = position
this.matrix = matrix
this.rootDefinition = rootDefinition
}
private get row() {
return this.position.row
}
private get column() {
return this.position.column
}
private get line(): MatrixLine | undefined {
return this.matrix[this.row]
}
private get isCommentCell() {
const { contents } = this
return contents && CommentCellDef.regex!.test(contents)
}
private get cellTerminalTypeDefinition(): CellDef | undefined {
const { rootDefinition } = this
if (this.isCommentCell) return CommentCellDef
const grammar = rootDefinition.grammar as unknown as Grammar
if (this.column === 0) return rootDefinition as unknown as CellDef
const firstWordOnLine = this.line ? this.line[0] : undefined
const isFirstWordAKeyword =
firstWordOnLine && grammar[firstWordOnLine] !== undefined
if (this.column === 1 && firstWordOnLine && isFirstWordAKeyword)
return grammar[firstWordOnLine]
if (!isFirstWordAKeyword) return undefined
// It has a keyword but it is column >1
const def = grammar[firstWordOnLine!]
const positionalCellTypeDef =
def.positionalCellDefs && def.positionalCellDefs[this.column - 2]
if (positionalCellTypeDef) return positionalCellTypeDef
if (def.isHorizontalList && this.contents) return def // For now only return a def for lists if it is non-empty
return NothingGoesThereCellDef
}
private get parentSubTableInfo() {
const { row, matrix } = this
let pointerRow = row
let subTableHeaderRow = -1
while (pointerRow >= 0) {
const line = matrix[pointerRow]
if (!line) break
const parentKeyword = line[0]
if (parentKeyword) {
if (subTableHeaderRow === -1) return undefined
return {
parentKeyword,
parentRow: pointerRow,
subTableHeaderRow,
isCellInHeader: row === subTableHeaderRow,
headerKeyword: matrix[subTableHeaderRow][this.column],
}
}
if (!isBlankLine(line)) subTableHeaderRow = pointerRow
pointerRow--
}
return undefined
}
@imemo private get subTableParseResults() {
const { cellTerminalTypeDefinition } = this
if (cellTerminalTypeDefinition) return undefined
const info = this.parentSubTableInfo
if (!info) return undefined
const {
parentKeyword,
isCellInHeader,
subTableHeaderRow,
headerKeyword,
} = info
const subTableDef = this.rootDefinition.grammar![parentKeyword]
const headerCellDef = subTableDef && subTableDef.headerCellDef
if (!headerCellDef) return undefined
const headerGrammar = headerCellDef.grammar
const valueCellDef =
!isCellInHeader && headerGrammar
? headerGrammar[headerKeyword]
: undefined
return {
isFrontierCell: this.isSubTableFrontierCell(
subTableHeaderRow,
subTableDef
),
def: isCellInHeader
? headerCellDef
: valueCellDef ?? SubTableValueCellDef,
}
}
/**
* If a cell is:
* - to the right of the last filled cell in a line
* - and that line is indented to be part of a subtable, with options
* - and it is the first non-blank line in the subtabel
*
* Then consider is a "frontier cell"
*
*/
private isSubTableFrontierCell(headerRow: number, subTableDef: CellDef) {
const { line, column, row } = this
const grammar = subTableDef.headerCellDef!.grammar
const isToTheImmediateRightOfLastFullCell =
line && trimArray(line).length === column
return (
row === headerRow &&
!isBlankLine(line) &&
isToTheImmediateRightOfLastFullCell &&
grammar &&
Object.keys(grammar).length
)
}
// If true show a +
private get isFirstCellOnFrontierRow() {
const { row, column } = this
const numRows = this.matrix.length
if (column) return false // Only first column should have a +
if (!isBlankLine(this.line)) return false // Only blank lines can be frontier
if (numRows === 1) {
if (row === 1) return !isBlankLine(this.matrix[0])
return row === 0
}
return row === numRows
}
private get suggestions() {
return []
}
private get definitionLinks(): CellPosition[] {
return []
}
private get implementationLinks(): CellPosition[] {
return []
}
@imemo get cellDef(): CellDef {
const def = this.cellTerminalTypeDefinition
if (def) return def
const subTable = this.subTableParseResults?.def
if (subTable) return subTable as unknown as CellDef
return WorkInProgressCellDef
}
get errorMessage() {
if (!this.line) return ""
const { cellDef, contents, optionKeywords } = this
const { regex, requirementsDescription, catchAllCellDef } = cellDef
const catchAllKeywordRegex = catchAllCellDef?.regex
if (contents === undefined || contents === "") return ""
const regexResult = regex
? validate(contents, regex, requirementsDescription)
: undefined
const catchAllRegexResult = catchAllKeywordRegex
? validate(
contents,
catchAllKeywordRegex,
catchAllCellDef!.requirementsDescription
)
: undefined
if (optionKeywords) {
if (optionKeywords.includes(contents)) return ""
if (regex) return regexResult
if (catchAllKeywordRegex) return catchAllRegexResult
const guess = didYouMean(contents, optionKeywords)
if (guess) return `Did you mean '${guess}'?`
return `Error: '${contents}' is not a valid option. Valid options are ${optionKeywords
.map((opt) => `'${opt}'`)
.join(", ")}`
}
if (regex) return regexResult
if (catchAllKeywordRegex) return catchAllRegexResult
return ""
}
get contents() {
return this.line ? this.line[this.column] : undefined
}
get comment() {
const { contents, errorMessage, cellDef } = this
if (isEmpty(contents)) return undefined
if (errorMessage) return errorMessage
if (cellDef.grammar) {
const def = cellDef.grammar[contents!] ?? cellDef.catchAllCellDef
return def.description
}
return [cellDef.description].join("\n")
}
get cssClasses() {
const { errorMessage, cellDef } = this
if (errorMessage) return [CellHasErrorsClass]
const showArrow =
this.isFirstCellOnFrontierRow ||
this.subTableParseResults?.isFrontierCell
? FrontierCellClass
: undefined
const hasSuggestions =
this.cellDef.keyword === "table" ? "HasSuggestions" : null // todo: switch from strings to constants
return [cellDef.cssClass, hasSuggestions, showArrow].filter(isPresent)
}
get placeholder() {
const { contents, cellDef } = this
const { terminalOptions } = cellDef
const firstOption = terminalOptions && terminalOptions[0]
const firstOptionName = firstOption && firstOption.keyword
const placeholder = cellDef.valuePlaceholder ?? firstOptionName
return isEmpty(contents) && placeholder
? `eg "${placeholder}"`
: undefined
}
get optionKeywords() {
const { cellDef } = this
const { grammar, headerCellDef, terminalOptions } = cellDef
return terminalOptions
? terminalOptions.map((def) => def.keyword)
: grammar
? Object.keys(grammar)
: headerCellDef
? Object.keys(headerCellDef.grammar!)
: undefined
}
}
const validate = (value: any, regex: RegExp, requirements?: string) => {
if (typeof value !== "string") return ""
if (!regex.test(value)) {
return `Error: ${
requirements
? requirements
: `'${value}' did not validate against ${regex}`
}`
}
return ""
}
|