Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CsCompleter: Implement GoToDocumentOutline. #1723

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ycmd/completers/cs/cs_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ def GetSubcommandsMap( self ):
self._SolutionSubcommand( request_data,
method = '_RefactorRename',
args = args ) ),
'GoToDocumentOutline' : ( lambda self, request_data, args:
self._SolutionSubcommand( request_data,
method = '_GoToDocumentOutline' ) ),
}


Expand Down Expand Up @@ -660,6 +663,28 @@ def _GoToSymbol( self, request_data, args ):
else:
raise RuntimeError( 'No symbols found' )


def _GoToDocumentOutline( self, request_data ):
request = self._DefaultParameters( request_data )
response = self._GetResponse( '/currentfilemembersasflat', request )
if response is not None and len( response ) > 0:
goto_locations = []
for ref in response:
ref_file = ref[ 'FileName' ]
ref_line = ref[ 'Line' ]
lines = GetFileLines( request_data, ref_file )
line = lines[ min( len( lines ), ref_line - 1 ) ]
goto_locations.append(
responses.BuildGoToResponseFromLocation(
_BuildLocation( request_data,
ref_file,
ref_line,
ref[ 'Column' ] ),
line ) )
return goto_locations
else:
raise RuntimeError( 'No symbols found' )

def _GoToReferences( self, request_data ):
""" Jump to references of identifier under cursor """
# _GetResponse can throw. Original code by @mispencer
Expand Down
85 changes: 85 additions & 0 deletions ycmd/tests/cs/subcommands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,88 @@ def test_Subcommands_OrganizeImports( self, app ):
LocationMatcher( filepath, 3, 1 ),
)
) } ) ) } ) )


@SharedYcmd
def test_Subcommands_GoToDocumentOutline( self, app ):

# we reuse the ImportTest.cs file as it contains a good selection of
# symbols/ symbol types.
filepath = PathToTestFile( 'testy', 'GotoTestCase.cs' )
with WrapOmniSharpServer( app, filepath ):

# the command name and file are the only relevant arguments for this
# subcommand, our current cursor position in the file doesn't matter.
request = BuildRequest( command_arguments = [ 'GoToDocumentOutline' ],
line_num = 11,
column_num = 2,
contents = ReadFile( filepath ),
filetype = 'cs',
filepath = filepath )

response = app.post_json( '/run_completer_command', request ).json

print( 'completer response = ', response )

assert_that( response,
has_items(
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 6, 8 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 26, 12 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 30, 8 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 35, 12 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 39, 12 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 43, 8 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 48, 8 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 8, 15 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 13, 15 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 17, 15 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 21, 15 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 27, 8 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 31, 15 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 36, 8 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 40, 8 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 44, 15 ),
LocationMatcher(
PathToTestFile( 'testy', 'GotoTestCase.cs' ), 49, 15 ),
)
)

@SharedYcmd
def test_Subcommands_GoToDocumentOutline_Empty( self, app ):

filepath = PathToTestFile( 'testy', 'Empty.cs' )
with WrapOmniSharpServer( app, filepath ):

# the command name and file are the only relevant arguments for this
# subcommand. our current cursor position in the file doesn't matter.
request = BuildRequest( command_arguments = [ 'GoToDocumentOutline' ],
line_num = 0,
column_num = 0,
contents = ReadFile( filepath ),
filetype = 'cs',
filepath = filepath )

response = app.post_json( '/run_completer_command',
request,
expect_errors = True ).json

print( 'completer response = ', response )

assert_that( response, ErrorMatcher( RuntimeError,
'No symbols found' ) )
6 changes: 6 additions & 0 deletions ycmd/tests/cs/testdata/testy/Empty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System;
using System.Data;

namespace testy
{
}
1 change: 1 addition & 0 deletions ycmd/tests/cs/testdata/testy/testy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ZeroColumnDiagnostic.cs" />
<Compile Include="Empty.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>