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

Fix gq to handle tab indentation #4050

Merged
merged 2 commits into from
Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,22 +841,27 @@ class ActionVisualReflowParagraph extends BaseOperator {
{ singleLine: true, start: '' },
];

public getIndentationLevel(s: string): number {
public getIndentation(s: string): string {
// Use the indentation of the first non-whitespace line, if any such line is
// selected.
for (const line of s.split('\n')) {
const result = line.match(/^\s+/g);
const indentLevel = result ? result[0].length : 0;
const indent = result ? result[0] : '';

if (indentLevel !== line.length) {
return indentLevel;
if (indent !== line) {
return indent;
}
}

return 0;
return '';
}

public reflowParagraph(s: string, indentLevel: number): string {
public reflowParagraph(s: string, indent: string): string {
let indentLevel = 0;
for (const char of indent) {
indentLevel += char === '\t' ? configuration.tabstop : 1;
}
const maximumLineLength = configuration.textwidth - indentLevel - 2;
const indent = Array(indentLevel + 1).join(' ');

// Chunk the lines by commenting style.

Expand Down Expand Up @@ -1033,9 +1038,9 @@ class ActionVisualReflowParagraph extends BaseOperator {
end = Position.LaterOf(start, end);

let textToReflow = TextEditor.getText(new vscode.Range(start, end));
let indentLevel = this.getIndentationLevel(textToReflow);
let indent = this.getIndentation(textToReflow);

textToReflow = this.reflowParagraph(textToReflow, indentLevel);
textToReflow = this.reflowParagraph(textToReflow, indent);

vimState.recordedState.transformations.push({
type: 'replaceText',
Expand Down
49 changes: 34 additions & 15 deletions test/mode/modeNormal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,22 +1330,41 @@ 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 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: '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: 'gq handles tabs before single line comments correctly',
start: [
'\t\t// We choose to write a vim extension, not because it is easy, but because it is hard|.',
],
keysPressed: 'Vgq',
end: [
'\t\t// We choose to write a vim extension, not because it is easy, but',
'|\t\t// because it is hard.',
],
});

newTest({
title: 'Can handle space',
Expand Down