text_properties.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2020, YouCompleteMe Contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from ycm import vimsupport
  18. from ycm.vimsupport import GetIntValue
  19. from ycmd import utils
  20. import vim
  21. import json
  22. import typing
  23. # FIXME/TODO: Merge this with vimsupport funcitons, added after these were
  24. # written. It's not trivial, as those vimsupport functions are a bit fiddly.
  25. # They also support neovim, but we don't.
  26. def AddTextPropertyType( name, **kwargs ):
  27. props = {
  28. 'highlight': 'Ignore',
  29. 'combine': 0,
  30. 'override': 0,
  31. 'start_incl': 0,
  32. 'end_incl': 0,
  33. 'priority': 10
  34. }
  35. props.update( kwargs )
  36. vim.eval( f"prop_type_add( '{ vimsupport.EscapeForVim( name ) }', "
  37. f" { json.dumps( props ) } )" )
  38. def GetTextPropertyTypes( *args, **kwargs ):
  39. return [ utils.ToUnicode( p ) for p in vim.eval( 'prop_type_list()' ) ]
  40. def AddTextProperty( bufnr,
  41. prop_id,
  42. prop_type,
  43. range,
  44. extra_args: dict = None ):
  45. props = {
  46. 'bufnr': bufnr,
  47. 'type': prop_type
  48. }
  49. if prop_id is not None:
  50. props[ 'id' ] = prop_id
  51. if extra_args:
  52. props.update( extra_args )
  53. if 'end' in range:
  54. props.update( {
  55. 'end_lnum': range[ 'end' ][ 'line_num' ],
  56. 'end_col': range[ 'end' ][ 'column_num' ],
  57. } )
  58. return vim.eval( f"prop_add( { range[ 'start' ][ 'line_num' ] },"
  59. f" { range[ 'start' ][ 'column_num' ] },"
  60. f" { json.dumps( props ) } )" )
  61. def ClearTextProperties(
  62. bufnr,
  63. prop_id = None,
  64. prop_types: typing.Union[ typing.List[ str ], str ] = None,
  65. first_line = None,
  66. last_line = None ):
  67. props = {
  68. 'bufnr': bufnr,
  69. 'all': 1,
  70. }
  71. if prop_id is not None:
  72. props[ 'id' ] = prop_id
  73. if prop_id is not None and prop_types is not None:
  74. props[ 'both' ] = 1
  75. def prop_remove():
  76. if last_line is not None:
  77. return GetIntValue( f"prop_remove( { json.dumps( props ) },"
  78. f" { first_line },"
  79. f" { last_line } )" )
  80. elif first_line is not None:
  81. return GetIntValue( f"prop_remove( { json.dumps( props ) },"
  82. f" { first_line } )" )
  83. else:
  84. return GetIntValue( f"prop_remove( { json.dumps( props ) } )" )
  85. if prop_types is None:
  86. return prop_remove()
  87. if not isinstance( prop_types, list ):
  88. prop_types = [ prop_types ]
  89. # 9.0.233 added types list to prop_remove, so use that
  90. if vimsupport.VimVersionAtLeast( '9.0.233' ):
  91. props[ 'types' ] = prop_types
  92. return prop_remove()
  93. # Older versions we have to run prop_remove for each type
  94. removed = 0
  95. for prop_type in prop_types:
  96. props[ 'type' ] = prop_type
  97. removed += prop_remove()
  98. return removed