| 356 | } |
| 357 | |
| 358 | void FormatterPlugin::formatDoc( UICodeEditor* editor ) { |
| 359 | ScopedOp op( |
| 360 | [this]() { |
| 361 | mWorkMutex.lock(); |
| 362 | mWorkersCount++; |
| 363 | }, |
| 364 | [this]() { |
| 365 | mWorkersCount--; |
| 366 | mWorkMutex.unlock(); |
| 367 | mWorkerCondition.notify_all(); |
| 368 | } ); |
| 369 | if ( !mReady ) |
| 370 | return; |
| 371 | |
| 372 | Clock clock; |
| 373 | std::shared_ptr<TextDocument> doc = editor->getDocumentRef(); |
| 374 | auto formatter = supportsFormatter( doc ); |
| 375 | if ( formatter.preferLSPFormatter || formatter.command.empty() ) { |
| 376 | if ( supportsLSPFormatter( doc ) ) |
| 377 | formatDocWithLSP( doc ); |
| 378 | return; |
| 379 | } else if ( doc->getFilePath().empty() ) { |
| 380 | return; |
| 381 | } |
| 382 | std::string path; |
| 383 | if ( doc->isDirty() || !doc->hasFilepath() || formatter.type == FormatterType::Inplace ) { |
| 384 | std::string tmpPath; |
| 385 | if ( !doc->hasFilepath() ) { |
| 386 | tmpPath = |
| 387 | Sys::getTempPath() + ".ecode-" + doc->getFilename() + "." + String::randString( 8 ); |
| 388 | } else { |
| 389 | std::string fileDir( FileSystem::fileRemoveFileName( doc->getFilePath() ) ); |
| 390 | FileSystem::dirAddSlashAtEnd( fileDir ); |
| 391 | tmpPath = fileDir + "." + String::randString( 8 ) + "." + doc->getFilename(); |
| 392 | } |
| 393 | |
| 394 | IOStreamString fileString; |
| 395 | doc->save( fileString, true ); |
| 396 | FileSystem::fileWrite( tmpPath, (Uint8*)fileString.getStreamPointer(), |
| 397 | fileString.getSize() ); |
| 398 | FileSystem::fileHide( tmpPath ); |
| 399 | runFormatter( editor, formatter, tmpPath ); |
| 400 | |
| 401 | if ( formatter.type == FormatterType::Inplace ) { |
| 402 | std::string data; |
| 403 | FileSystem::fileGet( tmpPath, data ); |
| 404 | |
| 405 | auto oldFile = MD5::fromStream( fileString ); |
| 406 | auto newFile = MD5::fromString( data ); |
| 407 | |
| 408 | if ( oldFile != newFile ) { |
| 409 | editor->runOnMainThread( [this, data, editor]() { |
| 410 | std::shared_ptr<TextDocument> doc = editor->getDocumentRef(); |
| 411 | auto pos = doc->getSelection(); |
| 412 | auto scroll = editor->getScroll(); |
| 413 | doc->resetSelection(); |
| 414 | doc->selectAll(); |
| 415 | doc->setRunningTransaction( true ); |
nothing calls this directly
no test coverage detected