reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
//===-- llvm/LineEditor/LineEditor.h - line editor --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LINEEDITOR_LINEEDITOR_H
#define LLVM_LINEEDITOR_LINEEDITOR_H

#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include <cstdio>
#include <memory>
#include <string>
#include <utility>
#include <vector>

namespace llvm {

class LineEditor {
public:
  /// Create a LineEditor object.
  ///
  /// \param ProgName The name of the current program. Used to form a default
  /// prompt.
  /// \param HistoryPath Path to the file in which to store history data, if
  /// possible.
  /// \param In The input stream used by the editor.
  /// \param Out The output stream used by the editor.
  /// \param Err The error stream used by the editor.
  LineEditor(StringRef ProgName, StringRef HistoryPath = "", FILE *In = stdin,
             FILE *Out = stdout, FILE *Err = stderr);
  ~LineEditor();

  /// Reads a line.
  ///
  /// \return The line, or llvm::Optional<std::string>() on EOF.
  llvm::Optional<std::string> readLine() const;

  void saveHistory();
  void loadHistory();

  static std::string getDefaultHistoryPath(StringRef ProgName);

  /// The action to perform upon a completion request.
  struct CompletionAction {
    enum ActionKind {
      /// Insert Text at the cursor position.
      AK_Insert,
      /// Show Completions, or beep if the list is empty.
      AK_ShowCompletions
    };

    ActionKind Kind;

    /// The text to insert.
    std::string Text;

    /// The list of completions to show.
    std::vector<std::string> Completions;
  };

  /// A possible completion at a given cursor position.
  struct Completion {
    Completion() {}
    Completion(const std::string &TypedText, const std::string &DisplayText)
        : TypedText(TypedText), DisplayText(DisplayText) {}

    /// The text to insert. If the user has already input some of the
    /// completion, this should only include the rest of the text.
    std::string TypedText;

    /// A description of this completion. This may be the completion itself, or
    /// maybe a summary of its type or arguments.
    std::string DisplayText;
  };

  /// Set the completer for this LineEditor. A completer is a function object
  /// which takes arguments of type StringRef (the string to complete) and
  /// size_t (the zero-based cursor position in the StringRef) and returns a
  /// CompletionAction.
  template <typename T> void setCompleter(T Comp) {
    Completer.reset(new CompleterModel<T>(Comp));
  }

  /// Set the completer for this LineEditor to the given list completer.
  /// A list completer is a function object which takes arguments of type
  /// StringRef (the string to complete) and size_t (the zero-based cursor
  /// position in the StringRef) and returns a std::vector<Completion>.
  template <typename T> void setListCompleter(T Comp) {
    Completer.reset(new ListCompleterModel<T>(Comp));
  }

  /// Use the current completer to produce a CompletionAction for the given
  /// completion request. If the current completer is a list completer, this
  /// will return an AK_Insert CompletionAction if each completion has a common
  /// prefix, or an AK_ShowCompletions CompletionAction otherwise.
  ///
  /// \param Buffer The string to complete
  /// \param Pos The zero-based cursor position in the StringRef
  CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const;

  const std::string &getPrompt() const { return Prompt; }
  void setPrompt(const std::string &P) { Prompt = P; }

  // Public so callbacks in LineEditor.cpp can use it.
  struct InternalData;

private:
  std::string Prompt;
  std::string HistoryPath;
  std::unique_ptr<InternalData> Data;

  struct CompleterConcept {
    virtual ~CompleterConcept();
    virtual CompletionAction complete(StringRef Buffer, size_t Pos) const = 0;
  };

  struct ListCompleterConcept : CompleterConcept {
    ~ListCompleterConcept() override;
    CompletionAction complete(StringRef Buffer, size_t Pos) const override;
    static std::string getCommonPrefix(const std::vector<Completion> &Comps);
    virtual std::vector<Completion> getCompletions(StringRef Buffer,
                                                   size_t Pos) const = 0;
  };

  template <typename T>
  struct CompleterModel : CompleterConcept {
    CompleterModel(T Value) : Value(Value) {}
    CompletionAction complete(StringRef Buffer, size_t Pos) const override {
      return Value(Buffer, Pos);
    }
    T Value;
  };

  template <typename T>
  struct ListCompleterModel : ListCompleterConcept {
    ListCompleterModel(T Value) : Value(std::move(Value)) {}
    std::vector<Completion> getCompletions(StringRef Buffer,
                                           size_t Pos) const override {
      return Value(Buffer, Pos);
    }
    T Value;
  };

  std::unique_ptr<const CompleterConcept> Completer;
};

}

#endif