2024-02-02 12:22:18 +03:00
|
|
|
import * as random from 'lib0/random'
|
|
|
|
import * as Y from 'yjs'
|
2024-02-20 02:57:42 +03:00
|
|
|
import {
|
|
|
|
Token,
|
|
|
|
asOwned,
|
|
|
|
isTokenId,
|
|
|
|
newExternalId,
|
|
|
|
subtreeRoots,
|
|
|
|
type AstId,
|
|
|
|
type Owned,
|
|
|
|
type SyncTokenId,
|
|
|
|
} from '.'
|
|
|
|
import { assert, assertDefined } from '../util/assert'
|
|
|
|
import type { SourceRangeEdit } from '../util/data/text'
|
|
|
|
import { defaultLocalOrigin, tryAsOrigin, type ExternalId, type Origin } from '../yjsModel'
|
2024-02-02 12:22:18 +03:00
|
|
|
import type { AstFields, FixedMap, Mutable } from './tree'
|
|
|
|
import {
|
|
|
|
Ast,
|
|
|
|
MutableAst,
|
|
|
|
MutableInvalid,
|
|
|
|
Wildcard,
|
2024-02-20 02:57:42 +03:00
|
|
|
composeFieldData,
|
2024-02-02 12:22:18 +03:00
|
|
|
invalidFields,
|
|
|
|
materializeMutable,
|
|
|
|
setAll,
|
|
|
|
} from './tree'
|
|
|
|
|
|
|
|
export interface Module {
|
|
|
|
edit(): MutableModule
|
|
|
|
root(): Ast | undefined
|
2024-02-20 02:57:42 +03:00
|
|
|
tryGet(id: AstId | undefined): Ast | undefined
|
2024-02-02 12:22:18 +03:00
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
/** Return the specified AST. Throws an exception if no AST with the provided ID was found. */
|
|
|
|
get(id: AstId): Ast
|
|
|
|
get(id: AstId | undefined): Ast | undefined
|
2024-02-02 12:22:18 +03:00
|
|
|
getToken(token: SyncTokenId): Token
|
|
|
|
getToken(token: SyncTokenId | undefined): Token | undefined
|
|
|
|
getAny(node: AstId | SyncTokenId): Ast | Token
|
|
|
|
has(id: AstId): boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ModuleUpdate {
|
2024-02-20 02:57:42 +03:00
|
|
|
nodesAdded: Set<AstId>
|
|
|
|
nodesDeleted: Set<AstId>
|
|
|
|
nodesUpdated: Set<AstId>
|
|
|
|
updateRoots: Set<AstId>
|
2024-02-02 12:22:18 +03:00
|
|
|
metadataUpdated: { id: AstId; changes: Map<string, unknown> }[]
|
2024-02-20 02:57:42 +03:00
|
|
|
origin: Origin | undefined
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type YNode = FixedMap<AstFields>
|
|
|
|
type YNodes = Y.Map<YNode>
|
|
|
|
|
|
|
|
export class MutableModule implements Module {
|
|
|
|
private readonly nodes: YNodes
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
private get ydoc() {
|
2024-02-02 12:22:18 +03:00
|
|
|
const ydoc = this.nodes.doc
|
|
|
|
assert(ydoc != null)
|
|
|
|
return ydoc
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return this module's copy of `ast`, if this module was created by cloning `ast`'s module. */
|
|
|
|
getVersion<T extends Ast>(ast: T): Mutable<T> {
|
2024-02-20 02:57:42 +03:00
|
|
|
const instance = this.get(ast.id)
|
2024-02-02 12:22:18 +03:00
|
|
|
return instance as Mutable<T>
|
|
|
|
}
|
|
|
|
|
|
|
|
edit(): MutableModule {
|
|
|
|
const doc = new Y.Doc()
|
|
|
|
Y.applyUpdateV2(doc, Y.encodeStateAsUpdateV2(this.ydoc))
|
|
|
|
return new MutableModule(doc)
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
applyEdit(edit: MutableModule, origin: Origin = defaultLocalOrigin) {
|
|
|
|
Y.applyUpdateV2(this.ydoc, Y.encodeStateAsUpdateV2(edit.ydoc), origin)
|
|
|
|
}
|
|
|
|
|
|
|
|
transact<T>(f: () => T, origin: Origin = defaultLocalOrigin): T {
|
|
|
|
return this.ydoc.transact(f, origin)
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:22:18 +03:00
|
|
|
root(): MutableAst | undefined {
|
|
|
|
return this.rootPointer()?.expression
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceRoot(newRoot: Owned | undefined): Owned | undefined {
|
|
|
|
if (newRoot) {
|
|
|
|
const rootPointer = this.rootPointer()
|
|
|
|
if (rootPointer) {
|
|
|
|
return rootPointer.expression.replace(newRoot)
|
|
|
|
} else {
|
|
|
|
invalidFields(this, this.baseObject('Invalid', undefined, ROOT_ID), {
|
|
|
|
whitespace: '',
|
|
|
|
node: newRoot,
|
|
|
|
})
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const oldRoot = this.root()
|
|
|
|
if (!oldRoot) return
|
|
|
|
this.nodes.delete(ROOT_ID)
|
|
|
|
oldRoot.fields.set('parent', undefined)
|
|
|
|
return asOwned(oldRoot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
syncRoot(root: Owned) {
|
|
|
|
this.replaceRoot(root)
|
|
|
|
this.gc()
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
syncToCode(code: string) {
|
|
|
|
const root = this.root()
|
|
|
|
if (root) {
|
|
|
|
root.syncToCode(code)
|
|
|
|
} else {
|
|
|
|
this.replaceRoot(Ast.parse(code, this))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Update the module according to changes to its corresponding source code. */
|
|
|
|
applyTextEdits(textEdits: SourceRangeEdit[], metadataSource?: Module) {
|
|
|
|
const root = this.root()
|
|
|
|
assertDefined(root)
|
|
|
|
root.applyTextEdits(textEdits, metadataSource)
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:22:18 +03:00
|
|
|
private gc() {
|
|
|
|
const live = new Set<AstId>()
|
|
|
|
const active = new Array<Ast>()
|
|
|
|
let next: Ast | undefined = this.root()
|
|
|
|
while (next) {
|
|
|
|
for (const child of next.children()) {
|
|
|
|
if (child instanceof Ast) active.push(child)
|
|
|
|
}
|
|
|
|
live.add(next.id)
|
|
|
|
next = active.pop()
|
|
|
|
}
|
|
|
|
const all = Array.from(this.nodes.keys())
|
|
|
|
for (const id of all) {
|
|
|
|
if (id === ROOT_ID) continue
|
|
|
|
assert(isAstId(id))
|
|
|
|
if (!live.has(id)) this.nodes.delete(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy the given node into the module. */
|
|
|
|
copy<T extends Ast>(ast: T): Owned<Mutable<T>> {
|
|
|
|
const id = newAstId(ast.typeName())
|
|
|
|
const fields = ast.fields.clone()
|
|
|
|
this.nodes.set(id, fields as any)
|
|
|
|
fields.set('id', id)
|
|
|
|
fields.set('parent', undefined)
|
|
|
|
const ast_ = materializeMutable(this, fields)
|
|
|
|
ast_.importReferences(ast.module)
|
|
|
|
return ast_ as Owned<Mutable<typeof ast>>
|
|
|
|
}
|
|
|
|
|
|
|
|
static Transient() {
|
|
|
|
return new this(new Y.Doc())
|
|
|
|
}
|
|
|
|
|
|
|
|
observe(observer: (update: ModuleUpdate) => void) {
|
2024-02-20 02:57:42 +03:00
|
|
|
const handle = (events: Y.YEvent<any>[], transaction: Y.Transaction) => {
|
|
|
|
observer(this.observeEvents(events, tryAsOrigin(transaction.origin)))
|
|
|
|
}
|
2024-02-06 19:44:24 +03:00
|
|
|
// Attach the observer first, so that if an update hook causes changes in reaction to the initial state update, we
|
|
|
|
// won't miss them.
|
|
|
|
this.nodes.observeDeep(handle)
|
|
|
|
observer(this.getStateAsUpdate())
|
|
|
|
return handle
|
|
|
|
}
|
|
|
|
|
|
|
|
unobserve(handle: ReturnType<typeof this.observe>) {
|
|
|
|
this.nodes.unobserveDeep(handle)
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getStateAsUpdate(): ModuleUpdate {
|
2024-02-20 02:57:42 +03:00
|
|
|
const updateBuilder = new UpdateBuilder(this, this.nodes, undefined)
|
2024-02-02 12:22:18 +03:00
|
|
|
for (const id of this.nodes.keys()) updateBuilder.addNode(id as AstId)
|
2024-02-20 02:57:42 +03:00
|
|
|
return updateBuilder.finish()
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
applyUpdate(update: Uint8Array, origin: Origin): ModuleUpdate | undefined {
|
2024-02-02 12:22:18 +03:00
|
|
|
let summary: ModuleUpdate | undefined
|
|
|
|
const observer = (events: Y.YEvent<any>[]) => {
|
2024-02-20 02:57:42 +03:00
|
|
|
summary = this.observeEvents(events, origin)
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
this.nodes.observeDeep(observer)
|
|
|
|
Y.applyUpdate(this.ydoc, update, origin)
|
|
|
|
this.nodes.unobserveDeep(observer)
|
|
|
|
return summary
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
private observeEvents(events: Y.YEvent<any>[], origin: Origin | undefined): ModuleUpdate {
|
|
|
|
const updateBuilder = new UpdateBuilder(this, this.nodes, origin)
|
2024-02-02 12:22:18 +03:00
|
|
|
for (const event of events) {
|
|
|
|
if (event.target === this.nodes) {
|
|
|
|
// Updates to the node map.
|
|
|
|
for (const [key, change] of event.changes.keys) {
|
|
|
|
const id = key as AstId
|
|
|
|
switch (change.action) {
|
|
|
|
case 'add':
|
|
|
|
updateBuilder.addNode(id)
|
|
|
|
break
|
|
|
|
case 'update':
|
|
|
|
updateBuilder.updateAllFields(id)
|
|
|
|
break
|
|
|
|
case 'delete':
|
|
|
|
updateBuilder.deleteNode(id)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (event.target.parent === this.nodes) {
|
|
|
|
// Updates to a node's fields.
|
|
|
|
assert(event.target instanceof Y.Map)
|
|
|
|
const id = event.target.get('id') as AstId
|
|
|
|
const node = this.nodes.get(id)
|
|
|
|
if (!node) continue
|
|
|
|
const changes: (readonly [string, unknown])[] = Array.from(event.changes.keys, ([key]) => [
|
|
|
|
key,
|
|
|
|
node.get(key as any),
|
|
|
|
])
|
|
|
|
updateBuilder.updateFields(id, changes)
|
|
|
|
} else if (event.target.parent.parent === this.nodes) {
|
|
|
|
// Updates to fields of a metadata object within a node.
|
|
|
|
const id = event.target.parent.get('id') as AstId
|
|
|
|
const node = this.nodes.get(id)
|
|
|
|
if (!node) continue
|
|
|
|
const metadata = node.get('metadata') as unknown as Map<string, unknown>
|
|
|
|
const changes: (readonly [string, unknown])[] = Array.from(event.changes.keys, ([key]) => [
|
|
|
|
key,
|
|
|
|
metadata.get(key as any),
|
|
|
|
])
|
|
|
|
updateBuilder.updateMetadata(id, changes)
|
|
|
|
}
|
|
|
|
}
|
2024-02-20 02:57:42 +03:00
|
|
|
return updateBuilder.finish()
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.nodes.clear()
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
get(id: AstId): Mutable
|
|
|
|
get(id: AstId | undefined): Mutable | undefined
|
|
|
|
get(id: AstId | undefined): Mutable | undefined {
|
2024-02-02 12:22:18 +03:00
|
|
|
if (!id) return undefined
|
2024-02-20 02:57:42 +03:00
|
|
|
const ast = this.tryGet(id)
|
2024-02-02 12:22:18 +03:00
|
|
|
assert(ast !== undefined, 'id in module')
|
|
|
|
return ast
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
tryGet(id: AstId | undefined): Mutable | undefined {
|
2024-02-02 12:22:18 +03:00
|
|
|
if (!id) return undefined
|
|
|
|
const nodeData = this.nodes.get(id)
|
|
|
|
if (!nodeData) return undefined
|
|
|
|
const fields = nodeData as any
|
|
|
|
return materializeMutable(this, fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
replace(id: AstId, value: Owned): Owned | undefined {
|
2024-02-20 02:57:42 +03:00
|
|
|
return this.tryGet(id)?.replace(value)
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
replaceValue(id: AstId, value: Owned): Owned | undefined {
|
2024-02-20 02:57:42 +03:00
|
|
|
return this.tryGet(id)?.replaceValue(value)
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
take(id: AstId): Owned {
|
2024-02-20 02:57:42 +03:00
|
|
|
return this.replace(id, Wildcard.new(this)) || asOwned(this.get(id))
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateValue<T extends MutableAst>(id: AstId, f: (x: Owned) => Owned<T>): T | undefined {
|
2024-02-20 02:57:42 +03:00
|
|
|
return this.tryGet(id)?.updateValue(f)
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
|
|
|
|
constructor(doc: Y.Doc) {
|
|
|
|
this.nodes = doc.getMap<YNode>('nodes')
|
|
|
|
}
|
|
|
|
|
|
|
|
private rootPointer(): MutableRootPointer | undefined {
|
2024-02-20 02:57:42 +03:00
|
|
|
const rootPointer = this.tryGet(ROOT_ID)
|
2024-02-02 12:22:18 +03:00
|
|
|
if (rootPointer) return rootPointer as MutableRootPointer
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
baseObject(type: string, externalId?: ExternalId, overrideId?: AstId): FixedMap<AstFields> {
|
|
|
|
const map = new Y.Map<unknown>()
|
|
|
|
const map_ = map as unknown as FixedMap<{}>
|
|
|
|
const id = overrideId ?? newAstId(type)
|
|
|
|
const metadata = new Y.Map<unknown>() as unknown as FixedMap<{}>
|
|
|
|
const metadataFields = setAll(metadata, {
|
|
|
|
externalId: externalId ?? newExternalId(),
|
|
|
|
})
|
|
|
|
const fields = setAll(map_, {
|
|
|
|
id,
|
|
|
|
type: type,
|
|
|
|
parent: undefined,
|
|
|
|
metadata: metadataFields,
|
|
|
|
})
|
2024-02-20 02:57:42 +03:00
|
|
|
const fieldObject = composeFieldData(fields, {})
|
|
|
|
this.nodes.set(id, fieldObject)
|
|
|
|
return fieldObject
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
getToken(token: SyncTokenId): Token
|
|
|
|
getToken(token: SyncTokenId | undefined): Token | undefined
|
|
|
|
getToken(token: SyncTokenId | undefined): Token | undefined {
|
|
|
|
if (!token) return token
|
|
|
|
if (token instanceof Token) return token
|
|
|
|
return Token.withId(token.code_, token.tokenType_, token.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
getAny(node: AstId | SyncTokenId): MutableAst | Token {
|
2024-02-20 02:57:42 +03:00
|
|
|
return isTokenId(node) ? this.getToken(node) : this.get(node)
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal Copy a node into the module, if it is bound to a different module. */
|
|
|
|
copyIfForeign<T extends MutableAst>(ast: Owned<T>): Owned<T>
|
|
|
|
copyIfForeign<T extends MutableAst>(ast: Owned<T> | undefined): Owned<T> | undefined {
|
|
|
|
if (!ast) return ast
|
|
|
|
if (ast.module === this) return ast
|
|
|
|
return this.copy(ast) as any
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
delete(id: AstId) {
|
|
|
|
this.nodes.delete(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
has(id: AstId) {
|
|
|
|
return this.nodes.has(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MutableRootPointer = MutableInvalid & { get expression(): MutableAst | undefined }
|
|
|
|
|
|
|
|
function newAstId(type: string): AstId {
|
|
|
|
return `ast:${type}#${random.uint53()}` as AstId
|
|
|
|
}
|
|
|
|
/** Checks whether the input looks like an AstId. */
|
|
|
|
export function isAstId(value: string): value is AstId {
|
|
|
|
return /ast:[A-Za-z]*#[0-9]*/.test(value)
|
|
|
|
}
|
|
|
|
export const ROOT_ID = `Root` as AstId
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
class UpdateBuilder {
|
|
|
|
readonly nodesAdded = new Set<AstId>()
|
|
|
|
readonly nodesDeleted = new Set<AstId>()
|
|
|
|
readonly nodesUpdated = new Set<AstId>()
|
2024-02-02 12:22:18 +03:00
|
|
|
readonly metadataUpdated: { id: AstId; changes: Map<string, unknown> }[] = []
|
2024-02-20 02:57:42 +03:00
|
|
|
readonly origin: Origin | undefined
|
2024-02-02 12:22:18 +03:00
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
private readonly module: Module
|
2024-02-02 12:22:18 +03:00
|
|
|
private readonly nodes: YNodes
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
constructor(module: Module, nodes: YNodes, origin: Origin | undefined) {
|
|
|
|
this.module = module
|
2024-02-02 12:22:18 +03:00
|
|
|
this.nodes = nodes
|
2024-02-20 02:57:42 +03:00
|
|
|
this.origin = origin
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addNode(id: AstId) {
|
2024-02-20 02:57:42 +03:00
|
|
|
this.nodesAdded.add(id)
|
2024-02-02 12:22:18 +03:00
|
|
|
this.updateAllFields(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAllFields(id: AstId) {
|
|
|
|
this.updateFields(id, this.nodes.get(id)!.entries())
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFields(id: AstId, changes: Iterable<readonly [string, unknown]>) {
|
2024-02-20 02:57:42 +03:00
|
|
|
let fieldsChanged = false
|
2024-02-02 12:22:18 +03:00
|
|
|
let metadataChanges = undefined
|
|
|
|
for (const entry of changes) {
|
|
|
|
const [key, value] = entry
|
|
|
|
if (key === 'metadata') {
|
|
|
|
assert(value instanceof Y.Map)
|
|
|
|
metadataChanges = new Map<string, unknown>(value.entries())
|
|
|
|
} else {
|
|
|
|
assert(!(value instanceof Y.AbstractType))
|
2024-02-20 02:57:42 +03:00
|
|
|
fieldsChanged = true
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-20 02:57:42 +03:00
|
|
|
if (fieldsChanged) this.nodesUpdated.add(id)
|
2024-02-02 12:22:18 +03:00
|
|
|
if (metadataChanges) this.metadataUpdated.push({ id, changes: metadataChanges })
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMetadata(id: AstId, changes: Iterable<readonly [string, unknown]>) {
|
|
|
|
const changeMap = new Map<string, unknown>()
|
|
|
|
for (const [key, value] of changes) changeMap.set(key, value)
|
|
|
|
this.metadataUpdated.push({ id, changes: changeMap })
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteNode(id: AstId) {
|
2024-02-20 02:57:42 +03:00
|
|
|
this.nodesDeleted.add(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
finish(): ModuleUpdate {
|
|
|
|
const updateRoots = subtreeRoots(this.module, new Set(this.nodesUpdated.keys()))
|
|
|
|
return { ...this, updateRoots }
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
}
|