<commit_before>import sublime, re
from ..utils import joinStr
from ..interpreter import *


class JavascriptInterpreter(Interpreter):

  def __init__(self):

    self.find_imports_regex = r"(import[\s\n]+((?:(?!from)[\s\S])*)[\s\n]+from[\s]+[\"\']([^\"\']+)[\"\'])"
    self.find_exports_regex = r"(export\s+(const|let|var|function|class)\s+(?P<value>[\w]+))"

    keys = {
      "variable": "[^\s]+",
      "module": "[^\s]+",
      "submodule": "[^\s]+",
      "submodules": "((?:(?!from)[\s\S])*)"
    }

    self.syntax = "javascript"
    self.settings = {
      "extensions": [".js", ".jsx"],
      "remove_extensions": [".js", ".jsx"],
      "extra_extensions": [".jpg", ".svg", ".json", ".gif", ".css", ".scss", ".less"],
      "modules_folder": "node_modules"
    }

    self.handlers = [
      Handler(
        name="import_all_from",
        matchers=[
          "import * as {variable} from {module}",
          "{variable}\.\*"
        ],
        keys=keys,
        force=True
      ),
      Handler(
        name="import_from",
        matchers=[
          "import {{submodules}} from [\'\"]{module}[\'\"]",
          "import {variable}, {{submodules}} from [\'\"]{module}[\'\"]",
          "{module}\.{submodule}",
          "{module}::{submodule}"
        ],
        keys=keys
      ),
      Handler(
        name="require_from",
        matchers=[
          "require {module}\.{submodule}",
          "{module}::{submodule}",
          "{module}\.{submodule}"
        ],
        keys=keys
      ),
      Handler(
        name="import",
        matchers=[
          "import {variable} from [\'\"]{module}[\'\"]",
          "{variable}\.{submodule}",
          "import {variable}",
          "(?P<module>[^\s\.]+)(\.[^\s]+){2,}"
        ],
        keys=keys
      ),
      Handler(
        name="require",
        matchers=[
          "require {variable}",
          "(const|let|var) {variable}",
          "(const|let|var) {variable} = {module}"
        ],
        keys=keys
      ),
      Handler(
        name="require_plain",
        matchers=[
          "req {variable}"
        ],
        keys=keys
      )
    ]

    self.setDefaultHandler("import")

  def parseModuleKey(self, value):
    for ext in self.getSetting('remove_extensions'):
      if value.endswith(ext):
        return value[0:-len(ext)]
    return value

  def parseSubmodulesKey(self, value):
    if type(value) is list :
      return value

    submodules = value.split(',')
    return [ submodule.strip() for submodule in submodules ]

  def parseVariableKey(self, value):
    return joinStr(re.sub(r"!|@|\*", "", value), r"\/|-|\.")

  def getQueryObject(self, interpreted):
    return interpreted.statements["module"]

  def onSearchResultChosen(self, interpreted, option_key, value):
    if option_key == "containing_files":
      interpreted.itype = "import_from"
      interpreted.statements['submodules'] = self.parseSubmodulesKey(interpreted.statements['variable'])
      del interpreted.statements['variable']

    interpreted.statements['module'] = self.parseModuleKey(value)

  def onInterprete(self, interpreted):
    statements = interpreted.statements
    if len(statements.keys()) == 0:
      index = 0
      keys = ["module", "variable"]
      values = interpreted.simport.expression.split(":")
      length = len(values)

      for key in keys:
        statements[key] = values[index]
        index += min(index + 1, length - 1)

      statements["module"] = statements["module"]
    else:
      if "module" not in statements:
        statements["module"] = statements["variable"]

      if "submodule" in statements:
        if "submodules" not in statements:
          statements["submodules"] = []
        statements["submodules"].append(statements["submodule"])
        statements.pop("submodule")

    return super().onInterprete(interpreted)

  def stringifyStatements(self, statements, itype=None, insert_type=Interpreted.IT_REPLACE):
    import_str = ''

    if itype.startswith('require'):
      if 'variable' in statements:
        import_str = 'const '
        import_str += statements['variable']
        import_str += ' = '

      if 'module':
        import_str += "require('{0}')".format(statements['module'])

    else:
      import_str = 'import '
      if itype == "import_all_from":<commit_msg>Fet: Add submoduling by ':' separator<commit_after>import sublime, re
from ..utils import joinStr
from ..interpreter import *


