responses.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. import os
  20. YCM_EXTRA_CONF_FILENAME = '.ycm_extra_conf.py'
  21. CONFIRM_CONF_FILE_MESSAGE = ('Found {0}. Load? \n\n(Question can be turned '
  22. 'off with options, see YCM docs)')
  23. NO_EXTRA_CONF_FILENAME_MESSAGE = ( 'No {0} file detected, so no compile flags '
  24. 'are available. Thus no semantic support for C/C++/ObjC/ObjC++. Go READ THE '
  25. 'DOCS *NOW*, DON\'T file a bug report.' ).format( YCM_EXTRA_CONF_FILENAME )
  26. NO_DIAGNOSTIC_SUPPORT_MESSAGE = ( 'YCM has no diagnostics support for this '
  27. 'filetype; refer to Syntastic docs if using Syntastic.')
  28. class ServerError( Exception ):
  29. def __init__( self, message ):
  30. super( ServerError, self ).__init__( message )
  31. class UnknownExtraConf( ServerError ):
  32. def __init__( self, extra_conf_file ):
  33. message = CONFIRM_CONF_FILE_MESSAGE.format( extra_conf_file )
  34. super( UnknownExtraConf, self ).__init__( message )
  35. self.extra_conf_file = extra_conf_file
  36. class NoExtraConfDetected( ServerError ):
  37. def __init__( self ):
  38. super( NoExtraConfDetected, self ).__init__(
  39. NO_EXTRA_CONF_FILENAME_MESSAGE )
  40. class NoDiagnosticSupport( ServerError ):
  41. def __init__( self ):
  42. super( NoDiagnosticSupport, self ).__init__( NO_DIAGNOSTIC_SUPPORT_MESSAGE )
  43. def BuildGoToResponse( filepath, line_num, column_num, description = None ):
  44. response = {
  45. 'filepath': os.path.realpath( filepath ),
  46. 'line_num': line_num,
  47. 'column_num': column_num
  48. }
  49. if description:
  50. response[ 'description' ] = description
  51. return response
  52. def BuildDescriptionOnlyGoToResponse( text ):
  53. return {
  54. 'description': text,
  55. }
  56. # TODO: Look at all the callers and ensure they are not using this instead of an
  57. # exception.
  58. def BuildDisplayMessageResponse( text ):
  59. return {
  60. 'message': text
  61. }
  62. def BuildCompletionData( insertion_text,
  63. extra_menu_info = None,
  64. detailed_info = None,
  65. menu_text = None,
  66. kind = None ):
  67. completion_data = {
  68. 'insertion_text': insertion_text
  69. }
  70. if extra_menu_info:
  71. completion_data[ 'extra_menu_info' ] = extra_menu_info
  72. if menu_text:
  73. completion_data[ 'menu_text' ] = menu_text
  74. if detailed_info:
  75. completion_data[ 'detailed_info' ] = detailed_info
  76. if kind:
  77. completion_data[ 'kind' ] = kind
  78. return completion_data
  79. def BuildDiagnosticData( diagnostic ):
  80. def BuildRangeData( source_range ):
  81. return {
  82. 'start': BuildLocationData( source_range.start_ ),
  83. 'end': BuildLocationData( source_range.end_ ),
  84. }
  85. def BuildLocationData( location ):
  86. return {
  87. 'line_num': location.line_number_ - 1,
  88. 'column_num': location.column_number_ - 1,
  89. 'filepath': location.filename_,
  90. }
  91. return {
  92. 'ranges': [ BuildRangeData( x ) for x in diagnostic.ranges_ ],
  93. 'location': BuildLocationData( diagnostic.location_ ),
  94. 'location_extent': BuildRangeData( diagnostic.location_extent_ ),
  95. 'text': diagnostic.text_,
  96. 'kind': diagnostic.kind_
  97. }
  98. def BuildExceptionResponse( exception, traceback ):
  99. return {
  100. 'exception': exception,
  101. 'message': str( exception ),
  102. 'traceback': traceback
  103. }