LLVM  14.0.0git
LineEditor.h
Go to the documentation of this file.
1 //===-- llvm/LineEditor/LineEditor.h - line editor --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LINEEDITOR_LINEEDITOR_H
10 #define LLVM_LINEEDITOR_LINEEDITOR_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <cstdio>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 namespace llvm {
21 
22 class LineEditor {
23 public:
24  /// Create a LineEditor object.
25  ///
26  /// \param ProgName The name of the current program. Used to form a default
27  /// prompt.
28  /// \param HistoryPath Path to the file in which to store history data, if
29  /// possible.
30  /// \param In The input stream used by the editor.
31  /// \param Out The output stream used by the editor.
32  /// \param Err The error stream used by the editor.
33  LineEditor(StringRef ProgName, StringRef HistoryPath = "", FILE *In = stdin,
34  FILE *Out = stdout, FILE *Err = stderr);
35  ~LineEditor();
36 
37  /// Reads a line.
38  ///
39  /// \return The line, or llvm::Optional<std::string>() on EOF.
41 
42  void saveHistory();
43  void loadHistory();
44 
45  static std::string getDefaultHistoryPath(StringRef ProgName);
46 
47  /// The action to perform upon a completion request.
49  enum ActionKind {
50  /// Insert Text at the cursor position.
52  /// Show Completions, or beep if the list is empty.
54  };
55 
57 
58  /// The text to insert.
59  std::string Text;
60 
61  /// The list of completions to show.
62  std::vector<std::string> Completions;
63  };
64 
65  /// A possible completion at a given cursor position.
66  struct Completion {
68  Completion(const std::string &TypedText, const std::string &DisplayText)
70 
71  /// The text to insert. If the user has already input some of the
72  /// completion, this should only include the rest of the text.
73  std::string TypedText;
74 
75  /// A description of this completion. This may be the completion itself, or
76  /// maybe a summary of its type or arguments.
77  std::string DisplayText;
78  };
79 
80  /// Set the completer for this LineEditor. A completer is a function object
81  /// which takes arguments of type StringRef (the string to complete) and
82  /// size_t (the zero-based cursor position in the StringRef) and returns a
83  /// CompletionAction.
84  template <typename T> void setCompleter(T Comp) {
85  Completer.reset(new CompleterModel<T>(Comp));
86  }
87 
88  /// Set the completer for this LineEditor to the given list completer.
89  /// A list completer is a function object which takes arguments of type
90  /// StringRef (the string to complete) and size_t (the zero-based cursor
91  /// position in the StringRef) and returns a std::vector<Completion>.
92  template <typename T> void setListCompleter(T Comp) {
93  Completer.reset(new ListCompleterModel<T>(Comp));
94  }
95 
96  /// Use the current completer to produce a CompletionAction for the given
97  /// completion request. If the current completer is a list completer, this
98  /// will return an AK_Insert CompletionAction if each completion has a common
99  /// prefix, or an AK_ShowCompletions CompletionAction otherwise.
100  ///
101  /// \param Buffer The string to complete
102  /// \param Pos The zero-based cursor position in the StringRef
103  CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const;
104 
105  const std::string &getPrompt() const { return Prompt; }
106  void setPrompt(const std::string &P) { Prompt = P; }
107 
108  // Public so callbacks in LineEditor.cpp can use it.
109  struct InternalData;
110 
111 private:
112  std::string Prompt;
113  std::string HistoryPath;
114  std::unique_ptr<InternalData> Data;
115 
116  struct CompleterConcept {
117  virtual ~CompleterConcept();
118  virtual CompletionAction complete(StringRef Buffer, size_t Pos) const = 0;
119  };
120 
121  struct ListCompleterConcept : CompleterConcept {
122  ~ListCompleterConcept() override;
123  CompletionAction complete(StringRef Buffer, size_t Pos) const override;
124  static std::string getCommonPrefix(const std::vector<Completion> &Comps);
125  virtual std::vector<Completion> getCompletions(StringRef Buffer,
126  size_t Pos) const = 0;
127  };
128 
129  template <typename T>
130  struct CompleterModel : CompleterConcept {
131  CompleterModel(T Value) : Value(Value) {}
132  CompletionAction complete(StringRef Buffer, size_t Pos) const override {
133  return Value(Buffer, Pos);
134  }
135  T Value;
136  };
137 
138  template <typename T>
139  struct ListCompleterModel : ListCompleterConcept {
140  ListCompleterModel(T Value) : Value(std::move(Value)) {}
141  std::vector<Completion> getCompletions(StringRef Buffer,
142  size_t Pos) const override {
143  return Value(Buffer, Pos);
144  }
145  T Value;
146  };
147 
148  std::unique_ptr<const CompleterConcept> Completer;
149 };
150 
151 }
152 
153 #endif
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:22
Optional.h
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::LineEditor::readLine
llvm::Optional< std::string > readLine() const
Reads a line.
Definition: LineEditor.cpp:299
llvm::Optional< std::string >
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::LineEditor::Completion
A possible completion at a given cursor position.
Definition: LineEditor.h:66
llvm::LineEditor::Completion::TypedText
std::string TypedText
The text to insert.
Definition: LineEditor.h:73
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::LineEditor::CompletionAction::AK_Insert
@ AK_Insert
Insert Text at the cursor position.
Definition: LineEditor.h:51
llvm::LineEditor
Definition: LineEditor.h:22
llvm::LineEditor::CompletionAction::Kind
ActionKind Kind
Definition: LineEditor.h:56
llvm::LineEditor::getCompletionAction
CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const
Use the current completer to produce a CompletionAction for the given completion request.
Definition: LineEditor.cpp:82
llvm::LineEditor::Completion::DisplayText
std::string DisplayText
A description of this completion.
Definition: LineEditor.h:77
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::LineEditor::CompletionAction::ActionKind
ActionKind
Definition: LineEditor.h:49
llvm::LineEditor::CompletionAction
The action to perform upon a completion request.
Definition: LineEditor.h:48
llvm::LineEditor::LineEditor
LineEditor(StringRef ProgName, StringRef HistoryPath="", FILE *In=stdin, FILE *Out=stdout, FILE *Err=stderr)
Create a LineEditor object.
Definition: LineEditor.cpp:285
llvm::LineEditor::CompletionAction::Text
std::string Text
The text to insert.
Definition: LineEditor.h:59
llvm::LineEditor::~LineEditor
~LineEditor()
Definition: LineEditor.cpp:292
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1641
llvm::LineEditor::setCompleter
void setCompleter(T Comp)
Set the completer for this LineEditor.
Definition: LineEditor.h:84
llvm::LineEditor::setListCompleter
void setListCompleter(T Comp)
Set the completer for this LineEditor to the given list completer.
Definition: LineEditor.h:92
llvm::LineEditor::saveHistory
void saveHistory()
Definition: LineEditor.cpp:296
llvm::LineEditor::loadHistory
void loadHistory()
Definition: LineEditor.cpp:297
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::LineEditor::Completion::Completion
Completion(const std::string &TypedText, const std::string &DisplayText)
Definition: LineEditor.h:68
std
Definition: BitVector.h:850
llvm::LineEditor::setPrompt
void setPrompt(const std::string &P)
Definition: LineEditor.h:106
llvm::LineEditor::getDefaultHistoryPath
static std::string getDefaultHistoryPath(StringRef ProgName)
Definition: LineEditor.cpp:23
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::LineEditor::Completion::Completion
Completion()
Definition: LineEditor.h:67
llvm::LineEditor::CompletionAction::Completions
std::vector< std::string > Completions
The list of completions to show.
Definition: LineEditor.h:62
llvm::LineEditor::getPrompt
const std::string & getPrompt() const
Definition: LineEditor.h:105
llvm::LineEditor::CompletionAction::AK_ShowCompletions
@ AK_ShowCompletions
Show Completions, or beep if the list is empty.
Definition: LineEditor.h:53