class JavascriptInterpreter(Interpreter):

  def __init__(self):

    self.find_imports_regex = r"(import[\s\n]+((?:(?!from)[\s\S])*)[\s\n]+from[\s]+[\"\']([^\"\']+)[\"\'])"
    self.find_exports_regex = r"(export\s+(const|let|var|function|class)\s+(?P<value>[\w]+))"

    keys = {
      "variable": "[^\s]+",
      "module": "[^\s]+",
      "submodule": "[^\s]+",
      "submodules": "((?:(?!from)[\s\S])*)"
    }

    self.syntax = "javascript"
    self.settings = {
      "extensions": [".js", ".jsx"],
      "remove_extensions": [".js", ".jsx"],
      "extra_extensions": [".jpg", ".svg", ".json", ".gif", ".css", ".scss", ".less"],
      "modules_folder": "node_modules"
    }

    self.handlers = [
      Handler(
        name="import_all_from",
        matchers=[
          "import * as {variable} from {module}",
          "{variable}\.\*"
        ],
        keys=keys,
        force=True
      ),
      Handler(
        name="import_from",
        matchers=[
          "import {{submodules}} from [\'\"]{module}[\'\"]",
          "import {variable}, {{submodules}} from [\'\"]{module}[\'\"]",
          "{submodule}::{module}"
        ],
        keys=keys
      ),
      Handler(
        name="import",
        matchers=[
          "import {variable} from [\'\"]{module}[\'\"]",
          "{variable}\.[^\s]+",
          "import {variable}",
          "(?P<module>[^\s\.]+)(\.[^\s]+){2,}"
        ],
        keys=keys
      ),
      Handler(
        name="require_from",
        matchers=[
          "require {module}\.{submodule}",
          "{submodule}::{module}",
          "{module}\.{submodule}"
        ],
        keys=keys
      ),
      Handler(
        name="require",
        matchers=[
          "require {variable}",
          "(const|let|var) {variable}",
          "(const|let|var) {variable} = {module}"
        ],
        keys=keys
      ),
      Handler(
        name="require_plain",
        matchers=[
          "req {variable}"
        ],
        keys=keys
      )
    ]

    self.setDefaultHandler("import")

  def parseModuleKey(self, value):
    for ext in self.getSetting('remove_extensions'):
      if value.endswith(ext):
        return value[0:-len(ext)]
    return value

  def parseSubmodulesKey(self, value):
    if type(value) is list :
      return value

    submodules = value.split(',')
    return [ submodule.strip() for submodule in submodules ]

  def parseVariableKey(self, value):
    return joinStr(re.sub(r"!|@|\*", "", value), r"\/|-|\.")

  def getQueryObject(self, interpreted):
    return interpreted.statements["module"]

  def onSearchResultChosen(self, interpreted, option_key, value):
    if option_key == "containing_files":
      interpreted.itype = "import_from"
      interpreted.statements['submodules'] = self.parseSubmodulesKey(interpreted.statements['variable'])
      del interpreted.statements['variable']

    interpreted.statements['module'] = self.parseModuleKey(value)

  def onInterprete(self, interpreted):
    statements = interpreted.statements
    if len(statements.keys()) == 0:
      values = interpreted.simport.expression.split(":")
      length = len(values)

      if length > 2:
        statements["module"] = values[-1]
        statements["submodules"] = values[:-1]
      else:
        statements["variable"] = values[0]
        statements["module"] = values[1] if length == 2 else statements["variable"]

    if "module" not in statements:
      statements["module"] = statements["variable"]

    if "submodule" in statements:
      if "submodules" not in statements:
        statements["submodules"] = []
      statements["submodules"].append(statements["submodule"])
      statements.pop("submodule")

    return super().onInterprete(interpreted)

  def stringifyStatements(self, statements, itype=None, insert_type=Interpreted.IT_REPLACE):
    import_str = ''

    if itype.startswith('require'):
      if 'variable' in statements:
        import_str = 'const '
        import_str += statements['variable']
        import_str += ' = '

      if 'module':
        import_str += "require('{0}')".format(statements['module'])

    else:
      import_str = 'import '
      if itype == "import_all_from":
{'fraction_non_alphanumeric': 0.1833757814308868, 'fraction_numerical': 0.001968048159296133, 'mean_word_length': 2.4947411003236244, 'pattern_counts': {'":': 22, '<': 7, '<?xml version=': 0, '>': 8, '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': '3857687', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2605, 'n_words': 496}