Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:48

0001 //===-- Editline.h ----------------------------------------------*- 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 // TODO: wire up window size changes
0010 
0011 // If we ever get a private copy of libedit, there are a number of defects that
0012 // would be nice to fix;
0013 // a) Sometimes text just disappears while editing.  In an 80-column editor
0014 // paste the following text, without
0015 //    the quotes:
0016 //    "This is a test of the input system missing Hello, World!  Do you
0017 //    disappear when it gets to a particular length?"
0018 //    Now press ^A to move to the start and type 3 characters, and you'll see a
0019 //    good amount of the text will
0020 //    disappear.  It's still in the buffer, just invisible.
0021 // b) The prompt printing logic for dealing with ANSI formatting characters is
0022 // broken, which is why we're working around it here.
0023 // c) The incremental search uses escape to cancel input, so it's confused by
0024 // ANSI sequences starting with escape.
0025 // d) Emoji support is fairly terrible, presumably it doesn't understand
0026 // composed characters?
0027 
0028 #ifndef LLDB_HOST_EDITLINE_H
0029 #define LLDB_HOST_EDITLINE_H
0030 
0031 #include "lldb/Host/Config.h"
0032 
0033 #include <locale>
0034 #include <sstream>
0035 #include <vector>
0036 
0037 #include "lldb/lldb-private.h"
0038 
0039 #if !defined(_WIN32) && !defined(__ANDROID__)
0040 #include <histedit.h>
0041 #endif
0042 
0043 #include <csignal>
0044 #include <mutex>
0045 #include <optional>
0046 #include <string>
0047 #include <vector>
0048 
0049 #include "lldb/Host/ConnectionFileDescriptor.h"
0050 #include "lldb/Utility/CompletionRequest.h"
0051 #include "lldb/Utility/FileSpec.h"
0052 #include "lldb/Utility/Predicate.h"
0053 #include "lldb/Utility/StringList.h"
0054 
0055 #include "llvm/ADT/FunctionExtras.h"
0056 
0057 namespace lldb_private {
0058 namespace line_editor {
0059 
0060 // type alias's to help manage 8 bit and wide character versions of libedit
0061 #if LLDB_EDITLINE_USE_WCHAR
0062 using EditLineStringType = std::wstring;
0063 using EditLineStringStreamType = std::wstringstream;
0064 using EditLineCharType = wchar_t;
0065 #else
0066 using EditLineStringType = std::string;
0067 using EditLineStringStreamType = std::stringstream;
0068 using EditLineCharType = char;
0069 #endif
0070 
0071 // At one point the callback type of el_set getchar callback changed from char
0072 // to wchar_t. It is not possible to detect differentiate between the two
0073 // versions exactly, but this is a pretty good approximation and allows us to
0074 // build against almost any editline version out there.
0075 // It does, however, require extra care when invoking el_getc, as the type
0076 // of the input is a single char buffer, but the callback will write a wchar_t.
0077 #if LLDB_EDITLINE_USE_WCHAR || defined(EL_CLIENTDATA) || LLDB_HAVE_EL_RFUNC_T
0078 using EditLineGetCharType = wchar_t;
0079 #else
0080 using EditLineGetCharType = char;
0081 #endif
0082 
0083 using EditlineGetCharCallbackType = int (*)(::EditLine *editline,
0084                                             EditLineGetCharType *c);
0085 using EditlineCommandCallbackType = unsigned char (*)(::EditLine *editline,
0086                                                       int ch);
0087 using EditlinePromptCallbackType = const char *(*)(::EditLine *editline);
0088 
0089 class EditlineHistory;
0090 
0091 using EditlineHistorySP = std::shared_ptr<EditlineHistory>;
0092 
0093 using IsInputCompleteCallbackType =
0094     llvm::unique_function<bool(Editline *, StringList &)>;
0095 
0096 using FixIndentationCallbackType =
0097     llvm::unique_function<int(Editline *, StringList &, int)>;
0098 
0099 using SuggestionCallbackType =
0100     llvm::unique_function<std::optional<std::string>(llvm::StringRef)>;
0101 
0102 using CompleteCallbackType = llvm::unique_function<void(CompletionRequest &)>;
0103 
0104 /// Status used to decide when and how to start editing another line in
0105 /// multi-line sessions.
0106 enum class EditorStatus {
0107 
0108   /// The default state proceeds to edit the current line.
0109   Editing,
0110 
0111   /// Editing complete, returns the complete set of edited lines.
0112   Complete,
0113 
0114   /// End of input reported.
0115   EndOfInput,
0116 
0117   /// Editing interrupted.
0118   Interrupted
0119 };
0120 
0121 /// Established locations that can be easily moved among with MoveCursor.
0122 enum class CursorLocation {
0123   /// The start of the first line in a multi-line edit session.
0124   BlockStart,
0125 
0126   /// The start of the current line in a multi-line edit session.
0127   EditingPrompt,
0128 
0129   /// The location of the cursor on the current line in a multi-line edit
0130   /// session.
0131   EditingCursor,
0132 
0133   /// The location immediately after the last character in a multi-line edit
0134   /// session.
0135   BlockEnd
0136 };
0137 
0138 /// Operation for the history.
0139 enum class HistoryOperation {
0140   Oldest,
0141   Older,
0142   Current,
0143   Newer,
0144   Newest
0145 };
0146 }
0147 
0148 using namespace line_editor;
0149 
0150 /// Instances of Editline provide an abstraction over libedit's EditLine
0151 /// facility.  Both single- and multi-line editing are supported.
0152 class Editline {
0153 public:
0154   Editline(const char *editor_name, FILE *input_file, FILE *output_file,
0155            FILE *error_file, bool color, std::recursive_mutex &output_mutex);
0156 
0157   ~Editline();
0158 
0159   /// Uses the user data storage of EditLine to retrieve an associated instance
0160   /// of Editline.
0161   static Editline *InstanceFor(::EditLine *editline);
0162 
0163   static void
0164   DisplayCompletions(Editline &editline,
0165                      llvm::ArrayRef<CompletionResult::Completion> results);
0166 
0167   /// Sets a string to be used as a prompt, or combined with a line number to
0168   /// form a prompt.
0169   void SetPrompt(const char *prompt);
0170 
0171   /// Sets an alternate string to be used as a prompt for the second line and
0172   /// beyond in multi-line editing scenarios.
0173   void SetContinuationPrompt(const char *continuation_prompt);
0174 
0175   /// Call when the terminal size changes.
0176   void TerminalSizeChanged();
0177 
0178   /// Returns the prompt established by SetPrompt.
0179   const char *GetPrompt();
0180 
0181   /// Returns the index of the line currently being edited.
0182   uint32_t GetCurrentLine();
0183 
0184   /// Interrupt the current edit as if ^C was pressed.
0185   bool Interrupt();
0186 
0187   /// Cancel this edit and obliterate all trace of it.
0188   bool Cancel();
0189 
0190   /// Register a callback for autosuggestion.
0191   void SetSuggestionCallback(SuggestionCallbackType callback) {
0192     m_suggestion_callback = std::move(callback);
0193   }
0194 
0195   /// Register a callback for the tab key
0196   void SetAutoCompleteCallback(CompleteCallbackType callback) {
0197     m_completion_callback = std::move(callback);
0198   }
0199 
0200   /// Register a callback for testing whether multi-line input is complete
0201   void SetIsInputCompleteCallback(IsInputCompleteCallbackType callback) {
0202     m_is_input_complete_callback = std::move(callback);
0203   }
0204 
0205   /// Register a callback for determining the appropriate indentation for a line
0206   /// when creating a newline.  An optional set of insertable characters can
0207   /// also trigger the callback.
0208   void SetFixIndentationCallback(FixIndentationCallbackType callback,
0209                                  const char *indent_chars) {
0210     m_fix_indentation_callback = std::move(callback);
0211     m_fix_indentation_callback_chars = indent_chars;
0212   }
0213 
0214   void SetPromptAnsiPrefix(std::string prefix) {
0215     if (m_color)
0216       m_prompt_ansi_prefix = std::move(prefix);
0217   }
0218 
0219   void SetPromptAnsiSuffix(std::string suffix) {
0220     if (m_color)
0221       m_prompt_ansi_suffix = std::move(suffix);
0222   }
0223 
0224   void SetSuggestionAnsiPrefix(std::string prefix) {
0225     if (m_color)
0226       m_suggestion_ansi_prefix = std::move(prefix);
0227   }
0228 
0229   void SetSuggestionAnsiSuffix(std::string suffix) {
0230     if (m_color)
0231       m_suggestion_ansi_suffix = std::move(suffix);
0232   }
0233 
0234   /// Prompts for and reads a single line of user input.
0235   bool GetLine(std::string &line, bool &interrupted);
0236 
0237   /// Prompts for and reads a multi-line batch of user input.
0238   bool GetLines(int first_line_number, StringList &lines, bool &interrupted);
0239 
0240   void PrintAsync(Stream *stream, const char *s, size_t len);
0241 
0242   /// Convert the current input lines into a UTF8 StringList
0243   StringList GetInputAsStringList(int line_count = UINT32_MAX);
0244 
0245   size_t GetTerminalWidth() { return m_terminal_width; }
0246 
0247   size_t GetTerminalHeight() { return m_terminal_height; }
0248 
0249 private:
0250   /// Sets the lowest line number for multi-line editing sessions.  A value of
0251   /// zero suppresses line number printing in the prompt.
0252   void SetBaseLineNumber(int line_number);
0253 
0254   /// Returns the complete prompt by combining the prompt or continuation prompt
0255   /// with line numbers as appropriate.  The line index is a zero-based index
0256   /// into the current multi-line session.
0257   std::string PromptForIndex(int line_index);
0258 
0259   /// Sets the current line index between line edits to allow free movement
0260   /// between lines.  Updates the prompt to match.
0261   void SetCurrentLine(int line_index);
0262 
0263   /// Determines the width of the prompt in characters.  The width is guaranteed
0264   /// to be the same for all lines of the current multi-line session.
0265   size_t GetPromptWidth();
0266 
0267   /// Returns true if the underlying EditLine session's keybindings are
0268   /// Emacs-based, or false if they are VI-based.
0269   bool IsEmacs();
0270 
0271   /// Returns true if the current EditLine buffer contains nothing but spaces,
0272   /// or is empty.
0273   bool IsOnlySpaces();
0274 
0275   /// Helper method used by MoveCursor to determine relative line position.
0276   int GetLineIndexForLocation(CursorLocation location, int cursor_row);
0277 
0278   /// Move the cursor from one well-established location to another using
0279   /// relative line positioning and absolute column positioning.
0280   void MoveCursor(CursorLocation from, CursorLocation to);
0281 
0282   /// Clear from cursor position to bottom of screen and print input lines
0283   /// including prompts, optionally starting from a specific line.  Lines are
0284   /// drawn with an extra space at the end to reserve room for the rightmost
0285   /// cursor position.
0286   void DisplayInput(int firstIndex = 0);
0287 
0288   /// Counts the number of rows a given line of content will end up occupying,
0289   /// taking into account both the preceding prompt and a single trailing space
0290   /// occupied by a cursor when at the end of the line.
0291   int CountRowsForLine(const EditLineStringType &content);
0292 
0293   /// Save the line currently being edited.
0294   void SaveEditedLine();
0295 
0296   /// Replaces the current multi-line session with the next entry from history.
0297   unsigned char RecallHistory(HistoryOperation op);
0298 
0299   /// Character reading implementation for EditLine that supports our multi-line
0300   /// editing trickery.
0301   int GetCharacter(EditLineGetCharType *c);
0302 
0303   /// Prompt implementation for EditLine.
0304   const char *Prompt();
0305 
0306   /// Line break command used when meta+return is pressed in multi-line mode.
0307   unsigned char BreakLineCommand(int ch);
0308 
0309   /// Command used when return is pressed in multi-line mode.
0310   unsigned char EndOrAddLineCommand(int ch);
0311 
0312   /// Delete command used when delete is pressed in multi-line mode.
0313   unsigned char DeleteNextCharCommand(int ch);
0314 
0315   /// Delete command used when backspace is pressed in multi-line mode.
0316   unsigned char DeletePreviousCharCommand(int ch);
0317 
0318   /// Line navigation command used when ^P or up arrow are pressed in multi-line
0319   /// mode.
0320   unsigned char PreviousLineCommand(int ch);
0321 
0322   /// Line navigation command used when ^N or down arrow are pressed in
0323   /// multi-line mode.
0324   unsigned char NextLineCommand(int ch);
0325 
0326   /// History navigation command used when Alt + up arrow is pressed in
0327   /// multi-line mode.
0328   unsigned char PreviousHistoryCommand(int ch);
0329 
0330   /// History navigation command used when Alt + down arrow is pressed in
0331   /// multi-line mode.
0332   unsigned char NextHistoryCommand(int ch);
0333 
0334   /// Buffer start command used when Esc < is typed in multi-line emacs mode.
0335   unsigned char BufferStartCommand(int ch);
0336 
0337   /// Buffer end command used when Esc > is typed in multi-line emacs mode.
0338   unsigned char BufferEndCommand(int ch);
0339 
0340   /// Context-sensitive tab insertion or code completion command used when the
0341   /// tab key is typed.
0342   unsigned char TabCommand(int ch);
0343 
0344   /// Apply autosuggestion part in gray as editline.
0345   unsigned char ApplyAutosuggestCommand(int ch);
0346 
0347   /// Command used when a character is typed.
0348   unsigned char TypedCharacter(int ch);
0349 
0350   /// Respond to normal character insertion by fixing line indentation
0351   unsigned char FixIndentationCommand(int ch);
0352 
0353   /// Revert line command used when moving between lines.
0354   unsigned char RevertLineCommand(int ch);
0355 
0356   /// Ensures that the current EditLine instance is properly configured for
0357   /// single or multi-line editing.
0358   void ConfigureEditor(bool multiline);
0359 
0360   bool CompleteCharacter(char ch, EditLineGetCharType &out);
0361 
0362   void ApplyTerminalSizeChange();
0363 
0364   // The following set various editline parameters.  It's not any less
0365   // verbose to put the editline calls into a function, but it
0366   // provides type safety, since the editline functions take varargs
0367   // parameters.
0368   void AddFunctionToEditLine(const EditLineCharType *command,
0369                              const EditLineCharType *helptext,
0370                              EditlineCommandCallbackType callbackFn);
0371   void SetEditLinePromptCallback(EditlinePromptCallbackType callbackFn);
0372   void SetGetCharacterFunction(EditlineGetCharCallbackType callbackFn);
0373 
0374   ::EditLine *m_editline = nullptr;
0375   EditlineHistorySP m_history_sp;
0376   bool m_in_history = false;
0377   std::vector<EditLineStringType> m_live_history_lines;
0378   bool m_multiline_enabled = false;
0379   std::vector<EditLineStringType> m_input_lines;
0380   EditorStatus m_editor_status;
0381   int m_terminal_width = 0;
0382   int m_terminal_height = 0;
0383   int m_base_line_number = 0;
0384   unsigned m_current_line_index = 0;
0385   int m_current_line_rows = -1;
0386   int m_revert_cursor_index = 0;
0387   int m_line_number_digits = 3;
0388   std::string m_set_prompt;
0389   std::string m_set_continuation_prompt;
0390   std::string m_current_prompt;
0391   bool m_needs_prompt_repaint = false;
0392   volatile std::sig_atomic_t m_terminal_size_has_changed = 0;
0393   std::string m_editor_name;
0394   FILE *m_input_file;
0395   FILE *m_output_file;
0396   FILE *m_error_file;
0397   ConnectionFileDescriptor m_input_connection;
0398 
0399   IsInputCompleteCallbackType m_is_input_complete_callback;
0400 
0401   FixIndentationCallbackType m_fix_indentation_callback;
0402   const char *m_fix_indentation_callback_chars = nullptr;
0403 
0404   CompleteCallbackType m_completion_callback;
0405   SuggestionCallbackType m_suggestion_callback;
0406 
0407   bool m_color;
0408   std::string m_prompt_ansi_prefix;
0409   std::string m_prompt_ansi_suffix;
0410   std::string m_suggestion_ansi_prefix;
0411   std::string m_suggestion_ansi_suffix;
0412 
0413   std::size_t m_previous_autosuggestion_size = 0;
0414   std::recursive_mutex &m_output_mutex;
0415 };
0416 }
0417 
0418 #endif // LLDB_HOST_EDITLINE_H