From f232cbf7d625182174a12c1873fdef9a0eb49703 Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Sat, 4 Jan 2020 13:48:30 +0800 Subject: [PATCH] Tidy comments --- src/actions/commands/actions.ts | 4 +-- src/common/number/numericString.ts | 57 ++++++++++++++++-------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/actions/commands/actions.ts b/src/actions/commands/actions.ts index cd80ec930371..da0ea23d45f5 100644 --- a/src/actions/commands/actions.ts +++ b/src/actions/commands/actions.ts @@ -3956,13 +3956,13 @@ abstract class IncrementDecrementNumberAction extends BaseCommand { startPos: Position, endPos: Position ): Promise { - const oldWidth = endPos.character - startPos.character + 1; + const oldLength = endPos.character + 1 - startPos.character; start.value += offset; const newNum = start.toString(); const range = new vscode.Range(startPos, endPos.getRight()); - if (oldWidth === newNum.length) { + if (oldLength === newNum.length) { await TextEditor.replace(range, newNum); } else { // Can't use replace, since new number is a different width than old diff --git a/src/common/number/numericString.ts b/src/common/number/numericString.ts index 4b9ed06df1d1..47f818249b02 100644 --- a/src/common/number/numericString.ts +++ b/src/common/number/numericString.ts @@ -7,22 +7,26 @@ * || => numPreffix * |-| => num * - * Use eager parsing, leftmost matching is the best and if begins at same - * position, matching with biggest span wins. - * When begin and span are both the same, use following priority sequence: - * - * (decimal => octal => hexadecimal) + * Parsing eagerly, leftmost match wins. + * If more than one match begins at the same position, the match with biggest + * span wins. + * If more than one match have the same begin and span (This usually happens + * on octal and decimal), this priority sequence is used: + * (decimal => octal => hexadecimal) * * Example: - * core | What we got | Rather than | - * Leftmost rule: 010xff | (010)xff [octal] | 01(0xff) [hex] | - * Biggest span rule: 0xff | (0xff) [hex] | (0)xff [decimal] | - * Priority rule: 00007 | (00007) [octal] | (00009) [hex] | + * | core | What we got | Rather than | + * ------------------|--------|----------------------|---------------------| + * Leftmost rule: | 010xff | (010)xff [octal] | 01(0xff) [hex] | + * Biggest span rule:| 0xff | (0xff) [hex] | (0)xff [decimal] | + * Priority rule: | 00007 | (00007) [octal] | (00007) [decimal] | * * Side Effect: - * 0 will be parsed as (0)[octal], it's trivial - * -0xf will be parsed as (-0)xf, workaround is capture '-' in hexadecimal - * to make begin index at '-' using hexNegative boolean value + * 0 Will be parsed as (0)[octal], but it's trivial + * -0xf Will be parsed as (-0)xf rather than -(0xf), current workaround is + * capturing '-' in hexadecimal regex but not consider '-' as a part + * of the number. This is achieved by using `negative` boolean value + * in NumericString class. */ export class NumericString { radix: number; @@ -30,7 +34,7 @@ export class NumericString { numLength: number; prefix: string; suffix: string; - // Is there ancilla negative sign + // If there is a ancilla negative sign negative: boolean; // Map radix to number prefix @@ -40,17 +44,17 @@ export class NumericString { 16: '0x', }; - // Keep octal top of decimal to avoid regarding 0000007 as decimal. - // Make 000009 match deicmal - // Make 000007 match octal - // Make -0xf match hex rather than decimal '-0' + // Keep octal at the top of decimal to avoid regarding 0000007 as decimal. + // '000009' matches deicmal. + // '000007' matches octal. + // '-0xf' matches hex rather than decimal '-0' private static matchings: { regex: RegExp; radix: number }[] = [ { regex: /-?0[0-7]+/, radix: 8 }, { regex: /-?\d+/, radix: 10 }, { regex: /-?0x[\da-fA-F]+/, radix: 16 }, ]; - // Return parse result and suffixOffset + // Return parse result and offset of suffix public static parse(input: string): [NumericString, number] | undefined { // Find core numeric part of input let coreBegin = -1; @@ -87,15 +91,16 @@ export class NumericString { // 0x00ff: numLength = 4 // 077: numLength = 2 // -0999: numLength = 3 - // The numLength is only useful for non-decimal. Decimal with leading - // zero will be trimmed. If value is negative, remove the negative - // sign's length. + // zero will be trimmed in `toString()`. If value is negative, remove + // the negative sign's length. const numLength = coreLength - NumericString.numPrefix[coreRadix].length - (value < 0 ? 1 : 0); // According to original vim's behavior, for 'hexadecimal' and 'octal', // leading '-' *should* be captured and preseved but *should not* be - // regarded as part of number. + // regarded as part of number, which means with , `-0xf` turns into + // `-0x10`. So for hex and octal, we make the value absolute and set + // the ancilla minus flag to true. let negative = false; if (coreRadix !== 10 && value < 0) { value = -value; @@ -133,17 +138,17 @@ export class NumericString { // Gen num part const absValue = Math.abs(this.value); let num = absValue.toString(this.radix); - // According to original vim's behavior, numLength of decimal *should not* - // be preserved. + // numLength of decimal *should not* be preserved. if (this.radix !== 10) { const diff = this.numLength - num.length; if (diff > 0) { - // Preserve num length if narrower. + // Preserve num length if it's narrower. num = '0'.repeat(diff) + num; } } const sign = this.negative || this.value < 0 ? '-' : ''; - return this.prefix + sign + NumericString.numPrefix[this.radix] + num + this.suffix; + const core = sign + NumericString.numPrefix[this.radix] + num; + return this.prefix + core + this.suffix; } }