Skip to content

Commit

Permalink
read only region with roundtrip emits replace injection point
Browse files Browse the repository at this point in the history
  • Loading branch information
colombod committed Jun 11, 2019
1 parent f47d088 commit fd07417
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
42 changes: 42 additions & 0 deletions MLS.Agent.Tests/Markdown/MarkdownFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,48 @@ static void MyProgram(string[] args)
output.Should().Contain($"{codeContent.HtmlEncode()}");
}

[Fact]
public async Task Should_emit_replace_injection_point_for_readonly_regions_from_source_file()
{
var expectedCode = @"Console.WriteLine(""Hello World!"");";

var codeContent = @"using System;
namespace BasicConsoleApp
{
class Program
{
static void MyProgram(string[] args)
{
#region code
Console.WriteLine(""Hello World!"");
#endregion
}
}
}".EnforceLF();

var html = await RenderHtml(
("sample.csproj", ""),
("Program.cs", codeContent),
("Readme.md",
@"```cs --source-file Program.cs --region code --editable false
using System;
```"));

var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
var code = htmlDocument.DocumentNode
.SelectSingleNode("//pre/code");

var output = code.InnerHtml.EnforceLF();

code.Attributes["data-trydotnet-mode"].Value.Should().Be("include");
code.Attributes["data-trydotnet-injection-point"].Value.Should().Be("replace");
code.ParentNode.Attributes["style"].Should().BeNull();

output.Should().Contain($"{expectedCode.HtmlEncode()}");
}

[Fact]
public async Task Should_emit_run_buttons_for_editable_blocks()
{
Expand Down
10 changes: 10 additions & 0 deletions MLS.Agent/Markdown/LocalCodeBlockAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ public override async Task AddAttributes(AnnotatedCodeBlock block)
fileName);
}

if (ReadOnlyRegionRoundtrip())
{
block.AddAttribute("data-trydotnet-injection-point", "replace");
}

bool ReadOnlyRegionRoundtrip()
{
return !Editable && !string.IsNullOrWhiteSpace(Region) && SourceFile != null && (DestinationFile == null || SourceFile.Equals(DestinationFile));
}

await base.AddAttributes(block);
}

Expand Down
6 changes: 5 additions & 1 deletion Microsoft.DotNet.Try.Protocol/BufferId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.DotNet.Try.Protocol
[JsonConverter(typeof(BufferIdConverter))]
public class BufferId
{
private const string ReplaceInjectionModifier = "[replace]";
private const string BeforeInjectionModifier = "[before]";
private const string AfterInjectionModifier = "[after]";

Expand Down Expand Up @@ -68,7 +69,10 @@ private static string RemoveInjectionModifiers(string regionName)
{
return string.IsNullOrWhiteSpace(regionName)
? regionName
: regionName.Replace(BeforeInjectionModifier, string.Empty).Replace(AfterInjectionModifier, string.Empty);
: regionName
.Replace(ReplaceInjectionModifier, string.Empty)
.Replace(BeforeInjectionModifier, string.Empty)
.Replace(AfterInjectionModifier, string.Empty);
}
public BufferId GetNormalized()
{
Expand Down

0 comments on commit fd07417

Please sign in to comment.