Skip to content

Commit

Permalink
Fixed CSV editing issues with regex separators.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Juback committed Mar 30, 2022
1 parent 68b6560 commit c30fbb9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 4.2.54 (March 29, 2022)
Fixed problems with CSV editing when the `csv-preview.separator` configuration setting was set to a regular expression instead of a single character.

## 4.2.53 (March 9, 2022)
Updated Wijmo license to remove nag screen.

Expand Down
7 changes: 7 additions & 0 deletions out/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ function initPage() {
autoSizeVisibleRows(flex, false);
});

flex.cellEditEnding.addHandler(function(s, e) {
var active = s.activeEditor.value;
var dataItem = s.rows[e.row].dataItem;
var binding = s.columns[e.col].binding;
dataItem[binding] = active;
});

flex.cellEditEnded.addHandler(function(s, e) {
var oldValue = e.data;
var newValue = s.getCellData(e.row, e.col);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "gc-excelviewer",
"displayName": "Excel Viewer",
"description": "Edit Excel spreadsheets and CSV files in Visual Studio Code and VS Code for the Web.",
"version": "4.2.53",
"version": "4.2.54",
"icon": "img/gc-excelviewer.png",
"publisher": "GrapeCity",
"license": "SEE LICENSE IN LICENSE.txt",
Expand Down
19 changes: 14 additions & 5 deletions src/csvDocumentView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,30 @@ export default class CsvDocumentView extends BaseDocumentView {
}, this);
}

private singleSeparator(sep: string) {
let skipFirst = sep.startsWith("[") || sep.startsWith(`\\`);
return skipFirst ? sep.slice(1, 2) : sep;
}

private endOfLine() {
return this._document.eol === EndOfLine.CRLF ? "\r\n" : "\n"
}

private appendRow(columns: number) {
let options = this.getOptions();
let sep = options.separator;
let empty = sep.repeat(columns - 1);
let empty = this.singleSeparator(sep).repeat(columns - 1);
let row = this._document.lineCount - 1;
let line = this._document.lineAt(row);

if (!line.isEmptyOrWhitespace) {
this.beginEdit();
this._wsEdit.insert(this.uri, line.range.end, this._document.eol === EndOfLine.CRLF ? "\r\n" : "\n");
this._wsEdit.insert(this.uri, line.range.end, this.endOfLine());
row++;
}

this._currentRange = new Range(row, 0, row, 0);
this._currentRow = empty.split(new RegExp(options.separator));
this._currentRow = empty.split(new RegExp(sep));
}

private beginEdit() {
Expand All @@ -146,8 +155,8 @@ export default class CsvDocumentView extends BaseDocumentView {
private async endEdit(refresh?: boolean) {
if (this._wsEdit) {
if (this._currentRow) {
let sep = this.getOptions().separator;
this._wsEdit.replace(this.uri, this._currentRange, this._currentRow.join(sep));
let sep = this.singleSeparator(this.getOptions().separator);
this._wsEdit.replace(this.uri, this._currentRange, this._currentRow.join(sep) + this.endOfLine());
}
await workspace.applyEdit(this._wsEdit);
this._wsEdit = undefined;
Expand Down

0 comments on commit c30fbb9

Please sign in to comment.