|
15 | 15 | # You should have received a copy of the GNU General Public License
|
16 | 16 | # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
17 | 17 |
|
| 18 | +import re |
| 19 | + |
18 | 20 | from ycm.client.base_request import BaseRequest, BuildRequestData
|
19 | 21 | from ycm import vimsupport
|
20 | 22 |
|
@@ -165,10 +167,22 @@ def _HandleFixitResponse( self ):
|
165 | 167 | ( len( fixits ) == 1 and
|
166 | 168 | self._command == 'FixIt' and
|
167 | 169 | fixits[ 0 ].get( 'kind' ) != 'quickfix' ) ):
|
168 |
| - fixit_index = vimsupport.SelectFromList( |
169 |
| - "FixIt suggestion(s) available at this location. " |
170 |
| - "Which one would you like to apply?", |
171 |
| - [ fixit[ 'text' ] for fixit in fixits ] ) |
| 170 | + # If the user provided another argument, use it as a pattern to |
| 171 | + # automatically select the fixit to apply. |
| 172 | + if len( self._arguments ) == 2: |
| 173 | + pat = self._arguments[ 1 ] |
| 174 | + fixit_index = _FindFirstIndex( |
| 175 | + lambda fixit: re.search( pat, fixit[ 'text' ] ), |
| 176 | + fixits ) |
| 177 | + if fixit_index is None: |
| 178 | + vimsupport.PostVimMessage( |
| 179 | + f'No fixits found for current line matching {pat}' ) |
| 180 | + return |
| 181 | + else: |
| 182 | + fixit_index = vimsupport.SelectFromList( |
| 183 | + "FixIt suggestion(s) available at this location. " |
| 184 | + "Which one would you like to apply?", |
| 185 | + [ fixit[ 'text' ] for fixit in fixits ] ) |
172 | 186 | chosen_fixit = fixits[ fixit_index ]
|
173 | 187 | if chosen_fixit[ 'resolve' ]:
|
174 | 188 | self._request_data.update( { 'fixit': chosen_fixit } )
|
@@ -226,3 +240,9 @@ def GetCommandResponse( arguments, extra_data = None ):
|
226 | 240 | silent = True )
|
227 | 241 | # Block here to get the response
|
228 | 242 | return request.StringResponse()
|
| 243 | + |
| 244 | + |
| 245 | +def _FindFirstIndex( matcher, items ): |
| 246 | + return next( |
| 247 | + ( i for ( i, item ) in enumerate( items ) if matcher( item ) ), |
| 248 | + None ) |
0 commit comments