Line data Source code
1 : //* This file is part of the MOOSE framework
2 : //* https://mooseframework.inl.gov
3 : //*
4 : //* All rights reserved, see COPYRIGHT for full restrictions
5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 : //*
7 : //* Licensed under LGPL 2.1, please see LICENSE for details
8 : //* https://www.gnu.org/licenses/lgpl-2.1.html
9 :
10 : #pragma once
11 :
12 : #include "MooseApp.h"
13 : #include "wasplsp/LSP.h"
14 : #include "wasplsp/ServerImpl.h"
15 : #include "wasplsp/Connection.h"
16 : #include "libmesh/ignore_warnings.h"
17 : #include "wasplsp/IOStreamConnection.h"
18 : #include "libmesh/restore_warnings.h"
19 : #include "waspcore/Object.h"
20 : #include "wasphit/HITNodeView.h"
21 : #include "waspsiren/SIRENInterpreter.h"
22 : #include "waspsiren/SIRENResultSet.h"
23 : #include "waspplot/CustomPlotFile.h"
24 : #include <string>
25 : #include <memory>
26 : #include <set>
27 : #include <map>
28 :
29 : class MooseServer : public wasp::lsp::ServerImpl
30 : {
31 : public:
32 : MooseServer(MooseApp & moose_app);
33 :
34 4 : virtual ~MooseServer() = default;
35 :
36 : /**
37 : * Get read / write connection - specific to this server implemention.
38 : * @return - shared pointer to the server's read / write connection
39 : */
40 2 : std::shared_ptr<wasp::lsp::Connection> getConnection() { return _connection; }
41 :
42 : /**
43 : * Public interface for writable check app reference with error checks.
44 : * @return - writable reference to check app for current input document
45 : */
46 : MooseApp & getCheckApp();
47 :
48 : /**
49 : * Override number of discrete points in continuous distribution plots.
50 : * Used by plotting unit test to lower sampling resolution for testing.
51 : * @param num_points - number of discrete points for distribution plots
52 : */
53 2 : void setDistPlotNumPoints(std::size_t num_points) { _dist_plot_num_points = num_points; }
54 :
55 : private:
56 : /**
57 : * SortedLocationNodes - type alias for set of nodes sorted by location
58 : */
59 : using SortedLocationNodes =
60 : std::set<wasp::HITNodeView,
61 : std::function<bool(const wasp::HITNodeView &, const wasp::HITNodeView &)>>;
62 :
63 : /**
64 : * Parse document for diagnostics - specific to this server implemention.
65 : * @param diagnosticsList - data array of diagnostics data objects to fill
66 : * @return - true if completed successfully - does not indicate parse fail
67 : */
68 : bool parseDocumentForDiagnostics(wasp::DataArray & diagnosticsList);
69 :
70 : /**
71 : * Add paths from includes and FileName parameters for client to watch.
72 : */
73 : void addResourcesForDocument();
74 :
75 : /**
76 : * Recursively walk input to gather all FileName type parameter values.
77 : * @param filename_vals - set to fill up with FileName parameter values
78 : * @param parent - nodeview for recursive tree traversal starting point
79 : */
80 : void getFileNameTypeValues(std::set<std::string> & filename_vals, wasp::HITNodeView parent);
81 :
82 : /**
83 : * Gather document completion items - specific to this server implemention.
84 : * @param completionItems - data array of completion item objects to fill
85 : * @param is_incomplete - flag indicating if the completions are complete
86 : * @param line - line to be used for completions gathering logic
87 : * @param character - column to be used for completions gathering logic
88 : * @return - true if the gathering of items completed successfully
89 : */
90 : bool gatherDocumentCompletionItems(wasp::DataArray & completionItems,
91 : bool & is_incomplete,
92 : int line,
93 : int character);
94 :
95 : /**
96 : * Get names of parameters and subblocks specified in given input node.
97 : * @param parent_node - object node context under which to gather input
98 : * @param existing_params - set to fill with parameter names from input
99 : * @param existing_subblocks - set to fill with subblock names in input
100 : */
101 : void getExistingInput(wasp::HITNodeView parent_node,
102 : std::set<std::string> & existing_params,
103 : std::set<std::string> & existing_subblocks);
104 :
105 : /**
106 : * Get all global parameters, action parameters, and object parameters.
107 : * @param valid_params - collection to fill with valid input parameters
108 : * @param object_path - full node path where autocomplete was requested
109 : * @param object_type - type of object where autocomplete was requested
110 : * @param obj_act_tasks - set for adding in all MooseObjectAction tasks
111 : */
112 : void getAllValidParameters(InputParameters & valid_params,
113 : const std::string & object_path,
114 : const std::string & object_type,
115 : std::set<std::string> & obj_act_tasks);
116 :
117 : /**
118 : * Get all action parameters using requested object path to collection.
119 : * @param valid_params - collection for filling action input parameters
120 : * @param object_path - full node path where autocomplete was requested
121 : * @param obj_act_tasks - set for adding in all MooseObjectAction tasks
122 : */
123 : void getActionParameters(InputParameters & valid_params,
124 : const std::string & object_path,
125 : std::set<std::string> & obj_act_tasks);
126 :
127 : /**
128 : * Get all object parameters using requested object path to collection.
129 : * @param valid_params - collection for filling object input parameters
130 : * @param object_type - type of object where autocomplete was requested
131 : * @param obj_act_tasks - tasks to verify object type with valid syntax
132 : */
133 : void getObjectParameters(InputParameters & valid_params,
134 : std::string object_type,
135 : const std::set<std::string> & obj_act_tasks);
136 :
137 : /**
138 : * Add parameters that were previously gathered to list for completion.
139 : * @param completionItems - list of completion objects to be filled out
140 : * @param valid_params - all valid parameters to add to completion list
141 : * @param existing_params - set of parameters already existing in input
142 : * @param replace_line_beg - start line of autocompletion replace range
143 : * @param replace_char_beg - start column of autocomplete replace range
144 : * @param replace_line_end - end line of autocomplete replacement range
145 : * @param replace_char_end - end column of autocompletion replace range
146 : * @param filtering_prefix - beginning text to filter list if not empty
147 : * @return - true if filling of completion items completed successfully
148 : */
149 : bool addParametersToList(wasp::DataArray & completionItems,
150 : const InputParameters & valid_params,
151 : const std::set<std::string> & existing_params,
152 : int replace_line_beg,
153 : int replace_char_beg,
154 : int replace_line_end,
155 : int replace_char_end,
156 : const std::string & filtering_prefix);
157 :
158 : /**
159 : * Add subblocks to completion list for request path, line, and column.
160 : * @param completionItems - list of completion objects to be filled out
161 : * @param object_path - full node path where autocomplete was requested
162 : * @param replace_line_beg - start line of autocompletion replace range
163 : * @param replace_char_beg - start column of autocomplete replace range
164 : * @param replace_line_end - end line of autocomplete replacement range
165 : * @param replace_char_end - end column of autocompletion replace range
166 : * @param filtering_prefix - beginning text to filter list if not empty
167 : * @return - true if filling of completion items completed successfully
168 : */
169 : bool addSubblocksToList(wasp::DataArray & completionItems,
170 : const std::string & object_path,
171 : int replace_line_beg,
172 : int replace_char_beg,
173 : int replace_line_end,
174 : int replace_char_end,
175 : const std::string & filtering_prefix,
176 : bool request_on_block_decl);
177 :
178 : /**
179 : * Add parameter values to completion list for request line and column.
180 : * @param completionItems - list of completion objects to be filled out
181 : * @param valid_params - all valid parameters used for value completion
182 : * @param existing_params - set of parameters already existing in input
183 : * @param existing_subblocks - active and inactive subblock name values
184 : * @param param_name - name of input parameter for value autocompletion
185 : * @param obj_act_tasks - tasks to verify object type with valid syntax
186 : * @param object_path - full node path where autocomplete was requested
187 : * @param replace_line_beg - start line of autocompletion replace range
188 : * @param replace_char_beg - start column of autocomplete replace range
189 : * @param replace_line_end - end line of autocomplete replacement range
190 : * @param replace_char_end - end column of autocompletion replace range
191 : * @return - true if filling of completion items completed successfully
192 : */
193 : bool addValuesToList(wasp::DataArray & completionItems,
194 : const InputParameters & valid_params,
195 : const std::set<std::string> & existing_params,
196 : const std::set<std::string> & existing_subblocks,
197 : const std::string & param_name,
198 : const std::set<std::string> & obj_act_tasks,
199 : const std::string & object_path,
200 : int replace_line_beg,
201 : int replace_char_beg,
202 : int replace_line_end,
203 : int replace_char_end);
204 :
205 : /**
206 : * Fill map of all options and descriptions if parameter is moose enum.
207 : * @param moose_enum_param - parameter to get documentation and options
208 : * @param options_and_descs - map to fill with options and descriptions
209 : */
210 : template <typename MooseEnumType>
211 : void getEnumsAndDocs(MooseEnumType & moose_enum_param,
212 : std::map<std::string, std::string> & options_and_descs);
213 :
214 : /**
215 : * Supplement completion list with objects in warehouses if applicable.
216 : * @param param_type - parameter type string to pick suitable warehouse
217 : * @param options_and_descs - map to fill with options and descriptions
218 : */
219 : void addObjectsFromWarehouses(const std::string & param_type,
220 : std::map<std::string, std::string> & options_and_descs);
221 :
222 : /**
223 : * Gather definition locations - specific to this server implemention.
224 : * @param definitionLocations - data array of locations objects to fill
225 : * @param line - line to be used for locations gathering logic
226 : * @param character - column to be used for locations gathering logic
227 : * @return - true if the gathering of locations completed successfully
228 : */
229 : bool
230 : gatherDocumentDefinitionLocations(wasp::DataArray & definitionLocations, int line, int character);
231 :
232 : /**
233 : * Get set of nodes from associated path lookups matching value string.
234 : * @param location_nodes - set to fill with lookup nodes matching value
235 : * @param clean_type - cpp type string used for key finding input paths
236 : * @param val_string - specified value used for gathering input lookups
237 : */
238 : void getInputLookupDefinitionNodes(SortedLocationNodes & location_nodes,
239 : const std::string & clean_type,
240 : const std::string & val_string);
241 :
242 : /**
243 : * Add set of nodes sorted by location to definition or reference list.
244 : * @param defsOrRefsLocations - data array of locations objects to fill
245 : * @param location_nodes - set of nodes that have locations to be added
246 : * @return - true if filling of location objects completed successfully
247 : */
248 : bool addLocationNodesToList(wasp::DataArray & defsOrRefsLocations,
249 : const SortedLocationNodes & location_nodes);
250 :
251 : /**
252 : * Get hover display text - logic specific to this server implemention.
253 : * @param display_text - string reference to add hover text for display
254 : * @param line - zero-based line to use for finding node and hover text
255 : * @param character - zero-based column for finding node and hover text
256 : * @return - true if display text was added or left empty without error
257 : */
258 : bool getHoverDisplayText(std::string & display_text, int line, int character);
259 :
260 : /**
261 : * Gather references locations - specific to this server implemention.
262 : * @param referencesLocations - data array of locations objects to fill
263 : * @param line - line to be used for locations gathering logic
264 : * @param character - column to be used for locations gathering logic
265 : * @param include_declaration - flag indicating declaration inclusion
266 : * @return - true if the gathering of locations completed successfully
267 : */
268 : bool gatherDocumentReferencesLocations(wasp::DataArray & referencesLocations,
269 : int line,
270 : int character,
271 : bool include_declaration);
272 :
273 : /**
274 : * Recursively walk input to gather all nodes matching value and types.
275 : * @param match_nodes - set to fill with nodes matching value and types
276 : * @param view_parent - nodeview used to start recursive tree traversal
277 : * @param target_value -
278 : * @param target_types -
279 : */
280 : void getNodesByValueAndTypes(SortedLocationNodes & match_nodes,
281 : wasp::HITNodeView view_parent,
282 : const std::string & target_value,
283 : const std::set<std::string> & target_types);
284 :
285 : /**
286 : * Gather formatting text edits - specific to this server implemention.
287 : * @param formattingTextEdits - data array of text edit objects to fill
288 : * @param tab_size - value of the size of a tab in spaces for formatting
289 : * @param insert_spaces - flag indicating whether to use spaces for tabs
290 : * @return - true if the gathering of text edits completed successfully
291 : */
292 : bool gatherDocumentFormattingTextEdits(wasp::DataArray & formattingTextEdits,
293 : int tab_size,
294 : bool insert_spaces);
295 :
296 : /**
297 : * Recursively walk down whole nodeview tree while formatting document.
298 : * @param parent - nodeview for recursive tree traversal starting point
299 : * @param prev_line - line of last print for blanks and inline comments
300 : * @param level - current level in document tree to use for indentation
301 : * @return - formatted string that gets appended to each recursive call
302 : */
303 : std::string formatDocument(wasp::HITNodeView parent, std::size_t & prev_line, std::size_t level);
304 :
305 : /**
306 : * Gather document symbols - specific to this server implemention.
307 : * @param documentSymbols - data array of symbols data objects to fill
308 : * @return - true if the gathering of symbols completed successfully
309 : */
310 : bool gatherDocumentSymbols(wasp::DataArray & documentSymbols);
311 :
312 : /**
313 : * Recursively fill document symbols from the given node.
314 : * @param view_parent - nodeview used in recursive tree traversal
315 : * @param data_parent - data object with array of symbol children
316 : * @return - true if no problems with this level of the resursion
317 : */
318 : bool traverseParseTreeAndFillSymbols(wasp::HITNodeView view_parent,
319 : wasp::DataObject & data_parent);
320 :
321 : /**
322 : * Get completion item kind value that client may use for icon in list.
323 : * @param valid_params - valid parameters used for completion item kind
324 : * @param param_name - name of input parameter for completion item kind
325 : * @param clean_type - type to decide if reference completion item kind
326 : * @param is_param - boolean denoting if kind is for parameter or value
327 : * @return - enumerated kind value that client may use for icon in list
328 : */
329 : int getCompletionItemKind(const InputParameters & valid_params,
330 : const std::string & param_name,
331 : const std::string & clean_type,
332 : bool is_param);
333 :
334 : /**
335 : * Get document symbol kind value that client may use for outline icon.
336 : * @param symbol_node - node that will be added to symbol tree for kind
337 : * @return - enumerated kind value that client may use for outline icon
338 : */
339 : int getDocumentSymbolKind(wasp::HITNodeView symbol_node);
340 :
341 : /**
342 : * Get required parameter completion text list for given subblock path.
343 : * @param subblock_path - subblock path for finding required parameters
344 : * @param subblock_type - subblock type for finding required parameters
345 : * @param existing_params - set of parameters already existing in input
346 : * @param indent_spaces - indentation to be added before each parameter
347 : * @return - list of required parameters to use in subblock insert text
348 : */
349 : std::string getRequiredParamsText(const std::string & subblock_path,
350 : const std::string & subblock_type,
351 : const std::set<std::string> & existing_params,
352 : const std::string & indent_spaces);
353 :
354 : /**
355 : * Gather extension responses - specific to this server implemention.
356 : * @param extensionResponses - data array of custom responses to fill
357 : * @param extensionMethod - name for current extension request method
358 : * @param line - zero-based line to use for logic of custom extension
359 : * @param character - zero-based column for logic of custom extension
360 : * @return - true if request successfully handled with response built
361 : */
362 : bool gatherExtensionResponses(wasp::DataArray & extensionResponses,
363 : const std::string & extensionMethod,
364 : int line,
365 : int character);
366 :
367 : /**
368 : * Build CustomPlot extension responses when method name is plotting.
369 : * @param plotting_responses - array for CustomPlot responses to fill
370 : * @param line - zero-based line to use for logic of custom extension
371 : * @param character - zero-based column for logic of custom extension
372 : * @return - true if request successfully handled with response built
373 : */
374 : bool gatherPlottingResponses(wasp::DataArray & plotting_responses, int line, int character);
375 :
376 : /**
377 : * Gather function data, build CustomPlot object, and add to responses.
378 : * @param plotting_responses - array to be filled by CustomPlot objects
379 : * @param problem - problem to query warehouses when building plot data
380 : * @param object_name - name of request object to use for problem query
381 : * @param object_type - type of request object to use in title for plot
382 : */
383 : void buildFuncPlotResponse(wasp::DataArray & plotting_responses,
384 : FEProblemBase & problem,
385 : const std::string & object_name,
386 : const std::string & object_type);
387 :
388 : /**
389 : * Compute PDF and CDF, build CustomPlot objects, and add to responses.
390 : * @param plotting_responses - array to be filled by CustomPlot objects
391 : * @param problem - problem to query warehouses when building plot data
392 : * @param object_name - name of request object to use for problem query
393 : * @param object_type - type of request object to use in title for plot
394 : */
395 : void buildDistPlotResponses(wasp::DataArray & plotting_responses,
396 : FEProblemBase & problem,
397 : const std::string & object_name,
398 : const std::string & object_type);
399 :
400 : /**
401 : * Build CustomPlot graph with provided keys, values, and plot title.
402 : * @param plot_object - CustomPlot object to be built into line graph
403 : * @param plot_title - title for plot composed of block name and type
404 : * @param x_axis_label - label for x-axis of plot dependent upon type
405 : * @param y_axis_label - label for y-axis of plot dependent upon type
406 : * @param graph_keys - x values of function or distribution for graph
407 : * @param graph_vals - y values of function or distribution for graph
408 : */
409 : void buildLineGraphPlot(wasp::CustomPlot & plot_object,
410 : const std::string & plot_title,
411 : const std::string & x_axis_label,
412 : const std::string & y_axis_label,
413 : const std::vector<double> & graph_keys,
414 : const std::vector<double> & graph_vals);
415 :
416 : /**
417 : * Read from connection into object - specific to this server's connection.
418 : * @param object - reference to object to be read into
419 : * @return - true if the read from the connection completed successfully
420 : */
421 0 : bool connectionRead(wasp::DataObject & object) { return _connection->read(object, errors); }
422 :
423 : /**
424 : * Write object json to connection - specific to this server's connection.
425 : * @param object - reference to object with contents to write to connection
426 : * @return - true if the write to the connection completed successfully
427 : */
428 0 : bool connectionWrite(wasp::DataObject & object) { return _connection->write(object, errors); }
429 :
430 : /**
431 : * @return Whether or not the root is valid
432 : *
433 : * Will be true if the app is valid, the root is not nullptr, and the root node view is not null
434 : */
435 : bool rootIsValid() const;
436 :
437 : /**
438 : * Helper for storing the state for a single document
439 : */
440 : struct CheckState
441 : {
442 26 : CheckState(std::shared_ptr<Parser> & parser) : parser(parser) {}
443 : std::shared_ptr<Parser> parser;
444 : std::unique_ptr<MooseApp> app;
445 : };
446 :
447 : /**
448 : * @return The check state for the current document path, if any
449 : */
450 : ///@{
451 : const CheckState * queryCheckState() const;
452 : CheckState * queryCheckState();
453 : ///@}
454 : /**
455 : * @return The check app for the current document path, if any
456 : */
457 : ///@{
458 : const MooseApp * queryCheckApp() const;
459 : MooseApp * queryCheckApp();
460 : ///@}
461 : /**
462 : * @return The check parser for the current document path, if any
463 : */
464 : ///@{
465 : const Parser * queryCheckParser() const;
466 : Parser * queryCheckParser();
467 : ///@}
468 : /**
469 : * @return The root node from the check parser for the current document path, if any
470 : */
471 : const hit::Node * queryRoot() const;
472 :
473 : /**
474 : * @return The root node from the check parser for the current document path, with error checking
475 : * on if it exists
476 : */
477 : const hit::Node & getRoot() const;
478 :
479 : /**
480 : * @brief _moose_app - reference to parent application that owns this server
481 : */
482 : MooseApp & _moose_app;
483 :
484 : /**
485 : * @brief _check_state - map from document paths to state (parser, app, text)
486 : */
487 : std::map<std::string, CheckState> _check_state;
488 :
489 : /**
490 : * @brief _connection - shared pointer to this server's read / write iostream
491 : */
492 : std::shared_ptr<wasp::lsp::IOStreamConnection> _connection;
493 :
494 : /**
495 : * @brief _syntax_to_subblocks - map of syntax paths to valid subblocks
496 : */
497 : std::map<std::string, std::set<std::string>> _syntax_to_subblocks;
498 :
499 : /**
500 : * @brief _type_to_input_paths - map of parameter types to lookup paths
501 : */
502 : std::map<std::string, std::set<std::string>> _type_to_input_paths;
503 :
504 : /**
505 : * @brief _type_to_input_paths - map of lookup paths to parameter types
506 : */
507 : std::map<std::string, std::set<std::string>> _input_path_to_types;
508 :
509 : /**
510 : * @brief _formatting_tab_size - number of indent spaces for formatting
511 : */
512 : std::size_t _formatting_tab_size;
513 :
514 : /**
515 : * @brief _dist_plot_num_points - distribution plot sampling resolution
516 : */
517 : std::size_t _dist_plot_num_points;
518 :
519 : /**
520 : * @brief _dist_plot_quantile_bound - epsilon to bound plot range tails
521 : */
522 : double _dist_plot_quantile_bound;
523 : };
|