Skip to content

Commit

Permalink
Columns and rows moving (#28)
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
rpeshkov authored Apr 14, 2018
1 parent bfcf033 commit 79a77d5
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@
{
"command": "text-tables.clearCell",
"title": "Text Tables: Clear cell"
},
{
"command": "text-tables.moveRowDown",
"title": "Move row down",
"category": "Text Tables"
},
{
"command": "text-tables.moveRowUp",
"title": "Move row up",
"category": "Text Tables"
},
{
"command": "text-tables.moveColRight",
"title": "Move column right",
"category": "Text Tables"
},
{
"command": "text-tables.moveColLeft",
"title": "Move column left",
"category": "Text Tables"
}
],
"configuration": {
Expand Down Expand Up @@ -115,6 +135,26 @@
"command": "text-tables.gotoPreviousCell",
"key": "shift+tab",
"when": "tableMode"
},
{
"command": "text-tables.moveRowUp",
"key": "alt+up",
"when": "tableMode"
},
{
"command": "text-tables.moveRowDown",
"key": "alt+down",
"when": "tableMode"
},
{
"command": "text-tables.moveColRight",
"key": "alt+right",
"when": "tableMode"
},
{
"command": "text-tables.moveColLeft",
"key": "alt+left",
"when": "tableMode"
}
]
},
Expand Down
137 changes: 137 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,125 @@ export function activate(ctx: vscode.ExtensionContext) {
}
});

ctx.subscriptions.push(vscode.commands.registerCommand('text-tables.moveRowDown', () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const table = formatAndGetTableUnderCursor(editor);
if (table) {
const rowNum = editor.selection.end.line - table.startLine;
if (rowNum >= table.rows.length - 1) {
vscode.window.showWarningMessage('Cannot move row further');
return;
}
vscode.commands.executeCommand('editor.action.moveLinesDownAction');
}
}
}));

ctx.subscriptions.push(vscode.commands.registerCommand('text-tables.moveRowUp', () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const table = formatAndGetTableUnderCursor(editor);
if (table) {
const rowNum = editor.selection.start.line - table.startLine;
if (rowNum <= 0) {
vscode.window.showWarningMessage('Cannot move row further');
return;
}
vscode.commands.executeCommand('editor.action.moveLinesUpAction');
}
}
}));

// Columns move
ctx.subscriptions.push(vscode.commands.registerCommand('text-tables.moveColRight', () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const tableRange = locator.locate(editor.document, editor.selection.start.line);

if (isUndefined(tableRange)) {
return;
}
const selectedText = editor.document.getText(tableRange);
const table = parser.parse(selectedText);

if (isUndefined(table)) {
return;
}

table.startLine = tableRange.start.line;

const rowCol = rowColFromPosition(table, editor.selection.start);
if (rowCol.col < 0) {
vscode.window.showWarningMessage('Not in table data field');
return;
}

if (rowCol.col >= table.cols.length - 1 ) {
vscode.window.showWarningMessage('Cannot move column further right');
return;
}

[table.cols[rowCol.col], table.cols[rowCol.col + 1]] = [table.cols[rowCol.col + 1], table.cols[rowCol.col]];

table.rows.forEach((_, i) => {
const v1 = table.getAt(i, rowCol.col);
const v2 = table.getAt(i, rowCol.col + 1);
table.setAt(i, rowCol.col + 1, v1);
table.setAt(i, rowCol.col, v2);
});

const newText = stringifier.stringify(table);
editor
.edit(b => b.replace(tableRange, newText))
.then(() => vscode.commands.executeCommand('text-tables.gotoNextCell'));
}
}));

ctx.subscriptions.push(vscode.commands.registerCommand('text-tables.moveColLeft', () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const tableRange = locator.locate(editor.document, editor.selection.start.line);

if (isUndefined(tableRange)) {
return;
}
const selectedText = editor.document.getText(tableRange);
const table = parser.parse(selectedText);

if (isUndefined(table)) {
return;
}

table.startLine = tableRange.start.line;

const rowCol = rowColFromPosition(table, editor.selection.start);
if (rowCol.col < 0) {
vscode.window.showWarningMessage('Not in table data field');
return;
}

if (rowCol.col === 0) {
vscode.window.showWarningMessage('Cannot move column further left');
return;
}

[table.cols[rowCol.col], table.cols[rowCol.col - 1]] = [table.cols[rowCol.col - 1], table.cols[rowCol.col]];

table.rows.forEach((_, i) => {
const v1 = table.getAt(i, rowCol.col);
const v2 = table.getAt(i, rowCol.col - 1);
table.setAt(i, rowCol.col - 1, v1);
table.setAt(i, rowCol.col, v2);
});

const newText = stringifier.stringify(table);
editor
.edit(b => b.replace(tableRange, newText))
.then(() => vscode.commands.executeCommand('text-tables.gotoPreviousCell'));
}
}));

// Command for manually enabling extension
ctx.subscriptions.push(vscode.commands.registerCommand('text-tables.enable', () => {
vscode.window.showInformationMessage('Text tables enabled!');
Expand Down Expand Up @@ -197,6 +316,24 @@ export function activate(ctx: vscode.ExtensionContext) {
export function deactivate() {
}

function rowColFromPosition(table: Table, position: vscode.Position): { row: number, col: number } {
const result = { row: -1, col: -1 };

result.row = position.line - table.startLine;
let counter = 1;
for (let i = 0; i < table.cols.length; ++i) {
const col = table.cols[i];
if (position.character >= counter && position.character < counter + col.width + 3) {
result.col = i;
break;
}

counter += col.width + 3;
}

return result;
}

/**
* Try to find table in provided editor in current cursor position.
* If table was found, formats the table and returns it. Otherwise - returns undefined.
Expand Down

0 comments on commit 79a77d5

Please sign in to comment.