def _ConstructGoToFromResponse(self, response_str): parsed = json.loads(ToUnicode(response_str)) if 'filename' in parsed and 'column' in parsed: return responses.BuildGoToResponse(parsed['filename'], int(parsed['line']), int(parsed['column'])) raise RuntimeError('Can\'t jump to definition.')
def _GoToDefinition(self, request_data): def find_end_of_command(line, match): if match is None: return -1 for i in range(match.start(), len(line)): e = line[i] if e == "}": return i return -1 line = utils.ToUnicode(request_data["line_value"]) match = self._ref_reg.search(line) end_of_command = find_end_of_command(line, match) if end_of_command == -1: raise RuntimeError( 'Can\'t jump to definition or declaration: not implemented yet' ) else: ref = line[match.end():end_of_command] if ref not in self._goto_labels: raise RuntimeError( 'Can\'t jump to definition or declaration: not implemented yet' ) filename, line, col = self._goto_labels[ref] return responses.BuildGoToResponse(filename, line, col)
def _CallHierarchy(self, request_data, args): self._Reload(request_data) response = self._SendRequest( f'provideCallHierarchy{ args[ 0 ] }Calls', { 'file': request_data['filepath'], 'line': request_data['line_num'], 'offset': request_data['column_codepoint'] }) goto_response = [] for hierarchy_item in response: description = hierarchy_item.get('from', hierarchy_item.get('to')) filepath = description['file'] start_position = hierarchy_item['fromSpans'][0]['start'] goto_line = start_position['line'] try: line_value = GetFileLines(request_data, filepath)[goto_line - 1] except IndexError: continue goto_column = utils.CodepointOffsetToByteOffset( line_value, start_position['offset']) goto_response.append( responses.BuildGoToResponse(filepath, goto_line, goto_column, description['name'])) if goto_response: return goto_response raise RuntimeError(f'No { args[ 0 ].lower() } calls found.')
def OnUserCommand(self, arguments, request_data): memfilepath, cfile, _ = _CreateTmpFile(request_data) completion = _GetCompletions(cfile, memfilepath, request_data['line_num'], request_data['column_num'], 'def') if not completion: raise ValueError("No idea") completion = completion[0].split('\t') if len(completion) < 6: raise ValueError("No such symbol") _, ctype, fullname, rtype, ffile, row, col, docstr = completion if not arguments: raise ValueError(self.UserCommandsHelpMessage()) elif arguments[0] == 'GetType': reply = '[' + TokenTypeMap.get(ctype, '') + '] (' + fullname + ')' if len(rtype) != 0: reply += ': ' + rtype + '\n--------------------------------------------------------------------------------\n' + FormatDocStr( docstr) return responses.BuildDisplayMessageResponse(ToUtf8IfNeeded(reply)) elif arguments[0] == 'GoTo': return responses.BuildGoToResponse( ToUtf8IfNeeded(ffile), int(row), int(col) + 1, ToUtf8IfNeeded(FormatDocStr(docstr))) else: raise RuntimeError(arguments)
def _GoToDefinition(self, request_data): try: definition = self._GetResponse('/find_definition', request_data) return responses.BuildGoToResponse(definition['file_path'], definition['line'], definition['column'] + 1) except Exception: raise RuntimeError('Can\'t jump to definition.')
def _GoToDefinition( self, request_data ): """ Jump to definition of identifier under cursor """ definition = self._GetResponse( '/gotodefinition', self._DefaultParameters( request_data ) ) if definition[ 'FileName' ] != None: return responses.BuildGoToResponse( definition[ 'FileName' ], definition[ 'Line' ], definition[ 'Column' ] ) else: raise RuntimeError( 'Can\'t jump to definition' )
def _GoToDefinition(self, request_data): query = { 'type': 'definition', } response = self._GetResponse(query, request_data) return responses.BuildGoToResponse(response['file'], response['start']['line'] + 1, response['start']['ch'] + 1)
def _GoToReferences( self, request_data ): query = { 'type': 'refs', } response = self._GetResponse( query, request_data ) return [ responses.BuildGoToResponse( ref[ 'file' ], ref[ 'start' ][ 'line' ] + 1, ref[ 'start' ][ 'ch' ] + 1 ) for ref in response[ 'refs' ] ]
def _BuildGoToResponse(self, definitions, request_data): if len(definitions) == 1: definition = definitions[0] column = 1 if definition.column is not None: column += definition.column filepath = definition.module_path or request_data['filepath'] return responses.BuildGoToResponse(filepath, definition.line, column) gotos = [] for definition in definitions: column = 1 if definition.column is not None: column += definition.column filepath = definition.module_path or request_data['filepath'] gotos.append( responses.BuildGoToResponse(filepath, definition.line, column, definition.description)) return gotos
def _BuildGoToResponse( self, definitions ): if len( definitions ) == 1: definition = definitions[ 0 ] if definition.in_builtin_module(): raise RuntimeError( 'Can\'t jump to builtin module.' ) return responses.BuildGoToResponse( definition.module_path, definition.line, definition.column + 1 ) gotos = [] for definition in definitions: if definition.in_builtin_module(): gotos.append( responses.BuildDescriptionOnlyGoToResponse( 'Builtin {}'.format( definition.description ) ) ) else: gotos.append( responses.BuildGoToResponse( definition.module_path, definition.line, definition.column + 1, definition.description ) ) return gotos
def _GoToImplementation( self, request_data, fallback_to_declaration ): """ Jump to implementation of identifier under cursor """ implementation = self._GetResponse( '/findimplementations', self._DefaultParameters( request_data ) ) if implementation[ 'QuickFixes' ]: if len( implementation[ 'QuickFixes' ] ) == 1: return responses.BuildGoToResponse( implementation[ 'QuickFixes' ][ 0 ][ 'FileName' ], implementation[ 'QuickFixes' ][ 0 ][ 'Line' ], implementation[ 'QuickFixes' ][ 0 ][ 'Column' ] ) else: return [ responses.BuildGoToResponse( x[ 'FileName' ], x[ 'Line' ], x[ 'Column' ] ) for x in implementation[ 'QuickFixes' ] ] else: if ( fallback_to_declaration ): return self._GoToDefinition( request_data ) elif implementation[ 'QuickFixes' ] == None: raise RuntimeError( 'Can\'t jump to implementation' ) else: raise RuntimeError( 'No implementations found' )
def _FindUsages(self, request_data): """ Show the usages of the identifier under the cursor """ usages = self._GetResponse('/findusages', self._DefaultParameters(request_data)) usagesResponse = [] for usage in usages['QuickFixes']: usageLocation = responses.BuildGoToResponse( str(usage['FileName']), usage['Line'], usage['Column'], str(usage['Text']).replace('\t', ' ')) usagesResponse.append(usageLocation) return usagesResponse
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_num'] }) return [ responses.BuildGoToResponse(filepath=ref['file'], line_num=ref['start']['line'], column_num=ref['start']['offset'], description=ref['lineText']) for ref in response['refs'] ]
def _GoToDefinition(self, request_data): self._Reload(request_data) filespans = self._SendRequest( 'definition', { 'file': request_data['filepath'], 'line': request_data['line_num'], 'offset': request_data['column_num'] }) if not filespans: raise RuntimeError('Could not find definition') span = filespans[0] return responses.BuildGoToResponse(filepath=span['file'], line_num=span['start']['line'], column_num=span['start']['offset'])
def _GoToDefinition(self, request_data): self._UpdateCurrentBuffer(request_data) col = request_data['column_num'] - 1 keyword = '' for it in re.finditer(r'\b[\w\.]+\b', request_data['line_value']): if it.start() <= col and it.end() > col: keyword = it.group(0) break (filepath, line, column) = self.daemon.find(keyword) if line == -1 and column == -1: raise RuntimeError('Definition for "%s" not found' % keyword) if line == -2: # reserved for quick return return if filepath == '' and line != -1 and column != -1: filepath = request_data['filepath'] if filepath.endswith('.oct') or filepath.endswith('.mex'): raise RuntimeError('"%s" is a shared module.' % keyword) elif os.path.isfile(filepath): line = 1 if line <= 0 else line column = 0 if column < 0 else column return responses.BuildGoToResponse(filepath, line, column) else: raise RuntimeError('Source for "%s" is unavailable.' % keyword)
def _ResponseForInclude(self, request_data): """Returns response for include file location if cursor is on the include statement, None otherwise. Throws RuntimeError if cursor is on include statement and corresponding include file not found.""" current_line = request_data['line_value'] include_file_name, quoted_include = GetFullIncludeValue(current_line) if not include_file_name: return None flags, current_file_path = self._FlagsForRequest(request_data) (quoted_include_paths, include_paths, framework_paths) = UserIncludePaths(flags, current_file_path) include_file_path = None if quoted_include: include_file_path = _GetAbsolutePath(include_file_name, quoted_include_paths) if not include_file_path: include_file_path = _GetAbsolutePath(include_file_name, include_paths) if not include_file_path and framework_paths: head, tail = PathLeftSplit(include_file_name) include_file_name = os.path.join(head + '.framework', 'Headers', tail) include_file_path = _GetAbsolutePath(include_file_name, framework_paths) if include_file_path: return responses.BuildGoToResponse(include_file_path, line_num=1, column_num=1) raise RuntimeError('Include file not found.')
def _ResponseForLocation(location): return responses.BuildGoToResponse(location.filename_, location.line_number_, location.column_number_)