Skip to content

Commit

Permalink
Merge pull request #284 from OmniSharp/update-buffer-from-disk
Browse files Browse the repository at this point in the history
Add flag to allow update buffer to read from disk
  • Loading branch information
david-driscoll committed Aug 13, 2015
2 parents 579a068 + b9cd005 commit f101d0d
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/OmniSharp/Api/v1/Buffer/OmnisharpController.Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace OmniSharp
public partial class OmnisharpController
{
[HttpPost("updatebuffer")]
public ObjectResult UpdateBuffer(Request request)
public ObjectResult UpdateBuffer(UpdateBufferRequest request)
{
_workspace.BufferManager.UpdateBuffer(request);
return new ObjectResult(true);
}

Expand Down
9 changes: 9 additions & 0 deletions src/OmniSharp/Models/v1/UpdateBufferRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OmniSharp.Models
{
public class UpdateBufferRequest : Request
{
// Instead of updating the buffer from the editor,
// set this to allow updating from disk
public bool FromDisk { get; set; }
}
}
18 changes: 14 additions & 4 deletions src/OmniSharp/Roslyn/BufferManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BufferManager
private readonly IDictionary<string, IEnumerable<DocumentId>> _transientDocuments = new Dictionary<string, IEnumerable<DocumentId>>(StringComparer.OrdinalIgnoreCase);
private readonly ISet<DocumentId> _transientDocumentIds = new HashSet<DocumentId>();
private readonly object _lock = new object();

public BufferManager(OmnisharpWorkspace workspace)
{
_workspace = workspace;
Expand All @@ -24,23 +24,33 @@ public BufferManager(OmnisharpWorkspace workspace)

public void UpdateBuffer(Request request)
{
if (request.Buffer == null || request.FileName == null)
string buffer = request.Buffer;

var updateRequest = request as UpdateBufferRequest;

if (updateRequest != null && updateRequest.FromDisk)
{
buffer = File.ReadAllText(updateRequest.FileName);
}

if (buffer == null || request.FileName == null)
{
return;
}

var documentIds = _workspace.CurrentSolution.GetDocumentIdsWithFilePath(request.FileName);

if (!documentIds.IsEmpty)
{
var sourceText = SourceText.From(request.Buffer);
var sourceText = SourceText.From(buffer);
foreach (var documentId in documentIds)
{
_workspace.OnDocumentChanged(documentId, sourceText);
}
}
else
{
TryAddTransientDocument(request.FileName, request.Buffer);
TryAddTransientDocument(request.FileName, buffer);
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/OmniSharp.Tests/BufferManagerFacts.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OmniSharp.Models;
using Xunit;

Expand Down Expand Up @@ -52,6 +54,22 @@ public void UpdateBufferFindsProjectBasedOnPath()
}
}

[Fact]
public async Task UpdateBufferReadsFromDisk()
{
var code = "public class MyClass {}";
string fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".cs";
var workspace = TestHelpers.CreateSimpleWorkspace("", fileName);

File.WriteAllText(fileName, code);
workspace.BufferManager.UpdateBuffer(new UpdateBufferRequest { FileName = fileName, FromDisk = true });

var document = workspace.GetDocument(fileName);
var text = await document.GetTextAsync();

Assert.Equal(code, text.ToString());
}

[Fact]
public void UpdateBufferFindsProjectBasedOnNearestPath()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/FindImplementationFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class SomeClass : Base$Class {}";
private async Task<IEnumerable<ISymbol>> FindImplementations(string source)
{
var workspace = TestHelpers.CreateSimpleWorkspace(source);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var request = CreateRequest(source);
var bufferFilter = new UpdateBufferFilter(workspace);
bufferFilter.OnActionExecuting(TestHelpers.CreateActionExecutingContext(request, controller));
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/FindReferencesFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private async Task<QuickFixResponse> FindUsages(string source, bool excludeDefin
private async Task<QuickFixResponse> FindUsages(Dictionary<string, string> sources, string currentFile, bool onlyThisFile, bool excludeDefinition = false)
{
var workspace = TestHelpers.CreateSimpleWorkspace(sources);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var request = CreateRequest(sources[currentFile], currentFile, excludeDefinition);
request.OnlyThisFile = onlyThisFile;
var bufferFilter = new UpdateBufferFilter(workspace);
Expand Down
4 changes: 2 additions & 2 deletions tests/OmniSharp.Tests/FindSymbolsFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ private string NestedMethod() {}
private async Task<QuickFixResponse> FindSymbols(string source)
{
var workspace = TestHelpers.CreateSimpleWorkspace(source);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
return await controller.FindSymbols();
}

private async Task<QuickFixResponse> FindSymbolsWithFilter(string source, string filter)
{
var workspace = TestHelpers.CreateSimpleWorkspace(source);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var request = new FindSymbolsRequest { Filter = filter };
return await controller.FindSymbols(request);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/OmniSharp.Tests/GoToFileFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void ReturnsAListOfAllWorkspaceFiles()
var workspace = TestHelpers.CreateSimpleWorkspace(new Dictionary<string, string> {
{ "foo.cs", source1 }, { "bar.cs", source2}
});
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var response = controller.GoToFile(new Request());

Assert.Equal(2, response.QuickFixes.Count());
Expand All @@ -29,7 +29,7 @@ public void ReturnsAListOfAllWorkspaceFiles()
public void ReturnsEmptyResponseForEmptyWorskpace()
{
var workspace = TestHelpers.CreateSimpleWorkspace(new Dictionary<string, string>());
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var response = controller.GoToFile(new Request());

Assert.Equal(0, response.QuickFixes.Count());
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/GoToRegionFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task DoesNotFindRegionsInFileWithoutRegions()
private async Task<QuickFixResponse> FindRegions(string source)
{
var workspace = TestHelpers.CreateSimpleWorkspace(source);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var request = CreateRequest(source);
var bufferFilter = new UpdateBufferFilter(workspace);
bufferFilter.OnActionExecuting(TestHelpers.CreateActionExecutingContext(request, controller));
Expand Down
16 changes: 8 additions & 8 deletions tests/OmniSharp.Tests/HighlightFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class C1 { int n = true; }
{ "a.cs", code }
});

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var regions = await controller.Highlight(new HighlightRequest() { FileName = "a.cs", Lines = new[] { 4 } });

AssertSyntax(regions.Highlights, code, 3,
Expand Down Expand Up @@ -54,7 +54,7 @@ class C1 { int n = true; }
{ "a.cs", code }
});

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var regions = await controller.Highlight(new HighlightRequest() { FileName = "a.cs" });

AssertSyntax(regions.Highlights, code, 0,
Expand Down Expand Up @@ -88,7 +88,7 @@ class C1
{ "a.cs", code }
});

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var regions = await controller.Highlight(new HighlightRequest() { FileName = "a.cs" });

AssertSyntax(regions.Highlights, code, 0,
Expand Down Expand Up @@ -160,7 +160,7 @@ class C1 { int n = true; }
{ "a.cs", code }
});

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var regions = await controller.Highlight(new HighlightRequest() { FileName = "a.cs", ExcludeClassifications = new [] { HighlightClassification.Keyword } });

Assert.DoesNotContain(regions.Highlights, x => x.Kind == "keyword");
Expand All @@ -181,7 +181,7 @@ class C1 { int n = true; }
{ "a.cs", code }
});

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var regions = await controller.Highlight(new HighlightRequest() { FileName = "a.cs", ExcludeClassifications = new [] { HighlightClassification.Punctuation } });

Assert.DoesNotContain(regions.Highlights, x => x.Kind == "punctuation");
Expand All @@ -202,7 +202,7 @@ class C1 { int n = true; }
{ "a.cs", code }
});

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var regions = await controller.Highlight(new HighlightRequest() { FileName = "a.cs", ExcludeClassifications = new [] { HighlightClassification.Operator } });

