def _BuildLocation( request_data, filename, line_num, column_num ): contents = utils.SplitLines( GetFileContents( request_data, filename ) ) line_value = contents[ line_num - 1 ] return responses.Location( line_num, CodepointOffsetToByteOffset( line_value, column_num ), filename )
def BuildFixItChunk(change): filepath = self._ServerPathToAbsolute(change['file']) file_contents = utils.SplitLines( GetFileContents(request_data, filepath)) return responses.FixItChunk( change['text'], BuildRange(file_contents, filepath, change['start'], change['end']))
def BuildFixItChunk(change): filename = os.path.abspath(change['file']) file_contents = utils.SplitLines( GetFileContents(request_data, filename)) return responses.FixItChunk( change['text'], BuildRange(file_contents, filename, change['start'], change['end']))
def BuildRefResponse( ref ): filepath = self._ServerPathToAbsolute( ref[ 'file' ] ) return responses.BuildGoToResponseFromLocation( _BuildLocation( utils.SplitLines( GetFileContents( request_data, filepath ) ), filepath, ref[ 'start' ][ 'line' ], ref[ 'start' ][ 'ch' ] ) )
def _BuildFixItChunksForFile( request_data, new_name, file_replacement ): """ returns a list of FixItChunk for each replacement range for the supplied file""" # On windows, tsserver annoyingly returns file path as C:/blah/blah, # whereas all other paths in Python are of the C:\\blah\\blah form. We use # normpath to have python do the conversion for us. file_path = os.path.normpath( file_replacement[ 'file' ] ) file_contents = utils.SplitLines( GetFileContents( request_data, file_path ) ) return [ _BuildFixItChunkForRange( new_name, file_contents, file_path, r ) for r in file_replacement[ 'locs' ] ]
def _BuildLocation(request_data, filename, line_num, column_num): if line_num <= 0: return None # OmniSharp sometimes incorrectly returns 0 for the column number. Assume the # column is 1 in that case. if column_num <= 0: column_num = 1 contents = utils.SplitLines(GetFileContents(request_data, filename)) line_value = contents[line_num - 1] return responses.Location( line_num, CodepointOffsetToByteOffset(line_value, column_num), filename)
def _GoToDefinition(self, request_data): query = { 'type': 'definition', } response = self._GetResponse(query, request_data['column_codepoint'], request_data) filepath = self._ServerPathToAbsolute(response['file']) return responses.BuildGoToResponseFromLocation( _BuildLocation( utils.SplitLines(GetFileContents(request_data, filepath)), filepath, response['start']['line'], response['start']['ch']))
def _GoToReferences(self, request_data): self._Reload(request_data) response = self._SendRequest( 'references', { 'file': request_data['filepath'], 'line': request_data['line_num'], 'offset': request_data['column_codepoint'] }) return [ responses.BuildGoToResponseFromLocation( _BuildLocation( utils.SplitLines(GetFileContents(request_data, ref['file'])), ref['file'], ref['start']['line'], ref['start']['offset']), ref['lineText']) for ref in response['refs'] ]
def _GoToReferences(self, request_data): query = { 'type': 'refs', } response = self._GetResponse(query, request_data['column_codepoint'], request_data) return [ responses.BuildGoToResponseFromLocation( _BuildLocation( utils.SplitLines(GetFileContents(request_data, ref['file'])), ref['file'], ref['start']['line'], ref['start']['ch'])) for ref in response['refs'] ]
def _GoToDefinition( self, request_data ): self._Reload( request_data ) try: filespans = self._SendRequest( 'definition', { 'file': request_data[ 'filepath' ], 'line': request_data[ 'line_num' ], 'offset': request_data[ 'column_codepoint' ] } ) span = filespans[ 0 ] return responses.BuildGoToResponseFromLocation( _BuildLocation( utils.SplitLines( GetFileContents( request_data, span[ 'file' ] ) ), span[ 'file' ], span[ 'start' ][ 'line' ], span[ 'start' ][ 'offset' ] ) ) except RuntimeError: raise RuntimeError( 'Could not find definition' )
def SplitLines_test(): # Tuples of ( input, expected_output ) for utils.SplitLines. tests = [ ('', ['']), (' ', [' ']), ('\n', ['', '']), (' \n', [' ', '']), (' \n ', [' ', ' ']), ('test\n', ['test', '']), ('\r', ['', '']), ('\r ', ['', ' ']), ('test\r', ['test', '']), ('\n\r', ['', '', '']), ('\r\n', ['', '']), ('\r\n\n', ['', '', '']), # Other behaviors are just the behavior of splitlines, so just a couple of # tests to prove that we don't mangle it. ('test\ntesting', ['test', 'testing']), ('\ntesting', ['', 'testing']), ] for test in tests: yield lambda: eq_(utils.SplitLines(test[0]), test[1])
def test_SplitLines(self): test_cases = [ ('', ['']), (' ', [' ']), ('\n', ['', '']), (' \n', [' ', '']), (' \n ', [' ', ' ']), ('test\n', ['test', '']), # Ignore \r on purpose. ('\r', ['\r']), ('\r ', ['\r ']), ('test\r', ['test\r']), ('\n\r', ['', '\r']), ('\r\n', ['\r', '']), ('\r\n\n', ['\r', '', '']), ('test\ntesting', ['test', 'testing']), ('\ntesting', ['', 'testing']), # Do not split lines on \f and \v characters. ('\f\n\v', ['\f', '\v']) ] for lines, expected in test_cases: with self.subTest(lines=lines, expected=expected): assert_that(utils.SplitLines(lines), expected)
def SplitLines_test(): # Tuples of ( input, expected_output ) for utils.SplitLines. tests = [ ('', ['']), (' ', [' ']), ('\n', ['', '']), (' \n', [' ', '']), (' \n ', [' ', ' ']), ('test\n', ['test', '']), # Ignore \r on purpose. ('\r', ['\r']), ('\r ', ['\r ']), ('test\r', ['test\r']), ('\n\r', ['', '\r']), ('\r\n', ['\r', '']), ('\r\n\n', ['\r', '', '']), ('test\ntesting', ['test', 'testing']), ('\ntesting', ['', 'testing']), # Do not split lines on \f and \v characters. ('\f\n\v', ['\f', '\v']) ] for test in tests: yield lambda: eq_(utils.SplitLines(test[0]), test[1])
def _SwiftDiagnosticToYcmdDiagnostic(filename, request_data, swift_diagnostic): line = swift_diagnostic.line contents = request_data['file_data'][filename]['contents'] diagnostics = [] # In stages other than source.diagnostic.stage.swift.parse # the file is included in offsets. This is probably a bug # in swift stage = swift_diagnostic.diagnostic_stage needs_offset_adjust = stage != DIAGNOSTIC_PHASE_PARSE split_lines = utils.SplitLines(contents) if needs_offset_adjust: line = line - (len(split_lines) - 1) diagnostic = _SwiftDiagnosticWithColumnToYcmdDiagnostic( filename, split_lines, swift_diagnostic, line) diagnostics.append(diagnostic) child_diagnostics = [ _SwiftDiagnosticWithColumnToYcmdDiagnostic(filename, split_lines, swift_diag, line) for swift_diag in swift_diagnostic.diagnostics ] diagnostics.extend(child_diagnostics) return diagnostics
def SplitLines_test(lines, expected): assert_that(utils.SplitLines(lines), expected)