| 125 | } |
| 126 | |
| 127 | void GlobalSearchController::replaceInFiles( std::string replaceText, |
| 128 | std::shared_ptr<ProjectSearch::ResultModel> model, |
| 129 | std::function<void( int )> onDoneCb, |
| 130 | bool bufferOnlyMode, |
| 131 | ProjectSearch::SearchConfig searchConfig ) { |
| 132 | int count = 0; |
| 133 | if ( model->isResultFromSymbolReference() ) { |
| 134 | // TODO Implement replacement from result from symbol reference |
| 135 | return onDoneCb( count ); |
| 136 | } |
| 137 | |
| 138 | const ProjectSearch::Result& res = model->getResult(); |
| 139 | bool hasCaptures = |
| 140 | model->isResultFromPatternMatch() && LuaPattern::hasMatches( replaceText, "$%d+" ); |
| 141 | |
| 142 | if ( bufferOnlyMode ) { |
| 143 | std::vector<ProjectSearch::ResultData> pendingResults; |
| 144 | |
| 145 | for ( const auto& fileResult : res ) { |
| 146 | if ( !fileResult.selected ) |
| 147 | continue; |
| 148 | |
| 149 | if ( fileResult.openDoc ) { |
| 150 | for ( const auto& result : fileResult.results ) { |
| 151 | if ( !result.selected ) |
| 152 | continue; |
| 153 | |
| 154 | auto oldSel = fileResult.openDoc->getSelections(); |
| 155 | fileResult.openDoc->setSelection( result.position ); |
| 156 | fileResult.openDoc->replaceSelection( |
| 157 | hasCaptures ? getReplaceText( result, replaceText ) : replaceText ); |
| 158 | fileResult.openDoc->setSelection( oldSel ); |
| 159 | count++; |
| 160 | } |
| 161 | } else { |
| 162 | pendingResults.push_back( fileResult ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if ( pendingResults.empty() ) { |
| 167 | onDoneCb( count ); |
| 168 | } else { |
| 169 | processNextReplaceInBuffer( std::move( replaceText ), std::move( searchConfig ), |
| 170 | std::move( onDoneCb ), count, std::move( pendingResults ), |
| 171 | hasCaptures ); |
| 172 | } |
| 173 | |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if ( hasCaptures ) { |
| 178 | for ( const auto& fileResult : res ) { |
| 179 | std::vector<std::pair<Int64, Int64>> replacements; |
| 180 | std::vector<std::string> replaceTexts; |
| 181 | |
| 182 | for ( const auto& result : fileResult.results ) { |
| 183 | if ( !result.selected ) |
| 184 | continue; |
nothing calls this directly
no test coverage detected