Skip to content

Commit

Permalink
Adds setting to disable automatic quoting, which is useful when forma…
Browse files Browse the repository at this point in the history
…tting source code.

When disabled, quote characters are still used to extend the range of a cell when parsing, but they are not stripped or re-added when formatting.
  • Loading branch information
wadetb committed Apr 2, 2015
1 parent d59d1c2 commit 13de7e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion AdvancedCSV.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"delimiter": ","
"delimiter": ",",
"auto_quote": true
}
13 changes: 12 additions & 1 deletion csvplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def __init__(self):
print("'{0}' is not a valid delimiter, reverting to ','.".format(self.delimiter))
self.delimiter = ','

self.auto_quote = self.settings.get('auto_quote')

def AddRow(self, row):
self.rows.append(row)

Expand Down Expand Up @@ -160,6 +162,8 @@ def RestoreSelection(self, view, saved_selection):
view.sel().add(region)

def QuoteText(self, text):
if not self.auto_quote:
return text
if self.delimiter in text or '"' in text:
return '"' + text.replace('"', '""') + '"'
else:
Expand Down Expand Up @@ -262,19 +266,26 @@ def ParseRow(self, row):
next_char = None

if char == '"' and next_char == '"':
currentword += '"'
if self.auto_quote:
currentword += '"'
else:
currentword += '""'
char_index += 2
continue

if insidequotes:
if char == '"':
insidequotes = False
if not self.auto_quote:
currentword += char
else:
currentword += char

else:
if char == '"':
insidequotes = True
if not self.auto_quote:
currentword += char

elif char == self.delimiter:
columns.append(CSVValue(currentword, first_char_index, char_index))
Expand Down

0 comments on commit 13de7e2

Please sign in to comment.