2018-01-06 07:16:30 -05:00
|
|
|
# Copyright (C) 2011-2018 YouCompleteMe contributors
|
2012-08-04 20:46:54 -04:00
|
|
|
#
|
|
|
|
# This file is part of YouCompleteMe.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import absolute_import
|
2017-03-09 09:57:27 -05:00
|
|
|
# Not installing aliases from python-future; it's unreliable and slow.
|
2016-02-27 19:12:24 -05:00
|
|
|
from builtins import * # noqa
|
|
|
|
|
|
|
|
from future.utils import iterkeys
|
2012-08-04 20:46:54 -04:00
|
|
|
import vim
|
2013-10-03 13:14:31 -04:00
|
|
|
import os
|
2013-10-04 15:23:33 -04:00
|
|
|
import json
|
2015-08-28 10:26:18 -04:00
|
|
|
import re
|
2018-02-13 17:21:12 -05:00
|
|
|
from collections import defaultdict, namedtuple
|
2017-06-17 17:29:13 -04:00
|
|
|
from ycmd.utils import ( ByteOffsetToCodepointOffset, GetCurrentDirectory,
|
|
|
|
JoinLinesAsUnicode, ToBytes, ToUnicode )
|
2014-05-13 16:09:19 -04:00
|
|
|
from ycmd import user_options_store
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2014-03-04 05:47:43 -05:00
|
|
|
BUFFER_COMMAND_MAP = { 'same-buffer' : 'edit',
|
|
|
|
'horizontal-split' : 'split',
|
|
|
|
'vertical-split' : 'vsplit',
|
|
|
|
'new-tab' : 'tabedit' }
|
|
|
|
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT = (
|
|
|
|
'The requested operation will apply changes to {0} files which are not '
|
|
|
|
'currently open. This will therefore open {0} new files in the hidden '
|
|
|
|
'buffers. The quickfix list can then be used to review the changes. No '
|
|
|
|
'files will be written to disk. Do you wish to continue?' )
|
|
|
|
|
2016-06-19 15:38:07 -04:00
|
|
|
NO_SELECTION_MADE_MSG = "No valid selection was made; aborting."
|
|
|
|
|
2018-02-13 17:21:12 -05:00
|
|
|
# This is the starting value assigned to the sign's id of each buffer. This
|
|
|
|
# value is then incremented for each new sign. This should prevent conflicts
|
|
|
|
# with other plugins using signs.
|
|
|
|
SIGN_BUFFER_ID_INITIAL_VALUE = 100000000
|
|
|
|
|
|
|
|
SIGN_PLACE_REGEX = re.compile(
|
|
|
|
r"^.*=(?P<line>\d+).*=(?P<id>\d+).*=(?P<name>Ycm\w+)$" )
|
|
|
|
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def CurrentLineAndColumn():
|
2012-09-24 22:20:33 -04:00
|
|
|
"""Returns the 0-based current line and 0-based current column."""
|
2012-08-04 20:46:54 -04:00
|
|
|
# See the comment in CurrentColumn about the calculation for the line and
|
|
|
|
# column number
|
|
|
|
line, column = vim.current.window.cursor
|
|
|
|
line -= 1
|
|
|
|
return line, column
|
|
|
|
|
|
|
|
|
2017-07-03 08:52:39 -04:00
|
|
|
def SetCurrentLineAndColumn( line, column ):
|
|
|
|
"""Sets the cursor position to the 0-based line and 0-based column."""
|
|
|
|
# Line from vim.current.window.cursor is 1-based.
|
|
|
|
vim.current.window.cursor = ( line + 1, column )
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def CurrentColumn():
|
2012-08-15 22:39:03 -04:00
|
|
|
"""Returns the 0-based current column. Do NOT access the CurrentColumn in
|
2013-06-09 20:31:17 -04:00
|
|
|
vim.current.line. It doesn't exist yet when the cursor is at the end of the
|
|
|
|
line. Only the chars before the current column exist in vim.current.line."""
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
# vim's columns are 1-based while vim.current.line columns are 0-based
|
|
|
|
# ... but vim.current.window.cursor (which returns a (line, column) tuple)
|
|
|
|
# columns are 0-based, while the line from that same tuple is 1-based.
|
|
|
|
# vim.buffers buffer objects OTOH have 0-based lines and columns.
|
|
|
|
# Pigs have wings and I'm a loopy purple duck. Everything makes sense now.
|
|
|
|
return vim.current.window.cursor[ 1 ]
|
|
|
|
|
|
|
|
|
2014-08-28 14:36:46 -04:00
|
|
|
def CurrentLineContents():
|
2016-02-28 17:42:18 -05:00
|
|
|
return ToUnicode( vim.current.line )
|
2014-08-28 14:36:46 -04:00
|
|
|
|
|
|
|
|
2017-06-17 17:29:13 -04:00
|
|
|
def CurrentLineContentsAndCodepointColumn():
|
|
|
|
"""Returns the line contents as a unicode string and the 0-based current
|
|
|
|
column as a codepoint offset. If the current column is outside the line,
|
|
|
|
returns the column position at the end of the line."""
|
|
|
|
line = CurrentLineContents()
|
|
|
|
byte_column = CurrentColumn()
|
|
|
|
# ByteOffsetToCodepointOffset expects 1-based offset.
|
|
|
|
column = ByteOffsetToCodepointOffset( line, byte_column + 1 ) - 1
|
|
|
|
return line, column
|
|
|
|
|
|
|
|
|
2013-06-09 22:00:49 -04:00
|
|
|
def TextAfterCursor():
|
|
|
|
"""Returns the text after CurrentColumn."""
|
2016-02-28 17:42:18 -05:00
|
|
|
return ToUnicode( vim.current.line[ CurrentColumn(): ] )
|
2013-06-09 22:00:49 -04:00
|
|
|
|
|
|
|
|
2015-08-28 10:26:04 -04:00
|
|
|
def TextBeforeCursor():
|
|
|
|
"""Returns the text before CurrentColumn."""
|
2016-02-28 17:42:18 -05:00
|
|
|
return ToUnicode( vim.current.line[ :CurrentColumn() ] )
|
2015-08-28 10:26:04 -04:00
|
|
|
|
|
|
|
|
2018-01-06 07:16:30 -05:00
|
|
|
def BufferModified( buffer_object ):
|
|
|
|
return buffer_object.options[ 'mod' ]
|
2013-09-06 02:43:14 -04:00
|
|
|
|
|
|
|
|
2018-01-06 07:16:30 -05:00
|
|
|
def GetBufferData( buffer_object ):
|
|
|
|
return {
|
|
|
|
# Add a newline to match what gets saved to disk. See #1455 for details.
|
|
|
|
'contents': JoinLinesAsUnicode( buffer_object ) + '\n',
|
|
|
|
'filetypes': FiletypesForBuffer( buffer_object )
|
|
|
|
}
|
2014-03-22 06:24:16 -04:00
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
|
2018-01-06 07:16:30 -05:00
|
|
|
def GetUnsavedAndSpecifiedBufferData( included_buffer, included_filepath ):
|
2016-09-05 11:33:30 -04:00
|
|
|
"""Build part of the request containing the contents and filetypes of all
|
2018-01-06 07:16:30 -05:00
|
|
|
dirty buffers as well as the buffer |included_buffer| with its filepath
|
|
|
|
|included_filepath|."""
|
|
|
|
buffers_data = { included_filepath: GetBufferData( included_buffer ) }
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
for buffer_object in vim.buffers:
|
2018-01-06 07:16:30 -05:00
|
|
|
if not BufferModified( buffer_object ):
|
2013-09-06 02:43:14 -04:00
|
|
|
continue
|
|
|
|
|
2018-01-06 07:16:30 -05:00
|
|
|
filepath = GetBufferFilepath( buffer_object )
|
|
|
|
if filepath in buffers_data:
|
|
|
|
continue
|
|
|
|
|
|
|
|
buffers_data[ filepath ] = GetBufferData( buffer_object )
|
2013-09-06 02:43:14 -04:00
|
|
|
|
|
|
|
return buffers_data
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2017-11-16 18:52:40 -05:00
|
|
|
def GetBufferNumberForFilename( filename, create_buffer_if_needed = False ):
|
2014-07-07 11:52:17 -04:00
|
|
|
return GetIntValue( u"bufnr('{0}', {1})".format(
|
2014-07-31 13:28:44 -04:00
|
|
|
EscapeForVim( os.path.realpath( filename ) ),
|
2017-11-16 18:52:40 -05:00
|
|
|
int( create_buffer_if_needed ) ) )
|
2013-10-03 13:14:31 -04:00
|
|
|
|
|
|
|
|
2013-10-06 22:45:47 -04:00
|
|
|
def GetCurrentBufferFilepath():
|
|
|
|
return GetBufferFilepath( vim.current.buffer )
|
|
|
|
|
|
|
|
|
2014-01-04 17:28:27 -05:00
|
|
|
def BufferIsVisible( buffer_number ):
|
|
|
|
if buffer_number < 0:
|
|
|
|
return False
|
|
|
|
window_number = GetIntValue( "bufwinnr({0})".format( buffer_number ) )
|
|
|
|
return window_number != -1
|
|
|
|
|
|
|
|
|
2013-10-06 22:45:47 -04:00
|
|
|
def GetBufferFilepath( buffer_object ):
|
|
|
|
if buffer_object.name:
|
2017-11-22 19:56:49 -05:00
|
|
|
return os.path.normpath( ToUnicode( buffer_object.name ) )
|
2013-10-06 22:45:47 -04:00
|
|
|
# Buffers that have just been created by a command like :enew don't have any
|
2016-10-11 22:40:25 -04:00
|
|
|
# buffer name so we use the buffer number for that.
|
|
|
|
return os.path.join( GetCurrentDirectory(), str( buffer_object.number ) )
|
2013-10-06 22:45:47 -04:00
|
|
|
|
|
|
|
|
2017-05-21 10:26:50 -04:00
|
|
|
def GetCurrentBufferNumber():
|
|
|
|
return vim.current.buffer.number
|
|
|
|
|
|
|
|
|
|
|
|
def GetBufferChangedTick( bufnr ):
|
|
|
|
return GetIntValue( 'getbufvar({0}, "changedtick")'.format( bufnr ) )
|
|
|
|
|
|
|
|
|
2018-02-18 07:40:35 -05:00
|
|
|
def CaptureVimCommand( command ):
|
|
|
|
vim.command( 'redir => b:ycm_command' )
|
|
|
|
vim.command( 'silent! {}'.format( command ) )
|
|
|
|
vim.command( 'redir END' )
|
2018-02-20 04:35:54 -05:00
|
|
|
output = ToUnicode( vim.eval( 'b:ycm_command' ) )
|
2018-02-18 07:40:35 -05:00
|
|
|
vim.command( 'unlet b:ycm_command' )
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2018-02-13 17:21:12 -05:00
|
|
|
class DiagnosticSign( namedtuple( 'DiagnosticSign',
|
|
|
|
[ 'id', 'line', 'name', 'buffer_number' ] ) ):
|
|
|
|
# We want two signs that have different ids but the same location to compare
|
|
|
|
# equal. ID doesn't matter.
|
|
|
|
def __eq__( self, other ):
|
|
|
|
return ( self.line == other.line and
|
|
|
|
self.name == other.name and
|
|
|
|
self.buffer_number == other.buffer_number )
|
|
|
|
|
|
|
|
|
|
|
|
def GetSignsInBuffer( buffer_number ):
|
2018-02-18 07:40:35 -05:00
|
|
|
sign_output = CaptureVimCommand(
|
|
|
|
'sign place buffer={}'.format( buffer_number ) )
|
2018-02-13 17:21:12 -05:00
|
|
|
signs = []
|
|
|
|
for line in sign_output.split( '\n' ):
|
|
|
|
match = SIGN_PLACE_REGEX.search( line )
|
|
|
|
if match:
|
|
|
|
signs.append( DiagnosticSign( int( match.group( 'id' ) ),
|
|
|
|
int( match.group( 'line' ) ),
|
|
|
|
match.group( 'name' ),
|
|
|
|
buffer_number ) )
|
|
|
|
return signs
|
|
|
|
|
2014-01-04 17:28:27 -05:00
|
|
|
|
2018-02-13 17:21:12 -05:00
|
|
|
def UnplaceSign( sign ):
|
|
|
|
vim.command( 'sign unplace {0} buffer={1}'.format( sign.id,
|
|
|
|
sign.buffer_number ) )
|
2014-01-04 17:28:27 -05:00
|
|
|
|
2014-06-09 13:18:03 -04:00
|
|
|
|
2018-02-13 17:21:12 -05:00
|
|
|
def PlaceSign( sign ):
|
2017-03-28 15:37:58 -04:00
|
|
|
vim.command( 'sign place {0} name={1} line={2} buffer={3}'.format(
|
2018-02-13 17:21:12 -05:00
|
|
|
sign.id, sign.name, sign.line, sign.buffer_number ) )
|
2014-01-04 17:28:27 -05:00
|
|
|
|
|
|
|
|
2018-02-15 15:38:58 -05:00
|
|
|
class DiagnosticMatch( namedtuple( 'DiagnosticMatch',
|
|
|
|
[ 'id', 'group', 'pattern' ] ) ):
|
|
|
|
def __eq__( self, other ):
|
|
|
|
return ( self.group == other.group and
|
|
|
|
self.pattern == other.pattern )
|
|
|
|
|
|
|
|
|
|
|
|
def GetDiagnosticMatchesInCurrentWindow():
|
|
|
|
vim_matches = vim.eval( 'getmatches()' )
|
|
|
|
return [ DiagnosticMatch( match[ 'id' ],
|
|
|
|
match[ 'group' ],
|
|
|
|
match[ 'pattern' ] )
|
|
|
|
for match in vim_matches if match[ 'group' ].startswith( 'Ycm' ) ]
|
|
|
|
|
|
|
|
|
|
|
|
def AddDiagnosticMatch( match ):
|
|
|
|
return GetIntValue( "matchadd('{}', '{}')".format( match.group,
|
|
|
|
match.pattern ) )
|
2014-01-05 16:30:22 -05:00
|
|
|
|
2018-02-15 15:38:58 -05:00
|
|
|
|
|
|
|
def RemoveDiagnosticMatch( match ):
|
|
|
|
return GetIntValue( "matchdelete({})".format( match.id ) )
|
|
|
|
|
|
|
|
|
|
|
|
def GetDiagnosticMatchPattern( line_num,
|
|
|
|
column_num,
|
|
|
|
line_end_num = None,
|
|
|
|
column_end_num = None ):
|
2014-01-09 18:48:48 -05:00
|
|
|
line_num, column_num = LineAndColumnNumbersClamped( line_num, column_num )
|
|
|
|
|
2016-08-16 10:08:11 -04:00
|
|
|
if not line_end_num or not column_end_num:
|
2018-02-15 15:38:58 -05:00
|
|
|
return '\%{}l\%{}c'.format( line_num, column_num )
|
2016-08-16 10:08:11 -04:00
|
|
|
|
|
|
|
# -1 and then +1 to account for column end not included in the range.
|
|
|
|
line_end_num, column_end_num = LineAndColumnNumbersClamped(
|
|
|
|
line_end_num, column_end_num - 1 )
|
|
|
|
column_end_num += 1
|
2018-02-15 15:38:58 -05:00
|
|
|
return '\%{}l\%{}c\_.\\{{-}}\%{}l\%{}c'.format( line_num,
|
|
|
|
column_num,
|
|
|
|
line_end_num,
|
|
|
|
column_end_num )
|
2014-01-04 21:32:42 -05:00
|
|
|
|
|
|
|
|
2014-01-09 18:48:48 -05:00
|
|
|
# Clamps the line and column numbers so that they are not past the contents of
|
2016-03-25 23:40:17 -04:00
|
|
|
# the buffer. Numbers are 1-based byte offsets.
|
2014-01-09 18:48:48 -05:00
|
|
|
def LineAndColumnNumbersClamped( line_num, column_num ):
|
|
|
|
new_line_num = line_num
|
|
|
|
new_column_num = column_num
|
|
|
|
|
|
|
|
max_line = len( vim.current.buffer )
|
|
|
|
if line_num and line_num > max_line:
|
|
|
|
new_line_num = max_line
|
|
|
|
|
|
|
|
max_column = len( vim.current.buffer[ new_line_num - 1 ] )
|
|
|
|
if column_num and column_num > max_column:
|
|
|
|
new_column_num = max_column
|
|
|
|
|
|
|
|
return new_line_num, new_column_num
|
|
|
|
|
|
|
|
|
2014-01-08 22:09:40 -05:00
|
|
|
def SetLocationList( diagnostics ):
|
2017-12-21 18:23:21 -05:00
|
|
|
"""Set the location list for the current window to the supplied diagnostics"""
|
|
|
|
SetLocationListForWindow( 0, diagnostics )
|
|
|
|
|
|
|
|
|
|
|
|
def GetWindowNumberForBufferDiagnostics( buffer_number ):
|
|
|
|
"""Return an appropriate window number to use for displaying diagnostics
|
|
|
|
associated with the buffer number supplied. Always returns a valid window
|
|
|
|
number or 0 meaning the current window."""
|
|
|
|
|
|
|
|
# Location lists are associated with _windows_ not _buffers_. This makes a lot
|
|
|
|
# of sense, but YCM associates diagnostics with _buffers_, because it is the
|
|
|
|
# buffer that actually gets parsed.
|
|
|
|
#
|
|
|
|
# The heuristic we use is to determine any open window for a specified buffer,
|
|
|
|
# and set that. If there is no such window on the current tab page, we use the
|
|
|
|
# current window (by passing 0 as the window number)
|
|
|
|
|
|
|
|
if buffer_number == vim.current.buffer.number:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
window_number = GetIntValue( "bufwinnr({0})".format( buffer_number ) )
|
|
|
|
|
|
|
|
if window_number < 0:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
return window_number
|
|
|
|
|
|
|
|
|
|
|
|
def SetLocationListForBuffer( buffer_number, diagnostics ):
|
|
|
|
"""Populate the location list of an apppropriate window for the supplied
|
|
|
|
buffer number. See SetLocationListForWindow for format of diagnostics."""
|
|
|
|
return SetLocationListForWindow(
|
|
|
|
GetWindowNumberForBufferDiagnostics( buffer_number ),
|
|
|
|
diagnostics )
|
|
|
|
|
|
|
|
|
|
|
|
def SetLocationListForWindow( window_number, diagnostics ):
|
2017-02-19 08:03:50 -05:00
|
|
|
"""Populate the location list with diagnostics. Diagnostics should be in
|
|
|
|
qflist format; see ":h setqflist" for details."""
|
2017-12-21 18:23:21 -05:00
|
|
|
vim.eval( 'setloclist( {0}, {1} )'.format( window_number,
|
|
|
|
json.dumps( diagnostics ) ) )
|
2014-01-08 22:09:40 -05:00
|
|
|
|
|
|
|
|
2017-02-19 08:03:50 -05:00
|
|
|
def OpenLocationList( focus = False, autoclose = False ):
|
2017-09-17 14:16:21 -04:00
|
|
|
"""Open the location list to the bottom of the current window with its
|
2017-02-19 08:03:50 -05:00
|
|
|
height automatically set to fit all entries. This behavior can be overridden
|
|
|
|
by using the YcmLocationOpened autocommand. When focus is set to True, the
|
|
|
|
location list window becomes the active window. When autoclose is set to True,
|
|
|
|
the location list window is automatically closed after an entry is
|
|
|
|
selected."""
|
2017-09-17 14:16:21 -04:00
|
|
|
vim.command( 'lopen' )
|
2017-02-19 08:03:50 -05:00
|
|
|
|
|
|
|
SetFittingHeightForCurrentWindow()
|
|
|
|
|
|
|
|
if autoclose:
|
|
|
|
# This autocommand is automatically removed when the location list window is
|
|
|
|
# closed.
|
|
|
|
vim.command( 'au WinLeave <buffer> q' )
|
|
|
|
|
|
|
|
if VariableExists( '#User#YcmLocationOpened' ):
|
|
|
|
vim.command( 'doautocmd User YcmLocationOpened' )
|
|
|
|
|
|
|
|
if not focus:
|
|
|
|
JumpToPreviousWindow()
|
|
|
|
|
|
|
|
|
|
|
|
def SetQuickFixList( quickfix_list ):
|
2016-05-31 04:40:25 -04:00
|
|
|
"""Populate the quickfix list and open it. List should be in qflist format:
|
2017-02-19 08:03:50 -05:00
|
|
|
see ":h setqflist" for details."""
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
vim.eval( 'setqflist( {0} )'.format( json.dumps( quickfix_list ) ) )
|
|
|
|
|
2016-05-31 04:40:25 -04:00
|
|
|
|
|
|
|
def OpenQuickFixList( focus = False, autoclose = False ):
|
|
|
|
"""Open the quickfix list to full width at the bottom of the screen with its
|
|
|
|
height automatically set to fit all entries. This behavior can be overridden
|
|
|
|
by using the YcmQuickFixOpened autocommand.
|
2017-02-19 08:03:50 -05:00
|
|
|
See the OpenLocationList function for the focus and autoclose options."""
|
2016-05-31 04:40:25 -04:00
|
|
|
vim.command( 'botright copen' )
|
|
|
|
|
|
|
|
SetFittingHeightForCurrentWindow()
|
|
|
|
|
|
|
|
if autoclose:
|
|
|
|
# This autocommand is automatically removed when the quickfix window is
|
|
|
|
# closed.
|
|
|
|
vim.command( 'au WinLeave <buffer> q' )
|
|
|
|
|
|
|
|
if VariableExists( '#User#YcmQuickFixOpened' ):
|
|
|
|
vim.command( 'doautocmd User YcmQuickFixOpened' )
|
|
|
|
|
|
|
|
if not focus:
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
JumpToPreviousWindow()
|
|
|
|
|
|
|
|
|
2016-05-31 04:40:25 -04:00
|
|
|
def SetFittingHeightForCurrentWindow():
|
|
|
|
window_width = GetIntValue( 'winwidth( 0 )' )
|
|
|
|
fitting_height = 0
|
|
|
|
for line in vim.current.buffer:
|
|
|
|
fitting_height += len( line ) // window_width + 1
|
|
|
|
vim.command( '{0}wincmd _'.format( fitting_height ) )
|
|
|
|
|
|
|
|
|
2014-01-08 22:09:40 -05:00
|
|
|
def ConvertDiagnosticsToQfList( diagnostics ):
|
|
|
|
def ConvertDiagnosticToQfFormat( diagnostic ):
|
2016-01-03 13:55:27 -05:00
|
|
|
# See :h getqflist for a description of the dictionary fields.
|
2014-01-08 22:09:40 -05:00
|
|
|
# Note that, as usual, Vim is completely inconsistent about whether
|
|
|
|
# line/column numbers are 1 or 0 based in its various APIs. Here, it wants
|
2016-01-03 13:55:27 -05:00
|
|
|
# them to be 1-based. The documentation states quite clearly that it
|
|
|
|
# expects a byte offset, by which it means "1-based column number" as
|
|
|
|
# described in :h getqflist ("the first column is 1").
|
2014-01-08 22:09:40 -05:00
|
|
|
location = diagnostic[ 'location' ]
|
2014-06-09 13:03:40 -04:00
|
|
|
line_num = location[ 'line_num' ]
|
2014-05-29 16:25:43 -04:00
|
|
|
|
|
|
|
# libclang can give us diagnostics that point "outside" the file; Vim borks
|
|
|
|
# on these.
|
|
|
|
if line_num < 1:
|
|
|
|
line_num = 1
|
|
|
|
|
2015-08-05 17:09:07 -04:00
|
|
|
text = diagnostic[ 'text' ]
|
|
|
|
if diagnostic.get( 'fixit_available', False ):
|
|
|
|
text += ' (FixIt available)'
|
|
|
|
|
2014-01-08 22:09:40 -05:00
|
|
|
return {
|
2017-11-16 18:52:40 -05:00
|
|
|
'bufnr' : GetBufferNumberForFilename( location[ 'filepath' ],
|
|
|
|
create_buffer_if_needed = True ),
|
2014-05-29 16:25:43 -04:00
|
|
|
'lnum' : line_num,
|
2014-06-09 13:03:40 -04:00
|
|
|
'col' : location[ 'column_num' ],
|
2016-02-27 19:12:24 -05:00
|
|
|
'text' : text,
|
2014-06-03 17:49:53 -04:00
|
|
|
'type' : diagnostic[ 'kind' ][ 0 ],
|
2014-01-08 22:09:40 -05:00
|
|
|
'valid' : 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ ConvertDiagnosticToQfFormat( x ) for x in diagnostics ]
|
|
|
|
|
|
|
|
|
2016-04-30 12:14:48 -04:00
|
|
|
def GetVimGlobalsKeys():
|
|
|
|
return vim.eval( 'keys( g: )' )
|
2013-10-04 15:23:33 -04:00
|
|
|
|
|
|
|
|
2013-10-26 19:22:43 -04:00
|
|
|
def VimExpressionToPythonType( vim_expression ):
|
2016-03-05 13:16:08 -05:00
|
|
|
"""Returns a Python type from the return value of the supplied Vim expression.
|
|
|
|
If the expression returns a list, dict or other non-string type, then it is
|
|
|
|
returned unmodified. If the string return can be converted to an
|
|
|
|
integer, returns an integer, otherwise returns the result converted to a
|
|
|
|
Unicode string."""
|
|
|
|
|
2013-10-26 19:22:43 -04:00
|
|
|
result = vim.eval( vim_expression )
|
2016-03-05 13:16:08 -05:00
|
|
|
if not ( isinstance( result, str ) or isinstance( result, bytes ) ):
|
|
|
|
return result
|
|
|
|
|
2013-10-26 19:22:43 -04:00
|
|
|
try:
|
|
|
|
return int( result )
|
|
|
|
except ValueError:
|
2016-03-05 13:16:08 -05:00
|
|
|
return ToUnicode( result )
|
2013-10-26 19:22:43 -04:00
|
|
|
|
|
|
|
|
2014-03-22 06:24:16 -04:00
|
|
|
def HiddenEnabled( buffer_object ):
|
2018-01-06 07:16:30 -05:00
|
|
|
if buffer_object.options[ 'bh' ] == "hide":
|
2017-07-05 09:10:56 -04:00
|
|
|
return True
|
|
|
|
return GetBoolValue( '&hidden' )
|
2014-03-22 06:24:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
def BufferIsUsable( buffer_object ):
|
|
|
|
return not BufferModified( buffer_object ) or HiddenEnabled( buffer_object )
|
|
|
|
|
|
|
|
|
2017-11-26 12:13:33 -05:00
|
|
|
def EscapeFilepathForVimCommand( filepath ):
|
|
|
|
to_eval = "fnameescape('{0}')".format( EscapeForVim( filepath ) )
|
|
|
|
return GetVariableValue( to_eval )
|
2014-05-19 15:37:30 -04:00
|
|
|
|
|
|
|
|
2015-03-22 21:07:35 -04:00
|
|
|
# Both |line| and |column| need to be 1-based
|
|
|
|
def TryJumpLocationInOpenedTab( filename, line, column ):
|
|
|
|
filepath = os.path.realpath( filename )
|
|
|
|
|
|
|
|
for tab in vim.tabpages:
|
|
|
|
for win in tab.windows:
|
2016-10-27 12:23:20 -04:00
|
|
|
if GetBufferFilepath( win.buffer ) == filepath:
|
2015-03-22 21:07:35 -04:00
|
|
|
vim.current.tabpage = tab
|
|
|
|
vim.current.window = win
|
|
|
|
vim.current.window.cursor = ( line, column - 1 )
|
|
|
|
|
|
|
|
# Center the screen on the jumped-to location
|
|
|
|
vim.command( 'normal! zz' )
|
2015-10-14 03:37:33 -04:00
|
|
|
return True
|
|
|
|
# 'filename' is not opened in any tab pages
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-10-27 12:00:11 -04:00
|
|
|
# Maps User command to vim command
|
|
|
|
def GetVimCommand( user_command, default = 'edit' ):
|
|
|
|
vim_command = BUFFER_COMMAND_MAP.get( user_command, default )
|
2015-10-14 03:37:33 -04:00
|
|
|
if vim_command == 'edit' and not BufferIsUsable( vim.current.buffer ):
|
|
|
|
vim_command = 'split'
|
|
|
|
return vim_command
|
2015-03-22 21:07:35 -04:00
|
|
|
|
|
|
|
|
2013-03-31 23:38:29 -04:00
|
|
|
# Both |line| and |column| need to be 1-based
|
|
|
|
def JumpToLocation( filename, line, column ):
|
|
|
|
# Add an entry to the jumplist
|
2013-04-01 00:01:38 -04:00
|
|
|
vim.command( "normal! m'" )
|
2013-03-31 23:38:29 -04:00
|
|
|
|
2013-10-06 22:45:47 -04:00
|
|
|
if filename != GetCurrentBufferFilepath():
|
2013-03-31 23:38:29 -04:00
|
|
|
# We prefix the command with 'keepjumps' so that opening the file is not
|
|
|
|
# recorded in the jumplist. So when we open the file and move the cursor to
|
|
|
|
# a location in it, the user can use CTRL-O to jump back to the original
|
|
|
|
# location, not to the start of the newly opened file.
|
|
|
|
# Sadly this fails on random occasions and the undesired jump remains in the
|
|
|
|
# jumplist.
|
2014-03-04 05:47:43 -05:00
|
|
|
user_command = user_options_store.Value( 'goto_buffer_command' )
|
2015-03-22 21:07:35 -04:00
|
|
|
|
|
|
|
if user_command == 'new-or-existing-tab':
|
2015-10-14 03:37:33 -04:00
|
|
|
if TryJumpLocationInOpenedTab( filename, line, column ):
|
2015-03-22 21:07:35 -04:00
|
|
|
return
|
2015-10-14 03:37:33 -04:00
|
|
|
user_command = 'new-tab'
|
2015-03-22 21:07:35 -04:00
|
|
|
|
2015-10-27 12:00:11 -04:00
|
|
|
vim_command = GetVimCommand( user_command )
|
2015-10-13 10:46:20 -04:00
|
|
|
try:
|
2017-11-26 12:13:33 -05:00
|
|
|
escaped_filename = EscapeFilepathForVimCommand( filename )
|
|
|
|
vim.command( 'keepjumps {0} {1}'.format( vim_command, escaped_filename ) )
|
2015-10-13 10:46:20 -04:00
|
|
|
# When the file we are trying to jump to has a swap file
|
|
|
|
# Vim opens swap-exists-choices dialog and throws vim.error with E325 error,
|
|
|
|
# or KeyboardInterrupt after user selects one of the options.
|
|
|
|
except vim.error as e:
|
2015-10-14 03:37:33 -04:00
|
|
|
if 'E325' not in str( e ):
|
2015-10-13 10:46:20 -04:00
|
|
|
raise
|
2015-10-14 03:37:33 -04:00
|
|
|
# Do nothing if the target file is still not opened (user chose (Q)uit)
|
|
|
|
if filename != GetCurrentBufferFilepath():
|
|
|
|
return
|
2015-10-13 10:46:20 -04:00
|
|
|
# Thrown when user chooses (A)bort in .swp message box
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return
|
2013-03-31 23:38:29 -04:00
|
|
|
vim.current.window.cursor = ( line, column - 1 )
|
|
|
|
|
|
|
|
# Center the screen on the jumped-to location
|
|
|
|
vim.command( 'normal! zz' )
|
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def NumLinesInBuffer( buffer_object ):
|
2012-08-04 20:46:54 -04:00
|
|
|
# This is actually less than obvious, that's why it's wrapped in a function
|
2013-09-06 02:43:14 -04:00
|
|
|
return len( buffer_object )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2015-06-19 22:46:26 -04:00
|
|
|
# Calling this function from the non-GUI thread will sometimes crash Vim. At
|
|
|
|
# the time of writing, YCM only uses the GUI thread inside Vim (this used to
|
|
|
|
# not be the case).
|
2016-08-28 02:34:09 -04:00
|
|
|
def PostVimMessage( message, warning = True, truncate = False ):
|
|
|
|
"""Display a message on the Vim status line. By default, the message is
|
|
|
|
highlighted and logged to Vim command-line history (see :h history).
|
|
|
|
Unset the |warning| parameter to disable this behavior. Set the |truncate|
|
|
|
|
parameter to avoid hit-enter prompts (see :h hit-enter) when the message is
|
|
|
|
longer than the window width."""
|
|
|
|
echo_command = 'echom' if warning else 'echo'
|
|
|
|
|
|
|
|
# Displaying a new message while previous ones are still on the status line
|
|
|
|
# might lead to a hit-enter prompt or the message appearing without a
|
|
|
|
# newline so we do a redraw first.
|
|
|
|
vim.command( 'redraw' )
|
|
|
|
|
|
|
|
if warning:
|
|
|
|
vim.command( 'echohl WarningMsg' )
|
|
|
|
|
|
|
|
message = ToUnicode( message )
|
2013-10-15 14:19:56 -04:00
|
|
|
|
2016-08-28 02:34:09 -04:00
|
|
|
if truncate:
|
|
|
|
vim_width = GetIntValue( '&columns' )
|
2014-01-04 17:28:27 -05:00
|
|
|
|
2016-08-28 02:34:09 -04:00
|
|
|
message = message.replace( '\n', ' ' )
|
2018-01-30 13:26:22 -05:00
|
|
|
if len( message ) >= vim_width:
|
2016-08-28 02:34:09 -04:00
|
|
|
message = message[ : vim_width - 4 ] + '...'
|
|
|
|
|
|
|
|
old_ruler = GetIntValue( '&ruler' )
|
|
|
|
old_showcmd = GetIntValue( '&showcmd' )
|
|
|
|
vim.command( 'set noruler noshowcmd' )
|
|
|
|
|
|
|
|
vim.command( "{0} '{1}'".format( echo_command,
|
|
|
|
EscapeForVim( message ) ) )
|
|
|
|
|
|
|
|
SetVariableValue( '&ruler', old_ruler )
|
|
|
|
SetVariableValue( '&showcmd', old_showcmd )
|
|
|
|
else:
|
|
|
|
for line in message.split( '\n' ):
|
|
|
|
vim.command( "{0} '{1}'".format( echo_command,
|
|
|
|
EscapeForVim( line ) ) )
|
|
|
|
|
|
|
|
if warning:
|
|
|
|
vim.command( 'echohl None' )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-02-25 04:49:51 -05:00
|
|
|
def PresentDialog( message, choices, default_choice_index = 0 ):
|
|
|
|
"""Presents the user with a dialog where a choice can be made.
|
|
|
|
This will be a dialog for gvim users or a question in the message buffer
|
|
|
|
for vim users or if `set guioptions+=c` was used.
|
|
|
|
|
|
|
|
choices is list of alternatives.
|
|
|
|
default_choice_index is the 0-based index of the default element
|
|
|
|
that will get choosen if the user hits <CR>. Use -1 for no default.
|
|
|
|
|
|
|
|
PresentDialog will return a 0-based index into the list
|
|
|
|
or -1 if the dialog was dismissed by using <Esc>, Ctrl-C, etc.
|
|
|
|
|
2016-06-19 15:38:07 -04:00
|
|
|
If you are presenting a list of options for the user to choose from, such as
|
|
|
|
a list of imports, or lines to insert (etc.), SelectFromList is a better
|
|
|
|
option.
|
|
|
|
|
2013-02-25 04:49:51 -05:00
|
|
|
See also:
|
|
|
|
:help confirm() in vim (Note that vim uses 1-based indexes)
|
|
|
|
|
|
|
|
Example call:
|
|
|
|
PresentDialog("Is this a nice example?", ["Yes", "No", "May&be"])
|
|
|
|
Is this a nice example?
|
|
|
|
[Y]es, (N)o, May(b)e:"""
|
2016-02-19 14:02:58 -05:00
|
|
|
to_eval = "confirm('{0}', '{1}', {2})".format(
|
|
|
|
EscapeForVim( ToUnicode( message ) ),
|
|
|
|
EscapeForVim( ToUnicode( "\n" .join( choices ) ) ),
|
|
|
|
default_choice_index + 1 )
|
2016-08-18 14:39:50 -04:00
|
|
|
try:
|
|
|
|
return GetIntValue( to_eval ) - 1
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return -1
|
2013-02-25 04:49:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
def Confirm( message ):
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
"""Display |message| with Ok/Cancel operations. Returns True if the user
|
|
|
|
selects Ok"""
|
2013-02-25 04:49:51 -05:00
|
|
|
return bool( PresentDialog( message, [ "Ok", "Cancel" ] ) == 0 )
|
|
|
|
|
|
|
|
|
2016-06-19 15:38:07 -04:00
|
|
|
def SelectFromList( prompt, items ):
|
|
|
|
"""Ask the user to select an item from the list |items|.
|
|
|
|
|
|
|
|
Presents the user with |prompt| followed by a numbered list of |items|,
|
|
|
|
from which they select one. The user is asked to enter the number of an
|
|
|
|
item or click it.
|
|
|
|
|
|
|
|
|items| should not contain leading ordinals: they are added automatically.
|
|
|
|
|
2017-12-22 09:00:27 -05:00
|
|
|
Returns the 0-based index in the list |items| that the user selected, or an
|
|
|
|
exception if no valid item was selected.
|
2016-06-19 15:38:07 -04:00
|
|
|
|
|
|
|
See also :help inputlist()."""
|
|
|
|
|
|
|
|
vim_items = [ prompt ]
|
|
|
|
vim_items.extend( [ "{0}: {1}".format( i + 1, item )
|
|
|
|
for i, item in enumerate( items ) ] )
|
|
|
|
|
|
|
|
# The vim documentation warns not to present lists larger than the number of
|
|
|
|
# lines of display. This is sound advice, but there really isn't any sensible
|
|
|
|
# thing we can do in that scenario. Testing shows that Vim just pages the
|
|
|
|
# message; that behaviour is as good as any, so we don't manipulate the list,
|
|
|
|
# or attempt to page it.
|
|
|
|
|
|
|
|
# For an explanation of the purpose of inputsave() / inputrestore(),
|
|
|
|
# see :help input(). Briefly, it makes inputlist() work as part of a mapping.
|
|
|
|
vim.eval( 'inputsave()' )
|
|
|
|
try:
|
|
|
|
# Vim returns the number the user entered, or the line number the user
|
|
|
|
# clicked. This may be wildly out of range for our list. It might even be
|
|
|
|
# negative.
|
|
|
|
#
|
|
|
|
# The first item is index 0, and this maps to our "prompt", so we subtract 1
|
|
|
|
# from the result and return that, assuming it is within the range of the
|
|
|
|
# supplied list. If not, we return negative.
|
|
|
|
#
|
|
|
|
# See :help input() for explanation of the use of inputsave() and inpput
|
|
|
|
# restore(). It is done in try/finally in case vim.eval ever throws an
|
|
|
|
# exception (such as KeyboardInterrupt)
|
2016-08-18 14:39:50 -04:00
|
|
|
selected = GetIntValue( "inputlist( " + json.dumps( vim_items ) + " )" ) - 1
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
selected = -1
|
2016-06-19 15:38:07 -04:00
|
|
|
finally:
|
|
|
|
vim.eval( 'inputrestore()' )
|
|
|
|
|
|
|
|
if selected < 0 or selected >= len( items ):
|
|
|
|
# User selected something outside of the range
|
|
|
|
raise RuntimeError( NO_SELECTION_MADE_MSG )
|
|
|
|
|
|
|
|
return selected
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def EscapeForVim( text ):
|
2016-02-28 22:16:06 -05:00
|
|
|
return ToUnicode( text.replace( "'", "''" ) )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-01-31 19:19:56 -05:00
|
|
|
def CurrentFiletypes():
|
2017-11-26 10:30:59 -05:00
|
|
|
return ToUnicode( vim.eval( "&filetype" ) ).split( '.' )
|
2012-08-05 16:12:10 -04:00
|
|
|
|
|
|
|
|
2017-06-05 14:14:51 -04:00
|
|
|
def GetBufferFiletypes( bufnr ):
|
|
|
|
command = 'getbufvar({0}, "&ft")'.format( bufnr )
|
2017-11-26 10:30:59 -05:00
|
|
|
return ToUnicode( vim.eval( command ) ).split( '.' )
|
2017-06-05 14:14:51 -04:00
|
|
|
|
|
|
|
|
2013-03-03 13:48:34 -05:00
|
|
|
def FiletypesForBuffer( buffer_object ):
|
|
|
|
# NOTE: Getting &ft for other buffers only works when the buffer has been
|
|
|
|
# visited by the user at least once, which is true for modified buffers
|
2018-01-06 07:16:30 -05:00
|
|
|
|
|
|
|
# We don't use
|
|
|
|
#
|
|
|
|
# buffer_object.options[ 'ft' ]
|
|
|
|
#
|
|
|
|
# to get the filetypes because this causes annoying flickering when the buffer
|
|
|
|
# is hidden.
|
|
|
|
return GetBufferFiletypes( buffer_object.number )
|
2013-03-03 13:48:34 -05:00
|
|
|
|
|
|
|
|
2015-08-28 10:26:18 -04:00
|
|
|
def VariableExists( variable ):
|
|
|
|
return GetBoolValue( "exists( '{0}' )".format( EscapeForVim( variable ) ) )
|
|
|
|
|
|
|
|
|
|
|
|
def SetVariableValue( variable, value ):
|
2016-04-30 12:14:48 -04:00
|
|
|
vim.command( "let {0} = {1}".format( variable, json.dumps( value ) ) )
|
2015-08-28 10:26:18 -04:00
|
|
|
|
|
|
|
|
2012-08-05 16:12:10 -04:00
|
|
|
def GetVariableValue( variable ):
|
|
|
|
return vim.eval( variable )
|
2013-02-25 04:49:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
def GetBoolValue( variable ):
|
|
|
|
return bool( int( vim.eval( variable ) ) )
|
2013-09-02 17:45:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
def GetIntValue( variable ):
|
|
|
|
return int( vim.eval( variable ) )
|
2013-10-22 13:51:37 -04:00
|
|
|
|
2015-08-05 17:09:07 -04:00
|
|
|
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
def _SortChunksByFile( chunks ):
|
|
|
|
"""Sort the members of the list |chunks| (which must be a list of dictionaries
|
|
|
|
conforming to ycmd.responses.FixItChunk) by their filepath. Returns a new
|
|
|
|
list in arbitrary order."""
|
|
|
|
|
|
|
|
chunks_by_file = defaultdict( list )
|
|
|
|
|
|
|
|
for chunk in chunks:
|
|
|
|
filepath = chunk[ 'range' ][ 'start' ][ 'filepath' ]
|
|
|
|
chunks_by_file[ filepath ].append( chunk )
|
|
|
|
|
|
|
|
return chunks_by_file
|
|
|
|
|
|
|
|
|
|
|
|
def _GetNumNonVisibleFiles( file_list ):
|
|
|
|
"""Returns the number of file in the iterable list of files |file_list| which
|
|
|
|
are not curerntly open in visible windows"""
|
|
|
|
return len(
|
|
|
|
[ f for f in file_list
|
2017-11-16 18:52:40 -05:00
|
|
|
if not BufferIsVisible( GetBufferNumberForFilename( f ) ) ] )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
def _OpenFileInSplitIfNeeded( filepath ):
|
|
|
|
"""Ensure that the supplied filepath is open in a visible window, opening a
|
|
|
|
new split if required. Returns the buffer number of the file and an indication
|
|
|
|
of whether or not a new split was opened.
|
|
|
|
|
|
|
|
If the supplied filename is already open in a visible window, return just
|
|
|
|
return its buffer number. If the supplied file is not visible in a window
|
|
|
|
in the current tab, opens it in a new vertical split.
|
|
|
|
|
|
|
|
Returns a tuple of ( buffer_num, split_was_opened ) indicating the buffer
|
|
|
|
number and whether or not this method created a new split. If the user opts
|
|
|
|
not to open a file, or if opening fails, this method raises RuntimeError,
|
|
|
|
otherwise, guarantees to return a visible buffer number in buffer_num."""
|
|
|
|
|
2017-11-16 18:52:40 -05:00
|
|
|
buffer_num = GetBufferNumberForFilename( filepath )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
|
|
|
# We only apply changes in the current tab page (i.e. "visible" windows).
|
|
|
|
# Applying changes in tabs does not lead to a better user experience, as the
|
|
|
|
# quickfix list no longer works as you might expect (doesn't jump into other
|
|
|
|
# tabs), and the complexity of choosing where to apply edits is significant.
|
|
|
|
if BufferIsVisible( buffer_num ):
|
|
|
|
# file is already open and visible, just return that buffer number (and an
|
|
|
|
# idicator that we *didn't* open a split)
|
|
|
|
return ( buffer_num, False )
|
|
|
|
|
|
|
|
# The file is not open in a visible window, so we open it in a split.
|
|
|
|
# We open the file with a small, fixed height. This means that we don't
|
|
|
|
# make the current buffer the smallest after a series of splits.
|
|
|
|
OpenFilename( filepath, {
|
|
|
|
'focus': True,
|
|
|
|
'fix': True,
|
|
|
|
'size': GetIntValue( '&previewheight' ),
|
|
|
|
} )
|
|
|
|
|
|
|
|
# OpenFilename returns us to the original cursor location. This is what we
|
|
|
|
# want, because we don't want to disorientate the user, but we do need to
|
|
|
|
# know the (now open) buffer number for the filename
|
2017-11-16 18:52:40 -05:00
|
|
|
buffer_num = GetBufferNumberForFilename( filepath )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
if not BufferIsVisible( buffer_num ):
|
|
|
|
# This happens, for example, if there is a swap file and the user
|
|
|
|
# selects the "Quit" or "Abort" options. We just raise an exception to
|
|
|
|
# make it clear to the user that the abort has left potentially
|
|
|
|
# partially-applied changes.
|
|
|
|
raise RuntimeError(
|
|
|
|
'Unable to open file: {0}\nFixIt/Refactor operation '
|
|
|
|
'aborted prior to completion. Your files have not been '
|
|
|
|
'fully updated. Please use undo commands to revert the '
|
|
|
|
'applied changes.'.format( filepath ) )
|
|
|
|
|
|
|
|
# We opened this file in a split
|
|
|
|
return ( buffer_num, True )
|
|
|
|
|
|
|
|
|
2018-02-10 18:48:22 -05:00
|
|
|
def ReplaceChunks( chunks, silent=False ):
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
"""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., one in a window visible in the current tab), we:
|
|
|
|
- issue a warning to the user that we're going to open new files (and offer
|
|
|
|
her the option to abort cleanly)
|
|
|
|
- open the file in a new split, make the changes, then hide the buffer.
|
|
|
|
|
|
|
|
If for some reason a file could not be opened or changed, raises RuntimeError.
|
|
|
|
Otherwise, returns no meaningful value."""
|
|
|
|
|
2018-02-10 08:10:44 -05:00
|
|
|
# We apply the edits file-wise for efficiency.
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
chunks_by_file = _SortChunksByFile( chunks )
|
|
|
|
|
2018-02-10 08:10:44 -05:00
|
|
|
# We sort the file list simply to enable repeatable testing.
|
2016-02-27 19:12:24 -05:00
|
|
|
sorted_file_list = sorted( iterkeys( chunks_by_file ) )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
2018-02-10 18:48:22 -05:00
|
|
|
if not silent:
|
|
|
|
# Make sure the user is prepared to have her screen mutilated by the new
|
|
|
|
# buffers.
|
|
|
|
num_files_to_open = _GetNumNonVisibleFiles( sorted_file_list )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
2018-02-10 18:48:22 -05:00
|
|
|
if num_files_to_open > 0:
|
|
|
|
if not Confirm(
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( num_files_to_open ) ):
|
2018-02-10 18:48:22 -05:00
|
|
|
return
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
|
|
|
# Store the list of locations where we applied changes. We use this to display
|
|
|
|
# the quickfix window showing the user where we applied changes.
|
|
|
|
locations = []
|
|
|
|
|
|
|
|
for filepath in sorted_file_list:
|
2018-02-10 08:10:44 -05:00
|
|
|
buffer_num, close_window = _OpenFileInSplitIfNeeded( filepath )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
2018-02-10 08:10:44 -05:00
|
|
|
locations.extend( ReplaceChunksInBuffer( chunks_by_file[ filepath ],
|
|
|
|
vim.buffers[ buffer_num ] ) )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
|
|
|
# When opening tons of files, we don't want to have a split for each new
|
|
|
|
# file, as this simply does not scale, so we open the window, make the
|
|
|
|
# edits, then hide the window.
|
|
|
|
if close_window:
|
|
|
|
# Some plugins (I'm looking at you, syntastic) might open a location list
|
|
|
|
# for the window we just opened. We don't want that location list hanging
|
|
|
|
# around, so we close it. lclose is a no-op if there is no location list.
|
|
|
|
vim.command( 'lclose' )
|
|
|
|
|
|
|
|
# Note that this doesn't lose our changes. It simply "hides" the buffer,
|
|
|
|
# which can later be re-accessed via the quickfix list or `:ls`
|
|
|
|
vim.command( 'hide' )
|
|
|
|
|
|
|
|
# Open the quickfix list, populated with entries for each location we changed.
|
2018-02-10 18:48:22 -05:00
|
|
|
if not silent:
|
|
|
|
if locations:
|
|
|
|
SetQuickFixList( locations )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
2018-02-10 18:48:22 -05:00
|
|
|
PostVimMessage( 'Applied {0} changes'.format( len( chunks ) ),
|
|
|
|
warning = False )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
|
|
|
|
2018-02-10 08:10:44 -05:00
|
|
|
def ReplaceChunksInBuffer( chunks, vim_buffer ):
|
|
|
|
"""Apply changes in |chunks| to the buffer-like object |buffer| and return the
|
|
|
|
locations for that buffer."""
|
|
|
|
|
|
|
|
# We apply the chunks from the bottom to the top of the buffer so that we
|
|
|
|
# don't need to adjust the position of the remaining chunks due to text
|
|
|
|
# changes. This assumes that chunks are not overlapping. However, we still
|
|
|
|
# allow multiple chunks to share the same starting position (because of the
|
|
|
|
# language server protocol specs). These chunks must be applied in their order
|
|
|
|
# of appareance. Since Python sorting is stable, if we sort the whole list in
|
|
|
|
# reverse order of location, these chunks will be reversed. Therefore, we
|
|
|
|
# need to fully reverse the list then sort it on the starting position in
|
|
|
|
# reverse order.
|
|
|
|
chunks.reverse()
|
2015-09-12 10:30:21 -04:00
|
|
|
chunks.sort( key = lambda chunk: (
|
2015-09-12 11:08:58 -04:00
|
|
|
chunk[ 'range' ][ 'start' ][ 'line_num' ],
|
|
|
|
chunk[ 'range' ][ 'start' ][ 'column_num' ]
|
2018-02-10 08:10:44 -05:00
|
|
|
), reverse = True )
|
2015-09-12 10:30:21 -04:00
|
|
|
|
2018-02-10 08:10:44 -05:00
|
|
|
# However, we still want to display the locations from the top of the buffer
|
|
|
|
# to its bottom.
|
|
|
|
return reversed( [ ReplaceChunk( chunk[ 'range' ][ 'start' ],
|
|
|
|
chunk[ 'range' ][ 'end' ],
|
|
|
|
chunk[ 'replacement_text' ],
|
|
|
|
vim_buffer )
|
|
|
|
for chunk in chunks ] )
|
2015-09-12 10:30:21 -04:00
|
|
|
|
2018-02-10 08:10:44 -05:00
|
|
|
|
|
|
|
def SplitLines( contents ):
|
|
|
|
"""Return a list of each of the lines in the byte string |contents|.
|
|
|
|
Behavior is equivalent to str.splitlines with the following exceptions:
|
|
|
|
- empty strings are returned as [ '' ];
|
|
|
|
- a trailing newline is not ignored (i.e. SplitLines( '\n' )
|
|
|
|
returns [ '', '' ], not [ '' ] )."""
|
|
|
|
if contents == b'':
|
|
|
|
return [ b'' ]
|
|
|
|
|
|
|
|
lines = contents.splitlines()
|
|
|
|
|
|
|
|
if contents.endswith( b'\r' ) or contents.endswith( b'\n' ):
|
|
|
|
lines.append( b'' )
|
|
|
|
|
|
|
|
return lines
|
2015-09-12 10:30:21 -04:00
|
|
|
|
|
|
|
|
2015-08-05 17:09:07 -04:00
|
|
|
# Replace the chunk of text specified by a contiguous range with the supplied
|
2018-02-10 08:10:44 -05:00
|
|
|
# text and return the location.
|
2015-08-05 17:09:07 -04:00
|
|
|
# * start and end are objects with line_num and column_num properties
|
|
|
|
# * the range is inclusive
|
|
|
|
# * indices are all 1-based
|
2016-03-25 23:40:17 -04:00
|
|
|
#
|
|
|
|
# NOTE: Works exclusively with bytes() instances and byte offsets as returned
|
|
|
|
# by ycmd and used within the Vim buffers
|
2018-02-10 08:10:44 -05:00
|
|
|
def ReplaceChunk( start, end, replacement_text, vim_buffer ):
|
2015-08-05 17:09:07 -04:00
|
|
|
# ycmd's results are all 1-based, but vim's/python's are all 0-based
|
|
|
|
# (so we do -1 on all of the values)
|
2018-02-10 08:10:44 -05:00
|
|
|
start_line = start[ 'line_num' ] - 1
|
|
|
|
end_line = end[ 'line_num' ] - 1
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
|
2018-02-10 08:10:44 -05:00
|
|
|
start_column = start[ 'column_num' ] - 1
|
2015-08-05 17:09:07 -04:00
|
|
|
end_column = end[ 'column_num' ] - 1
|
|
|
|
|
2016-03-25 23:40:17 -04:00
|
|
|
# NOTE: replacement_text is unicode, but all our offsets are byte offsets,
|
|
|
|
# so we convert to bytes
|
2018-02-10 08:10:44 -05:00
|
|
|
replacement_lines = SplitLines( ToBytes( replacement_text ) )
|
2015-08-05 17:09:07 -04:00
|
|
|
|
2016-07-13 20:39:33 -04:00
|
|
|
# NOTE: Vim buffers are a list of byte objects on Python 2 but unicode
|
|
|
|
# objects on Python 3.
|
|
|
|
start_existing_text = ToBytes( vim_buffer[ start_line ] )[ : start_column ]
|
2018-02-10 08:10:44 -05:00
|
|
|
end_existing_text = ToBytes( vim_buffer[ end_line ] )[ end_column : ]
|
2015-08-05 17:09:07 -04:00
|
|
|
|
|
|
|
replacement_lines[ 0 ] = start_existing_text + replacement_lines[ 0 ]
|
|
|
|
replacement_lines[ -1 ] = replacement_lines[ -1 ] + end_existing_text
|
|
|
|
|
|
|
|
vim_buffer[ start_line : end_line + 1 ] = replacement_lines[:]
|
|
|
|
|
2018-02-10 08:10:44 -05:00
|
|
|
return {
|
|
|
|
'bufnr': vim_buffer.number,
|
|
|
|
'filename': vim_buffer.name,
|
|
|
|
# line and column numbers are 1-based in qflist
|
|
|
|
'lnum': start_line + 1,
|
|
|
|
'col': start_column + 1,
|
|
|
|
'text': replacement_text,
|
|
|
|
'type': 'F',
|
|
|
|
}
|
2015-08-28 10:26:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
def InsertNamespace( namespace ):
|
2015-08-31 12:51:23 -04:00
|
|
|
if VariableExists( 'g:ycm_csharp_insert_namespace_expr' ):
|
|
|
|
expr = GetVariableValue( 'g:ycm_csharp_insert_namespace_expr' )
|
|
|
|
if expr:
|
|
|
|
SetVariableValue( "g:ycm_namespace_to_insert", namespace )
|
|
|
|
vim.eval( expr )
|
|
|
|
return
|
|
|
|
|
|
|
|
pattern = '^\s*using\(\s\+[a-zA-Z0-9]\+\s\+=\)\?\s\+[a-zA-Z0-9.]\+\s*;\s*'
|
2016-11-02 08:08:30 -04:00
|
|
|
existing_indent = ''
|
2015-08-31 12:51:23 -04:00
|
|
|
line = SearchInCurrentBuffer( pattern )
|
2016-11-03 11:21:15 -04:00
|
|
|
if line:
|
2016-11-02 08:08:30 -04:00
|
|
|
existing_line = LineTextInCurrentBuffer( line )
|
|
|
|
existing_indent = re.sub( r"\S.*", "", existing_line )
|
2018-02-10 08:10:44 -05:00
|
|
|
new_line = "{0}using {1};\n".format( existing_indent, namespace )
|
2015-08-31 12:51:23 -04:00
|
|
|
replace_pos = { 'line_num': line + 1, 'column_num': 1 }
|
2018-02-10 08:10:44 -05:00
|
|
|
ReplaceChunk( replace_pos, replace_pos, new_line, vim.current.buffer )
|
2016-08-28 02:34:09 -04:00
|
|
|
PostVimMessage( 'Add namespace: {0}'.format( namespace ), warning = False )
|
2015-08-28 10:26:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
def SearchInCurrentBuffer( pattern ):
|
2016-11-02 08:08:30 -04:00
|
|
|
""" Returns the 1-indexed line on which the pattern matches
|
|
|
|
(going UP from the current position) or 0 if not found """
|
2015-08-28 10:26:18 -04:00
|
|
|
return GetIntValue( "search('{0}', 'Wcnb')".format( EscapeForVim( pattern )))
|
|
|
|
|
|
|
|
|
2016-11-03 13:47:26 -04:00
|
|
|
def LineTextInCurrentBuffer( line_number ):
|
2016-11-02 08:08:30 -04:00
|
|
|
""" Returns the text on the 1-indexed line (NOT 0-indexed) """
|
2016-11-03 13:47:26 -04:00
|
|
|
return vim.current.buffer[ line_number - 1 ]
|
2015-09-06 15:07:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
def ClosePreviewWindow():
|
|
|
|
""" Close the preview window if it is present, otherwise do nothing """
|
|
|
|
vim.command( 'silent! pclose!' )
|
|
|
|
|
|
|
|
|
|
|
|
def JumpToPreviewWindow():
|
|
|
|
""" Jump the vim cursor to the preview window, which must be active. Returns
|
|
|
|
boolean indicating if the cursor ended up in the preview window """
|
|
|
|
vim.command( 'silent! wincmd P' )
|
|
|
|
return vim.current.window.options[ 'previewwindow' ]
|
|
|
|
|
|
|
|
|
|
|
|
def JumpToPreviousWindow():
|
|
|
|
""" Jump the vim cursor to its previous window position """
|
|
|
|
vim.command( 'silent! wincmd p' )
|
|
|
|
|
|
|
|
|
2015-10-27 12:00:11 -04:00
|
|
|
def JumpToTab( tab_number ):
|
|
|
|
"""Jump to Vim tab with corresponding number """
|
|
|
|
vim.command( 'silent! tabn {0}'.format( tab_number ) )
|
|
|
|
|
|
|
|
|
2015-09-06 15:07:42 -04:00
|
|
|
def OpenFileInPreviewWindow( filename ):
|
|
|
|
""" Open the supplied filename in the preview window """
|
|
|
|
vim.command( 'silent! pedit! ' + filename )
|
|
|
|
|
|
|
|
|
|
|
|
def WriteToPreviewWindow( message ):
|
|
|
|
""" Display the supplied message in the preview window """
|
|
|
|
|
|
|
|
# This isn't something that comes naturally to Vim. Vim only wants to show
|
|
|
|
# tags and/or actual files in the preview window, so we have to hack it a
|
|
|
|
# little bit. We generate a temporary file name and "open" that, then write
|
|
|
|
# the data to it. We make sure the buffer can't be edited or saved. Other
|
|
|
|
# approaches include simply opening a split, but we want to take advantage of
|
|
|
|
# the existing Vim options for preview window height, etc.
|
|
|
|
|
|
|
|
ClosePreviewWindow()
|
|
|
|
|
2016-11-11 13:53:26 -05:00
|
|
|
OpenFileInPreviewWindow( vim.eval( 'tempname()' ) )
|
2015-09-06 15:07:42 -04:00
|
|
|
|
|
|
|
if JumpToPreviewWindow():
|
|
|
|
# We actually got to the preview window. By default the preview window can't
|
|
|
|
# be changed, so we make it writable, write to it, then make it read only
|
|
|
|
# again.
|
|
|
|
vim.current.buffer.options[ 'modifiable' ] = True
|
|
|
|
vim.current.buffer.options[ 'readonly' ] = False
|
|
|
|
|
|
|
|
vim.current.buffer[:] = message.splitlines()
|
|
|
|
|
|
|
|
vim.current.buffer.options[ 'buftype' ] = 'nofile'
|
2016-11-11 13:15:22 -05:00
|
|
|
vim.current.buffer.options[ 'bufhidden' ] = 'wipe'
|
|
|
|
vim.current.buffer.options[ 'buflisted' ] = False
|
2015-09-06 15:07:42 -04:00
|
|
|
vim.current.buffer.options[ 'swapfile' ] = False
|
|
|
|
vim.current.buffer.options[ 'modifiable' ] = False
|
|
|
|
vim.current.buffer.options[ 'readonly' ] = True
|
|
|
|
|
|
|
|
# We need to prevent closing the window causing a warning about unsaved
|
|
|
|
# file, so we pretend to Vim that the buffer has not been changed.
|
|
|
|
vim.current.buffer.options[ 'modified' ] = False
|
|
|
|
|
|
|
|
JumpToPreviousWindow()
|
|
|
|
else:
|
|
|
|
# We couldn't get to the preview window, but we still want to give the user
|
|
|
|
# the information we have. The only remaining option is to echo to the
|
|
|
|
# status area.
|
2016-08-28 02:34:09 -04:00
|
|
|
PostVimMessage( message, warning = False )
|
2015-10-27 12:00:11 -04:00
|
|
|
|
|
|
|
|
2015-11-11 08:32:01 -05:00
|
|
|
def BufferIsVisibleForFilename( filename ):
|
2015-10-27 12:00:11 -04:00
|
|
|
"""Check if a buffer exists for a specific file."""
|
2017-11-16 18:52:40 -05:00
|
|
|
buffer_number = GetBufferNumberForFilename( filename )
|
2015-11-11 08:32:01 -05:00
|
|
|
return BufferIsVisible( buffer_number )
|
2015-10-27 12:00:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
def CloseBuffersForFilename( filename ):
|
|
|
|
"""Close all buffers for a specific file."""
|
2017-11-16 18:52:40 -05:00
|
|
|
buffer_number = GetBufferNumberForFilename( filename )
|
2016-02-28 22:16:06 -05:00
|
|
|
while buffer_number != -1:
|
2015-10-27 12:00:11 -04:00
|
|
|
vim.command( 'silent! bwipeout! {0}'.format( buffer_number ) )
|
2017-11-16 18:52:40 -05:00
|
|
|
new_buffer_number = GetBufferNumberForFilename( filename )
|
2015-11-11 08:32:01 -05:00
|
|
|
if buffer_number == new_buffer_number:
|
|
|
|
raise RuntimeError( "Buffer {0} for filename '{1}' should already be "
|
|
|
|
"wiped out.".format( buffer_number, filename ) )
|
|
|
|
buffer_number = new_buffer_number
|
2015-10-27 12:00:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
def OpenFilename( filename, options = {} ):
|
|
|
|
"""Open a file in Vim. Following options are available:
|
|
|
|
- command: specify which Vim command is used to open the file. Choices
|
|
|
|
are same-buffer, horizontal-split, vertical-split, and new-tab (default:
|
|
|
|
horizontal-split);
|
|
|
|
- size: set the height of the window for a horizontal split or the width for
|
|
|
|
a vertical one (default: '');
|
|
|
|
- fix: set the winfixheight option for a horizontal split or winfixwidth for
|
|
|
|
a vertical one (default: False). See :h winfix for details;
|
|
|
|
- focus: focus the opened file (default: False);
|
|
|
|
- watch: automatically watch for changes (default: False). This is useful
|
|
|
|
for logs;
|
|
|
|
- position: set the position where the file is opened (default: start).
|
|
|
|
Choices are start and end."""
|
|
|
|
|
|
|
|
# Set the options.
|
|
|
|
command = GetVimCommand( options.get( 'command', 'horizontal-split' ),
|
|
|
|
'horizontal-split' )
|
|
|
|
size = ( options.get( 'size', '' ) if command in [ 'split', 'vsplit' ] else
|
|
|
|
'' )
|
|
|
|
focus = options.get( 'focus', False )
|
|
|
|
|
|
|
|
# There is no command in Vim to return to the previous tab so we need to
|
|
|
|
# remember the current tab if needed.
|
2016-02-27 19:12:24 -05:00
|
|
|
if not focus and command == 'tabedit':
|
2015-10-27 12:00:11 -04:00
|
|
|
previous_tab = GetIntValue( 'tabpagenr()' )
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
else:
|
|
|
|
previous_tab = None
|
2015-10-27 12:00:11 -04:00
|
|
|
|
2016-05-06 01:52:04 -04:00
|
|
|
# Open the file.
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
try:
|
|
|
|
vim.command( '{0}{1} {2}'.format( size, command, filename ) )
|
|
|
|
# When the file we are trying to jump to has a swap file,
|
|
|
|
# Vim opens swap-exists-choices dialog and throws vim.error with E325 error,
|
|
|
|
# or KeyboardInterrupt after user selects one of the options which actually
|
|
|
|
# opens the file (Open read-only/Edit anyway).
|
|
|
|
except vim.error as e:
|
|
|
|
if 'E325' not in str( e ):
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Otherwise, the user might have chosen Quit. This is detectable by the
|
|
|
|
# current file not being the target file
|
|
|
|
if filename != GetCurrentBufferFilepath():
|
|
|
|
return
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
# Raised when the user selects "Abort" after swap-exists-choices
|
|
|
|
return
|
|
|
|
|
|
|
|
_SetUpLoadedBuffer( command,
|
|
|
|
filename,
|
|
|
|
options.get( 'fix', False ),
|
|
|
|
options.get( 'position', 'start' ),
|
|
|
|
options.get( 'watch', False ) )
|
|
|
|
|
|
|
|
# Vim automatically set the focus to the opened file so we need to get the
|
|
|
|
# focus back (if the focus option is disabled) when opening a new tab or
|
|
|
|
# window.
|
|
|
|
if not focus:
|
2016-02-27 19:12:24 -05:00
|
|
|
if command == 'tabedit':
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
JumpToTab( previous_tab )
|
|
|
|
if command in [ 'split', 'vsplit' ]:
|
|
|
|
JumpToPreviousWindow()
|
|
|
|
|
|
|
|
|
|
|
|
def _SetUpLoadedBuffer( command, filename, fix, position, watch ):
|
|
|
|
"""After opening a buffer, configure it according to the supplied options,
|
|
|
|
which are as defined by the OpenFilename method."""
|
2015-10-27 12:00:11 -04:00
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
if command == 'split':
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
vim.current.window.options[ 'winfixheight' ] = fix
|
2016-02-27 19:12:24 -05:00
|
|
|
if command == 'vsplit':
|
Support FixIt commands across buffers
We simply apply the changes to each file in turn. The existing replacement
logic is unchanged, except that it now no longer implicitly assumes we are
talking about the current buffer.
If a buffer is not visible for the requested file name, we open it in
a horizontal split, make the edits, then hide the window. Because this
can cause UI flickering, and leave hidden, modified buffers around, we
issue a warning to the user stating the number of files for which we are
going to do this. We pop up the quickfix list at the end of applying
the edits to allow the user to see what we changed.
If the user opts to abort due to, say, the file being open in another
window, we simply raise an error and give up, as undoing the changes
is too complex to do programatically, but trivial to do manually in such
a rare case.
2016-01-11 17:19:33 -05:00
|
|
|
vim.current.window.options[ 'winfixwidth' ] = fix
|
2015-10-27 12:00:11 -04:00
|
|
|
|
|
|
|
if watch:
|
|
|
|
vim.current.buffer.options[ 'autoread' ] = True
|
|
|
|
vim.command( "exec 'au BufEnter <buffer> :silent! checktime {0}'"
|
|
|
|
.format( filename ) )
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
if position == 'end':
|
2017-01-31 15:35:34 -05:00
|
|
|
vim.command( 'silent! normal! Gzz' )
|
2018-02-07 13:57:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
def BuildRange( start_line, end_line ):
|
|
|
|
# Vim only returns the starting and ending lines of the range of a command.
|
|
|
|
# Check if those lines correspond to a previous visual selection and if they
|
|
|
|
# do, use the columns of that selection to build the range.
|
|
|
|
start = vim.current.buffer.mark( '<' )
|
|
|
|
end = vim.current.buffer.mark( '>' )
|
|
|
|
if not start or not end or start_line != start[ 0 ] or end_line != end[ 0 ]:
|
|
|
|
start = [ start_line, 0 ]
|
|
|
|
end = [ end_line, len( vim.current.buffer[ end_line - 1 ] ) ]
|
|
|
|
# Vim Python API returns 1-based lines and 0-based columns while ycmd expects
|
|
|
|
# 1-based lines and columns.
|
|
|
|
return {
|
|
|
|
'range': {
|
|
|
|
'start': {
|
|
|
|
'line_num': start[ 0 ],
|
|
|
|
'column_num': start[ 1 ] + 1
|
|
|
|
},
|
|
|
|
'end': {
|
|
|
|
'line_num': end[ 0 ],
|
|
|
|
# Vim returns the maximum 32-bit integer value when a whole line is
|
|
|
|
# selected. Use the end of line instead.
|
|
|
|
'column_num': min( end[ 1 ],
|
|
|
|
len( vim.current.buffer[ end[ 0 ] - 1 ] ) ) + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|