         if @eslintIndentOptions.jsxIndentProps[1]
           jsxIndentTabs += jsxPropsIndent * @eslintIndentOptions.jsxIndentProps[1]
     @editor.setIndentationForBufferRow row, jsxIndentTabs, { preserveLeadingWhitespace: false }
+
+  # get tab indents from eslint indent
+  getEslintIndent: () ->
+    if @eslintIndentOptions.indent[0] then jsIndent = @eslintIndentOptions.indent[1]
+    else jsIndent = 0
+    jsIndent

      # watch eslintrc for changes
      @eslintrcFilename.onDidChange => @getEslintrcOptions(@eslintrcFilename.getPath())

  # you know it makes sense
  destroy: () ->
    @editor.languageMode.autoIndentBufferRow = @atomAutoIndentBufferRow
    @disposables.dispose()

  # monkey patched code
  autoIndentBufferRow: (bufferRow, options) =>
    if not @autoJsx or not @jsxInScope(bufferRow)
      return @atomAutoIndentBufferRow.call(@editor.languageMode, bufferRow, options )
    # a new line has been added so what jsx is before it?
    endPointOfLine = new Point bufferRow-1, @editor.lineTextForBufferRow(bufferRow-1).length
    startOfJSX =  autoCompleteJSX.getStartOfJSX @editor, endPointOfLine
    endPointOfLine = new Point bufferRow+1, 0
    indent = @indentJSX new Range(startOfJSX, endPointOfLine)
    columnToMoveTo = /\S|$/.exec(@editor.lineTextForBufferRow(bufferRow)).index
    @editor.setCursorBufferPosition [bufferRow, columnToMoveTo]
    indent

  # command option to format line from a cursor position upwards to JSX start
  autoIndentJsxCommand: () ->
    return if atom.workspace.getActiveTextEditor().id isnt @editor.id
    bufferRow = @editor.getCursorBufferPosition().row
    return if not @jsxInScope(bufferRow)
    endPointOfLine = new Point bufferRow, @editor.lineTextForBufferRow(bufferRow).length
    startOfJSX =  autoCompleteJSX.getStartOfJSX @editor, endPointOfLine
    @editor.transact 300, =>
      @indentJSX new Range(startOfJSX, endPointOfLine)

  # is the jsx on this line in scope
  jsxInScope: (bufferRow) ->

                hardTabsFound += ((line.substr i, 1) is '\t')
              hardTabsFound

          if isFirstTokenOfLine
              firstTagInLineIndentation =  tagIndentation

          # big switch statement follows for each token. If the line is reformated
          # then we recalculate the new position.
          switch (token)
            # tags starting <tag
            when JSXTAG_OPEN
              tokenOnThisLine = true
              # indent only on first token of a line
              if isFirstTokenOfLine and indentRecalc
                stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
                # isFirstTagOfBlock is used to mark the tag that starts the JSX but
                # also the first tag of blocks inside  embedded expressions. e.g.
                # <tbody> and <objectRow> are first tags
                # return (
                #       <tbody>
                #         {objects.map(function(object, i){
                #           return <ObjectRow obj={object} key={i} />;
                #         })}
                #       </tbody>
                #     )
                # but we don't position the <tbody> as we have no knowledge of the preceeding
                # js syntax
                if isFirstTagOfBlock and
                    parentTokenIdx? and
                    tokenStack[parentTokenIdx].type is BRACE_OPEN and
                    tokenStack[parentTokenIdx].row is ( row - 1)
                      # previous line started with a brace so use different indent rule
                      # based upon eslint indent not React/indent
                      tagIndentation = firstCharIndentation =
                        @getEslintIndent() + @getIndentOfPreviousRow row
                      @indentRow(row, firstCharIndentation)
                      line = @editor.lineTextForBufferRow row
                      JSXREGEXP.lastIndex = 0 #force regex to start again
                      indentRecalc = false
                      continue
                else if isFirstTagOfBlock and parentTokenIdx?
                  tagIndentation = firstCharIndentation = @getIndentOfPreviousRow row
                  @indentRow(row, firstCharIndentation, 1)
                  line = @editor.lineTextForBufferRow row
                  JSXREGEXP.lastIndex = 0 #force regex to start again
                  indentRecalc = false
                  continue
                else if parentTokenIdx?
                  @indentRow(row, tokenStack[parentTokenIdx].firstCharIndentation, 1 )
                  line = @editor.lineTextForBufferRow row
                  JSXREGEXP.lastIndex = 0 #force regex to start again
                  indentRecalc = false
                  continue

              isFirstTokenOfLine = false
              isFirstTagOfBlock = false

              stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
              tokenStack.push
                type: JSXTAG_OPEN
                name: match[2]
                row: row
                firstTagInLineIndentation: firstTagInLineIndentation
                tagIndentation: tagIndentation
                firstCharIndentation: firstCharIndentation
                parentTokenIdx: parentTokenIdx
                termsThisTagsAttributesIdx: null  # ptr to > tag
                termsThisTagIdx: null             # ptr to </tag>

              stackOfTokensStillOpen.push idxOfToken
              idxOfToken++
              isInsideTag = true
              break;

            # tags ending </tag>
            when JSXTAG_CLOSE
              tokenOnThisLine = true
              if isFirstTokenOfLine and indentRecalc
                stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
                @indentRow(row, tokenStack[parentTokenIdx].firstCharIndentation )
                line = @editor.lineTextForBufferRow row
                JSXREGEXP.lastIndex = 0 #force regex to start again
                indentRecalc = false
                continue

              isFirstTokenOfLine = false
              isFirstTagOfBlock = false

              parentTokenIdx = stackOfTokensStillOpen.pop()
              tokenStack.push
                type: JSXTAG_CLOSE
                name: match[5]
                row: row
                parentTokenIdx: parentTokenIdx         # ptr to <tag
              if parentTokenIdx >=0 then tokenStack[parentTokenIdx].termsThisTagIdx = idxOfToken
              idxOfToken++
              isInsideTag = false
              break;

            # tags ending />
            when JSXTAG_SELFCLOSE_END
              tokenOnThisLine = true
              if isFirstTokenOfLine and indentRecalc
                stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
                if firstTagInLineIndentation is firstCharIndentation
                  @indentForClosingBracket  row,
                    tokenStack[parentTokenIdx],
                    @eslintIndentOptions.jsxClosingBracketLocation[1].selfClosing
                else
                  @indentRow  row,
                    tokenStack[parentTokenIdx].firstTagInLineIndentation,0,1
                line = @editor.lineTextForBufferRow row
                JSXREGEXP.lastIndex = 0 #force regex to start again
                indentRecalc = false
                continue

              isFirstTagOfBlock = false
              isFirstTokenOfLine = false

              parentTokenIdx = stackOfTokensStillOpen.pop()
              tokenStack.push
                type: JSXTAG_SELFCLOSE_END
                name: tokenStack[parentTokenIdx].name
                row: row
                parentTokenIdx: parentTokenIdx       # ptr to <tag
              if parentTokenIdx >= 0
                tokenStack[parentTokenIdx].termsThisTagsAttributesIdx = idxOfToken
                tokenStack[parentTokenIdx].type = JSXTAG_SELFCLOSE_START
                tokenStack[parentTokenIdx].termsThisTagIdx = idxOfToken
              idxOfToken++
              isInsideTag = false
              break;

            # tags ending >
            when JSXTAG_CLOSE_ATTRS
              tokenOnThisLine = true
              if isFirstTokenOfLine and indentRecalc
                stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
                if firstTagInLineIndentation is firstCharIndentation
                  @indentForClosingBracket  row,
                    tokenStack[parentTokenIdx],
                    @eslintIndentOptions.jsxClosingBracketLocation[1].nonEmpty
                else
                  @indentRow  row,
                    tokenStack[parentTokenIdx].firstTagInLineIndentation,0,1
                line = @editor.lineTextForBufferRow row
                JSXREGEXP.lastIndex = 0 #force regex to start again
                indentRecalc = false
                continue

              isFirstTagOfBlock = false
              isFirstTokenOfLine = false

              stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
              tokenStack.push
                type: JSXTAG_CLOSE_ATTRS
                name: tokenStack[parentTokenIdx].name
                row: row
                parentTokenIdx: parentTokenIdx            # ptr to <tag
              if parentTokenIdx >= 0 then tokenStack[parentTokenIdx].termsThisTagsAttributesIdx = idxOfToken
              idxOfToken++
              isInsideTag = false
              break;

            # embeded expression start { but not inside a tag
            when JSXBRACE_OPEN
              break if isInsideTag
              tokenOnThisLine = true
              if isFirstTokenOfLine and indentRecalc
                stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
                if parentTokenIdx?
                  @indentRow(row, tokenStack[parentTokenIdx].firstCharIndentation, 1 )
                  line = @editor.lineTextForBufferRow row
                  JSXREGEXP.lastIndex = 0 #force regex to start again
                  indentRecalc = false
                  continue

              isFirstTagOfBlock = true  # this may be the start of a new JSX block
              isFirstTokenOfLine = false

              stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
              tokenStack.push
                type: JSXBRACE_OPEN
                name: ''
                row: row
                firstTagInLineIndentation: firstTagInLineIndentation
                tagIndentation: tagIndentation
                firstCharIndentation: firstCharIndentation
                parentTokenIdx: parentTokenIdx
                termsThisTagsAttributesIdx: null  # ptr to > tag
                termsThisTagIdx: null             # ptr to </tag>

              stackOfTokensStillOpen.push idxOfToken
              idxOfToken++
              break;

            # embeded expression end } but not inside a tag
            when JSXBRACE_CLOSE
              break if isInsideTag
              tokenOnThisLine = true
              if isFirstTokenOfLine and indentRecalc
                stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
                @indentRow(row, tokenStack[parentTokenIdx].firstCharIndentation )
                line = @editor.lineTextForBufferRow row
                JSXREGEXP.lastIndex = 0 #force regex to start again
                indentRecalc = false
                continue

              isFirstTagOfBlock = false
              isFirstTokenOfLine = false

              parentTokenIdx = stackOfTokensStillOpen.pop()
              tokenStack.push
                type: JSXBRACE_CLOSE
                name: ''
                row: row
                parentTokenIdx: parentTokenIdx         # ptr to <tag
              if parentTokenIdx >=0 then tokenStack[parentTokenIdx].termsThisTagIdx = idxOfToken
              idxOfToken++
              break;

            # Javascript brace Start {
            when BRACE_OPEN
              break if isInsideTag
              tokenOnThisLine = true
              if isFirstTokenOfLine and indentRecalc
                stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
                if isFirstTagOfBlock and
                    parentTokenIdx? and
                    tokenStack[parentTokenIdx].type is BRACE_OPEN and
                    tokenStack[parentTokenIdx].row is ( row - 1)
                      # previous line started with a brace so use different indent rule
                      # based upon eslint indent not React/indent
                      tagIndentation = firstCharIndentation =
                        @getEslintIndent() + @getIndentOfPreviousRow row
                      @indentRow(row, firstCharIndentation)
                      line = @editor.lineTextForBufferRow row
                      JSXREGEXP.lastIndex = 0 #force regex to start again
                      indentRecalc = false
                      continue
                else if parentTokenIdx?
                  @indentRow(row, tokenStack[parentTokenIdx].firstCharIndentation, 1 )
                  line = @editor.lineTextForBufferRow row
                  JSXREGEXP.lastIndex = 0 #force regex to start again
                  indentRecalc = false
                  continue

              isFirstTokenOfLine = false

              stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
              tokenStack.push
                type: BRACE_OPEN
                name: ''
                row: row
                firstTagInLineIndentation: firstTagInLineIndentation
                tagIndentation: tagIndentation
                firstCharIndentation: firstCharIndentation
                parentTokenIdx: parentTokenIdx
                termsThisTagsAttributesIdx: null  # ptr to > tag
                termsThisTagIdx: null             # ptr to </tag>

              stackOfTokensStillOpen.push idxOfToken
              idxOfToken++
              break;

            # Javascript brace End }
            when BRACE_CLOSE
              break if isInsideTag
              tokenOnThisLine = true
              if isFirstTokenOfLine and indentRecalc
                stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
                if parentTokenIdx?
                  @indentRow(row, tokenStack[parentTokenIdx].firstCharIndentation )
                  line = @editor.lineTextForBufferRow row
                  JSXREGEXP.lastIndex = 0 #force regex to start again
                  indentRecalc = false
                  continue

              isFirstTokenOfLine = false

              parentTokenIdx = stackOfTokensStillOpen.pop()
              if parentTokenIdx?
                tokenStack.push
                  type: BRACE_CLOSE
                  name: ''
                  row: row
                  parentTokenIdx: parentTokenIdx         # ptr to <tag
                if parentTokenIdx >=0 then tokenStack[parentTokenIdx].termsThisTagIdx = idxOfToken
                idxOfToken++
              break;

      if idxOfToken and not tokenOnThisLine and  row isnt range.end.row
        if not isInsideTag
          stackOfTokensStillOpen.push parentTokenIdx = stackOfTokensStillOpen.pop()
          if parentTokenIdx?
            if tokenStack[parentTokenIdx].type is BRACE_OPEN and
              tokenStack[parentTokenIdx].row is ( row - 1)
                # indent based upon preceeding js brace
                @indentRow  row, tokenStack[parentTokenIdx].firstCharIndentation, @getEslintIndent()
            else
              @indentRow  row, @getIndentOfPreviousRow row
        else
          # component properties most likely
          @indentRow  row, tokenStack[idxOfToken-1].firstCharIndentation,0,1

      previousLineIndent = @editor.indentationForBufferRow row
      if row is range.end.row - 1 # the row that was inserted
        indent =  @editor.indentationForBufferRow row # will be returned
    return indent

  # get the token at the given match position or return truthy false
  getToken: (bufferRow, match) ->
    scope = @editor.scopeDescriptorForBufferPosition([bufferRow, match.index]).getScopesArray().pop()
    if 'punctuation.definition.tag.jsx' is scope
      if      match[1]? then return JSXTAG_OPEN
      else if match[3]? then return JSXTAG_SELFCLOSE_END
    else if 'JSXEndTagStart' is scope
      if match[4]? then return JSXTAG_CLOSE
    else if 'JSXStartTagEnd' is scope
      if match[7]? then return JSXTAG_CLOSE_ATTRS
    else if match[8]?
      if 'punctuation.section.embedded.begin.jsx' is scope
        return JSXBRACE_OPEN
      else if 'meta.brace.curly.js' is scope
        return BRACE_OPEN
    else if match[9]?
      if 'punctuation.section.embedded.end.jsx' is scope
        return JSXBRACE_CLOSE
      else if 'meta.brace.curly.js' is scope
        return BRACE_CLOSE
    return NO_TOKEN


  # get indent of the previous row with chars in it
  getIndentOfPreviousRow: (row) ->
    return 0 unless row
    for row in [row-1...0]
      line = @editor.lineTextForBufferRow row
      return @editor.indentationForBufferRow row if  /.*\S/.test line
    return 0

  # allign nonEmpty and selfClosing tags based on eslint rules
  # row to be indented based upon a parentTags properties and a rule type
