Ejemplo n.º 1
0
def is_keyword_statement(view, point):
    nesting = expression.get_nesting(view, point - 1, expression=r'{')
    if nesting == None:
        return False

    chars_before_nesting = view.substr(
        sublime.Region(max(nesting[0] - 512, 0), nesting[0] - 1))

    match = re.search(r'\)(\s*)$', chars_before_nesting)
    if match == None:
        return False

    parenthesis_nesting = expression.get_nesting(view,
                                                 nesting[0] - 2 -
                                                 len(match.group(1)),
                                                 expression=r'\(')

    if parenthesis_nesting == None:
        return False

    chars_before_parenthesis = view.substr(
        sublime.Region(max(parenthesis_nesting[0] - 512, 0),
                       parenthesis_nesting[0] - 1))

    keyword_regexp = r'(if|for|while|function\s+\w+)\s*$'
    return re.search(keyword_regexp, chars_before_parenthesis) != None
Ejemplo n.º 2
0
def is_keyword_statement(view, point):
  nesting = expression.get_nesting(view, point - 1, expression = r'{')
  if nesting == None:
    return False

  chars_before_nesting = view.substr(sublime.Region(
    max(nesting[0] - 512, 0),
    nesting[0] - 1
  ))

  match = re.search(r'\)(\s*)$', chars_before_nesting)
  if match == None:
    return False

  parenthesis_nesting = expression.get_nesting(view, nesting[0] - 2 -
    len(match.group(1)), expression = r'\(')

  if parenthesis_nesting == None:
    return False

  chars_before_parenthesis = view.substr(sublime.Region(
    max(parenthesis_nesting[0] - 512, 0),
    parenthesis_nesting[0] - 1
  ))

  keyword_regexp = r'(if|for|while|function\s+\w+)\s*$'
  return re.search(keyword_regexp, chars_before_parenthesis) != None
Ejemplo n.º 3
0
def get_parent_argument(view, cursor, expr = r'[({\[]'):
  nesting = expression.get_nesting(view, cursor, RANGE, {}, expr)
  if not is_arguments(view, cursor, nesting):
    return None

  _, argument = get_argument(view, nesting[0] - 1)
  return argument
Ejemplo n.º 4
0
def _find_completions(view, backward, completions, words, word, search):
    lookup_arguments = True
    for sel in view.sel():
        if expression.get_nesting(view, sel.b, 1024 * 5) == None:
            lookup_arguments = False
            break

    point = view.sel()[0].b
    cursor = None
    if backward:
        point -= len(word)
        cursor = 'end'

    matches = expression.find_matches(view, point, search, {
        'backward': backward,
        'nesting': True,
        'string': True,
        'cursor': cursor,
    })

    for match in matches:
        start = match.start(1)

        call_end = _get_call_end(view, match)
        if call_end != None:
            _append_region(view, completions, words,
                           sublime.Region(start, call_end))

        if lookup_arguments:
            arg_end = _get_argument_end(view, match)
            if arg_end != None:
                _append_region(view, completions, words,
                               sublime.Region(start, arg_end))

    return completions
def get_partial_statement(view, point):
    nesting = expression.get_nesting(view, point)

    as_argument = (nesting != None and statement.get_statement(
        view, nesting[0]) == statement.get_statement(view, nesting[1]))

    if as_argument:
        argument = statement.get_argument(view, point)
        if argument != None:
            _, argument = argument

        argument = prepare_argument(view, argument, point)
        if argument == None or is_variable(view, argument):
            token = statement.get_parent_token(view, point)
            return prepare_argument(view, token, point)

        return argument
    else:
        tokens = statement.get_tokens(view, point)
        if tokens == None:
            return None

        _, token = statement.get_token(view, point, tokens)
        if token == None or not is_variable(view, token):
            return token

        tokens_range = [tokens[0][0], tokens[len(tokens) - 1][1]]
        assignment_match = expression.find_match(view, tokens_range[0],
                                                 r'=[^=]\s*',
                                                 {'range': tokens_range})
        if assignment_match != None:
            tokens_range[0] += assignment_match.end(0)
        return tokens_range
Ejemplo n.º 6
0
  def _get_info(self, sel, as_arguments):
    nesting = expression.get_nesting(self.view, sel.b)

    if sel.size() > 0:
      container = [sel.begin(), sel.end()]
    else:
      container = (nesting or statement.get_statement(self.view, sel.b))

    if container == None:
      return None

    if as_arguments:
      tokens = statement.get_arguments(self.view, sel.b, container)
    else:
      raw_tokens = statement.get_tokens(self.view, sel.b, container)
      if len(raw_tokens) == 0:
        tokens = raw_tokens
      else:
        tokens = self._get_logical_tokens(raw_tokens)

    if len(tokens) == 0:
      return None

    if not as_arguments and nesting == None:
      start = max(container[0], tokens[0][0])
      end = min(container[1], tokens[len(tokens) - 1][1])
      container = [start, end]

    new_lines = as_arguments or nesting != None
    return container, tokens, new_lines
Ejemplo n.º 7
0
  def run(self, edit):
    for sel in reversed(self.view.sel()):
      nesting = expression.get_nesting(self.view, sel.begin(), 2048)
      if nesting == None:
        continue

      self.view.erase(edit, sublime.Region(nesting[1], nesting[1] + 1))
