Skip to content

Commit cbc8d95

Browse files
Fix exception thrown by disctionary inside formatting options (#412)
1 parent fe3456c commit cbc8d95

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/Protocol/Models/FormattingOptions.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class FormattingOptions : Dictionary<string, BooleanNumberString>
1515
[JsonIgnore]
1616
public long TabSize
1717
{
18-
get => this["tabSize"].IsLong ? this["tabSize"].Long : -1;
18+
get => TryGetValue("tabSize", out var tabSize) && tabSize.IsLong ? tabSize.Long : -1;
1919
set => this["tabSize"] = value;
2020
}
2121

@@ -25,7 +25,7 @@ public long TabSize
2525
[JsonIgnore]
2626
public bool InsertSpaces
2727
{
28-
get => this["insertSpaces"].IsBool ? this["insertSpaces"].Bool : false;
28+
get => TryGetValue("insertSpaces", out var insertSpaces) && insertSpaces.IsBool && insertSpaces.Bool;
2929
set => this["insertSpaces"] = value;
3030
}
3131

@@ -37,7 +37,7 @@ public bool InsertSpaces
3737
[JsonIgnore]
3838
public bool TrimTrailingWhitespace
3939
{
40-
get => this["trimTrailingWhitespace"].IsBool ? this["trimTrailingWhitespace"].Bool : false;
40+
get => TryGetValue("trimTrailingWhitespace", out var trimTrailingWhitespace) && trimTrailingWhitespace.IsBool && trimTrailingWhitespace.Bool;
4141
set => this["trimTrailingWhitespace"] = value;
4242
}
4343

@@ -49,7 +49,7 @@ public bool TrimTrailingWhitespace
4949
[JsonIgnore]
5050
public bool InsertFinalNewline
5151
{
52-
get => this["insertFinalNewline"].IsBool ? this["insertFinalNewline"].Bool : false;
52+
get => TryGetValue("insertFinalNewline", out var insertFinalNewline) && insertFinalNewline.IsBool && insertFinalNewline.Bool;
5353
set => this["insertFinalNewline"] = value;
5454
}
5555

@@ -61,7 +61,7 @@ public bool InsertFinalNewline
6161
[JsonIgnore]
6262
public bool TrimFinalNewlines
6363
{
64-
get => this["trimFinalNewlines"].IsBool ? this["trimFinalNewlines"].Bool : false;
64+
get => TryGetValue("trimFinalNewlines", out var trimFinalNewlines) && trimFinalNewlines.IsBool && trimFinalNewlines.Bool;
6565
set => this["trimFinalNewlines"] = value;
6666
}
6767
}

test/Lsp.Tests/Models/FormattingOptionsTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using Microsoft.CodeAnalysis.CSharp.Syntax;
23
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
34
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
45
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization;
@@ -24,5 +25,27 @@ public void SimpleTest(string expected)
2425
var deresult = new Serializer(ClientVersion.Lsp3).DeserializeObject<FormattingOptions>(expected);
2526
deresult.Should().BeEquivalentTo(model);
2627
}
28+
29+
[Fact]
30+
public void Should_Not_Require_A_Value_To_Be_Defined()
31+
{
32+
var model = new FormattingOptions();
33+
model.InsertSpaces.Should().BeFalse();
34+
model.TabSize.Should().Be(-1L);
35+
model.InsertFinalNewline.Should().BeFalse();
36+
model.TrimFinalNewlines.Should().BeFalse();
37+
model.TrimTrailingWhitespace.Should().BeFalse();
38+
}
39+
40+
[Fact]
41+
public void Should_Allow_Values_To_Be_Set()
42+
{
43+
var model = new FormattingOptions();
44+
model.InsertSpaces = true;
45+
model.TabSize = 4;
46+
model.InsertFinalNewline = true;
47+
model.TrimFinalNewlines = true;
48+
model.TrimTrailingWhitespace = true;
49+
}
2750
}
2851
}

0 commit comments

Comments
 (0)