LLVM API Documentation

LineEditor.h
Go to the documentation of this file.
00001 //===-- llvm/LineEditor/LineEditor.h - line editor --------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_LINEEDITOR_LINEEDITOR_H
00011 #define LLVM_LINEEDITOR_LINEEDITOR_H
00012 
00013 #include "llvm/ADT/Optional.h"
00014 #include "llvm/ADT/StringRef.h"
00015 #include <cstdio>
00016 #include <memory>
00017 #include <string>
00018 #include <vector>
00019 
00020 namespace llvm {
00021 
00022 class LineEditor {
00023 public:
00024   /// Create a LineEditor object.
00025   ///
00026   /// \param ProgName The name of the current program. Used to form a default
00027   /// prompt.
00028   /// \param HistoryPath Path to the file in which to store history data, if
00029   /// possible.
00030   /// \param In The input stream used by the editor.
00031   /// \param Out The output stream used by the editor.
00032   /// \param Err The error stream used by the editor.
00033   LineEditor(StringRef ProgName, StringRef HistoryPath = "", FILE *In = stdin,
00034              FILE *Out = stdout, FILE *Err = stderr);
00035   ~LineEditor();
00036 
00037   /// Reads a line.
00038   ///
00039   /// \return The line, or llvm::Optional<std::string>() on EOF.
00040   llvm::Optional<std::string> readLine() const;
00041 
00042   void saveHistory();
00043   void loadHistory();
00044 
00045   static std::string getDefaultHistoryPath(StringRef ProgName);
00046 
00047   /// The action to perform upon a completion request.
00048   struct CompletionAction {
00049     enum ActionKind {
00050       /// Insert Text at the cursor position.
00051       AK_Insert,
00052       /// Show Completions, or beep if the list is empty.
00053       AK_ShowCompletions
00054     };
00055 
00056     ActionKind Kind;
00057 
00058     /// The text to insert.
00059     std::string Text;
00060 
00061     /// The list of completions to show.
00062     std::vector<std::string> Completions;
00063   };
00064 
00065   /// A possible completion at a given cursor position.
00066   struct Completion {
00067     Completion() {}
00068     Completion(const std::string &TypedText, const std::string &DisplayText)
00069         : TypedText(TypedText), DisplayText(DisplayText) {}
00070 
00071     /// The text to insert. If the user has already input some of the
00072     /// completion, this should only include the rest of the text.
00073     std::string TypedText;
00074 
00075     /// A description of this completion. This may be the completion itself, or
00076     /// maybe a summary of its type or arguments.
00077     std::string DisplayText;
00078   };
00079 
00080   /// Set the completer for this LineEditor. A completer is a function object
00081   /// which takes arguments of type StringRef (the string to complete) and
00082   /// size_t (the zero-based cursor position in the StringRef) and returns a
00083   /// CompletionAction.
00084   template <typename T> void setCompleter(T Comp) {
00085     Completer.reset(new CompleterModel<T>(Comp));
00086   }
00087 
00088   /// Set the completer for this LineEditor to the given list completer.
00089   /// A list completer is a function object which takes arguments of type
00090   /// StringRef (the string to complete) and size_t (the zero-based cursor
00091   /// position in the StringRef) and returns a std::vector<Completion>.
00092   template <typename T> void setListCompleter(T Comp) {
00093     Completer.reset(new ListCompleterModel<T>(Comp));
00094   }
00095 
00096   /// Use the current completer to produce a CompletionAction for the given
00097   /// completion request. If the current completer is a list completer, this
00098   /// will return an AK_Insert CompletionAction if each completion has a common
00099   /// prefix, or an AK_ShowCompletions CompletionAction otherwise.
00100   ///
00101   /// \param Buffer The string to complete
00102   /// \param Pos The zero-based cursor position in the StringRef
00103   CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const;
00104 
00105   const std::string &getPrompt() const { return Prompt; }
00106   void setPrompt(const std::string &P) { Prompt = P; }
00107 
00108   // Public so callbacks in LineEditor.cpp can use it.
00109   struct InternalData;
00110 
00111 private:
00112   std::string Prompt;
00113   std::string HistoryPath;
00114   std::unique_ptr<InternalData> Data;
00115 
00116   struct CompleterConcept {
00117     virtual ~CompleterConcept();
00118     virtual CompletionAction complete(StringRef Buffer, size_t Pos) const = 0;
00119   };
00120 
00121   struct ListCompleterConcept : CompleterConcept {
00122     ~ListCompleterConcept();
00123     CompletionAction complete(StringRef Buffer, size_t Pos) const override;
00124     static std::string getCommonPrefix(const std::vector<Completion> &Comps);
00125     virtual std::vector<Completion> getCompletions(StringRef Buffer,
00126                                                    size_t Pos) const = 0;
00127   };
00128 
00129   template <typename T>
00130   struct CompleterModel : CompleterConcept {
00131     CompleterModel(T Value) : Value(Value) {}
00132     CompletionAction complete(StringRef Buffer, size_t Pos) const override {
00133       return Value(Buffer, Pos);
00134     }
00135     T Value;
00136   };
00137 
00138   template <typename T>
00139   struct ListCompleterModel : ListCompleterConcept {
00140     ListCompleterModel(T Value) : Value(Value) {}
00141     std::vector<Completion> getCompletions(StringRef Buffer,
00142                                            size_t Pos) const override {
00143       return Value(Buffer, Pos);
00144     }
00145     T Value;
00146   };
00147 
00148   std::unique_ptr<const CompleterConcept> Completer;
00149 };
00150 
00151 }
00152 
00153 #endif