Skip to content

Commit

Permalink
Tidy comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm0 committed Jan 4, 2020
1 parent cd58b1e commit f232cbf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3956,13 +3956,13 @@ abstract class IncrementDecrementNumberAction extends BaseCommand {
startPos: Position,
endPos: Position
): Promise<Position> {
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
Expand Down
57 changes: 31 additions & 26 deletions src/common/number/numericString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,34 @@
* || => 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;
value: number;
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
Expand All @@ -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;
Expand Down Expand Up @@ -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 <C-a>, `-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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit f232cbf

Please sign in to comment.