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 <string>
24 : #include <memory>
25 : #include <set>
26 : #include <map>
27 :
28 : class MooseServer : public wasp::lsp::ServerImpl
29 : {
30 : public:
31 : MooseServer(MooseApp & moose_app);
32 :
33 2 : virtual ~MooseServer() = default;
34 :
35 : /**
36 : * Get read / write connection - specific to this server implemention.
37 : * @return - shared pointer to the server's read / write connection
38 : */
39 1 : std::shared_ptr<wasp::lsp::Connection> getConnection() { return _connection; }
40 :
41 : private:
42 : /**
43 : * SortedLocationNodes - type alias for set of nodes sorted by location
44 : */
45 : using SortedLocationNodes =
46 : std::set<wasp::HITNodeView,
47 : std::function<bool(const wasp::HITNodeView &, const wasp::HITNodeView &)>>;
48 :
49 : /**
50 : * Parse document for diagnostics - specific to this server implemention.
51 : * @param diagnosticsList - data array of diagnostics data objects to fill
52 : * @return - true if completed successfully - does not indicate parse fail
53 : */
54 : bool parseDocumentForDiagnostics(wasp::DataArray & diagnosticsList);
55 :
56 : /**
57 : * Update document text changes - specific to this server implemention.
58 : * @param replacement_text - text to be replaced over the provided range
59 : * @param start_line - starting replace line number ( zero-based )
60 : * @param start_character - starting replace column number ( zero-based )
61 : * @param end_line - ending replace line number ( zero-based )
62 : * @param end_character - ending replace column number ( zero-based )
63 : * @param range_length - length of replace range - server specific
64 : * @return - true if the document text was updated successfully
65 : */
66 : bool updateDocumentTextChanges(const std::string & replacement_text,
67 : int start_line,
68 : int start_character,
69 : int end_line,
70 : int end_character,
71 : int range_length);
72 :
73 : /**
74 : * Gather document completion items - specific to this server implemention.
75 : * @param completionItems - data array of completion item objects to fill
76 : * @param is_incomplete - flag indicating if the completions are complete
77 : * @param line - line to be used for completions gathering logic
78 : * @param character - column to be used for completions gathering logic
79 : * @return - true if the gathering of items completed successfully
80 : */
81 : bool gatherDocumentCompletionItems(wasp::DataArray & completionItems,
82 : bool & is_incomplete,
83 : int line,
84 : int character);
85 :
86 : /**
87 : * Get names of parameters and subblocks specified in given input node.
88 : * @param parent_node - object node context under which to gather input
89 : * @param existing_params - set to fill with parameter names from input
90 : * @param existing_subblocks - set to fill with subblock names in input
91 : */
92 : void getExistingInput(wasp::HITNodeView parent_node,
93 : std::set<std::string> & existing_params,
94 : std::set<std::string> & existing_subblocks);
95 :
96 : /**
97 : * Get all global parameters, action parameters, and object parameters.
98 : * @param valid_params - collection to fill with valid input parameters
99 : * @param object_path - full node path where autocomplete was requested
100 : * @param object_type - type of object where autocomplete was requested
101 : * @param obj_act_tasks - set for adding in all MooseObjectAction tasks
102 : */
103 : void getAllValidParameters(InputParameters & valid_params,
104 : const std::string & object_path,
105 : const std::string & object_type,
106 : std::set<std::string> & obj_act_tasks);
107 :
108 : /**
109 : * Get all action parameters using requested object path to collection.
110 : * @param valid_params - collection for filling action input parameters
111 : * @param object_path - full node path where autocomplete was requested
112 : * @param obj_act_tasks - set for adding in all MooseObjectAction tasks
113 : */
114 : void getActionParameters(InputParameters & valid_params,
115 : const std::string & object_path,
116 : std::set<std::string> & obj_act_tasks);
117 :
118 : /**
119 : * Get all object parameters using requested object path to collection.
120 : * @param valid_params - collection for filling object input parameters
121 : * @param object_type - type of object where autocomplete was requested
122 : * @param obj_act_tasks - tasks to verify object type with valid syntax
123 : */
124 : void getObjectParameters(InputParameters & valid_params,
125 : std::string object_type,
126 : const std::set<std::string> & obj_act_tasks);
127 :
128 : /**
129 : * Add parameters that were previously gathered to list for completion.
130 : * @param completionItems - list of completion objects to be filled out
131 : * @param valid_params - all valid parameters to add to completion list
132 : * @param existing_params - set of parameters already existing in input
133 : * @param replace_line_beg - start line of autocompletion replace range
134 : * @param replace_char_beg - start column of autocomplete replace range
135 : * @param replace_line_end - end line of autocomplete replacement range
136 : * @param replace_char_end - end column of autocompletion replace range
137 : * @param filtering_prefix - beginning text to filter list if not empty
138 : * @return - true if filling of completion items completed successfully
139 : */
140 : bool addParametersToList(wasp::DataArray & completionItems,
141 : const InputParameters & valid_params,
142 : const std::set<std::string> & existing_params,
143 : int replace_line_beg,
144 : int replace_char_beg,
145 : int replace_line_end,
146 : int replace_char_end,
147 : const std::string & filtering_prefix);
148 :
149 : /**
150 : * Add subblocks to completion list for request path, line, and column.
151 : * @param completionItems - list of completion objects to be filled out
152 : * @param object_path - full node path where autocomplete was requested
153 : * @param replace_line_beg - start line of autocompletion replace range
154 : * @param replace_char_beg - start column of autocomplete replace range
155 : * @param replace_line_end - end line of autocomplete replacement range
156 : * @param replace_char_end - end column of autocompletion replace range
157 : * @param filtering_prefix - beginning text to filter list if not empty
158 : * @return - true if filling of completion items completed successfully
159 : */
160 : bool addSubblocksToList(wasp::DataArray & completionItems,
161 : const std::string & object_path,
162 : int replace_line_beg,
163 : int replace_char_beg,
164 : int replace_line_end,
165 : int replace_char_end,
166 : const std::string & filtering_prefix,
167 : bool request_on_block_decl);
168 :
169 : /**
170 : * Add parameter values to completion list for request line and column.
171 : * @param completionItems - list of completion objects to be filled out
172 : * @param valid_params - all valid parameters used for value completion
173 : * @param existing_params - set of parameters already existing in input
174 : * @param existing_subblocks - active and inactive subblock name values
175 : * @param param_name - name of input parameter for value autocompletion
176 : * @param obj_act_tasks - tasks to verify object type with valid syntax
177 : * @param object_path - full node path where autocomplete was requested
178 : * @param replace_line_beg - start line of autocompletion replace range
179 : * @param replace_char_beg - start column of autocomplete replace range
180 : * @param replace_line_end - end line of autocomplete replacement range
181 : * @param replace_char_end - end column of autocompletion replace range
182 : * @return - true if filling of completion items completed successfully
183 : */
184 : bool addValuesToList(wasp::DataArray & completionItems,
185 : const InputParameters & valid_params,
186 : const std::set<std::string> & existing_params,
187 : const std::set<std::string> & existing_subblocks,
188 : const std::string & param_name,
189 : const std::set<std::string> & obj_act_tasks,
190 : const std::string & object_path,
191 : int replace_line_beg,
192 : int replace_char_beg,
193 : int replace_line_end,
194 : int replace_char_end);
195 :
196 : /**
197 : * Fill map of all options and descriptions if parameter is moose enum.
198 : * @param moose_enum_param - parameter to get documentation and options
199 : * @param options_and_descs - map to fill with options and descriptions
200 : */
201 : template <typename MooseEnumType>
202 : void getEnumsAndDocs(MooseEnumType & moose_enum_param,
203 : std::map<std::string, std::string> & options_and_descs);
204 :
205 : /**
206 : * Gather definition locations - specific to this server implemention.
207 : * @param definitionLocations - data array of locations objects to fill
208 : * @param line - line to be used for locations gathering logic
209 : * @param character - column to be used for locations gathering logic
210 : * @return - true if the gathering of locations completed successfully
211 : */
212 : bool
213 : gatherDocumentDefinitionLocations(wasp::DataArray & definitionLocations, int line, int character);
214 :
215 : /**
216 : * Get set of nodes from associated path lookups matching value string.
217 : * @param location_nodes - set to fill with lookup nodes matching value
218 : * @param clean_type - cpp type string used for key finding input paths
219 : * @param val_string - specified value used for gathering input lookups
220 : */
221 : void getInputLookupDefinitionNodes(SortedLocationNodes & location_nodes,
222 : const std::string & clean_type,
223 : const std::string & val_string);
224 :
225 : /**
226 : * Add set of nodes sorted by location to definition or reference list.
227 : * @param defsOrRefsLocations - data array of locations objects to fill
228 : * @param location_nodes - set of nodes that have locations to be added
229 : * @return - true if filling of location objects completed successfully
230 : */
231 : bool addLocationNodesToList(wasp::DataArray & defsOrRefsLocations,
232 : const SortedLocationNodes & location_nodes);
233 :
234 : /**
235 : * Get hover display text - logic specific to this server implemention.
236 : * @param display_text - string reference to add hover text for display
237 : * @param line - zero-based line to use for finding node and hover text
238 : * @param character - zero-based column for finding node and hover text
239 : * @return - true if display text was added or left empty without error
240 : */
241 : bool getHoverDisplayText(std::string & display_text, int line, int character);
242 :
243 : /**
244 : * Gather references locations - specific to this server implemention.
245 : * @param referencesLocations - data array of locations objects to fill
246 : * @param line - line to be used for locations gathering logic
247 : * @param character - column to be used for locations gathering logic
248 : * @param include_declaration - flag indicating declaration inclusion
249 : * @return - true if the gathering of locations completed successfully
250 : */
251 : bool gatherDocumentReferencesLocations(wasp::DataArray & referencesLocations,
252 : int line,
253 : int character,
254 : bool include_declaration);
255 :
256 : /**
257 : * Recursively walk input to gather all nodes matching value and types.
258 : * @param match_nodes - set to fill with nodes matching value and types
259 : * @param view_parent - nodeview used to start recursive tree traversal
260 : * @param target_value -
261 : * @param target_types -
262 : */
263 : void getNodesByValueAndTypes(SortedLocationNodes & match_nodes,
264 : wasp::HITNodeView view_parent,
265 : const std::string & target_value,
266 : const std::set<std::string> & target_types);
267 :
268 : /**
269 : * Gather formatting text edits - specific to this server implemention.
270 : * @param formattingTextEdits - data array of text edit objects to fill
271 : * @param tab_size - value of the size of a tab in spaces for formatting
272 : * @param insert_spaces - flag indicating whether to use spaces for tabs
273 : * @return - true if the gathering of text edits completed successfully
274 : */
275 : bool gatherDocumentFormattingTextEdits(wasp::DataArray & formattingTextEdits,
276 : int tab_size,
277 : bool insert_spaces);
278 :
279 : /**
280 : * Recursively walk down whole nodeview tree while formatting document.
281 : * @param parent - nodeview for recursive tree traversal starting point
282 : * @param prev_line - line of last print for blanks and inline comments
283 : * @param level - current level in document tree to use for indentation
284 : * @return - formatted string that gets appended to each recursive call
285 : */
286 : std::string formatDocument(wasp::HITNodeView parent, std::size_t & prev_line, std::size_t level);
287 :
288 : /**
289 : * Gather document symbols - specific to this server implemention.
290 : * @param documentSymbols - data array of symbols data objects to fill
291 : * @return - true if the gathering of symbols completed successfully
292 : */
293 : bool gatherDocumentSymbols(wasp::DataArray & documentSymbols);
294 :
295 : /**
296 : * Recursively fill document symbols from the given node.
297 : * @param view_parent - nodeview used in recursive tree traversal
298 : * @param data_parent - data object with array of symbol children
299 : * @return - true if no problems with this level of the resursion
300 : */
301 : bool traverseParseTreeAndFillSymbols(wasp::HITNodeView view_parent,
302 : wasp::DataObject & data_parent);
303 :
304 : /**
305 : * Get completion item kind value that client may use for icon in list.
306 : * @param valid_params - valid parameters used for completion item kind
307 : * @param param_name - name of input parameter for completion item kind
308 : * @param clean_type - type to decide if reference completion item kind
309 : * @param is_param - boolean denoting if kind is for parameter or value
310 : * @return - enumerated kind value that client may use for icon in list
311 : */
312 : int getCompletionItemKind(const InputParameters & valid_params,
313 : const std::string & param_name,
314 : const std::string & clean_type,
315 : bool is_param);
316 :
317 : /**
318 : * Get document symbol kind value that client may use for outline icon.
319 : * @param symbol_node - node that will be added to symbol tree for kind
320 : * @return - enumerated kind value that client may use for outline icon
321 : */
322 : int getDocumentSymbolKind(wasp::HITNodeView symbol_node);
323 :
324 : /**
325 : * Get required parameter completion text list for given subblock path.
326 : * @param subblock_path - subblock path for finding required parameters
327 : * @param subblock_type - subblock type for finding required parameters
328 : * @param existing_params - set of parameters already existing in input
329 : * @param indent_spaces - indentation to be added before each parameter
330 : * @return - list of required parameters to use in subblock insert text
331 : */
332 : std::string getRequiredParamsText(const std::string & subblock_path,
333 : const std::string & subblock_type,
334 : const std::set<std::string> & existing_params,
335 : const std::string & indent_spaces);
336 :
337 : /**
338 : * Read from connection into object - specific to this server's connection.
339 : * @param object - reference to object to be read into
340 : * @return - true if the read from the connection completed successfully
341 : */
342 0 : bool connectionRead(wasp::DataObject & object) { return _connection->read(object, errors); }
343 :
344 : /**
345 : * Write object json to connection - specific to this server's connection.
346 : * @param object - reference to object with contents to write to connection
347 : * @return - true if the write to the connection completed successfully
348 : */
349 0 : bool connectionWrite(wasp::DataObject & object) { return _connection->write(object, errors); }
350 :
351 : /**
352 : * @return Whether or not the root is valid
353 : *
354 : * Will be true if the app is valid, the root is not nullptr, and the root node view is not null
355 : */
356 : bool rootIsValid() const;
357 :
358 : /**
359 : * @return The current root node
360 : */
361 : hit::Node & getRoot();
362 :
363 : /**
364 : * @return Input check application for document path from current operation
365 : */
366 : std::shared_ptr<MooseApp> getCheckApp() const;
367 :
368 : /**
369 : * @return up to date text string associated with current document path
370 : */
371 : const std::string & getDocumentText() const;
372 :
373 : /**
374 : * @brief _moose_app - reference to parent application that owns this server
375 : */
376 : MooseApp & _moose_app;
377 :
378 : /**
379 : * @brief _check_apps - map from document paths to input check applications
380 : */
381 : std::map<std::string, std::shared_ptr<MooseApp>> _check_apps;
382 :
383 : /**
384 : * @brief _path_to_text - map of document paths to current text strings
385 : */
386 : std::map<std::string, std::string> _path_to_text;
387 :
388 : /**
389 : * @brief _connection - shared pointer to this server's read / write iostream
390 : */
391 : std::shared_ptr<wasp::lsp::IOStreamConnection> _connection;
392 :
393 : /**
394 : * @brief _syntax_to_subblocks - map of syntax paths to valid subblocks
395 : */
396 : std::map<std::string, std::set<std::string>> _syntax_to_subblocks;
397 :
398 : /**
399 : * @brief _type_to_input_paths - map of parameter types to lookup paths
400 : */
401 : std::map<std::string, std::set<std::string>> _type_to_input_paths;
402 :
403 : /**
404 : * @brief _type_to_input_paths - map of lookup paths to parameter types
405 : */
406 : std::map<std::string, std::set<std::string>> _input_path_to_types;
407 :
408 : /**
409 : * @brief _formatting_tab_size - number of indent spaces for formatting
410 : */
411 : std::size_t _formatting_tab_size;
412 : };
|