diff --git a/csvplugin.py b/csvplugin.py index 6698a0a..ade9ede 100644 --- a/csvplugin.py +++ b/csvplugin.py @@ -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) diff --git a/semi.csv b/semi.csv new file mode 100644 index 0000000..f4c8b8e --- /dev/null +++ b/semi.csv @@ -0,0 +1,2 @@ +a ;b;c +111;2;3