def BuildRequestData( buffer_number = None ):
  """Build request for the current buffer or the buffer with number
  |buffer_number| if specified."""
  working_dir = GetCurrentDirectory()
  current_buffer = vim.current.buffer

  if buffer_number and current_buffer.number != buffer_number:
    # Cursor position is irrelevant when filepath is not the current buffer.
    buffer_object = vim.buffers[ buffer_number ]
    filepath = vimsupport.GetBufferFilepath( buffer_object )
    return {
      'filepath': filepath,
      'line_num': 1,
      'column_num': 1,
      'working_dir': working_dir,
      'file_data': vimsupport.GetUnsavedAndSpecifiedBufferData( buffer_object,
                                                                filepath )
    }

  current_filepath = vimsupport.GetBufferFilepath( current_buffer )
  line, column = vimsupport.CurrentLineAndColumn()

  return {
    'filepath': current_filepath,
    'line_num': line + 1,
    'column_num': column + 1,
    'working_dir': working_dir,
    'file_data': vimsupport.GetUnsavedAndSpecifiedBufferData( current_buffer,
                                                              current_filepath )
  }
示例#2
0
def BuildRequestData(filepath=None):
    """Build request for the current buffer or the buffer corresponding to
  |filepath| if specified."""
    current_filepath = vimsupport.GetCurrentBufferFilepath()
    working_dir = GetCurrentDirectory()

    if filepath and current_filepath != filepath:
        # Cursor position is irrelevant when filepath is not the current buffer.
        return {
            'filepath': filepath,
            'line_num': 1,
            'column_num': 1,
            'working_dir': working_dir,
            'file_data': vimsupport.GetUnsavedAndSpecifiedBufferData(filepath)
        }

    line, column = vimsupport.CurrentLineAndColumn()

    return {
        'filepath': current_filepath,
        'line_num': line + 1,
        'column_num': column + 1,
        'working_dir': working_dir,
        'file_data':
        vimsupport.GetUnsavedAndSpecifiedBufferData(current_filepath)
    }
示例#3
0
def CurrentWorkingDirectory(path):
    old_cwd = GetCurrentDirectory()
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(old_cwd)
示例#4
0
 def GetWorkingDirectory(self, request_data):
     if self.user_options['filepath_completion_use_working_dir']:
         # Return paths relative to the working directory of the client, if
         # supplied, otherwise relative to the current working directory of this
         # process.
         return request_data.get('working_dir') or GetCurrentDirectory()
     # Return paths relative to the file.
     return os.path.dirname(request_data['filepath'])
def WorkingDir_UseFilePath_test(app):
    ok_(GetCurrentDirectory() != DATA_DIR, ('Please run this test from a '
                                            'different directory'))

    completer = FilenameCompleter(user_options_store.GetAll())

    data = _CompletionResultsForLine(completer, 'ls ./include/')
    assert_that(data, contains_inanyorder(('Qt', '[Dir]'), ('QtGui', '[Dir]')))
示例#6
0
def WorkingDir_UseFilePath_test(app):
    ok_(GetCurrentDirectory() != DATA_DIR, ('Please run this test from a '
                                            'different directory'))

    completer = FilenameCompleter(user_options_store.GetAll())

    data = sorted(_CompletionResultsForLine(completer, 'ls ./include/'))
    eq_([('Qt', '[Dir]'), ('QtGui', '[Dir]')], data)
示例#7
0
def GetBufferFilepath(buffer_object):
    if buffer_object.name:
        return os.path.abspath(ToUnicode(buffer_object.name))
    # Buffers that have just been created by a command like :enew don't have any
    # buffer name so we use the buffer number for that.
    name = os.path.join(GetCurrentDirectory(), str(buffer_object.number))
    MADEUP_FILENAME_TO_BUFFER_NUMBER[name] = buffer_object.number
    return name
示例#8
0
def GetBufferFilepath(buffer_object):
    buffer_name = buffer_object.name
    if buffer_name:
        return (os.path.abspath(ToUnicode(buffer_name))
                if not IsJdtUri(buffer_name) else buffer_name)
    # Buffers that have just been created by a command like :enew don't have any
    # buffer name so we use the buffer number for that.
    return os.path.join(GetCurrentDirectory(), str(buffer_object.number))
def WorkingDir_UseFilePath_test():
    ok_(GetCurrentDirectory() != DATA_DIR, ('Please run this test from a '
                                            'different directory'))

    with UserOption('filepath_completion_use_working_dir', 0) as options:
        completer = FilenameCompleter(options)

        data = sorted(_CompletionResultsForLine(completer, 'ls ./include/'))
        eq_([('Qt', '[Dir]'), ('QtGui', '[Dir]')], data)
示例#10
0
def setUpPackage():
    """Initializes the ycmd server as a WebTest application that will be shared
  by all tests using the SharedYcmd decorator in this package. Additional
  configuration that is common to these tests, like starting a semantic
  subserver, should be done here."""
    global shared_app, shared_current_dir

    shared_app = SetUpApp()
    shared_current_dir = GetCurrentDirectory()
    os.chdir(PathToTestFile())
    WaitUntilCompleterServerReady(shared_app, 'javascript')
def WorkingDir_UseClientWorkingDirectory_test(app):
    test_dir = os.path.join(DATA_DIR, 'include')
    ok_(GetCurrentDirectory() != test_dir, ('Please run this test from a '
                                            'different directory'))

    completer = FilenameCompleter(user_options_store.GetAll())

    # We supply working_dir in the request, so we expect results to be
    # relative to the supplied path.
    data = _CompletionResultsForLine(completer, 'ls ./',
                                     {'working_dir': test_dir})
    assert_that(data, contains_inanyorder(('Qt', '[Dir]'), ('QtGui', '[Dir]')))
