Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1684: Fixed gq spacing issues #1686

Merged
merged 11 commits into from May 19, 2017
38 changes: 23 additions & 15 deletions src/actions/commands/actions.ts
Expand Up @@ -2531,6 +2531,7 @@ class ActionVisualReflowParagraph extends BaseCommand {
let chunksToReflow: {
commentType: CommentType;
content: string;
indentLevelAfterComment: number;
}[] = [];

for (const line of s.split("\n")) {
Expand Down Expand Up @@ -2568,10 +2569,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 @@ -2603,8 +2609,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 @@ -2625,14 +2632,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 @@ -2648,25 +2660,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
18 changes: 18 additions & 0 deletions test/mode/modeNormal.test.ts
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