Repository: vscode
Issue: Improved CSV/TSV editing
Labels: feature-request,editor-core

<!-- ⚠️⚠️ Do Not Delete This! feature_request_template ⚠️⚠️ -->
<!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ -->
<!-- Please search existing issues to avoid creating duplicates. -->

<!-- Describe the feature you'd like. -->

This is a request for changes in the text editor to improve support for delimited text files.  Sorry about the length!

A CSV file is "just" a text file, but the semantics of the delimiter make the standard text editor a little cumbersome.  The main thing it can do better is align the columns.  Other helpful features might be handling multi-line content in a single cell, identifying column numbers, the ability to select whole columns, and so on.  Most of this is within reach of the current text editor.

There are several CSV extensions available.  Some use custom editors which look out of place in vscode and lack many of the cool features of the text editor.

So I did a proof-of-concept in the vscode source.  Here's an example:

![vscode-csv](https://user-images.githubusercontent.com/99791/158284882-a6ce453d-482b-49a2-9653-8f293c567428.gif)

You can see my changes at [the sbj42/csv-mode branch in my fork](https://github.com/sbj42/vscode/tree/sbj42/csv-mode), but keep in mind that I don't know the vscode source very well and I cut some corners in making the proof-of-concept.

I considered changing the extension API, but I don't think that will be enough. For instance, you could add API hooks for custom tab-like characters (e.g. so comma could be treated like a tab), along with "tab stops" which are computed from the text model and updated as the buffer changes.  But that would only go so far.

A better idea is to add built-in CSV support to the editor.

### Minimal features

Since this is a complex new feature, there are lots of optional pieces to it. In the spirit of the CONTRIBUTING doc's request for one feature per issue, here are the core changes that I think would need to be implemented.  I'll ut other related ideas in a separate section below.

#### CSV file type

A built-in CSV language mode, which would be different from "Plain Text".  It would be associated with `.csv` and `.tsv` extensions, but could potentially be detected in `.txt` files.

#### Delimiter option

In this mode, there is a delimiter character.  Tab and comma should be supported.  Maybe also pipe.  Similar to tab size, a heuristic could be used to guess the delimiter, an editor status widget would show the current delimiter, and the user could click it to change the delimiter.

I'm not sure if it's better to have separate CSV and TSV languages, but I'm leaning towards a single language with a variable delimiter.

#### Column alignment

In this mode, vscode should compute the widths of the columns based on the file content.  This calculation would need to be updated as edits are made. In case the file is very large, it could measure widths in the first N lines and also in the visible lines.

The line renderer would insert spaces to get the columns to line up, like it does today with indentation.

#### Marking columns

Some decoration would be useful to help identify the columns.  In the proof-of-concept I took over the "rulers", which would not otherwise be useful in this mode, but it should probably be a different decoration, so that it could be styled differently.

#### Handling quotes

It is common to allow the delimiter character to appear in a CSV value by quoting the value, so vscode would need to be aware of the quoting rules.  The raw characters would still be rendered, including the quotes.

#### Navigation commands

There should be new editor commands for navigating to the next/previous row or column.

#### Diff editor

When computing the column widths for diffs, both sides should be considered, so that the values all line up.

### Stretch goals

Here are some other ideas which I think could be part of the feature, or could be saved for later feature requests:

#### Handling multi-line values

Some applications permit newlines to appear in quoted values.  This would be nice to support, but it could be difficult.  Perhaps it could be handled by "wrapping" the cell on the newline.

#### Handling extreme cases

A wide column would not be too bad, but if a column has a small number of wide outliers then that could cause some confusion, as a user might see a lot of white space next to the narrow values.

I'm not sure what would be best here.  Truncation with ellipses would look nice at a high level, but would make editing much harder.  Some maximum column width setting combined with wrapping values in cells would be great, but might prove difficult to implement.

#### Column numbers

It would be nice to see column numbers, similar to row numbers.  I'm not sure if the `Ln X, Col Y` status bar widget should be different in CSV mode or not.

#### Selecting columns

If column numbers are shown, clicking on them could select the entire column. This could be implemented as multiple selections, one for each line.

#### Locking column headers

For some CSV files, the first line is a header line, containing the names of the columns.  It might be nice to support showing that line even when the view is scrolled down, but I don't think this is necessary.