Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===-- llvm/LineEditor/LineEditor.h - line editor --------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_LINEEDITOR_LINEEDITOR_H
0010 #define LLVM_LINEEDITOR_LINEEDITOR_H
0011 
0012 #include "llvm/ADT/StringRef.h"
0013 #include <cstdio>
0014 #include <memory>
0015 #include <optional>
0016 #include <string>
0017 #include <utility>
0018 #include <vector>
0019 
0020 namespace llvm {
0021 
0022 class LineEditor {
0023 public:
0024   /// Create a LineEditor object.
0025   ///
0026   /// \param ProgName The name of the current program. Used to form a default
0027   /// prompt.
0028   /// \param HistoryPath Path to the file in which to store history data, if
0029   /// possible.
0030   /// \param In The input stream used by the editor.
0031   /// \param Out The output stream used by the editor.
0032   /// \param Err The error stream used by the editor.
0033   LineEditor(StringRef ProgName, StringRef HistoryPath = "", FILE *In = stdin,
0034              FILE *Out = stdout, FILE *Err = stderr);
0035   ~LineEditor();
0036 
0037   /// Reads a line.
0038   ///
0039   /// \return The line, or std::optional<std::string>() on EOF.
0040   std::optional<std::string> readLine() const;
0041 
0042   void saveHistory();
0043   void loadHistory();
0044   void setHistorySize(int size);
0045 
0046   static std::string getDefaultHistoryPath(StringRef ProgName);
0047 
0048   /// The action to perform upon a completion request.
0049   struct CompletionAction {
0050     enum ActionKind {
0051       /// Insert Text at the cursor position.
0052       AK_Insert,
0053       /// Show Completions, or beep if the list is empty.
0054       AK_ShowCompletions
0055     };
0056 
0057     ActionKind Kind;
0058 
0059     /// The text to insert.
0060     std::string Text;
0061 
0062     /// The list of completions to show.
0063     std::vector<std::string> Completions;
0064   };
0065 
0066   /// A possible completion at a given cursor position.
0067   struct Completion {
0068     Completion() = default;
0069     Completion(const std::string &TypedText, const std::string &DisplayText)
0070         : TypedText(TypedText), DisplayText(DisplayText) {}
0071 
0072     /// The text to insert. If the user has already input some of the
0073     /// completion, this should only include the rest of the text.
0074     std::string TypedText;
0075 
0076     /// A description of this completion. This may be the completion itself, or
0077     /// maybe a summary of its type or arguments.
0078     std::string DisplayText;
0079   };
0080 
0081   /// Set the completer for this LineEditor. A completer is a function object
0082   /// which takes arguments of type StringRef (the string to complete) and
0083   /// size_t (the zero-based cursor position in the StringRef) and returns a
0084   /// CompletionAction.
0085   template <typename T> void setCompleter(T Comp) {
0086     Completer.reset(new CompleterModel<T>(Comp));
0087   }
0088 
0089   /// Set the completer for this LineEditor to the given list completer.
0090   /// A list completer is a function object which takes arguments of type
0091   /// StringRef (the string to complete) and size_t (the zero-based cursor
0092   /// position in the StringRef) and returns a std::vector<Completion>.
0093   template <typename T> void setListCompleter(T Comp) {
0094     Completer.reset(new ListCompleterModel<T>(Comp));
0095   }
0096 
0097   /// Use the current completer to produce a CompletionAction for the given
0098   /// completion request. If the current completer is a list completer, this
0099   /// will return an AK_Insert CompletionAction if each completion has a common
0100   /// prefix, or an AK_ShowCompletions CompletionAction otherwise.
0101   ///
0102   /// \param Buffer The string to complete
0103   /// \param Pos The zero-based cursor position in the StringRef
0104   CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const;
0105 
0106   const std::string &getPrompt() const { return Prompt; }
0107   void setPrompt(const std::string &P) { Prompt = P; }
0108 
0109   // Public so callbacks in LineEditor.cpp can use it.
0110   struct InternalData;
0111 
0112 private:
0113   std::string Prompt;
0114   std::string HistoryPath;
0115   std::unique_ptr<InternalData> Data;
0116 
0117   struct CompleterConcept {
0118     virtual ~CompleterConcept();
0119     virtual CompletionAction complete(StringRef Buffer, size_t Pos) const = 0;
0120   };
0121 
0122   struct ListCompleterConcept : CompleterConcept {
0123     ~ListCompleterConcept() override;
0124     CompletionAction complete(StringRef Buffer, size_t Pos) const override;
0125     static std::string getCommonPrefix(const std::vector<Completion> &Comps);
0126     virtual std::vector<Completion> getCompletions(StringRef Buffer,
0127                                                    size_t Pos) const = 0;
0128   };
0129 
0130   template <typename T>
0131   struct CompleterModel : CompleterConcept {
0132     CompleterModel(T Value) : Value(Value) {}
0133     CompletionAction complete(StringRef Buffer, size_t Pos) const override {
0134       return Value(Buffer, Pos);
0135     }
0136     T Value;
0137   };
0138 
0139   template <typename T>
0140   struct ListCompleterModel : ListCompleterConcept {
0141     ListCompleterModel(T Value) : Value(std::move(Value)) {}
0142     std::vector<Completion> getCompletions(StringRef Buffer,
0143                                            size_t Pos) const override {
0144       return Value(Buffer, Pos);
0145     }
0146     T Value;
0147   };
0148 
0149   std::unique_ptr<const CompleterConcept> Completer;
0150 };
0151 
0152 }
0153 
0154 #endif