Skip to content

Commit

Permalink
Define schema help model for elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
tintoy committed Apr 14, 2018
1 parent 43ffa7d commit d37dc65
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/LanguageServer.Common/Help/ElementHelp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace MSBuildProjectTools.LanguageServer.Help
{
/// <summary>
/// Help information for an MSBuild element.
/// </summary>
public class ElementHelp
{
/// <summary>
/// The property description.
/// </summary>
[JsonProperty("description")]
public string Description { get; set; }

/// <summary>
/// Help link for the element (if any).
/// </summary>
[JsonProperty("help")]
public string HelpLink { get; set; }

/// <summary>
/// Load help property help from JSON.
/// </summary>
/// <param name="json">
/// A <see cref="JsonReader"/> representing the JSON ("PropertyName": { "description": "PropertyDescription" }).
/// </param>
/// <returns>
/// A sorted dictionary of help items, keyed by property name.
/// </returns>
public static SortedDictionary<string, ElementHelp> FromJson(JsonReader json)
{
if (json == null)
throw new ArgumentNullException(nameof(json));

return new JsonSerializer().Deserialize<SortedDictionary<string, ElementHelp>>(json);
}
}
}
40 changes: 40 additions & 0 deletions src/LanguageServer.SemanticModel.MSBuild/MSBuildSchemaHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ static MSBuildSchemaHelp()
HelpDirectory = new DirectoryInfo(
Path.Combine(extensionDirectory, "help")
);
ElementHelpFile = new FileInfo(
Path.Combine(HelpDirectory.FullName, "elements.json")
);
PropertyHelpFile = new FileInfo(
Path.Combine(HelpDirectory.FullName, "properties.json")
);
Expand All @@ -35,6 +38,12 @@ static MSBuildSchemaHelp()
Path.Combine(HelpDirectory.FullName, "tasks.json")
);

using (StreamReader input = ElementHelpFile.OpenText())
using (JsonTextReader json = new JsonTextReader(input))
{
ElementHelp = Help.ElementHelp.FromJson(json);
}

using (StreamReader input = PropertyHelpFile.OpenText())
using (JsonTextReader json = new JsonTextReader(input))
{
Expand All @@ -55,6 +64,11 @@ static MSBuildSchemaHelp()
}
}

/// <summary>
/// Help for MSBuild elements.
/// </summary>
static SortedDictionary<string, ElementHelp> ElementHelp { get; }

/// <summary>
/// Help for MSBuild properties.
/// </summary>
Expand All @@ -80,6 +94,11 @@ static MSBuildSchemaHelp()
/// </summary>
public static DirectoryInfo HelpDirectory { get; }

/// <summary>
/// The file that stores help for well-known MSBuild elements.
/// </summary>
public static FileInfo ElementHelpFile { get; }

/// <summary>
/// The file that stores help for well-known MSBuild properties.
/// </summary>
Expand Down Expand Up @@ -159,6 +178,27 @@ public static string ForElement(string elementName)
return null;
}

/// <summary>
/// Get a help link (if available) for the specified element.
/// </summary>
/// <param name="elementName">
/// The element name.
/// </param>
/// <returns>
/// The element help link, or <c>null</c> if no link is available for it.
/// </returns>
public static string HelpLinkForElement(string elementName)
{
if (String.IsNullOrWhiteSpace(elementName))
throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'elementName'.", nameof(elementName));

string helpKey = elementName;
if (ElementHelp.TryGetValue(helpKey, out ElementHelp help))
return help.HelpLink;

return null;
}

/// <summary>
/// Get help content for the specified attribute.
/// </summary>
Expand Down

0 comments on commit d37dc65

Please sign in to comment.