completion_string_illustration.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env ruby
  2. require "clangc"
  3. source_file = "#{File.expand_path(File.dirname(__FILE__))}/completion_string.cpp"
  4. clang_headers_path = Dir.glob("/usr/lib/clang/*/include").collect {|x| "-I#{x}"}
  5. index = Clangc::Index.new(false, true)
  6. tu = index.create_translation_unit_from_source_file(source_file,
  7. ['-x'] +['c++'] +
  8. clang_headers_path)
  9. exit unless tu
  10. def cursor_kind_spelling(cursor)
  11. default = "unexposed_decl"
  12. Clangc::CursorKind.constants.each do |c|
  13. if Clangc::CursorKind.const_get(c) == cursor.kind
  14. default = c.to_s.downcase
  15. else
  16. next
  17. end
  18. end
  19. default
  20. end
  21. def display_cursor_info(cursor)
  22. code = cursor.spelling
  23. kind = cursor_kind_spelling(cursor)
  24. "Cursor spelling : #{code} - kind : #{kind}"
  25. end
  26. def completion_string_availability(cs)
  27. default = "not_available"
  28. Clangc::AvailabilityKind.constants.each do |c|
  29. if Clangc::AvailabilityKind.const_get(c) == cs.availability
  30. default = c.to_s.downcase
  31. else
  32. next
  33. end
  34. end
  35. default
  36. end
  37. def display_completion_string_info(cs, margin = "")
  38. availability = completion_string_availability(cs)
  39. priority = cs.priority
  40. chunks = ""
  41. (0..(cs.num_chunks - 1)).each do |i|
  42. chunks += (margin + display_chunk_info(cs, i, margin + "\t"))
  43. end
  44. "#{margin}Completion string : \n\
  45. #{margin}availability : #{availability}\n\
  46. #{margin}priority : #{priority}\n\
  47. #{margin}num chunks : #{cs.num_chunks}\n\
  48. #{chunks}"
  49. end
  50. def chunk_kind_string(kind)
  51. default = "not_available"
  52. Clangc::CompletionChunkKind.constants.each do |c|
  53. if Clangc::CompletionChunkKind.const_get(c) == kind
  54. default = c.to_s.downcase
  55. else
  56. next
  57. end
  58. end
  59. default
  60. end
  61. def display_chunk_info(cs, index, margin = "")
  62. kind = chunk_kind_string(cs.chunk_kind(index))
  63. text = cs.chunk_text(index)
  64. if cs.chunk_kind(index) == Clangc::CompletionChunkKind::OPTIONAL
  65. completion_string = display_completion_string_info(cs.chunk_completion_string(index), margin + "\t")
  66. end
  67. " Chunk infos: \n\
  68. #{margin}kind #{kind}\n\
  69. #{margin}text #{text}\n\
  70. #{completion_string}"
  71. end
  72. Clangc.visit_children(cursor: tu.cursor) do |cursor, parent|
  73. if cursor.location.spelling[0].name == source_file
  74. puts display_cursor_info(cursor)
  75. completion_string = cursor.completion_string
  76. puts display_completion_string_info(completion_string, margin = "\t") if completion_string
  77. end
  78. Clangc::ChildVisitResult::RECURSE
  79. end