clang-tools  9.0.0
Tweak.h
Go to the documentation of this file.
1 //===--- Tweak.h -------------------------------------------------*- 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 // Tweaks are small actions that run over the AST and produce edits, messages
9 // etc as a result. They are local, i.e. they should take the current editor
10 // context, e.g. the cursor position and selection into account.
11 // The actions are executed in two stages:
12 // - Stage 1 should check whether the action is available in a current
13 // context. It should be cheap and fast to compute as it is executed for all
14 // available actions on every client request, which happen quite frequently.
15 // - Stage 2 is performed after stage 1 and can be more expensive to compute.
16 // It is performed when the user actually chooses the action.
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_ACTIONS_TWEAK_H
20 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_ACTIONS_TWEAK_H
21 
22 #include "ClangdUnit.h"
23 #include "Protocol.h"
24 #include "Selection.h"
25 #include "clang/Tooling/Core/Replacement.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/ADT/StringRef.h"
28 namespace clang {
29 namespace clangd {
30 
31 /// An interface base for small context-sensitive refactoring actions.
32 /// To implement a new tweak use the following pattern in a .cpp file:
33 /// class MyTweak : public Tweak {
34 /// public:
35 /// const char* id() const override final; // defined by REGISTER_TWEAK.
36 /// // implement other methods here.
37 /// };
38 /// REGISTER_TWEAK(MyTweak);
39 class Tweak {
40 public:
41  /// Input to prepare and apply tweaks.
42  struct Selection {
43  Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd);
44  /// The text of the active document.
45  llvm::StringRef Code;
46  /// Parsed AST of the active file.
48  /// A location of the cursor in the editor.
49  // FIXME: Cursor is redundant and should be removed
50  SourceLocation Cursor;
51  /// The begin offset of the selection
52  unsigned SelectionBegin;
53  /// The end offset of the selection
54  unsigned SelectionEnd;
55  /// The AST nodes that were selected.
57  // FIXME: provide a way to get sources and ASTs for other files.
58  };
59 
60  /// Output of a tweak.
61  enum Intent {
62  /// Apply changes that preserve the behavior of the code.
64  /// Provide information to the user.
66  };
67  struct Effect {
68  /// A message to be displayed to the user.
69  llvm::Optional<std::string> ShowMessage;
70  /// An edit to apply to the input file.
71  llvm::Optional<tooling::Replacements> ApplyEdit;
72 
73  static Effect applyEdit(tooling::Replacements R) {
74  Effect E;
75  E.ApplyEdit = std::move(R);
76  return E;
77  }
78  static Effect showMessage(StringRef S) {
79  Effect E;
80  E.ShowMessage = S;
81  return E;
82  }
83  };
84 
85  virtual ~Tweak() = default;
86  /// A unique id of the action, it is always equal to the name of the class
87  /// defining the Tweak. Definition is provided automatically by
88  /// REGISTER_TWEAK.
89  virtual const char *id() const = 0;
90  /// Run the first stage of the action. Returns true indicating that the
91  /// action is available and should be shown to the user. Returns false if the
92  /// action is not available.
93  /// This function should be fast, if the action requires non-trivial work it
94  /// should be moved into 'apply'.
95  /// Returns true iff the action is available and apply() can be called on it.
96  virtual bool prepare(const Selection &Sel) = 0;
97  /// Run the second stage of the action that would produce the actual effect.
98  /// EXPECTS: prepare() was called and returned true.
99  virtual Expected<Effect> apply(const Selection &Sel) = 0;
100 
101  /// A one-line title of the action that should be shown to the users in the
102  /// UI.
103  /// EXPECTS: prepare() was called and returned true.
104  virtual std::string title() const = 0;
105  /// Describes what kind of action this is.
106  /// EXPECTS: prepare() was called and returned true.
107  virtual Intent intent() const = 0;
108  /// Is this a 'hidden' tweak, which are off by default.
109  virtual bool hidden() const { return false; }
110 };
111 
112 // All tweaks must be registered in the .cpp file next to their definition.
113 #define REGISTER_TWEAK(Subclass) \
114  ::llvm::Registry<::clang::clangd::Tweak>::Add<Subclass> \
115  TweakRegistrationFor##Subclass(#Subclass, /*Description=*/""); \
116  const char *Subclass::id() const { return #Subclass; }
117 
118 /// Calls prepare() on all tweaks that satisfy the filter, returning those that
119 /// can run on the selection.
120 std::vector<std::unique_ptr<Tweak>>
121 prepareTweaks(const Tweak::Selection &S,
122  llvm::function_ref<bool(const Tweak &)> Filter);
123 
124 // Calls prepare() on the tweak with a given ID.
125 // If prepare() returns false, returns an error.
126 // If prepare() returns true, returns the corresponding tweak.
127 llvm::Expected<std::unique_ptr<Tweak>> prepareTweak(StringRef TweakID,
128  const Tweak::Selection &S);
129 
130 } // namespace clangd
131 } // namespace clang
132 
133 #endif
virtual ~Tweak()=default
virtual std::string title() const =0
A one-line title of the action that should be shown to the users in the UI.
virtual bool prepare(const Selection &Sel)=0
Run the first stage of the action.
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S)
Definition: Tweak.cpp:69
Apply changes that preserve the behavior of the code.
Definition: Tweak.h:63
llvm::Optional< tooling::Replacements > ApplyEdit
An edit to apply to the input file.
Definition: Tweak.h:71
Intent
Output of a tweak.
Definition: Tweak.h:61
Provide information to the user.
Definition: Tweak.h:65
llvm::StringRef Code
The text of the active document.
Definition: Tweak.h:45
Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd)
Definition: Tweak.cpp:41
SelectionTree ASTSelection
The AST nodes that were selected.
Definition: Tweak.h:56
virtual Expected< Effect > apply(const Selection &Sel)=0
Run the second stage of the action that would produce the actual effect.
SourceLocation Cursor
A location of the cursor in the editor.
Definition: Tweak.h:50
Input to prepare and apply tweaks.
Definition: Tweak.h:42
virtual bool hidden() const
Is this a &#39;hidden&#39; tweak, which are off by default.
Definition: Tweak.h:109
Stores and provides access to parsed AST.
Definition: ClangdUnit.h:73
virtual const char * id() const =0
A unique id of the action, it is always equal to the name of the class defining the Tweak...
unsigned SelectionBegin
The begin offset of the selection.
Definition: Tweak.h:52
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
virtual Intent intent() const =0
Describes what kind of action this is.
static Effect applyEdit(tooling::Replacements R)
Definition: Tweak.h:73
An interface base for small context-sensitive refactoring actions.
Definition: Tweak.h:39
unsigned SelectionEnd
The end offset of the selection.
Definition: Tweak.h:54
llvm::Optional< std::string > ShowMessage
A message to be displayed to the user.
Definition: Tweak.h:69
std::vector< std::unique_ptr< Tweak > > prepareTweaks(const Tweak::Selection &S, llvm::function_ref< bool(const Tweak &)> Filter)
Calls prepare() on all tweaks that satisfy the filter, returning those that can run on the selection...
Definition: Tweak.cpp:51
ParsedAST & AST
Parsed AST of the active file.
Definition: Tweak.h:47
static Effect showMessage(StringRef S)
Definition: Tweak.h:78