Assert.DoesNotContain(regions.Highlights, x => x.Kind == "operator");
Expand All @@ -223,7 +223,7 @@ class C1 { int n = true; }
{ "a.cs", code }
});

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var regions = await controller.Highlight(new HighlightRequest() { FileName = "a.cs", ExcludeClassifications = new [] { HighlightClassification.Identifier } });

Assert.DoesNotContain(regions.Highlights, x => x.Kind == "identifier");
Expand All @@ -244,7 +244,7 @@ class C1 { int n = true; }
{ "a.cs", code }
});

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var regions = await controller.Highlight(new HighlightRequest() { FileName = "a.cs", ExcludeClassifications = new [] { HighlightClassification.Name } });

Assert.DoesNotContain(regions.Highlights, x => x.Kind.EndsWith(" name"));
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/IntellisenseFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private void ContainsCompletions(IEnumerable<string> completions, params string[
private async Task<IEnumerable<AutoCompleteResponse>> FindCompletionsAsync(string source, AutoCompleteRequest request = null)
{
var workspace = TestHelpers.CreateSimpleWorkspace(source);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());

if (request == null)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/NavigateUpDownFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ private async Task<NavigateResponse> SendRequest(OmnisharpWorkspace workspace, s
{
var initialCursorLineColumn = TestHelpers.GetLineAndColumnFromDollar(TestHelpers.RemovePercentMarker(fileContent));
var fileContentNoDollarMarker = TestHelpers.RemoveDollarMarker(fileContent);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var request = new Request
{
Line = initialCursorLineColumn.Line,
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/OmnisharpControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class OmnisharpControllerFacts
private void CreateSimpleWorkspace(out OmnisharpWorkspace workspace, out OmnisharpController controller, out DocumentInfo document, string filename, string contents)
{
workspace = new OmnisharpWorkspace();
controller = new OmnisharpController(workspace, null);
controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());

var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(),
"ProjectNameVal", "AssemblyNameVal", LanguageNames.CSharp);
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/RenameFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RenameFacts
private async Task<RenameResponse> SendRequest(OmnisharpWorkspace workspace, string renameTo, string filename, string fileContent, bool wantsTextChanges = false)
{
var lineColumn = TestHelpers.GetLineAndColumnFromDollar(fileContent);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var request = new RenameRequest
{
Line = lineColumn.Line,
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/SignatureHelpFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ private async Task<SignatureHelp> GetSignatureHelp(string source)
};

var workspace = TestHelpers.CreateSimpleWorkspace(source);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
return await controller.GetSignatureHelp(request);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/OmniSharp.Tests/SnippetFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private void ContainsSnippet(string expected, IEnumerable<string> completions)
private async Task<IEnumerable<string>> FindCompletionsAsync(string source)
{
var workspace = TestHelpers.CreateSimpleWorkspace(source);
var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var request = CreateRequest(source);
var response = await controller.AutoComplete(request);
var completions = response as IEnumerable<AutoCompleteResponse>;
Expand Down
1 change: 1 addition & 0 deletions tests/OmniSharp.Tests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Models;

Expand Down
4 changes: 2 additions & 2 deletions tests/OmniSharp.Tests/TypeLookupFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task OmitsNamespaceForNonRegularCSharpSyntax()

var workspace = TestHelpers.CreateCsxWorkspace(source1);

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var response = await controller.TypeLookup(new TypeLookupRequest { FileName = "dummy.csx", Line = 1, Column = 8 });

Assert.Equal("Foo", response.Type);
Expand All @@ -28,7 +28,7 @@ class Foo {}

var workspace = TestHelpers.CreateSimpleWorkspace(source1);

var controller = new OmnisharpController(workspace, null);
var controller = new OmnisharpController(workspace, new FakeOmniSharpOptions());
var response = await controller.TypeLookup(new TypeLookupRequest { FileName = "dummy.cs", Line = 2, Column = 20 });

Assert.Equal("Bar.Foo", response.Type);
Expand Down

0 comments on commit f101d0d

Please sign in to comment.