Skip to content

Commit

Permalink
fixed leading tabs when copying multiple selections (#1562)
Browse files Browse the repository at this point in the history
* fixed leading tabs when copying multiple selections

* add tabs in the end as well

* remove space from tab
  • Loading branch information
Aditya Bist committed Jan 14, 2020
1 parent 1229b3b commit 7ff7fe4
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/controllers/queryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ export default class QueryRunner {

// Go through all rows and get selections for them
let allRowIds = rowIdToRowMap.keys();
const endColumns = this.getSelectionEndColumns(rowIdToRowMap, rowIdToSelectionMap);
const firstColumn = endColumns[0];
const lastColumn = endColumns[1];
for (let rowId of allRowIds) {
let row = rowIdToRowMap.get(rowId);
const rowSelections = rowIdToSelectionMap.get(rowId);
Expand All @@ -428,20 +431,25 @@ export default class QueryRunner {
return ((a.fromCell < b.fromCell) ? -1 : (a.fromCell > b.fromCell) ? 1 : 0);
});

// start copy paste from left-most column
const firstColumn = rowSelections[0].fromCell;

for (let i = 0; i < rowSelections.length; i++) {
let rowSelection = rowSelections[i];

// Add tabs starting from the first column of the selection
for (let j = firstColumn; j < rowSelection.fromCell; j++) {
copyString += ' \t';
copyString += '\t';
}
let cellObjects = row.slice(rowSelection.fromCell, (rowSelection.toCell + 1));

// Remove newlines if requested
let cells = this.shouldRemoveNewLines()
? cellObjects.map(x => this.removeNewLines(x.displayValue))
: cellObjects.map(x => x.displayValue);
copyString += cells.join('\t');

// Add tabs until the end column of the selection
for (let k = rowSelection.toCell; k < lastColumn; k++) {
copyString += '\t';
}
}
copyString += os.EOL;
}
Expand Down Expand Up @@ -518,6 +526,27 @@ export default class QueryRunner {
}
}

/**
* Gets the first and last column of a selection: [first, last]
*/
private getSelectionEndColumns(rowIdToRowMap: Map<number, DbCellValue[]>, rowIdToSelectionMap: Map<number, ISlickRange[]>): number[] {
let allRowIds = rowIdToRowMap.keys();
let firstColumn = -1;
let lastColumn = -1;
for (let rowId of allRowIds) {
const rowSelections = rowIdToSelectionMap.get(rowId);
for (let i = 0; i < rowSelections.length; i++) {
if (firstColumn === -1 || rowSelections[i].fromCell < firstColumn) {
firstColumn = rowSelections[i].fromCell;
}
if (lastColumn === -1 || rowSelections[i].toCell > lastColumn) {
lastColumn = rowSelections[i].toCell;
}
}
}
return [firstColumn, lastColumn];
}

/**
* Sets a selection range in the editor for this query
* @param selection The selection range to select
Expand Down

0 comments on commit 7ff7fe4

Please sign in to comment.