Skip to content

Commit

Permalink
Merge pull request #4155 from puremourning/getdoc-mods
Browse files Browse the repository at this point in the history
Accept command modifiers (mods) for GetDoc subcommand
  • Loading branch information
mergify[bot] committed Jul 5, 2023
2 parents f93c2e9 + dd419e5 commit e35c131
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
26 changes: 26 additions & 0 deletions README.md
Expand Up @@ -1931,6 +1931,11 @@ See the [file type feature summary](#quick-feature-summary) for an overview of
the features available for each file type. See the _YcmCompleter subcommands_
section for more information on the available subcommands and their usage.

Some commands, like `Format` accept a range, like `:%YcmCompleter Format`.

Some commands like `GetDoc` and the various `GoTo` commands respect modifiers,
like `:rightbelow YcmCompleter GetDoc`, `:vertical YcmCompleter GoTo`.

YcmCompleter Subcommands
------------------------

Expand Down Expand Up @@ -2147,6 +2152,27 @@ under the cursor. Depending on the file type, this includes things like:
* Python docstrings,
* etc.

The documentation is opened in the preview window, and options like
`previewheight` are respected. If you would like to customise the height and
position of this window, we suggest a custom command that:

* Sets `previewheight` temporarily
* Runs the `GetDoc` command with supplied modifiers
* Restores `previewheight`.

For example:

```viml
command -count ShowDocWithSize
\ let g:ph=&previewheight
\ <bar> set previewheight=<count>
\ <bar> <mods> YcmCompleter GetDoc
\ <bar> let &previewheight=g:ph
```

You can then use something like `:botright vertical 80ShowDocWithSize`. Here's an
example of that: https://asciinema.org/a/hE6Pi1gU6omBShwFna8iwGEe9

Supported in filetypes: `c, cpp, objc, objcpp, cuda, cs, go, java, javascript,
python, typescript, rust`

Expand Down
7 changes: 4 additions & 3 deletions python/ycm/client/command_request.py
Expand Up @@ -93,7 +93,7 @@ def RunPostCommandActionsIfNeeded( self,
return self._HandleMessageResponse()

if 'detailed_info' in self._response:
return self._HandleDetailedInfoResponse()
return self._HandleDetailedInfoResponse( modifiers )

# The only other type of response we understand is GoTo, and that is the
# only one that we can't detect just by inspecting the response (it should
Expand Down Expand Up @@ -201,8 +201,9 @@ def _HandleMessageResponse( self ):
vimsupport.PostVimMessage( self._response[ 'message' ], warning = False )


def _HandleDetailedInfoResponse( self ):
vimsupport.WriteToPreviewWindow( self._response[ 'detailed_info' ] )
def _HandleDetailedInfoResponse( self, modifiers ):
vimsupport.WriteToPreviewWindow( self._response[ 'detailed_info' ],
modifiers )


def SendCommandRequestAsync( arguments, extra_data = None, silent = True ):
Expand Down
2 changes: 1 addition & 1 deletion python/ycm/tests/client/command_request_test.py
Expand Up @@ -283,7 +283,7 @@ def DetailedInfoTest( command, info ):
request = CommandRequest( [ command ] )
request._response = { 'detailed_info': info }
request.RunPostCommandActionsIfNeeded( 'topleft' )
write_to_preview.assert_called_with( info )
write_to_preview.assert_called_with( info, 'topleft' )

for command, info in [
[ '___________', 'This is a message' ],
Expand Down
36 changes: 32 additions & 4 deletions python/ycm/tests/vimsupport_test.py
Expand Up @@ -1375,7 +1375,7 @@ def test_ReplaceChunks_MultiFile_Open( self,
def test_WriteToPreviewWindow( self, vim_current, vim_command ):
vim_current.window.options.__getitem__ = MagicMock( return_value = True )

vimsupport.WriteToPreviewWindow( "test" )
vimsupport.WriteToPreviewWindow( "test", '' )

vim_command.assert_has_exact_calls( [
call( 'silent! pclose!' ),
Expand All @@ -1398,11 +1398,39 @@ def test_WriteToPreviewWindow( self, vim_current, vim_command ):
call( 'readonly', True ),
], any_order = True )

@patch( 'vim.command', new_callable=ExtendedMock )
@patch( 'vim.current', new_callable=ExtendedMock )
def test_WriteToPreviewWindow_Mods( self, vim_current, vim_command ):
vim_current.window.options.__getitem__ = MagicMock( return_value = True )

vimsupport.WriteToPreviewWindow( "test", 'tab leftabove' )

vim_command.assert_has_exact_calls( [
call( 'silent! pclose!' ),
call( 'silent! tab leftabove pedit! _TEMP_FILE_' ),
call( 'silent! wincmd P' ),
call( 'silent! wincmd p' ) ] )

vim_current.buffer.__setitem__.assert_called_with(
slice( None, None, None ), [ 'test' ] )

vim_current.buffer.options.__setitem__.assert_has_exact_calls( [
call( 'modifiable', True ),
call( 'readonly', False ),
call( 'buftype', 'nofile' ),
call( 'bufhidden', 'wipe' ),
call( 'buflisted', False ),
call( 'swapfile', False ),
call( 'modifiable', False ),
call( 'modified', False ),
call( 'readonly', True ),
], any_order = True )


@patch( 'vim.current' )
def test_WriteToPreviewWindow_MultiLine( self, vim_current ):
vim_current.window.options.__getitem__ = MagicMock( return_value = True )
vimsupport.WriteToPreviewWindow( "test\ntest2" )
vimsupport.WriteToPreviewWindow( "test\ntest2", '' )

vim_current.buffer.__setitem__.assert_called_with(
slice( None, None, None ), [ 'test', 'test2' ] )
Expand All @@ -1413,7 +1441,7 @@ def test_WriteToPreviewWindow_MultiLine( self, vim_current ):
def test_WriteToPreviewWindow_JumpFail( self, vim_current, vim_command ):
vim_current.window.options.__getitem__ = MagicMock( return_value = False )

vimsupport.WriteToPreviewWindow( "test" )
vimsupport.WriteToPreviewWindow( "test", '' )

vim_command.assert_has_exact_calls( [
call( 'silent! pclose!' ),
Expand All @@ -1434,7 +1462,7 @@ def test_WriteToPreviewWindow_JumpFail_MultiLine(

vim_current.window.options.__getitem__ = MagicMock( return_value = False )

vimsupport.WriteToPreviewWindow( "test\ntest2" )
vimsupport.WriteToPreviewWindow( "test\ntest2", '' )

vim_command.assert_has_exact_calls( [
call( 'silent! pclose!' ),
Expand Down
10 changes: 6 additions & 4 deletions python/ycm/vimsupport.py
Expand Up @@ -1236,12 +1236,14 @@ def JumpToTab( tab_number ):
vim.command( f'silent! tabn { tab_number }' )


def OpenFileInPreviewWindow( filename ):
def OpenFileInPreviewWindow( filename, modifiers ):
""" Open the supplied filename in the preview window """
vim.command( 'silent! pedit! ' + filename )
if modifiers:
modifiers = ' ' + modifiers
vim.command( f'silent!{modifiers} pedit! { filename }' )


def WriteToPreviewWindow( message ):
def WriteToPreviewWindow( message, modifiers ):
""" Display the supplied message in the preview window """

# This isn't something that comes naturally to Vim. Vim only wants to show
Expand All @@ -1253,7 +1255,7 @@ def WriteToPreviewWindow( message ):

ClosePreviewWindow()

OpenFileInPreviewWindow( vim.eval( 'tempname()' ) )
OpenFileInPreviewWindow( vim.eval( 'tempname()' ), modifiers )

if JumpToPreviewWindow():
# We actually got to the preview window. By default the preview window can't
Expand Down

0 comments on commit e35c131

Please sign in to comment.