-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(navigation): #140 - Forget blocks.
- Loading branch information
Showing
9 changed files
with
287 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as ts from "typescript"; | ||
import {Node} from "./../compiler"; | ||
import {KeyValueCache, createHashSet, HashSet} from "./../utils"; | ||
|
||
/** | ||
* Extension of KeyValueCache that allows for "forget points." | ||
*/ | ||
export class ForgetfulNodeCache extends KeyValueCache<ts.Node, Node> { | ||
private readonly forgetStack: HashSet<Node>[] = []; | ||
|
||
getOrCreate<TCreate extends Node>(key: ts.Node, createFunc: () => TCreate) { | ||
return super.getOrCreate(key, () => { | ||
const node = createFunc(); | ||
if (this.forgetStack.length > 0) | ||
this.forgetStack[this.forgetStack.length - 1].add(node); | ||
return node; | ||
}); | ||
} | ||
|
||
setForgetPoint() { | ||
this.forgetStack.push(createHashSet()); | ||
} | ||
|
||
forgetLastPoint() { | ||
const nodes = this.forgetStack.pop(); | ||
if (nodes != null) | ||
this.forgetNodes(nodes.values()); | ||
} | ||
|
||
rememberNode(node: Node) { | ||
let wasInForgetStack = false; | ||
for (const stackItem of this.forgetStack) { | ||
if (stackItem.delete(node)) { | ||
wasInForgetStack = true; | ||
break; | ||
} | ||
} | ||
|
||
if (wasInForgetStack) | ||
this.rememberParentOfNode(node); | ||
|
||
return wasInForgetStack; | ||
} | ||
|
||
private rememberParentOfNode(node: Node) { | ||
const parent = node.getParentSyntaxList() || node.getParent(); | ||
if (parent != null) | ||
this.rememberNode(parent); | ||
} | ||
|
||
private forgetNodes(nodes: IterableIterator<Node>) { | ||
for (const node of nodes) { | ||
if (node.getKind() === ts.SyntaxKind.SourceFile) | ||
continue; | ||
node.forgetOnlyThis(); | ||
} | ||
} | ||
} |
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,45 @@ | ||
import * as ts from "typescript"; | ||
import {expect} from "chai"; | ||
import {ArrayUtils} from "./../../utils"; | ||
import {getInfoFromText} from "./../compiler/testHelpers"; | ||
import {ForgetfulNodeCache} from "./../../factories/ForgetfulNodeCache"; | ||
|
||
describe(nameof(ForgetfulNodeCache), () => { | ||
it("should forget nodes created after a forget point", () => { | ||
const {firstChild} = getInfoFromText("class MyClass { prop: string; }"); | ||
const cache = new ForgetfulNodeCache(); | ||
cache.getOrCreate(firstChild.compilerNode, () => firstChild); | ||
|
||
cache.setForgetPoint(); | ||
const classKeyword = firstChild.getFirstChildByKindOrThrow(ts.SyntaxKind.ClassKeyword); | ||
cache.getOrCreate(classKeyword.compilerNode, () => classKeyword); | ||
|
||
cache.setForgetPoint(); | ||
const openBraceToken = firstChild.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenBraceToken); | ||
cache.getOrCreate(openBraceToken.compilerNode, () => openBraceToken); | ||
|
||
cache.forgetLastPoint(); | ||
expect(openBraceToken.wasForgotten()).to.be.true; | ||
expect(classKeyword.wasForgotten()).to.be.false; | ||
expect(firstChild.wasForgotten()).to.be.false; | ||
|
||
cache.setForgetPoint(); | ||
const closeBraceToken = firstChild.getFirstChildByKindOrThrow(ts.SyntaxKind.CloseBraceToken); | ||
cache.getOrCreate(closeBraceToken.compilerNode, () => closeBraceToken); | ||
|
||
cache.forgetLastPoint(); | ||
expect(closeBraceToken.wasForgotten()).to.be.true; | ||
expect(firstChild.wasForgotten()).to.be.false; | ||
const syntaxList = firstChild.getChildSyntaxListOrThrow(); | ||
const property = syntaxList.getChildren()[0]; | ||
cache.rememberNode(property); | ||
|
||
cache.forgetLastPoint(); | ||
expect(openBraceToken.wasForgotten()).to.be.true; | ||
expect(closeBraceToken.wasForgotten()).to.be.true; | ||
expect(classKeyword.wasForgotten()).to.be.true; | ||
expect(property.wasForgotten()).to.be.false; // it was remembered | ||
expect(syntaxList.wasForgotten()).to.be.false; // it should remember the parents | ||
expect(firstChild.wasForgotten()).to.be.false; | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import {expect} from "chai"; | ||
import {Es5HashSet} from "./../../utils"; | ||
import {Es5HashSet, ArrayUtils} from "./../../utils"; | ||
|
||
describe(nameof(Es5HashSet), () => { | ||
it("should add values to the hash set and say they exist when they do", () => { | ||
const hashSet = new Es5HashSet<string>(); | ||
expect(ArrayUtils.from(hashSet.values())).to.deep.equal([]); | ||
expect(hashSet.has("")).to.be.false; | ||
hashSet.add(""); | ||
expect(ArrayUtils.from(hashSet.values())).to.deep.equal([""]); | ||
expect(hashSet.has("")).to.be.true; | ||
expect(hashSet.delete("")).to.be.true; | ||
expect(hashSet.delete("")).to.be.false; | ||
expect(ArrayUtils.from(hashSet.values())).to.deep.equal([]); | ||
expect(hashSet.has("")).to.be.false; | ||
}); | ||
}); |
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