Skip to content

Commit

Permalink
Some more base types
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 9, 2024
1 parent a6d4d8c commit 4366076
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
28 changes: 28 additions & 0 deletions openrewrite/src/core/markers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {random_id, UUID} from "./tree";

export interface Marker {
}

export class Markers {
public static readonly EMPTY: Markers = new Markers(random_id(), []);

private readonly _id: string;
private readonly _markers: Marker[] = [];

constructor(id: UUID, markers: Marker[]) {
this._id = id;
this._markers = markers;
}

public id(): string {
return this._id;
}

public markers(): Marker[] {
return this._markers;
}

public findFirst<T extends Marker>(markerType: new () => T): T | null {
return this._markers.find((marker) => marker instanceof markerType) as T || null;
}
}
31 changes: 22 additions & 9 deletions openrewrite/src/core/tree.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { v4 as uuidv4 } from 'uuid';
import {v4 as uuidv4} from 'uuid';

type UUID = string;
export type UUID = string;

export const random_id = (): UUID => {
return uuidv4();
}

class CompilationUnit {
constructor(
public readonly id: UUID
) {
export class Cursor {
private readonly _parent: Cursor | null;
private readonly _value: Object;
private _messages: Map<string, Object>;

constructor(parent: Cursor | null, value: Object) {
this._parent = parent;
this._value = value;
this._messages = new Map<string, Object>();
}

withId(newId: UUID): CompilationUnit {
return newId == this.id ? this : new CompilationUnit(newId);
public parent(): Cursor | null {
return this._parent;
}
}

public value<T>(): T {
return this._value as T;
}

public fork(): Cursor {
return new Cursor(this._parent === null ? null : this._parent.fork(), this.value);
}
}

0 comments on commit 4366076

Please sign in to comment.