Apply the source file deltas supplied in |chunks| to arbitrary files. |chunks| is a list of changes defined by ycmd.responses.FixItChunk, which may apply arbitrary modifications to arbitrary files. If a file specified in a particular chunk is not currently open in a visible buffer (i.e., on
( chunks, silent=False )
| 1003 | |
| 1004 | |
| 1005 | def ReplaceChunks( chunks, silent=False ): |
| 1006 | """Apply the source file deltas supplied in |chunks| to arbitrary files. |
| 1007 | |chunks| is a list of changes defined by ycmd.responses.FixItChunk, |
| 1008 | which may apply arbitrary modifications to arbitrary files. |
| 1009 | |
| 1010 | If a file specified in a particular chunk is not currently open in a visible |
| 1011 | buffer (i.e., one in a window visible in the current tab), we: |
| 1012 | - issue a warning to the user that we're going to open new files (and offer |
| 1013 | her the option to abort cleanly) |
| 1014 | - open the file in a new split, make the changes, then hide the buffer. |
| 1015 | |
| 1016 | If for some reason a file could not be opened or changed, raises RuntimeError. |
| 1017 | Otherwise, returns no meaningful value.""" |
| 1018 | |
| 1019 | # We apply the edits file-wise for efficiency. |
| 1020 | chunks_by_file = _SortChunksByFile( chunks ) |
| 1021 | |
| 1022 | # We sort the file list simply to enable repeatable testing. |
| 1023 | sorted_file_list = sorted( chunks_by_file.keys() ) |
| 1024 | |
| 1025 | if not silent: |
| 1026 | # Make sure the user is prepared to have her screen mutilated by the new |
| 1027 | # buffers. |
| 1028 | num_files_to_open = _GetNumNonVisibleFiles( sorted_file_list ) |
| 1029 | |
| 1030 | if num_files_to_open > 0: |
| 1031 | if not Confirm( |
| 1032 | FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( num_files_to_open ) ): |
| 1033 | return |
| 1034 | |
| 1035 | # Store the list of locations where we applied changes. We use this to display |
| 1036 | # the quickfix window showing the user where we applied changes. |
| 1037 | locations = [] |
| 1038 | |
| 1039 | for filepath in sorted_file_list: |
| 1040 | buffer_num, close_window = _OpenFileInSplitIfNeeded( filepath ) |
| 1041 | |
| 1042 | locations.extend( ReplaceChunksInBuffer( chunks_by_file[ filepath ], |
| 1043 | vim.buffers[ buffer_num ] ) ) |
| 1044 | |
| 1045 | # When opening tons of files, we don't want to have a split for each new |
| 1046 | # file, as this simply does not scale, so we open the window, make the |
| 1047 | # edits, then hide the window. |
| 1048 | if close_window: |
| 1049 | # Some plugins (I'm looking at you, syntastic) might open a location list |
| 1050 | # for the window we just opened. We don't want that location list hanging |
| 1051 | # around, so we close it. lclose is a no-op if there is no location list. |
| 1052 | vim.command( 'lclose' ) |
| 1053 | |
| 1054 | # Note that this doesn't lose our changes. It simply "hides" the buffer, |
| 1055 | # which can later be re-accessed via the quickfix list or `:ls` |
| 1056 | vim.command( 'hide' ) |
| 1057 | |
| 1058 | # Open the quickfix list, populated with entries for each location we changed. |
| 1059 | if not silent: |
| 1060 | if locations: |
| 1061 | SetQuickFixList( locations ) |
| 1062 |
nothing calls this directly
no test coverage detected