LineEditor.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===-- llvm/LineEditor/LineEditor.h - line editor --------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_LINEEDITOR_LINEEDITOR_H
  10. #define LLVM_LINEEDITOR_LINEEDITOR_H
  11. #include "llvm/ADT/Optional.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include <cstdio>
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. namespace llvm {
  18. class LineEditor {
  19. public:
  20. /// Create a LineEditor object.
  21. ///
  22. /// \param ProgName The name of the current program. Used to form a default
  23. /// prompt.
  24. /// \param HistoryPath Path to the file in which to store history data, if
  25. /// possible.
  26. /// \param In The input stream used by the editor.
  27. /// \param Out The output stream used by the editor.
  28. /// \param Err The error stream used by the editor.
  29. LineEditor(StringRef ProgName, StringRef HistoryPath = "", FILE *In = stdin,
  30. FILE *Out = stdout, FILE *Err = stderr);
  31. ~LineEditor();
  32. /// Reads a line.
  33. ///
  34. /// \return The line, or llvm::Optional<std::string>() on EOF.
  35. llvm::Optional<std::string> readLine() const;
  36. void saveHistory();
  37. void loadHistory();
  38. static std::string getDefaultHistoryPath(StringRef ProgName);
  39. /// The action to perform upon a completion request.
  40. struct CompletionAction {
  41. enum ActionKind {
  42. /// Insert Text at the cursor position.
  43. AK_Insert,
  44. /// Show Completions, or beep if the list is empty.
  45. AK_ShowCompletions
  46. };
  47. ActionKind Kind;
  48. /// The text to insert.
  49. std::string Text;
  50. /// The list of completions to show.
  51. std::vector<std::string> Completions;
  52. };
  53. /// A possible completion at a given cursor position.
  54. struct Completion {
  55. Completion() {}
  56. Completion(const std::string &TypedText, const std::string &DisplayText)
  57. : TypedText(TypedText), DisplayText(DisplayText) {}
  58. /// The text to insert. If the user has already input some of the
  59. /// completion, this should only include the rest of the text.
  60. std::string TypedText;
  61. /// A description of this completion. This may be the completion itself, or
  62. /// maybe a summary of its type or arguments.
  63. std::string DisplayText;
  64. };
  65. /// Set the completer for this LineEditor. A completer is a function object
  66. /// which takes arguments of type StringRef (the string to complete) and
  67. /// size_t (the zero-based cursor position in the StringRef) and returns a
  68. /// CompletionAction.
  69. template <typename T> void setCompleter(T Comp) {
  70. Completer.reset(new CompleterModel<T>(Comp));
  71. }
  72. /// Set the completer for this LineEditor to the given list completer.
  73. /// A list completer is a function object which takes arguments of type
  74. /// StringRef (the string to complete) and size_t (the zero-based cursor
  75. /// position in the StringRef) and returns a std::vector<Completion>.
  76. template <typename T> void setListCompleter(T Comp) {
  77. Completer.reset(new ListCompleterModel<T>(Comp));
  78. }
  79. /// Use the current completer to produce a CompletionAction for the given
  80. /// completion request. If the current completer is a list completer, this
  81. /// will return an AK_Insert CompletionAction if each completion has a common
  82. /// prefix, or an AK_ShowCompletions CompletionAction otherwise.
  83. ///
  84. /// \param Buffer The string to complete
  85. /// \param Pos The zero-based cursor position in the StringRef
  86. CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const;
  87. const std::string &getPrompt() const { return Prompt; }
  88. void setPrompt(const std::string &P) { Prompt = P; }
  89. // Public so callbacks in LineEditor.cpp can use it.
  90. struct InternalData;
  91. private:
  92. std::string Prompt;
  93. std::string HistoryPath;
  94. std::unique_ptr<InternalData> Data;
  95. struct CompleterConcept {
  96. virtual ~CompleterConcept();
  97. virtual CompletionAction complete(StringRef Buffer, size_t Pos) const = 0;
  98. };
  99. struct ListCompleterConcept : CompleterConcept {
  100. ~ListCompleterConcept() override;
  101. CompletionAction complete(StringRef Buffer, size_t Pos) const override;
  102. static std::string getCommonPrefix(const std::vector<Completion> &Comps);
  103. virtual std::vector<Completion> getCompletions(StringRef Buffer,
  104. size_t Pos) const = 0;
  105. };
  106. template <typename T>
  107. struct CompleterModel : CompleterConcept {
  108. CompleterModel(T Value) : Value(Value) {}
  109. CompletionAction complete(StringRef Buffer, size_t Pos) const override {
  110. return Value(Buffer, Pos);
  111. }
  112. T Value;
  113. };
  114. template <typename T>
  115. struct ListCompleterModel : ListCompleterConcept {
  116. ListCompleterModel(T Value) : Value(Value) {}
  117. std::vector<Completion> getCompletions(StringRef Buffer,
  118. size_t Pos) const override {
  119. return Value(Buffer, Pos);
  120. }
  121. T Value;
  122. };
  123. std::unique_ptr<const CompleterConcept> Completer;
  124. };
  125. }
  126. #endif