                 newtext = just(row[i], justify[i], widths[i])
                 line.push(newtext)
 
-            keepFirstAndLastPipes = atom.config.get("markdown-table-formatter.keepFirstAndLastPipes")
-
-            if keepFirstAndLastPipes
+            if @keepFirstAndLastPipes
                 formatted.push('|' + line.join('|') + '|')
             else
                 formatted.push(line.join('|'))


        columns = justify.length

        spacePadding = atom.config.get("markdown-table-formatter.spacePadding")

        content = []
        for line in lines
            line = line.trim().replace(/(^\||\|$)/g,"")
            cells = line.split('|')
            linecontent = ( ' '.repeat(spacePadding) + x.trim() + ' '.repeat(spacePadding) for x in cells )
            content.push(linecontent)

        rows = content.length

    #regex: /((.+?)\|)+?(.+)?\r?\n(([:\-\|]+?)\|)+?([:\-\|]+)?[ ]*(\r?\n((.+?)\|)+?(.+)?)+/mg

    constructor: ->
      @subscriptions = new CompositeDisposable
      @initConfig()
      atom.workspace.observeTextEditors (editor) =>
        @subscriptions.add editor.getBuffer().onWillSave =>
          @format editor, true if @formatOnSave

    readConfig: (key,callback) ->
      key='markdown-table-formatter.'+key
      @subscriptions.add atom.config.onDidChange key, callback
      callback
        newValue: atom.config.get(key)
        oldValue: undefined

    initConfig: () ->
      @readConfig "autoSelectEntireDocument", ({newValue}) =>
        @autoSelectEntireDocument = newValue
      @readConfig "spacePadding", ({newValue}) =>
        @spacePadding = newValue
      @readConfig "keepFirstAndLastPipes", ({newValue}) =>
        @keepFirstAndLastPipes = newValue
      @readConfig "formatOnSave", ({newValue}) =>
        @formatOnSave = newValue


    destroy: ->
        @subscriptions.dispose()
