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

Support for comments when linting via added setting comment_symbol #34

Merged
merged 3 commits into from
Apr 25, 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ The linter will check the following:
To disable automatic CSV Linting set `"rainbow_csv.enable_auto_csv_lint": false` in "Rainbow CSV" section of VS Code settings.
To recheck a csv file click on "CSVLint" button.

If `"rainbow_csv.comment_prefix"` in "Rainbow CSV" section of VS Code settings is set to some string, the linter will ignore all lines that start with it.
This is useful, e.g., if you have engineering CSVs that often start with multiple comment lines containing meta-data before the actual header.


#### Working with large files
To enable Rainbow CSV for very big files (more than 300K lines or 20MB) disable "Editor:Large File Optimizations" option in VS Code settings.
Expand All @@ -60,7 +63,7 @@ To remove cursor/selection from the header line use "Alt+Click" on it.

#### SetVirtualHeader
Adjust column names displayed in hover tooltips. Actual header line and file content won't be affected.
Rainbow CSV always assumes the first row as a header, so when there is no real header in a spreadsheet, you can use this command and provide comma-separated string with column names to create a "virtual" header for more comfortable data viewing. Accepted CSV format doesn't require you to customize all of the columns - this is useful when you want to name only some small subset of available columns. Note that you must provide comma-separated string no matter what separator is actually used in your spreadsheet file. "Virtual" header is persistent and will be associated with the parent file across VSCode sessions.
Rainbow CSV always assumes the first row that is not a comment (if comments are enabled) as a header, so when there is no real header in a spreadsheet, you can use this command and provide comma-separated string with column names to create a "virtual" header for more comfortable data viewing. Accepted CSV format doesn't require you to customize all of the columns - this is useful when you want to name only some small subset of available columns. Note that you must provide comma-separated string no matter what separator is actually used in your spreadsheet file. "Virtual" header is persistent and will be associated with the parent file across VSCode sessions.

### Colors customization
You can customize Rainbow CSV colors to increase contrast. [Instructions](test/color_customization_example.md#colors-customization)
Expand Down
28 changes: 26 additions & 2 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var global_state = null;

var preview_panel = null;

var comment_prefix = '#';

function dbg_log(msg) {
if (!enable_dev_mode)
Expand Down Expand Up @@ -125,6 +126,18 @@ function sample_preview_records_from_context(rbql_context) {
return preview_records;
}

function get_header_line(document) {
if (!comment_prefix)
return document.lineAt(0).text;
const num_lines = document.lineCount;
for (let lnum = 0; lnum < num_lines; ++lnum) {
const line_text = document.lineAt(lnum).text;
if (!line_text.startsWith(comment_prefix)) {
return line_text;
}
}
return null;
}

function get_header(document, delim, policy) {
var file_path = document.fileName;
Expand All @@ -134,7 +147,7 @@ function get_header(document, delim, policy) {
return rainbow_utils.smart_split(header, ',', 'quoted', false)[0];
}
}
return rainbow_utils.smart_split(document.lineAt(0).text, delim, policy, false)[0];
return rainbow_utils.smart_split(get_header_line(document), delim, policy, false)[0];
}


Expand All @@ -158,6 +171,9 @@ function make_hover_text(document, position, language_id) {
var cnum = position.character;
var line = document.lineAt(lnum).text;

if (comment_prefix && line.startsWith(comment_prefix))
return 'Comment';

var report = rainbow_utils.smart_split(line, delim, policy, true);

var entries = report[0];
Expand Down Expand Up @@ -206,12 +222,15 @@ function produce_lint_report(active_doc, delim, policy, max_check_size) {
var line_text = active_doc.lineAt(lnum).text;
if (lnum + 1 == num_lines && !line_text)
break;
if (comment_prefix && line_text.startsWith(comment_prefix))
continue;
var split_result = rainbow_utils.smart_split(line_text, delim, policy, false);
if (split_result[1]) {
return 'Error. Line ' + (lnum + 1) + ' has formatting error: double quote chars are not consistent';
}
if (lnum === 0)
if (!num_fields) {
num_fields = split_result[0].length;
}
if (num_fields != split_result[0].length) {
return 'Error. Number of fields is not consistent: e.g. line 1 has ' + num_fields + ' fields, and line ' + (lnum + 1) + ' has ' + split_result[0].length + ' fields.';
}
Expand Down Expand Up @@ -822,6 +841,8 @@ function column_edit(edit_mode) {
let line_text = active_doc.lineAt(lnum).text;
if (lnum + 1 == num_lines && !line_text)
break;
if (comment_prefix && line_text.startsWith(comment_prefix))
continue;
let report = rainbow_utils.smart_split(line_text, delim, policy, true);
let entries = report[0];
quoting_warning = quoting_warning || report[1];
Expand Down Expand Up @@ -966,6 +987,8 @@ function is_delimited_table(active_doc, delim, policy) {
var line_text = active_doc.lineAt(lnum).text;
if (lnum + 1 == num_lines && !line_text)
break;
if (comment_prefix && line_text.startsWith(comment_prefix))
continue;
let [fields, warning] = rainbow_utils.smart_split(line_text, delim, policy, true);
if (warning)
return false; // TODO don't fail on warnings in future versions
Expand Down Expand Up @@ -1132,6 +1155,7 @@ function activate(context) {
enable_tooltip_column_names = false;
if (config.get('enable_tooltip_warnings') === false)
enable_tooltip_warnings = false;
comment_prefix = config.get('comment_prefix');
}

global_state = context.globalState;
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"type": "string",
"default": "utf-8",
"description": "Manually set csv table encoding"
},
"rainbow_csv.comment_prefix": {
"type": "string",
"default": "#",
"description": "Comment prefix to denote lines that should be ignored when linting"
}
}
},
Expand Down