示例#12
0
    def test_WorkingDir_UseFilePath(self, app):
        assert_that(GetCurrentDirectory() != DATA_DIR,
                    'Please run this test from a different directory')

        completion_data = BuildRequest(contents='ls ./dir with spaces (x64)/',
                                       filepath=PATH_TO_TEST_FILE,
                                       column_num=28)
        results = app.post_json('/completions',
                                completion_data).json['completions']
        assert_that(
            results,
            contains_inanyorder(CompletionEntryMatcher('Qt', '[Dir]'),
                                CompletionEntryMatcher('QtGui', '[Dir]')))
def WorkingDir_UseClientWorkingDirectory_test():
    test_dir = os.path.join(DATA_DIR, 'include')
    ok_(GetCurrentDirectory() != test_dir, ('Please run this test from a '
                                            'different directory'))

    with UserOption('filepath_completion_use_working_dir', 1) as options:
        completer = FilenameCompleter(options)

        # We supply working_dir in the request, so we expect results to be
        # relative to the supplied path.
        data = sorted(
            _CompletionResultsForLine(completer, 'ls ./',
                                      {'working_dir': test_dir}))
        eq_([('Qt', '[Dir]'), ('QtGui', '[Dir]')], data)
示例#14
0
    def test_WorkingDir_UseClientWorkingDirectory(self, app):
        test_dir = os.path.join(DATA_DIR, 'dir with spaces (x64)')
        assert_that(GetCurrentDirectory() != test_dir,
                    'Please run this test from a different directory')

        # We supply working_dir in the request, so we expect results to be
        # relative to the supplied path.
        completion_data = BuildRequest(contents='ls ./',
                                       filepath=PATH_TO_TEST_FILE,
                                       column_num=6,
                                       working_dir=test_dir)
        results = app.post_json('/completions',
                                completion_data).json['completions']
        assert_that(
            results,
            contains_inanyorder(CompletionEntryMatcher('Qt', '[Dir]'),
                                CompletionEntryMatcher('QtGui', '[Dir]')))
示例#15
0
def _GetAbsolutePathForCompletions(path_dir, use_working_dir, filepath,
                                   working_dir):
    """
  Returns the absolute path for which completion suggestions should be returned
  (in the standard case).
  """

    if os.path.isabs(path_dir):
        # This is already an absolute path, return it
        return path_dir
    elif use_working_dir:
        # Return paths relative to the working directory of the client, if
        # supplied, otherwise relative to the current working directory of this
        # process
        if working_dir:
            return os.path.join(working_dir, path_dir)
        else:
            return os.path.join(GetCurrentDirectory(), path_dir)
    else:
        # Return paths relative to the file
        return os.path.join(os.path.join(os.path.dirname(filepath)), path_dir)
示例#16
0
def GetBufferFilepath(buffer_object):
    if buffer_object.name:
        return buffer_object.name
    # Buffers that have just been created by a command like :enew don't have any
    # buffer name so we use the buffer number for that.
    return os.path.join(GetCurrentDirectory(), str(buffer_object.number))
def EventNotification_OnFileReadyToParse_NoProjectFile_test(app, *args):
    WaitUntilCompleterServerReady(app, 'javascript')

    # We raise an error if we can't detect a .tern-project file.
    # We only do this on the first OnFileReadyToParse event after a
    # server startup.
    os.chdir(PathToTestFile('..'))
    contents = ReadFile(PathToTestFile('simple_test.js'))

    response = app.post_json('/event_notification',
                             BuildRequest(event_name='FileReadyToParse',
                                          contents=contents,
                                          filetype='javascript'),
                             expect_errors=True)

    print('event response: {0}'.format(pformat(response.json)))

    eq_(response.status_code, requests.codes.internal_server_error)

    assert_that(
        response.json,
        ErrorMatcher(
            RuntimeError, 'Warning: Unable to detect a .tern-project file '
            'in the hierarchy before ' + GetCurrentDirectory() +
            ' and no global .tern-config file was found. '
            'This is required for accurate JavaScript '
            'completion. Please see the User Guide for '
            'details.'))

    # Check that a subsequent call does *not* raise the error

    response = app.post_json('/event_notification',
                             BuildRequest(event_name='FileReadyToParse',
                                          contents=contents,
                                          filetype='javascript'),
                             expect_errors=True)

    print('event response: {0}'.format(pformat(response.json)))

    eq_(response.status_code, requests.codes.ok)
    assert_that(response.json, empty())

    # Restart the server and check that it raises it again

    app.post_json(
        '/run_completer_command',
        BuildRequest(command_arguments=['RestartServer'],
                     filetype='javascript',
                     contents=contents,
                     completer_target='filetype_default'))

    WaitUntilCompleterServerReady(app, 'javascript')

    response = app.post_json('/event_notification',
                             BuildRequest(event_name='FileReadyToParse',
                                          contents=contents,
                                          filetype='javascript'),
                             expect_errors=True)

    print('event response: {0}'.format(pformat(response.json)))

    eq_(response.status_code, requests.codes.internal_server_error)

    assert_that(
        response.json,
        ErrorMatcher(
            RuntimeError, 'Warning: Unable to detect a .tern-project file '
            'in the hierarchy before ' + GetCurrentDirectory() +
            ' and no global .tern-config file was found. '
            'This is required for accurate JavaScript '
            'completion. Please see the User Guide for '
            'details.'))