| 242 | } |
| 243 | |
| 244 | void ClassDefinitionImpl::addComponentClause(const std::string& text) { |
| 245 | try { |
| 246 | antlr4::ANTLRInputStream testStream(text); |
| 247 | modelicaLexer testLexer(&testStream); |
| 248 | antlr4::CommonTokenStream testTokens(&testLexer); |
| 249 | modelicaParser testParser(&testTokens); |
| 250 | const auto* componentClauseCtx = testParser.component_clause(); |
| 251 | if (!componentClauseCtx || testParser.getNumberOfSyntaxErrors() > 0) { |
| 252 | LOG_AND_THROW("Invalid component clause syntax: " + text); |
| 253 | } |
| 254 | } catch (const std::exception& e) { |
| 255 | LOG_AND_THROW("Failed to parse component clause: " + std::string(e.what())); |
| 256 | } |
| 257 | |
| 258 | // Find insertion point - look for the composition within this class |
| 259 | auto* composition = ctx()->class_specifier()->long_class_specifier()->composition(); |
| 260 | if (!composition) { |
| 261 | LOG_AND_THROW("Cannot find composition in class definition"); |
| 262 | } |
| 263 | |
| 264 | // Get the element list (vector of elements) |
| 265 | auto elementList = composition->element_list(); |
| 266 | |
| 267 | // Find the end position - either after the last element or at the composition end |
| 268 | antlr4::Token* stopToken = nullptr; |
| 269 | if (!elementList.empty()) { |
| 270 | // Insert after the last element |
| 271 | stopToken = elementList.back()->getStop(); |
| 272 | } else { |
| 273 | // Empty element list, insert at the start of composition |
| 274 | stopToken = composition->getStart(); |
| 275 | } |
| 276 | if (!stopToken) { |
| 277 | LOG_AND_THROW("Cannot determine insertion position for component clause"); |
| 278 | } |
| 279 | |
| 280 | // Get the full file content |
| 281 | auto file = modelicaFileShared(); |
| 282 | if (!file) { |
| 283 | LOG_AND_THROW("ModelicaFile is no longer available"); |
| 284 | } |
| 285 | |
| 286 | std::string fullContent = file->getText(); |
| 287 | |
| 288 | // Calculate the insertion position using ANTLR token positions |
| 289 | size_t insertPos = stopToken->getStopIndex() + 1; |
| 290 | |
| 291 | // Insert the new component clause with proper formatting |
| 292 | fullContent.insert(insertPos, "\n " + text + "\n"); |
| 293 | |
| 294 | // Reparse the entire file |
| 295 | file->parse(fullContent); |
| 296 | markComponentClausesDirty(); |
| 297 | } |
| 298 | |
| 299 | void ClassDefinitionImpl::markComponentClausesDirty() { |
| 300 | m_componentClausesDirty = true; |
nothing calls this directly
no test coverage detected