Skip to content

Commit 9ac9743

Browse files
committed
Add support for GetDoc in C#
LSP OmniSharp-Roslyn uses a different request than we previously have for this purposes. This results in some questionable formatting, but there's no way we can fix that without breaking other stuff.
1 parent 2f18c12 commit 9ac9743

File tree

3 files changed

+108
-24
lines changed

3 files changed

+108
-24
lines changed

ycmd/completers/cs/cs_completer.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,28 @@ def SupportedFiletypes( self ):
9999

100100

101101
def GetType( self, request_data ):
102-
raw_hover = self.GetHoverResponse( request_data )
103-
value = raw_hover[ 'value' ]
104-
if not value:
102+
hover_value = self.GetHoverResponse( request_data )[ 'value' ]
103+
if not hover_value:
105104
raise RuntimeError( 'No type found.' )
106-
value = value.split( '\n' )[ 1 ]
107-
return responses.BuildDetailedInfoResponse( value )
105+
value = hover_value.split( '\n', maxsplit = 2 )[ 1 ]
106+
return responses.BuildDisplayMessageResponse( value )
107+
108+
109+
def GetDoc( self, request_data ):
110+
hover_value = self.GetHoverResponse( request_data )[ 'value' ]
111+
if not hover_value:
112+
raise RuntimeError( 'No documentation available.' )
113+
# The response looks like this:
114+
#
115+
# ```csharp
116+
# type info
117+
# ```
118+
#
119+
# docstring
120+
#
121+
# The idea is to get rid of silly markdown backticks.
122+
lines = hover_value.splitlines()
123+
del lines[ 2 ]
124+
del lines[ 0 ]
125+
result = '\n'.join( lines )
126+
return responses.BuildDetailedInfoResponse( result )

ycmd/tests/cs/subcommands_test.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def test_Subcommands_GetType_VariableDeclaration( self, app ):
499499
filepath = filepath )
500500

501501
response = app.post_json( '/run_completer_command', gettype_data ).json
502-
assert_that( response, has_entry( 'detailed_info',
502+
assert_that( response, has_entry( 'message',
503503
'(local variable) string str' ) )
504504

505505

@@ -517,7 +517,7 @@ def test_Subcommands_GetType_VariableUsage( self, app ):
517517
filepath = filepath )
518518

519519
response = app.post_json( '/run_completer_command', gettype_data ).json
520-
assert_that( response, has_entry( 'detailed_info',
520+
assert_that( response, has_entry( 'message',
521521
'(local variable) string str' ) )
522522

523523

@@ -536,7 +536,68 @@ def test_Subcommands_GetType_DocsIgnored( self, app ):
536536

537537
response = app.post_json( '/run_completer_command', gettype_data ).json
538538
assert_that( response, has_entry(
539-
'detailed_info', '(field) int GetTypeTestCase.an_int_with_docs' ) )
539+
'message', '(field) int GetTypeTestCase.an_int_with_docs' ) )
540+
541+
542+
@SharedYcmd
543+
def test_Subcommands_GetDoc_Invalid( self, app ):
544+
filepath = PathToTestFile( 'testy', 'GetDocTestCase.cs' )
545+
contents = ReadFile( filepath )
546+
547+
getdoc_data = BuildRequest( completer_target = 'filetype_default',
548+
command_arguments = [ 'GetDoc' ],
549+
line_num = 1,
550+
column_num = 1,
551+
contents = contents,
552+
filetype = 'cs',
553+
filepath = filepath )
554+
555+
response = app.post_json( '/run_completer_command',
556+
getdoc_data,
557+
expect_errors = True ).json
558+
assert_that( response, ErrorMatcher( RuntimeError,
559+
'No documentation available.' ) )
560+
561+
562+
@SharedYcmd
563+
def test_Subcommands_GetDoc_Variable( self, app ):
564+
filepath = PathToTestFile( 'testy', 'GetDocTestCase.cs' )
565+
contents = ReadFile( filepath )
566+
567+
getdoc_data = BuildRequest( completer_target = 'filetype_default',
568+
command_arguments = [ 'GetDoc' ],
569+
line_num = 13,
570+
column_num = 28,
571+
contents = contents,
572+
filetype = 'cs',
573+
filepath = filepath )
574+
575+
response = app.post_json( '/run_completer_command', getdoc_data ).json
576+
assert_that( response,
577+
has_entry( 'detailed_info',
578+
'(field) int GetDocTestCase.an_int\n\n'
579+
'an integer, or something' ) )
580+
581+
582+
@SharedYcmd
583+
def test_Subcommands_GetDoc_Function( self, app ):
584+
filepath = PathToTestFile( 'testy', 'GetDocTestCase.cs' )
585+
contents = ReadFile( filepath )
586+
587+
getdoc_data = BuildRequest( completer_target = 'filetype_default',
588+
command_arguments = [ 'GetDoc' ],
589+
line_num = 37,
590+
column_num = 27,
591+
contents = contents,
592+
filetype = 'cs',
593+
filepath = filepath )
594+
595+
response = app.post_json( '/run_completer_command', getdoc_data ).json
596+
# And here LSP OmniSharp-Roslyn messes up the formatting.
597+
assert_that( response, has_entry( 'detailed_info',
598+
'int GetDocTestCase.DoATest()\n\n'
599+
'Very important method\\. With multiple lines of '
600+
'commentary And Format\\- \\-ting' ) )
540601

541602

542603
@IsolatedYcmd()

ycmd/tests/cs/testdata/testy/GetDocTestCase.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
/**
2-
* testy is a namespace for testing
3-
*/
1+
/// <summary>
2+
/// testy is a namespace for testing
3+
/// </summary>
44
namespace testy {
5-
/**
6-
* Tests the GetDoc subcommands
7-
*/
5+
/// <summary>
6+
/// Tests the GetDoc subcommands
7+
/// </summary>
88
public class GetDocTestCase {
9-
/**
10-
* Constructor
11-
*/
9+
/// <summary>
10+
/// Constructor
11+
/// </summary>
1212
public GetDocTestCase() {
1313
this.an_int = 1;
1414
}
1515

16-
/**
17-
* Very important method.
18-
*
19-
* With multiple lines of commentary
20-
* And Format-
21-
* -ting
22-
*/
16+
/// <summary>
17+
/// Very important method.
18+
///
19+
/// With multiple lines of commentary
20+
/// And Format-
21+
/// -ting
22+
/// </summary>
2323
public int DoATest() {
2424
return an_int;
2525
}
2626

27+
/// <summary>
2728
/// an integer, or something
29+
/// </summary>
2830
private int an_int;
2931

32+
/// <summary>
3033
/// Use this for testing
34+
/// </summary>
3135
private static void DoTesting() {
3236
GetDocTestCase tc;
3337
tc.DoATest();

0 commit comments

Comments
 (0)