Skip to content

Commit

Permalink
Add command to override the delimiter for the view; setting is saved …
Browse files Browse the repository at this point in the history
…in the project file.
  • Loading branch information
wadetb committed Jul 23, 2015
1 parent 179cdcd commit b71f8be
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
4 changes: 4 additions & 0 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
"command": "csv_delete_trailing_cols",
"caption": "Delete empty trailing columns"
},
{
"command": "csv_set_delimiter",
"caption": "Set delimiter"
},
{
"caption": "-"
},
Expand Down
4 changes: 4 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"command": "csv_delete_trailing_cols",
"caption": "CSV: Delete empty trailing columns"
},
{
"command": "csv_set_delimiter",
"caption": "CSV: Set delimiter"
},
{
"command": "csv_evaluate",
"caption": "CSV: Evaluate cells"
Expand Down
4 changes: 4 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
"command": "csv_delete_trailing_cols",
"caption": "Delete empty trailing columns"
},
{
"command": "csv_set_delimiter",
"caption": "Set delimiter"
},
{
"caption": "-"
},
Expand Down
48 changes: 35 additions & 13 deletions csvplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,27 @@ def __lt__(self, other): return self.Compare(other) < 0
def __eq__(self, other): return self.Compare(other) == 0

class CSVMatrix:
def __init__(self):
self.settings = sublime.load_settings('AdvancedCSV.sublime-settings')

def __init__(self, view):
self.rows = []
self.num_columns = 0
self.valid = False
self.view = view

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

self.delimiter = self.GetSetting( 'delimiter', ',' )

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

self.auto_quote = self.settings.get('auto_quote')
self.auto_quote = self.GetSetting( 'auto_quote', True )

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

def AddRow(self, row):
self.rows.append(row)
Expand Down Expand Up @@ -311,8 +319,10 @@ def ParseRow(self, row):
return columns

@staticmethod
def FromText(text):
matrix = CSVMatrix()
def FromView(view):
matrix = CSVMatrix(view)

text = view.substr(sublime.Region(0, view.size()))

for line in text.split("\n"):
row = matrix.ParseRow(line)
Expand All @@ -323,12 +333,6 @@ def FromText(text):

return matrix

@staticmethod
def FromView(view):
text = view.substr(sublime.Region(0, view.size()))

return CSVMatrix.FromText(text)

def GetColumnIndexFromCursor(self, view):
selection = view.sel()[0]

Expand Down Expand Up @@ -699,3 +703,21 @@ def on_change(self, input):

def on_cancel(self):
pass

class CsvSetDelimiterCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()

self.window.show_input_panel('Delimiter character', "",
self.on_done, self.on_change, self.on_cancel)

def on_done(self, input):
view = self.window.active_view()

view.settings().set('delimiter', input)

def on_change(self, input):
pass

def on_cancel(self):
pass
24 changes: 12 additions & 12 deletions test.csv
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
hello ,col0,sdfadfasdf,col1,328000822119.0 ,1.7320508075688772
aa ,bb , ,c ,328000822119.0 ,1.7320508075688772
1 ,2 , ,3 ,328000822119.0 ,1.7320508075688772
4 ,3 , ,2 ,0.01e10
",a "" ",b , ,c ,.45
'asd' , , , ,125.0
, , ,0.45,[0:3]=m[:].sum()
, , , ,"[0:3,+1:+2]=sqrt(3)", ,
, , , ,12


,
hello |xcol0|sdfadfasdf|col1|328000822119.0 |1.7320508075688772
aa |xbb | |c |328000822119.0 |1.7320508075688772
1 |x2 | |3 |328000822119.0 |1.7320508075688772
4 |x3 | |2 |0.01e10
"|a ""|xb"| |c |.45
'asd' |x | | |125.0
|x | |0.45|[0:3]=m[:].sum()
|x | | |[0:3 |+1:+2]=sqrt(3) ||
|x | | |12
|x

0 comments on commit b71f8be

Please sign in to comment.