Skip to content

Commit

Permalink
Fix per-view delimiter selection
Browse files Browse the repository at this point in the history
The priority order of delimiter selection previously favored filename mappings over a per-view setting (CSV -> Set Delimiter).
This was an unintended consequence of wrapping per-view and global settings access.

The delimiter code has been reworked to determine the delimiter in an explicit fallback order: view, filename mapping, global setting, comma.
  • Loading branch information
wadetb committed Nov 11, 2016
1 parent 4b42651 commit 64d9b35
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
44 changes: 28 additions & 16 deletions csvplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,48 @@ def __init__(self, view):

self.settings = sublime.load_settings('AdvancedCSV.sublime-settings')

self.DetermineDelimiter()
if not isstr(self.delimiter) or len(self.delimiter) != 1:
print("'{0}' is not a valid delimiter, reverting to ','.".format(self.delimiter))
self.delimiter = ','
#print("Using delimiter: '{0}'.".format(self.delimiter))
self.ChooseDelimiter()

self.auto_quote = self.GetViewSetting( 'auto_quote', True )
self.auto_quote = self.GetViewOrUserSetting( 'auto_quote', True )

def GetViewSetting(self, name, default):
def GetViewOrUserSetting(self, name, default):
if self.view.settings().has(name):
return self.view.settings().get(name)
else:
return self.settings.get(name, default)

def DetermineDelimiter(self):
filename = self.view.file_name()
def ChooseDelimiter(self):
self.delimiter = None

# Highest priority: per-view saved setting (CSV -> Set Delimiter).
if self.view.settings().has('delimiter'):
self.delimiter = self.view.settings().get('delimiter')

# Second highest priority: filename-based matching
if not self.delimiter:
filename = self.view.file_name()

if filename:
self.delimiter_mapping = self.settings.get('delimiter_mapping', {})
for k, v in self.delimiter_mapping.items():
if fnmatch.fnmatch(filename, k):
self.delimiter = v
return
if filename:
self.delimiter_mapping = self.settings.get('delimiter_mapping', {})
for k, v in self.delimiter_mapping.items():
if fnmatch.fnmatch(filename, k):
self.delimiter = v
break

self.delimiter = self.GetViewSetting('delimiter', ',')
# Final priority: user or system setting, fallback to comma.
if not self.delimiter:
self.delimiter = self.settings.get('delimiter', ',')

# Special case for recognizing '\t' for tabs.
if self.delimiter == '\\t':
self.delimiter = '\t'

if not isstr(self.delimiter) or len(self.delimiter) != 1:
print("'{0}' is not a valid delimiter, reverting to ','.".format(self.delimiter))
self.delimiter = ','

print("Using delimiter: '{0}'.".format(self.delimiter))

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

Expand Down
2 changes: 2 additions & 0 deletions semi.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a ;b;c
111;2;3

0 comments on commit 64d9b35

Please sign in to comment.