MCPcopy Create free account
hub / github.com/SpartanJ/eepp / formatDoc

Method formatDoc

src/tools/ecode/plugins/formatter/formatterplugin.cpp:358–441  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

356}
357
358void 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 );

Callers

nothing calls this directly

Calls 15

fromStringFunction · 0.85
infoFunction · 0.85
getDocumentRefMethod · 0.80
getFilePathMethod · 0.80
hasFilepathMethod · 0.80
getFilenameMethod · 0.80
getStreamPointerMethod · 0.80
runOnMainThreadMethod · 0.80
resetSelectionMethod · 0.80
setRunningTransactionMethod · 0.80
textInputMethod · 0.80
setScrollMethod · 0.80

Tested by

no test coverage detected