diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index 7f56b6e8..9a5b6d48 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -117,17 +117,31 @@ class CsharpCompleter( Completer ): self._logger.info( 'startup' ) self._omnisharp_port = utils.GetUnusedLocalhostPort() - solutionfiles, folder = _FindSolutionFiles( request_data[ 'filepath' ] ) + solution_files, folder = _FindSolutionFiles( request_data[ 'filepath' ] ) - if len( solutionfiles ) == 0: + if len( solution_files ) == 0: raise RuntimeError( 'Error starting OmniSharp server: no solutionfile found' ) - elif len( solutionfiles ) == 1: - solutionfile = solutionfiles[ 0 ] + elif len( solution_files ) == 1: + solutionfile = solution_files[ 0 ] else: - raise RuntimeError( - 'Found multiple solution files instead of one!\n{0}'.format( - solutionfiles ) ) + # multiple solutions found : if there is one whose name is the same + # as the folder containing the file we edit, use this one + # (e.g. if we have bla/Project.sln and we are editing + # bla/Project/Folder/File.cs, use bla/Project.sln) + filepath_components = _PathComponents( request_data[ 'filepath' ] ) + solutionpath = _PathComponents( folder ) + foldername = '' + if len( filepath_components ) > len( solutionpath ): + foldername = filepath_components[ len( solutionpath ) ] + solution_file_candidates = [ solutionfile for solutionfile in solution_files + if _GetFilenameWithoutExtension( solutionfile ) == foldername ] + if len( solution_file_candidates ) == 1: + solutionfile = solution_file_candidates[ 0 ] + else: + raise RuntimeError( + 'Found multiple solution files instead of one!\n{0}'.format( + solution_files ) ) omnisharp = os.path.join( os.path.abspath( os.path.dirname( __file__ ) ), @@ -230,3 +244,20 @@ def _FindSolutionFiles( filepath ): break solutionfiles = glob.glob1( folder, '*.sln' ) return solutionfiles, folder + +def _PathComponents( path ): + path_components = [] + while True: + path, folder = os.path.split( path ) + if folder: + path_components.append( folder ) + else: + if path: + path_components.append( path ) + break + path_components.reverse() + return path_components + +def _GetFilenameWithoutExtension( path ): + return os.path.splitext( os.path.basename ( path ) )[ 0 ] + diff --git a/python/ycm/server/tests/get_completions_test.py b/python/ycm/server/tests/get_completions_test.py index 4bfbee0f..6386cb0e 100644 --- a/python/ycm/server/tests/get_completions_test.py +++ b/python/ycm/server/tests/get_completions_test.py @@ -23,7 +23,7 @@ import time import httplib from .test_utils import ( Setup, BuildRequest, PathToTestFile, ChangeSpecificOptions ) -from webtest import TestApp +from webtest import TestApp, AppError from nose.tools import eq_, with_setup from hamcrest import ( assert_that, has_item, has_items, has_entry, contains_inanyorder, empty ) @@ -95,6 +95,80 @@ def GetCompletions_CsCompleter_Works_test(): command_arguments = ['StopServer'], filetype = 'cs' ) ) +@with_setup( Setup ) +def GetCompletions_CsCompleter_StartsWithUnambiguousMultipleSolutions_test(): + app = TestApp( handlers.app ) + filepath = PathToTestFile( ('testy-multiple-solutions/' + 'solution-named-like-folder/' + 'testy/Program.cs') ) + contents = open( filepath ).read() + event_data = BuildRequest( filepath = filepath, + filetype = 'cs', + contents = contents, + event_name = 'FileReadyToParse' ) + + # Here the server will raise an exception if it can't start + app.post_json( '/event_notification', event_data ) + + # Now for some cleanup: wait for the server to start then shut it down + while True: + result = app.post_json( '/run_completer_command', + BuildRequest( completer_target = 'filetype_default', + command_arguments = ['ServerRunning'], + filetype = 'cs' ) ).json + if result: + break + time.sleep( 0.2 ) + + # We need to turn off the CS server so that it doesn't stick around + app.post_json( '/run_completer_command', + BuildRequest( completer_target = 'filetype_default', + command_arguments = ['StopServer'], + filetype = 'cs' ) ) + +@with_setup( Setup ) +def GetCompletions_CsCompleter_DoesntStartWithAmbiguousMultipleSolutions_test(): + app = TestApp( handlers.app ) + filepath = PathToTestFile( ('testy-multiple-solutions/' + 'solution-not-named-like-folder/' + 'testy/Program.cs') ) + contents = open( filepath ).read() + event_data = BuildRequest( filepath = filepath, + filetype = 'cs', + contents = contents, + event_name = 'FileReadyToParse' ) + + exception_caught = False + try: + app.post_json( '/event_notification', event_data ) + except AppError as e: + if 'Found multiple solution files' in str(e): + exception_caught = True + + # the test passes if we caught an exception when trying to start it, + # so raise one if it managed to start + if not exception_caught: + # Now for some cleanup: wait for the server to start then shut it down + while True: + result = app.post_json( '/run_completer_command', + BuildRequest( completer_target = 'filetype_default', + command_arguments = ['ServerRunning'], + filetype = 'cs' ) ).json + if result: + break + time.sleep( 0.2 ) + + # We need to turn off the CS server so that it doesn't stick around + app.post_json( '/run_completer_command', + BuildRequest( completer_target = 'filetype_default', + command_arguments = ['StopServer'], + filetype = 'cs' ) ) + + raise Exception( ('The Omnisharp server started, despite us not being able ' + 'to find a suitable solution file to feed it. Did you ' + 'fiddle with the solution finding code in ' + 'cs_completer.py? Hopefully you\'ve enhanced it: you need' + 'to update this test then :)') ) @with_setup( Setup ) def GetCompletions_ClangCompleter_WorksWithExplicitFlags_test(): diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy.sln b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy.sln new file mode 100644 index 00000000..82e3d155 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testy", "testy/testy.csproj", "{0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.ActiveCfg = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.Build.0 = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.ActiveCfg = Release|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = testy/testy.csproj + EndGlobalSection +EndGlobal diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/Program.cs b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/Program.cs new file mode 100644 index 00000000..cac606c8 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace testy +{ + class MainClass + { + public static void Main (string[] args) + { + Console. + } + } +} diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/Properties/AssemblyInfo.cs b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..bed715b8 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. +[assembly: AssemblyTitle ("testy")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("valloric")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. +[assembly: AssemblyVersion ("1.0.*")] +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/testy.csproj b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/testy.csproj new file mode 100644 index 00000000..1f1c0dd1 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy/testy.csproj @@ -0,0 +1,41 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F} + Exe + testy + testy + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + full + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + \ No newline at end of file diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy2.sln b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy2.sln new file mode 100644 index 00000000..82e3d155 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-named-like-folder/testy2.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testy", "testy/testy.csproj", "{0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.ActiveCfg = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.Build.0 = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.ActiveCfg = Release|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = testy/testy.csproj + EndGlobalSection +EndGlobal diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/Program.cs b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/Program.cs new file mode 100644 index 00000000..cac606c8 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace testy +{ + class MainClass + { + public static void Main (string[] args) + { + Console. + } + } +} diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/Properties/AssemblyInfo.cs b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..bed715b8 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. +[assembly: AssemblyTitle ("testy")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("valloric")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. +[assembly: AssemblyVersion ("1.0.*")] +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/testy.csproj b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/testy.csproj new file mode 100644 index 00000000..1f1c0dd1 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy/testy.csproj @@ -0,0 +1,41 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F} + Exe + testy + testy + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + full + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + \ No newline at end of file diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy1.sln b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy1.sln new file mode 100644 index 00000000..82e3d155 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy1.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testy", "testy/testy.csproj", "{0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.ActiveCfg = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.Build.0 = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.ActiveCfg = Release|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = testy/testy.csproj + EndGlobalSection +EndGlobal diff --git a/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy2.sln b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy2.sln new file mode 100644 index 00000000..82e3d155 --- /dev/null +++ b/python/ycm/server/tests/testdata/testy-multiple-solutions/solution-not-named-like-folder/testy2.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testy", "testy/testy.csproj", "{0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.ActiveCfg = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Debug|x86.Build.0 = Debug|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.ActiveCfg = Release|x86 + {0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = testy/testy.csproj + EndGlobalSection +EndGlobal