Merge pull request #680 from nop00/master

Omnisharp: smarter solution file finder
This commit is contained in:
Val Markovic 2013-12-04 10:32:13 -08:00
commit 7f5dd0fcf0
12 changed files with 343 additions and 8 deletions

View File

@ -117,17 +117,31 @@ class CsharpCompleter( Completer ):
self._logger.info( 'startup' ) self._logger.info( 'startup' )
self._omnisharp_port = utils.GetUnusedLocalhostPort() 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( raise RuntimeError(
'Error starting OmniSharp server: no solutionfile found' ) 'Error starting OmniSharp server: no solutionfile found' )
elif len( solutionfiles ) == 1: elif len( solution_files ) == 1:
solutionfile = solutionfiles[ 0 ] solutionfile = solution_files[ 0 ]
else: else:
raise RuntimeError( # multiple solutions found : if there is one whose name is the same
'Found multiple solution files instead of one!\n{0}'.format( # as the folder containing the file we edit, use this one
solutionfiles ) ) # (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( omnisharp = os.path.join(
os.path.abspath( os.path.dirname( __file__ ) ), os.path.abspath( os.path.dirname( __file__ ) ),
@ -230,3 +244,20 @@ def _FindSolutionFiles( filepath ):
break break
solutionfiles = glob.glob1( folder, '*.sln' ) solutionfiles = glob.glob1( folder, '*.sln' )
return solutionfiles, folder 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 ]

View File

@ -23,7 +23,7 @@ import time
import httplib import httplib
from .test_utils import ( Setup, BuildRequest, PathToTestFile, from .test_utils import ( Setup, BuildRequest, PathToTestFile,
ChangeSpecificOptions ) ChangeSpecificOptions )
from webtest import TestApp from webtest import TestApp, AppError
from nose.tools import eq_, with_setup from nose.tools import eq_, with_setup
from hamcrest import ( assert_that, has_item, has_items, has_entry, from hamcrest import ( assert_that, has_item, has_items, has_entry,
contains_inanyorder, empty ) contains_inanyorder, empty )
@ -95,6 +95,80 @@ def GetCompletions_CsCompleter_Works_test():
command_arguments = ['StopServer'], command_arguments = ['StopServer'],
filetype = 'cs' ) ) 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 ) @with_setup( Setup )
def GetCompletions_ClangCompleter_WorksWithExplicitFlags_test(): def GetCompletions_ClangCompleter_WorksWithExplicitFlags_test():

View File

@ -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

View File

@ -0,0 +1,12 @@
using System;
namespace testy
{
class MainClass
{
public static void Main (string[] args)
{
Console.
}
}
}

View File

@ -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("")]

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>testy</RootNamespace>
<AssemblyName>testy</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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

View File

@ -0,0 +1,12 @@
using System;
namespace testy
{
class MainClass
{
public static void Main (string[] args)
{
Console.
}
}
}

View File

@ -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("")]

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{0C99F719-E00E-4CCD-AB9F-FEFBCD97C51F}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>testy</RootNamespace>
<AssemblyName>testy</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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

View File

@ -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