def MakeSignature(s): label = s.description + '( ' parameters = [] for index, p in enumerate(s.params): # We remove 'param ' from the start of each parameter (hence the 6:) param = p.description[6:] start = len(label) end = start + len(param) label += param if index < len(s.params) - 1: label += ', ' parameters.append({ 'label': [ CodepointOffsetToByteOffset(label, start), CodepointOffsetToByteOffset(label, end) ] }) label += ' )' return { 'label': label, 'parameters': parameters, }
def MakeSignature(s): sig_label = s['Label'] end = 0 parameters = [] for arg in s['Parameters']: arg_label = arg['Label'] begin = sig_label.find(arg_label, end) end = begin + len(arg_label) parameters.append({ 'label': [ CodepointOffsetToByteOffset(sig_label, begin), CodepointOffsetToByteOffset(sig_label, end) ] }) return {'label': sig_label, 'parameters': parameters}
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 _IndexToLineColumn( text, index ): """Get 1-based (line_number, col) of `index` in `string`, where string is a unicode string and col is a byte offset.""" lines = text.splitlines( True ) curr_pos = 0 for linenum, line in enumerate( lines ): if curr_pos + len( line ) > index: return ( linenum + 1, CodepointOffsetToByteOffset( line, index - curr_pos + 1 ) ) curr_pos += len( line ) assert False
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 = GetFileLines(request_data, filename) line_value = contents[line_num - 1] return responses.Location( line_num, CodepointOffsetToByteOffset(line_value, column_num), filename)
def CompletionStartColumn(line_value, column_num, filetype): """Returns the 1-based byte index where the completion query should start. So if the user enters: foo.bar^ with the cursor being at the location of the caret (so the character *AFTER* 'r'), then the starting column would be the index of the letter 'b'. NOTE: if the line contains multi-byte characters, then the result is not the 'character' index (see CompletionStartCodepoint for that), and therefore it is not safe to perform any character-relevant arithmetic on the result of this method.""" return CodepointOffsetToByteOffset( ToUnicode(line_value), CompletionStartCodepoint(line_value, column_num, filetype))
def _SetCompletionStartCodepoint(self, codepoint_offset): self._cached_computed['start_codepoint'] = codepoint_offset # Note: We must pre-compute (and cache) the byte equivalent. This is because # the value calculated by the getter (_GetCompletionStartColumn) would be # based on self[ 'column_num' ], which would be incorrect; it does not know # that the user has forced this value to be independent of the column. self._cached_computed['start_column'] = CodepointOffsetToByteOffset( self['line_value'], codepoint_offset) # The same applies to the 'query' (the bit after the start column up to the # cursor column). It's dependent on the 'start_codepoint' so we must reset # it. self._cached_computed.pop('query', None)
def _OffsetToPosition(offset, filename, text, newlines): """Convert the 0-based codepoint offset |offset| to a position (line/col) in |text|. |filename| is the full path of the file containing |text| and |newlines| is a cache of the 0-based character offsets of all the \n characters in |text| (plus one extra). Returns responses.Position.""" for index, newline in enumerate(newlines): if newline >= offset: start_of_line = newlines[index - 1] + 1 if index > 0 else 0 column = offset - start_of_line line_value = text[start_of_line:newline] return responses.Location( index + 1, CodepointOffsetToByteOffset(line_value, column + 1), filename) # Invalid position - it's outside of the text. Just return the last # position in the text. This is an internal error. LOGGER.error("Invalid offset %s in file %s with text %s and newlines %s", offset, filename, text, newlines) raise RuntimeError("Invalid file offset in diff")