From dd612940f9d86a530aa5b545b6cbeb11a032de2a Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 23 Aug 2018 19:15:38 -0700 Subject: [PATCH 1/8] Add the misc files creation to the BufferManager --- .../MiscellaneousFilesProjectSystem.cs | 2 +- src/OmniSharp.Roslyn/BufferManager.cs | 45 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/OmniSharp.MiscellaneousFiles/MiscellaneousFilesProjectSystem.cs b/src/OmniSharp.MiscellaneousFiles/MiscellaneousFilesProjectSystem.cs index 054fb93707..f02e660575 100644 --- a/src/OmniSharp.MiscellaneousFiles/MiscellaneousFilesProjectSystem.cs +++ b/src/OmniSharp.MiscellaneousFiles/MiscellaneousFilesProjectSystem.cs @@ -17,7 +17,7 @@ namespace OmniSharp.MiscellaneousFile { [ExtensionOrder(After = ProjectSystemNames.MSBuildProjectSystem)] [ExtensionOrder(After = ProjectSystemNames.DotNetProjectSystem)] - [ExportProjectSystem(ProjectSystemNames.MiscellaneousFilesProjectSystem), Shared] + //[ExportProjectSystem(ProjectSystemNames.MiscellaneousFilesProjectSystem), Shared] public class MiscellaneousFilesProjectSystem : IProjectSystem { private const string miscFileExtension = ".cs"; diff --git a/src/OmniSharp.Roslyn/BufferManager.cs b/src/OmniSharp.Roslyn/BufferManager.cs index ded2c544f8..b1fb69e876 100644 --- a/src/OmniSharp.Roslyn/BufferManager.cs +++ b/src/OmniSharp.Roslyn/BufferManager.cs @@ -128,32 +128,35 @@ private bool TryAddTransientDocument(string fileName, string fileContent) var projects = FindProjectsByFileName(fileName); if (!projects.Any()) { - return false; + //todo: deal with the deletion of the misc files + _workspace.TryAddMiscellaneousDocument(fileName, LanguageNames.CSharp); } - - var sourceText = SourceText.From(fileContent); - var documentInfos = new List(); - foreach (var project in projects) + else { - var id = DocumentId.CreateNewId(project.Id); - var version = VersionStamp.Create(); - var documentInfo = DocumentInfo.Create( - id, fileName, filePath: fileName, - loader: TextLoader.From(TextAndVersion.Create(sourceText, version))); + var sourceText = SourceText.From(fileContent); + var documentInfos = new List(); + foreach (var project in projects) + { + var id = DocumentId.CreateNewId(project.Id); + var version = VersionStamp.Create(); + var documentInfo = DocumentInfo.Create( + id, fileName, filePath: fileName, + loader: TextLoader.From(TextAndVersion.Create(sourceText, version))); - documentInfos.Add(documentInfo); - } + documentInfos.Add(documentInfo); + } - lock (_lock) - { - var documentIds = documentInfos.Select(document => document.Id); - _transientDocuments.Add(fileName, documentIds); - _transientDocumentIds.UnionWith(documentIds); - } + lock (_lock) + { + var documentIds = documentInfos.Select(document => document.Id); + _transientDocuments.Add(fileName, documentIds); + _transientDocumentIds.UnionWith(documentIds); + } - foreach (var documentInfo in documentInfos) - { - _workspace.AddDocument(documentInfo); + foreach (var documentInfo in documentInfos) + { + _workspace.AddDocument(documentInfo); + } } return true; From a80525cd06450b870427acc3e188ca50c7347630 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 23 Aug 2018 19:22:34 -0700 Subject: [PATCH 2/8] Remove ordered project systems --- src/OmniSharp.Host/WorkspaceInitializer.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/OmniSharp.Host/WorkspaceInitializer.cs b/src/OmniSharp.Host/WorkspaceInitializer.cs index 2e4b82f130..cc1247c072 100644 --- a/src/OmniSharp.Host/WorkspaceInitializer.cs +++ b/src/OmniSharp.Host/WorkspaceInitializer.cs @@ -27,11 +27,9 @@ public static void Initialize( var projectEventForwarder = compositionHost.GetExport(); projectEventForwarder.Initialize(); - var projectSystems = compositionHost.GetExports>(); - var ps = projectSystems.Select(n => n.Value); - var orderedProjectSystems = ExtensionOrderer.GetOrderedOrUnorderedList(ps, eps => eps.Name); - - foreach (var projectSystem in orderedProjectSystems) + var projectSystems = compositionHost.GetExports(); + + foreach (var projectSystem in projectSystems) { try { From e13702cc9a0e0dcfe1a0e79329847d12c2da3e52 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 23 Aug 2018 19:29:47 -0700 Subject: [PATCH 3/8] Remove references to misc files --- OmniSharp.sln | 30 ---------------------- src/OmniSharp.Stdio/OmniSharp.Stdio.csproj | 1 - tests/TestUtility/OmniSharpTestHost.cs | 2 -- tests/TestUtility/TestUtility.csproj | 1 - 4 files changed, 34 deletions(-) diff --git a/OmniSharp.sln b/OmniSharp.sln index 76a7afeab5..c7744573da 100644 --- a/OmniSharp.sln +++ b/OmniSharp.sln @@ -69,10 +69,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OmniSharp.Stdio.Driver", "s EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OmniSharp.Script.Tests", "tests\OmniSharp.Script.Tests\OmniSharp.Script.Tests.csproj", "{9E4BA68C-7F4B-429A-A0C7-8CE7D41D610F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OmniSharp.MiscellaneousFiles", "src\OmniSharp.MiscellaneousFiles\OmniSharp.MiscellaneousFiles.csproj", "{49358F28-883B-4FA0-B853-8774A732E188}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OmniSharp.MiscellaneousFiles.Tests", "tests\OmniSharp.MiscellaneousFiles.Tests\OmniSharp.MiscellaneousFiles.Tests.csproj", "{51F1D224-A543-48C5-BD37-139B935E71D8}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -395,30 +391,6 @@ Global {9E4BA68C-7F4B-429A-A0C7-8CE7D41D610F}.Release|x64.Build.0 = Release|Any CPU {9E4BA68C-7F4B-429A-A0C7-8CE7D41D610F}.Release|x86.ActiveCfg = Release|Any CPU {9E4BA68C-7F4B-429A-A0C7-8CE7D41D610F}.Release|x86.Build.0 = Release|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Debug|Any CPU.Build.0 = Debug|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Debug|x64.ActiveCfg = Debug|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Debug|x64.Build.0 = Debug|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Debug|x86.ActiveCfg = Debug|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Debug|x86.Build.0 = Debug|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Release|Any CPU.ActiveCfg = Release|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Release|Any CPU.Build.0 = Release|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Release|x64.ActiveCfg = Release|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Release|x64.Build.0 = Release|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Release|x86.ActiveCfg = Release|Any CPU - {49358F28-883B-4FA0-B853-8774A732E188}.Release|x86.Build.0 = Release|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Debug|x64.ActiveCfg = Debug|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Debug|x64.Build.0 = Debug|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Debug|x86.ActiveCfg = Debug|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Debug|x86.Build.0 = Debug|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Release|Any CPU.Build.0 = Release|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Release|x64.ActiveCfg = Release|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Release|x64.Build.0 = Release|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Release|x86.ActiveCfg = Release|Any CPU - {51F1D224-A543-48C5-BD37-139B935E71D8}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -450,8 +422,6 @@ Global {BC640CBF-F6E2-42EA-9D61-FB6E515AEA44} = {2C348365-A9D8-459E-9276-56FC46AAEE31} {D2A78CEE-B278-476F-AF34-A7D6F792F973} = {2C348365-A9D8-459E-9276-56FC46AAEE31} {9E4BA68C-7F4B-429A-A0C7-8CE7D41D610F} = {35E025BF-BBB2-4FAC-9F4B-37CBA083EE47} - {49358F28-883B-4FA0-B853-8774A732E188} = {2C348365-A9D8-459E-9276-56FC46AAEE31} - {51F1D224-A543-48C5-BD37-139B935E71D8} = {35E025BF-BBB2-4FAC-9F4B-37CBA083EE47} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4DD725CE-B49A-4151-8B77-BB33FE88E46E} diff --git a/src/OmniSharp.Stdio/OmniSharp.Stdio.csproj b/src/OmniSharp.Stdio/OmniSharp.Stdio.csproj index 6b7ca88600..29ee1a386c 100644 --- a/src/OmniSharp.Stdio/OmniSharp.Stdio.csproj +++ b/src/OmniSharp.Stdio/OmniSharp.Stdio.csproj @@ -8,7 +8,6 @@ - diff --git a/tests/TestUtility/OmniSharpTestHost.cs b/tests/TestUtility/OmniSharpTestHost.cs index fb78cc746f..7741ca5ff9 100644 --- a/tests/TestUtility/OmniSharpTestHost.cs +++ b/tests/TestUtility/OmniSharpTestHost.cs @@ -15,7 +15,6 @@ using OmniSharp.DotNetTest.Models; using OmniSharp.Eventing; using OmniSharp.Mef; -using OmniSharp.MiscellaneousFile; using OmniSharp.Models.WorkspaceInformation; using OmniSharp.MSBuild; using OmniSharp.Options; @@ -42,7 +41,6 @@ public class OmniSharpTestHost : DisposableObject typeof(OmniSharpWorkspace).GetTypeInfo().Assembly, // OmniSharp.Roslyn typeof(RoslynFeaturesHostServicesProvider).GetTypeInfo().Assembly, // OmniSharp.Roslyn.CSharp typeof(CakeProjectSystem).GetTypeInfo().Assembly, // OmniSharp.Cake - typeof(MiscellaneousFilesProjectSystem).GetTypeInfo().Assembly // OmniSharp.MiscellanousFiles }); private readonly TestServiceProvider _serviceProvider; diff --git a/tests/TestUtility/TestUtility.csproj b/tests/TestUtility/TestUtility.csproj index ede88fae3d..14de90b483 100644 --- a/tests/TestUtility/TestUtility.csproj +++ b/tests/TestUtility/TestUtility.csproj @@ -9,7 +9,6 @@ - From e2ead0c9af5906596615176d8a3adff00604bce9 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 23 Aug 2018 19:30:40 -0700 Subject: [PATCH 4/8] Delete the misc files project system --- .../MiscellaneousFilesProjectSystem.cs | 111 ------ .../OmniSharp.MiscellaneousFiles.csproj | 17 - .../AssemblyInfo.cs | 1 - .../EndpointFacts.cs | 376 ------------------ .../OmniSharp.MiscellaneousFiles.Tests.csproj | 27 -- 5 files changed, 532 deletions(-) delete mode 100644 src/OmniSharp.MiscellaneousFiles/MiscellaneousFilesProjectSystem.cs delete mode 100644 src/OmniSharp.MiscellaneousFiles/OmniSharp.MiscellaneousFiles.csproj delete mode 100644 tests/OmniSharp.MiscellaneousFiles.Tests/AssemblyInfo.cs delete mode 100644 tests/OmniSharp.MiscellaneousFiles.Tests/EndpointFacts.cs delete mode 100644 tests/OmniSharp.MiscellaneousFiles.Tests/OmniSharp.MiscellaneousFiles.Tests.csproj diff --git a/src/OmniSharp.MiscellaneousFiles/MiscellaneousFilesProjectSystem.cs b/src/OmniSharp.MiscellaneousFiles/MiscellaneousFilesProjectSystem.cs deleted file mode 100644 index f02e660575..0000000000 --- a/src/OmniSharp.MiscellaneousFiles/MiscellaneousFilesProjectSystem.cs +++ /dev/null @@ -1,111 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Composition; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; -using OmniSharp.FileSystem; -using OmniSharp.FileWatching; -using OmniSharp.Mef; -using OmniSharp.Models.WorkspaceInformation; -using OmniSharp.Services; - -namespace OmniSharp.MiscellaneousFile -{ - [ExtensionOrder(After = ProjectSystemNames.MSBuildProjectSystem)] - [ExtensionOrder(After = ProjectSystemNames.DotNetProjectSystem)] - //[ExportProjectSystem(ProjectSystemNames.MiscellaneousFilesProjectSystem), Shared] - public class MiscellaneousFilesProjectSystem : IProjectSystem - { - private const string miscFileExtension = ".cs"; - public string Key => ProjectSystemNames.MiscellaneousFilesProjectSystem; - public string Language => LanguageNames.CSharp; - IEnumerable IProjectSystem.Extensions => new[] { miscFileExtension }; - public bool EnabledByDefault { get; } = true; - - private readonly OmniSharpWorkspace _workspace; - private readonly IFileSystemWatcher _fileSystemWatcher; - private readonly FileSystemHelper _fileSystemHelper; - private readonly List _projectSystems; - private readonly ILogger _logger; - - [ImportingConstructor] - public MiscellaneousFilesProjectSystem(OmniSharpWorkspace workspace, IFileSystemWatcher fileSystemWatcher, FileSystemHelper fileSystemHelper, - ILoggerFactory loggerFactory, [ImportMany] IEnumerable> projectSystems) - { - _workspace = workspace; - _fileSystemWatcher = fileSystemWatcher; - _fileSystemHelper = fileSystemHelper; - _logger = loggerFactory.CreateLogger(); - _projectSystems = projectSystems - .Where(ps => ps.Metadata.Name == ProjectSystemNames.MSBuildProjectSystem || - ps.Metadata.Name == ProjectSystemNames.DotNetProjectSystem) - .Select(ps => ps.Value) - .Cast() - .ToList(); - } - - Task IProjectSystem.GetProjectModelAsync(string filePath) - { - return Task.FromResult(null); - } - - Task IProjectSystem.GetWorkspaceModelAsync(WorkspaceInformationRequest request) - { - return Task.FromResult(null); - } - - void IProjectSystem.Initalize(IConfiguration configuration) - { - var allFiles = _fileSystemHelper.GetFiles("**/*.cs"); - foreach (var filePath in allFiles) - TryAddMiscellaneousFile(filePath); - - _fileSystemWatcher.Watch(miscFileExtension, OnMiscellaneousFileChanged); - } - - private async void TryAddMiscellaneousFile(string filePath) - { - //wait for the project systems to finish processing the updates - foreach (var projectSystem in _projectSystems) - { - await projectSystem.WaitForUpdatesAsync(); - } - - var absoluteFilePath = new FileInfo(filePath).FullName; - if (!File.Exists(absoluteFilePath)) - return; - - if (_workspace.TryAddMiscellaneousDocument(absoluteFilePath, Language) != null) - { - _logger.LogInformation($"Successfully added file '{absoluteFilePath}' to workspace"); - } - } - - private void OnMiscellaneousFileChanged(string filePath, FileChangeType changeType) - { - if (changeType == FileChangeType.Unspecified && File.Exists(filePath) || - changeType == FileChangeType.Create) - { - TryAddMiscellaneousFile(filePath); - } - - else if (changeType == FileChangeType.Unspecified && !File.Exists(filePath) || - changeType == FileChangeType.Delete) - { - RemoveFromWorkspace(filePath); - } - } - - private void RemoveFromWorkspace(string filePath) - { - if (_workspace.TryRemoveMiscellaneousDocument(filePath)) - { - _logger.LogDebug($"Removed file '{filePath}' from the workspace."); - } - } - } -} diff --git a/src/OmniSharp.MiscellaneousFiles/OmniSharp.MiscellaneousFiles.csproj b/src/OmniSharp.MiscellaneousFiles/OmniSharp.MiscellaneousFiles.csproj deleted file mode 100644 index 89bb4ca4c4..0000000000 --- a/src/OmniSharp.MiscellaneousFiles/OmniSharp.MiscellaneousFiles.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - net461 - AnyCPU - - - - - - - - - - - - diff --git a/tests/OmniSharp.MiscellaneousFiles.Tests/AssemblyInfo.cs b/tests/OmniSharp.MiscellaneousFiles.Tests/AssemblyInfo.cs deleted file mode 100644 index 9933b8fd08..0000000000 --- a/tests/OmniSharp.MiscellaneousFiles.Tests/AssemblyInfo.cs +++ /dev/null @@ -1 +0,0 @@ -[assembly: Xunit.CollectionBehavior(DisableTestParallelization = true)] diff --git a/tests/OmniSharp.MiscellaneousFiles.Tests/EndpointFacts.cs b/tests/OmniSharp.MiscellaneousFiles.Tests/EndpointFacts.cs deleted file mode 100644 index 9fd6d7647a..0000000000 --- a/tests/OmniSharp.MiscellaneousFiles.Tests/EndpointFacts.cs +++ /dev/null @@ -1,376 +0,0 @@ -using System.Linq; -using System.Threading.Tasks; -using OmniSharp.Models; -using OmniSharp.Models.CodeCheck; -using OmniSharp.Models.FilesChanged; -using OmniSharp.Models.FindImplementations; -using OmniSharp.Models.FindSymbols; -using OmniSharp.Models.FindUsages; -using OmniSharp.Models.FixUsings; -using OmniSharp.Models.SignatureHelp; -using OmniSharp.Models.TypeLookup; -using OmniSharp.Roslyn.CSharp.Services.Files; -using OmniSharp.Roslyn.CSharp.Services.Types; -using TestUtility; -using Xunit; -using Xunit.Abstractions; - -namespace OmniSharp.MiscellaneousFiles.Tests -{ - public class EndpointFacts : AbstractTestFixture - { - public EndpointFacts(ITestOutputHelper output) - : base(output) - { - } - - [Fact] - public async Task Returns_only_syntactic_diagnotics() - { - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - var testfile = new TestFile("a.cs", "class C { b a = new b(); int n }"); - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath = AddTestFile(testProject, testfile); - await WaitForFileUpdate(filePath, host); - var request = new CodeCheckRequest() { FileName = filePath }; - var actual = await host.GetResponse(OmniSharpEndpoints.CodeCheck, request); - Assert.Single(actual.QuickFixes); - Assert.Equal("; expected", actual.QuickFixes.First().Text); - } - } - } - - [Fact] - public async Task Returns_Signature_help() - { - const string source = -@"class Program -{ - public static void Main(){ - System.Guid.NewGuid($$); - } -}"; - var testfile = new TestFile("a.cs", source); - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath = AddTestFile(testProject, testfile); - await WaitForFileUpdate(filePath, host); - var point = testfile.Content.GetPointFromPosition(); - var request = new SignatureHelpRequest() - { - FileName = filePath, - Line = point.Line, - Column = point.Offset, - Buffer = testfile.Content.Code - }; - - var actual = await host.GetResponse(OmniSharpEndpoints.SignatureHelp, request); - Assert.Single(actual.Signatures); - Assert.Equal(0, actual.ActiveParameter); - Assert.Equal(0, actual.ActiveSignature); - Assert.Equal("NewGuid", actual.Signatures.ElementAt(0).Name); - Assert.Empty(actual.Signatures.ElementAt(0).Parameters); - } - } - } - - [Fact] - public async Task Returns_Implementations() - { - const string source = @" - public class MyClass - { - public MyClass() { Fo$$o(); } - - public void Foo() {} - }"; - - var testfile = new TestFile("a.cs", source); - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath = AddTestFile(testProject, testfile); - await WaitForFileUpdate(filePath, host); - var point = testfile.Content.GetPointFromPosition(); - var request = new FindImplementationsRequest() - { - FileName = filePath, - Line = point.Line, - Column = point.Offset, - Buffer = testfile.Content.Code - }; - - var actual = await host.GetResponse(OmniSharpEndpoints.FindImplementations, request); - Assert.Single(actual.QuickFixes); - Assert.Equal("public void Foo() {}", actual.QuickFixes.First().Text.Trim()); - } - } - } - - [Fact] - public async Task Returns_Usages() - { - const string source = @" - public class F$$oo - { - public string prop { get; set; } - } - - public class FooConsumer - { - public FooConsumer() - { - var temp = new Foo(); - var prop = foo.prop; - } - }"; - - var testfile = new TestFile("a.cs", source); - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath = AddTestFile(testProject, testfile); - await WaitForFileUpdate(filePath, host); - - var point = testfile.Content.GetPointFromPosition(); - var request = new FindUsagesRequest() - { - FileName = filePath, - Line = point.Line, - Column = point.Offset, - Buffer = testfile.Content.Code - }; - - var actual = await host.GetResponse(OmniSharpEndpoints.FindUsages, request); - Assert.Equal(2, actual.QuickFixes.Count()); - } - } - } - - [Fact] - public async Task Returns_Symbols() - { - const string source = @" - namespace Some.Long.Namespace - { - public class Foo - { - private string _field = 0; - private string AutoProperty { get; } - private string Property - { - get { return _field; } - set { _field = value; } - } - private string Method() {} - private string Method(string param) {} - - private class Nested - { - private string NestedMethod() {} - } - } - }"; - - var testfile = new TestFile("a.cs", source); - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath = AddTestFile(testProject, testfile); - await WaitForFileUpdate(filePath, host); - var actual = await host.GetResponse(OmniSharpEndpoints.FindSymbols, null); - var symbols = actual.QuickFixes.Select(q => q.Text); - - var expected = new[] - { - "Foo", - "_field", - "AutoProperty", - "Property", - "Method()", - "Method(string param)", - "Nested", - "NestedMethod()" - }; - - Assert.Equal(expected, symbols); - } - } - } - - [Fact] - public async Task Returns_FixUsings() - { - const string code = @" -namespace nsA -{ - public class classX{} -} - -namespace OmniSharp -{ - public class class1 - { - public method1() - { - var c1 = new classX(); - } - } -}"; - - const string expectedCode = @" -using nsA; - -namespace nsA -{ - public class classX{} -} - -namespace OmniSharp -{ - public class class1 - { - public method1() - { - var c1 = new classX(); - } - } -}"; - - var testfile = new TestFile("a.cs", code); - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath = AddTestFile(testProject, testfile); - await WaitForFileUpdate(filePath, host); - var request = new FixUsingsRequest(){ FileName = filePath }; - var actual = await host.GetResponse(OmniSharpEndpoints.FixUsings, request); - Assert.Equal(expectedCode.Replace("\r\n", "\n"), actual.Buffer.Replace("\r\n", "\n")); - } - } - } - - [Fact] - public async Task Returns_TypeLookup() - { - const string code = @"class F$$oo {}"; - var testfile = new TestFile("a.cs", code); - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath = AddTestFile(testProject, testfile); - await WaitForFileUpdate(filePath, host); - var service = host.GetRequestHandler(OmniSharpEndpoints.TypeLookup); - var point = testfile.Content.GetPointFromPosition(); - var request = new TypeLookupRequest - { - FileName = filePath, - Line = point.Line, - Column = point.Offset, - }; - - var actual = await host.GetResponse(OmniSharpEndpoints.TypeLookup, request); - Assert.Equal("Foo", actual.Type); - } - } - } - - [Fact] - public async Task Adds_Multiple_Misc_Files_To_Same_project() - { - const string source1 = -@"class Program -{ - public static void Main(){ - A a = new A(4, $$5); - } -}"; - - const string source2 = -@"class A -{ - A(int a, int b) - { - } -}"; - var testfile1 = new TestFile("file1.cs", source1); - var testfile2 = new TestFile("file2.cs", source2); - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath1 = AddTestFile(testProject, testfile1); - var filePath2 = AddTestFile(testProject, testfile2); - await WaitForFileUpdate(filePath1, host); - await WaitForFileUpdate(filePath2, host); - var point = testfile1.Content.GetPointFromPosition(); - var request = new SignatureHelpRequest() - { - FileName = filePath1, - Line = point.Line, - Column = point.Offset, - Buffer = testfile1.Content.Code - }; - - var actual = await host.GetResponse(OmniSharpEndpoints.SignatureHelp, request); - Assert.Single(actual.Signatures); - Assert.Equal(1, actual.ActiveParameter); - Assert.Equal(0, actual.ActiveSignature); - Assert.Equal("A", actual.Signatures.ElementAt(0).Name); - Assert.Equal(2, actual.Signatures.ElementAt(0).Parameters.Count()); - } - } - } - - [Fact] - public async Task Handles_File_Deletion() - { - //When the file is deleted the diagnostics must not be returned - using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) - { - var testfile = new TestFile("a.cs", "class C { b a = new b(); int n }"); - using (var host = CreateOmniSharpHost(testProject.Directory)) - { - var filePath = AddTestFile(testProject, testfile); - await WaitForFileUpdate(filePath, host); - var request = new CodeCheckRequest() { FileName = filePath }; - var actual = await host.GetResponse(OmniSharpEndpoints.CodeCheck, request); - Assert.Single(actual.QuickFixes); - - await WaitForFileUpdate(filePath, host, FileWatching.FileChangeType.Delete); - actual = await host.GetResponse(OmniSharpEndpoints.CodeCheck, request); - Assert.Empty(actual.QuickFixes); - } - } - } - - private string AddTestFile(ITestProject testProject, TestFile testfile) - { - return testProject.AddDisposableFile(testfile.FileName, testfile.Content.Text.ToString()); - } - - private async Task WaitForFileUpdate(string filePath, OmniSharpTestHost host, FileWatching.FileChangeType changeType = FileWatching.FileChangeType.Create) - { - var fileChangedService = host.GetRequestHandler(OmniSharpEndpoints.FilesChanged); - await fileChangedService.Handle(new[] - { - new FilesChangedRequest - { - FileName = filePath, - ChangeType = changeType - } - }); - - await Task.Delay(2000); - } - } -} diff --git a/tests/OmniSharp.MiscellaneousFiles.Tests/OmniSharp.MiscellaneousFiles.Tests.csproj b/tests/OmniSharp.MiscellaneousFiles.Tests/OmniSharp.MiscellaneousFiles.Tests.csproj deleted file mode 100644 index 7837179d01..0000000000 --- a/tests/OmniSharp.MiscellaneousFiles.Tests/OmniSharp.MiscellaneousFiles.Tests.csproj +++ /dev/null @@ -1,27 +0,0 @@ - - - - net461 - AnyCPU - true - - - - - - - - - - - - - - - - - - - - - From 84a0e28d020971c327213529209519f0ca75c278 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 23 Aug 2018 19:59:57 -0700 Subject: [PATCH 5/8] Clean up --- src/OmniSharp.Abstractions/Mef/ProjectSystemMetadata.cs | 7 ------- src/OmniSharp.Abstractions/Services/IUpdates.cs | 9 --------- src/OmniSharp.DotNet/DotNetProjectSystem.cs | 7 +------ src/OmniSharp.MSBuild/ProjectSystem.cs | 7 +------ 4 files changed, 2 insertions(+), 28 deletions(-) delete mode 100644 src/OmniSharp.Abstractions/Mef/ProjectSystemMetadata.cs delete mode 100644 src/OmniSharp.Abstractions/Services/IUpdates.cs diff --git a/src/OmniSharp.Abstractions/Mef/ProjectSystemMetadata.cs b/src/OmniSharp.Abstractions/Mef/ProjectSystemMetadata.cs deleted file mode 100644 index ef2498dea3..0000000000 --- a/src/OmniSharp.Abstractions/Mef/ProjectSystemMetadata.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace OmniSharp.Mef -{ - public class ProjectSystemMetadata - { - public string Name { get; set; } - } -} diff --git a/src/OmniSharp.Abstractions/Services/IUpdates.cs b/src/OmniSharp.Abstractions/Services/IUpdates.cs deleted file mode 100644 index 4c70f27a1d..0000000000 --- a/src/OmniSharp.Abstractions/Services/IUpdates.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Threading.Tasks; - -namespace OmniSharp.Services -{ - public interface IWaitableProjectSystem: IProjectSystem - { - Task WaitForUpdatesAsync(); - } -} diff --git a/src/OmniSharp.DotNet/DotNetProjectSystem.cs b/src/OmniSharp.DotNet/DotNetProjectSystem.cs index cb57bb455c..796deda7dc 100644 --- a/src/OmniSharp.DotNet/DotNetProjectSystem.cs +++ b/src/OmniSharp.DotNet/DotNetProjectSystem.cs @@ -25,7 +25,7 @@ namespace OmniSharp.DotNet { [ExportProjectSystem(ProjectSystemNames.DotNetProjectSystem), Shared] - public class DotNetProjectSystem : IWaitableProjectSystem + public class DotNetProjectSystem : IProjectSystem { private const string CompilationConfiguration = "Debug"; @@ -424,10 +424,5 @@ private static LanguageVersion ParseLanguageVersion(string value) return languageVersion; } - - async Task IWaitableProjectSystem.WaitForUpdatesAsync() - { - await ((IProjectSystem)this).GetWorkspaceModelAsync(new WorkspaceInformationRequest()); - } } } diff --git a/src/OmniSharp.MSBuild/ProjectSystem.cs b/src/OmniSharp.MSBuild/ProjectSystem.cs index d86f0d5a72..74ba90e759 100644 --- a/src/OmniSharp.MSBuild/ProjectSystem.cs +++ b/src/OmniSharp.MSBuild/ProjectSystem.cs @@ -22,7 +22,7 @@ namespace OmniSharp.MSBuild { [ExportProjectSystem(ProjectSystemNames.MSBuildProjectSystem), Shared] - public class ProjectSystem : IWaitableProjectSystem + public class ProjectSystem : IProjectSystem { private readonly IOmniSharpEnvironment _environment; private readonly OmniSharpWorkspace _workspace; @@ -210,10 +210,5 @@ async Task IProjectSystem.GetProjectModelAsync(string filePath) return new MSBuildProjectInfo(projectFileInfo); } - - public async Task WaitForUpdatesAsync() - { - await _manager.WaitForQueueEmptyAsync(); - } } } From e45144e6527ba28b2ea4eeae525764e2b505ea3e Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 27 Aug 2018 13:54:51 -0700 Subject: [PATCH 6/8] Add file system watch to the buffer manager --- src/OmniSharp.Roslyn/BufferManager.cs | 21 +++++++++++++++++-- src/OmniSharp.Roslyn/OmniSharpWorkspace.cs | 5 +++-- .../LineIndexHelperFacts.cs | 2 +- .../BufferFacts.cs | 3 ++- .../BufferManagerFacts.cs | 5 +++-- tests/TestUtility/TestHelpers.cs | 3 ++- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/OmniSharp.Roslyn/BufferManager.cs b/src/OmniSharp.Roslyn/BufferManager.cs index b1fb69e876..fdd396afa8 100644 --- a/src/OmniSharp.Roslyn/BufferManager.cs +++ b/src/OmniSharp.Roslyn/BufferManager.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Text; +using OmniSharp.FileWatching; using OmniSharp.Models; using OmniSharp.Models.ChangeBuffer; using OmniSharp.Models.UpdateBuffer; @@ -17,11 +18,13 @@ public class BufferManager private readonly IDictionary> _transientDocuments = new Dictionary>(StringComparer.OrdinalIgnoreCase); private readonly ISet _transientDocumentIds = new HashSet(); private readonly object _lock = new object(); + private readonly IFileSystemWatcher _fileSystemWatcher; - public BufferManager(OmniSharpWorkspace workspace) + public BufferManager(OmniSharpWorkspace workspace, IFileSystemWatcher fileSystemWatcher) { _workspace = workspace; _workspace.WorkspaceChanged += OnWorkspaceChanged; + _fileSystemWatcher = fileSystemWatcher; } public async Task UpdateBufferAsync(Request request) @@ -129,7 +132,13 @@ private bool TryAddTransientDocument(string fileName, string fileContent) if (!projects.Any()) { //todo: deal with the deletion of the misc files - _workspace.TryAddMiscellaneousDocument(fileName, LanguageNames.CSharp); + if (fileName.EndsWith(".cs") && _workspace.TryAddMiscellaneousDocument(fileName, LanguageNames.CSharp) != null) + { + _fileSystemWatcher.Watch(fileName, OnMiscFileChanged); + return true; + } + + return false; } else { @@ -162,6 +171,14 @@ private bool TryAddTransientDocument(string fileName, string fileContent) return true; } + private void OnMiscFileChanged(string filePath, FileChangeType changeType) + { + if(changeType == FileChangeType.Unspecified && !File.Exists(filePath) || changeType == FileChangeType.Delete) + { + _workspace.TryRemoveMiscellaneousDocument(filePath); + } + } + private IEnumerable FindProjectsByFileName(string fileName) { var fileInfo = new FileInfo(fileName); diff --git a/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs b/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs index 6ed38b5f0c..1e0a9b0bc9 100644 --- a/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs +++ b/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Text; using Microsoft.Extensions.Logging; +using OmniSharp.FileWatching; using OmniSharp.Roslyn; using OmniSharp.Roslyn.Utilities; using OmniSharp.Utilities; @@ -26,10 +27,10 @@ public class OmniSharpWorkspace : Workspace private readonly ConcurrentDictionary miscDocumentsProjectInfos = new ConcurrentDictionary(); [ImportingConstructor] - public OmniSharpWorkspace(HostServicesAggregator aggregator, ILoggerFactory loggerFactory) + public OmniSharpWorkspace(HostServicesAggregator aggregator, ILoggerFactory loggerFactory, IFileSystemWatcher fileSystemWatcher) : base(aggregator.CreateHostServices(), "Custom") { - BufferManager = new BufferManager(this); + BufferManager = new BufferManager(this, fileSystemWatcher); _logger = loggerFactory.CreateLogger(); } diff --git a/tests/OmniSharp.Cake.Tests/LineIndexHelperFacts.cs b/tests/OmniSharp.Cake.Tests/LineIndexHelperFacts.cs index 9fa0d1f4d6..3dc6f031f4 100644 --- a/tests/OmniSharp.Cake.Tests/LineIndexHelperFacts.cs +++ b/tests/OmniSharp.Cake.Tests/LineIndexHelperFacts.cs @@ -59,7 +59,7 @@ private static OmniSharpWorkspace CreateSimpleWorkspace(string fileName, string var workspace = new OmniSharpWorkspace( new HostServicesAggregator( Enumerable.Empty(), new LoggerFactory()), - new LoggerFactory()); + new LoggerFactory(), null); var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), "ProjectNameVal", "AssemblyNameVal", LanguageNames.CSharp); diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/BufferFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/BufferFacts.cs index e0bc99202c..9ff43c80eb 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/BufferFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/BufferFacts.cs @@ -3,6 +3,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Text; using Microsoft.Extensions.Logging; +using OmniSharp.FileWatching; using OmniSharp.Models.ChangeBuffer; using OmniSharp.Roslyn.CSharp.Services.Buffer; using OmniSharp.Services; @@ -17,7 +18,7 @@ public class BufferFacts var workspace = new OmniSharpWorkspace( new HostServicesAggregator( Enumerable.Empty(), new LoggerFactory()), - new LoggerFactory()); + new LoggerFactory(), new ManualFileSystemWatcher()); var service = new ChangeBufferService(workspace); diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerFacts.cs index a868cd9691..57297da047 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerFacts.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using OmniSharp.FileWatching; using OmniSharp.Models; using OmniSharp.Models.UpdateBuffer; using OmniSharp.Services; @@ -97,7 +98,7 @@ public async Task UpdateBufferFindsProjectBasedOnNearestPath() var workspace = new OmniSharpWorkspace( new HostServicesAggregator( Enumerable.Empty(), new LoggerFactory()), - new LoggerFactory()); + new LoggerFactory(), new ManualFileSystemWatcher()); TestHelpers.AddProjectToWorkspace(workspace, filePath: Path.Combine("src", "root", "foo.csproj"), @@ -164,7 +165,7 @@ private static OmniSharpWorkspace GetWorkspaceWithProjects() var workspace = new OmniSharpWorkspace( new HostServicesAggregator( Enumerable.Empty(), new LoggerFactory()), - new LoggerFactory()); + new LoggerFactory(), new ManualFileSystemWatcher()); TestHelpers.AddProjectToWorkspace(workspace, filePath: Path.Combine("src", "project.json"), diff --git a/tests/TestUtility/TestHelpers.cs b/tests/TestUtility/TestHelpers.cs index 7af0b6cfce..a2f4c34a75 100644 --- a/tests/TestUtility/TestHelpers.cs +++ b/tests/TestUtility/TestHelpers.cs @@ -6,6 +6,7 @@ using Microsoft.CodeAnalysis.Scripting.Hosting; using Microsoft.Extensions.Logging; using OmniSharp; +using OmniSharp.FileWatching; using OmniSharp.Script; using OmniSharp.Services; @@ -15,7 +16,7 @@ public static class TestHelpers { public static OmniSharpWorkspace CreateCsxWorkspace(TestFile testFile) { - var workspace = new OmniSharpWorkspace(new HostServicesAggregator(Enumerable.Empty(), new LoggerFactory()), new LoggerFactory()); + var workspace = new OmniSharpWorkspace(new HostServicesAggregator(Enumerable.Empty(), new LoggerFactory()), new LoggerFactory(), new ManualFileSystemWatcher()); AddCsxProjectToWorkspace(workspace, testFile); return workspace; } From c3e7805748aaccd932947cb46e5c8b4a089d0935 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 27 Aug 2018 14:40:08 -0700 Subject: [PATCH 7/8] Add tests for the buffer manager misc files --- .../BufferManagerFacts.cs | 6 +- .../BufferManagerMiscFilesFacts.cs | 362 ++++++++++++++++++ 2 files changed, 365 insertions(+), 3 deletions(-) create mode 100644 tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerMiscFilesFacts.cs diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerFacts.cs index 57297da047..2eb53cf89f 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerFacts.cs @@ -42,12 +42,12 @@ public async Task UpdateBufferIgnoresVoidRequests() } [Fact] - public async Task UpdateBufferIgnoresFilePathsThatDontMatchAProjectPath() + public async Task UpdateBufferIgnoresNonCsFilePathsThatDontMatchAProjectPath() { var workspace = GetWorkspaceWithProjects(); - await workspace.BufferManager.UpdateBufferAsync(new Request() { FileName = Path.Combine("some", " path.cs"), Buffer = "enum E {}" }); - var documents = workspace.GetDocuments(Path.Combine("some", "path.cs")); + await workspace.BufferManager.UpdateBufferAsync(new Request() { FileName = Path.Combine("some", " path.fs"), Buffer = "enum E {}" }); + var documents = workspace.GetDocuments(Path.Combine("some", "path.fs")); Assert.Empty(documents); } diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerMiscFilesFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerMiscFilesFacts.cs new file mode 100644 index 0000000000..c3ed03acec --- /dev/null +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/BufferManagerMiscFilesFacts.cs @@ -0,0 +1,362 @@ +using System.Linq; +using System.Threading.Tasks; +using OmniSharp.Models; +using OmniSharp.Models.CodeCheck; +using OmniSharp.Models.FilesChanged; +using OmniSharp.Models.FindImplementations; +using OmniSharp.Models.FindSymbols; +using OmniSharp.Models.FindUsages; +using OmniSharp.Models.FixUsings; +using OmniSharp.Models.SignatureHelp; +using OmniSharp.Models.TypeLookup; +using OmniSharp.Roslyn.CSharp.Services.Files; +using OmniSharp.Roslyn.CSharp.Services.Types; +using TestUtility; +using Xunit; +using Xunit.Abstractions; + +namespace OmniSharp.Tests +{ + public class BufferManagerMiscFilesFacts : AbstractTestFixture + { + public BufferManagerMiscFilesFacts(ITestOutputHelper output) + : base(output) + { + } + + [Fact] + public async Task Adds_Misc_Document_Which_Supports_Only_syntactic_diagnostics() + { + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + var testfile = new TestFile("a.cs", "class C { b a = new b(); int n }"); + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath = await AddTestFile(host, testProject, testfile); + var request = new CodeCheckRequest() { FileName = filePath }; + var actual = await host.GetResponse(OmniSharpEndpoints.CodeCheck, request); + Assert.Single(actual.QuickFixes); + Assert.Equal("; expected", actual.QuickFixes.First().Text); + } + } + } + + [Fact] + public async Task Adds_Misc_Document_Which_Supports_Signature_help() + { + const string source = +@"class Program +{ + public static void Main(){ + System.Guid.NewGuid($$); + } +}"; + var testfile = new TestFile("a.cs", source); + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath = await AddTestFile(host, testProject, testfile); + var point = testfile.Content.GetPointFromPosition(); + var request = new SignatureHelpRequest() + { + FileName = filePath, + Line = point.Line, + Column = point.Offset, + Buffer = testfile.Content.Code + }; + + var actual = await host.GetResponse(OmniSharpEndpoints.SignatureHelp, request); + Assert.Single(actual.Signatures); + Assert.Equal(0, actual.ActiveParameter); + Assert.Equal(0, actual.ActiveSignature); + Assert.Equal("NewGuid", actual.Signatures.ElementAt(0).Name); + Assert.Empty(actual.Signatures.ElementAt(0).Parameters); + } + } + } + + [Fact] + public async Task Adds_Misc_Document_Which_Supports_Implementations() + { + const string source = @" + public class MyClass + { + public MyClass() { Fo$$o(); } + public void Foo() {} + }"; + + var testfile = new TestFile("a.cs", source); + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath = await AddTestFile(host, testProject, testfile); + var point = testfile.Content.GetPointFromPosition(); + var request = new FindImplementationsRequest() + { + FileName = filePath, + Line = point.Line, + Column = point.Offset, + Buffer = testfile.Content.Code + }; + + var actual = await host.GetResponse(OmniSharpEndpoints.FindImplementations, request); + Assert.Single(actual.QuickFixes); + Assert.Equal("public void Foo() {}", actual.QuickFixes.First().Text.Trim()); + } + } + } + + [Fact] + public async Task Adds_Misc_Document_Which_Supports_Usages() + { + const string source = @" + public class F$$oo + { + public string prop { get; set; } + } + public class FooConsumer + { + public FooConsumer() + { + var temp = new Foo(); + var prop = foo.prop; + } + }"; + + var testfile = new TestFile("a.cs", source); + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath = await AddTestFile(host, testProject, testfile); + var point = testfile.Content.GetPointFromPosition(); + var request = new FindUsagesRequest() + { + FileName = filePath, + Line = point.Line, + Column = point.Offset, + Buffer = testfile.Content.Code + }; + + var actual = await host.GetResponse(OmniSharpEndpoints.FindUsages, request); + Assert.Equal(2, actual.QuickFixes.Count()); + } + } + } + + [Fact] + public async Task Adds_Misc_Document_Which_Supports_Symbols() + { + const string source = @" + namespace Some.Long.Namespace + { + public class Foo + { + private string _field = 0; + private string AutoProperty { get; } + private string Property + { + get { return _field; } + set { _field = value; } + } + private string Method() {} + private string Method(string param) {} + private class Nested + { + private string NestedMethod() {} + } + } + }"; + + var testfile = new TestFile("a.cs", source); + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath = await AddTestFile(host, testProject, testfile); + var actual = await host.GetResponse(OmniSharpEndpoints.FindSymbols, null); + var symbols = actual.QuickFixes.Select(q => q.Text); + + var expected = new[] + { + "Foo", + "_field", + "AutoProperty", + "Property", + "Method()", + "Method(string param)", + "Nested", + "NestedMethod()" + }; + + Assert.Equal(expected, symbols); + } + } + } + + [Fact] + public async Task Adds_Misc_Document_Which_Supports_FixUsings() + { + const string code = @" +namespace nsA +{ + public class classX{} +} +namespace OmniSharp +{ + public class class1 + { + public method1() + { + var c1 = new classX(); + } + } +}"; + + const string expectedCode = @" +using nsA; + +namespace nsA +{ + public class classX{} +} +namespace OmniSharp +{ + public class class1 + { + public method1() + { + var c1 = new classX(); + } + } +}"; + + var testfile = new TestFile("a.cs", code); + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath = await AddTestFile(host, testProject, testfile); + var request = new FixUsingsRequest() { FileName = filePath }; + var actual = await host.GetResponse(OmniSharpEndpoints.FixUsings, request); + Assert.Equal(expectedCode.Replace("\r\n", "\n"), actual.Buffer.Replace("\r\n", "\n")); + } + } + } + + [Fact] + public async Task Adds_Misc_Document_Which_Supports_TypeLookup() + { + const string code = @"class F$$oo {}"; + var testfile = new TestFile("a.cs", code); + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath = await AddTestFile(host, testProject, testfile); + var service = host.GetRequestHandler(OmniSharpEndpoints.TypeLookup); + var point = testfile.Content.GetPointFromPosition(); + var request = new TypeLookupRequest + { + FileName = filePath, + Line = point.Line, + Column = point.Offset, + }; + + var actual = await host.GetResponse(OmniSharpEndpoints.TypeLookup, request); + Assert.Equal("Foo", actual.Type); + } + } + } + + [Fact] + public async Task Adds_Multiple_Misc_Files_To_Same_project() + { + const string source1 = +@"class Program +{ + public static void Main(){ + A a = new A(4, $$5); + } +}"; + + const string source2 = +@"class A +{ + A(int a, int b) + { + } +}"; + var testfile1 = new TestFile("file1.cs", source1); + var testfile2 = new TestFile("file2.cs", source2); + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath1 = await AddTestFile(host, testProject, testfile1); + var filePath2 = await AddTestFile(host, testProject, testfile2); + var point = testfile1.Content.GetPointFromPosition(); + var request = new SignatureHelpRequest() + { + FileName = filePath1, + Line = point.Line, + Column = point.Offset, + Buffer = testfile1.Content.Code + }; + + var actual = await host.GetResponse(OmniSharpEndpoints.SignatureHelp, request); + Assert.Single(actual.Signatures); + Assert.Equal(1, actual.ActiveParameter); + Assert.Equal(0, actual.ActiveSignature); + Assert.Equal("A", actual.Signatures.ElementAt(0).Name); + Assert.Equal(2, actual.Signatures.ElementAt(0).Parameters.Count()); + } + } + } + + [Fact] + public async Task Handles_Misc_File_Deletion() + { + //When the file is deleted the diagnostics must not be returned + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("EmptyProject")) + { + var testfile = new TestFile("a.cs", "class C { b a = new b(); int n }"); + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var filePath = await AddTestFile(host, testProject, testfile); + var request = new CodeCheckRequest() { FileName = filePath }; + var actual = await host.GetResponse(OmniSharpEndpoints.CodeCheck, request); + Assert.Single(actual.QuickFixes); + + await WaitForFileUpdate(filePath, host, FileWatching.FileChangeType.Delete); + actual = await host.GetResponse(OmniSharpEndpoints.CodeCheck, request); + Assert.Empty(actual.QuickFixes); + } + } + } + + private async Task AddTestFile(OmniSharpTestHost host, ITestProject testProject, TestFile testfile) + { + var filePath = testProject.AddDisposableFile(testfile.FileName, testfile.Content.Text.ToString()); + await host.Workspace.BufferManager.UpdateBufferAsync(new Request() { FileName = filePath, Buffer = testfile.Content.Text.ToString() }); + return filePath; + } + + private async Task WaitForFileUpdate(string filePath, OmniSharpTestHost host, FileWatching.FileChangeType changeType = FileWatching.FileChangeType.Create) + { + var fileChangedService = host.GetRequestHandler(OmniSharpEndpoints.FilesChanged); + await fileChangedService.Handle(new[] + { + new FilesChangedRequest + { + FileName = filePath, + ChangeType = changeType + } + }); + + await Task.Delay(2000); + } + } +} From b4e2b8c717b2cd3f68a19cc9b983925b07e7e86f Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 27 Aug 2018 15:27:41 -0700 Subject: [PATCH 8/8] PR feedback --- src/OmniSharp.Roslyn/BufferManager.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/OmniSharp.Roslyn/BufferManager.cs b/src/OmniSharp.Roslyn/BufferManager.cs index fdd396afa8..9b5b2ff815 100644 --- a/src/OmniSharp.Roslyn/BufferManager.cs +++ b/src/OmniSharp.Roslyn/BufferManager.cs @@ -19,12 +19,14 @@ public class BufferManager private readonly ISet _transientDocumentIds = new HashSet(); private readonly object _lock = new object(); private readonly IFileSystemWatcher _fileSystemWatcher; + private readonly Action _onFileChanged; public BufferManager(OmniSharpWorkspace workspace, IFileSystemWatcher fileSystemWatcher) { _workspace = workspace; _workspace.WorkspaceChanged += OnWorkspaceChanged; _fileSystemWatcher = fileSystemWatcher; + _onFileChanged = OnFileChanged; } public async Task UpdateBufferAsync(Request request) @@ -131,10 +133,9 @@ private bool TryAddTransientDocument(string fileName, string fileContent) var projects = FindProjectsByFileName(fileName); if (!projects.Any()) { - //todo: deal with the deletion of the misc files if (fileName.EndsWith(".cs") && _workspace.TryAddMiscellaneousDocument(fileName, LanguageNames.CSharp) != null) { - _fileSystemWatcher.Watch(fileName, OnMiscFileChanged); + _fileSystemWatcher.Watch(fileName, OnFileChanged); return true; } @@ -171,9 +172,9 @@ private bool TryAddTransientDocument(string fileName, string fileContent) return true; } - private void OnMiscFileChanged(string filePath, FileChangeType changeType) + private void OnFileChanged(string filePath, FileChangeType changeType) { - if(changeType == FileChangeType.Unspecified && !File.Exists(filePath) || changeType == FileChangeType.Delete) + if (changeType == FileChangeType.Unspecified && !File.Exists(filePath) || changeType == FileChangeType.Delete) { _workspace.TryRemoveMiscellaneousDocument(filePath); }