ROOT  6.06/09
Reference Guide
cppcompleter.py
Go to the documentation of this file.
1 import utils
2 import ROOT
3 
4 def rreplace(s, old, new, occurrence):
5  li = s.rsplit(old, occurrence)
6  return new.join(li)
7 
8 # Jit a wrapper for the ttabcom
9 _TTabComHookCode = """
10 std::vector<std::string> _TTabComHook(const char* pattern){
11  static auto ttc = new TTabCom;
12  int pLoc = strlen(pattern);
13  std::ostringstream oss;
14  ttc->Hook((char* )pattern, &pLoc, oss);
15  std::stringstream ss(oss.str());
16  std::istream_iterator<std::string> vbegin(ss), vend;
17  return std::vector<std::string> (vbegin, vend);
18 }
19 """
20 
21 class CppCompleter(object):
22  '''
23  Completer which interfaces to the TTabCom of ROOT. It is activated
24  (deactivated) upon the load(unload) of the load of the extension.
25 
26  >>> comp = CppCompleter()
27  >>> comp.activate()
28  >>> for suggestion in comp._completeImpl("TH1"):
29  ... print suggestion
30  TH1
31  TH1C
32  TH1D
33  TH1F
34  TH1I
35  TH1K
36  TH1S
37  >>> for suggestion in comp._completeImpl("TH2"):
38  ... print suggestion
39  TH2
40  TH2C
41  TH2D
42  TH2F
43  TH2GL
44  TH2I
45  TH2Poly
46  TH2PolyBin
47  TH2S
48  >>> garbage = ROOT.gInterpreter.ProcessLine("TH1F* h")
49  >>> for suggestion in comp._completeImpl("h->GetA"):
50  ... print suggestion
51  h->GetArray
52  h->GetAsymmetry
53  h->GetAt
54  h->GetAxisColor
55  >>> for suggestion in comp._completeImpl("TROOT::Is"):
56  ... print suggestion
57  IsA
58  IsBatch
59  IsEqual
60  IsEscaped
61  IsExecutingMacro
62  IsFolder
63  IsInterrupted
64  IsLineProcessing
65  IsModified
66  IsOnHeap
67  IsProofServ
68  IsRootFile
69  IsSortable
70  IsWritable
71  IsZombie
72  >>> comp.deactivate()
73  >>> for suggestion in comp._completeImpl("TG"):
74  ... print suggestion
75  '''
76 
77  def __init__(self):
78  self.hook = None
79  self.active = True
80  self.firstActivation = True
81  self.accessors = [".", "->", "::"]
82 
83  def activate(self):
84  self.active = True
85  if self.firstActivation:
86  utils.declareCppCode('#include "dlfcn.h"')
87  dlOpenRint = 'dlopen("libRint.so",RTLD_NOW);'
88  utils.processCppCode(dlOpenRint)
89  utils.declareCppCode(_TTabComHookCode)
90  self.hook = ROOT._TTabComHook
91  self.firstActivation = False
92 
93  def deactivate(self):
94  self.active = False
95 
96  def _getSuggestions(self,line):
97  if self.active:
98  return self.hook(line)
99  return []
100 
101  def _getLastAccessorPos(self,line):
102  accessorPos = -1
103  for accessor in self.accessors:
104  tmpAccessorPos = line.rfind(accessor)
105  if accessorPos < tmpAccessorPos:
106  accessorPos = tmpAccessorPos+len(accessor) if accessor!="::" else 0
107  return accessorPos
108 
109  def _completeImpl(self, line):
110  line=line.split()[-1]
111  suggestions = self._getSuggestions(line)
112  if not suggestions: return []
113  accessorPos = self._getLastAccessorPos(line)
114  suggestions = sorted(suggestions)
115  if accessorPos > 0:
116  suggestions = [line[:accessorPos]+sugg for sugg in suggestions]
117  return suggestions
118 
119  def complete(self, ip, event) :
120  '''
121  Autocomplete interfacing to TTabCom. If an accessor of a scope is
122  present in the line, the suggestions are prepended with the line.
123  That's how completers work. For example:
124  myGraph.Set<tab> will return "myGraph.Set+suggestion in the list of
125  suggestions.
126  '''
127  return self._completeImpl(event.line)
128 
129 
130 _cppCompleter = CppCompleter()
131 
133  _cppCompleter.activate()
134  ipython.set_hook('complete_command', _cppCompleter.complete, re_key=r"[(.*)[\.,::,\->](.*)]|(.*)")
135 
137  _cppCompleter.deactivate()
138 
def rreplace(s, old, new, occurrence)
Definition: cppcompleter.py:4
def load_ipython_extension(ipython)
def unload_ipython_extension(ipython)