Skip to content

Commit

Permalink
Merge branch 'master' into 1715
Browse files Browse the repository at this point in the history
  • Loading branch information
Chillee authored May 20, 2017
2 parents 3a2064f + 24426b6 commit 74a1075
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 41 deletions.
10 changes: 0 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,6 @@
"command": "extension.vim_ctrl+b",
"when": "editorTextFocus && vim.active && vim.use<C-b> && vim.mode != 'Insert' && !inDebugRepl"
},
{
"key": "ctrl+j",
"command": "extension.vim_ctrl+j",
"when": "editorTextFocus && vim.active && vim.use<C-j> && vim.mode != 'Insert' && !inDebugRepl"
},
{
"key": "ctrl+k",
"command": "extension.vim_ctrl+k",
"when": "editorTextFocus && vim.active && vim.use<C-k> && vim.mode != 'Insert' && !inDebugRepl"
},
{
"key": "ctrl+h",
"command": "extension.vim_ctrl+h",
Expand Down
58 changes: 36 additions & 22 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,7 @@ class ActionVisualReflowParagraph extends BaseCommand {
let chunksToReflow: {
commentType: CommentType;
content: string;
indentLevelAfterComment: number;
}[] = [];

for (const line of s.split("\n")) {
Expand Down Expand Up @@ -2574,10 +2575,15 @@ class ActionVisualReflowParagraph extends BaseCommand {

// Did they start a new comment type?
if (!lastChunk || commentType.start !== lastChunk.commentType.start) {
chunksToReflow.push({
let chunk = {
commentType,
content: `${ trimmedLine.substr(commentType.start.length).trim() }`
});
content: `${ trimmedLine.substr(commentType.start.length).trim() }`,
indentLevelAfterComment: 0
};
if (commentType.singleLine) {
chunk.indentLevelAfterComment = trimmedLine.substr(commentType.start.length).length - chunk.content.length;
}
chunksToReflow.push(chunk);

continue;
}
Expand Down Expand Up @@ -2609,8 +2615,9 @@ class ActionVisualReflowParagraph extends BaseCommand {
// Reflow each chunk.
let result: string[] = [];

for (const { commentType, content } of chunksToReflow) {
for (const { commentType, content, indentLevelAfterComment } of chunksToReflow) {
let lines: string[];
const indentAfterComment = Array(indentLevelAfterComment + 1).join(" ");

if (commentType.singleLine) {
lines = [``];
Expand All @@ -2631,14 +2638,19 @@ class ActionVisualReflowParagraph extends BaseCommand {
}

// Add word by word, wrapping when necessary.

for (const word of line.split(/\s+/)) {
const words = line.split(/\s+/);
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (word === "") { continue; }

if (lines[lines.length - 1].length + word.length + 1 < maximumLineLength) {
lines[lines.length - 1] += ` ${ word }`;
if (i) {
lines[lines.length - 1] += ` ${ word }`;
} else {
lines[lines.length - 1] += `${ word }`;
}
} else {
lines.push(` ${ word }`);
lines.push(`${ word }`);
}
}
}
Expand All @@ -2654,25 +2666,21 @@ class ActionVisualReflowParagraph extends BaseCommand {

for (let i = 0; i < lines.length; i++) {
if (commentType.singleLine) {
lines[i] = `${ indent }${ commentType.start }${ lines[i] }`;
lines[i] = `${ indent }${ commentType.start }${ indentAfterComment }${ lines[i] }`;
} else {
if (i === 0) {
lines[i] = `${ indent }${ commentType.start }${ lines[i] }`;
lines[i] = `${ indent }${ commentType.start } ${ lines[i] }`;
} else if (i === lines.length - 1) {
lines[i] = `${ indent } ${ commentType.final }`;
} else {
lines[i] = `${ indent } ${ commentType.inner }${ lines[i] }`;
lines[i] = `${ indent } ${ commentType.inner } ${ lines[i] }`;
}
}
}

result = result.concat(lines);
}

// Remove extra first space if it exists.
if (result[0][0] === " ") {
result[0] = result[0].slice(1);
}
// Gather up multiple empty lines into single empty lines.
return result.join("\n");
}
Expand Down Expand Up @@ -3182,13 +3190,19 @@ abstract class IncrementDecrementNumberAction extends BaseCommand {
word = text[start.character] + word;
}
// Strict number parsing so "1a" doesn't silently get converted to "1"
const num = NumericString.parse(word);

if (num !== null) {
vimState.cursorPosition = await this.replaceNum(num, this.offset * (vimState.recordedState.count || 1), start, end);
vimState.cursorPosition = vimState.cursorPosition.getLeftByCount(num.suffix.length);
return vimState;
}
do {
const num = NumericString.parse(word);
if (num !== null && position.character < start.character + num.prefix.length + num.value.toString().length) {
vimState.cursorPosition = await this.replaceNum(num, this.offset * (vimState.recordedState.count || 1), start, end);
vimState.cursorPosition = vimState.cursorPosition.getLeftByCount(num.suffix.length);
return vimState;
} else if (num !== null) {
word = word.slice(num.prefix.length + num.value.toString().length);
start = new Position(start.line, start.character + num.prefix.length + num.value.toString().length);
} else {
break;
}
} while (true);
}
// No usable numbers, return the original position
return vimState;
Expand Down
17 changes: 8 additions & 9 deletions src/common/number/numericString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export class NumericString {
suffix: string;

private static matchings: { regex: RegExp, base: number, prefix: string }[] = [
{ regex: /^([-+])?0([0-7]+)$/, base: 8, prefix: "0" },
{ regex: /^([-+])?(\d+)$/, base: 10, prefix: "" },
{ regex: /^([-+])?0x([\da-fA-F]+)$/, base: 16, prefix: "0x" },
{ regex: /^([-+])?0([0-7]+)$/g, base: 8, prefix: "0" },
{ regex: /^([-+])?(\d+)$/g, base: 10, prefix: "" },
{ regex: /^([-+])?0x([\da-fA-F]+)$/g, base: 16, prefix: "0x" },
{ regex: /\d/, base: 10, prefix: "" }
];

Expand All @@ -20,14 +20,13 @@ export class NumericString {

// Regex to determine if this number has letters around it,
// if it doesn't then that is easy and no prefix or suffix is needed
let findNondigits = /[^\d-+]+/i;
let findNondigits = /[^\d-+]+/g;

// Regex to find any leading characters before the number
let findPrefix = /^[^\d-+]+(?=[0-9]+)/i;
let findPrefix = /^[^\d-+]+(?=[0-9]+)/g;

// Regex to find any trailing characters after the number
let findSuffix = /[^\d]*$/i;

let findSuffix = /[^\d]*[\d]*(.*)/g;
let newPrefix = prefix;
let newSuffix = "";
let newNum = input;
Expand All @@ -44,11 +43,11 @@ export class NumericString {

// Find the suffix if it exists
if (suffixFound !== null) {
newSuffix = suffixFound.toString();
newSuffix = suffixFound[1].toString();
}

// Obtain just the number with no extra letters
newNum = input.replace(/[^\d-+]+/ig, '');
newNum = newNum.slice(newPrefix.length, newNum.length - newSuffix.length);
}

return new NumericString(parseInt(newNum, base), base, newPrefix, newSuffix);
Expand Down
32 changes: 32 additions & 0 deletions test/mode/modeNormal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,24 @@ suite("Mode Normal", () => {
end: ['testtest', 'testtest', 'testtes|t']
});

// These tests run poorly on Travis for w.e. reason
// newTest({
// title: "gq handles spaces after single line comments correctly",
// start: ['// We choose to write a vim extension, not because it is easy, but because it is hard|.'],
// keysPressed: 'Vgq',
// end: [ '// We choose to write a vim extension, not because it is easy, but because it is',
// '|// hard.'],
// });

// newTest({
// title: "gq handles spaces before single line comments correctly",
// start: [' // We choose to write a vim extension, not because it is easy, but because it is hard|.'],
// keysPressed: 'Vgq',
// end: [ ' // We choose to write a vim extension, not because it is easy, but because',
// '| // it is hard.']
// });


newTest({
title: "Can handle space",
start: ['|abc', 'def'],
Expand Down Expand Up @@ -1410,6 +1428,20 @@ suite("Mode Normal", () => {
end: ["id: 1|,", "someOtherId: 1"]
});

newTest({
title: "can <C-a> on word with multiple numbers (incrementing first number)",
start: ["f|oo1bar2"],
keysPressed: "<C-a>",
end: ["foo|2bar2"]
});

newTest({
title: "can <C-a> on word with multiple numbers (incrementing second number)",
start: ["foo1|bar2"],
keysPressed: "<C-a>",
end: ["foo1bar|3"]
});

newTest({
title: "can do Y",
start: ["|blah blah"],
Expand Down

0 comments on commit 74a1075

Please sign in to comment.