Merge branch 'master' into watchImprovements

This commit is contained in:
Sheetal Nandi
2017-10-02 12:25:22 -07:00
22 changed files with 125 additions and 153 deletions

View File

@@ -6,38 +6,20 @@ namespace ts.formatting {
public map: RulesBucket[];
public mapRowLength: number;
constructor() {
this.map = [];
this.mapRowLength = 0;
}
static create(rules: Rule[]): RulesMap {
const result = new RulesMap();
result.Initialize(rules);
return result;
}
public Initialize(rules: Rule[]) {
constructor(rules: ReadonlyArray<Rule>) {
this.mapRowLength = SyntaxKind.LastToken + 1;
this.map = <any>new Array(this.mapRowLength * this.mapRowLength); // new Array<RulesBucket>(this.mapRowLength * this.mapRowLength);
this.map = new Array<RulesBucket>(this.mapRowLength * this.mapRowLength);
// This array is used only during construction of the rulesbucket in the map
const rulesBucketConstructionStateList: RulesBucketConstructionState[] = <any>new Array(this.map.length); // new Array<RulesBucketConstructionState>(this.map.length);
this.FillRules(rules, rulesBucketConstructionStateList);
return this.map;
}
public FillRules(rules: Rule[], rulesBucketConstructionStateList: RulesBucketConstructionState[]): void {
rules.forEach((rule) => {
const rulesBucketConstructionStateList: RulesBucketConstructionState[] = new Array<RulesBucketConstructionState>(this.map.length);
for (const rule of rules) {
this.FillRule(rule, rulesBucketConstructionStateList);
});
}
}
private GetRuleBucketIndex(row: number, column: number): number {
Debug.assert(row <= SyntaxKind.LastKeyword && column <= SyntaxKind.LastKeyword, "Must compute formatting context from tokens");
const rulesBucketIndex = (row * this.mapRowLength) + column;
return rulesBucketIndex;
return (row * this.mapRowLength) + column;
}
private FillRule(rule: Rule, rulesBucketConstructionStateList: RulesBucketConstructionState[]): void {
@@ -57,7 +39,7 @@ namespace ts.formatting {
});
}
public GetRule(context: FormattingContext): Rule {
public GetRule(context: FormattingContext): Rule | undefined {
const bucketIndex = this.GetRuleBucketIndex(context.currentTokenSpan.kind, context.nextTokenSpan.kind);
const bucket = this.map[bucketIndex];
if (bucket) {
@@ -74,7 +56,7 @@ namespace ts.formatting {
const MaskBitSize = 5;
const Mask = 0x1f;
export enum RulesPosition {
enum RulesPosition {
IgnoreRulesSpecific = 0,
IgnoreRulesAny = MaskBitSize * 1,
ContextRulesSpecific = MaskBitSize * 2,

View File

@@ -10,7 +10,7 @@ namespace ts.formatting {
constructor() {
this.globalRules = new Rules();
const activeRules = this.globalRules.HighPriorityCommonRules.concat(this.globalRules.UserConfigurableRules).concat(this.globalRules.LowPriorityCommonRules);
this.rulesMap = RulesMap.create(activeRules);
this.rulesMap = new RulesMap(activeRules);
}
public getRulesMap() {

View File

@@ -201,9 +201,9 @@ namespace ts.refactor.extractSymbol {
// Walk up starting from the the start position until we find a non-SourceFile node that subsumes the selected span.
// This may fail (e.g. you select two statements in the root of a source file)
let start = getParentNodeInSpan(getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false), sourceFile, span);
const start = getParentNodeInSpan(getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false), sourceFile, span);
// Do the same for the ending position
let end = getParentNodeInSpan(findTokenOnLeftOfPosition(sourceFile, textSpanEnd(span)), sourceFile, span);
const end = getParentNodeInSpan(findTokenOnLeftOfPosition(sourceFile, textSpanEnd(span)), sourceFile, span);
const declarations: Symbol[] = [];
@@ -217,31 +217,10 @@ namespace ts.refactor.extractSymbol {
}
if (start.parent !== end.parent) {
// handle cases like 1 + [2 + 3] + 4
// user selection is marked with [].
// in this case 2 + 3 does not belong to the same tree node
// instead the shape of the tree looks like this:
// +
// / \
// + 4
// / \
// + 3
// / \
// 1 2
// in this case there is no such one node that covers ends of selection and is located inside the selection
// to handle this we check if both start and end of the selection belong to some binary operation
// and start node is parented by the parent of the end node
// if this is the case - expand the selection to the entire parent of end node (in this case it will be [1 + 2 + 3] + 4)
const startParent = skipParentheses(start.parent);
const endParent = skipParentheses(end.parent);
if (isBinaryExpression(startParent) && isBinaryExpression(endParent) && isNodeDescendantOf(startParent, endParent)) {
start = end = endParent;
}
else {
// start and end nodes belong to different subtrees
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.CannotExtractRange)] };
}
// start and end nodes belong to different subtrees
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.CannotExtractRange)] };
}
if (start !== end) {
// start and end should be statements and parent should be either block or a source file
if (!isBlockLike(start.parent)) {