private closestNonEmpty()

in src/lib/CodeMirror/indentationMarkers.ts [242:276]


	private closestNonEmpty(from: Line, dir: -1 | 1) {
		let lineNo = from.number + dir;

		while (dir === -1 ? lineNo >= 1 : lineNo <= this.state.doc.lines) {
			if (this.has(lineNo)) {
				const entry = this.get(lineNo);
				if (!entry.empty) {
					return entry;
				}
			}

			// we can check if the line is empty, if it's not, we can
			// just create a new entry for it and return it.
			// this prevents us from hitting the beginning/end of the document unnecessarily.

			const line = this.state.doc.line(lineNo);

			if (line.text.trim().length) {
				const col = numColumns(line.text, this.state.tabSize);
				const level = Math.floor(col / this.unitWidth);

				return this.set(line, col, level);
			}

			lineNo += dir;
		}

		// if we're here, we didn't find anything.
		// that means we're at the beginning/end of the document,
		// and the first/last line is empty.

		const line = this.state.doc.line(dir === -1 ? 1 : this.state.doc.lines);

		return this.set(line, 0, 0);
	}