-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce SourceDocument for text edit support (#8957)
Introduce `SourceDocument`, a reactive source code representation that can be synced from a `MutableModule`. `SourceDocument` replaces various logic for tracking source code and spans--most importantly, `ReactiveModule`. There is no longer any reactively-tracked `Module`, per-se: Changes to the `MutableModule` attached to the synchronized `ydoc` are pushed as Y.Js events to the `SourceDocument` and `GraphDb`, which are reactively tracked. This avoids a problem in the upcoming text-synchronization (next PR) that was caused by a reactive back channel bypassing the `GraphDb` and resulting in observation of inconsistent states. Stacked on #8956. Part of #8238.
- Loading branch information
Showing
10 changed files
with
137 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { print, type AstId, type Module, type ModuleUpdate } from '.' | ||
import { rangeEquals, sourceRangeFromKey, type SourceRange } from '../yjsModel' | ||
|
||
/** Provides a view of the text representation of a module, | ||
* and information about the correspondence between the text and the ASTs, | ||
* that can be kept up-to-date by applying AST changes. | ||
*/ | ||
export class SourceDocument { | ||
private text_: string | ||
private readonly spans: Map<AstId, SourceRange> | ||
|
||
private constructor(text: string, spans: Map<AstId, SourceRange>) { | ||
this.text_ = text | ||
this.spans = spans | ||
} | ||
|
||
static Empty() { | ||
return new this('', new Map()) | ||
} | ||
|
||
clear() { | ||
if (this.text_ !== '') this.text_ = '' | ||
if (this.spans.size !== 0) this.spans.clear() | ||
} | ||
|
||
applyUpdate(module: Module, update: ModuleUpdate) { | ||
for (const id of update.nodesDeleted) this.spans.delete(id) | ||
const root = module.root() | ||
if (!root) return | ||
const printed = print(root) | ||
for (const [key, nodes] of printed.info.nodes) { | ||
const range = sourceRangeFromKey(key) | ||
for (const node of nodes) { | ||
const oldSpan = this.spans.get(node.id) | ||
if (!oldSpan || !rangeEquals(range, oldSpan)) this.spans.set(node.id, range) | ||
} | ||
} | ||
if (printed.code !== this.text_) this.text_ = printed.code | ||
} | ||
|
||
get text(): string { | ||
return this.text_ | ||
} | ||
|
||
getSpan(id: AstId): SourceRange | undefined { | ||
return this.spans.get(id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.