<commit_before>{Range} = require 'atom'
FunctionInfo = require './function-info'

module.exports =
class TextEditorParser
  constructor: (@textEditor = null) ->

  setEditor: (textEditor) ->
    @textEditor = textEditor

  getFileType: ->
    return null if not @textEditor?

    splitTitle = @textEditor.getTitle().split(".")
    return splitTitle[splitTitle.length - 1]

  getWordContainingCursor: ->
    return null if not @textEditor?

    initial = @textEditor.getCursorBufferPosition()

    @textEditor.moveToBeginningOfWord()
    start = @textEditor.getCursorBufferPosition()

    @textEditor.moveToEndOfWord()
    end = @textEditor.getCursorBufferPosition()

    @textEditor.setCursorBufferPosition(initial)

    return @textEditor.getTextInBufferRange(new Range(start, end))

  getFunctionInfo: (startingRow, isTabBased) ->
    if (isTabBased)
      functionInfo = @getFunctionInfoForTab(startingRow)
      return functionInfo

    initial = @textEditor.getCursorBufferPosition()

    openBrackets = 0
    currRow = startingRow
    endOfLineColumn = 0

    while currRow <= @textEditor.getLastBufferRow()
      line = @textEditor.lineTextForBufferRow(currRow)

      # determine end of line column
      @textEditor.setCursorBufferPosition([currRow, 0])
      @textEditor.moveToEndOfLine()
      endOfLineColumn = @textEditor.getCursorBufferPosition().column

      if line.includes "{"
        openBrackets++

      if line.includes "}"
        openBrackets--
        break if openBrackets is 0

      currRow++

    @textEditor.setCursorBufferPosition(initial)

    functionRange = new Range([startingRow, 0], [currRow, endOfLineColumn])
    functionText = @textEditor.getTextInBufferRange(functionRange)
    return new FunctionInfo(functionText, functionRange)

  getFunctionInfoForTab: (startingRow) ->
    # TODO handle if there is a line break with no tab
    tabText = @textEditor.getTabText()

    regExp = new RegExp("#{tabText}", "g")
    line = @textEditor.lineTextForBufferRow(startingRow)
    initialTabCount = (line.match(regExp) || []).length

    endOfLineColumn = 0
    currRow = startingRow + 1
    initial = @textEditor.getCursorBufferPosition()

    while currRow <= @textEditor.getLastBufferRow()
      line = @textEditor.lineTextForBufferRow(currRow)

      lineTabCount = (line.match(regExp) || []).length

      if lineTabCount <= initialTabCount and line
        # go back a row
        currRow--

        # determine end of line column
        @textEditor.setCursorBufferPosition([currRow, 0])
        @textEditor.moveToEndOfLine()
        endOfLineColumn = @textEditor.getCursorBufferPosition().column

        break

      currRow++

    @textEditor.setCursorBufferPosition(initial)

    functionRange = new Range([startingRow, 0], [currRow, endOfLineColumn])
    functionText = @textEditor.getTextInBufferRange(functionRange)
    return new FunctionInfo(functionText, functionRange)
<commit_msg>Remove TODO statement<commit_after>{Range} = require 'atom'
FunctionInfo = require './function-info'

module.exports =
class TextEditorParser
  constructor: (@textEditor = null) ->

  setEditor: (textEditor) ->
    @textEditor = textEditor

  getFileType: ->
    return null if not @textEditor?

    splitTitle = @textEditor.getTitle().split(".")
    return splitTitle[splitTitle.length - 1]

  getWordContainingCursor: ->
    return null if not @textEditor?

    initial = @textEditor.getCursorBufferPosition()

    @textEditor.moveToBeginningOfWord()
    start = @textEditor.getCursorBufferPosition()

    @textEditor.moveToEndOfWord()
    end = @textEditor.getCursorBufferPosition()

    @textEditor.setCursorBufferPosition(initial)

    return @textEditor.getTextInBufferRange(new Range(start, end))

  getFunctionInfo: (startingRow, isTabBased) ->
    if (isTabBased)
      functionInfo = @getFunctionInfoForTab(startingRow)
      return functionInfo

    initial = @textEditor.getCursorBufferPosition()

    openBrackets = 0
    currRow = startingRow
    endOfLineColumn = 0

    while currRow <= @textEditor.getLastBufferRow()
      line = @textEditor.lineTextForBufferRow(currRow)

      # determine end of line column
      @textEditor.setCursorBufferPosition([currRow, 0])
      @textEditor.moveToEndOfLine()
      endOfLineColumn = @textEditor.getCursorBufferPosition().column

      if line.includes "{"
        openBrackets++

      if line.includes "}"
        openBrackets--
        break if openBrackets is 0

      currRow++

    @textEditor.setCursorBufferPosition(initial)

    functionRange = new Range([startingRow, 0], [currRow, endOfLineColumn])
    functionText = @textEditor.getTextInBufferRange(functionRange)
    return new FunctionInfo(functionText, functionRange)

  getFunctionInfoForTab: (startingRow) ->
    tabText = @textEditor.getTabText()

    regExp = new RegExp("#{tabText}", "g")
    line = @textEditor.lineTextForBufferRow(startingRow)
    initialTabCount = (line.match(regExp) || []).length

    endOfLineColumn = 0
    currRow = startingRow + 1
    initial = @textEditor.getCursorBufferPosition()

    while currRow <= @textEditor.getLastBufferRow()
      line = @textEditor.lineTextForBufferRow(currRow)

      lineTabCount = (line.match(regExp) || []).length

      if lineTabCount <= initialTabCount and line
        # go back a row
        currRow--

        # determine end of line column
        @textEditor.setCursorBufferPosition([currRow, 0])
        @textEditor.moveToEndOfLine()
        endOfLineColumn = @textEditor.getCursorBufferPosition().column

        break

      currRow++

    @textEditor.setCursorBufferPosition(initial)

    functionRange = new Range([startingRow, 0], [currRow, endOfLineColumn])
    functionText = @textEditor.getTextInBufferRange(functionRange)
    return new FunctionInfo(functionText, functionRange)

{'fraction_non_alphanumeric': 0.09915793091596495, 'fraction_numerical': 0.003437016669530847, 'mean_word_length': 3.7049312853678256, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6619781', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1624, 'n_words': 345}