wxMaxima
Autocomplete.h
Go to the documentation of this file.
1 // -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 //
3 // Copyright (C) 2009-2015 Andrej Vodopivec <andrej.vodopivec@gmail.com>
4 // Copyright (C) 2015 Gunter Königsmann <wxMaxima@physikbuch.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // SPDX-License-Identifier: GPL-2.0+
22 
30 #ifndef AUTOCOMPLETE_H
31 #define AUTOCOMPLETE_H
32 
33 #include "precomp.h"
34 #include <thread>
35 #include <memory>
36 #include <wx/wx.h>
37 #include <wx/dir.h>
38 #include <wx/arrstr.h>
39 #include <wx/regex.h>
40 #include <wx/filename.h>
41 #include <vector>
42 #include "Configuration.h"
43 
44 /* The autocompletion logic
45 
46  The wordlists for autocompletion for keywords come from several sources:
47 
48  - wxMaxima::ReadLoadSymbols receive the contents of maxima's variables
49  "values" and "functions" after a package is loaded.
50  - all words that appear in the worksheet
51  - and a list of maxima's builtin commands.
52  */
54 {
55  WX_DECLARE_STRING_HASH_MAP(int, WorksheetWords);
56 
57 public:
58  using WordList = std::vector<wxString>;
59 
62  {
63  command,
70  };
71  explicit AutoComplete(Configuration *configuration);
72 
74  ~AutoComplete();
75 
77  void LoadSymbols();
78 
85  bool LoadBuiltinSymbols();
86 
88  void AddSymbol(wxString fun, autoCompletionType type = command);
90  void AddSymbols(wxString xml);
92  void AddSymbols_Backgroundtask(wxString xml);
93 
95  void UpdateDemoFiles(wxString partial, wxString maximaDir);
97  void UpdateLoadFiles(wxString partial, wxString maximaDir);
99  void UpdateGeneralFiles(wxString partial, wxString maximaDir);
100 
102  void AddWorksheetWords(const WordList &words);
103  void AddWorksheetWords(WordList::const_iterator begin, WordList::const_iterator end);
104 
106  void ClearWorksheetWords();
108  void ClearDemofileList();
109 
111  wxArrayString CompleteSymbol(wxString partial, autoCompletionType type = command);
113  static wxString FixTemplate(wxString templ);
114 
115 private:
117  void AddSymbol_nowait(wxString fun, autoCompletionType type = command);
119  Configuration *m_configuration;
121  void LoadableFiles_BackgroundTask();
123  void BuiltinSymbols_BackgroundTask();
124 
126  void UpdateLoadFiles_BackgroundTask(wxString partial, wxString maximaDir);
128  wxArrayString m_builtInLoadFiles;
130  wxArrayString m_builtInDemoFiles;
131 
133  class GetGeneralFiles : public wxDirTraverser
134  {
135  public:
136  explicit GetGeneralFiles(wxArrayString& files, wxString prefix = wxEmptyString) :
137  m_files(files), m_prefix(prefix) { }
138  virtual wxDirTraverseResult OnFile(const wxString& filename) override
139  {
140  wxFileName newItemName(filename);
141  wxString newItem = "\"" + m_prefix + newItemName.GetFullName() + "\"";
142  newItem.Replace(wxFileName::GetPathSeparator(),"/");
143  if(m_files.Index(newItem) == wxNOT_FOUND)
144  m_files.Add(newItem);
145  return wxDIR_CONTINUE;
146  }
147  virtual wxDirTraverseResult OnDir(const wxString& dirname) override
148  {
149  wxFileName newItemName(dirname);
150  wxString newItem = "\"" + m_prefix + newItemName.GetFullName() + "/\"";
151  newItem.Replace(wxFileName::GetPathSeparator(),"/");
152  if(m_files.Index(newItem) == wxNOT_FOUND)
153  m_files.Add(newItem);
154  return wxDIR_IGNORE;
155  }
156  wxArrayString& GetResult(){return m_files;}
157  protected:
158  wxArrayString& m_files;
159  wxString m_prefix;
160  };
161 
163  class GetMacFiles_includingSubdirs : public wxDirTraverser
164  {
165  public:
166  explicit GetMacFiles_includingSubdirs(wxArrayString& files, wxString prefix = wxEmptyString) :
167  m_files(files), m_prefix(prefix) { }
168  virtual wxDirTraverseResult OnFile(const wxString& filename) override
169  {
170  if(
171  (filename.EndsWith(".mac"))||
172  (filename.EndsWith(".lisp"))||
173  (filename.EndsWith(".wxm"))
174  )
175  {
176  wxFileName newItemName(filename);
177  wxString newItem = "\"" + m_prefix + newItemName.GetName() + "\"";
178  newItem.Replace(wxFileName::GetPathSeparator(),"/");
179  if(m_files.Index(newItem) == wxNOT_FOUND)
180  m_files.Add(newItem);
181  }
182  return wxDIR_CONTINUE;
183  }
184  virtual wxDirTraverseResult OnDir(const wxString& dirname) override
185  {
186  if((dirname.EndsWith(".git")) ||
187  (dirname.EndsWith("/share/share")) ||
188  (dirname.EndsWith("/src/src")) ||
189  (dirname.EndsWith("/doc/doc")) ||
190  (dirname.EndsWith("/interfaces/interfaces"))
191  )
192  return wxDIR_STOP;
193  else
194  return wxDIR_CONTINUE;
195  }
196  wxArrayString& GetResult(){return m_files;}
197  protected:
198  wxArrayString& m_files;
199  wxString m_prefix;
200  };
201 
203  class GetMacFiles : public GetMacFiles_includingSubdirs
204  {
205  public:
206  explicit GetMacFiles(wxArrayString& files, wxString prefix = wxEmptyString) :
207  GetMacFiles_includingSubdirs(files, prefix){ }
208  virtual wxDirTraverseResult OnDir(const wxString& dirname) override
209  {
210  wxFileName newItemName(dirname);
211  wxString newItem = "\"" + m_prefix + newItemName.GetFullName() + "/\"";
212  newItem.Replace(wxFileName::GetPathSeparator(),"/");
213  if(m_files.Index(newItem) == wxNOT_FOUND)
214  m_files.Add(newItem);
215  return wxDIR_IGNORE;
216  }
217  };
218 
220  class GetDemoFiles_includingSubdirs : public wxDirTraverser
221  {
222  public:
223  explicit GetDemoFiles_includingSubdirs(wxArrayString& files, wxString prefix = wxEmptyString) :
224  m_files(files), m_prefix(prefix) { }
225  virtual wxDirTraverseResult OnFile(const wxString& filename) override
226  {
227  if(filename.EndsWith(".dem"))
228  {
229  wxFileName newItemName(filename);
230  wxString newItem = "\"" + m_prefix + newItemName.GetName() + "\"";
231  newItem.Replace(wxFileName::GetPathSeparator(),"/");
232  if(m_files.Index(newItem) == wxNOT_FOUND)
233  m_files.Add(newItem);
234  }
235  return wxDIR_CONTINUE;
236  }
237  virtual wxDirTraverseResult OnDir(const wxString& dirname) override
238  {
239  if((dirname.EndsWith(".git")) ||
240  (dirname.EndsWith("/share/share")) ||
241  (dirname.EndsWith("/src/src")) ||
242  (dirname.EndsWith("/doc/doc")) ||
243  (dirname.EndsWith("/interfaces/interfaces"))
244  )
245  return wxDIR_STOP;
246  else
247  return wxDIR_CONTINUE;
248  }
249  wxArrayString& GetResult(){return m_files;}
250  protected:
251  wxArrayString& m_files;
252  wxString m_prefix;
253  };
254 
256  class GetDemoFiles : public GetDemoFiles_includingSubdirs
257  {
258  public:
259  explicit GetDemoFiles(wxArrayString& files, wxString prefix = wxEmptyString) :
260  GetDemoFiles_includingSubdirs(files, prefix){ }
261  virtual wxDirTraverseResult OnDir(const wxString& dirname) override
262  {
263  wxFileName newItemName(dirname);
264  wxString newItem = "\"" + m_prefix + newItemName.GetFullName() + "/\"";
265  newItem.Replace(wxFileName::GetPathSeparator(),"/");
266  if(m_files.Index(newItem) == wxNOT_FOUND)
267  m_files.Add(newItem);
268  return wxDIR_IGNORE;
269  }
270  };
271 
272  void WaitForBackgroundThread_Symbols();
273  void WaitForBackgroundThread_Files();
274  void WaitForBackgroundThreads();
275 
277  wxArrayString m_wordList[7];
278  static wxRegEx m_args;
279  WorksheetWords m_worksheetWords;
280  std::unique_ptr<std::thread> m_addSymbols_backgroundThread;
281  std::unique_ptr<std::thread> m_addFiles_backgroundThread;
282 };
283 
284 #endif // AUTOCOMPLETE_H
AutoComplete::AddSymbol
void AddSymbol(wxString fun, autoCompletionType type=command)
Manually add an autocompletable symbol to our symbols lists.
Definition: Autocomplete.cpp:517
AutoComplete::~AutoComplete
~AutoComplete()
The destructor of AutoComplete.
Definition: Autocomplete.cpp:171
AutoComplete::ClearWorksheetWords
void ClearWorksheetWords()
Clear the list of words that appear in the workSheet's code cells.
Definition: Autocomplete.cpp:47
AutoComplete::AddSymbols_Backgroundtask
void AddSymbols_Backgroundtask(wxString xml)
The real work of AddSymbols is made here and in the background.
Definition: Autocomplete.cpp:73
AutoComplete::esccommand
@ esccommand
general files
Definition: Autocomplete.h:68
AutoComplete::UpdateDemoFiles
void UpdateDemoFiles(wxString partial, wxString maximaDir)
Replace the list of files in the directory the worksheet file is in to the demo files list.
Definition: Autocomplete.cpp:339
AutoComplete::generalfile
@ generalfile
loadable files
Definition: Autocomplete.h:67
AutoComplete::UpdateLoadFiles
void UpdateLoadFiles(wxString partial, wxString maximaDir)
Replace the list of files in the directory the worksheet file is in to the load files list.
Definition: Autocomplete.cpp:414
AutoComplete::CompleteSymbol
wxArrayString CompleteSymbol(wxString partial, autoCompletionType type=command)
Returns a list of possible autocompletions for the string "partial".
Definition: Autocomplete.cpp:455
AutoComplete::UpdateGeneralFiles
void UpdateGeneralFiles(wxString partial, wxString maximaDir)
Assemble a list of files.
Definition: Autocomplete.cpp:378
AutoComplete::demofile
@ demofile
loadable files
Definition: Autocomplete.h:66
AutoComplete::AddSymbols
void AddSymbols(wxString xml)
Interprets the XML autocompletable symbol list maxima can send us.
Definition: Autocomplete.cpp:63
AutoComplete::AddWorksheetWords
void AddWorksheetWords(const WordList &words)
Add words to the list of words that appear in the workSheet's code cells.
Definition: Autocomplete.cpp:166
AutoComplete::loadfile
@ loadfile
Function templates.
Definition: Autocomplete.h:65
AutoComplete::LoadBuiltinSymbols
bool LoadBuiltinSymbols()
Definition: Autocomplete_Builtins.cpp:32
AutoComplete::unit
@ unit
Esc commands describing symbols.
Definition: Autocomplete.h:69
AutoComplete
Definition: Autocomplete.h:53
AutoComplete::FixTemplate
static wxString FixTemplate(wxString templ)
Basically runs a regex over templates.
Definition: Autocomplete.cpp:566
AutoComplete::autoCompletionType
autoCompletionType
All types of things we can autocomplete.
Definition: Autocomplete.h:61
Configuration
Definition: Configuration.h:83
AutoComplete::LoadSymbols
void LoadSymbols()
Load all autocomplete symbols wxMaxima knows about by itself.
Definition: Autocomplete.cpp:176
AutoComplete::tmplte
@ tmplte
Command names.
Definition: Autocomplete.h:64
AutoComplete::ClearDemofileList
void ClearDemofileList()
Clear the list of files demo() can be applied on.
Definition: Autocomplete.cpp:53