wxMaxima
Loading...
Searching...
No Matches
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 <thread>
34#include <algorithm>
35#include <memory>
36#include <mutex>
37#include <wx/wx.h>
38#include <wx/xml/xml.h>
39#include <wx/event.h>
40#include <wx/dir.h>
41#include <vector>
42#include <wx/object.h>
43#include <wx/regex.h>
44#include <wx/filename.h>
45#include <wx/hashmap.h>
46#include "Configuration.h"
47#include "precomp.h"
48#include "Version.h"
49#include <unordered_map>
50/* The autocompletion logic
51
52 The wordlists for autocompletion for keywords come from several sources:
53
54 - wxMaxima::ReadLoadSymbols receive the contents of maxima's variables
55 "values" and "functions" after a package is loaded.
56 - all words that appear in the worksheet
57 - and a list of maxima's builtin commands.
58*/
59class AutoComplete : public wxEvtHandler
60{
61 typedef std::unordered_map <wxString, int, wxStringHash> WorksheetWords;
62public:
63 using WordList = std::vector<wxString>;
64
67 {
68 command = 0,
76 };
77 explicit AutoComplete(Configuration *configuration);
78
80 virtual ~AutoComplete();
81
83 void LoadSymbols();
84
91 void LoadBuiltinSymbols();
92
94 void AddSymbol(wxString fun, autoCompletionType type = command);
96 void AddSymbols(wxString xml);
98 void AddSymbols(wxXmlDocument xml);
100 void AddSymbols_Backgroundtask_string(wxString xml);
102 void AddSymbols_Backgroundtask(wxXmlDocument xmldoc);
103
104
106 void UpdateDemoFiles(wxString partial, const wxString &maximaDir);
108 void UpdateLoadFiles(wxString partial, const wxString &maximaDir);
110 void UpdateGeneralFiles(wxString partial, const wxString &maximaDir);
111
113 void AddWorksheetWords(const WordList &words);
114 void AddWorksheetWords(WordList::const_iterator begin, WordList::const_iterator end);
115
117 void ClearWorksheetWords();
119 void ClearDemofileList();
120
122 std::vector<wxString> CompleteSymbol(wxString partial, autoCompletionType type = command);
124 static wxString FixTemplate(wxString templ);
126 std::vector<wxString> GetDemoFilesList();
128 std::vector<wxString> GetSymbolList();
130 bool HasDemofile(const wxString &commandname);
131
132private:
134 Configuration *m_configuration;
136 void LoadableFiles_BackgroundTask(wxString sharedir, wxString demodir);
138 void BuiltinSymbols_BackgroundTask();
139
141 void UpdateLoadFiles_BackgroundTask(wxString partial, wxString maximaDir);
143 std::vector<wxString> m_builtInLoadFiles;
145 std::vector<wxString> m_builtInDemoFiles;
146
148 class GetGeneralFiles : public wxDirTraverser
149 {
150 public:
151 explicit GetGeneralFiles(std::vector<wxString>& files,
152 std::mutex *lock,
153 const wxString &prefix = wxEmptyString) :
154 m_files(files), m_lock(lock), m_prefix(prefix) { }
155 wxDirTraverseResult OnFile(const wxString& filename) override
156 {
157 wxFileName newItemName(filename);
158 wxString newItem = "\"" + m_prefix + newItemName.GetFullName() + "\"";
159 newItem.Replace(wxFileName::GetPathSeparator(), "/");
160 {
161 const std::lock_guard<std::mutex> lock(*m_lock);
162 if(std::find(m_files.begin(), m_files.end(), newItem) == m_files.end())
163 m_files.push_back(newItem);
164 }
165 return wxDIR_CONTINUE;
166 }
167 wxDirTraverseResult OnDir(const wxString& dirname) override
168 {
169 wxFileName newItemName(dirname);
170 wxString newItem = "\"" + m_prefix + newItemName.GetFullName() + "/\"";
171 newItem.Replace(wxFileName::GetPathSeparator(), "/");
172 {
173 const std::lock_guard<std::mutex> lock(*m_lock);
174 if(std::find(m_files.begin(), m_files.end(), newItem) == m_files.end())
175 m_files.push_back(newItem);
176 }
177 return wxDIR_IGNORE;
178 }
179 std::vector<wxString> GetResult(){
180 const std::lock_guard<std::mutex> lock(*m_lock);
181 return m_files;
182 }
183 protected:
184 std::vector<wxString>& m_files;
185 std::mutex *m_lock;
186 wxString m_prefix;
187 };
188
190 class GetMacFiles_includingSubdirs : public wxDirTraverser
191 {
192 public:
193 explicit GetMacFiles_includingSubdirs(std::vector<wxString>& files,
194 std::mutex *lock,
195 const wxString &prefix = wxEmptyString) :
196 m_files(files), m_lock(lock), m_prefix(prefix) { }
197 wxDirTraverseResult OnFile(const wxString& filename) override
198 {
199 if(
200 (filename.EndsWith(".mac"))||
201 (filename.EndsWith(".lisp"))||
202 (filename.EndsWith(".wxm"))
203 )
204 {
205 wxFileName newItemName(filename);
206 wxString newItem = "\"" + m_prefix + newItemName.GetName() + "\"";
207 newItem.Replace(wxFileName::GetPathSeparator(), "/");
208 {
209 const std::lock_guard<std::mutex> lock(*m_lock);
210 if(std::find(m_files.begin(), m_files.end(), newItem) == m_files.end())
211 m_files.push_back(newItem);
212 }
213 }
214 return wxDIR_CONTINUE;
215 }
216 wxDirTraverseResult OnDir(const wxString& dirname) override
217 {
218 if((dirname.EndsWith(".git")) ||
219 (dirname.EndsWith("/share/share")) ||
220 (dirname.EndsWith("/src/src")) ||
221 (dirname.EndsWith("/doc/doc")) ||
222 (dirname.EndsWith("/interfaces/interfaces"))
223 )
224 return wxDIR_STOP;
225 else
226 return wxDIR_CONTINUE;
227 }
228 std::vector<wxString> GetResult(){
229 const std::lock_guard<std::mutex> lock(*m_lock);
230 return m_files;
231 }
232 protected:
233 std::vector<wxString>& m_files;
234 std::mutex *m_lock;
235 wxString m_prefix;
236 };
237
239 class GetMacFiles : public GetMacFiles_includingSubdirs
240 {
241 public:
242 explicit GetMacFiles(std::vector<wxString>& files,
243 std::mutex *lock,
244 const wxString &prefix = wxEmptyString) :
245 GetMacFiles_includingSubdirs(files, lock, prefix){ }
246 wxDirTraverseResult OnDir(const wxString& dirname) override
247 {
248 wxFileName newItemName(dirname);
249 wxString newItem = "\"" + m_prefix + newItemName.GetFullName() + "/\"";
250 newItem.Replace(wxFileName::GetPathSeparator(), "/");
251 {
252 const std::lock_guard<std::mutex> lock(*m_lock);
253 if(std::find(m_files.begin(), m_files.end(), newItem) == m_files.end())
254 m_files.push_back(newItem);
255 }
256 return wxDIR_IGNORE;
257 }
258 };
259
261 class GetDemoFiles_includingSubdirs : public wxDirTraverser
262 {
263 public:
264 explicit GetDemoFiles_includingSubdirs(std::vector<wxString>& files,
265 std::mutex *lock,
266 const wxString &prefix = wxEmptyString) :
267 m_files(files), m_lock(lock), m_prefix(prefix) { }
268 wxDirTraverseResult OnFile(const wxString& filename) override
269 {
270 if(filename.EndsWith(".dem"))
271 {
272 wxFileName newItemName(filename);
273 wxString newItem = "\"" + m_prefix + newItemName.GetName() + "\"";
274 newItem.Replace(wxFileName::GetPathSeparator(), "/");
275 {
276 const std::lock_guard<std::mutex> lock(*m_lock);
277 if(std::find(m_files.begin(), m_files.end(), newItem) == m_files.end())
278 m_files.push_back(newItem);
279 }
280 }
281 return wxDIR_CONTINUE;
282 }
283 wxDirTraverseResult OnDir(const wxString& dirname) override
284 {
285 if((dirname.EndsWith(".git")) ||
286 (dirname.EndsWith("/share/share")) ||
287 (dirname.EndsWith("/src/src")) ||
288 (dirname.EndsWith("/doc/doc")) ||
289 (dirname.EndsWith("/interfaces/interfaces"))
290 )
291 return wxDIR_STOP;
292 else
293 return wxDIR_CONTINUE;
294 }
295 std::vector<wxString> GetResult(){
296 const std::lock_guard<std::mutex> lock(*m_lock);
297 return m_files;
298 }
299 protected:
300 std::vector<wxString>& m_files;
301 std::mutex *m_lock;
302 wxString m_prefix;
303 };
304
306 class GetDemoFiles : public GetDemoFiles_includingSubdirs
307 {
308 public:
309 explicit GetDemoFiles(std::vector<wxString>& files,
310 std::mutex *lock,
311 const wxString &prefix = wxEmptyString) :
312 GetDemoFiles_includingSubdirs(files, lock, prefix){ }
313 virtual wxDirTraverseResult OnDir(const wxString& dirname) override
314 {
315 wxFileName newItemName(dirname);
316 wxString newItem = "\"" + m_prefix + newItemName.GetFullName() + "/\"";
317 newItem.Replace(wxFileName::GetPathSeparator(), "/");
318 {
319 const std::lock_guard<std::mutex> lock(*m_lock);
320 if(std::find(m_files.begin(), m_files.end(), newItem) == m_files.end())
321 m_files.push_back(newItem);
322 }
323 return wxDIR_IGNORE;
324 }
325 };
326
327 jthread m_addSymbols_backgroundThread;
328 jthread m_addFiles_backgroundThread;
330 std::mutex m_keywordsLock;
332 std::vector<std::vector<wxString>> m_wordList;
333 static wxRegEx m_args;
334 WorksheetWords m_worksheetWords;
335};
336
337wxDECLARE_EVENT(NEW_DEMO_FILES_EVENT, wxCommandEvent);
338
339#endif // AUTOCOMPLETE_H
Definition: Autocomplete.h:60
std::vector< wxString > GetSymbolList()
Returns a list of Symbols we know.
Definition: Autocomplete.cpp:63
void AddSymbols_Backgroundtask(wxXmlDocument xmldoc)
The real work of AddSymbols is made here and in the background.
Definition: Autocomplete.cpp:144
virtual ~AutoComplete()
The destructor of AutoComplete.
Definition: Autocomplete.cpp:220
void ClearDemofileList()
Clear the list of files demo() can be applied on.
Definition: Autocomplete.cpp:104
std::vector< wxString > GetDemoFilesList()
Returns a list of demo files we know of.
Definition: Autocomplete.cpp:58
void UpdateDemoFiles(wxString partial, const wxString &maximaDir)
Replace the list of files in the directory the worksheet file is in to the demo files list.
Definition: Autocomplete.cpp:393
void LoadSymbols()
Load all autocomplete symbols wxMaxima knows about by itself.
Definition: Autocomplete.cpp:227
autoCompletionType
All types of things we can autocomplete.
Definition: Autocomplete.h:67
@ tmplte
Command names.
Definition: Autocomplete.h:69
@ generalfile
loadable files
Definition: Autocomplete.h:72
@ demofile
loadable files
Definition: Autocomplete.h:71
@ numberOfTypes
Unit names.
Definition: Autocomplete.h:75
@ unit
Esc commands describing symbols.
Definition: Autocomplete.h:74
@ loadfile
Function templates.
Definition: Autocomplete.h:70
@ esccommand
general files
Definition: Autocomplete.h:73
std::vector< wxString > CompleteSymbol(wxString partial, autoCompletionType type=command)
Returns a list of possible autocompletions for the string "partial".
Definition: Autocomplete.cpp:504
void UpdateLoadFiles(wxString partial, const wxString &maximaDir)
Replace the list of files in the directory the worksheet file is in to the load files list.
Definition: Autocomplete.cpp:462
void LoadBuiltinSymbols()
Makes wxMaxima know all its builtin symbols.
Definition: Autocomplete.cpp:69
void UpdateGeneralFiles(wxString partial, const wxString &maximaDir)
Assemble a list of files.
Definition: Autocomplete.cpp:429
void AddSymbols(wxString xml)
Interprets the XML autocompletable symbol list maxima can send us.
Definition: Autocomplete.cpp:109
void AddWorksheetWords(const WordList &words)
Add words to the list of words that appear in the workSheet's code cells.
Definition: Autocomplete.cpp:216
void AddSymbols_Backgroundtask_string(wxString xml)
The real work of AddSymbols is made here and in the background.
Definition: Autocomplete.cpp:137
static wxString FixTemplate(wxString templ)
Basically runs a regex over templates.
Definition: Autocomplete.cpp:610
bool HasDemofile(const wxString &commandname)
Does a demo file for this command exist?
Definition: Autocomplete.cpp:94
void AddSymbol(wxString fun, autoCompletionType type=command)
Manually add an autocompletable symbol to our symbols lists.
Definition: Autocomplete.cpp:559
void ClearWorksheetWords()
Clear the list of words that appear in the workSheet's code cells.
Definition: Autocomplete.cpp:53
The configuration storage for the current worksheet.
Definition: Configuration.h:85