Ejemplo n.º 8
0
def _find_completions(view, backward, completions, words, word, search):
  lookup_arguments = True
  for sel in view.sel():
    if expression.get_nesting(view, sel.b, 1024 * 5) == None:
      lookup_arguments = False
      break

  point = view.sel()[0].b
  cursor = None
  if backward:
    point -= len(word)
    cursor = 'end'

  matches = expression.find_matches(view, point, search, {'backward': backward,
    'nesting': True, 'string': True, 'cursor': cursor,})

  for match in matches:
    start = match.start(1)

    call_end = _get_call_end(view, match)
    if call_end != None:
      _append_region(view, completions, words, sublime.Region(start, call_end))

    if lookup_arguments:
      arg_end = _get_argument_end(view, match)
      if arg_end != None:
        _append_region(view, completions, words, sublime.Region(start, arg_end))

  return completions
  def _get_value(self, view, sel):
    nesting = expression.get_nesting(view, sel.begin(), 2048)
    if nesting == None:
      return None

    start, end = nesting[0], nesting[1]

    if sel.begin() != sel.end():
      nesting_end = expression.get_nesting(view, sel.begin(), 2048)
      if nesting_end == None:
        return None

      end_start, end_end = nesting_end[0], nesting_end[1]
      if end_start != start and end_end != end:
        return None

    previous_character = view.substr(sublime.Region(start - 2, start - 1))
    return re.search(r'[\w?!]', previous_character) != None
Ejemplo n.º 10
0
def _get_call_end(view, match):
  _, point, _, _ = expression.lookup(view, match.end(1), r'^\s*([\(\[])',
    {'nesting': True, 'range': [0, 52], 'limit': 1})

  if point == None:
    return

  nesting = expression.get_nesting(view, point + 1, [1, 1024 * 5])
  if nesting == None:
    return None

  return nesting[1] + 1
Ejemplo n.º 11
0
def _get_call_end(view, match):
    _, point, _, _ = expression.lookup(view, match.end(1), r'^\s*([\(\[])', {
        'nesting': True,
        'range': [0, 52],
        'limit': 1
    })

    if point == None:
        return

    nesting = expression.get_nesting(view, point + 1, [1, 1024 * 5])
    if nesting == None:
        return None

    return nesting[1] + 1
Ejemplo n.º 12
0
  def _check_point(self, view, sel):
    nesting = expression.get_nesting(view, sel.a, None, {}, r'[\(]')
    if nesting != None:
      return False

    point = sel.a

    point = indentation_navigation.get_point(view, point, alignment = "left",
      backward = True, before = 1, type = "lesser")
    if point == 0:
      text = view.substr(view.line(0))
    else:
      text = view.substr(sublime.Region(max(point - 512, 0), point))

    expr = r'(\n|^)\s*(struct|class|module|interface|trait).*(\s*\{\s*)?$'
    return re.search(expr, text, re.IGNORECASE) != None
def is_unidented_arguments(view, text, line):
  point = view.text_point(line, 0)
  nesting = expression.get_nesting(view, point, 512)
  if nesting == None:
    return False

  start = view.substr(view.line(nesting[0]))
  start_spaces = re.search(r'^(\s*)', start).group(1)

  end = view.substr(view.line(nesting[1]))
  end_spaces = re.search(r'^(\s*)', end).group(1)

  if start_spaces == end_spaces:
    return False

  return statement.is_arguments(view, point)
Ejemplo n.º 14
0
def get_root_statement(view, point):
  current = get_statement(view, point)
  if current == None:
    return

  while True:
    nesting = expression.get_nesting(view, current[0], RANGE, {})
    if nesting == None:
      return current

    if not is_arguments(view, current[0], nesting):
      return current

    new = get_statement(view, nesting[0] - 1)
    if new == None:
      return current

    current = new
Ejemplo n.º 15
0
def is_arguments(view, point, nesting = None):
  if nesting == None:
    nesting = expression.get_nesting(view, point, RANGE)

  if nesting == None:
    return False

  nesting_opener_region = sublime.Region(nesting[0] - 1, nesting[0])
  nesting_opener = view.substr(nesting_opener_region)
  if nesting_opener == '[':
    return True

  preceding_64_bytes_region = sublime.Region(nesting[0] - 65, nesting[0] - 1)
  preceding_64_bytes = view.substr(preceding_64_bytes_region)

  if nesting_opener == '(':
    return re.search(r'\w\s*$', preceding_64_bytes) != None
  elif nesting_opener == '{':
    return re.search(r'[\w)]\s*$', preceding_64_bytes) == None
  else:
    raise Exception('Unknown nesting opener "' + nesting_opener + '"')

  return False
Ejemplo n.º 16
0
def _get_argument_end(view, match):
  nesting = expression.get_nesting(view, match.start(1) + 1, 1024 * 5)
  if nesting == None:
    return None

  return nesting[1]
Ejemplo n.º 17
0
def _get_argument_end(view, match):
    nesting = expression.get_nesting(view, match.start(1) + 1, 1024 * 5)
    if nesting == None:
        return None

    return nesting[1]