2023-10-07 23:57:47 +03:00
|
|
|
import * as object from 'lib0/object'
|
|
|
|
import * as random from 'lib0/random'
|
2023-09-22 06:43:25 +03:00
|
|
|
import * as Y from 'yjs'
|
|
|
|
|
|
|
|
export type Uuid = `${string}-${string}-${string}-${string}-${string}`
|
2024-01-24 22:22:05 +03:00
|
|
|
|
|
|
|
declare const brandExternalId: unique symbol
|
|
|
|
/** Identifies an AST node or token. Used in module serialization and communication with the language server. */
|
|
|
|
export type ExternalId = Uuid & { [brandExternalId]: never }
|
2023-09-22 06:43:25 +03:00
|
|
|
|
2023-10-07 23:57:47 +03:00
|
|
|
export type VisualizationModule =
|
|
|
|
| { kind: 'Builtin' }
|
|
|
|
| { kind: 'CurrentProject' }
|
|
|
|
| { kind: 'Library'; name: string }
|
|
|
|
|
|
|
|
export interface VisualizationIdentifier {
|
|
|
|
module: VisualizationModule
|
|
|
|
name: string
|
|
|
|
}
|
|
|
|
|
2023-12-01 15:41:24 +03:00
|
|
|
export interface VisualizationMetadata {
|
|
|
|
identifier: VisualizationIdentifier | null
|
2023-10-07 23:57:47 +03:00
|
|
|
visible: boolean
|
2024-02-29 19:37:40 +03:00
|
|
|
fullscreen: boolean
|
|
|
|
width: number | null
|
2023-10-07 23:57:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function visMetadataEquals(
|
|
|
|
a: VisualizationMetadata | null | undefined,
|
|
|
|
b: VisualizationMetadata | null | undefined,
|
|
|
|
) {
|
|
|
|
return (
|
2023-12-01 15:41:24 +03:00
|
|
|
(!a && !b) ||
|
2024-02-29 19:37:40 +03:00
|
|
|
(a &&
|
|
|
|
b &&
|
|
|
|
a.visible === b.visible &&
|
|
|
|
a.fullscreen == b.fullscreen &&
|
|
|
|
a.width == b.width &&
|
|
|
|
visIdentifierEquals(a.identifier, b.identifier))
|
2023-10-07 23:57:47 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-01 15:41:24 +03:00
|
|
|
export function visIdentifierEquals(
|
|
|
|
a: VisualizationIdentifier | null | undefined,
|
|
|
|
b: VisualizationIdentifier | null | undefined,
|
|
|
|
) {
|
|
|
|
return (!a && !b) || (a && b && a.name === b.name && object.equalFlat(a.module, b.module))
|
2023-10-07 23:57:47 +03:00
|
|
|
}
|
|
|
|
|
2023-10-30 18:08:08 +03:00
|
|
|
export type ProjectSetting = string
|
2023-09-22 06:43:25 +03:00
|
|
|
|
|
|
|
export class DistributedProject {
|
|
|
|
doc: Y.Doc
|
|
|
|
name: Y.Text
|
|
|
|
modules: Y.Map<Y.Doc>
|
2023-10-30 18:08:08 +03:00
|
|
|
settings: Y.Map<ProjectSetting>
|
2023-09-22 06:43:25 +03:00
|
|
|
|
|
|
|
constructor(doc: Y.Doc) {
|
|
|
|
this.doc = doc
|
|
|
|
this.name = this.doc.getText('name')
|
|
|
|
this.modules = this.doc.getMap('modules')
|
2023-10-30 18:08:08 +03:00
|
|
|
this.settings = this.doc.getMap('settings')
|
2023-09-22 06:43:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
moduleNames(): string[] {
|
|
|
|
return Array.from(this.modules.keys())
|
|
|
|
}
|
|
|
|
|
|
|
|
findModuleByDocId(id: string): string | null {
|
|
|
|
for (const [name, doc] of this.modules.entries()) {
|
|
|
|
if (doc.guid === id) return name
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
async openModule(name: string): Promise<DistributedModule | null> {
|
|
|
|
const doc = this.modules.get(name)
|
|
|
|
if (doc == null) return null
|
|
|
|
return await DistributedModule.load(doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
openUnloadedModule(name: string): DistributedModule | null {
|
|
|
|
const doc = this.modules.get(name)
|
|
|
|
if (doc == null) return null
|
|
|
|
return new DistributedModule(doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
createUnloadedModule(name: string, doc: Y.Doc): DistributedModule {
|
|
|
|
this.modules.set(name, doc)
|
|
|
|
return new DistributedModule(doc)
|
|
|
|
}
|
|
|
|
|
2023-10-29 22:02:07 +03:00
|
|
|
createNewModule(name: string): DistributedModule {
|
2023-09-22 06:43:25 +03:00
|
|
|
return this.createUnloadedModule(name, new Y.Doc())
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteModule(name: string): void {
|
|
|
|
this.modules.delete(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose(): void {
|
|
|
|
this.doc.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 15:01:03 +03:00
|
|
|
export class ModuleDoc {
|
|
|
|
ydoc: Y.Doc
|
2024-02-02 12:22:18 +03:00
|
|
|
nodes: Y.Map<any>
|
2023-10-02 15:01:03 +03:00
|
|
|
constructor(ydoc: Y.Doc) {
|
|
|
|
this.ydoc = ydoc
|
2024-02-02 12:22:18 +03:00
|
|
|
this.nodes = ydoc.getMap('nodes')
|
2023-10-02 15:01:03 +03:00
|
|
|
}
|
|
|
|
}
|
2023-09-22 06:43:25 +03:00
|
|
|
|
2023-10-02 15:01:03 +03:00
|
|
|
export class DistributedModule {
|
|
|
|
doc: ModuleDoc
|
2023-10-07 23:57:47 +03:00
|
|
|
undoManager: Y.UndoManager
|
2023-10-02 15:01:03 +03:00
|
|
|
|
|
|
|
static async load(ydoc: Y.Doc): Promise<DistributedModule> {
|
|
|
|
ydoc.load()
|
|
|
|
await ydoc.whenLoaded
|
|
|
|
return new DistributedModule(ydoc)
|
2023-09-22 06:43:25 +03:00
|
|
|
}
|
|
|
|
|
2023-10-02 15:01:03 +03:00
|
|
|
constructor(ydoc: Y.Doc) {
|
|
|
|
this.doc = new ModuleDoc(ydoc)
|
2024-02-02 12:22:18 +03:00
|
|
|
this.undoManager = new Y.UndoManager([this.doc.nodes])
|
2023-09-22 06:43:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose(): void {
|
2023-10-02 15:01:03 +03:00
|
|
|
this.doc.ydoc.destroy()
|
2023-09-22 06:43:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
export const localOrigins = ['local', 'local:CodeEditor'] as const
|
|
|
|
export type LocalOrigin = (typeof localOrigins)[number]
|
|
|
|
export type Origin = LocalOrigin | 'remote'
|
|
|
|
/** Locally-originated changes not otherwise specified. */
|
|
|
|
export const defaultLocalOrigin: LocalOrigin = 'local'
|
|
|
|
export function isLocalOrigin(origin: string): origin is LocalOrigin {
|
|
|
|
const localOriginNames: readonly string[] = localOrigins
|
|
|
|
return localOriginNames.includes(origin)
|
|
|
|
}
|
|
|
|
export function tryAsOrigin(origin: string): Origin | undefined {
|
|
|
|
if (isLocalOrigin(origin)) return origin
|
|
|
|
if (origin === 'remote') return origin
|
|
|
|
}
|
|
|
|
|
2023-12-19 08:41:14 +03:00
|
|
|
export type SourceRange = readonly [start: number, end: number]
|
2024-01-24 22:22:05 +03:00
|
|
|
declare const brandSourceRangeKey: unique symbol
|
|
|
|
export type SourceRangeKey = string & { [brandSourceRangeKey]: never }
|
|
|
|
|
|
|
|
export function sourceRangeKey(range: SourceRange): SourceRangeKey {
|
|
|
|
return `${range[0].toString(16)}:${range[1].toString(16)}` as SourceRangeKey
|
|
|
|
}
|
|
|
|
export function sourceRangeFromKey(key: SourceRangeKey): SourceRange {
|
|
|
|
return key.split(':').map((x) => parseInt(x, 16)) as [number, number]
|
|
|
|
}
|
2023-09-22 06:43:25 +03:00
|
|
|
|
|
|
|
export class IdMap {
|
2024-01-24 22:22:05 +03:00
|
|
|
private readonly rangeToExpr: Map<string, ExternalId>
|
2023-09-22 06:43:25 +03:00
|
|
|
|
2024-01-24 22:22:05 +03:00
|
|
|
constructor(entries?: [string, ExternalId][]) {
|
2023-12-19 20:58:11 +03:00
|
|
|
this.rangeToExpr = new Map(entries ?? [])
|
2023-09-22 06:43:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-15 19:26:18 +03:00
|
|
|
static Mock(): IdMap {
|
2023-12-19 20:58:11 +03:00
|
|
|
return new IdMap([])
|
2023-11-15 19:26:18 +03:00
|
|
|
}
|
|
|
|
|
2024-01-24 22:22:05 +03:00
|
|
|
insertKnownId(range: SourceRange, id: ExternalId) {
|
|
|
|
const key = sourceRangeKey(range)
|
2023-09-22 06:43:25 +03:00
|
|
|
this.rangeToExpr.set(key, id)
|
|
|
|
}
|
|
|
|
|
2024-01-24 22:22:05 +03:00
|
|
|
getIfExist(range: SourceRange): ExternalId | undefined {
|
|
|
|
const key = sourceRangeKey(range)
|
2023-10-24 17:05:12 +03:00
|
|
|
return this.rangeToExpr.get(key)
|
|
|
|
}
|
|
|
|
|
2024-01-24 22:22:05 +03:00
|
|
|
getOrInsertUniqueId(range: SourceRange): ExternalId {
|
|
|
|
const key = sourceRangeKey(range)
|
2023-09-22 06:43:25 +03:00
|
|
|
const val = this.rangeToExpr.get(key)
|
2023-12-19 20:58:11 +03:00
|
|
|
if (val !== undefined) {
|
2023-09-22 06:43:25 +03:00
|
|
|
return val
|
|
|
|
} else {
|
2024-01-24 22:22:05 +03:00
|
|
|
const newId = random.uuidv4() as ExternalId
|
2023-09-22 06:43:25 +03:00
|
|
|
this.rangeToExpr.set(key, newId)
|
|
|
|
return newId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 22:22:05 +03:00
|
|
|
entries(): [SourceRangeKey, ExternalId][] {
|
|
|
|
return [...this.rangeToExpr] as [SourceRangeKey, ExternalId][]
|
2023-09-22 06:43:25 +03:00
|
|
|
}
|
|
|
|
|
2023-12-19 20:58:11 +03:00
|
|
|
get size(): number {
|
|
|
|
return this.rangeToExpr.size
|
2023-09-22 06:43:25 +03:00
|
|
|
}
|
|
|
|
|
2023-12-19 20:58:11 +03:00
|
|
|
clear(): void {
|
|
|
|
this.rangeToExpr.clear()
|
|
|
|
}
|
2023-09-22 06:43:25 +03:00
|
|
|
|
2023-12-19 20:58:11 +03:00
|
|
|
isEqual(other: IdMap): boolean {
|
|
|
|
if (other.size !== this.size) return false
|
|
|
|
for (const [key, value] of this.rangeToExpr.entries()) {
|
|
|
|
const oldValue = other.rangeToExpr.get(key)
|
|
|
|
if (oldValue !== value) return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2024-01-02 18:18:11 +03:00
|
|
|
|
|
|
|
validate() {
|
|
|
|
const uniqueValues = new Set(this.rangeToExpr.values())
|
|
|
|
if (uniqueValues.size < this.rangeToExpr.size) {
|
|
|
|
console.warn(`Duplicate UUID in IdMap`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clone(): IdMap {
|
|
|
|
return new IdMap(this.entries())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debugging.
|
|
|
|
compare(other: IdMap) {
|
|
|
|
console.info(`IdMap.compare -------`)
|
|
|
|
const allKeys = new Set<string>()
|
|
|
|
for (const key of this.rangeToExpr.keys()) allKeys.add(key)
|
|
|
|
for (const key of other.rangeToExpr.keys()) allKeys.add(key)
|
|
|
|
for (const key of allKeys) {
|
|
|
|
const mine = this.rangeToExpr.get(key)
|
|
|
|
const yours = other.rangeToExpr.get(key)
|
|
|
|
if (mine !== yours) {
|
|
|
|
console.info(`IdMap.compare[${key}]: ${mine} -> ${yours}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 06:43:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const uuidRegex = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/
|
2023-10-02 12:03:47 +03:00
|
|
|
export function isUuid(x: unknown): x is Uuid {
|
2023-09-22 06:43:25 +03:00
|
|
|
return typeof x === 'string' && x.length === 36 && uuidRegex.test(x)
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:18:11 +03:00
|
|
|
export function rangeEquals(a: SourceRange, b: SourceRange): boolean {
|
2023-11-23 13:54:35 +03:00
|
|
|
return a[0] == b[0] && a[1] == b[1]
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
export function rangeIncludes(a: SourceRange, b: number): boolean {
|
|
|
|
return a[0] <= b && a[1] >= b
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rangeLength(a: SourceRange): number {
|
|
|
|
return a[1] - a[0]
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:18:11 +03:00
|
|
|
export function rangeEncloses(a: SourceRange, b: SourceRange): boolean {
|
2023-09-22 06:43:25 +03:00
|
|
|
return a[0] <= b[0] && a[1] >= b[1]
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:18:11 +03:00
|
|
|
export function rangeIntersects(a: SourceRange, b: SourceRange): boolean {
|
2023-09-22 06:43:25 +03:00
|
|
|
return a[0] <= b[1] && a[1] >= b[0]
|
|
|
|
}
|
|
|
|
|
2023-11-08 04:44:22 +03:00
|
|
|
/** Whether the given range is before the other range. */
|
2024-01-02 18:18:11 +03:00
|
|
|
export function rangeIsBefore(a: SourceRange, b: SourceRange): boolean {
|
2023-11-08 04:44:22 +03:00
|
|
|
return a[1] <= b[0]
|
|
|
|
}
|